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

//go:build (freebsd && amd64) || (freebsd && arm64) || (linux && amd64) || (linux && arm64) || (linux && loong64) || (linux && ppc64le) || (linux && riscv64) || (linux && s390x) || (netbsd && amd64) || (openbsd && amd64) || (openbsd && arm64) || (windows && (amd64 || arm64))

package sqlite3

import (
	"unsafe"

	"modernc.org/libc"
)

// C documentation
//
//	/*
//	** Allocate a new, empty, sqlite3_changegroup.
//	*/
func Xsqlite3changegroup_new(tls *libc.TLS, pp uintptr) (r int32) {
	var p uintptr
	var rc int32
	_, _ = p, rc
	rc = SQLITE_OK /* New object */
	p = Xsqlite3_malloc(tls, int32(96))
	if p == uintptr(0) {
		rc = int32(SQLITE_NOMEM)
	} else {
		libc.Xmemset(tls, p, 0, uint64(96))
	}
	**(**uintptr)(__ccgo_up(pp)) = p
	return rc
}

// C documentation
//
//	/*
//	** Invert a changeset object.
//	*/
func Xsqlite3changeset_invert(tls *libc.TLS, nChangeset int32, pChangeset uintptr, pnInverted uintptr, ppInverted uintptr) (r int32) {
	bp := tls.Alloc(80)
	defer tls.Free(80)
	var _ /* sInput at bp+0 */ TSessionInput
	/* Set up the input stream */
	libc.Xmemset(tls, bp, 0, uint64(72))
	(**(**TSessionInput)(__ccgo_up(bp))).FnData = nChangeset
	(**(**TSessionInput)(__ccgo_up(bp))).FaData = pChangeset
	return _sessionChangesetInvert(tls, bp, uintptr(0), uintptr(0), pnInverted, ppInverted)
}

// C documentation
//
//	/*
//	** Streaming version of sqlite3changeset_invert().
//	*/
func Xsqlite3changeset_invert_strm(tls *libc.TLS, __ccgo_fp_xInput uintptr, pIn uintptr, __ccgo_fp_xOutput uintptr, pOut uintptr) (r int32) {
	bp := tls.Alloc(80)
	defer tls.Free(80)
	var rc int32
	var _ /* sInput at bp+0 */ TSessionInput
	_ = rc
	/* Set up the input stream */
	libc.Xmemset(tls, bp, 0, uint64(72))
	(**(**TSessionInput)(__ccgo_up(bp))).FxInput = __ccgo_fp_xInput
	(**(**TSessionInput)(__ccgo_up(bp))).FpIn = pIn
	rc = _sessionChangesetInvert(tls, bp, __ccgo_fp_xOutput, pOut, uintptr(0), uintptr(0))
	Xsqlite3_free(tls, (**(**TSessionInput)(__ccgo_up(bp))).Fbuf.FaBuf)
	return rc
}

// C documentation
//
//	/*
//	** Create a new rebaser object.
//	*/
func Xsqlite3rebaser_create(tls *libc.TLS, ppNew uintptr) (r int32) {
	var pNew uintptr
	var rc int32
	_, _ = pNew, rc
	rc = SQLITE_OK
	pNew = Xsqlite3_malloc(tls, int32(96))
	if pNew == uintptr(0) {
		rc = int32(SQLITE_NOMEM)
	} else {
		libc.Xmemset(tls, pNew, 0, uint64(96))
	}
	**(**uintptr)(__ccgo_up(ppNew)) = pNew
	return rc
}

// C documentation
//
//	/*
//	** Convert every pAggInfo->aFunc[].pExpr such that any node within
//	** those expressions that has pAppInfo set is changed into a TK_AGG_COLUMN
//	** opcode.
//	*/
func _aggregateConvertIndexedExprRefToColumn(tls *libc.TLS, pAggInfo uintptr) {
	bp := tls.Alloc(48)
	defer tls.Free(48)
	var i int32
	var _ /* w at bp+0 */ TWalker
	_ = i
	libc.Xmemset(tls, bp, 0, uint64(48))
	(**(**TWalker)(__ccgo_up(bp))).FxExprCallback = __ccgo_fp(_aggregateIdxEprRefToColCallback)
	i = 0
	for {
		if !(i < (*TAggInfo)(unsafe.Pointer(pAggInfo)).FnFunc) {
			break
		}
		_sqlite3WalkExpr(tls, bp, (**(**TAggInfo_func)(__ccgo_up((*TAggInfo)(unsafe.Pointer(pAggInfo)).FaFunc + uintptr(i)*32))).FpFExpr)
		goto _1
	_1:
		;
		i = i + 1
	}
}

// C documentation
//
//	/*
//	** Append text z[] to the end of p[].  Return a pointer to the first
//	** character after then zero terminator on the new text in p[].
//	*/
func _appendText(tls *libc.TLS, p uintptr, z uintptr) (r uintptr) {
	var n Tsize_t
	_ = n
	n = libc.Xstrlen(tls, z)
	libc.Xmemcpy(tls, p, z, n+uint64(1))
	return p + uintptr(n) + uintptr(1)
}

// C documentation
//
//	/* The RFC-7539 ChaCha20 block function
//	*/
func _chacha_block(tls *libc.TLS, out uintptr, in uintptr) {
	bp := tls.Alloc(64)
	defer tls.Free(64)
	var i int32
	var _ /* x at bp+0 */ [16]Tu32
	_ = i
	libc.Xmemcpy(tls, bp, in, uint64(64))
	i = 0
	for {
		if !(i < int32(10)) {
			break
		}
		**(**Tu32)(__ccgo_up(bp)) += (**(**[16]Tu32)(__ccgo_up(bp)))[int32(4)]
		**(**Tu32)(__ccgo_up(bp + 12*4)) ^= (**(**[16]Tu32)(__ccgo_up(bp)))[0]
		(**(**[16]Tu32)(__ccgo_up(bp)))[int32(12)] = (**(**[16]Tu32)(__ccgo_up(bp)))[int32(12)]<<libc.Int32FromInt32(16) | (**(**[16]Tu32)(__ccgo_up(bp)))[int32(12)]>>(libc.Int32FromInt32(32)-libc.Int32FromInt32(16))
		**(**Tu32)(__ccgo_up(bp + 8*4)) += (**(**[16]Tu32)(__ccgo_up(bp)))[int32(12)]
		**(**Tu32)(__ccgo_up(bp + 4*4)) ^= (**(**[16]Tu32)(__ccgo_up(bp)))[int32(8)]
		(**(**[16]Tu32)(__ccgo_up(bp)))[int32(4)] = (**(**[16]Tu32)(__ccgo_up(bp)))[int32(4)]<<libc.Int32FromInt32(12) | (**(**[16]Tu32)(__ccgo_up(bp)))[int32(4)]>>(libc.Int32FromInt32(32)-libc.Int32FromInt32(12))
		**(**Tu32)(__ccgo_up(bp)) += (**(**[16]Tu32)(__ccgo_up(bp)))[int32(4)]
		**(**Tu32)(__ccgo_up(bp + 12*4)) ^= (**(**[16]Tu32)(__ccgo_up(bp)))[0]
		(**(**[16]Tu32)(__ccgo_up(bp)))[int32(12)] = (**(**[16]Tu32)(__ccgo_up(bp)))[int32(12)]<<libc.Int32FromInt32(8) | (**(**[16]Tu32)(__ccgo_up(bp)))[int32(12)]>>(libc.Int32FromInt32(32)-libc.Int32FromInt32(8))
		**(**Tu32)(__ccgo_up(bp + 8*4)) += (**(**[16]Tu32)(__ccgo_up(bp)))[int32(12)]
		**(**Tu32)(__ccgo_up(bp + 4*4)) ^= (**(**[16]Tu32)(__ccgo_up(bp)))[int32(8)]
		(**(**[16]Tu32)(__ccgo_up(bp)))[int32(4)] = (**(**[16]Tu32)(__ccgo_up(bp)))[int32(4)]<<libc.Int32FromInt32(7) | (**(**[16]Tu32)(__ccgo_up(bp)))[int32(4)]>>(libc.Int32FromInt32(32)-libc.Int32FromInt32(7))
		**(**Tu32)(__ccgo_up(bp + 1*4)) += (**(**[16]Tu32)(__ccgo_up(bp)))[int32(5)]
		**(**Tu32)(__ccgo_up(bp + 13*4)) ^= (**(**[16]Tu32)(__ccgo_up(bp)))[int32(1)]
		(**(**[16]Tu32)(__ccgo_up(bp)))[int32(13)] = (**(**[16]Tu32)(__ccgo_up(bp)))[int32(13)]<<libc.Int32FromInt32(16) | (**(**[16]Tu32)(__ccgo_up(bp)))[int32(13)]>>(libc.Int32FromInt32(32)-libc.Int32FromInt32(16))
		**(**Tu32)(__ccgo_up(bp + 9*4)) += (**(**[16]Tu32)(__ccgo_up(bp)))[int32(13)]
		**(**Tu32)(__ccgo_up(bp + 5*4)) ^= (**(**[16]Tu32)(__ccgo_up(bp)))[int32(9)]
		(**(**[16]Tu32)(__ccgo_up(bp)))[int32(5)] = (**(**[16]Tu32)(__ccgo_up(bp)))[int32(5)]<<libc.Int32FromInt32(12) | (**(**[16]Tu32)(__ccgo_up(bp)))[int32(5)]>>(libc.Int32FromInt32(32)-libc.Int32FromInt32(12))
		**(**Tu32)(__ccgo_up(bp + 1*4)) += (**(**[16]Tu32)(__ccgo_up(bp)))[int32(5)]
		**(**Tu32)(__ccgo_up(bp + 13*4)) ^= (**(**[16]Tu32)(__ccgo_up(bp)))[int32(1)]
		(**(**[16]Tu32)(__ccgo_up(bp)))[int32(13)] = (**(**[16]Tu32)(__ccgo_up(bp)))[int32(13)]<<libc.Int32FromInt32(8) | (**(**[16]Tu32)(__ccgo_up(bp)))[int32(13)]>>(libc.Int32FromInt32(32)-libc.Int32FromInt32(8))
		**(**Tu32)(__ccgo_up(bp + 9*4)) += (**(**[16]Tu32)(__ccgo_up(bp)))[int32(13)]
		**(**Tu32)(__ccgo_up(bp + 5*4)) ^= (**(**[16]Tu32)(__ccgo_up(bp)))[int32(9)]
		(**(**[16]Tu32)(__ccgo_up(bp)))[int32(5)] = (**(**[16]Tu32)(__ccgo_up(bp)))[int32(5)]<<libc.Int32FromInt32(7) | (**(**[16]Tu32)(__ccgo_up(bp)))[int32(5)]>>(libc.Int32FromInt32(32)-libc.Int32FromInt32(7))
		**(**Tu32)(__ccgo_up(bp + 2*4)) += (**(**[16]Tu32)(__ccgo_up(bp)))[int32(6)]
		**(**Tu32)(__ccgo_up(bp + 14*4)) ^= (**(**[16]Tu32)(__ccgo_up(bp)))[int32(2)]
		(**(**[16]Tu32)(__ccgo_up(bp)))[int32(14)] = (**(**[16]Tu32)(__ccgo_up(bp)))[int32(14)]<<libc.Int32FromInt32(16) | (**(**[16]Tu32)(__ccgo_up(bp)))[int32(14)]>>(libc.Int32FromInt32(32)-libc.Int32FromInt32(16))
		**(**Tu32)(__ccgo_up(bp + 10*4)) += (**(**[16]Tu32)(__ccgo_up(bp)))[int32(14)]
		**(**Tu32)(__ccgo_up(bp + 6*4)) ^= (**(**[16]Tu32)(__ccgo_up(bp)))[int32(10)]
		(**(**[16]Tu32)(__ccgo_up(bp)))[int32(6)] = (**(**[16]Tu32)(__ccgo_up(bp)))[int32(6)]<<libc.Int32FromInt32(12) | (**(**[16]Tu32)(__ccgo_up(bp)))[int32(6)]>>(libc.Int32FromInt32(32)-libc.Int32FromInt32(12))
		**(**Tu32)(__ccgo_up(bp + 2*4)) += (**(**[16]Tu32)(__ccgo_up(bp)))[int32(6)]
		**(**Tu32)(__ccgo_up(bp + 14*4)) ^= (**(**[16]Tu32)(__ccgo_up(bp)))[int32(2)]
		(**(**[16]Tu32)(__ccgo_up(bp)))[int32(14)] = (**(**[16]Tu32)(__ccgo_up(bp)))[int32(14)]<<libc.Int32FromInt32(8) | (**(**[16]Tu32)(__ccgo_up(bp)))[int32(14)]>>(libc.Int32FromInt32(32)-libc.Int32FromInt32(8))
		**(**Tu32)(__ccgo_up(bp + 10*4)) += (**(**[16]Tu32)(__ccgo_up(bp)))[int32(14)]
		**(**Tu32)(__ccgo_up(bp + 6*4)) ^= (**(**[16]Tu32)(__ccgo_up(bp)))[int32(10)]
		(**(**[16]Tu32)(__ccgo_up(bp)))[int32(6)] = (**(**[16]Tu32)(__ccgo_up(bp)))[int32(6)]<<libc.Int32FromInt32(7) | (**(**[16]Tu32)(__ccgo_up(bp)))[int32(6)]>>(libc.Int32FromInt32(32)-libc.Int32FromInt32(7))
		**(**Tu32)(__ccgo_up(bp + 3*4)) += (**(**[16]Tu32)(__ccgo_up(bp)))[int32(7)]
		**(**Tu32)(__ccgo_up(bp + 15*4)) ^= (**(**[16]Tu32)(__ccgo_up(bp)))[int32(3)]
		(**(**[16]Tu32)(__ccgo_up(bp)))[int32(15)] = (**(**[16]Tu32)(__ccgo_up(bp)))[int32(15)]<<libc.Int32FromInt32(16) | (**(**[16]Tu32)(__ccgo_up(bp)))[int32(15)]>>(libc.Int32FromInt32(32)-libc.Int32FromInt32(16))
		**(**Tu32)(__ccgo_up(bp + 11*4)) += (**(**[16]Tu32)(__ccgo_up(bp)))[int32(15)]
		**(**Tu32)(__ccgo_up(bp + 7*4)) ^= (**(**[16]Tu32)(__ccgo_up(bp)))[int32(11)]
		(**(**[16]Tu32)(__ccgo_up(bp)))[int32(7)] = (**(**[16]Tu32)(__ccgo_up(bp)))[int32(7)]<<libc.Int32FromInt32(12) | (**(**[16]Tu32)(__ccgo_up(bp)))[int32(7)]>>(libc.Int32FromInt32(32)-libc.Int32FromInt32(12))
		**(**Tu32)(__ccgo_up(bp + 3*4)) += (**(**[16]Tu32)(__ccgo_up(bp)))[int32(7)]
		**(**Tu32)(__ccgo_up(bp + 15*4)) ^= (**(**[16]Tu32)(__ccgo_up(bp)))[int32(3)]
		(**(**[16]Tu32)(__ccgo_up(bp)))[int32(15)] = (**(**[16]Tu32)(__ccgo_up(bp)))[int32(15)]<<libc.Int32FromInt32(8) | (**(**[16]Tu32)(__ccgo_up(bp)))[int32(15)]>>(libc.Int32FromInt32(32)-libc.Int32FromInt32(8))
		**(**Tu32)(__ccgo_up(bp + 11*4)) += (**(**[16]Tu32)(__ccgo_up(bp)))[int32(15)]
		**(**Tu32)(__ccgo_up(bp + 7*4)) ^= (**(**[16]Tu32)(__ccgo_up(bp)))[int32(11)]
		(**(**[16]Tu32)(__ccgo_up(bp)))[int32(7)] = (**(**[16]Tu32)(__ccgo_up(bp)))[int32(7)]<<libc.Int32FromInt32(7) | (**(**[16]Tu32)(__ccgo_up(bp)))[int32(7)]>>(libc.Int32FromInt32(32)-libc.Int32FromInt32(7))
		**(**Tu32)(__ccgo_up(bp)) += (**(**[16]Tu32)(__ccgo_up(bp)))[int32(5)]
		**(**Tu32)(__ccgo_up(bp + 15*4)) ^= (**(**[16]Tu32)(__ccgo_up(bp)))[0]
		(**(**[16]Tu32)(__ccgo_up(bp)))[int32(15)] = (**(**[16]Tu32)(__ccgo_up(bp)))[int32(15)]<<libc.Int32FromInt32(16) | (**(**[16]Tu32)(__ccgo_up(bp)))[int32(15)]>>(libc.Int32FromInt32(32)-libc.Int32FromInt32(16))
		**(**Tu32)(__ccgo_up(bp + 10*4)) += (**(**[16]Tu32)(__ccgo_up(bp)))[int32(15)]
		**(**Tu32)(__ccgo_up(bp + 5*4)) ^= (**(**[16]Tu32)(__ccgo_up(bp)))[int32(10)]
		(**(**[16]Tu32)(__ccgo_up(bp)))[int32(5)] = (**(**[16]Tu32)(__ccgo_up(bp)))[int32(5)]<<libc.Int32FromInt32(12) | (**(**[16]Tu32)(__ccgo_up(bp)))[int32(5)]>>(libc.Int32FromInt32(32)-libc.Int32FromInt32(12))
		**(**Tu32)(__ccgo_up(bp)) += (**(**[16]Tu32)(__ccgo_up(bp)))[int32(5)]
		**(**Tu32)(__ccgo_up(bp + 15*4)) ^= (**(**[16]Tu32)(__ccgo_up(bp)))[0]
		(**(**[16]Tu32)(__ccgo_up(bp)))[int32(15)] = (**(**[16]Tu32)(__ccgo_up(bp)))[int32(15)]<<libc.Int32FromInt32(8) | (**(**[16]Tu32)(__ccgo_up(bp)))[int32(15)]>>(libc.Int32FromInt32(32)-libc.Int32FromInt32(8))
		**(**Tu32)(__ccgo_up(bp + 10*4)) += (**(**[16]Tu32)(__ccgo_up(bp)))[int32(15)]
		**(**Tu32)(__ccgo_up(bp + 5*4)) ^= (**(**[16]Tu32)(__ccgo_up(bp)))[int32(10)]
		(**(**[16]Tu32)(__ccgo_up(bp)))[int32(5)] = (**(**[16]Tu32)(__ccgo_up(bp)))[int32(5)]<<libc.Int32FromInt32(7) | (**(**[16]Tu32)(__ccgo_up(bp)))[int32(5)]>>(libc.Int32FromInt32(32)-libc.Int32FromInt32(7))
		**(**Tu32)(__ccgo_up(bp + 1*4)) += (**(**[16]Tu32)(__ccgo_up(bp)))[int32(6)]
		**(**Tu32)(__ccgo_up(bp + 12*4)) ^= (**(**[16]Tu32)(__ccgo_up(bp)))[int32(1)]
		(**(**[16]Tu32)(__ccgo_up(bp)))[int32(12)] = (**(**[16]Tu32)(__ccgo_up(bp)))[int32(12)]<<libc.Int32FromInt32(16) | (**(**[16]Tu32)(__ccgo_up(bp)))[int32(12)]>>(libc.Int32FromInt32(32)-libc.Int32FromInt32(16))
		**(**Tu32)(__ccgo_up(bp + 11*4)) += (**(**[16]Tu32)(__ccgo_up(bp)))[int32(12)]
		**(**Tu32)(__ccgo_up(bp + 6*4)) ^= (**(**[16]Tu32)(__ccgo_up(bp)))[int32(11)]
		(**(**[16]Tu32)(__ccgo_up(bp)))[int32(6)] = (**(**[16]Tu32)(__ccgo_up(bp)))[int32(6)]<<libc.Int32FromInt32(12) | (**(**[16]Tu32)(__ccgo_up(bp)))[int32(6)]>>(libc.Int32FromInt32(32)-libc.Int32FromInt32(12))
		**(**Tu32)(__ccgo_up(bp + 1*4)) += (**(**[16]Tu32)(__ccgo_up(bp)))[int32(6)]
		**(**Tu32)(__ccgo_up(bp + 12*4)) ^= (**(**[16]Tu32)(__ccgo_up(bp)))[int32(1)]
		(**(**[16]Tu32)(__ccgo_up(bp)))[int32(12)] = (**(**[16]Tu32)(__ccgo_up(bp)))[int32(12)]<<libc.Int32FromInt32(8) | (**(**[16]Tu32)(__ccgo_up(bp)))[int32(12)]>>(libc.Int32FromInt32(32)-libc.Int32FromInt32(8))
		**(**Tu32)(__ccgo_up(bp + 11*4)) += (**(**[16]Tu32)(__ccgo_up(bp)))[int32(12)]
		**(**Tu32)(__ccgo_up(bp + 6*4)) ^= (**(**[16]Tu32)(__ccgo_up(bp)))[int32(11)]
		(**(**[16]Tu32)(__ccgo_up(bp)))[int32(6)] = (**(**[16]Tu32)(__ccgo_up(bp)))[int32(6)]<<libc.Int32FromInt32(7) | (**(**[16]Tu32)(__ccgo_up(bp)))[int32(6)]>>(libc.Int32FromInt32(32)-libc.Int32FromInt32(7))
		**(**Tu32)(__ccgo_up(bp + 2*4)) += (**(**[16]Tu32)(__ccgo_up(bp)))[int32(7)]
		**(**Tu32)(__ccgo_up(bp + 13*4)) ^= (**(**[16]Tu32)(__ccgo_up(bp)))[int32(2)]
		(**(**[16]Tu32)(__ccgo_up(bp)))[int32(13)] = (**(**[16]Tu32)(__ccgo_up(bp)))[int32(13)]<<libc.Int32FromInt32(16) | (**(**[16]Tu32)(__ccgo_up(bp)))[int32(13)]>>(libc.Int32FromInt32(32)-libc.Int32FromInt32(16))
		**(**Tu32)(__ccgo_up(bp + 8*4)) += (**(**[16]Tu32)(__ccgo_up(bp)))[int32(13)]
		**(**Tu32)(__ccgo_up(bp + 7*4)) ^= (**(**[16]Tu32)(__ccgo_up(bp)))[int32(8)]
		(**(**[16]Tu32)(__ccgo_up(bp)))[int32(7)] = (**(**[16]Tu32)(__ccgo_up(bp)))[int32(7)]<<libc.Int32FromInt32(12) | (**(**[16]Tu32)(__ccgo_up(bp)))[int32(7)]>>(libc.Int32FromInt32(32)-libc.Int32FromInt32(12))
		**(**Tu32)(__ccgo_up(bp + 2*4)) += (**(**[16]Tu32)(__ccgo_up(bp)))[int32(7)]
		**(**Tu32)(__ccgo_up(bp + 13*4)) ^= (**(**[16]Tu32)(__ccgo_up(bp)))[int32(2)]
		(**(**[16]Tu32)(__ccgo_up(bp)))[int32(13)] = (**(**[16]Tu32)(__ccgo_up(bp)))[int32(13)]<<libc.Int32FromInt32(8) | (**(**[16]Tu32)(__ccgo_up(bp)))[int32(13)]>>(libc.Int32FromInt32(32)-libc.Int32FromInt32(8))
		**(**Tu32)(__ccgo_up(bp + 8*4)) += (**(**[16]Tu32)(__ccgo_up(bp)))[int32(13)]
		**(**Tu32)(__ccgo_up(bp + 7*4)) ^= (**(**[16]Tu32)(__ccgo_up(bp)))[int32(8)]
		(**(**[16]Tu32)(__ccgo_up(bp)))[int32(7)] = (**(**[16]Tu32)(__ccgo_up(bp)))[int32(7)]<<libc.Int32FromInt32(7) | (**(**[16]Tu32)(__ccgo_up(bp)))[int32(7)]>>(libc.Int32FromInt32(32)-libc.Int32FromInt32(7))
		**(**Tu32)(__ccgo_up(bp + 3*4)) += (**(**[16]Tu32)(__ccgo_up(bp)))[int32(4)]
		**(**Tu32)(__ccgo_up(bp + 14*4)) ^= (**(**[16]Tu32)(__ccgo_up(bp)))[int32(3)]
		(**(**[16]Tu32)(__ccgo_up(bp)))[int32(14)] = (**(**[16]Tu32)(__ccgo_up(bp)))[int32(14)]<<libc.Int32FromInt32(16) | (**(**[16]Tu32)(__ccgo_up(bp)))[int32(14)]>>(libc.Int32FromInt32(32)-libc.Int32FromInt32(16))
		**(**Tu32)(__ccgo_up(bp + 9*4)) += (**(**[16]Tu32)(__ccgo_up(bp)))[int32(14)]
		**(**Tu32)(__ccgo_up(bp + 4*4)) ^= (**(**[16]Tu32)(__ccgo_up(bp)))[int32(9)]
		(**(**[16]Tu32)(__ccgo_up(bp)))[int32(4)] = (**(**[16]Tu32)(__ccgo_up(bp)))[int32(4)]<<libc.Int32FromInt32(12) | (**(**[16]Tu32)(__ccgo_up(bp)))[int32(4)]>>(libc.Int32FromInt32(32)-libc.Int32FromInt32(12))
		**(**Tu32)(__ccgo_up(bp + 3*4)) += (**(**[16]Tu32)(__ccgo_up(bp)))[int32(4)]
		**(**Tu32)(__ccgo_up(bp + 14*4)) ^= (**(**[16]Tu32)(__ccgo_up(bp)))[int32(3)]
		(**(**[16]Tu32)(__ccgo_up(bp)))[int32(14)] = (**(**[16]Tu32)(__ccgo_up(bp)))[int32(14)]<<libc.Int32FromInt32(8) | (**(**[16]Tu32)(__ccgo_up(bp)))[int32(14)]>>(libc.Int32FromInt32(32)-libc.Int32FromInt32(8))
		**(**Tu32)(__ccgo_up(bp + 9*4)) += (**(**[16]Tu32)(__ccgo_up(bp)))[int32(14)]
		**(**Tu32)(__ccgo_up(bp + 4*4)) ^= (**(**[16]Tu32)(__ccgo_up(bp)))[int32(9)]
		(**(**[16]Tu32)(__ccgo_up(bp)))[int32(4)] = (**(**[16]Tu32)(__ccgo_up(bp)))[int32(4)]<<libc.Int32FromInt32(7) | (**(**[16]Tu32)(__ccgo_up(bp)))[int32(4)]>>(libc.Int32FromInt32(32)-libc.Int32FromInt32(7))
		goto _1
	_1:
		;
		i = i + 1
	}
	i = 0
	for {
		if !(i < int32(16)) {
			break
		}
		**(**Tu32)(__ccgo_up(out + uintptr(i)*4)) = (**(**[16]Tu32)(__ccgo_up(bp)))[i] + **(**Tu32)(__ccgo_up(in + uintptr(i)*4))
		goto _2
	_2:
		;
		i = i + 1
	}
}

// C documentation
//
//	/*
//	** Put the DateTime object into its error state.
//	*/
func _datetimeError(tls *libc.TLS, p uintptr) {
	libc.Xmemset(tls, p, 0, uint64(48))
	libc.SetBitFieldPtr8Uint32(p+44, libc.Uint32FromInt32(1), 1, 0x2)
}

// C documentation
//
//	/*
//	** Open a new dbpagevfs cursor.
//	*/
func _dbpageOpen(tls *libc.TLS, pVTab uintptr, ppCursor uintptr) (r int32) {
	var pCsr uintptr
	_ = pCsr
	pCsr = Xsqlite3_malloc64(tls, uint64(40))
	if pCsr == uintptr(0) {
		return int32(SQLITE_NOMEM)
	} else {
		libc.Xmemset(tls, pCsr, 0, uint64(40))
		(*TDbpageCursor)(unsafe.Pointer(pCsr)).Fbase.FpVtab = pVTab
		(*TDbpageCursor)(unsafe.Pointer(pCsr)).Fpgno = uint32(0)
	}
	**(**uintptr)(__ccgo_up(ppCursor)) = pCsr
	return SQLITE_OK
}

// C documentation
//
//	/*
//	** Disable lookaside memory allocation for objects that might be
//	** shared across database connections.
//	*/
func _disableLookaside(tls *libc.TLS, pParse uintptr) {
	var db uintptr
	_ = db
	db = (*TParse)(unsafe.Pointer(pParse)).Fdb
	(*TParse)(unsafe.Pointer(pParse)).FdisableLookaside = (*TParse)(unsafe.Pointer(pParse)).FdisableLookaside + 1
	libc.Xmemset(tls, pParse+256, 0, uint64(32))
	(*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FbDisable = (*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FbDisable + 1
	(*Tsqlite3)(unsafe.Pointer(db)).Flookaside.Fsz = uint16(0)
}

// C documentation
//
//	/*
//	** Allocate a new segment-id for the structure pStruct. The new segment
//	** id must be between 1 and 65335 inclusive, and must not be used by
//	** any currently existing segment. If a free segment id cannot be found,
//	** SQLITE_FULL is returned.
//	**
//	** If an error has already occurred, this function is a no-op. 0 is
//	** returned in this case.
//	*/
func _fts5AllocateSegid(tls *libc.TLS, p uintptr, pStruct uintptr) (r int32) {
	bp := tls.Alloc(256)
	defer tls.Free(256)
	var i, iId, iLvl, iSeg, iSegid int32
	var mask Tu32
	var _ /* aUsed at bp+0 */ [63]Tu32
	_, _, _, _, _, _ = i, iId, iLvl, iSeg, iSegid, mask
	iSegid = 0
	if (*TFts5Index)(unsafe.Pointer(p)).Frc == SQLITE_OK {
		if (*TFts5Structure)(unsafe.Pointer(pStruct)).FnSegment >= int32(FTS5_MAX_SEGMENT) {
			(*TFts5Index)(unsafe.Pointer(p)).Frc = int32(SQLITE_FULL)
		} else {
			libc.Xmemset(tls, bp, 0, uint64(252))
			iLvl = 0
			for {
				if !(iLvl < (*TFts5Structure)(unsafe.Pointer(pStruct)).FnLevel) {
					break
				}
				iSeg = 0
				for {
					if !(iSeg < (*(*TFts5StructureLevel)(unsafe.Pointer(pStruct + 32 + uintptr(iLvl)*16))).FnSeg) {
						break
					}
					iId = (**(**TFts5StructureSegment)(__ccgo_up((*(*TFts5StructureLevel)(unsafe.Pointer(pStruct + 32 + uintptr(iLvl)*16))).FaSeg + uintptr(iSeg)*56))).FiSegid
					if iId <= int32(FTS5_MAX_SEGMENT) && iId > 0 {
						**(**Tu32)(__ccgo_up(bp + uintptr((iId-int32(1))/int32(32))*4)) |= libc.Uint32FromInt32(1) << ((iId - int32(1)) % int32(32))
					}
					goto _2
				_2:
					;
					iSeg = iSeg + 1
				}
				goto _1
			_1:
				;
				iLvl = iLvl + 1
			}
			i = 0
			for {
				if !((**(**[63]Tu32)(__ccgo_up(bp)))[i] == uint32(0xFFFFFFFF)) {
					break
				}
				goto _3
			_3:
				;
				i = i + 1
			}
			mask = (**(**[63]Tu32)(__ccgo_up(bp)))[i]
			iSegid = 0
			for {
				if !(mask&(libc.Uint32FromInt32(1)<<iSegid) != 0) {
					break
				}
				goto _4
			_4:
				;
				iSegid = iSegid + 1
			}
			iSegid = iSegid + (int32(1) + i*int32(32))
		}
	}
	return iSegid
}

// C documentation
//
//	/*
//	** Initialize the iterator object indicated by the final parameter to
//	** iterate through coalesced phrase instances in column iCol.
//	*/
func _fts5CInstIterInit(tls *libc.TLS, pApi uintptr, pFts uintptr, iCol int32, pIter uintptr) (r int32) {
	var rc int32
	_ = rc
	libc.Xmemset(tls, pIter, 0, uint64(40))
	(*TCInstIter)(unsafe.Pointer(pIter)).FpApi = pApi
	(*TCInstIter)(unsafe.Pointer(pIter)).FpFts = pFts
	(*TCInstIter)(unsafe.Pointer(pIter)).FiCol = iCol
	rc = (*(*func(*libc.TLS, uintptr, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*TFts5ExtensionApi)(unsafe.Pointer(pApi)).FxInstCount})))(tls, pFts, pIter+24)
	if rc == SQLITE_OK {
		rc = _fts5CInstIterNext(tls, pIter)
	}
	return rc
}

func _fts5DlidxIterLast(tls *libc.TLS, p uintptr, pIter uintptr) {
	var i int32
	var pChild, pLvl uintptr
	_, _, _ = i, pChild, pLvl
	/* Advance each level to the last entry on the last page */
	i = (*TFts5DlidxIter)(unsafe.Pointer(pIter)).FnLvl - int32(1)
	for {
		if !((*TFts5Index)(unsafe.Pointer(p)).Frc == SQLITE_OK && i >= 0) {
			break
		}
		pLvl = pIter + 8 + uintptr(i)*32
		for _fts5DlidxLvlNext(tls, pLvl) == 0 {
		}
		(*TFts5DlidxLvl)(unsafe.Pointer(pLvl)).FbEof = 0
		if i > 0 {
			pChild = pLvl + uintptr(-libc.Int32FromInt32(1))*32
			_fts5DataRelease(tls, (*TFts5DlidxLvl)(unsafe.Pointer(pChild)).FpData)
			libc.Xmemset(tls, pChild, 0, uint64(32))
			(*TFts5DlidxLvl)(unsafe.Pointer(pChild)).FpData = _fts5DataRead(tls, p, int64((*TFts5DlidxIter)(unsafe.Pointer(pIter)).FiSegid)<<(libc.Int32FromInt32(FTS5_DATA_PAGE_B)+libc.Int32FromInt32(FTS5_DATA_HEIGHT_B)+libc.Int32FromInt32(FTS5_DATA_DLI_B))+int64(libc.Int32FromInt32(1))<<(libc.Int32FromInt32(FTS5_DATA_PAGE_B)+libc.Int32FromInt32(FTS5_DATA_HEIGHT_B))+int64(i-libc.Int32FromInt32(1))<<libc.Int32FromInt32(FTS5_DATA_PAGE_B)+int64((*TFts5DlidxLvl)(unsafe.Pointer(pLvl)).FiLeafPgno))
		}
		goto _1
	_1:
		;
		i = i - 1
	}
}

// C documentation
//
//	/*
//	** Advance the iterator passed as the only argument.
//	*/
func _fts5DlidxIterNextR(tls *libc.TLS, p uintptr, pIter uintptr, iLvl int32) (r int32) {
	var pLvl uintptr
	_ = pLvl
	pLvl = pIter + 8 + uintptr(iLvl)*32
	if _fts5DlidxLvlNext(tls, pLvl) != 0 {
		if iLvl+int32(1) < (*TFts5DlidxIter)(unsafe.Pointer(pIter)).FnLvl {
			_fts5DlidxIterNextR(tls, p, pIter, iLvl+int32(1))
			if (**(**TFts5DlidxLvl)(__ccgo_up(pLvl + 1*32))).FbEof == 0 {
				_fts5DataRelease(tls, (*TFts5DlidxLvl)(unsafe.Pointer(pLvl)).FpData)
				libc.Xmemset(tls, pLvl, 0, uint64(32))
				(*TFts5DlidxLvl)(unsafe.Pointer(pLvl)).FpData = _fts5DataRead(tls, p, int64((*TFts5DlidxIter)(unsafe.Pointer(pIter)).FiSegid)<<(libc.Int32FromInt32(FTS5_DATA_PAGE_B)+libc.Int32FromInt32(FTS5_DATA_HEIGHT_B)+libc.Int32FromInt32(FTS5_DATA_DLI_B))+int64(libc.Int32FromInt32(1))<<(libc.Int32FromInt32(FTS5_DATA_PAGE_B)+libc.Int32FromInt32(FTS5_DATA_HEIGHT_B))+int64(iLvl)<<libc.Int32FromInt32(FTS5_DATA_PAGE_B)+int64((**(**TFts5DlidxLvl)(__ccgo_up(pLvl + 1*32))).FiLeafPgno))
				if (*TFts5DlidxLvl)(unsafe.Pointer(pLvl)).FpData != 0 {
					_fts5DlidxLvlNext(tls, pLvl)
				}
			}
		}
	}
	return (*(*TFts5DlidxLvl)(unsafe.Pointer(pIter + 8))).FbEof
}

func _fts5DlidxIterPrevR(tls *libc.TLS, p uintptr, pIter uintptr, iLvl int32) (r int32) {
	var pLvl uintptr
	_ = pLvl
	pLvl = pIter + 8 + uintptr(iLvl)*32
	if _fts5DlidxLvlPrev(tls, pLvl) != 0 {
		if iLvl+int32(1) < (*TFts5DlidxIter)(unsafe.Pointer(pIter)).FnLvl {
			_fts5DlidxIterPrevR(tls, p, pIter, iLvl+int32(1))
			if (**(**TFts5DlidxLvl)(__ccgo_up(pLvl + 1*32))).FbEof == 0 {
				_fts5DataRelease(tls, (*TFts5DlidxLvl)(unsafe.Pointer(pLvl)).FpData)
				libc.Xmemset(tls, pLvl, 0, uint64(32))
				(*TFts5DlidxLvl)(unsafe.Pointer(pLvl)).FpData = _fts5DataRead(tls, p, int64((*TFts5DlidxIter)(unsafe.Pointer(pIter)).FiSegid)<<(libc.Int32FromInt32(FTS5_DATA_PAGE_B)+libc.Int32FromInt32(FTS5_DATA_HEIGHT_B)+libc.Int32FromInt32(FTS5_DATA_DLI_B))+int64(libc.Int32FromInt32(1))<<(libc.Int32FromInt32(FTS5_DATA_PAGE_B)+libc.Int32FromInt32(FTS5_DATA_HEIGHT_B))+int64(iLvl)<<libc.Int32FromInt32(FTS5_DATA_PAGE_B)+int64((**(**TFts5DlidxLvl)(__ccgo_up(pLvl + 1*32))).FiLeafPgno))
				if (*TFts5DlidxLvl)(unsafe.Pointer(pLvl)).FpData != 0 {
					for _fts5DlidxLvlNext(tls, pLvl) == 0 {
					}
					(*TFts5DlidxLvl)(unsafe.Pointer(pLvl)).FbEof = 0
				}
			}
		}
	}
	return (*(*TFts5DlidxLvl)(unsafe.Pointer(pIter + 8))).FbEof
}

func _fts5DoclistIterInit(tls *libc.TLS, pBuf uintptr, pIter uintptr) {
	libc.Xmemset(tls, pIter, 0, uint64(32))
	if (*TFts5Buffer)(unsafe.Pointer(pBuf)).Fn > 0 {
		(*TFts5DoclistIter)(unsafe.Pointer(pIter)).FaPoslist = (*TFts5Buffer)(unsafe.Pointer(pBuf)).Fp
		(*TFts5DoclistIter)(unsafe.Pointer(pIter)).FaEof = (*TFts5Buffer)(unsafe.Pointer(pBuf)).Fp + uintptr((*TFts5Buffer)(unsafe.Pointer(pBuf)).Fn)
		_fts5DoclistIterNext(tls, pIter)
	}
}

// C documentation
//
//	/*
//	** Find a tokenizer. This is the implementation of the
//	** fts5_api.xFindTokenizer() method.
//	*/
func _fts5FindTokenizer(tls *libc.TLS, pApi uintptr, zName uintptr, ppUserData uintptr, pTokenizer uintptr) (r int32) {
	var pMod uintptr
	var rc int32
	_, _ = pMod, rc
	rc = SQLITE_OK
	pMod = _fts5LocateTokenizer(tls, pApi, zName)
	if pMod != 0 {
		if (*TFts5TokenizerModule)(unsafe.Pointer(pMod)).FbV2Native == 0 {
			**(**uintptr)(__ccgo_up(ppUserData)) = (*TFts5TokenizerModule)(unsafe.Pointer(pMod)).FpUserData
		} else {
			**(**uintptr)(__ccgo_up(ppUserData)) = pMod
		}
		**(**Tfts5_tokenizer)(__ccgo_up(pTokenizer)) = (*TFts5TokenizerModule)(unsafe.Pointer(pMod)).Fx1
	} else {
		libc.Xmemset(tls, pTokenizer, 0, uint64(24))
		**(**uintptr)(__ccgo_up(ppUserData)) = uintptr(0)
		rc = int32(SQLITE_ERROR)
	}
	return rc
}

func _fts5LookaheadReaderInit(tls *libc.TLS, a uintptr, n int32, p uintptr) (r int32) {
	libc.Xmemset(tls, p, 0, uint64(32))
	(*TFts5LookaheadReader)(unsafe.Pointer(p)).Fa = a
	(*TFts5LookaheadReader)(unsafe.Pointer(p)).Fn = n
	_fts5LookaheadReaderNext(tls, p)
	return _fts5LookaheadReaderNext(tls, p)
}

// C documentation
//
//	/*
//	** Zero the iterator passed as the only argument.
//	*/
func _fts5SegIterClear(tls *libc.TLS, pIter uintptr) {
	_sqlite3Fts5BufferFree(tls, pIter+96)
	_fts5DataRelease(tls, (*TFts5SegIter)(unsafe.Pointer(pIter)).FpLeaf)
	_fts5DataRelease(tls, (*TFts5SegIter)(unsafe.Pointer(pIter)).FpNextLeaf)
	_fts5TombstoneArrayDelete(tls, (*TFts5SegIter)(unsafe.Pointer(pIter)).FpTombArray)
	_fts5DlidxIterFree(tls, (*TFts5SegIter)(unsafe.Pointer(pIter)).FpDlidx)
	Xsqlite3_free(tls, (*TFts5SegIter)(unsafe.Pointer(pIter)).FaRowidOffset)
	libc.Xmemset(tls, pIter, 0, uint64(128))
}

// C documentation
//
//	/*
//	** Initialize the iterator object pIter to iterate through the entries in
//	** segment pSeg. The iterator is left pointing to the first entry when
//	** this function returns.
//	**
//	** If an error occurs, Fts5Index.rc is set to an appropriate error code. If
//	** an error has already occurred when this function is called, it is a no-op.
//	*/
func _fts5SegIterInit(tls *libc.TLS, p uintptr, pSeg uintptr, pIter uintptr) {
	if (*TFts5StructureSegment)(unsafe.Pointer(pSeg)).FpgnoFirst == 0 {
		/* This happens if the segment is being used as an input to an incremental
		 ** merge and all data has already been "trimmed". See function
		 ** fts5TrimSegments() for details. In this case leave the iterator empty.
		 ** The caller will see the (pIter->pLeaf==0) and assume the iterator is
		 ** at EOF already. */
		return
	}
	if (*TFts5Index)(unsafe.Pointer(p)).Frc == SQLITE_OK {
		libc.Xmemset(tls, pIter, 0, uint64(128))
		_fts5SegIterSetNext(tls, p, pIter)
		(*TFts5SegIter)(unsafe.Pointer(pIter)).FpSeg = pSeg
		(*TFts5SegIter)(unsafe.Pointer(pIter)).FiLeafPgno = (*TFts5StructureSegment)(unsafe.Pointer(pSeg)).FpgnoFirst - int32(1)
		for cond := true; cond; cond = (*TFts5Index)(unsafe.Pointer(p)).Frc == SQLITE_OK && (*TFts5SegIter)(unsafe.Pointer(pIter)).FpLeaf != 0 && (*TFts5Data)(unsafe.Pointer((*TFts5SegIter)(unsafe.Pointer(pIter)).FpLeaf)).Fnn == int32(4) {
			_fts5SegIterNextPage(tls, p, pIter)
		}
	}
	if (*TFts5Index)(unsafe.Pointer(p)).Frc == SQLITE_OK && (*TFts5SegIter)(unsafe.Pointer(pIter)).FpLeaf != 0 {
		(*TFts5SegIter)(unsafe.Pointer(pIter)).FiLeafOffset = int64(4)
		(*TFts5SegIter)(unsafe.Pointer(pIter)).FiPgidxOff = (*TFts5Data)(unsafe.Pointer((*TFts5SegIter)(unsafe.Pointer(pIter)).FpLeaf)).FszLeaf + int32(1)
		_fts5SegIterLoadTerm(tls, p, pIter, 0)
		_fts5SegIterLoadNPos(tls, p, pIter)
		_fts5SegIterAllocTombstone(tls, p, pIter)
	}
}

// C documentation
//
//	/*
//	** This is similar to fts5SegIterSeekInit(), except that it initializes
//	** the segment iterator to point to the first term following the page
//	** with pToken/nToken on it.
//	*/
func _fts5SegIterNextInit(tls *libc.TLS, p uintptr, pTerm uintptr, nTerm int32, pSeg uintptr, pIter uintptr) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var a, pSel uintptr
	var bDlidx, iPg int32
	var val Ti64
	var _ /* iTermOff at bp+0 */ int32
	_, _, _, _, _ = a, bDlidx, iPg, pSel, val
	iPg = -int32(1) /* Page of segment to open */
	bDlidx = 0
	pSel = uintptr(0) /* SELECT to find iPg */
	pSel = _fts5IdxNextStmt(tls, p)
	if pSel != 0 {
		Xsqlite3_bind_int(tls, pSel, int32(1), (*TFts5StructureSegment)(unsafe.Pointer(pSeg)).FiSegid)
		Xsqlite3_bind_blob(tls, pSel, int32(2), pTerm, nTerm, libc.UintptrFromInt32(0))
		if Xsqlite3_step(tls, pSel) == int32(SQLITE_ROW) {
			val = Xsqlite3_column_int64(tls, pSel, 0)
			iPg = int32(val >> libc.Int32FromInt32(1))
			bDlidx = int32(val & libc.Int64FromInt32(0x0001))
		}
		(*TFts5Index)(unsafe.Pointer(p)).Frc = Xsqlite3_reset(tls, pSel)
		Xsqlite3_bind_null(tls, pSel, int32(2))
		if (*TFts5Index)(unsafe.Pointer(p)).Frc != 0 {
			return
		}
	}
	libc.Xmemset(tls, pIter, 0, uint64(128))
	(*TFts5SegIter)(unsafe.Pointer(pIter)).FpSeg = pSeg
	**(**int32)(__ccgo_up(pIter + 8)) |= int32(FTS5_SEGITER_ONETERM)
	if iPg >= 0 {
		(*TFts5SegIter)(unsafe.Pointer(pIter)).FiLeafPgno = iPg - int32(1)
		_fts5SegIterNextPage(tls, p, pIter)
		_fts5SegIterSetNext(tls, p, pIter)
	}
	if (*TFts5SegIter)(unsafe.Pointer(pIter)).FpLeaf != 0 {
		a = (*TFts5Data)(unsafe.Pointer((*TFts5SegIter)(unsafe.Pointer(pIter)).FpLeaf)).Fp
		**(**int32)(__ccgo_up(bp)) = 0
		(*TFts5SegIter)(unsafe.Pointer(pIter)).FiPgidxOff = (*TFts5Data)(unsafe.Pointer((*TFts5SegIter)(unsafe.Pointer(pIter)).FpLeaf)).FszLeaf
		**(**int32)(__ccgo_up(pIter + 64)) += _sqlite3Fts5GetVarint32(tls, a+uintptr((*TFts5SegIter)(unsafe.Pointer(pIter)).FiPgidxOff), bp)
		(*TFts5SegIter)(unsafe.Pointer(pIter)).FiLeafOffset = int64(**(**int32)(__ccgo_up(bp)))
		_fts5SegIterLoadTerm(tls, p, pIter, 0)
		_fts5SegIterLoadNPos(tls, p, pIter)
		if bDlidx != 0 {
			_fts5SegIterLoadDlidx(tls, p, pIter)
		}
	}
}

// C documentation
//
//	/*
//	** Initialize the object pIter to point to term pTerm/nTerm within segment
//	** pSeg. If there is no such term in the index, the iterator is set to EOF.
//	**
//	** If an error occurs, Fts5Index.rc is set to an appropriate error code. If
//	** an error has already occurred when this function is called, it is a no-op.
//	*/
func _fts5SegIterSeekInit(tls *libc.TLS, p uintptr, pTerm uintptr, nTerm int32, flags int32, pSeg uintptr, pIter uintptr) {
	var bDlidx, bGe, iPg int32
	var pIdxSelect uintptr
	var val Ti64
	_, _, _, _, _ = bDlidx, bGe, iPg, pIdxSelect, val
	iPg = int32(1)
	bGe = flags & int32(FTS5INDEX_QUERY_SCAN)
	bDlidx = 0 /* True if there is a doclist-index */
	pIdxSelect = uintptr(0)
	libc.Xmemset(tls, pIter, 0, uint64(128))
	(*TFts5SegIter)(unsafe.Pointer(pIter)).FpSeg = pSeg
	/* This block sets stack variable iPg to the leaf page number that may
	 ** contain term (pTerm/nTerm), if it is present in the segment. */
	pIdxSelect = _fts5IdxSelectStmt(tls, p)
	if (*TFts5Index)(unsafe.Pointer(p)).Frc != 0 {
		return
	}
	Xsqlite3_bind_int(tls, pIdxSelect, int32(1), (*TFts5StructureSegment)(unsafe.Pointer(pSeg)).FiSegid)
	Xsqlite3_bind_blob(tls, pIdxSelect, int32(2), pTerm, nTerm, libc.UintptrFromInt32(0))
	if int32(SQLITE_ROW) == Xsqlite3_step(tls, pIdxSelect) {
		val = int64(Xsqlite3_column_int(tls, pIdxSelect, 0))
		iPg = int32(val >> libc.Int32FromInt32(1))
		bDlidx = int32(val & libc.Int64FromInt32(0x0001))
	}
	(*TFts5Index)(unsafe.Pointer(p)).Frc = Xsqlite3_reset(tls, pIdxSelect)
	Xsqlite3_bind_null(tls, pIdxSelect, int32(2))
	if iPg < (*TFts5StructureSegment)(unsafe.Pointer(pSeg)).FpgnoFirst {
		iPg = (*TFts5StructureSegment)(unsafe.Pointer(pSeg)).FpgnoFirst
		bDlidx = 0
	}
	(*TFts5SegIter)(unsafe.Pointer(pIter)).FiLeafPgno = iPg - int32(1)
	_fts5SegIterNextPage(tls, p, pIter)
	if (*TFts5SegIter)(unsafe.Pointer(pIter)).FpLeaf != 0 {
		_fts5LeafSeek(tls, p, bGe, pIter, pTerm, nTerm)
	}
	if (*TFts5Index)(unsafe.Pointer(p)).Frc == SQLITE_OK && (bGe == 0 || flags&int32(FTS5INDEX_QUERY_SCANONETERM) != 0) {
		**(**int32)(__ccgo_up(pIter + 8)) |= int32(FTS5_SEGITER_ONETERM)
		if (*TFts5SegIter)(unsafe.Pointer(pIter)).FpLeaf != 0 {
			if flags&int32(FTS5INDEX_QUERY_DESC) != 0 {
				**(**int32)(__ccgo_up(pIter + 8)) |= int32(FTS5_SEGITER_REVERSE)
			}
			if bDlidx != 0 {
				_fts5SegIterLoadDlidx(tls, p, pIter)
			}
			if flags&int32(FTS5INDEX_QUERY_DESC) != 0 {
				_fts5SegIterReverse(tls, p, pIter)
			}
		}
	}
	_fts5SegIterSetNext(tls, p, pIter)
	if 0 == flags&int32(FTS5INDEX_QUERY_SCANONETERM) {
		_fts5SegIterAllocTombstone(tls, p, pIter)
	}
	/* Either:
	 **
	 **   1) an error has occurred, or
	 **   2) the iterator points to EOF, or
	 **   3) the iterator points to an entry with term (pTerm/nTerm), or
	 **   4) the FTS5INDEX_QUERY_SCAN flag was set and the iterator points
	 **      to an entry with a term greater than or equal to (pTerm/nTerm).
	 */
}

// C documentation
//
//	/*
//	** Store the current contents of the p->nTotalRow and p->aTotalSize[]
//	** variables in the "averages" record on disk.
//	**
//	** Return SQLITE_OK if successful, or an SQLite error code if an error
//	** occurs.
//	*/
func _fts5StorageSaveTotals(tls *libc.TLS, p uintptr) (r int32) {
	bp := tls.Alloc(32)
	defer tls.Free(32)
	var i, nCol int32
	var _ /* buf at bp+0 */ TFts5Buffer
	var _ /* rc at bp+16 */ int32
	_, _ = i, nCol
	nCol = (*TFts5Config)(unsafe.Pointer((*TFts5Storage)(unsafe.Pointer(p)).FpConfig)).FnCol
	**(**int32)(__ccgo_up(bp + 16)) = SQLITE_OK
	libc.Xmemset(tls, bp, 0, uint64(16))
	_sqlite3Fts5BufferAppendVarint(tls, bp+16, bp, (*TFts5Storage)(unsafe.Pointer(p)).FnTotalRow)
	i = 0
	for {
		if !(i < nCol) {
			break
		}
		_sqlite3Fts5BufferAppendVarint(tls, bp+16, bp, **(**Ti64)(__ccgo_up((*TFts5Storage)(unsafe.Pointer(p)).FaTotalSize + uintptr(i)*8)))
		goto _1
	_1:
		;
		i = i + 1
	}
	if **(**int32)(__ccgo_up(bp + 16)) == SQLITE_OK {
		**(**int32)(__ccgo_up(bp + 16)) = _sqlite3Fts5IndexSetAverages(tls, (*TFts5Storage)(unsafe.Pointer(p)).FpIndex, (**(**TFts5Buffer)(__ccgo_up(bp))).Fp, (**(**TFts5Buffer)(__ccgo_up(bp))).Fn)
	}
	Xsqlite3_free(tls, (**(**TFts5Buffer)(__ccgo_up(bp))).Fp)
	return **(**int32)(__ccgo_up(bp + 16))
}

// C documentation
//
//	/*
//	** Return a copy of index structure pStruct. Except, promote as many
//	** segments as possible to level iPromote. If an OOM occurs, NULL is
//	** returned.
//	*/
func _fts5StructurePromoteTo(tls *libc.TLS, p uintptr, iPromote int32, szPromote int32, pStruct uintptr) {
	var il, is, sz int32
	var pLvl, pOut uintptr
	_, _, _, _, _ = il, is, pLvl, pOut, sz
	pOut = pStruct + 32 + uintptr(iPromote)*16
	if (*TFts5StructureLevel)(unsafe.Pointer(pOut)).FnMerge == 0 {
		il = iPromote + int32(1)
		for {
			if !(il < (*TFts5Structure)(unsafe.Pointer(pStruct)).FnLevel) {
				break
			}
			pLvl = pStruct + 32 + uintptr(il)*16
			if (*TFts5StructureLevel)(unsafe.Pointer(pLvl)).FnMerge != 0 {
				return
			}
			is = (*TFts5StructureLevel)(unsafe.Pointer(pLvl)).FnSeg - int32(1)
			for {
				if !(is >= 0) {
					break
				}
				sz = _fts5SegmentSize(tls, (*TFts5StructureLevel)(unsafe.Pointer(pLvl)).FaSeg+uintptr(is)*56)
				if sz > szPromote {
					return
				}
				_fts5StructureExtendLevel(tls, p+60, pStruct, iPromote, int32(1), int32(1))
				if (*TFts5Index)(unsafe.Pointer(p)).Frc != 0 {
					return
				}
				libc.Xmemcpy(tls, (*TFts5StructureLevel)(unsafe.Pointer(pOut)).FaSeg, (*TFts5StructureLevel)(unsafe.Pointer(pLvl)).FaSeg+uintptr(is)*56, uint64(56))
				(*TFts5StructureLevel)(unsafe.Pointer(pOut)).FnSeg = (*TFts5StructureLevel)(unsafe.Pointer(pOut)).FnSeg + 1
				(*TFts5StructureLevel)(unsafe.Pointer(pLvl)).FnSeg = (*TFts5StructureLevel)(unsafe.Pointer(pLvl)).FnSeg - 1
				goto _2
			_2:
				;
				is = is - 1
			}
			goto _1
		_1:
			;
			il = il + 1
		}
	}
}

// C documentation
//
//	/*
//	** The two input arrays - a1[] and a2[] - are in sorted order. This function
//	** merges the two arrays together and writes the result to output array
//	** aOut[]. aOut[] is guaranteed to be large enough to hold the result.
//	**
//	** Duplicate entries are copied into the output. So the size of the output
//	** array is always (n1+n2) entries.
//	*/
func _fts5TokendataMerge(tls *libc.TLS, a1 uintptr, n1 int32, a2 uintptr, n2 int32, aOut uintptr) {
	var i1, i2 int32
	var pOut uintptr
	_, _, _ = i1, i2, pOut
	i1 = 0
	i2 = 0
	for i1 < n1 || i2 < n2 {
		pOut = aOut + uintptr(i1+i2)*24
		if i2 >= n2 || i1 < n1 && ((**(**TFts5TokenDataMap)(__ccgo_up(a1 + uintptr(i1)*24))).FiRowid < (**(**TFts5TokenDataMap)(__ccgo_up(a2 + uintptr(i2)*24))).FiRowid || (**(**TFts5TokenDataMap)(__ccgo_up(a1 + uintptr(i1)*24))).FiRowid == (**(**TFts5TokenDataMap)(__ccgo_up(a2 + uintptr(i2)*24))).FiRowid && (**(**TFts5TokenDataMap)(__ccgo_up(a1 + uintptr(i1)*24))).FiPos <= (**(**TFts5TokenDataMap)(__ccgo_up(a2 + uintptr(i2)*24))).FiPos) {
			libc.Xmemcpy(tls, pOut, a1+uintptr(i1)*24, uint64(24))
			i1 = i1 + 1
		} else {
			libc.Xmemcpy(tls, pOut, a2+uintptr(i2)*24, uint64(24))
			i2 = i2 + 1
		}
	}
}

// C documentation
//
//	/*
//	** Implementation of the geopoly_group_bbox(X) aggregate SQL function.
//	*/
func _geopolyBBoxStep(tls *libc.TLS, context uintptr, argc int32, argv uintptr) {
	bp := tls.Alloc(32)
	defer tls.Free(32)
	var pBBox uintptr
	var _ /* a at bp+0 */ [4]TRtreeCoord
	var _ /* rc at bp+16 */ int32
	_ = pBBox
	**(**int32)(__ccgo_up(bp + 16)) = SQLITE_OK
	_ = argc
	_geopolyBBox(tls, context, **(**uintptr)(__ccgo_up(argv)), bp, bp+16)
	if **(**int32)(__ccgo_up(bp + 16)) == SQLITE_OK {
		pBBox = Xsqlite3_aggregate_context(tls, context, int32(20))
		if pBBox == uintptr(0) {
			return
		}
		if libc.AtomicLoadPInt32(pBBox) == 0 {
			libc.AtomicStorePInt32(pBBox, int32(1))
			libc.Xmemcpy(tls, pBBox+4, bp, libc.Uint64FromInt64(4)*libc.Uint64FromInt32(4))
		} else {
			if *(*TRtreeValue)(unsafe.Pointer(bp)) < *(*TRtreeValue)(unsafe.Pointer(pBBox + 4)) {
				**(**TRtreeCoord)(__ccgo_up(pBBox + 4)) = (**(**[4]TRtreeCoord)(__ccgo_up(bp)))[0]
			}
			if *(*TRtreeValue)(unsafe.Pointer(bp + 1*4)) > *(*TRtreeValue)(unsafe.Pointer(pBBox + 4 + 1*4)) {
				**(**TRtreeCoord)(__ccgo_up(pBBox + 4 + 1*4)) = (**(**[4]TRtreeCoord)(__ccgo_up(bp)))[int32(1)]
			}
			if *(*TRtreeValue)(unsafe.Pointer(bp + 2*4)) < *(*TRtreeValue)(unsafe.Pointer(pBBox + 4 + 2*4)) {
				**(**TRtreeCoord)(__ccgo_up(pBBox + 4 + 2*4)) = (**(**[4]TRtreeCoord)(__ccgo_up(bp)))[int32(2)]
			}
			if *(*TRtreeValue)(unsafe.Pointer(bp + 3*4)) > *(*TRtreeValue)(unsafe.Pointer(pBBox + 4 + 3*4)) {
				**(**TRtreeCoord)(__ccgo_up(pBBox + 4 + 3*4)) = (**(**[4]TRtreeCoord)(__ccgo_up(bp)))[int32(3)]
			}
		}
	}
}

// C documentation
//
//	/*
//	** Transfer eligible terms from the HAVING clause of a query, which is
//	** processed after grouping, to the WHERE clause, which is processed before
//	** grouping. For example, the query:
//	**
//	**   SELECT * FROM <tables> WHERE a=? GROUP BY b HAVING b=? AND c=?
//	**
//	** can be rewritten as:
//	**
//	**   SELECT * FROM <tables> WHERE a=? AND b=? GROUP BY b HAVING c=?
//	**
//	** A term of the HAVING expression is eligible for transfer if it consists
//	** entirely of constants and expressions that are also GROUP BY terms that
//	** use the "BINARY" collation sequence.
//	*/
func _havingToWhere(tls *libc.TLS, pParse uintptr, p uintptr) {
	bp := tls.Alloc(48)
	defer tls.Free(48)
	var _ /* sWalker at bp+0 */ TWalker
	libc.Xmemset(tls, bp, 0, uint64(48))
	(**(**TWalker)(__ccgo_up(bp))).FpParse = pParse
	(**(**TWalker)(__ccgo_up(bp))).FxExprCallback = __ccgo_fp(_havingToWhereExprCb)
	*(*uintptr)(unsafe.Pointer(bp + 40)) = p
	_sqlite3WalkExpr(tls, bp, (*TSelect)(unsafe.Pointer(p)).FpHaving)
}

func _incrAggFunctionDepth(tls *libc.TLS, pExpr uintptr, N int32) {
	bp := tls.Alloc(48)
	defer tls.Free(48)
	var _ /* w at bp+0 */ TWalker
	if N > 0 {
		libc.Xmemset(tls, bp, 0, uint64(48))
		(**(**TWalker)(__ccgo_up(bp))).FxExprCallback = __ccgo_fp(_incrAggDepth)
		*(*int32)(unsafe.Pointer(bp + 40)) = N
		_sqlite3WalkExpr(tls, bp, pExpr)
	}
}

func _jsonAppendRaw(tls *libc.TLS, p uintptr, zIn uintptr, N Tu32) {
	if N == uint32(0) {
		return
	}
	if uint64(N)+(*TJsonString)(unsafe.Pointer(p)).FnUsed >= (*TJsonString)(unsafe.Pointer(p)).FnAlloc {
		_jsonStringExpandAndAppend(tls, p, zIn, N)
	} else {
		libc.Xmemcpy(tls, (*TJsonString)(unsafe.Pointer(p)).FzBuf+uintptr((*TJsonString)(unsafe.Pointer(p)).FnUsed), zIn, uint64(N))
		**(**Tu64)(__ccgo_up(p + 24)) += uint64(N)
	}
}

func _jsonAppendRawNZ(tls *libc.TLS, p uintptr, zIn uintptr, N Tu32) {
	if uint64(N)+(*TJsonString)(unsafe.Pointer(p)).FnUsed >= (*TJsonString)(unsafe.Pointer(p)).FnAlloc {
		_jsonStringExpandAndAppend(tls, p, zIn, N)
	} else {
		libc.Xmemcpy(tls, (*TJsonString)(unsafe.Pointer(p)).FzBuf+uintptr((*TJsonString)(unsafe.Pointer(p)).FnUsed), zIn, uint64(N))
		**(**Tu64)(__ccgo_up(p + 24)) += uint64(N)
	}
}

// C documentation
//
//	/*
//	** If pParse->aBlob is not previously editable (because it is taken
//	** from sqlite3_value_blob(), as indicated by the fact that
//	** pParse->nBlobAlloc==0 and pParse->nBlob>0) then make it editable
//	** by making a copy into space obtained from malloc.
//	**
//	** Return true on success.  Return false on OOM.
//	*/
func _jsonBlobMakeEditable(tls *libc.TLS, pParse uintptr, nExtra Tu32) (r int32) {
	var aOld uintptr
	var nSize Tu32
	_, _ = aOld, nSize
	if (*TJsonParse)(unsafe.Pointer(pParse)).Foom != 0 {
		return 0
	}
	if (*TJsonParse)(unsafe.Pointer(pParse)).FnBlobAlloc > uint32(0) {
		return int32(1)
	}
	aOld = (*TJsonParse)(unsafe.Pointer(pParse)).FaBlob
	nSize = (*TJsonParse)(unsafe.Pointer(pParse)).FnBlob + nExtra
	(*TJsonParse)(unsafe.Pointer(pParse)).FaBlob = uintptr(0)
	if _jsonBlobExpand(tls, pParse, nSize) != 0 {
		return 0
	}
	libc.Xmemcpy(tls, (*TJsonParse)(unsafe.Pointer(pParse)).FaBlob, aOld, uint64((*TJsonParse)(unsafe.Pointer(pParse)).FnBlob))
	return int32(1)
}

// C documentation
//
//	/*
//	** Convert a JSON BLOB into text and make that text the return value
//	** of an SQL function.
//	*/
func _jsonReturnTextJsonFromBlob(tls *libc.TLS, ctx uintptr, aBlob uintptr, nBlob Tu32) {
	bp := tls.Alloc(208)
	defer tls.Free(208)
	var _ /* s at bp+72 */ TJsonString
	var _ /* x at bp+0 */ TJsonParse
	if aBlob == uintptr(0) {
		return
	}
	libc.Xmemset(tls, bp, 0, uint64(72))
	(**(**TJsonParse)(__ccgo_up(bp))).FaBlob = aBlob
	(**(**TJsonParse)(__ccgo_up(bp))).FnBlob = nBlob
	_jsonStringInit(tls, bp+72, ctx)
	_jsonTranslateBlobToText(tls, bp, uint32(0), bp+72)
	_jsonReturnString(tls, bp+72, uintptr(0), uintptr(0))
}

// C documentation
//
//	/* Append N bytes from zIn onto the end of the JsonString string.
//	*/
func _jsonStringExpandAndAppend(tls *libc.TLS, p uintptr, zIn uintptr, N Tu32) {
	if _jsonStringGrow(tls, p, N) != 0 {
		return
	}
	libc.Xmemcpy(tls, (*TJsonString)(unsafe.Pointer(p)).FzBuf+uintptr((*TJsonString)(unsafe.Pointer(p)).FnUsed), zIn, uint64(N))
	**(**Tu64)(__ccgo_up(p + 24)) += uint64(N)
}

// C documentation
//
//	/* Enlarge pJson->zBuf so that it can hold at least N more bytes.
//	** Return zero on success.  Return non-zero on an OOM error
//	*/
func _jsonStringGrow(tls *libc.TLS, p uintptr, N Tu32) (r int32) {
	var nTotal Tu64
	var zNew, v2 uintptr
	var v1 uint64
	_, _, _, _ = nTotal, zNew, v1, v2
	if uint64(N) < (*TJsonString)(unsafe.Pointer(p)).FnAlloc {
		v1 = (*TJsonString)(unsafe.Pointer(p)).FnAlloc * uint64(2)
	} else {
		v1 = (*TJsonString)(unsafe.Pointer(p)).FnAlloc + uint64(N) + uint64(10)
	}
	nTotal = v1
	if (*TJsonString)(unsafe.Pointer(p)).FbStatic != 0 {
		if (*TJsonString)(unsafe.Pointer(p)).FeErr != 0 {
			return int32(1)
		}
		zNew = _sqlite3RCStrNew(tls, nTotal)
		if zNew == uintptr(0) {
			_jsonStringOom(tls, p)
			return int32(SQLITE_NOMEM)
		}
		libc.Xmemcpy(tls, zNew, (*TJsonString)(unsafe.Pointer(p)).FzBuf, (*TJsonString)(unsafe.Pointer(p)).FnUsed)
		(*TJsonString)(unsafe.Pointer(p)).FzBuf = zNew
		(*TJsonString)(unsafe.Pointer(p)).FbStatic = uint8(0)
	} else {
		(*TJsonString)(unsafe.Pointer(p)).FzBuf = _sqlite3RCStrResize(tls, (*TJsonString)(unsafe.Pointer(p)).FzBuf, nTotal)
		if (*TJsonString)(unsafe.Pointer(p)).FzBuf == uintptr(0) {
			v2 = p + 33
			*(*Tu8)(unsafe.Pointer(v2)) = Tu8(int32(*(*Tu8)(unsafe.Pointer(v2))) | libc.Int32FromInt32(JSTRING_OOM))
			_jsonStringZero(tls, p)
			return int32(SQLITE_NOMEM)
		}
	}
	(*TJsonString)(unsafe.Pointer(p)).FnAlloc = nTotal
	return SQLITE_OK
}

// C documentation
//
//	/*
//	** Flush the contents of memory to a real file on disk.
//	*/
func _memjrnlCreateFile(tls *libc.TLS, p uintptr) (r int32) {
	var copy1 TMemJournal
	var iOff Ti64
	var nChunk, rc int32
	var pIter, pReal uintptr
	_, _, _, _, _, _ = copy1, iOff, nChunk, pIter, pReal, rc
	pReal = p
	copy1 = **(**TMemJournal)(__ccgo_up(p))
	libc.Xmemset(tls, p, 0, uint64(80))
	rc = _sqlite3OsOpen(tls, copy1.FpVfs, copy1.FzJournal, pReal, copy1.Fflags, uintptr(0))
	if rc == SQLITE_OK {
		nChunk = copy1.FnChunkSize
		iOff = 0
		pIter = copy1.FpFirst
		for {
			if !(pIter != 0) {
				break
			}
			if iOff+int64(nChunk) > copy1.Fendpoint.FiOffset {
				nChunk = int32(copy1.Fendpoint.FiOffset - iOff)
			}
			rc = _sqlite3OsWrite(tls, pReal, pIter+8, nChunk, iOff)
			if rc != 0 {
				break
			}
			iOff = iOff + int64(nChunk)
			goto _1
		_1:
			;
			pIter = (*TFileChunk)(unsafe.Pointer(pIter)).FpNext
		}
		if rc == SQLITE_OK {
			/* No error has occurred. Free the in-memory buffers. */
			_memjrnlFreeChunks(tls, copy1.FpFirst)
		}
	}
	if rc != SQLITE_OK {
		/* If an error occurred while creating or writing to the file, restore
		 ** the original before returning. This way, SQLite uses the in-memory
		 ** journal data to roll back changes made to the internal page-cache
		 ** before this function was called.  */
		_sqlite3OsClose(tls, pReal)
		**(**TMemJournal)(__ccgo_up(p)) = copy1
	}
	return rc
}

// C documentation
//
//	/*
//	** This routine is called to increment the value of the database file
//	** change-counter, stored as a 4-byte big-endian integer starting at
//	** byte offset 24 of the pager file.  The secondary change counter at
//	** 92 is also updated, as is the SQLite version number at offset 96.
//	**
//	** But this only happens if the pPager->changeCountDone flag is false.
//	** To avoid excess churning of page 1, the update only happens once.
//	** See also the pager_write_changecounter() routine that does an
//	** unconditional update of the change counters.
//	**
//	** If the isDirectMode flag is zero, then this is done by calling
//	** sqlite3PagerWrite() on page 1, then modifying the contents of the
//	** page data. In this case the file will be updated when the current
//	** transaction is committed.
//	**
//	** The isDirectMode flag may only be non-zero if the library was compiled
//	** with the SQLITE_ENABLE_ATOMIC_WRITE macro defined. In this case,
//	** if isDirect is non-zero, then the database file is updated directly
//	** by writing an updated version of page 1 using a call to the
//	** sqlite3OsWrite() function.
//	*/
func _pager_incr_changecounter(tls *libc.TLS, pPager uintptr, isDirectMode int32) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var pCopy, zBuf uintptr
	var rc int32
	var _ /* pPgHdr at bp+0 */ uintptr
	_, _, _ = pCopy, rc, zBuf
	rc = SQLITE_OK
	/* Declare and initialize constant integer 'isDirect'. If the
	 ** atomic-write optimization is enabled in this build, then isDirect
	 ** is initialized to the value passed as the isDirectMode parameter
	 ** to this function. Otherwise, it is always set to zero.
	 **
	 ** The idea is that if the atomic-write optimization is not
	 ** enabled at compile time, the compiler can omit the tests of
	 ** 'isDirect' below, as well as the block enclosed in the
	 ** "if( isDirect )" condition.
	 */
	_ = isDirectMode
	if !((*TPager)(unsafe.Pointer(pPager)).FchangeCountDone != 0) && (*TPager)(unsafe.Pointer(pPager)).FdbSize > uint32(0) { /* Reference to page 1 */
		/* Open page 1 of the file for writing. */
		rc = _sqlite3PagerGet(tls, pPager, uint32(1), bp, 0)
		/* If page one was fetched successfully, and this function is not
		 ** operating in direct-mode, make page 1 writable.  When not in
		 ** direct mode, page 1 is always held in cache and hence the PagerGet()
		 ** above is always successful - hence the ALWAYS on rc==SQLITE_OK.
		 */
		if libc.Bool(!(libc.Int32FromInt32(DIRECT_MODE) != 0)) && rc == SQLITE_OK {
			rc = _sqlite3PagerWrite(tls, **(**uintptr)(__ccgo_up(bp)))
		}
		if rc == SQLITE_OK {
			/* Actually do the update of the change counter */
			_pager_write_changecounter(tls, **(**uintptr)(__ccgo_up(bp)))
			/* If running in direct mode, write the contents of page 1 to the file. */
			if DIRECT_MODE != 0 {
				zBuf = (*TPgHdr)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp)))).FpData
				if rc == SQLITE_OK {
					rc = _sqlite3OsWrite(tls, (*TPager)(unsafe.Pointer(pPager)).Ffd, zBuf, int32((*TPager)(unsafe.Pointer(pPager)).FpageSize), 0)
					**(**Tu32)(__ccgo_up(pPager + 248 + 2*4)) = **(**Tu32)(__ccgo_up(pPager + 248 + 2*4)) + 1
				}
				if rc == SQLITE_OK {
					/* Update the pager's copy of the change-counter. Otherwise, the
					 ** next time a read transaction is opened the cache will be
					 ** flushed (as the change-counter values will not match).  */
					pCopy = zBuf + 24
					libc.Xmemcpy(tls, pPager+136, pCopy, uint64(16))
					(*TPager)(unsafe.Pointer(pPager)).FchangeCountDone = uint8(1)
				}
			} else {
				(*TPager)(unsafe.Pointer(pPager)).FchangeCountDone = uint8(1)
			}
		}
		/* Release the page reference. */
		_sqlite3PagerUnref(tls, **(**uintptr)(__ccgo_up(bp)))
	}
	return rc
}

// C documentation
//
//	/*
//	** Implementation of the sqlite3_pcache.xInit method.
//	*/
func _pcache1Init(tls *libc.TLS, NotUsed uintptr) (r int32) {
	_ = NotUsed
	libc.Xmemset(tls, uintptr(unsafe.Pointer(&_pcache1_g)), 0, uint64(144))
	/*
	 ** The pcache1.separateCache variable is true if each PCache has its own
	 ** private PGroup (mode-1).  pcache1.separateCache is false if the single
	 ** PGroup in pcache1.grp is used for all page caches (mode-2).
	 **
	 **   *  Always use a unified cache (mode-2) if ENABLE_MEMORY_MANAGEMENT
	 **
	 **   *  Use a unified cache in single-threaded applications that have
	 **      configured a start-time buffer for use as page-cache memory using
	 **      sqlite3_config(SQLITE_CONFIG_PAGECACHE, pBuf, sz, N) with non-NULL
	 **      pBuf argument.
	 **
	 **   *  Otherwise use separate caches (mode-1)
	 */
	_pcache1_g.FseparateCache = 0
	if _sqlite3Config.FbCoreMutex != 0 {
		_pcache1_g.Fgrp.Fmutex = _sqlite3MutexAlloc(tls, int32(SQLITE_MUTEX_STATIC_LRU))
		_pcache1_g.Fmutex = _sqlite3MutexAlloc(tls, int32(SQLITE_MUTEX_STATIC_PMEM))
	}
	if _pcache1_g.FseparateCache != 0 && _sqlite3Config.FnPage != 0 && _sqlite3Config.FpPage == uintptr(0) {
		_pcache1_g.FnInitPage = _sqlite3Config.FnPage
	} else {
		_pcache1_g.FnInitPage = 0
	}
	_pcache1_g.Fgrp.FmxPinned = uint32(10)
	libc.AtomicStorePInt32(uintptr(unsafe.Pointer(&_pcache1_g))+80, int32(1))
	return SQLITE_OK
}

// C documentation
//
//	/*
//	** Implementation of the sqlite3_pcache.xShutdown method.
//	** Note that the static mutex allocated in xInit does
//	** not need to be freed.
//	*/
func _pcache1Shutdown(tls *libc.TLS, NotUsed uintptr) {
	_ = NotUsed
	libc.Xmemset(tls, uintptr(unsafe.Pointer(&_pcache1_g)), 0, uint64(144))
}

// C documentation
//
//	/*
//	** This is a helper routine for sqlite3PcacheFetchFinish()
//	**
//	** In the uncommon case where the page being fetched has not been
//	** initialized, this routine is invoked to do the initialization.
//	** This routine is broken out into a separate function since it
//	** requires extra stack manipulation that can be avoided in the common
//	** case.
//	*/
func _pcacheFetchFinishWithInit(tls *libc.TLS, pCache uintptr, pgno TPgno, pPage uintptr) (r uintptr) {
	var pPgHdr uintptr
	_ = pPgHdr
	pPgHdr = (*Tsqlite3_pcache_page)(unsafe.Pointer(pPage)).FpExtra
	libc.Xmemset(tls, pPgHdr+32, 0, libc.Uint64FromInt64(80)-uint64(libc.UintptrFromInt32(0)+32))
	(*TPgHdr)(unsafe.Pointer(pPgHdr)).FpPage = pPage
	(*TPgHdr)(unsafe.Pointer(pPgHdr)).FpData = (*Tsqlite3_pcache_page)(unsafe.Pointer(pPage)).FpBuf
	(*TPgHdr)(unsafe.Pointer(pPgHdr)).FpExtra = pPgHdr + 1*80
	libc.Xmemset(tls, (*TPgHdr)(unsafe.Pointer(pPgHdr)).FpExtra, 0, uint64(8))
	(*TPgHdr)(unsafe.Pointer(pPgHdr)).FpCache = pCache
	(*TPgHdr)(unsafe.Pointer(pPgHdr)).Fpgno = pgno
	(*TPgHdr)(unsafe.Pointer(pPgHdr)).Fflags = uint16(PGHDR_CLEAN)
	return _sqlite3PcacheFetchFinish(tls, pCache, pgno, pPage)
}

// C documentation
//
//	/*
//	** Sort the list of pages in ascending order by pgno.  Pages are
//	** connected by pDirty pointers.  The pDirtyPrev pointers are
//	** corrupted by this sort.
//	**
//	** Since there cannot be more than 2^31 distinct pages in a database,
//	** there cannot be more than 31 buckets required by the merge sorter.
//	** One extra bucket is added to catch overflow in case something
//	** ever changes to make the previous sentence incorrect.
//	*/
func _pcacheSortDirtyList(tls *libc.TLS, pIn uintptr) (r uintptr) {
	bp := tls.Alloc(256)
	defer tls.Free(256)
	var i int32
	var p, v3 uintptr
	var _ /* a at bp+0 */ [32]uintptr
	_, _, _ = i, p, v3
	libc.Xmemset(tls, bp, 0, uint64(256))
	for pIn != 0 {
		p = pIn
		pIn = (*TPgHdr)(unsafe.Pointer(p)).FpDirty
		(*TPgHdr)(unsafe.Pointer(p)).FpDirty = uintptr(0)
		i = 0
		for {
			if !(i < libc.Int32FromInt32(N_SORT_BUCKET)-libc.Int32FromInt32(1)) {
				break
			}
			if (**(**[32]uintptr)(__ccgo_up(bp)))[i] == uintptr(0) {
				(**(**[32]uintptr)(__ccgo_up(bp)))[i] = p
				break
			} else {
				p = _pcacheMergeDirtyList(tls, (**(**[32]uintptr)(__ccgo_up(bp)))[i], p)
				(**(**[32]uintptr)(__ccgo_up(bp)))[i] = uintptr(0)
			}
			goto _1
		_1:
			;
			i = i + 1
		}
		if i == libc.Int32FromInt32(N_SORT_BUCKET)-libc.Int32FromInt32(1) {
			/* To get here, there need to be 2^(N_SORT_BUCKET) elements in
			 ** the input list.  But that is impossible.
			 */
			(**(**[32]uintptr)(__ccgo_up(bp)))[i] = _pcacheMergeDirtyList(tls, (**(**[32]uintptr)(__ccgo_up(bp)))[i], p)
		}
	}
	p = (**(**[32]uintptr)(__ccgo_up(bp)))[0]
	i = int32(1)
	for {
		if !(i < int32(N_SORT_BUCKET)) {
			break
		}
		if (**(**[32]uintptr)(__ccgo_up(bp)))[i] == uintptr(0) {
			goto _2
		}
		if p != 0 {
			v3 = _pcacheMergeDirtyList(tls, p, (**(**[32]uintptr)(__ccgo_up(bp)))[i])
		} else {
			v3 = (**(**[32]uintptr)(__ccgo_up(bp)))[i]
		}
		p = v3
		goto _2
	_2:
		;
		i = i + 1
	}
	return p
}

// C documentation
//
//	/* Create a new cursor for the pragma virtual table */
func _pragmaVtabOpen(tls *libc.TLS, pVtab uintptr, ppCursor uintptr) (r int32) {
	var pCsr uintptr
	_ = pCsr
	pCsr = Xsqlite3_malloc(tls, int32(40))
	if pCsr == uintptr(0) {
		return int32(SQLITE_NOMEM)
	}
	libc.Xmemset(tls, pCsr, 0, uint64(40))
	(*TPragmaVtabCursor)(unsafe.Pointer(pCsr)).Fbase.FpVtab = pVtab
	**(**uintptr)(__ccgo_up(ppCursor)) = pCsr
	return SQLITE_OK
}

// C documentation
//
//	/*
//	** Allocate and return an RBU handle with all fields zeroed except for the
//	** error code, which is set to SQLITE_MISUSE.
//	*/
func _rbuMisuseError(tls *libc.TLS) (r uintptr) {
	var pRet uintptr
	_ = pRet
	pRet = Xsqlite3_malloc64(tls, uint64(416))
	if pRet != 0 {
		libc.Xmemset(tls, pRet, 0, uint64(416))
		(*Tsqlite3rbu)(unsafe.Pointer(pRet)).Frc = int32(SQLITE_MISUSE)
	}
	return pRet
}

// C documentation
//
//	/*
//	** Clean up any resources allocated as part of the iterator object passed
//	** as the only argument.
//	*/
func _rbuObjIterFinalize(tls *libc.TLS, pIter uintptr) {
	_rbuObjIterClearStatements(tls, pIter)
	Xsqlite3_finalize(tls, (*TRbuObjIter)(unsafe.Pointer(pIter)).FpTblIter)
	Xsqlite3_finalize(tls, (*TRbuObjIter)(unsafe.Pointer(pIter)).FpIdxIter)
	_rbuObjIterFreeCols(tls, pIter)
	libc.Xmemset(tls, pIter, 0, uint64(192))
}

// C documentation
//
//	/*
//	** The first argument must be a nul-terminated string. This function
//	** returns a copy of the string in memory obtained from sqlite3_malloc().
//	** It is the responsibility of the caller to eventually free this memory
//	** using sqlite3_free().
//	**
//	** If an OOM condition is encountered when attempting to allocate memory,
//	** output variable (*pRc) is set to SQLITE_NOMEM before returning. Otherwise,
//	** if the allocation succeeds, (*pRc) is left unchanged.
//	*/
func _rbuStrndup(tls *libc.TLS, zStr uintptr, pRc uintptr) (r uintptr) {
	var nCopy Tsize_t
	var zRet uintptr
	_, _ = nCopy, zRet
	zRet = uintptr(0)
	if **(**int32)(__ccgo_up(pRc)) == SQLITE_OK {
		if zStr != 0 {
			nCopy = libc.Xstrlen(tls, zStr) + uint64(1)
			zRet = Xsqlite3_malloc64(tls, nCopy)
			if zRet != 0 {
				libc.Xmemcpy(tls, zRet, zStr, nCopy)
			} else {
				**(**int32)(__ccgo_up(pRc)) = int32(SQLITE_NOMEM)
			}
		}
	}
	return zRet
}

func _recomputeColumnsUsed(tls *libc.TLS, pSelect uintptr, pSrcItem uintptr) {
	bp := tls.Alloc(48)
	defer tls.Free(48)
	var _ /* w at bp+0 */ TWalker
	if (*TSrcItem)(unsafe.Pointer(pSrcItem)).FpSTab == uintptr(0) {
		return
	}
	libc.Xmemset(tls, bp, 0, uint64(48))
	(**(**TWalker)(__ccgo_up(bp))).FxExprCallback = __ccgo_fp(_recomputeColumnsUsedExpr)
	(**(**TWalker)(__ccgo_up(bp))).FxSelectCallback = __ccgo_fp(_sqlite3SelectWalkNoop)
	*(*uintptr)(unsafe.Pointer(bp + 40)) = pSrcItem
	(*TSrcItem)(unsafe.Pointer(pSrcItem)).FcolUsed = uint64(0)
	_sqlite3WalkSelect(tls, bp, pSelect)
}

// C documentation
//
//	/*
//	** Resolve all symbols in the trigger at pParse->pNewTrigger, assuming
//	** it was read from the schema of database zDb. Return SQLITE_OK if
//	** successful. Otherwise, return an SQLite error code and leave an error
//	** message in the Parse object.
//	*/
func _renameResolveTrigger(tls *libc.TLS, pParse uintptr) (r int32) {
	bp := tls.Alloc(64)
	defer tls.Free(64)
	var db, p, pNew, pSel, pSrc, pStep, pUpsert, pUpsertSet uintptr
	var i, rc, v2 int32
	var _ /* sNC at bp+0 */ TNameContext
	_, _, _, _, _, _, _, _, _, _, _ = db, i, p, pNew, pSel, pSrc, pStep, pUpsert, pUpsertSet, rc, v2
	db = (*TParse)(unsafe.Pointer(pParse)).Fdb
	pNew = (*TParse)(unsafe.Pointer(pParse)).FpNewTrigger
	rc = SQLITE_OK
	libc.Xmemset(tls, bp, 0, uint64(56))
	(**(**TNameContext)(__ccgo_up(bp))).FpParse = pParse
	(*TParse)(unsafe.Pointer(pParse)).FpTriggerTab = _sqlite3FindTable(tls, db, (*TTrigger)(unsafe.Pointer(pNew)).Ftable, (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(_sqlite3SchemaToIndex(tls, db, (*TTrigger)(unsafe.Pointer(pNew)).FpTabSchema))*32))).FzDbSName)
	(*TParse)(unsafe.Pointer(pParse)).FeTriggerOp = (*TTrigger)(unsafe.Pointer(pNew)).Fop
	/* ALWAYS() because if the table of the trigger does not exist, the
	 ** error would have been hit before this point */
	if (*TParse)(unsafe.Pointer(pParse)).FpTriggerTab != 0 {
		rc = libc.BoolInt32(_sqlite3ViewGetColumnNames(tls, pParse, (*TParse)(unsafe.Pointer(pParse)).FpTriggerTab) != 0)
	}
	/* Resolve symbols in WHEN clause */
	if rc == SQLITE_OK && (*TTrigger)(unsafe.Pointer(pNew)).FpWhen != 0 {
		rc = _sqlite3ResolveExprNames(tls, bp, (*TTrigger)(unsafe.Pointer(pNew)).FpWhen)
	}
	pStep = (*TTrigger)(unsafe.Pointer(pNew)).Fstep_list
	for {
		if !(rc == SQLITE_OK && pStep != 0) {
			break
		}
		if (*TTriggerStep)(unsafe.Pointer(pStep)).FpSelect != 0 {
			_sqlite3SelectPrep(tls, pParse, (*TTriggerStep)(unsafe.Pointer(pStep)).FpSelect, bp)
			if (*TParse)(unsafe.Pointer(pParse)).FnErr != 0 {
				rc = (*TParse)(unsafe.Pointer(pParse)).Frc
			}
		}
		if rc == SQLITE_OK && (*TTriggerStep)(unsafe.Pointer(pStep)).FpSrc != 0 {
			pSrc = _sqlite3SrcListDup(tls, db, (*TTriggerStep)(unsafe.Pointer(pStep)).FpSrc, 0)
			if pSrc != 0 {
				pSel = _sqlite3SelectNew(tls, pParse, (*TTriggerStep)(unsafe.Pointer(pStep)).FpExprList, pSrc, uintptr(0), uintptr(0), uintptr(0), uintptr(0), uint32(0), uintptr(0))
				if pSel == uintptr(0) {
					(*TTriggerStep)(unsafe.Pointer(pStep)).FpExprList = uintptr(0)
					pSrc = uintptr(0)
					rc = int32(SQLITE_NOMEM)
				} else {
					/* pStep->pExprList contains an expression-list used for an UPDATE
					 ** statement. So the a[].zEName values are the RHS of the
					 ** "<col> = <expr>" clauses of the UPDATE statement. So, before
					 ** running SelectPrep(), change all the eEName values in
					 ** pStep->pExprList to ENAME_SPAN (from their current value of
					 ** ENAME_NAME). This is to prevent any ids in ON() clauses that are
					 ** part of pSrc from being incorrectly resolved against the
					 ** a[].zEName values as if they were column aliases.  */
					_renameSetENames(tls, (*TTriggerStep)(unsafe.Pointer(pStep)).FpExprList, int32(ENAME_SPAN))
					_sqlite3SelectPrep(tls, pParse, pSel, uintptr(0))
					_renameSetENames(tls, (*TTriggerStep)(unsafe.Pointer(pStep)).FpExprList, ENAME_NAME)
					if (*TParse)(unsafe.Pointer(pParse)).FnErr != 0 {
						v2 = int32(SQLITE_ERROR)
					} else {
						v2 = SQLITE_OK
					}
					rc = v2
					if (*TTriggerStep)(unsafe.Pointer(pStep)).FpExprList != 0 {
						(*TSelect)(unsafe.Pointer(pSel)).FpEList = uintptr(0)
					}
					(*TSelect)(unsafe.Pointer(pSel)).FpSrc = uintptr(0)
					_sqlite3SelectDelete(tls, db, pSel)
				}
				if (*TTriggerStep)(unsafe.Pointer(pStep)).FpSrc != 0 {
					i = 0
					for {
						if !(i < (*TSrcList)(unsafe.Pointer((*TTriggerStep)(unsafe.Pointer(pStep)).FpSrc)).FnSrc && rc == SQLITE_OK) {
							break
						}
						p = (*TTriggerStep)(unsafe.Pointer(pStep)).FpSrc + 8 + uintptr(i)*80
						if int32(*(*uint32)(unsafe.Pointer(p + 24 + 4))&0x4>>2) != 0 {
							_sqlite3SelectPrep(tls, pParse, (*TSubquery)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(p + 72)))).FpSelect, uintptr(0))
						}
						goto _3
					_3:
						;
						i = i + 1
					}
				}
				if (*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0 {
					rc = int32(SQLITE_NOMEM)
				}
				(**(**TNameContext)(__ccgo_up(bp))).FpSrcList = pSrc
				if rc == SQLITE_OK && (*TTriggerStep)(unsafe.Pointer(pStep)).FpWhere != 0 {
					rc = _sqlite3ResolveExprNames(tls, bp, (*TTriggerStep)(unsafe.Pointer(pStep)).FpWhere)
				}
				if rc == SQLITE_OK {
					rc = _sqlite3ResolveExprListNames(tls, bp, (*TTriggerStep)(unsafe.Pointer(pStep)).FpExprList)
				}
				if (*TTriggerStep)(unsafe.Pointer(pStep)).FpUpsert != 0 && rc == SQLITE_OK {
					pUpsert = (*TTriggerStep)(unsafe.Pointer(pStep)).FpUpsert
					(*TUpsert)(unsafe.Pointer(pUpsert)).FpUpsertSrc = pSrc
					*(*uintptr)(unsafe.Pointer(bp + 16)) = pUpsert
					(**(**TNameContext)(__ccgo_up(bp))).FncFlags = int32(NC_UUpsert)
					rc = _sqlite3ResolveExprListNames(tls, bp, (*TUpsert)(unsafe.Pointer(pUpsert)).FpUpsertTarget)
					if rc == SQLITE_OK {
						pUpsertSet = (*TUpsert)(unsafe.Pointer(pUpsert)).FpUpsertSet
						rc = _sqlite3ResolveExprListNames(tls, bp, pUpsertSet)
					}
					if rc == SQLITE_OK {
						rc = _sqlite3ResolveExprNames(tls, bp, (*TUpsert)(unsafe.Pointer(pUpsert)).FpUpsertWhere)
					}
					if rc == SQLITE_OK {
						rc = _sqlite3ResolveExprNames(tls, bp, (*TUpsert)(unsafe.Pointer(pUpsert)).FpUpsertTargetWhere)
					}
					(**(**TNameContext)(__ccgo_up(bp))).FncFlags = 0
				}
				(**(**TNameContext)(__ccgo_up(bp))).FpSrcList = uintptr(0)
				_sqlite3SrcListDelete(tls, db, pSrc)
			} else {
				rc = int32(SQLITE_NOMEM)
			}
		}
		goto _1
	_1:
		;
		pStep = (*TTriggerStep)(unsafe.Pointer(pStep)).FpNext
	}
	return rc
}

// C documentation
//
//	/*
//	** Iterate through the Select objects that are part of WITH clauses attached
//	** to select statement pSelect.
//	*/
func _renameWalkWith(tls *libc.TLS, pWalker uintptr, pSelect uintptr) {
	bp := tls.Alloc(64)
	defer tls.Free(64)
	var i int32
	var p, pCopy, pParse, pWith uintptr
	var _ /* sNC at bp+0 */ TNameContext
	_, _, _, _, _ = i, p, pCopy, pParse, pWith
	pWith = (*TSelect)(unsafe.Pointer(pSelect)).FpWith
	if pWith != 0 {
		pParse = (*TWalker)(unsafe.Pointer(pWalker)).FpParse
		pCopy = uintptr(0)
		if (*TSelect)(unsafe.Pointer((*(*TCte)(unsafe.Pointer(pWith + 16))).FpSelect)).FselFlags&uint32(SF_Expanded) == uint32(0) {
			/* Push a copy of the With object onto the with-stack. We use a copy
			 ** here as the original will be expanded and resolved (flags SF_Expanded
			 ** and SF_Resolved) below. And the parser code that uses the with-stack
			 ** fails if the Select objects on it have already been expanded and
			 ** resolved.  */
			pCopy = _sqlite3WithDup(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, pWith)
			pCopy = _sqlite3WithPush(tls, pParse, pCopy, uint8(1))
		}
		i = 0
		for {
			if !(i < (*TWith)(unsafe.Pointer(pWith)).FnCte) {
				break
			}
			p = (*(*TCte)(unsafe.Pointer(pWith + 16 + uintptr(i)*48))).FpSelect
			libc.Xmemset(tls, bp, 0, uint64(56))
			(**(**TNameContext)(__ccgo_up(bp))).FpParse = pParse
			if pCopy != 0 {
				_sqlite3SelectPrep(tls, (**(**TNameContext)(__ccgo_up(bp))).FpParse, p, bp)
			}
			if (*Tsqlite3)(unsafe.Pointer((*TParse)(unsafe.Pointer((**(**TNameContext)(__ccgo_up(bp))).FpParse)).Fdb)).FmallocFailed != 0 {
				return
			}
			_sqlite3WalkSelect(tls, pWalker, p)
			_sqlite3RenameExprlistUnmap(tls, pParse, (*(*TCte)(unsafe.Pointer(pWith + 16 + uintptr(i)*48))).FpCols)
			goto _1
		_1:
			;
			i = i + 1
		}
		if pCopy != 0 && (*TParse)(unsafe.Pointer(pParse)).FpWith == pCopy {
			(*TParse)(unsafe.Pointer(pParse)).FpWith = (*TWith)(unsafe.Pointer(pCopy)).FpOuter
		}
	}
}

// C documentation
//
//	/*
//	** Assign a new cursor number to each cursor in the FROM clause (Select.pSrc)
//	** of the SELECT statement passed as the second argument, and to each
//	** cursor in the FROM clause of any FROM clause sub-selects, recursively.
//	** Except, do not assign a new cursor number to the iExcept'th element in
//	** the FROM clause of (*p). Update all expressions and other references
//	** to refer to the new cursor numbers.
//	**
//	** Argument aCsrMap is an array that may be used for temporary working
//	** space. Two guarantees are made by the caller:
//	**
//	**   * the array is larger than the largest cursor number used within the
//	**     select statement passed as an argument, and
//	**
//	**   * the array entries for all cursor numbers that do *not* appear in
//	**     FROM clauses of the select statement as described above are
//	**     initialized to zero.
//	*/
func _renumberCursors(tls *libc.TLS, pParse uintptr, p uintptr, iExcept int32, aCsrMap uintptr) {
	bp := tls.Alloc(48)
	defer tls.Free(48)
	var _ /* w at bp+0 */ TWalker
	_srclistRenumberCursors(tls, pParse, aCsrMap, (*TSelect)(unsafe.Pointer(p)).FpSrc, iExcept)
	libc.Xmemset(tls, bp, 0, uint64(48))
	*(*uintptr)(unsafe.Pointer(bp + 40)) = aCsrMap
	(**(**TWalker)(__ccgo_up(bp))).FxExprCallback = __ccgo_fp(_renumberCursorsCb)
	(**(**TWalker)(__ccgo_up(bp))).FxSelectCallback = __ccgo_fp(_sqlite3SelectWalkNoop)
	_sqlite3WalkSelect(tls, bp, p)
}

// C documentation
//
//	/*
//	** Reset a cursor back to its initial state.
//	*/
func _resetCursor(tls *libc.TLS, pCsr uintptr) {
	var i, ii int32
	var pInfo, pRtree, pStmt uintptr
	_, _, _, _, _ = i, ii, pInfo, pRtree, pStmt
	pRtree = (*TRtreeCursor)(unsafe.Pointer(pCsr)).Fbase.FpVtab
	if (*TRtreeCursor)(unsafe.Pointer(pCsr)).FaConstraint != 0 { /* Used to iterate through constraint array */
		i = 0
		for {
			if !(i < (*TRtreeCursor)(unsafe.Pointer(pCsr)).FnConstraint) {
				break
			}
			pInfo = (**(**TRtreeConstraint)(__ccgo_up((*TRtreeCursor)(unsafe.Pointer(pCsr)).FaConstraint + uintptr(i)*24))).FpInfo
			if pInfo != 0 {
				if (*Tsqlite3_rtree_query_info)(unsafe.Pointer(pInfo)).FxDelUser != 0 {
					(*(*func(*libc.TLS, uintptr))(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3_rtree_query_info)(unsafe.Pointer(pInfo)).FxDelUser})))(tls, (*Tsqlite3_rtree_query_info)(unsafe.Pointer(pInfo)).FpUser)
				}
				Xsqlite3_free(tls, pInfo)
			}
			goto _1
		_1:
			;
			i = i + 1
		}
		Xsqlite3_free(tls, (*TRtreeCursor)(unsafe.Pointer(pCsr)).FaConstraint)
		(*TRtreeCursor)(unsafe.Pointer(pCsr)).FaConstraint = uintptr(0)
	}
	ii = 0
	for {
		if !(ii < int32(RTREE_CACHE_SZ)) {
			break
		}
		_nodeRelease(tls, pRtree, **(**uintptr)(__ccgo_up(pCsr + 88 + uintptr(ii)*8)))
		goto _2
	_2:
		;
		ii = ii + 1
	}
	Xsqlite3_free(tls, (*TRtreeCursor)(unsafe.Pointer(pCsr)).FaPoint)
	pStmt = (*TRtreeCursor)(unsafe.Pointer(pCsr)).FpReadAux
	libc.Xmemset(tls, pCsr, 0, uint64(296))
	(*TRtreeCursor)(unsafe.Pointer(pCsr)).Fbase.FpVtab = pRtree
	(*TRtreeCursor)(unsafe.Pointer(pCsr)).FpReadAux = pStmt
	/* The following will only fail if the previous sqlite3_step() call failed,
	 ** in which case the error has already been caught. This statement never
	 ** encounters an error within an sqlite3_column_xxx() function, as it
	 ** calls sqlite3_column_value(), which does not use malloc(). So it is safe
	 ** to ignore the error code here.  */
	Xsqlite3_reset(tls, pStmt)
}

// C documentation
//
//	/*
//	** pE is a pointer to an expression which is a single term in the
//	** ORDER BY of a compound SELECT.  The expression has not been
//	** name resolved.
//	**
//	** At the point this routine is called, we already know that the
//	** ORDER BY term is not an integer index into the result set.  That
//	** case is handled by the calling routine.
//	**
//	** Attempt to match pE against result set columns in the left-most
//	** SELECT statement.  Return the index i of the matching column,
//	** as an indication to the caller that it should sort by the i-th column.
//	** The left-most column is 1.  In other words, the value returned is the
//	** same integer value that would be used in the SQL statement to indicate
//	** the column.
//	**
//	** If there is no match, return 0.  Return -1 if an error occurs.
//	*/
func _resolveOrderByTermToExprList(tls *libc.TLS, pParse uintptr, pSelect uintptr, pE uintptr) (r int32) {
	bp := tls.Alloc(64)
	defer tls.Free(64)
	var db, pEList uintptr
	var i, rc int32
	var savedSuppErr Tu8
	var _ /* nc at bp+0 */ TNameContext
	_, _, _, _, _ = db, i, pEList, rc, savedSuppErr /* Saved value of db->suppressErr */
	pEList = (*TSelect)(unsafe.Pointer(pSelect)).FpEList
	/* Resolve all names in the ORDER BY term expression
	 */
	libc.Xmemset(tls, bp, 0, uint64(56))
	(**(**TNameContext)(__ccgo_up(bp))).FpParse = pParse
	(**(**TNameContext)(__ccgo_up(bp))).FpSrcList = (*TSelect)(unsafe.Pointer(pSelect)).FpSrc
	*(*uintptr)(unsafe.Pointer(bp + 16)) = pEList
	(**(**TNameContext)(__ccgo_up(bp))).FncFlags = libc.Int32FromInt32(NC_AllowAgg) | libc.Int32FromInt32(NC_UEList) | libc.Int32FromInt32(NC_NoSelect)
	(**(**TNameContext)(__ccgo_up(bp))).FnNcErr = 0
	db = (*TParse)(unsafe.Pointer(pParse)).Fdb
	savedSuppErr = (*Tsqlite3)(unsafe.Pointer(db)).FsuppressErr
	(*Tsqlite3)(unsafe.Pointer(db)).FsuppressErr = uint8(1)
	rc = _sqlite3ResolveExprNames(tls, bp, pE)
	(*Tsqlite3)(unsafe.Pointer(db)).FsuppressErr = savedSuppErr
	if rc != 0 {
		return 0
	}
	/* Try to match the ORDER BY expression against an expression
	 ** in the result set.  Return an 1-based index of the matching
	 ** result-set entry.
	 */
	i = 0
	for {
		if !(i < (*TExprList)(unsafe.Pointer(pEList)).FnExpr) {
			break
		}
		if _sqlite3ExprCompare(tls, uintptr(0), (*(*TExprList_item)(unsafe.Pointer(pEList + 8 + uintptr(i)*32))).FpExpr, pE, -int32(1)) < int32(2) {
			return i + int32(1)
		}
		goto _1
	_1:
		;
		i = i + 1
	}
	/* If no match, return 0. */
	return 0
}

// C documentation
//
//	/*
//	** Sort all elements on the list of RowSetEntry objects into order of
//	** increasing v.
//	*/
func _rowSetEntrySort(tls *libc.TLS, pIn uintptr) (r uintptr) {
	bp := tls.Alloc(320)
	defer tls.Free(320)
	var i uint32
	var pNext, v3 uintptr
	var _ /* aBucket at bp+0 */ [40]uintptr
	_, _, _ = i, pNext, v3
	libc.Xmemset(tls, bp, 0, uint64(320))
	for pIn != 0 {
		pNext = (*TRowSetEntry)(unsafe.Pointer(pIn)).FpRight
		(*TRowSetEntry)(unsafe.Pointer(pIn)).FpRight = uintptr(0)
		i = uint32(0)
		for {
			if !((**(**[40]uintptr)(__ccgo_up(bp)))[i] != 0) {
				break
			}
			pIn = _rowSetEntryMerge(tls, (**(**[40]uintptr)(__ccgo_up(bp)))[i], pIn)
			(**(**[40]uintptr)(__ccgo_up(bp)))[i] = uintptr(0)
			goto _1
		_1:
			;
			i = i + 1
		}
		(**(**[40]uintptr)(__ccgo_up(bp)))[i] = pIn
		pIn = pNext
	}
	pIn = (**(**[40]uintptr)(__ccgo_up(bp)))[0]
	i = uint32(1)
	for {
		if !(uint64(i) < libc.Uint64FromInt64(320)/libc.Uint64FromInt64(8)) {
			break
		}
		if (**(**[40]uintptr)(__ccgo_up(bp)))[i] == uintptr(0) {
			goto _2
		}
		if pIn != 0 {
			v3 = _rowSetEntryMerge(tls, pIn, (**(**[40]uintptr)(__ccgo_up(bp)))[i])
		} else {
			v3 = (**(**[40]uintptr)(__ccgo_up(bp)))[i]
		}
		pIn = v3
		goto _2
	_2:
		;
		i = i + 1
	}
	return pIn
}

// C documentation
//
//	/*
//	** Rtree virtual table module xOpen method.
//	*/
func _rtreeOpen(tls *libc.TLS, pVTab uintptr, ppCursor uintptr) (r int32) {
	var pCsr, pRtree uintptr
	var rc int32
	_, _, _ = pCsr, pRtree, rc
	rc = int32(SQLITE_NOMEM)
	pRtree = pVTab
	pCsr = Xsqlite3_malloc64(tls, uint64(296))
	if pCsr != 0 {
		libc.Xmemset(tls, pCsr, 0, uint64(296))
		(*TRtreeCursor)(unsafe.Pointer(pCsr)).Fbase.FpVtab = pVTab
		rc = SQLITE_OK
		(*TRtree)(unsafe.Pointer(pRtree)).FnCursor = (*TRtree)(unsafe.Pointer(pRtree)).FnCursor + 1
	}
	**(**uintptr)(__ccgo_up(ppCursor)) = pCsr
	return rc
}

// C documentation
//
//	/*
//	** Iterate through each expression in expression-list pEList. For each:
//	**
//	**   * TK_COLUMN,
//	**   * aggregate function, or
//	**   * window function with a Window object that is not a member of the
//	**     Window list passed as the second argument (pWin).
//	**
//	** Append the node to output expression-list (*ppSub). And replace it
//	** with a TK_COLUMN that reads the (N-1)th element of table
//	** pWin->iEphCsr, where N is the number of elements in (*ppSub) after
//	** appending the new one.
//	*/
func _selectWindowRewriteEList(tls *libc.TLS, pParse uintptr, pWin uintptr, pSrc uintptr, pEList uintptr, pTab uintptr, ppSub uintptr) {
	bp := tls.Alloc(96)
	defer tls.Free(96)
	var _ /* sRewrite at bp+48 */ TWindowRewrite
	var _ /* sWalker at bp+0 */ TWalker
	libc.Xmemset(tls, bp, 0, uint64(48))
	libc.Xmemset(tls, bp+48, 0, uint64(40))
	(**(**TWindowRewrite)(__ccgo_up(bp + 48))).FpSub = **(**uintptr)(__ccgo_up(ppSub))
	(**(**TWindowRewrite)(__ccgo_up(bp + 48))).FpWin = pWin
	(**(**TWindowRewrite)(__ccgo_up(bp + 48))).FpSrc = pSrc
	(**(**TWindowRewrite)(__ccgo_up(bp + 48))).FpTab = pTab
	(**(**TWalker)(__ccgo_up(bp))).FpParse = pParse
	(**(**TWalker)(__ccgo_up(bp))).FxExprCallback = __ccgo_fp(_selectWindowRewriteExprCb)
	(**(**TWalker)(__ccgo_up(bp))).FxSelectCallback = __ccgo_fp(_selectWindowRewriteSelectCb)
	*(*uintptr)(unsafe.Pointer(bp + 40)) = bp + 48
	_sqlite3WalkExprList(tls, bp, pEList)
	**(**uintptr)(__ccgo_up(ppSub)) = (**(**TWindowRewrite)(__ccgo_up(bp + 48))).FpSub
}

// C documentation
//
//	/*
//	** Do the work for either sqlite3changeset_start() or start_strm().
//	*/
func _sessionChangesetStart(tls *libc.TLS, pp uintptr, __ccgo_fp_xInput uintptr, pIn uintptr, nChangeset int32, pChangeset uintptr, bInvert int32, bSkipEmpty int32) (r int32) {
	var nByte, v1 int32
	var pRet uintptr
	_, _, _ = nByte, pRet, v1 /* Number of bytes to allocate for iterator */
	/* Zero the output variable in case an error occurs. */
	**(**uintptr)(__ccgo_up(pp)) = uintptr(0)
	/* Allocate and initialize the iterator structure. */
	nByte = int32(152)
	pRet = Xsqlite3_malloc(tls, nByte)
	if !(pRet != 0) {
		return int32(SQLITE_NOMEM)
	}
	libc.Xmemset(tls, pRet, 0, uint64(152))
	(*Tsqlite3_changeset_iter)(unsafe.Pointer(pRet)).Fin.FaData = pChangeset
	(*Tsqlite3_changeset_iter)(unsafe.Pointer(pRet)).Fin.FnData = nChangeset
	(*Tsqlite3_changeset_iter)(unsafe.Pointer(pRet)).Fin.FxInput = __ccgo_fp_xInput
	(*Tsqlite3_changeset_iter)(unsafe.Pointer(pRet)).Fin.FpIn = pIn
	if __ccgo_fp_xInput != 0 {
		v1 = 0
	} else {
		v1 = int32(1)
	}
	(*Tsqlite3_changeset_iter)(unsafe.Pointer(pRet)).Fin.FbEof = v1
	(*Tsqlite3_changeset_iter)(unsafe.Pointer(pRet)).FbInvert = bInvert
	(*Tsqlite3_changeset_iter)(unsafe.Pointer(pRet)).FbSkipEmpty = bSkipEmpty
	/* Populate the output variable and return success. */
	**(**uintptr)(__ccgo_up(pp)) = pRet
	return SQLITE_OK
}

// C documentation
//
//	/*
//	** Initialize a Walker object so that will persist AggInfo entries referenced
//	** by the tree that is walked.
//	*/
func _sqlite3AggInfoPersistWalkerInit(tls *libc.TLS, pWalker uintptr, pParse uintptr) {
	libc.Xmemset(tls, pWalker, 0, uint64(48))
	(*TWalker)(unsafe.Pointer(pWalker)).FpParse = pParse
	(*TWalker)(unsafe.Pointer(pWalker)).FxExprCallback = __ccgo_fp(_agginfoPersistExprCb)
	(*TWalker)(unsafe.Pointer(pWalker)).FxSelectCallback = __ccgo_fp(_sqlite3SelectWalkNoop)
}

// C documentation
//
//	/*
//	** Initialize memory that will be converted into a BtCursor object.
//	**
//	** The simple approach here would be to memset() the entire object
//	** to zero.  But it turns out that the apPage[] and aiIdx[] arrays
//	** do not need to be zeroed and they are large, so we can save a lot
//	** of run-time by skipping the initialization of those elements.
//	*/
func _sqlite3BtreeCursorZero(tls *libc.TLS, p uintptr) {
	libc.Xmemset(tls, p, 0, uint64(libc.UintptrFromInt32(0)+32))
}

// C documentation
//
//	/*
//	** Look through the list of open database files in db->aDb[] and if
//	** any have been closed, remove them from the list.  Reallocate the
//	** db->aDb[] structure to a smaller size, if possible.
//	**
//	** Entry 0 (the "main" database) and entry 1 (the "temp" database)
//	** are never candidates for being collapsed.
//	*/
func _sqlite3CollapseDatabaseArray(tls *libc.TLS, db uintptr) {
	var i, j, v2 int32
	var pDb uintptr
	_, _, _, _ = i, j, pDb, v2
	v2 = libc.Int32FromInt32(2)
	j = v2
	i = v2
	for {
		if !(i < (*Tsqlite3)(unsafe.Pointer(db)).FnDb) {
			break
		}
		pDb = (*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(i)*32
		if (*TDb)(unsafe.Pointer(pDb)).FpBt == uintptr(0) {
			_sqlite3DbFree(tls, db, (*TDb)(unsafe.Pointer(pDb)).FzDbSName)
			(*TDb)(unsafe.Pointer(pDb)).FzDbSName = uintptr(0)
			goto _1
		}
		if j < i {
			**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(j)*32)) = **(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(i)*32))
		}
		j = j + 1
		goto _1
	_1:
		;
		i = i + 1
	}
	(*Tsqlite3)(unsafe.Pointer(db)).FnDb = j
	if (*Tsqlite3)(unsafe.Pointer(db)).FnDb <= int32(2) && (*Tsqlite3)(unsafe.Pointer(db)).FaDb != db+696 {
		libc.Xmemcpy(tls, db+696, (*Tsqlite3)(unsafe.Pointer(db)).FaDb, libc.Uint64FromInt32(2)*libc.Uint64FromInt64(32))
		_sqlite3DbFree(tls, db, (*Tsqlite3)(unsafe.Pointer(db)).FaDb)
		(*Tsqlite3)(unsafe.Pointer(db)).FaDb = db + 696
	}
}

// C documentation
//
//	/*
//	** Allocate and zero memory.  If the allocation fails, make
//	** the mallocFailed flag in the connection pointer.
//	*/
func _sqlite3DbMallocZero(tls *libc.TLS, db uintptr, n Tu64) (r uintptr) {
	var p uintptr
	_ = p
	p = _sqlite3DbMallocRaw(tls, db, n)
	if p != 0 {
		libc.Xmemset(tls, p, 0, n)
	}
	return p
}

// C documentation
//
//	/*
//	** Make a copy of a string in memory obtained from sqliteMalloc(). These
//	** functions call sqlite3MallocRaw() directly instead of sqliteMalloc(). This
//	** is because when memory debugging is turned on, these two functions are
//	** called via macros that record the current file and line number in the
//	** ThreadData structure.
//	*/
func _sqlite3DbStrDup(tls *libc.TLS, db uintptr, z uintptr) (r uintptr) {
	var n Tsize_t
	var zNew uintptr
	_, _ = n, zNew
	if z == uintptr(0) {
		return uintptr(0)
	}
	n = libc.Xstrlen(tls, z) + uint64(1)
	zNew = _sqlite3DbMallocRaw(tls, db, n)
	if zNew != 0 {
		libc.Xmemcpy(tls, zNew, z, n)
	}
	return zNew
}

// C documentation
//
//	/*
//	** Determine if an index pIdx on table with cursor iCur contains will
//	** the expression pExpr.  Return true if the index does cover the
//	** expression and false if the pExpr expression references table columns
//	** that are not found in the index pIdx.
//	**
//	** An index covering an expression means that the expression can be
//	** evaluated using only the index and without having to lookup the
//	** corresponding table entry.
//	*/
func _sqlite3ExprCoveredByIndex(tls *libc.TLS, pExpr uintptr, iCur int32, pIdx uintptr) (r int32) {
	bp := tls.Alloc(64)
	defer tls.Free(64)
	var _ /* w at bp+0 */ TWalker
	var _ /* xcov at bp+48 */ TIdxCover
	libc.Xmemset(tls, bp, 0, uint64(48))
	(**(**TIdxCover)(__ccgo_up(bp + 48))).FiCur = iCur
	(**(**TIdxCover)(__ccgo_up(bp + 48))).FpIdx = pIdx
	(**(**TWalker)(__ccgo_up(bp))).FxExprCallback = __ccgo_fp(_exprIdxCover)
	*(*uintptr)(unsafe.Pointer(bp + 40)) = bp + 48
	_sqlite3WalkExpr(tls, bp, pExpr)
	return libc.BoolInt32(!((**(**TWalker)(__ccgo_up(bp))).FeCode != 0))
}

// C documentation
//
//	/*
//	** The following set of routines walk through the parse tree and assign
//	** a specific database to all table references where the database name
//	** was left unspecified in the original SQL statement.  The pFix structure
//	** must have been initialized by a prior call to sqlite3FixInit().
//	**
//	** These routines are used to make sure that an index, trigger, or
//	** view in one database does not refer to objects in a different database.
//	** (Exception: indices, triggers, and views in the TEMP database are
//	** allowed to refer to anything.)  If a reference is explicitly made
//	** to an object in a different database, an error message is added to
//	** pParse->zErrMsg and these routines return non-zero.  If everything
//	** checks out, these routines return 0.
//	*/
func _sqlite3FixSrcList(tls *libc.TLS, pFix uintptr, pList uintptr) (r int32) {
	bp := tls.Alloc(128)
	defer tls.Free(128)
	var res int32
	var _ /* s at bp+0 */ TSelect
	_ = res
	res = 0
	if pList != 0 {
		libc.Xmemset(tls, bp, 0, uint64(120))
		(**(**TSelect)(__ccgo_up(bp))).FpSrc = pList
		res = _sqlite3WalkSelect(tls, pFix+8, bp)
	}
	return res
}

// C documentation
//
//	/*
//	** Free any buffer allocated by pBuf. Zero the structure before returning.
//	*/
func _sqlite3Fts5BufferFree(tls *libc.TLS, pBuf uintptr) {
	Xsqlite3_free(tls, (*TFts5Buffer)(unsafe.Pointer(pBuf)).Fp)
	libc.Xmemset(tls, pBuf, 0, uint64(16))
}

// C documentation
//
//	/*
//	** Return true if the floating point value is Not a Number (NaN).
//	**
//	** Use the math library isnan() function if compiled with SQLITE_HAVE_ISNAN.
//	** Otherwise, we have our own implementation that works on most systems.
//	*/
func _sqlite3IsNaN(tls *libc.TLS, _x float64) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	*(*float64)(unsafe.Pointer(bp)) = _x
	var rc int32
	var _ /* y at bp+8 */ Tu64
	_ = rc
	libc.Xmemcpy(tls, bp+8, bp, uint64(8))
	rc = libc.BoolInt32(**(**Tu64)(__ccgo_up(bp + 8))&(libc.Uint64FromInt32(0x7ff)<<libc.Int32FromInt32(52)) == libc.Uint64FromInt32(0x7ff)<<libc.Int32FromInt32(52) && **(**Tu64)(__ccgo_up(bp + 8))&(libc.Uint64FromInt32(1)<<libc.Int32FromInt32(52)-libc.Uint64FromInt32(1)) != uint64(0))
	return rc
}

// C documentation
//
//	/*
//	** Return true if the floating point value is NaN or +Inf or -Inf.
//	*/
func _sqlite3IsOverflow(tls *libc.TLS, _x float64) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	*(*float64)(unsafe.Pointer(bp)) = _x
	var rc int32
	var _ /* y at bp+8 */ Tu64
	_ = rc
	libc.Xmemcpy(tls, bp+8, bp, uint64(8))
	rc = libc.BoolInt32(**(**Tu64)(__ccgo_up(bp + 8))&(libc.Uint64FromInt32(0x7ff)<<libc.Int32FromInt32(52)) == libc.Uint64FromInt32(0x7ff)<<libc.Int32FromInt32(52))
	return rc
}

// C documentation
//
//	/*
//	** Deinitialize the memory allocation subsystem.
//	*/
func _sqlite3MallocEnd(tls *libc.TLS) {
	if _sqlite3Config.Fm.FxShutdown != 0 {
		(*(*func(*libc.TLS, uintptr))(unsafe.Pointer(&struct{ uintptr }{_sqlite3Config.Fm.FxShutdown})))(tls, _sqlite3Config.Fm.FpAppData)
	}
	libc.Xmemset(tls, uintptr(unsafe.Pointer(&_mem0)), 0, uint64(32))
}

// C documentation
//
//	/*
//	** Initialize the memory allocation subsystem.
//	*/
func _sqlite3MallocInit(tls *libc.TLS) (r int32) {
	var rc int32
	_ = rc
	if _sqlite3Config.Fm.FxMalloc == uintptr(0) {
		_sqlite3MemSetDefault(tls)
	}
	_mem0.Fmutex = _sqlite3MutexAlloc(tls, int32(SQLITE_MUTEX_STATIC_MEM))
	if _sqlite3Config.FpPage == uintptr(0) || _sqlite3Config.FszPage < int32(512) || _sqlite3Config.FnPage <= 0 {
		_sqlite3Config.FpPage = uintptr(0)
		_sqlite3Config.FszPage = 0
	}
	rc = (*(*func(*libc.TLS, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{_sqlite3Config.Fm.FxInit})))(tls, _sqlite3Config.Fm.FpAppData)
	if rc != SQLITE_OK {
		libc.Xmemset(tls, uintptr(unsafe.Pointer(&_mem0)), 0, uint64(32))
	}
	return rc
}

// C documentation
//
//	/*
//	** Allocate and zero memory.
//	*/
func _sqlite3MallocZero(tls *libc.TLS, n Tu64) (r uintptr) {
	var p uintptr
	_ = p
	p = _sqlite3Malloc(tls, n)
	if p != 0 {
		libc.Xmemset(tls, p, 0, n)
	}
	return p
}

func _sqlite3PrngRestoreState(tls *libc.TLS) {
	libc.Xmemcpy(tls, uintptr(unsafe.Pointer(&_sqlite3Prng)), uintptr(unsafe.Pointer(&_sqlite3SavedPrng)), uint64(132))
}

func _sqlite3PrngSaveState(tls *libc.TLS) {
	libc.Xmemcpy(tls, uintptr(unsafe.Pointer(&_sqlite3SavedPrng)), uintptr(unsafe.Pointer(&_sqlite3Prng)), uint64(132))
}

// C documentation
//
//	/*
//	** Scan the expression list that is the argument to RETURNING looking
//	** for subqueries that depend on the table which is being modified in the
//	** statement that is hosting the RETURNING clause (pTab).  Mark all such
//	** subqueries as SF_Correlated.  If the subqueries are part of an
//	** expression, mark the expression as EP_VarSelect.
//	**
//	** https://sqlite.org/forum/forumpost/2c83569ce8945d39
//	*/
func _sqlite3ProcessReturningSubqueries(tls *libc.TLS, pEList uintptr, pTab uintptr) {
	bp := tls.Alloc(48)
	defer tls.Free(48)
	var _ /* w at bp+0 */ TWalker
	libc.Xmemset(tls, bp, 0, uint64(48))
	(**(**TWalker)(__ccgo_up(bp))).FxExprCallback = __ccgo_fp(_sqlite3ExprWalkNoop)
	(**(**TWalker)(__ccgo_up(bp))).FxSelectCallback = __ccgo_fp(_sqlite3ReturningSubqueryCorrelated)
	*(*uintptr)(unsafe.Pointer(bp + 40)) = pTab
	_sqlite3WalkExprList(tls, bp, pEList)
	if (**(**TWalker)(__ccgo_up(bp))).FeCode != 0 {
		(**(**TWalker)(__ccgo_up(bp))).FxExprCallback = __ccgo_fp(_sqlite3ReturningSubqueryVarSelect)
		(**(**TWalker)(__ccgo_up(bp))).FxSelectCallback = __ccgo_fp(_sqlite3SelectWalkNoop)
		_sqlite3WalkExprList(tls, bp, pEList)
	}
}

// C documentation
//
//	/*
//	** Remove all nodes that are part of expression pExpr from the rename list.
//	*/
func _sqlite3RenameExprUnmap(tls *libc.TLS, pParse uintptr, pExpr uintptr) {
	bp := tls.Alloc(48)
	defer tls.Free(48)
	var eMode Tu8
	var _ /* sWalker at bp+0 */ TWalker
	_ = eMode
	eMode = (*TParse)(unsafe.Pointer(pParse)).FeParseMode
	libc.Xmemset(tls, bp, 0, uint64(48))
	(**(**TWalker)(__ccgo_up(bp))).FpParse = pParse
	(**(**TWalker)(__ccgo_up(bp))).FxExprCallback = __ccgo_fp(_renameUnmapExprCb)
	(**(**TWalker)(__ccgo_up(bp))).FxSelectCallback = __ccgo_fp(_renameUnmapSelectCb)
	(*TParse)(unsafe.Pointer(pParse)).FeParseMode = uint8(PARSE_MODE_UNMAP)
	_sqlite3WalkExpr(tls, bp, pExpr)
	(*TParse)(unsafe.Pointer(pParse)).FeParseMode = eMode
}

// C documentation
//
//	/*
//	** Remove all nodes that are part of expression-list pEList from the
//	** rename list.
//	*/
func _sqlite3RenameExprlistUnmap(tls *libc.TLS, pParse uintptr, pEList uintptr) {
	bp := tls.Alloc(48)
	defer tls.Free(48)
	var i int32
	var _ /* sWalker at bp+0 */ TWalker
	_ = i
	if pEList != 0 {
		libc.Xmemset(tls, bp, 0, uint64(48))
		(**(**TWalker)(__ccgo_up(bp))).FpParse = pParse
		(**(**TWalker)(__ccgo_up(bp))).FxExprCallback = __ccgo_fp(_renameUnmapExprCb)
		_sqlite3WalkExprList(tls, bp, pEList)
		i = 0
		for {
			if !(i < (*TExprList)(unsafe.Pointer(pEList)).FnExpr) {
				break
			}
			if int32(uint32(*(*uint16)(unsafe.Pointer(pEList + 8 + uintptr(i)*32 + 16 + 4))&0x3>>0)) == ENAME_NAME {
				_sqlite3RenameTokenRemap(tls, pParse, uintptr(0), (*(*TExprList_item)(unsafe.Pointer(pEList + 8 + uintptr(i)*32))).FzEName)
			}
			goto _1
		_1:
			;
			i = i + 1
		}
	}
}

// C documentation
//
//	/*
//	** Resolve names in expressions that can only reference a single table
//	** or which cannot reference any tables at all.  Examples:
//	**
//	**                                                    "type" flag
//	**                                                    ------------
//	**    (1)   CHECK constraints                         NC_IsCheck
//	**    (2)   WHERE clauses on partial indices          NC_PartIdx
//	**    (3)   Expressions in indexes on expressions     NC_IdxExpr
//	**    (4)   Expression arguments to VACUUM INTO.      0
//	**    (5)   GENERATED ALWAYS as expressions           NC_GenCol
//	**
//	** In all cases except (4), the Expr.iTable value for Expr.op==TK_COLUMN
//	** nodes of the expression is set to -1 and the Expr.iColumn value is
//	** set to the column number.  In case (4), TK_COLUMN nodes cause an error.
//	**
//	** Any errors cause an error message to be set in pParse.
//	*/
func _sqlite3ResolveSelfReference(tls *libc.TLS, pParse uintptr, pTab uintptr, type1 int32, pExpr uintptr, pList uintptr) (r int32) {
	bp := tls.Alloc(144)
	defer tls.Free(144)
	var pSrc uintptr
	var rc, v1 int32
	var _ /* sNC at bp+0 */ TNameContext
	var _ /* uSrc at bp+56 */ struct {
		FsrcSpace    [0][88]Tu8
		FsSrc        TSrcList
		F__ccgo_pad2 [80]byte
	}
	_, _, _ = pSrc, rc, v1
	libc.Xmemset(tls, bp, 0, uint64(56))
	libc.Xmemset(tls, bp+56, 0, uint64(88))
	pSrc = bp + 56
	if pTab != 0 {
		(*TSrcList)(unsafe.Pointer(pSrc)).FnSrc = int32(1)
		(*(*TSrcItem)(unsafe.Pointer(pSrc + 8))).FzName = (*TTable)(unsafe.Pointer(pTab)).FzName
		(*(*TSrcItem)(unsafe.Pointer(pSrc + 8))).FpSTab = pTab
		(*(*TSrcItem)(unsafe.Pointer(pSrc + 8))).FiCursor = -int32(1)
		if (*TTable)(unsafe.Pointer(pTab)).FpSchema != (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer((*TParse)(unsafe.Pointer(pParse)).Fdb)).FaDb + 1*32))).FpSchema {
			/* Cause EP_FromDDL to be set on TK_FUNCTION nodes of non-TEMP
			 ** schema elements */
			type1 = type1 | int32(NC_FromDDL)
		}
	}
	(**(**TNameContext)(__ccgo_up(bp))).FpParse = pParse
	(**(**TNameContext)(__ccgo_up(bp))).FpSrcList = pSrc
	(**(**TNameContext)(__ccgo_up(bp))).FncFlags = type1 | int32(NC_IsDDL)
	v1 = _sqlite3ResolveExprNames(tls, bp, pExpr)
	rc = v1
	if v1 != SQLITE_OK {
		return rc
	}
	if pList != 0 {
		rc = _sqlite3ResolveExprListNames(tls, bp, pList)
	}
	return rc
}

// C documentation
//
//	/*
//	** Attach a Subquery object to pItem->uv.pSubq.  Set the
//	** pSelect value but leave all the other values initialized
//	** to zero.
//	**
//	** A copy of the Select object is made if dupSelect is true, and the
//	** SrcItem takes responsibility for deleting the copy.  If dupSelect is
//	** false, ownership of the Select passes to the SrcItem.  Either way,
//	** the SrcItem will take responsibility for deleting the Select.
//	**
//	** When dupSelect is zero, that means the Select might get deleted right
//	** away if there is an OOM error.  Beware.
//	**
//	** Return non-zero on success.  Return zero on an OOM error.
//	*/
func _sqlite3SrcItemAttachSubquery(tls *libc.TLS, pParse uintptr, pItem uintptr, pSelect uintptr, dupSelect int32) (r int32) {
	var p, v1 uintptr
	_, _ = p, v1
	if int32(*(*uint32)(unsafe.Pointer(pItem + 24 + 4))&0x10000>>16) != 0 {
		*(*uintptr)(unsafe.Pointer(pItem + 72)) = uintptr(0)
		libc.SetBitFieldPtr32Uint32(pItem+24+4, libc.Uint32FromInt32(0), 16, 0x10000)
	} else {
		if *(*uintptr)(unsafe.Pointer(pItem + 72)) != uintptr(0) {
			_sqlite3DbFree(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, *(*uintptr)(unsafe.Pointer(pItem + 72)))
			*(*uintptr)(unsafe.Pointer(pItem + 72)) = uintptr(0)
		}
	}
	if dupSelect != 0 {
		pSelect = _sqlite3SelectDup(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, pSelect, 0)
		if pSelect == uintptr(0) {
			return 0
		}
	}
	v1 = _sqlite3DbMallocRawNN(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, uint64(24))
	*(*uintptr)(unsafe.Pointer(pItem + 72)) = v1
	p = v1
	if p == uintptr(0) {
		_sqlite3SelectDelete(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, pSelect)
		return 0
	}
	libc.SetBitFieldPtr32Uint32(pItem+24+4, libc.Uint32FromInt32(1), 2, 0x4)
	(*TSubquery)(unsafe.Pointer(p)).FpSelect = pSelect
	libc.Xmemset(tls, p+uintptr(8), 0, libc.Uint64FromInt64(24)-libc.Uint64FromInt64(8))
	return int32(1)
}

// C documentation
//
//	/*
//	** Convert a UTF-16 string in the native encoding into a UTF-8 string.
//	** Memory to hold the UTF-8 string is obtained from sqlite3_malloc and must
//	** be freed by the calling function.
//	**
//	** NULL is returned if there is an allocation error.
//	*/
func _sqlite3Utf16to8(tls *libc.TLS, db uintptr, z uintptr, nByte int32, enc Tu8) (r uintptr) {
	bp := tls.Alloc(64)
	defer tls.Free(64)
	var _ /* m at bp+0 */ TMem
	libc.Xmemset(tls, bp, 0, uint64(56))
	(**(**TMem)(__ccgo_up(bp))).Fdb = db
	_sqlite3VdbeMemSetStr(tls, bp, z, int64(nByte), enc, libc.UintptrFromInt32(0))
	_sqlite3VdbeChangeEncoding(tls, bp, int32(SQLITE_UTF8))
	if (*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0 {
		_sqlite3VdbeMemRelease(tls, bp)
		(**(**TMem)(__ccgo_up(bp))).Fz = uintptr(0)
	}
	return (**(**TMem)(__ccgo_up(bp))).Fz
}

// C documentation
//
//	/*
//	** Add an opcode that includes the p4 value with a P4_INT64 or
//	** P4_REAL type.
//	*/
func _sqlite3VdbeAddOp4Dup8(tls *libc.TLS, p uintptr, op int32, p1 int32, p2 int32, p3 int32, zP4 uintptr, p4type int32) (r int32) {
	var p4copy uintptr
	_ = p4copy
	p4copy = _sqlite3DbMallocRawNN(tls, _sqlite3VdbeDb(tls, p), uint64(8))
	if p4copy != 0 {
		libc.Xmemcpy(tls, p4copy, zP4, uint64(8))
	}
	return _sqlite3VdbeAddOp4(tls, p, op, p1, p2, p3, p4copy, p4type)
}

// C documentation
//
//	/*
//	** Create a new virtual database engine.
//	*/
func _sqlite3VdbeCreate(tls *libc.TLS, pParse uintptr) (r uintptr) {
	var db, p uintptr
	_, _ = db, p
	db = (*TParse)(unsafe.Pointer(pParse)).Fdb
	p = _sqlite3DbMallocRawNN(tls, db, uint64(304))
	if p == uintptr(0) {
		return uintptr(0)
	}
	libc.Xmemset(tls, p+136, 0, libc.Uint64FromInt64(304)-uint64(libc.UintptrFromInt32(0)+136))
	(*TVdbe)(unsafe.Pointer(p)).Fdb = db
	if (*Tsqlite3)(unsafe.Pointer(db)).FpVdbe != 0 {
		(*TVdbe)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FpVdbe)).FppVPrev = p + 16
	}
	(*TVdbe)(unsafe.Pointer(p)).FpVNext = (*Tsqlite3)(unsafe.Pointer(db)).FpVdbe
	(*TVdbe)(unsafe.Pointer(p)).FppVPrev = db + 8
	(*Tsqlite3)(unsafe.Pointer(db)).FpVdbe = p
	(*TVdbe)(unsafe.Pointer(p)).FpParse = pParse
	(*TParse)(unsafe.Pointer(pParse)).FpVdbe = p
	_sqlite3VdbeAddOp2(tls, p, int32(OP_Init), 0, int32(1))
	return p
}

// C documentation
//
//	/*
//	** Memory cell pAccum contains the context of an aggregate function.
//	** This routine calls the xValue method for that function and stores
//	** the results in memory cell pMem.
//	**
//	** SQLITE_ERROR is returned if xValue() reports an error. SQLITE_OK
//	** otherwise.
//	*/
func _sqlite3VdbeMemAggValue(tls *libc.TLS, pAccum uintptr, pOut uintptr, pFunc uintptr) (r int32) {
	bp := tls.Alloc(48)
	defer tls.Free(48)
	var _ /* ctx at bp+0 */ Tsqlite3_context
	libc.Xmemset(tls, bp, 0, uint64(48))
	_sqlite3VdbeMemSetNull(tls, pOut)
	(**(**Tsqlite3_context)(__ccgo_up(bp))).FpOut = pOut
	(**(**Tsqlite3_context)(__ccgo_up(bp))).FpMem = pAccum
	(**(**Tsqlite3_context)(__ccgo_up(bp))).FpFunc = pFunc
	(**(**Tsqlite3_context)(__ccgo_up(bp))).Fenc = (*Tsqlite3)(unsafe.Pointer((*TMem)(unsafe.Pointer(pAccum)).Fdb)).Fenc
	(*(*func(*libc.TLS, uintptr))(unsafe.Pointer(&struct{ uintptr }{(*TFuncDef)(unsafe.Pointer(pFunc)).FxValue})))(tls, bp)
	return (**(**Tsqlite3_context)(__ccgo_up(bp))).FisError
}

// C documentation
//
//	/*
//	** Memory cell pMem contains the context of an aggregate function.
//	** This routine calls the finalize method for that function.  The
//	** result of the aggregate is stored back into pMem.
//	**
//	** Return SQLITE_ERROR if the finalizer reports an error.  SQLITE_OK
//	** otherwise.
//	*/
func _sqlite3VdbeMemFinalize(tls *libc.TLS, pMem uintptr, pFunc uintptr) (r int32) {
	bp := tls.Alloc(112)
	defer tls.Free(112)
	var _ /* ctx at bp+0 */ Tsqlite3_context
	var _ /* t at bp+48 */ TMem
	libc.Xmemset(tls, bp, 0, uint64(48))
	libc.Xmemset(tls, bp+48, 0, uint64(56))
	(**(**TMem)(__ccgo_up(bp + 48))).Fflags = uint16(MEM_Null)
	(**(**TMem)(__ccgo_up(bp + 48))).Fdb = (*TMem)(unsafe.Pointer(pMem)).Fdb
	(**(**Tsqlite3_context)(__ccgo_up(bp))).FpOut = bp + 48
	(**(**Tsqlite3_context)(__ccgo_up(bp))).FpMem = pMem
	(**(**Tsqlite3_context)(__ccgo_up(bp))).FpFunc = pFunc
	(**(**Tsqlite3_context)(__ccgo_up(bp))).Fenc = (*Tsqlite3)(unsafe.Pointer((**(**TMem)(__ccgo_up(bp + 48))).Fdb)).Fenc
	(*(*func(*libc.TLS, uintptr))(unsafe.Pointer(&struct{ uintptr }{(*TFuncDef)(unsafe.Pointer(pFunc)).FxFinalize})))(tls, bp) /* IMP: R-24505-23230 */
	if (*TMem)(unsafe.Pointer(pMem)).FszMalloc > 0 {
		_sqlite3DbFreeNN(tls, (*TMem)(unsafe.Pointer(pMem)).Fdb, (*TMem)(unsafe.Pointer(pMem)).FzMalloc)
	}
	libc.Xmemcpy(tls, pMem, bp+48, uint64(56))
	return (**(**Tsqlite3_context)(__ccgo_up(bp))).FisError
}

// C documentation
//
//	/*
//	** Transfer the contents of pFrom to pTo. Any existing value in pTo is
//	** freed. If pFrom contains ephemeral data, a copy is made.
//	**
//	** pFrom contains an SQL NULL when this routine returns.
//	*/
func _sqlite3VdbeMemMove(tls *libc.TLS, pTo uintptr, pFrom uintptr) {
	_sqlite3VdbeMemRelease(tls, pTo)
	libc.Xmemcpy(tls, pTo, pFrom, uint64(56))
	(*TMem)(unsafe.Pointer(pFrom)).Fflags = uint16(MEM_Null)
	(*TMem)(unsafe.Pointer(pFrom)).FszMalloc = 0
}

// C documentation
//
//	/*
//	** Swap byte-code between two VDBE structures.
//	**
//	** This happens after pB was previously run and returned
//	** SQLITE_SCHEMA.  The statement was then reprepared in pA.
//	** This routine transfers the new bytecode in pA over to pB
//	** so that pB can be run again.  The old pB byte code is
//	** moved back to pA so that it will be cleaned up when pA is
//	** finalized.
//	*/
func _sqlite3VdbeSwap(tls *libc.TLS, pA uintptr, pB uintptr) {
	var pTmp, ppTmp, zTmp uintptr
	var tmp TVdbe
	_, _, _, _ = pTmp, ppTmp, tmp, zTmp
	tmp = **(**TVdbe)(__ccgo_up(pA))
	**(**TVdbe)(__ccgo_up(pA)) = **(**TVdbe)(__ccgo_up(pB))
	**(**TVdbe)(__ccgo_up(pB)) = tmp
	pTmp = (*TVdbe)(unsafe.Pointer(pA)).FpVNext
	(*TVdbe)(unsafe.Pointer(pA)).FpVNext = (*TVdbe)(unsafe.Pointer(pB)).FpVNext
	(*TVdbe)(unsafe.Pointer(pB)).FpVNext = pTmp
	ppTmp = (*TVdbe)(unsafe.Pointer(pA)).FppVPrev
	(*TVdbe)(unsafe.Pointer(pA)).FppVPrev = (*TVdbe)(unsafe.Pointer(pB)).FppVPrev
	(*TVdbe)(unsafe.Pointer(pB)).FppVPrev = ppTmp
	zTmp = (*TVdbe)(unsafe.Pointer(pA)).FzSql
	(*TVdbe)(unsafe.Pointer(pA)).FzSql = (*TVdbe)(unsafe.Pointer(pB)).FzSql
	(*TVdbe)(unsafe.Pointer(pB)).FzSql = zTmp
	(*TVdbe)(unsafe.Pointer(pB)).Fexpmask = (*TVdbe)(unsafe.Pointer(pA)).Fexpmask
	(*TVdbe)(unsafe.Pointer(pB)).FprepFlags = (*TVdbe)(unsafe.Pointer(pA)).FprepFlags
	libc.Xmemcpy(tls, pB+212, pA+212, uint64(36))
	**(**Tu32)(__ccgo_up(pB + 212 + 5*4)) = **(**Tu32)(__ccgo_up(pB + 212 + 5*4)) + 1
}

// C documentation
//
//	/* Create a snapshot object.  The content of a snapshot is opaque to
//	** every other subsystem, so the WAL module can put whatever it needs
//	** in the object.
//	*/
func _sqlite3WalSnapshotGet(tls *libc.TLS, pWal uintptr, ppSnapshot uintptr) (r int32) {
	var pRet uintptr
	var rc int32
	_, _ = pRet, rc
	rc = SQLITE_OK
	if libc.Xmemcmp(tls, pWal+72+24, uintptr(unsafe.Pointer(&_aZero)), uint64(16)) == 0 {
		**(**uintptr)(__ccgo_up(ppSnapshot)) = uintptr(0)
		return int32(SQLITE_ERROR)
	}
	pRet = Xsqlite3_malloc(tls, int32(48))
	if pRet == uintptr(0) {
		rc = int32(SQLITE_NOMEM)
	} else {
		libc.Xmemcpy(tls, pRet, pWal+72, uint64(48))
		**(**uintptr)(__ccgo_up(ppSnapshot)) = pRet
	}
	return rc
}

// C documentation
//
//	/*
//	** If any data has been written (but not committed) to the log file, this
//	** function moves the write-pointer back to the start of the transaction.
//	**
//	** Additionally, the callback function is invoked for each frame written
//	** to the WAL since the start of the transaction. If the callback returns
//	** other than SQLITE_OK, it is not invoked again and the error code is
//	** returned to the caller.
//	**
//	** Otherwise, if the callback function does not return an error, this
//	** function returns SQLITE_OK.
//	*/
func _sqlite3WalUndo(tls *libc.TLS, pWal uintptr, __ccgo_fp_xUndo uintptr, pUndoCtx uintptr) (r int32) {
	var iFrame, iMax TPgno
	var rc int32
	_, _, _ = iFrame, iMax, rc
	rc = SQLITE_OK
	if (*TWal)(unsafe.Pointer(pWal)).FwriteLock != 0 {
		iMax = (*TWal)(unsafe.Pointer(pWal)).Fhdr.FmxFrame
		/* Restore the clients cache of the wal-index header to the state it
		 ** was in before the client began writing to the database.
		 */
		libc.Xmemcpy(tls, pWal+72, _walIndexHdr(tls, pWal), uint64(48))
		iFrame = (*TWal)(unsafe.Pointer(pWal)).Fhdr.FmxFrame + uint32(1)
		for {
			if !(rc == SQLITE_OK && iFrame <= iMax) {
				break
			}
			/* This call cannot fail. Unless the page for which the page number
			 ** is passed as the second argument is (a) in the cache and
			 ** (b) has an outstanding reference, then xUndo is either a no-op
			 ** (if (a) is false) or simply expels the page from the cache (if (b)
			 ** is false).
			 **
			 ** If the upper layer is doing a rollback, it is guaranteed that there
			 ** are no outstanding references to any page other than page 1. And
			 ** page 1 is never written to the log until the transaction is
			 ** committed. As a result, the call to xUndo may not fail.
			 */
			rc = (*(*func(*libc.TLS, uintptr, TPgno) int32)(unsafe.Pointer(&struct{ uintptr }{__ccgo_fp_xUndo})))(tls, pUndoCtx, _walFramePgno(tls, pWal, iFrame))
			goto _1
		_1:
			;
			iFrame = iFrame + 1
		}
		if iMax != (*TWal)(unsafe.Pointer(pWal)).Fhdr.FmxFrame {
			_walCleanupHash(tls, pWal)
		}
		(*TWal)(unsafe.Pointer(pWal)).FiReCksum = uint32(0)
	}
	return rc
}

func _sqlite3WhereRealloc(tls *libc.TLS, pWInfo uintptr, pOld uintptr, nByte Tu64) (r uintptr) {
	var pNew, pOldBlk uintptr
	_, _ = pNew, pOldBlk
	pNew = _sqlite3WhereMalloc(tls, pWInfo, nByte)
	if pNew != 0 && pOld != 0 {
		pOldBlk = pOld
		pOldBlk -= 16
		libc.Xmemcpy(tls, pNew, pOld, (*TWhereMemBlock)(unsafe.Pointer(pOldBlk)).Fsz)
	}
	return pNew
}

func _statClearPage(tls *libc.TLS, p uintptr) {
	var aPg uintptr
	_ = aPg
	aPg = (*TStatPage)(unsafe.Pointer(p)).FaPg
	_statClearCells(tls, p)
	Xsqlite3_free(tls, (*TStatPage)(unsafe.Pointer(p)).FzPath)
	libc.Xmemset(tls, p, 0, uint64(64))
	(*TStatPage)(unsafe.Pointer(p)).FaPg = aPg
}

// C documentation
//
//	/*
//	** Open a new DBSTAT cursor.
//	*/
func _statOpen(tls *libc.TLS, pVTab uintptr, ppCursor uintptr) (r int32) {
	var pCsr, pTab uintptr
	_, _ = pCsr, pTab
	pTab = pVTab
	pCsr = Xsqlite3_malloc64(tls, uint64(2152))
	if pCsr == uintptr(0) {
		return int32(SQLITE_NOMEM)
	} else {
		libc.Xmemset(tls, pCsr, 0, uint64(2152))
		(*TStatCursor)(unsafe.Pointer(pCsr)).Fbase.FpVtab = pVTab
		(*TStatCursor)(unsafe.Pointer(pCsr)).FiDb = (*TStatTable)(unsafe.Pointer(pTab)).FiDb
	}
	**(**uintptr)(__ccgo_up(ppCursor)) = pCsr
	return SQLITE_OK
}

// C documentation
//
//	/*
//	** Finish off a string by making sure it is zero-terminated.
//	** Return a pointer to the resulting string.  Return a NULL
//	** pointer if any kind of error was encountered.
//	*/
func _strAccumFinishRealloc(tls *libc.TLS, p uintptr) (r uintptr) {
	var zText, v1 uintptr
	_, _ = zText, v1
	zText = _sqlite3DbMallocRaw(tls, (*TStrAccum)(unsafe.Pointer(p)).Fdb, uint64(1)+uint64((*TStrAccum)(unsafe.Pointer(p)).FnChar))
	if zText != 0 {
		libc.Xmemcpy(tls, zText, (*TStrAccum)(unsafe.Pointer(p)).FzText, uint64((*TStrAccum)(unsafe.Pointer(p)).FnChar+uint32(1)))
		v1 = p + 29
		*(*Tu8)(unsafe.Pointer(v1)) = Tu8(int32(*(*Tu8)(unsafe.Pointer(v1))) | libc.Int32FromInt32(SQLITE_PRINTF_MALLOCED))
	} else {
		_sqlite3StrAccumSetError(tls, p, uint8(SQLITE_NOMEM))
	}
	(*TStrAccum)(unsafe.Pointer(p)).FzText = zText
	return zText
}

// C documentation
//
//	/*
//	** This routine is called if the collation factory fails to deliver a
//	** collation function in the best encoding but there may be other versions
//	** of this collation function (for other text encodings) available. Use one
//	** of these instead if they exist. Avoid a UTF-8 <-> UTF-16 conversion if
//	** possible.
//	*/
func _synthCollSeq(tls *libc.TLS, db uintptr, pColl uintptr) (r int32) {
	var i int32
	var pColl2, z uintptr
	_, _, _ = i, pColl2, z
	z = (*TCollSeq)(unsafe.Pointer(pColl)).FzName
	i = 0
	for {
		if !(i < int32(3)) {
			break
		}
		pColl2 = _sqlite3FindCollSeq(tls, db, _aEnc[i], z, 0)
		if (*TCollSeq)(unsafe.Pointer(pColl2)).FxCmp != uintptr(0) {
			libc.Xmemcpy(tls, pColl, pColl2, uint64(40))
			(*TCollSeq)(unsafe.Pointer(pColl)).FxDel = uintptr(0) /* Do not copy the destructor */
			return SQLITE_OK
		}
		goto _1
	_1:
		;
		i = i + 1
	}
	return int32(SQLITE_ERROR)
}

// C documentation
//
//	/*
//	** Free all memory belonging to the PmaReader object passed as the
//	** argument. All structure fields are set to zero before returning.
//	*/
func _vdbePmaReaderClear(tls *libc.TLS, pReadr uintptr) {
	Xsqlite3_free(tls, (*TPmaReader)(unsafe.Pointer(pReadr)).FaAlloc)
	Xsqlite3_free(tls, (*TPmaReader)(unsafe.Pointer(pReadr)).FaBuffer)
	if (*TPmaReader)(unsafe.Pointer(pReadr)).FaMap != 0 {
		_sqlite3OsUnfetch(tls, (*TPmaReader)(unsafe.Pointer(pReadr)).FpFd, 0, (*TPmaReader)(unsafe.Pointer(pReadr)).FaMap)
	}
	_vdbeIncrFree(tls, (*TPmaReader)(unsafe.Pointer(pReadr)).FpIncr)
	libc.Xmemset(tls, pReadr, 0, uint64(80))
}

// C documentation
//
//	/*
//	** Free all resources owned by the object indicated by argument pTask. All
//	** fields of *pTask are zeroed before returning.
//	*/
func _vdbeSortSubtaskCleanup(tls *libc.TLS, db uintptr, pTask uintptr) {
	_sqlite3DbFree(tls, db, (*TSortSubtask)(unsafe.Pointer(pTask)).FpUnpacked)
	/* pTask->list.aMemory can only be non-zero if it was handed memory
	 ** from the main thread.  That only occurs SQLITE_MAX_WORKER_THREADS>0 */
	if (*TSortSubtask)(unsafe.Pointer(pTask)).Flist.FaMemory != 0 {
		Xsqlite3_free(tls, (*TSortSubtask)(unsafe.Pointer(pTask)).Flist.FaMemory)
	} else {
		_vdbeSorterRecordFree(tls, uintptr(0), (*TSortSubtask)(unsafe.Pointer(pTask)).Flist.FpList)
	}
	if (*TSortSubtask)(unsafe.Pointer(pTask)).Ffile.FpFd != 0 {
		_sqlite3OsCloseFree(tls, (*TSortSubtask)(unsafe.Pointer(pTask)).Ffile.FpFd)
	}
	if (*TSortSubtask)(unsafe.Pointer(pTask)).Ffile2.FpFd != 0 {
		_sqlite3OsCloseFree(tls, (*TSortSubtask)(unsafe.Pointer(pTask)).Ffile2.FpFd)
	}
	libc.Xmemset(tls, pTask, 0, uint64(104))
}

// C documentation
//
//	/*
//	** This function does the work of sqlite3WalBeginReadTransaction() (see
//	** below). That function simply calls this one inside an SEH_TRY{...} block.
//	*/
func _walBeginReadTransaction(tls *libc.TLS, pWal uintptr, pChanged uintptr) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var bChanged, ckptLock, rc int32
	var pInfo, pSnapshot uintptr
	var _ /* cnt at bp+0 */ int32
	_, _, _, _, _ = bChanged, ckptLock, pInfo, pSnapshot, rc /* Return code */
	**(**int32)(__ccgo_up(bp)) = 0                           /* Number of TryBeginRead attempts */
	ckptLock = 0
	bChanged = 0
	pSnapshot = (*TWal)(unsafe.Pointer(pWal)).FpSnapshot
	if pSnapshot != 0 {
		if libc.Xmemcmp(tls, pSnapshot, pWal+72, uint64(48)) != 0 {
			bChanged = int32(1)
		}
		/* It is possible that there is a checkpointer thread running
		 ** concurrent with this code. If this is the case, it may be that the
		 ** checkpointer has already determined that it will checkpoint
		 ** snapshot X, where X is later in the wal file than pSnapshot, but
		 ** has not yet set the pInfo->nBackfillAttempted variable to indicate
		 ** its intent. To avoid the race condition this leads to, ensure that
		 ** there is no checkpointer process by taking a shared CKPT lock
		 ** before checking pInfo->nBackfillAttempted.  */
		rc = _walLockShared(tls, pWal, int32(WAL_CKPT_LOCK))
		if rc != SQLITE_OK {
			return rc
		}
		ckptLock = int32(1)
	}
	for cond := true; cond; cond = rc == -int32(1) {
		rc = _walTryBeginRead(tls, pWal, pChanged, 0, bp)
	}
	if rc == SQLITE_OK {
		if pSnapshot != 0 && libc.Xmemcmp(tls, pSnapshot, pWal+72, uint64(48)) != 0 {
			/* At this point the client has a lock on an aReadMark[] slot holding
			 ** a value equal to or smaller than pSnapshot->mxFrame, but pWal->hdr
			 ** is populated with the wal-index header corresponding to the head
			 ** of the wal file. Verify that pSnapshot is still valid before
			 ** continuing.  Reasons why pSnapshot might no longer be valid:
			 **
			 **    (1)  The WAL file has been reset since the snapshot was taken.
			 **         In this case, the salt will have changed.
			 **
			 **    (2)  A checkpoint as been attempted that wrote frames past
			 **         pSnapshot->mxFrame into the database file.  Note that the
			 **         checkpoint need not have completed for this to cause problems.
			 */
			pInfo = _walCkptInfo(tls, pWal)
			/* Check that the wal file has not been wrapped. Assuming that it has
			 ** not, also check that no checkpointer has attempted to checkpoint any
			 ** frames beyond pSnapshot->mxFrame. If either of these conditions are
			 ** true, return SQLITE_ERROR_SNAPSHOT. Otherwise, overwrite pWal->hdr
			 ** with *pSnapshot and set *pChanged as appropriate for opening the
			 ** snapshot.  */
			if !(libc.Xmemcmp(tls, pSnapshot+32, pWal+72+32, uint64(8)) != 0) && (*TWalIndexHdr)(unsafe.Pointer(pSnapshot)).FmxFrame >= (*TWalCkptInfo)(unsafe.Pointer(pInfo)).FnBackfillAttempted {
				libc.Xmemcpy(tls, pWal+72, pSnapshot, uint64(48))
				**(**int32)(__ccgo_up(pChanged)) = bChanged
			} else {
				rc = libc.Int32FromInt32(SQLITE_ERROR) | libc.Int32FromInt32(3)<<libc.Int32FromInt32(8)
			}
			/* A client using a non-current snapshot may not ignore any frames
			 ** from the start of the wal file. This is because, for a system
			 ** where (minFrame < iSnapshot < maxFrame), a checkpointer may
			 ** have omitted to checkpoint a frame earlier than minFrame in
			 ** the file because there exists a frame after iSnapshot that
			 ** is the same database page.  */
			(*TWal)(unsafe.Pointer(pWal)).FminFrame = uint32(1)
			if rc != SQLITE_OK {
				_sqlite3WalEndReadTransaction(tls, pWal)
			}
		}
	}
	/* Release the shared CKPT lock obtained above. */
	if ckptLock != 0 {
		_walUnlockShared(tls, pWal, int32(WAL_CKPT_LOCK))
	}
	return rc
}

// C documentation
//
//	/*
//	** Move the content of pSrc into pDest
//	*/
func _whereOrMove(tls *libc.TLS, pDest uintptr, pSrc uintptr) {
	(*TWhereOrSet)(unsafe.Pointer(pDest)).Fn = (*TWhereOrSet)(unsafe.Pointer(pSrc)).Fn
	libc.Xmemcpy(tls, pDest+8, pSrc+8, uint64((*TWhereOrSet)(unsafe.Pointer(pDest)).Fn)*uint64(16))
}

// C documentation
//
//	/*
//	** Remove any Window objects owned by the expression pExpr from the
//	** Select.pWin list of Select object pSelect.
//	*/
func _windowRemoveExprFromSelect(tls *libc.TLS, pSelect uintptr, pExpr uintptr) {
	bp := tls.Alloc(48)
	defer tls.Free(48)
	var _ /* sWalker at bp+0 */ TWalker
	if (*TSelect)(unsafe.Pointer(pSelect)).FpWin != 0 {
		libc.Xmemset(tls, bp, 0, uint64(48))
		(**(**TWalker)(__ccgo_up(bp))).FxExprCallback = __ccgo_fp(_resolveRemoveWindowsCb)
		*(*uintptr)(unsafe.Pointer(bp + 40)) = pSelect
		_sqlite3WalkExpr(tls, bp, pExpr)
	}
}
