// Code generated by modernc.org/undup from the per-target sqlite_*.go files; DO NOT EDIT.

//go:build (darwin && amd64) || (darwin && arm64) || (freebsd && 386) || (freebsd && amd64) || (freebsd && arm) || (freebsd && arm64) || (linux && 386) || (linux && amd64) || (linux && arm) || (linux && arm64) || (linux && loong64) || (linux && ppc64le) || (linux && riscv64) || (netbsd && amd64) || (openbsd && amd64) || (openbsd && arm64)

package sqlite3

import (
	"unsafe"

	"modernc.org/libc"
)

const BYTE_ORDER = 1234

func Xsqlite3_bind_text16(tls *libc.TLS, pStmt uintptr, i int32, zData uintptr, n int32, __ccgo_fp_xDel uintptr) (r int32) {
	return _bindText(tls, pStmt, i, zData, libc.Int64FromUint64(libc.Uint64FromInt32(n) & ^libc.Uint64FromInt32(1)), __ccgo_fp_xDel, uint8(SQLITE_UTF16LE))
}

func Xsqlite3_bind_text64(tls *libc.TLS, pStmt uintptr, i int32, zData uintptr, nData Tsqlite3_uint64, __ccgo_fp_xDel uintptr, enc uint8) (r int32) {
	if libc.Int32FromUint8(enc) != int32(SQLITE_UTF8) && libc.Int32FromUint8(enc) != int32(SQLITE_UTF8_ZT) {
		if libc.Int32FromUint8(enc) == int32(SQLITE_UTF16) {
			enc = uint8(SQLITE_UTF16LE)
		}
		nData = nData & ^libc.Uint64FromInt32(1)
	}
	return _bindText(tls, pStmt, i, zData, libc.Int64FromUint64(nData), __ccgo_fp_xDel, enc)
}

// C documentation
//
//	/*
//	** Register a new collation sequence with the database handle db.
//	*/
func Xsqlite3_create_collation16(tls *libc.TLS, db uintptr, zName uintptr, enc int32, pCtx uintptr, __ccgo_fp_xCompare uintptr) (r int32) {
	var rc int32
	var zName8 uintptr
	_, _ = rc, zName8
	rc = SQLITE_OK
	Xsqlite3_mutex_enter(tls, (*Tsqlite3)(unsafe.Pointer(db)).Fmutex)
	zName8 = _sqlite3Utf16to8(tls, db, zName, -int32(1), uint8(SQLITE_UTF16LE))
	if zName8 != 0 {
		rc = _createCollation(tls, db, zName8, libc.Uint8FromInt32(enc), pCtx, __ccgo_fp_xCompare, uintptr(0))
		_sqlite3DbFree(tls, db, zName8)
	}
	rc = _sqlite3ApiExit(tls, db, rc)
	Xsqlite3_mutex_leave(tls, (*Tsqlite3)(unsafe.Pointer(db)).Fmutex)
	return rc
}

func Xsqlite3_result_text16(tls *libc.TLS, pCtx uintptr, z uintptr, n int32, __ccgo_fp_xDel uintptr) {
	_setResultStrOrError(tls, pCtx, z, libc.Int32FromUint64(libc.Uint64FromInt32(n) & ^libc.Uint64FromInt32(1)), uint8(SQLITE_UTF16LE), __ccgo_fp_xDel)
}

func Xsqlite3_result_text64(tls *libc.TLS, pCtx uintptr, z uintptr, n Tsqlite3_uint64, __ccgo_fp_xDel uintptr, enc uint8) {
	if libc.Int32FromUint8(enc) != int32(SQLITE_UTF8) && libc.Int32FromUint8(enc) != int32(SQLITE_UTF8_ZT) {
		if libc.Int32FromUint8(enc) == int32(SQLITE_UTF16) {
			enc = uint8(SQLITE_UTF16LE)
		}
		n = n & ^libc.Uint64FromInt32(1)
	}
	if n > uint64(0x7fffffff) {
		_invokeValueDestructor(tls, z, __ccgo_fp_xDel, pCtx)
	} else {
		_setResultStrOrError(tls, pCtx, z, libc.Int32FromUint64(n), enc, __ccgo_fp_xDel)
		_sqlite3VdbeMemZeroTerminateIfAble(tls, (*Tsqlite3_context)(unsafe.Pointer(pCtx)).FpOut)
	}
}

// C documentation
//
//	/*
//	** Do additional sanity check after btreeInitPage() if
//	** PRAGMA cell_size_check=ON
//	*/
func _btreeCellSizeCheck(tls *libc.TLS, pPage uintptr) (r int32) {
	var cellOffset, i, iCellFirst, iCellLast, pc, sz, usableSize int32
	var data uintptr
	_, _, _, _, _, _, _, _ = cellOffset, data, i, iCellFirst, iCellLast, pc, sz, usableSize /* Start of cell content area */
	iCellFirst = libc.Int32FromUint16((*TMemPage)(unsafe.Pointer(pPage)).FcellOffset) + int32(2)*libc.Int32FromUint16((*TMemPage)(unsafe.Pointer(pPage)).FnCell)
	usableSize = libc.Int32FromUint32((*TBtShared)(unsafe.Pointer((*TMemPage)(unsafe.Pointer(pPage)).FpBt)).FusableSize)
	iCellLast = usableSize - int32(4)
	data = (*TMemPage)(unsafe.Pointer(pPage)).FaData
	cellOffset = libc.Int32FromUint16((*TMemPage)(unsafe.Pointer(pPage)).FcellOffset)
	if !((*TMemPage)(unsafe.Pointer(pPage)).Fleaf != 0) {
		iCellLast = iCellLast - 1
	}
	i = 0
	for {
		if !(i < libc.Int32FromUint16((*TMemPage)(unsafe.Pointer(pPage)).FnCell)) {
			break
		}
		pc = libc.Int32FromUint8(**(**Tu8)(__ccgo_up(data + uintptr(cellOffset+i*int32(2)))))<<int32(8) | libc.Int32FromUint8(**(**Tu8)(__ccgo_up(data + uintptr(cellOffset+i*int32(2)) + 1)))
		if pc < iCellFirst || pc > iCellLast {
			return _sqlite3CorruptError(tls, int32(75424))
		}
		sz = libc.Int32FromUint16((*(*func(*libc.TLS, uintptr, uintptr) Tu16)(unsafe.Pointer(&struct{ uintptr }{(*TMemPage)(unsafe.Pointer(pPage)).FxCellSize})))(tls, pPage, data+uintptr(pc)))
		if pc+sz > usableSize {
			return _sqlite3CorruptError(tls, int32(75429))
		}
		goto _1
	_1:
		;
		i = i + 1
	}
	return SQLITE_OK
}

func _btreeParseCell(tls *libc.TLS, pPage uintptr, iCell int32, pInfo uintptr) {
	(*(*func(*libc.TLS, uintptr, uintptr, uintptr))(unsafe.Pointer(&struct{ uintptr }{(*TMemPage)(unsafe.Pointer(pPage)).FxParseCell})))(tls, pPage, (*TMemPage)(unsafe.Pointer(pPage)).FaData+uintptr(libc.Int32FromUint16((*TMemPage)(unsafe.Pointer(pPage)).FmaskPage)&(libc.Int32FromUint8(**(**Tu8)(__ccgo_up((*TMemPage)(unsafe.Pointer(pPage)).FaCellIdx + uintptr(int32(2)*iCell))))<<libc.Int32FromInt32(8)|libc.Int32FromUint8(**(**Tu8)(__ccgo_up((*TMemPage)(unsafe.Pointer(pPage)).FaCellIdx + uintptr(int32(2)*iCell) + 1))))), pInfo)
}

// C documentation
//
//	/*
//	** Step the cursor to the back to the previous entry in the database.
//	** Return values:
//	**
//	**     SQLITE_OK     success
//	**     SQLITE_DONE   the cursor is already on the first element of the table
//	**     otherwise     some kind of error occurred
//	**
//	** The main entry point is sqlite3BtreePrevious().  That routine is optimized
//	** for the common case of merely decrementing the cell counter BtCursor.aiIdx
//	** to the previous cell on the current page.  The (slower) btreePrevious()
//	** helper routine is called when it is necessary to move to a different page
//	** or to restore the cursor.
//	**
//	** If bit 0x01 of the F argument to sqlite3BtreePrevious(C,F) is 1, then
//	** the cursor corresponds to an SQL index and this routine could have been
//	** skipped if the SQL index had been a unique index.  The F argument is a
//	** hint to the implement.  The native SQLite btree implementation does not
//	** use this hint, but COMDB2 does.
//	*/
func _btreePrevious(tls *libc.TLS, pCur uintptr) (r int32) {
	var idx, rc, v1 int32
	var pPage uintptr
	_, _, _, _ = idx, pPage, rc, v1
	if libc.Int32FromUint8((*TBtCursor)(unsafe.Pointer(pCur)).FeState) != CURSOR_VALID {
		if libc.Int32FromUint8((*TBtCursor)(unsafe.Pointer(pCur)).FeState) >= int32(CURSOR_REQUIRESEEK) {
			v1 = _btreeRestoreCursorPosition(tls, pCur)
		} else {
			v1 = SQLITE_OK
		}
		rc = v1
		if rc != SQLITE_OK {
			return rc
		}
		if int32(CURSOR_INVALID) == libc.Int32FromUint8((*TBtCursor)(unsafe.Pointer(pCur)).FeState) {
			return int32(SQLITE_DONE)
		}
		if int32(CURSOR_SKIPNEXT) == libc.Int32FromUint8((*TBtCursor)(unsafe.Pointer(pCur)).FeState) {
			(*TBtCursor)(unsafe.Pointer(pCur)).FeState = uint8(CURSOR_VALID)
			if (*TBtCursor)(unsafe.Pointer(pCur)).FskipNext < 0 {
				return SQLITE_OK
			}
		}
	}
	pPage = (*TBtCursor)(unsafe.Pointer(pCur)).FpPage
	if _sqlite3FaultSim(tls, int32(412)) != 0 {
		(*TMemPage)(unsafe.Pointer(pPage)).FisInit = uint8(0)
	}
	if !((*TMemPage)(unsafe.Pointer(pPage)).FisInit != 0) {
		return _sqlite3CorruptError(tls, int32(79674))
	}
	if !((*TMemPage)(unsafe.Pointer(pPage)).Fleaf != 0) {
		idx = libc.Int32FromUint16((*TBtCursor)(unsafe.Pointer(pCur)).Fix)
		rc = _moveToChild(tls, pCur, _sqlite3Get4byte(tls, (*TMemPage)(unsafe.Pointer(pPage)).FaData+uintptr(libc.Int32FromUint16((*TMemPage)(unsafe.Pointer(pPage)).FmaskPage)&(libc.Int32FromUint8(**(**Tu8)(__ccgo_up((*TMemPage)(unsafe.Pointer(pPage)).FaCellIdx + uintptr(int32(2)*idx))))<<libc.Int32FromInt32(8)|libc.Int32FromUint8(**(**Tu8)(__ccgo_up((*TMemPage)(unsafe.Pointer(pPage)).FaCellIdx + uintptr(int32(2)*idx) + 1)))))))
		if rc != 0 {
			return rc
		}
		rc = _moveToRightmost(tls, pCur)
	} else {
		for libc.Int32FromUint16((*TBtCursor)(unsafe.Pointer(pCur)).Fix) == 0 {
			if int32((*TBtCursor)(unsafe.Pointer(pCur)).FiPage) == 0 {
				(*TBtCursor)(unsafe.Pointer(pCur)).FeState = uint8(CURSOR_INVALID)
				return int32(SQLITE_DONE)
			}
			_moveToParent(tls, pCur)
		}
		(*TBtCursor)(unsafe.Pointer(pCur)).Fix = (*TBtCursor)(unsafe.Pointer(pCur)).Fix - 1
		pPage = (*TBtCursor)(unsafe.Pointer(pCur)).FpPage
		if (*TMemPage)(unsafe.Pointer(pPage)).FintKey != 0 && !((*TMemPage)(unsafe.Pointer(pPage)).Fleaf != 0) {
			rc = _sqlite3BtreePrevious(tls, pCur, 0)
		} else {
			rc = SQLITE_OK
		}
	}
	return rc
}

// C documentation
//
//	/*
//	** Invoke the 'collation needed' callback to request a collation sequence
//	** in the encoding enc of name zName, length nName.
//	*/
func _callCollNeeded(tls *libc.TLS, db uintptr, enc int32, zName uintptr) {
	var pTmp, zExternal, zExternal1 uintptr
	_, _, _ = pTmp, zExternal, zExternal1
	if (*Tsqlite3)(unsafe.Pointer(db)).FxCollNeeded != 0 {
		zExternal = _sqlite3DbStrDup(tls, db, zName)
		if !(zExternal != 0) {
			return
		}
		(*(*func(*libc.TLS, uintptr, uintptr, int32, uintptr))(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3)(unsafe.Pointer(db)).FxCollNeeded})))(tls, (*Tsqlite3)(unsafe.Pointer(db)).FpCollNeededArg, db, enc, zExternal)
		_sqlite3DbFree(tls, db, zExternal)
	}
	if (*Tsqlite3)(unsafe.Pointer(db)).FxCollNeeded16 != 0 {
		pTmp = _sqlite3ValueNew(tls, db)
		_sqlite3ValueSetStr(tls, pTmp, -int32(1), zName, uint8(SQLITE_UTF8), libc.UintptrFromInt32(0))
		zExternal1 = _sqlite3ValueText(tls, pTmp, uint8(SQLITE_UTF16LE))
		if zExternal1 != 0 {
			(*(*func(*libc.TLS, uintptr, uintptr, int32, uintptr))(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3)(unsafe.Pointer(db)).FxCollNeeded16})))(tls, (*Tsqlite3)(unsafe.Pointer(db)).FpCollNeededArg, db, libc.Int32FromUint8((*Tsqlite3)(unsafe.Pointer(db)).Fenc), zExternal1)
		}
		_sqlite3ValueFree(tls, pTmp)
	}
}

// C documentation
//
//	/*
//	** Compare the "idx"-th cell on the page pPage against the key
//	** pointing to by pIdxKey using xRecordCompare.  Return negative or
//	** zero if the cell is less than or equal pIdxKey.  Return positive
//	** if unknown.
//	**
//	**    Return value negative:     Cell at pCur[idx] less than pIdxKey
//	**
//	**    Return value is zero:      Cell at pCur[idx] equals pIdxKey
//	**
//	**    Return value positive:     Nothing is known about the relationship
//	**                               of the cell at pCur[idx] and pIdxKey.
//	**
//	** This routine is part of an optimization.  It is always safe to return
//	** a positive value as that will cause the optimization to be skipped.
//	*/
func _indexCellCompare(tls *libc.TLS, pPage uintptr, idx int32, pIdxKey uintptr, __ccgo_fp_xRecordCompare TRecordCompare) (r int32) {
	var c, nCell, v1 int32
	var pCell uintptr
	var v2 bool
	_, _, _, _, _ = c, nCell, pCell, v1, v2 /* Size of the pCell cell in bytes */
	pCell = (*TMemPage)(unsafe.Pointer(pPage)).FaDataOfst + uintptr(libc.Int32FromUint16((*TMemPage)(unsafe.Pointer(pPage)).FmaskPage)&(libc.Int32FromUint8(**(**Tu8)(__ccgo_up((*TMemPage)(unsafe.Pointer(pPage)).FaCellIdx + uintptr(int32(2)*idx))))<<libc.Int32FromInt32(8)|libc.Int32FromUint8(**(**Tu8)(__ccgo_up((*TMemPage)(unsafe.Pointer(pPage)).FaCellIdx + uintptr(int32(2)*idx) + 1)))))
	nCell = libc.Int32FromUint8(**(**Tu8)(__ccgo_up(pCell)))
	if nCell <= libc.Int32FromUint8((*TMemPage)(unsafe.Pointer(pPage)).Fmax1bytePayload) {
		/* This branch runs if the record-size field of the cell is a
		 ** single byte varint and the record fits entirely on the main
		 ** b-tree page.  */
		if pCell+uintptr(nCell) >= (*TMemPage)(unsafe.Pointer(pPage)).FaDataEnd {
			return int32(99)
		}
		c = (*(*func(*libc.TLS, int32, uintptr, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{__ccgo_fp_xRecordCompare})))(tls, nCell, pCell+1, pIdxKey)
	} else {
		if v2 = !(libc.Int32FromUint8(**(**Tu8)(__ccgo_up(pCell + 1)))&libc.Int32FromInt32(0x80) != 0); v2 {
			v1 = nCell&libc.Int32FromInt32(0x7f)<<libc.Int32FromInt32(7) + libc.Int32FromUint8(**(**Tu8)(__ccgo_up(pCell + 1)))
			nCell = v1
		}
		if v2 && v1 <= libc.Int32FromUint16((*TMemPage)(unsafe.Pointer(pPage)).FmaxLocal) {
			/* The record-size field is a 2 byte varint and the record
			 ** fits entirely on the main b-tree page.  */
			if pCell+uintptr(nCell) >= (*TMemPage)(unsafe.Pointer(pPage)).FaDataEnd {
				return int32(99)
			}
			c = (*(*func(*libc.TLS, int32, uintptr, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{__ccgo_fp_xRecordCompare})))(tls, nCell, pCell+2, pIdxKey)
		} else {
			/* If the record extends into overflow pages, do not attempt
			 ** the optimization. */
			c = int32(99)
		}
	}
	return c
}

// C documentation
//
//	/*
//	** Somewhere on pPage is a pointer to page iFrom.  Modify this pointer so
//	** that it points to iTo. Parameter eType describes the type of pointer to
//	** be modified, as  follows:
//	**
//	** PTRMAP_BTREE:     pPage is a btree-page. The pointer points at a child
//	**                   page of pPage.
//	**
//	** PTRMAP_OVERFLOW1: pPage is a btree-page. The pointer points at an overflow
//	**                   page pointed to by one of the cells on pPage.
//	**
//	** PTRMAP_OVERFLOW2: pPage is an overflow-page. The pointer points at the next
//	**                   overflow page in the list.
//	*/
func _modifyPagePointer(tls *libc.TLS, pPage uintptr, iFrom TPgno, iTo TPgno, eType Tu8) (r int32) {
	bp := tls.Alloc(32)
	defer tls.Free(32)
	var i, nCell, rc, v1 int32
	var pCell uintptr
	var _ /* info at bp+0 */ TCellInfo
	_, _, _, _, _ = i, nCell, pCell, rc, v1
	if libc.Int32FromUint8(eType) == int32(PTRMAP_OVERFLOW2) {
		/* The pointer is always the first 4 bytes of the page in this case.  */
		if _sqlite3Get4byte(tls, (*TMemPage)(unsafe.Pointer(pPage)).FaData) != iFrom {
			return _sqlite3CorruptError(tls, int32(77112))
		}
		_sqlite3Put4byte(tls, (*TMemPage)(unsafe.Pointer(pPage)).FaData, iTo)
	} else {
		if (*TMemPage)(unsafe.Pointer(pPage)).FisInit != 0 {
			v1 = SQLITE_OK
		} else {
			v1 = _btreeInitPage(tls, pPage)
		}
		rc = v1
		if rc != 0 {
			return rc
		}
		nCell = libc.Int32FromUint16((*TMemPage)(unsafe.Pointer(pPage)).FnCell)
		i = 0
		for {
			if !(i < nCell) {
				break
			}
			pCell = (*TMemPage)(unsafe.Pointer(pPage)).FaData + uintptr(libc.Int32FromUint16((*TMemPage)(unsafe.Pointer(pPage)).FmaskPage)&(libc.Int32FromUint8(**(**Tu8)(__ccgo_up((*TMemPage)(unsafe.Pointer(pPage)).FaCellIdx + uintptr(int32(2)*i))))<<libc.Int32FromInt32(8)|libc.Int32FromUint8(**(**Tu8)(__ccgo_up((*TMemPage)(unsafe.Pointer(pPage)).FaCellIdx + uintptr(int32(2)*i) + 1)))))
			if libc.Int32FromUint8(eType) == int32(PTRMAP_OVERFLOW1) {
				(*(*func(*libc.TLS, uintptr, uintptr, uintptr))(unsafe.Pointer(&struct{ uintptr }{(*TMemPage)(unsafe.Pointer(pPage)).FxParseCell})))(tls, pPage, pCell, bp)
				if uint32((**(**TCellInfo)(__ccgo_up(bp))).FnLocal) < (**(**TCellInfo)(__ccgo_up(bp))).FnPayload {
					if pCell+uintptr((**(**TCellInfo)(__ccgo_up(bp))).FnSize) > (*TMemPage)(unsafe.Pointer(pPage)).FaData+uintptr((*TBtShared)(unsafe.Pointer((*TMemPage)(unsafe.Pointer(pPage)).FpBt)).FusableSize) {
						return _sqlite3CorruptError(tls, int32(77131))
					}
					if iFrom == _sqlite3Get4byte(tls, pCell+uintptr((**(**TCellInfo)(__ccgo_up(bp))).FnSize)-uintptr(4)) {
						_sqlite3Put4byte(tls, pCell+uintptr((**(**TCellInfo)(__ccgo_up(bp))).FnSize)-uintptr(4), iTo)
						break
					}
				}
			} else {
				if pCell+uintptr(4) > (*TMemPage)(unsafe.Pointer(pPage)).FaData+uintptr((*TBtShared)(unsafe.Pointer((*TMemPage)(unsafe.Pointer(pPage)).FpBt)).FusableSize) {
					return _sqlite3CorruptError(tls, int32(77140))
				}
				if _sqlite3Get4byte(tls, pCell) == iFrom {
					_sqlite3Put4byte(tls, pCell, iTo)
					break
				}
			}
			goto _2
		_2:
			;
			i = i + 1
		}
		if i == nCell {
			if libc.Int32FromUint8(eType) != int32(PTRMAP_BTREE) || _sqlite3Get4byte(tls, (*TMemPage)(unsafe.Pointer(pPage)).FaData+uintptr(libc.Int32FromUint8((*TMemPage)(unsafe.Pointer(pPage)).FhdrOffset)+int32(8))) != iFrom {
				return _sqlite3CorruptError(tls, int32(77152))
			}
			_sqlite3Put4byte(tls, (*TMemPage)(unsafe.Pointer(pPage)).FaData+uintptr(libc.Int32FromUint8((*TMemPage)(unsafe.Pointer(pPage)).FhdrOffset)+int32(8)), iTo)
		}
	}
	return SQLITE_OK
}

// C documentation
//
//	/*
//	** Move the cursor down to the left-most leaf entry beneath the
//	** entry to which it is currently pointing.
//	**
//	** The left-most leaf is the one with the smallest key - the first
//	** in ascending order.
//	*/
func _moveToLeftmost(tls *libc.TLS, pCur uintptr) (r int32) {
	var pPage, v1 uintptr
	var pgno TPgno
	var rc int32
	var v2 bool
	_, _, _, _, _ = pPage, pgno, rc, v1, v2
	rc = SQLITE_OK
	for {
		if v2 = rc == SQLITE_OK; v2 {
			v1 = (*TBtCursor)(unsafe.Pointer(pCur)).FpPage
			pPage = v1
		}
		if !(v2 && !((*TMemPage)(unsafe.Pointer(v1)).Fleaf != 0)) {
			break
		}
		pgno = _sqlite3Get4byte(tls, (*TMemPage)(unsafe.Pointer(pPage)).FaData+uintptr(libc.Int32FromUint16((*TMemPage)(unsafe.Pointer(pPage)).FmaskPage)&(libc.Int32FromUint8(**(**Tu8)(__ccgo_up((*TMemPage)(unsafe.Pointer(pPage)).FaCellIdx + uintptr(int32(2)*libc.Int32FromUint16((*TBtCursor)(unsafe.Pointer(pCur)).Fix)))))<<libc.Int32FromInt32(8)|libc.Int32FromUint8(**(**Tu8)(__ccgo_up((*TMemPage)(unsafe.Pointer(pPage)).FaCellIdx + uintptr(int32(2)*libc.Int32FromUint16((*TBtCursor)(unsafe.Pointer(pCur)).Fix)) + 1))))))
		rc = _moveToChild(tls, pCur, pgno)
	}
	return rc
}

func _readInt64(tls *libc.TLS, p uintptr) (r Ti64) {
	return libc.Int64FromUint64(uint64(**(**Tu8)(__ccgo_up(p)))<<libc.Int32FromInt32(56) + uint64(**(**Tu8)(__ccgo_up(p + 1)))<<libc.Int32FromInt32(48) + uint64(**(**Tu8)(__ccgo_up(p + 2)))<<libc.Int32FromInt32(40) + uint64(**(**Tu8)(__ccgo_up(p + 3)))<<libc.Int32FromInt32(32) + uint64(**(**Tu8)(__ccgo_up(p + 4)))<<libc.Int32FromInt32(24) + uint64(**(**Tu8)(__ccgo_up(p + 5)))<<libc.Int32FromInt32(16) + uint64(**(**Tu8)(__ccgo_up(p + 6)))<<libc.Int32FromInt32(8) + uint64(**(**Tu8)(__ccgo_up(p + 7)))<<libc.Int32FromInt32(0))
}

// C documentation
//
//	/*
//	** Set the pointer-map entries for all children of page pPage. Also, if
//	** pPage contains cells that point to overflow pages, set the pointer
//	** map entries for the overflow pages as well.
//	*/
func _setChildPtrmaps(tls *libc.TLS, pPage uintptr) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var childPgno, childPgno1, pgno TPgno
	var i, nCell, v1 int32
	var pBt, pCell uintptr
	var _ /* rc at bp+0 */ int32
	_, _, _, _, _, _, _, _ = childPgno, childPgno1, i, nCell, pBt, pCell, pgno, v1 /* Return code */
	pBt = (*TMemPage)(unsafe.Pointer(pPage)).FpBt
	pgno = (*TMemPage)(unsafe.Pointer(pPage)).Fpgno
	if (*TMemPage)(unsafe.Pointer(pPage)).FisInit != 0 {
		v1 = SQLITE_OK
	} else {
		v1 = _btreeInitPage(tls, pPage)
	}
	**(**int32)(__ccgo_up(bp)) = v1
	if **(**int32)(__ccgo_up(bp)) != SQLITE_OK {
		return **(**int32)(__ccgo_up(bp))
	}
	nCell = libc.Int32FromUint16((*TMemPage)(unsafe.Pointer(pPage)).FnCell)
	i = 0
	for {
		if !(i < nCell) {
			break
		}
		pCell = (*TMemPage)(unsafe.Pointer(pPage)).FaData + uintptr(libc.Int32FromUint16((*TMemPage)(unsafe.Pointer(pPage)).FmaskPage)&(libc.Int32FromUint8(**(**Tu8)(__ccgo_up((*TMemPage)(unsafe.Pointer(pPage)).FaCellIdx + uintptr(int32(2)*i))))<<libc.Int32FromInt32(8)|libc.Int32FromUint8(**(**Tu8)(__ccgo_up((*TMemPage)(unsafe.Pointer(pPage)).FaCellIdx + uintptr(int32(2)*i) + 1)))))
		_ptrmapPutOvflPtr(tls, pPage, pPage, pCell, bp)
		if !((*TMemPage)(unsafe.Pointer(pPage)).Fleaf != 0) {
			childPgno = _sqlite3Get4byte(tls, pCell)
			_ptrmapPut(tls, pBt, childPgno, uint8(PTRMAP_BTREE), pgno, bp)
		}
		goto _2
	_2:
		;
		i = i + 1
	}
	if !((*TMemPage)(unsafe.Pointer(pPage)).Fleaf != 0) {
		childPgno1 = _sqlite3Get4byte(tls, (*TMemPage)(unsafe.Pointer(pPage)).FaData+uintptr(libc.Int32FromUint8((*TMemPage)(unsafe.Pointer(pPage)).FhdrOffset)+int32(8)))
		_ptrmapPut(tls, pBt, childPgno1, uint8(PTRMAP_BTREE), pgno, bp)
	}
	return **(**int32)(__ccgo_up(bp))
}

// C documentation
//
//	/* Move the cursor so that it points to an entry in a table (a.k.a INTKEY)
//	** table near the key intKey.   Return a success code.
//	**
//	** If an exact match is not found, then the cursor is always
//	** left pointing at a leaf page which would hold the entry if it
//	** were present.  The cursor might point to an entry that comes
//	** before or after the key.
//	**
//	** An integer is written into *pRes which is the result of
//	** comparing the key with the entry to which the cursor is
//	** pointing.  The meaning of the integer written into
//	** *pRes is as follows:
//	**
//	**     *pRes<0      The cursor is left pointing at an entry that
//	**                  is smaller than intKey or if the table is empty
//	**                  and the cursor is therefore left point to nothing.
//	**
//	**     *pRes==0     The cursor is left pointing at an entry that
//	**                  exactly matches intKey.
//	**
//	**     *pRes>0      The cursor is left pointing at an entry that
//	**                  is larger than intKey.
//	*/
func _sqlite3BtreeTableMoveto(tls *libc.TLS, pCur uintptr, intKey Ti64, biasRight int32, pRes uintptr) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var c, idx, lwr, rc, upr int32
	var chldPg TPgno
	var pCell, pPage, v3 uintptr
	var _ /* nCellKey at bp+0 */ Ti64
	_, _, _, _, _, _, _, _, _ = c, chldPg, idx, lwr, pCell, pPage, rc, upr, v3
	/* If the cursor is already positioned at the point we are trying
	 ** to move to, then just return without doing any work */
	if libc.Int32FromUint8((*TBtCursor)(unsafe.Pointer(pCur)).FeState) == CURSOR_VALID && libc.Int32FromUint8((*TBtCursor)(unsafe.Pointer(pCur)).FcurFlags)&int32(BTCF_ValidNKey) != 0 {
		if (*TBtCursor)(unsafe.Pointer(pCur)).Finfo.FnKey == intKey {
			**(**int32)(__ccgo_up(pRes)) = 0
			return SQLITE_OK
		}
		if (*TBtCursor)(unsafe.Pointer(pCur)).Finfo.FnKey < intKey {
			if libc.Int32FromUint8((*TBtCursor)(unsafe.Pointer(pCur)).FcurFlags)&int32(BTCF_AtLast) != 0 {
				**(**int32)(__ccgo_up(pRes)) = -int32(1)
				return SQLITE_OK
			}
			/* If the requested key is one more than the previous key, then
			 ** try to get there using sqlite3BtreeNext() rather than a full
			 ** binary search.  This is an optimization only.  The correct answer
			 ** is still obtained without this case, only a little more slowly. */
			if (*TBtCursor)(unsafe.Pointer(pCur)).Finfo.FnKey+int64(1) == intKey {
				**(**int32)(__ccgo_up(pRes)) = 0
				rc = _sqlite3BtreeNext(tls, pCur, 0)
				if rc == SQLITE_OK {
					_getCellInfo(tls, pCur)
					if (*TBtCursor)(unsafe.Pointer(pCur)).Finfo.FnKey == intKey {
						return SQLITE_OK
					}
				} else {
					if rc != int32(SQLITE_DONE) {
						return rc
					}
				}
			}
		}
	}
	rc = _moveToRoot(tls, pCur)
	if rc != 0 {
		if rc == int32(SQLITE_EMPTY) {
			**(**int32)(__ccgo_up(pRes)) = -int32(1)
			return SQLITE_OK
		}
		return rc
	}
	for {
		pPage = (*TBtCursor)(unsafe.Pointer(pCur)).FpPage /* Pointer to current cell in pPage */
		/* pPage->nCell must be greater than zero. If this is the root-page
		 ** the cursor would have been INVALID above and this for(;;) loop
		 ** not run. If this is not the root-page, then the moveToChild() routine
		 ** would have already detected db corruption. Similarly, pPage must
		 ** be the right kind (index or table) of b-tree page. Otherwise
		 ** a moveToChild() or moveToRoot() call would have detected corruption.  */
		lwr = 0
		upr = libc.Int32FromUint16((*TMemPage)(unsafe.Pointer(pPage)).FnCell) - int32(1)
		idx = upr >> (int32(1) - biasRight) /* idx = biasRight ? upr : (lwr+upr)/2; */
		for {
			pCell = (*TMemPage)(unsafe.Pointer(pPage)).FaDataOfst + uintptr(libc.Int32FromUint16((*TMemPage)(unsafe.Pointer(pPage)).FmaskPage)&(libc.Int32FromUint8(**(**Tu8)(__ccgo_up((*TMemPage)(unsafe.Pointer(pPage)).FaCellIdx + uintptr(int32(2)*idx))))<<libc.Int32FromInt32(8)|libc.Int32FromUint8(**(**Tu8)(__ccgo_up((*TMemPage)(unsafe.Pointer(pPage)).FaCellIdx + uintptr(int32(2)*idx) + 1)))))
			if (*TMemPage)(unsafe.Pointer(pPage)).FintKeyLeaf != 0 {
				for {
					v3 = pCell
					pCell = pCell + 1
					if !(int32(0x80) <= libc.Int32FromUint8(**(**Tu8)(__ccgo_up(v3)))) {
						break
					}
					if pCell >= (*TMemPage)(unsafe.Pointer(pPage)).FaDataEnd {
						return _sqlite3CorruptError(tls, int32(79121))
					}
				}
			}
			_sqlite3GetVarint(tls, pCell, bp)
			if **(**Ti64)(__ccgo_up(bp)) < intKey {
				lwr = idx + int32(1)
				if lwr > upr {
					c = -int32(1)
					break
				}
			} else {
				if **(**Ti64)(__ccgo_up(bp)) > intKey {
					upr = idx - int32(1)
					if lwr > upr {
						c = +libc.Int32FromInt32(1)
						break
					}
				} else {
					(*TBtCursor)(unsafe.Pointer(pCur)).Fix = libc.Uint16FromInt32(idx)
					if !((*TMemPage)(unsafe.Pointer(pPage)).Fleaf != 0) {
						lwr = idx
						goto moveto_table_next_layer
					} else {
						v3 = pCur + 1
						*(*Tu8)(unsafe.Pointer(v3)) = Tu8(int32(*(*Tu8)(unsafe.Pointer(v3))) | libc.Int32FromInt32(BTCF_ValidNKey))
						(*TBtCursor)(unsafe.Pointer(pCur)).Finfo.FnKey = **(**Ti64)(__ccgo_up(bp))
						(*TBtCursor)(unsafe.Pointer(pCur)).Finfo.FnSize = uint16(0)
						**(**int32)(__ccgo_up(pRes)) = 0
						return SQLITE_OK
					}
				}
			}
			idx = (lwr + upr) >> int32(1) /* idx = (lwr+upr)/2; */
			goto _2
		_2:
		}
		if (*TMemPage)(unsafe.Pointer(pPage)).Fleaf != 0 {
			(*TBtCursor)(unsafe.Pointer(pCur)).Fix = libc.Uint16FromInt32(idx)
			**(**int32)(__ccgo_up(pRes)) = c
			rc = SQLITE_OK
			goto moveto_table_finish
		}
		goto moveto_table_next_layer
	moveto_table_next_layer:
		;
		if lwr >= libc.Int32FromUint16((*TMemPage)(unsafe.Pointer(pPage)).FnCell) {
			chldPg = _sqlite3Get4byte(tls, (*TMemPage)(unsafe.Pointer(pPage)).FaData+uintptr(libc.Int32FromUint8((*TMemPage)(unsafe.Pointer(pPage)).FhdrOffset)+int32(8)))
		} else {
			chldPg = _sqlite3Get4byte(tls, (*TMemPage)(unsafe.Pointer(pPage)).FaData+uintptr(libc.Int32FromUint16((*TMemPage)(unsafe.Pointer(pPage)).FmaskPage)&(libc.Int32FromUint8(**(**Tu8)(__ccgo_up((*TMemPage)(unsafe.Pointer(pPage)).FaCellIdx + uintptr(int32(2)*lwr))))<<libc.Int32FromInt32(8)|libc.Int32FromUint8(**(**Tu8)(__ccgo_up((*TMemPage)(unsafe.Pointer(pPage)).FaCellIdx + uintptr(int32(2)*lwr) + 1))))))
		}
		(*TBtCursor)(unsafe.Pointer(pCur)).Fix = libc.Uint16FromInt32(lwr)
		rc = _moveToChild(tls, pCur, chldPg)
		if rc != 0 {
			break
		}
		goto _1
	_1:
	}
	goto moveto_table_finish
moveto_table_finish:
	;
	(*TBtCursor)(unsafe.Pointer(pCur)).Finfo.FnSize = uint16(0)
	return rc
}

// C documentation
//
//	/*
//	** Read or write a four-byte big-endian integer value.
//	*/
func _sqlite3Get4byte(tls *libc.TLS, p uintptr) (r Tu32) {
	return uint32(**(**Tu8)(__ccgo_up(p)))<<int32(24) | libc.Uint32FromInt32(libc.Int32FromUint8(**(**Tu8)(__ccgo_up(p + 1)))<<libc.Int32FromInt32(16)) | libc.Uint32FromInt32(libc.Int32FromUint8(**(**Tu8)(__ccgo_up(p + 2)))<<libc.Int32FromInt32(8)) | uint32(**(**Tu8)(__ccgo_up(p + 3)))
}

func _writeInt64(tls *libc.TLS, p uintptr, i Ti64) (r int32) {
	**(**Tu8)(__ccgo_up(p)) = libc.Uint8FromInt64(i >> libc.Int32FromInt32(56) & int64(0xFF))
	**(**Tu8)(__ccgo_up(p + 1)) = libc.Uint8FromInt64(i >> libc.Int32FromInt32(48) & int64(0xFF))
	**(**Tu8)(__ccgo_up(p + 2)) = libc.Uint8FromInt64(i >> libc.Int32FromInt32(40) & int64(0xFF))
	**(**Tu8)(__ccgo_up(p + 3)) = libc.Uint8FromInt64(i >> libc.Int32FromInt32(32) & int64(0xFF))
	**(**Tu8)(__ccgo_up(p + 4)) = libc.Uint8FromInt64(i >> libc.Int32FromInt32(24) & int64(0xFF))
	**(**Tu8)(__ccgo_up(p + 5)) = libc.Uint8FromInt64(i >> libc.Int32FromInt32(16) & int64(0xFF))
	**(**Tu8)(__ccgo_up(p + 6)) = libc.Uint8FromInt64(i >> libc.Int32FromInt32(8) & int64(0xFF))
	**(**Tu8)(__ccgo_up(p + 7)) = libc.Uint8FromInt64(i >> libc.Int32FromInt32(0) & int64(0xFF))
	return int32(8)
}
