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

//go:build (freebsd && 386) || (freebsd && arm)

package vec

import (
	"unsafe"

	"modernc.org/libc"
	libsqlite3 "modernc.org/sqlite/lib"
)

func Xbitmap_clear(tls *libc.TLS, bitmap uintptr, n Ti32) {
	libc.Xmemset(tls, bitmap, 0, libc.Uint32FromInt32(n/int32(m___CHAR_BIT)))
}

func Xbitmap_copy(tls *libc.TLS, base uintptr, from uintptr, n Ti32) {
	libc.Xmemcpy(tls, base, from, libc.Uint32FromInt32(n/int32(m___CHAR_BIT)))
}

func Xbitmap_fill(tls *libc.TLS, bitmap uintptr, n Ti32) {
	libc.Xmemset(tls, bitmap, int32(0xFF), libc.Uint32FromInt32(n/int32(m___CHAR_BIT)))
}

func Xbitmap_new(tls *libc.TLS, n Ti32) (r uintptr) {
	var p uintptr
	_ = p
	p = libsqlite3.Xsqlite3_malloc(tls, libc.Int32FromUint32(libc.Uint32FromInt32(n)*uint32(1)/uint32(m___CHAR_BIT)))
	if p != 0 {
		libc.Xmemset(tls, p, 0, libc.Uint32FromInt32(n)*uint32(1)/uint32(m___CHAR_BIT))
	}
	return p
}

func Xbitmap_new_from(tls *libc.TLS, n Ti32, from uintptr) (r uintptr) {
	var p uintptr
	_ = p
	p = libsqlite3.Xsqlite3_malloc(tls, libc.Int32FromUint32(libc.Uint32FromInt32(n)*uint32(1)/uint32(m___CHAR_BIT)))
	if p != 0 {
		libc.Xmemcpy(tls, p, from, libc.Uint32FromInt32(n/int32(m___CHAR_BIT)))
	}
	return p
}

func Xparse_npy_file(tls *libc.TLS, pVTab uintptr, file uintptr, pCur uintptr) (r int32) {
	bp := tls.Alloc(64)
	defer tls.Free(64)
	var dataSize, expectedDataSize Ti32
	var fileSize, n, rc int32
	var headerX uintptr
	var major, minor Tu8
	var totalHeaderLength Tsize_t
	var _ /* element_type at bp+16 */ _VectorElementType
	var _ /* fortran_order at bp+12 */ int32
	var _ /* header at bp+0 */ [10]uint8
	var _ /* headerLength at bp+10 */ Tuint16_t
	var _ /* numDimensions at bp+24 */ Tsize_t
	var _ /* numElements at bp+20 */ Tsize_t
	_, _, _, _, _, _, _, _, _ = dataSize, expectedDataSize, fileSize, headerX, major, minor, n, rc, totalHeaderLength
	libc.Xfseek(tls, file, 0, int32(m_SEEK_END))
	fileSize = libc.Xftell(tls, file)
	libc.Xfseek(tls, file, 0, m_SEEK_SET)
	n = libc.Int32FromUint32(libc.Xfread(tls, bp, uint32(1), uint32(10), file))
	if n != int32(10) {
		Xvtab_set_error(tls, pVTab, __ccgo_ts+2988, 0)
		return int32(m_SQLITE_ERROR)
	}
	if libc.Xmemcmp(tls, uintptr(unsafe.Pointer(&_NPY_MAGIC)), bp, uint32(6)) != 0 {
		Xvtab_set_error(tls, pVTab, __ccgo_ts+3015, 0)
		return int32(m_SQLITE_ERROR)
	}
	major = (**(**[10]uint8)(__ccgo_up(bp)))[int32(6)]
	minor = (**(**[10]uint8)(__ccgo_up(bp)))[int32(7)]
	**(**Tuint16_t)(__ccgo_up(bp + 10)) = uint16(0)
	libc.Xmemcpy(tls, bp+10, bp+8, uint32(2))
	totalHeaderLength = libc.Uint32FromInt64(6) + libc.Uint32FromInt64(1) + libc.Uint32FromInt64(1) + libc.Uint32FromInt64(2) + uint32(**(**Tuint16_t)(__ccgo_up(bp + 10)))
	dataSize = libc.Int32FromUint32(libc.Uint32FromInt32(fileSize) - totalHeaderLength)
	if dataSize < 0 {
		Xvtab_set_error(tls, pVTab, __ccgo_ts+3068, 0)
		return int32(m_SQLITE_ERROR)
	}
	headerX = libsqlite3.Xsqlite3_malloc(tls, libc.Int32FromUint16(**(**Tuint16_t)(__ccgo_up(bp + 10))))
	if **(**Tuint16_t)(__ccgo_up(bp + 10)) != 0 && !(headerX != 0) {
		return int32(m_SQLITE_NOMEM)
	}
	n = libc.Int32FromUint32(libc.Xfread(tls, headerX, uint32(1), uint32(**(**Tuint16_t)(__ccgo_up(bp + 10))), file))
	if n != libc.Int32FromUint16(**(**Tuint16_t)(__ccgo_up(bp + 10))) {
		libsqlite3.Xsqlite3_free(tls, headerX)
		Xvtab_set_error(tls, pVTab, __ccgo_ts+3068, 0)
		return int32(m_SQLITE_ERROR)
	}
	rc = Xparse_npy_header(tls, pVTab, headerX, uint32(**(**Tuint16_t)(__ccgo_up(bp + 10))), bp+16, bp+12, bp+20, bp+24)
	libsqlite3.Xsqlite3_free(tls, headerX)
	if rc != m_SQLITE_OK {
		// parse_npy_header already attackes an error emssage
		return rc
	}
	expectedDataSize = libc.Int32FromUint32(**(**Tsize_t)(__ccgo_up(bp + 20)) * Xvector_byte_size(tls, **(**_VectorElementType)(__ccgo_up(bp + 16)), **(**Tsize_t)(__ccgo_up(bp + 24))))
	if expectedDataSize != dataSize {
		Xvtab_set_error(tls, pVTab, __ccgo_ts+3110, libc.VaList(bp+40, expectedDataSize, dataSize))
		return int32(m_SQLITE_ERROR)
	}
	(*Tvec_npy_each_cursor)(unsafe.Pointer(pCur)).FmaxChunks = uint32(1024)
	(*Tvec_npy_each_cursor)(unsafe.Pointer(pCur)).FchunksBufferSize = Xvector_byte_size(tls, **(**_VectorElementType)(__ccgo_up(bp + 16)), **(**Tsize_t)(__ccgo_up(bp + 24))) * (*Tvec_npy_each_cursor)(unsafe.Pointer(pCur)).FmaxChunks
	(*Tvec_npy_each_cursor)(unsafe.Pointer(pCur)).FchunksBuffer = libsqlite3.Xsqlite3_malloc(tls, libc.Int32FromUint32((*Tvec_npy_each_cursor)(unsafe.Pointer(pCur)).FchunksBufferSize))
	if (*Tvec_npy_each_cursor)(unsafe.Pointer(pCur)).FchunksBufferSize != 0 && !((*Tvec_npy_each_cursor)(unsafe.Pointer(pCur)).FchunksBuffer != 0) {
		return int32(m_SQLITE_NOMEM)
	}
	(*Tvec_npy_each_cursor)(unsafe.Pointer(pCur)).FcurrentChunkSize = libc.Xfread(tls, (*Tvec_npy_each_cursor)(unsafe.Pointer(pCur)).FchunksBuffer, Xvector_byte_size(tls, **(**_VectorElementType)(__ccgo_up(bp + 16)), **(**Tsize_t)(__ccgo_up(bp + 24))), (*Tvec_npy_each_cursor)(unsafe.Pointer(pCur)).FmaxChunks, file)
	(*Tvec_npy_each_cursor)(unsafe.Pointer(pCur)).FcurrentChunkIndex = uint32(0)
	(*Tvec_npy_each_cursor)(unsafe.Pointer(pCur)).FelementType = **(**_VectorElementType)(__ccgo_up(bp + 16))
	(*Tvec_npy_each_cursor)(unsafe.Pointer(pCur)).FnElements = **(**Tsize_t)(__ccgo_up(bp + 20))
	(*Tvec_npy_each_cursor)(unsafe.Pointer(pCur)).FnDimensions = **(**Tsize_t)(__ccgo_up(bp + 24))
	(*Tvec_npy_each_cursor)(unsafe.Pointer(pCur)).Finput_type = int32(_VEC_NPY_EACH_INPUT_FILE)
	(*Tvec_npy_each_cursor)(unsafe.Pointer(pCur)).Feof = libc.BoolInt32((*Tvec_npy_each_cursor)(unsafe.Pointer(pCur)).FcurrentChunkSize == uint32(0))
	(*Tvec_npy_each_cursor)(unsafe.Pointer(pCur)).Ffile = file
	return m_SQLITE_OK
}

func Xvec0Update_Delete_ClearMetadata(tls *libc.TLS, p uintptr, metadata_idx int32, rowid Ti64, chunk_id Ti64, chunk_offset Tu64) (r int32) {
	bp := tls.Alloc(80)
	defer tls.Free(80)
	var kind Tvec0_metadata_column_kind
	var rc, rc2 int32
	var zSql uintptr
	var _ /* blobValue at bp+0 */ uintptr
	var _ /* block at bp+4 */ Tu8
	var _ /* n at bp+24 */ int32
	var _ /* stmt at bp+44 */ uintptr
	var _ /* v at bp+16 */ float64
	var _ /* v at bp+8 */ Ti64
	var _ /* view at bp+28 */ [16]Tu8
	_, _, _, _ = kind, rc, rc2, zSql
	kind = (**(**TVec0MetadataColumnDefinition)(__ccgo_up(p + 1000 + uintptr(metadata_idx)*12))).Fkind
	rc = libsqlite3.Xsqlite3_blob_open(tls, (*Tvec0_vtab)(unsafe.Pointer(p)).Fdb, (*Tvec0_vtab)(unsafe.Pointer(p)).FschemaName, **(**uintptr)(__ccgo_up(p + 376 + uintptr(metadata_idx)*4)), __ccgo_ts+4053, chunk_id, int32(1), bp)
	if rc != m_SQLITE_OK {
		return rc
	}
	switch kind {
	case int32(_VEC0_METADATA_COLUMN_KIND_BOOLEAN):
		rc = libsqlite3.Xsqlite3_blob_read(tls, **(**uintptr)(__ccgo_up(bp)), bp+4, int32(1), libc.Int32FromUint64(chunk_offset/libc.Uint64FromInt32(m___CHAR_BIT)))
		if rc != m_SQLITE_OK {
			goto done
		}
		**(**Tu8)(__ccgo_up(bp + 4)) = libc.Uint8FromInt32(int32(**(**Tu8)(__ccgo_up(bp + 4))) & ^(libc.Int32FromInt32(1) << (chunk_offset % libc.Uint64FromInt32(m___CHAR_BIT))))
		rc = libsqlite3.Xsqlite3_blob_write(tls, **(**uintptr)(__ccgo_up(bp)), bp+4, int32(1), libc.Int32FromUint64(chunk_offset/uint64(m___CHAR_BIT)))
	case int32(_VEC0_METADATA_COLUMN_KIND_INTEGER):
		**(**Ti64)(__ccgo_up(bp + 8)) = 0
		rc = libsqlite3.Xsqlite3_blob_write(tls, **(**uintptr)(__ccgo_up(bp)), bp+8, int32(8), libc.Int32FromUint64(chunk_offset*uint64(8)))
	case int32(_VEC0_METADATA_COLUMN_KIND_FLOAT):
		**(**float64)(__ccgo_up(bp + 16)) = libc.Float64FromInt32(0)
		rc = libsqlite3.Xsqlite3_blob_write(tls, **(**uintptr)(__ccgo_up(bp)), bp+16, int32(8), libc.Int32FromUint64(chunk_offset*uint64(8)))
	case int32(_VEC0_METADATA_COLUMN_KIND_TEXT):
		rc = libsqlite3.Xsqlite3_blob_read(tls, **(**uintptr)(__ccgo_up(bp)), bp+24, int32(4), libc.Int32FromUint64(chunk_offset*uint64(m_VEC0_METADATA_TEXT_VIEW_BUFFER_LENGTH)))
		if rc != m_SQLITE_OK {
			goto done
		}
		libc.Xmemset(tls, bp+28, 0, uint32(m_VEC0_METADATA_TEXT_VIEW_BUFFER_LENGTH))
		rc = libsqlite3.Xsqlite3_blob_write(tls, **(**uintptr)(__ccgo_up(bp)), bp+28, int32(16), libc.Int32FromUint64(chunk_offset*uint64(m_VEC0_METADATA_TEXT_VIEW_BUFFER_LENGTH)))
		if rc != m_SQLITE_OK {
			goto done
		}
		if **(**int32)(__ccgo_up(bp + 24)) > int32(m_VEC0_METADATA_TEXT_VIEW_DATA_LENGTH) {
			zSql = libsqlite3.Xsqlite3_mprintf(tls, __ccgo_ts+12987, libc.VaList(bp+56, (*Tvec0_vtab)(unsafe.Pointer(p)).FschemaName, (*Tvec0_vtab)(unsafe.Pointer(p)).FtableName, metadata_idx))
			if !(zSql != 0) {
				rc = int32(m_SQLITE_NOMEM)
				goto done
			}
			rc = libsqlite3.Xsqlite3_prepare_v2(tls, (*Tvec0_vtab)(unsafe.Pointer(p)).Fdb, zSql, -int32(1), bp+44, libc.UintptrFromInt32(0))
			if rc != m_SQLITE_OK {
				goto done
			}
			libsqlite3.Xsqlite3_bind_int64(tls, **(**uintptr)(__ccgo_up(bp + 44)), int32(1), rowid)
			rc = libsqlite3.Xsqlite3_step(tls, **(**uintptr)(__ccgo_up(bp + 44)))
			libsqlite3.Xsqlite3_finalize(tls, **(**uintptr)(__ccgo_up(bp + 44)))
			if rc != int32(m_SQLITE_DONE) {
				rc = int32(m_SQLITE_ERROR)
				goto done
			}
			// Fix for https://github.com/asg017/sqlite-vec/issues/274
			// sqlite3_step returns SQLITE_DONE (101) on DML success, but the
			// `done:` epilogue treats anything other than SQLITE_OK as an error.
			// Without this, SQLITE_DONE propagates up to vec0Update_Delete,
			// which aborts the DELETE scan and silently drops remaining rows.
			rc = m_SQLITE_OK
		}
		break
	}
	goto done
done:
	;
	rc2 = libsqlite3.Xsqlite3_blob_close(tls, **(**uintptr)(__ccgo_up(bp)))
	if rc == m_SQLITE_OK {
		return rc2
	}
	return rc
}

// C documentation
//
//	/**
//	 * @brief
//	 *
//	 * @param p vec0 virtual table
//	 * @param chunk_rowid: which chunk to write to
//	 * @param chunk_offset: the offset inside the chunk to write the vector to.
//	 * @param rowid: the rowid of the inserting row
//	 * @param vectorDatas: array of the vector data to insert
//	 * @param blobValidity: writeable validity blob of the row's assigned chunk.
//	 * @param validity: snapshot buffer of the valdity column from the row's
//	 * assigned chunk.
//	 * @return int SQLITE_OK on success, error code on failure
//	 */
func Xvec0Update_InsertWriteFinalStep(tls *libc.TLS, p uintptr, chunk_rowid Ti64, chunk_offset Ti64, _rowid Ti64, vectorDatas uintptr, blobChunksValidity uintptr, bufferChunksValidity uintptr) (r int32) {
	bp := tls.Alloc(80)
	defer tls.Free(80)
	*(*Ti64)(unsafe.Pointer(bp)) = _rowid
	var actual, actual1, expected, expected1 Ti64
	var brc, i, rc int32
	var _ /* blobChunksRowids at bp+8 */ uintptr
	var _ /* blobVectors at bp+16 */ uintptr
	var _ /* bx at bp+12 */ uint8
	_, _, _, _, _, _, _ = actual, actual1, brc, expected, expected1, i, rc
	**(**uintptr)(__ccgo_up(bp + 8)) = libc.UintptrFromInt32(0)
	// mark the validity bit for this row in the chunk's validity bitmap
	// Get the byte offset of the bitmap
	**(**uint8)(__ccgo_up(bp + 12)) = **(**uint8)(__ccgo_up(bufferChunksValidity + uintptr(chunk_offset/int64(m___CHAR_BIT))))
	// set the bit at the chunk_offset position inside that byte
	**(**uint8)(__ccgo_up(bp + 12)) = libc.Uint8FromInt32(libc.Int32FromUint8(**(**uint8)(__ccgo_up(bp + 12))) | int32(1)<<(chunk_offset%int64(m___CHAR_BIT)))
	// write that 1 byte
	rc = libsqlite3.Xsqlite3_blob_write(tls, blobChunksValidity, bp+12, int32(1), int32(chunk_offset/int64(m___CHAR_BIT)))
	if rc != m_SQLITE_OK {
		Xvtab_set_error(tls, p, __ccgo_ts+11995, 0)
		return rc
	}
	// Go insert the vector data into the vector chunk shadow tables
	i = 0
	for {
		if !(i < (*Tvec0_vtab)(unsafe.Pointer(p)).FnumVectorColumns) {
			break
		}
		rc = libsqlite3.Xsqlite3_blob_open(tls, (*Tvec0_vtab)(unsafe.Pointer(p)).Fdb, (*Tvec0_vtab)(unsafe.Pointer(p)).FschemaName, **(**uintptr)(__ccgo_up(p + 312 + uintptr(i)*4)), __ccgo_ts+3712, chunk_rowid, int32(1), bp+16)
		if rc != m_SQLITE_OK {
			Xvtab_set_error(tls, p, __ccgo_ts+12051, libc.VaList(bp+32, (*Tvec0_vtab)(unsafe.Pointer(p)).FschemaName, **(**uintptr)(__ccgo_up(p + 312 + uintptr(i)*4)), chunk_rowid))
			goto cleanup
		}
		expected = libc.Int64FromUint32(libc.Uint32FromInt32((*Tvec0_vtab)(unsafe.Pointer(p)).Fchunk_size) * Xvector_column_byte_size(tls, **(**TVectorColumnDefinition)(__ccgo_up(p + 440 + uintptr(i)*20))))
		actual = int64(libsqlite3.Xsqlite3_blob_bytes(tls, **(**uintptr)(__ccgo_up(bp + 16))))
		if actual != expected {
			// IMP: V16386_00456
			Xvtab_set_error(tls, p, __ccgo_ts+12091, libc.VaList(bp+32, (*Tvec0_vtab)(unsafe.Pointer(p)).FschemaName, **(**uintptr)(__ccgo_up(p + 312 + uintptr(i)*4)), chunk_rowid, expected, actual))
			rc = int32(m_SQLITE_ERROR)
			// already error, can ignore result code
			libsqlite3.Xsqlite3_blob_close(tls, **(**uintptr)(__ccgo_up(bp + 16)))
			goto cleanup
		}
		rc = _vec0_write_vector_to_vector_blob(tls, **(**uintptr)(__ccgo_up(bp + 16)), chunk_offset, **(**uintptr)(__ccgo_up(vectorDatas + uintptr(i)*4)), (**(**TVectorColumnDefinition)(__ccgo_up(p + 440 + uintptr(i)*20))).Fdimensions, (**(**TVectorColumnDefinition)(__ccgo_up(p + 440 + uintptr(i)*20))).Felement_type)
		if rc != m_SQLITE_OK {
			Xvtab_set_error(tls, p, __ccgo_ts+12186, libc.VaList(bp+32, (*Tvec0_vtab)(unsafe.Pointer(p)).FschemaName, **(**uintptr)(__ccgo_up(p + 312 + uintptr(i)*4)), chunk_rowid))
			rc = int32(m_SQLITE_ERROR)
			// already error, can ignore result code
			libsqlite3.Xsqlite3_blob_close(tls, **(**uintptr)(__ccgo_up(bp + 16)))
			goto cleanup
		}
		rc = libsqlite3.Xsqlite3_blob_close(tls, **(**uintptr)(__ccgo_up(bp + 16)))
		if rc != m_SQLITE_OK {
			Xvtab_set_error(tls, p, __ccgo_ts+12255, libc.VaList(bp+32, (*Tvec0_vtab)(unsafe.Pointer(p)).FschemaName, **(**uintptr)(__ccgo_up(p + 312 + uintptr(i)*4)), chunk_rowid))
			rc = int32(m_SQLITE_ERROR)
			goto cleanup
		}
		goto _1
	_1:
		;
		i = i + 1
	}
	// write the new rowid to the rowids column of the _chunks table
	rc = libsqlite3.Xsqlite3_blob_open(tls, (*Tvec0_vtab)(unsafe.Pointer(p)).Fdb, (*Tvec0_vtab)(unsafe.Pointer(p)).FschemaName, (*Tvec0_vtab)(unsafe.Pointer(p)).FshadowChunksName, __ccgo_ts+9690, chunk_rowid, int32(1), bp+8)
	if rc != m_SQLITE_OK {
		// IMP: V09221_26060
		Xvtab_set_error(tls, p, __ccgo_ts+12324, libc.VaList(bp+32, (*Tvec0_vtab)(unsafe.Pointer(p)).FschemaName, (*Tvec0_vtab)(unsafe.Pointer(p)).FshadowChunksName, chunk_rowid))
		goto cleanup
	}
	expected1 = libc.Int64FromUint32(libc.Uint32FromInt32((*Tvec0_vtab)(unsafe.Pointer(p)).Fchunk_size) * uint32(8))
	actual1 = int64(libsqlite3.Xsqlite3_blob_bytes(tls, **(**uintptr)(__ccgo_up(bp + 8))))
	if expected1 != actual1 {
		// IMP: V12779_29618
		Xvtab_set_error(tls, p, __ccgo_ts+12392, libc.VaList(bp+32, (*Tvec0_vtab)(unsafe.Pointer(p)).FschemaName, (*Tvec0_vtab)(unsafe.Pointer(p)).FshadowChunksName, chunk_rowid, expected1, actual1))
		rc = int32(m_SQLITE_ERROR)
		goto cleanup
	}
	rc = libsqlite3.Xsqlite3_blob_write(tls, **(**uintptr)(__ccgo_up(bp + 8)), bp, int32(8), int32(chunk_offset*int64(8)))
	if rc != m_SQLITE_OK {
		Xvtab_set_error(tls, p, __ccgo_ts+12487, libc.VaList(bp+32, (*Tvec0_vtab)(unsafe.Pointer(p)).FschemaName, (*Tvec0_vtab)(unsafe.Pointer(p)).FshadowChunksName, chunk_rowid))
		rc = int32(m_SQLITE_ERROR)
		goto cleanup
	}
	// Now with all the vectors inserted, go back and update the _rowids table
	// with the new chunk_rowid/chunk_offset values
	rc = Xvec0_rowids_update_position(tls, p, **(**Ti64)(__ccgo_up(bp)), chunk_rowid, chunk_offset)
	goto cleanup
cleanup:
	;
	brc = libsqlite3.Xsqlite3_blob_close(tls, **(**uintptr)(__ccgo_up(bp + 8)))
	if rc == m_SQLITE_OK && brc != m_SQLITE_OK {
		Xvtab_set_error(tls, p, __ccgo_ts+12556, libc.VaList(bp+32, (*Tvec0_vtab)(unsafe.Pointer(p)).FschemaName, (*Tvec0_vtab)(unsafe.Pointer(p)).FshadowChunksName, chunk_rowid))
		return brc
	}
	return rc
}

// C documentation
//
//	/**
//	 * @brief Adds a new chunk for the vec0 table, and the corresponding vector
//	 * chunks.
//	 *
//	 * Inserts a new row into the _chunks table, with blank data, and uses that new
//	 * rowid to insert new blank rows into _vector_chunksXX tables.
//	 *
//	 * @param p: vec0 table to add new chunk
//	 * @param paritionKeyValues: Array of partition key valeus for the new chunk, if available
//	 * @param chunk_rowid: Output pointer, if not NULL, then will be filled with the
//	 * new chunk rowid.
//	 * @return int SQLITE_OK on success, error code otherwise.
//	 */
func Xvec0_new_chunk(tls *libc.TLS, p uintptr, partitionKeyValues uintptr, chunk_rowid uintptr) (r int32) {
	bp := tls.Alloc(48)
	defer tls.Free(48)
	var failed, i, i1, i2, i3, i4, metadata_column_idx, rc, vector_column_idx int32
	var rowid, vectorsSize Ti64
	var s, zSql uintptr
	var _ /* stmt at bp+0 */ uintptr
	_, _, _, _, _, _, _, _, _, _, _, _, _ = failed, i, i1, i2, i3, i4, metadata_column_idx, rc, rowid, s, vector_column_idx, vectorsSize, zSql
	// Step 1: Insert a new row in _chunks, capture that new rowid
	if (*Tvec0_vtab)(unsafe.Pointer(p)).FnumPartitionColumns > 0 {
		s = libsqlite3.Xsqlite3_str_new(tls, libc.UintptrFromInt32(0))
		libsqlite3.Xsqlite3_str_appendf(tls, s, __ccgo_ts+5165, libc.VaList(bp+16, (*Tvec0_vtab)(unsafe.Pointer(p)).FschemaName, (*Tvec0_vtab)(unsafe.Pointer(p)).FtableName))
		libsqlite3.Xsqlite3_str_appendall(tls, s, __ccgo_ts+5194)
		i = 0
		for {
			if !(i < (*Tvec0_vtab)(unsafe.Pointer(p)).FnumPartitionColumns) {
				break
			}
			libsqlite3.Xsqlite3_str_appendf(tls, s, __ccgo_ts+5218, libc.VaList(bp+16, i))
			goto _1
		_1:
			;
			i = i + 1
		}
		libsqlite3.Xsqlite3_str_appendall(tls, s, __ccgo_ts+5234)
		i1 = 0
		for {
			if !(i1 < (*Tvec0_vtab)(unsafe.Pointer(p)).FnumPartitionColumns) {
				break
			}
			libsqlite3.Xsqlite3_str_appendall(tls, s, __ccgo_ts+5252)
			goto _2
		_2:
			;
			i1 = i1 + 1
		}
		libsqlite3.Xsqlite3_str_appendall(tls, s, __ccgo_ts+5256)
		zSql = libsqlite3.Xsqlite3_str_finish(tls, s)
	} else {
		zSql = libsqlite3.Xsqlite3_mprintf(tls, __ccgo_ts+5258, libc.VaList(bp+16, (*Tvec0_vtab)(unsafe.Pointer(p)).FschemaName, (*Tvec0_vtab)(unsafe.Pointer(p)).FtableName))
	}
	if !(zSql != 0) {
		return int32(m_SQLITE_NOMEM)
	}
	rc = libsqlite3.Xsqlite3_prepare_v2(tls, (*Tvec0_vtab)(unsafe.Pointer(p)).Fdb, zSql, -int32(1), bp, libc.UintptrFromInt32(0))
	libsqlite3.Xsqlite3_free(tls, zSql)
	if rc != m_SQLITE_OK {
		libsqlite3.Xsqlite3_finalize(tls, **(**uintptr)(__ccgo_up(bp)))
		return rc
	}
	libsqlite3.Xsqlite3_bind_int64(tls, **(**uintptr)(__ccgo_up(bp)), int32(1), int64((*Tvec0_vtab)(unsafe.Pointer(p)).Fchunk_size))                                                   // size
	libsqlite3.Xsqlite3_bind_zeroblob(tls, **(**uintptr)(__ccgo_up(bp)), int32(2), (*Tvec0_vtab)(unsafe.Pointer(p)).Fchunk_size/int32(m___CHAR_BIT))                                   // validity bitmap
	libsqlite3.Xsqlite3_bind_zeroblob(tls, **(**uintptr)(__ccgo_up(bp)), int32(3), libc.Int32FromUint32(libc.Uint32FromInt32((*Tvec0_vtab)(unsafe.Pointer(p)).Fchunk_size)*uint32(8))) // rowids
	i2 = 0
	for {
		if !(i2 < (*Tvec0_vtab)(unsafe.Pointer(p)).FnumPartitionColumns) {
			break
		}
		libsqlite3.Xsqlite3_bind_value(tls, **(**uintptr)(__ccgo_up(bp)), int32(4)+i2, **(**uintptr)(__ccgo_up(partitionKeyValues + uintptr(i2)*4)))
		goto _3
	_3:
		;
		i2 = i2 + 1
	}
	rc = libsqlite3.Xsqlite3_step(tls, **(**uintptr)(__ccgo_up(bp)))
	failed = libc.BoolInt32(rc != int32(m_SQLITE_DONE))
	rowid = libsqlite3.Xsqlite3_last_insert_rowid(tls, (*Tvec0_vtab)(unsafe.Pointer(p)).Fdb)
	libsqlite3.Xsqlite3_finalize(tls, **(**uintptr)(__ccgo_up(bp)))
	if failed != 0 {
		return int32(m_SQLITE_ERROR)
	}
	// Step 2: Create new vector chunks for each vector column, with
	//          that new chunk_rowid.
	//
	// SHADOW_TABLE_ROWID_QUIRK: The _vector_chunksNN and _metadatachunksNN
	// shadow tables declare "rowid PRIMARY KEY" without the INTEGER type, so
	// the user-defined "rowid" column is NOT an alias for the internal SQLite
	// rowid (_rowid_). When only appending rows these two happen to stay in
	// sync, but after a chunk is deleted (vec0Update_Delete_DeleteChunkIfEmpty)
	// and a new one is created, the auto-assigned _rowid_ can diverge from the
	// user "rowid" value. Since sqlite3_blob_open() addresses rows by internal
	// _rowid_, we must explicitly set BOTH _rowid_ and "rowid" to the same
	// value so that later blob operations can find the row.
	//
	// The correct long-term fix is changing the schema to
	//   "rowid INTEGER PRIMARY KEY"
	// which makes it a true alias, but that would break existing databases.
	i3 = 0
	for {
		if !(i3 < Xvec0_num_defined_user_columns(tls, p)) {
			break
		}
		if **(**Tvec0_user_column_kind)(__ccgo_up(p + 52 + uintptr(i3)*4)) != int32(_SQLITE_VEC0_USER_COLUMN_KIND_VECTOR) {
			goto _4
		}
		vector_column_idx = libc.Int32FromUint8(**(**Tuint8_t)(__ccgo_up(p + 260 + uintptr(i3))))
		vectorsSize = libc.Int64FromUint32(libc.Uint32FromInt32((*Tvec0_vtab)(unsafe.Pointer(p)).Fchunk_size) * Xvector_column_byte_size(tls, **(**TVectorColumnDefinition)(__ccgo_up(p + 440 + uintptr(vector_column_idx)*20))))
		// See SHADOW_TABLE_ROWID_QUIRK above for why _rowid_ and rowid are both set.
		zSql = libsqlite3.Xsqlite3_mprintf(tls, __ccgo_ts+5329, libc.VaList(bp+16, (*Tvec0_vtab)(unsafe.Pointer(p)).FschemaName, (*Tvec0_vtab)(unsafe.Pointer(p)).FtableName, vector_column_idx))
		if !(zSql != 0) {
			return int32(m_SQLITE_NOMEM)
		}
		rc = libsqlite3.Xsqlite3_prepare_v2(tls, (*Tvec0_vtab)(unsafe.Pointer(p)).Fdb, zSql, -int32(1), bp, libc.UintptrFromInt32(0))
		libsqlite3.Xsqlite3_free(tls, zSql)
		if rc != m_SQLITE_OK {
			libsqlite3.Xsqlite3_finalize(tls, **(**uintptr)(__ccgo_up(bp)))
			return rc
		}
		libsqlite3.Xsqlite3_bind_int64(tls, **(**uintptr)(__ccgo_up(bp)), int32(1), rowid) // _rowid_ (internal SQLite rowid)
		libsqlite3.Xsqlite3_bind_int64(tls, **(**uintptr)(__ccgo_up(bp)), int32(2), rowid) // rowid   (user-defined column)
		libsqlite3.Xsqlite3_bind_zeroblob64(tls, **(**uintptr)(__ccgo_up(bp)), int32(3), libc.Uint64FromInt64(vectorsSize))
		rc = libsqlite3.Xsqlite3_step(tls, **(**uintptr)(__ccgo_up(bp)))
		libsqlite3.Xsqlite3_finalize(tls, **(**uintptr)(__ccgo_up(bp)))
		if rc != int32(m_SQLITE_DONE) {
			return rc
		}
		goto _4
	_4:
		;
		i3 = i3 + 1
	}
	// Step 3: Create new metadata chunks for each metadata column
	i4 = 0
	for {
		if !(i4 < Xvec0_num_defined_user_columns(tls, p)) {
			break
		}
		if **(**Tvec0_user_column_kind)(__ccgo_up(p + 52 + uintptr(i4)*4)) != int32(_SQLITE_VEC0_USER_COLUMN_KIND_METADATA) {
			goto _5
		}
		metadata_column_idx = libc.Int32FromUint8(**(**Tuint8_t)(__ccgo_up(p + 260 + uintptr(i4))))
		// See SHADOW_TABLE_ROWID_QUIRK above for why _rowid_ and rowid are both set.
		zSql = libsqlite3.Xsqlite3_mprintf(tls, __ccgo_ts+5410, libc.VaList(bp+16, (*Tvec0_vtab)(unsafe.Pointer(p)).FschemaName, (*Tvec0_vtab)(unsafe.Pointer(p)).FtableName, metadata_column_idx))
		if !(zSql != 0) {
			return int32(m_SQLITE_NOMEM)
		}
		rc = libsqlite3.Xsqlite3_prepare_v2(tls, (*Tvec0_vtab)(unsafe.Pointer(p)).Fdb, zSql, -int32(1), bp, libc.UintptrFromInt32(0))
		libsqlite3.Xsqlite3_free(tls, zSql)
		if rc != m_SQLITE_OK {
			libsqlite3.Xsqlite3_finalize(tls, **(**uintptr)(__ccgo_up(bp)))
			return rc
		}
		libsqlite3.Xsqlite3_bind_int64(tls, **(**uintptr)(__ccgo_up(bp)), int32(1), rowid) // _rowid_ (internal SQLite rowid)
		libsqlite3.Xsqlite3_bind_int64(tls, **(**uintptr)(__ccgo_up(bp)), int32(2), rowid) // rowid   (user-defined column)
		libsqlite3.Xsqlite3_bind_zeroblob64(tls, **(**uintptr)(__ccgo_up(bp)), int32(3), libc.Uint64FromInt32(Xvec0_metadata_chunk_size(tls, (**(**TVec0MetadataColumnDefinition)(__ccgo_up(p + 1000 + uintptr(metadata_column_idx)*12))).Fkind, (*Tvec0_vtab)(unsafe.Pointer(p)).Fchunk_size)))
		rc = libsqlite3.Xsqlite3_step(tls, **(**uintptr)(__ccgo_up(bp)))
		libsqlite3.Xsqlite3_finalize(tls, **(**uintptr)(__ccgo_up(bp)))
		if rc != int32(m_SQLITE_DONE) {
			return rc
		}
		goto _5
	_5:
		;
		i4 = i4 + 1
	}
	if chunk_rowid != 0 {
		**(**Ti64)(__ccgo_up(chunk_rowid)) = rowid
	}
	return m_SQLITE_OK
}

// C documentation
//
//	/**
//	 * @brief Result the given metadata value for the given row and metadata column index.
//	 * Will traverse the metadatachunksNN table with BLOB I/0 for the given rowid.
//	 *
//	 * @param p
//	 * @param rowid
//	 * @param metadata_idx
//	 * @param context
//	 * @return int
//	 */
func Xvec0_result_metadata_value_for_rowid(tls *libc.TLS, p uintptr, rowid Ti64, metadata_idx int32, context uintptr) (r int32) {
	bp := tls.Alloc(96)
	defer tls.Free(96)
	var length, rc, value int32
	var zSql uintptr
	var _ /* blobValue at bp+16 */ uintptr
	var _ /* block at bp+20 */ Tu8
	var _ /* chunk_id at bp+0 */ Ti64
	var _ /* chunk_offset at bp+8 */ Ti64
	var _ /* stmt at bp+56 */ uintptr
	var _ /* value at bp+24 */ Ti64
	var _ /* value at bp+32 */ float64
	var _ /* view at bp+40 */ [16]Tu8
	_, _, _, _ = length, rc, value, zSql
	rc = Xvec0_get_chunk_position(tls, p, rowid, libc.UintptrFromInt32(0), bp, bp+8)
	if rc != m_SQLITE_OK {
		return rc
	}
	rc = libsqlite3.Xsqlite3_blob_open(tls, (*Tvec0_vtab)(unsafe.Pointer(p)).Fdb, (*Tvec0_vtab)(unsafe.Pointer(p)).FschemaName, **(**uintptr)(__ccgo_up(p + 376 + uintptr(metadata_idx)*4)), __ccgo_ts+4053, **(**Ti64)(__ccgo_up(bp)), 0, bp+16)
	if rc != m_SQLITE_OK {
		return rc
	}
	switch (**(**TVec0MetadataColumnDefinition)(__ccgo_up(p + 1000 + uintptr(metadata_idx)*12))).Fkind {
	case int32(_VEC0_METADATA_COLUMN_KIND_BOOLEAN):
		rc = libsqlite3.Xsqlite3_blob_read(tls, **(**uintptr)(__ccgo_up(bp + 16)), bp+20, int32(1), int32(**(**Ti64)(__ccgo_up(bp + 8))/int64(m___CHAR_BIT)))
		if rc != m_SQLITE_OK {
			goto done
		}
		value = libc.Int32FromUint8(**(**Tu8)(__ccgo_up(bp + 20))) >> (**(**Ti64)(__ccgo_up(bp + 8)) % int64(m___CHAR_BIT)) & int32(1)
		libsqlite3.Xsqlite3_result_int(tls, context, value)
	case int32(_VEC0_METADATA_COLUMN_KIND_INTEGER):
		rc = libsqlite3.Xsqlite3_blob_read(tls, **(**uintptr)(__ccgo_up(bp + 16)), bp+24, int32(8), int32(**(**Ti64)(__ccgo_up(bp + 8))*int64(8)))
		if rc != m_SQLITE_OK {
			goto done
		}
		libsqlite3.Xsqlite3_result_int64(tls, context, **(**Ti64)(__ccgo_up(bp + 24)))
	case int32(_VEC0_METADATA_COLUMN_KIND_FLOAT):
		rc = libsqlite3.Xsqlite3_blob_read(tls, **(**uintptr)(__ccgo_up(bp + 16)), bp+32, int32(8), int32(**(**Ti64)(__ccgo_up(bp + 8))*int64(8)))
		if rc != m_SQLITE_OK {
			goto done
		}
		libsqlite3.Xsqlite3_result_double(tls, context, **(**float64)(__ccgo_up(bp + 32)))
	case int32(_VEC0_METADATA_COLUMN_KIND_TEXT):
		rc = libsqlite3.Xsqlite3_blob_read(tls, **(**uintptr)(__ccgo_up(bp + 16)), bp+40, int32(m_VEC0_METADATA_TEXT_VIEW_BUFFER_LENGTH), int32(**(**Ti64)(__ccgo_up(bp + 8))*int64(m_VEC0_METADATA_TEXT_VIEW_BUFFER_LENGTH)))
		if rc != m_SQLITE_OK {
			goto done
		}
		length = **(**int32)(__ccgo_up(bp + 40))
		if length <= int32(m_VEC0_METADATA_TEXT_VIEW_DATA_LENGTH) {
			libsqlite3.Xsqlite3_result_text(tls, context, bp+40+libc.UintptrFromInt32(4), length, uintptr(-libc.Int32FromInt32(1)))
		} else {
			zSql = libsqlite3.Xsqlite3_mprintf(tls, __ccgo_ts+4058, libc.VaList(bp+72, (*Tvec0_vtab)(unsafe.Pointer(p)).FschemaName, (*Tvec0_vtab)(unsafe.Pointer(p)).FtableName, metadata_idx))
			if !(zSql != 0) {
				rc = int32(m_SQLITE_ERROR)
				goto done
			}
			rc = libsqlite3.Xsqlite3_prepare_v2(tls, (*Tvec0_vtab)(unsafe.Pointer(p)).Fdb, zSql, -int32(1), bp+56, libc.UintptrFromInt32(0))
			libsqlite3.Xsqlite3_free(tls, zSql)
			if rc != m_SQLITE_OK {
				goto done
			}
			libsqlite3.Xsqlite3_bind_int64(tls, **(**uintptr)(__ccgo_up(bp + 56)), int32(1), rowid)
			rc = libsqlite3.Xsqlite3_step(tls, **(**uintptr)(__ccgo_up(bp + 56)))
			if rc != int32(m_SQLITE_ROW) {
				libsqlite3.Xsqlite3_finalize(tls, **(**uintptr)(__ccgo_up(bp + 56)))
				rc = int32(m_SQLITE_ERROR)
				goto done
			}
			libsqlite3.Xsqlite3_result_value(tls, context, libsqlite3.Xsqlite3_column_value(tls, **(**uintptr)(__ccgo_up(bp + 56)), 0))
			libsqlite3.Xsqlite3_finalize(tls, **(**uintptr)(__ccgo_up(bp + 56)))
			rc = m_SQLITE_OK
		}
		break
	}
	goto done
done:
	;
	// blobValue is read-only, will not fail on close
	libsqlite3.Xsqlite3_blob_close(tls, **(**uintptr)(__ccgo_up(bp + 16)))
	return rc
}

// C documentation
//
//	/**
//	 * @brief Fill in bitmap of chunk values, whether or not the values match a metadata constraint
//	 *
//	 * @param p vec0_vtab
//	 * @param metadata_idx index of the metatadata column to perfrom constraints on
//	 * @param value sqlite3_value of the constraints value
//	 * @param blob sqlite3_blob that is already opened on the metdata column's shadow chunk table
//	 * @param chunk_rowid rowid of the chunk to calculate on
//	 * @param b pre-allocated and zero'd out bitmap to write results to
//	 * @param size size of the chunk
//	 * @return int SQLITE_OK on success, error code otherwise
//	 */
func Xvec0_set_metadata_filter_bitmap(tls *libc.TLS, p uintptr, metadata_idx int32, op Tvec0_metadata_operator, value uintptr, blob uintptr, chunk_rowid Ti64, b uintptr, size int32, aMetadataIn uintptr, argv_idx int32) (r int32) {
	var aTarget, array, array1, buffer, metadataIn, metadataIn1 uintptr
	var blobSize, i, i1, i10, i11, i12, i13, i14, i15, i2, i3, i4, i5, i6, i7, i9, metadataInIdx, rc, szMatch, target int32
	var i8, target_idx Tsize_t
	var kind Tvec0_metadata_column_kind
	var target1 Ti64
	var target2 float64
	_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = aTarget, array, array1, blobSize, buffer, i, i1, i10, i11, i12, i13, i14, i15, i2, i3, i4, i5, i6, i7, i8, i9, kind, metadataIn, metadataIn1, metadataInIdx, rc, szMatch, target, target1, target2, target_idx
	rc = libsqlite3.Xsqlite3_blob_reopen(tls, blob, chunk_rowid)
	if rc != m_SQLITE_OK {
		return rc
	}
	kind = (**(**TVec0MetadataColumnDefinition)(__ccgo_up(p + 1000 + uintptr(metadata_idx)*12))).Fkind
	szMatch = 0
	blobSize = libsqlite3.Xsqlite3_blob_bytes(tls, blob)
	switch kind {
	case int32(_VEC0_METADATA_COLUMN_KIND_BOOLEAN):
		szMatch = libc.BoolInt32(blobSize == size/int32(m___CHAR_BIT))
	case int32(_VEC0_METADATA_COLUMN_KIND_INTEGER):
		szMatch = libc.BoolInt32(libc.Uint32FromInt32(blobSize) == libc.Uint32FromInt32(size)*uint32(8))
	case int32(_VEC0_METADATA_COLUMN_KIND_FLOAT):
		szMatch = libc.BoolInt32(libc.Uint32FromInt32(blobSize) == libc.Uint32FromInt32(size)*uint32(8))
	case int32(_VEC0_METADATA_COLUMN_KIND_TEXT):
		szMatch = libc.BoolInt32(blobSize == size*int32(m_VEC0_METADATA_TEXT_VIEW_BUFFER_LENGTH))
		break
	}
	if !(szMatch != 0) {
		return int32(m_SQLITE_ERROR)
	}
	buffer = libsqlite3.Xsqlite3_malloc(tls, blobSize)
	if !(buffer != 0) {
		return int32(m_SQLITE_NOMEM)
	}
	rc = libsqlite3.Xsqlite3_blob_read(tls, blob, buffer, blobSize, 0)
	if rc != m_SQLITE_OK {
		goto done
	}
	switch kind {
	case int32(_VEC0_METADATA_COLUMN_KIND_BOOLEAN):
		target = libsqlite3.Xsqlite3_value_int(tls, value)
		if target != 0 && op == int32(_VEC0_METADATA_OPERATOR_EQ) || !(target != 0) && op == int32(_VEC0_METADATA_OPERATOR_NE) {
			i = 0
			for {
				if !(i < size) {
					break
				}
				Xbitmap_set(tls, b, i, Xbitmap_get(tls, buffer, i))
				goto _1
			_1:
				;
				i = i + 1
			}
		} else {
			i1 = 0
			for {
				if !(i1 < size) {
					break
				}
				Xbitmap_set(tls, b, i1, libc.BoolInt32(!(Xbitmap_get(tls, buffer, i1) != 0)))
				goto _2
			_2:
				;
				i1 = i1 + 1
			}
		}
	case int32(_VEC0_METADATA_COLUMN_KIND_INTEGER):
		array = buffer
		target1 = libsqlite3.Xsqlite3_value_int64(tls, value)
		switch op {
		case int32(_VEC0_METADATA_OPERATOR_EQ):
			i2 = 0
			for {
				if !(i2 < size) {
					break
				}
				Xbitmap_set(tls, b, i2, libc.BoolInt32(**(**Ti64)(__ccgo_up(array + uintptr(i2)*8)) == target1))
				goto _3
			_3:
				;
				i2 = i2 + 1
			}
		case int32(_VEC0_METADATA_OPERATOR_GT):
			i3 = 0
			for {
				if !(i3 < size) {
					break
				}
				Xbitmap_set(tls, b, i3, libc.BoolInt32(**(**Ti64)(__ccgo_up(array + uintptr(i3)*8)) > target1))
				goto _4
			_4:
				;
				i3 = i3 + 1
			}
		case int32(_VEC0_METADATA_OPERATOR_LE):
			i4 = 0
			for {
				if !(i4 < size) {
					break
				}
				Xbitmap_set(tls, b, i4, libc.BoolInt32(**(**Ti64)(__ccgo_up(array + uintptr(i4)*8)) <= target1))
				goto _5
			_5:
				;
				i4 = i4 + 1
			}
		case int32(_VEC0_METADATA_OPERATOR_LT):
			i5 = 0
			for {
				if !(i5 < size) {
					break
				}
				Xbitmap_set(tls, b, i5, libc.BoolInt32(**(**Ti64)(__ccgo_up(array + uintptr(i5)*8)) < target1))
				goto _6
			_6:
				;
				i5 = i5 + 1
			}
		case int32(_VEC0_METADATA_OPERATOR_GE):
			i6 = 0
			for {
				if !(i6 < size) {
					break
				}
				Xbitmap_set(tls, b, i6, libc.BoolInt32(**(**Ti64)(__ccgo_up(array + uintptr(i6)*8)) >= target1))
				goto _7
			_7:
				;
				i6 = i6 + 1
			}
		case int32(_VEC0_METADATA_OPERATOR_NE):
			i7 = 0
			for {
				if !(i7 < size) {
					break
				}
				Xbitmap_set(tls, b, i7, libc.BoolInt32(**(**Ti64)(__ccgo_up(array + uintptr(i7)*8)) != target1))
				goto _8
			_8:
				;
				i7 = i7 + 1
			}
		case int32(_VEC0_METADATA_OPERATOR_IN):
			metadataInIdx = -int32(1)
			i8 = uint32(0)
			for {
				if !(i8 < (*TArray)(unsafe.Pointer(aMetadataIn)).Flength) {
					break
				}
				metadataIn = (*TArray)(unsafe.Pointer(aMetadataIn)).Fz + uintptr(i8)*24
				if (*TVec0MetadataIn)(unsafe.Pointer(metadataIn)).Fargv_idx == argv_idx {
					metadataInIdx = libc.Int32FromUint32(i8)
					break
				}
				goto _9
			_9:
				;
				i8 = i8 + 1
			}
			if metadataInIdx < 0 {
				rc = int32(m_SQLITE_ERROR)
				goto done
			}
			metadataIn1 = (*TArray)(unsafe.Pointer(aMetadataIn)).Fz + uintptr(metadataInIdx)*24
			aTarget = metadataIn1 + 8
			i9 = 0
			for {
				if !(i9 < size) {
					break
				}
				target_idx = uint32(0)
				for {
					if !(target_idx < (*TArray)(unsafe.Pointer(aTarget)).Flength) {
						break
					}
					if **(**Ti64)(__ccgo_up((*TArray)(unsafe.Pointer(aTarget)).Fz + uintptr(target_idx)*8)) == **(**Ti64)(__ccgo_up(array + uintptr(i9)*8)) {
						Xbitmap_set(tls, b, i9, int32(1))
						break
					}
					goto _11
				_11:
					;
					target_idx = target_idx + 1
				}
				goto _10
			_10:
				;
				i9 = i9 + 1
			}
			break
		}
	case int32(_VEC0_METADATA_COLUMN_KIND_FLOAT):
		array1 = buffer
		target2 = libsqlite3.Xsqlite3_value_double(tls, value)
		switch op {
		case int32(_VEC0_METADATA_OPERATOR_EQ):
			i10 = 0
			for {
				if !(i10 < size) {
					break
				}
				Xbitmap_set(tls, b, i10, libc.BoolInt32(**(**float64)(__ccgo_up(array1 + uintptr(i10)*8)) == target2))
				goto _12
			_12:
				;
				i10 = i10 + 1
			}
		case int32(_VEC0_METADATA_OPERATOR_GT):
			i11 = 0
			for {
				if !(i11 < size) {
					break
				}
				Xbitmap_set(tls, b, i11, libc.BoolInt32(**(**float64)(__ccgo_up(array1 + uintptr(i11)*8)) > target2))
				goto _13
			_13:
				;
				i11 = i11 + 1
			}
		case int32(_VEC0_METADATA_OPERATOR_LE):
			i12 = 0
			for {
				if !(i12 < size) {
					break
				}
				Xbitmap_set(tls, b, i12, libc.BoolInt32(**(**float64)(__ccgo_up(array1 + uintptr(i12)*8)) <= target2))
				goto _14
			_14:
				;
				i12 = i12 + 1
			}
		case int32(_VEC0_METADATA_OPERATOR_LT):
			i13 = 0
			for {
				if !(i13 < size) {
					break
				}
				Xbitmap_set(tls, b, i13, libc.BoolInt32(**(**float64)(__ccgo_up(array1 + uintptr(i13)*8)) < target2))
				goto _15
			_15:
				;
				i13 = i13 + 1
			}
		case int32(_VEC0_METADATA_OPERATOR_GE):
			i14 = 0
			for {
				if !(i14 < size) {
					break
				}
				Xbitmap_set(tls, b, i14, libc.BoolInt32(**(**float64)(__ccgo_up(array1 + uintptr(i14)*8)) >= target2))
				goto _16
			_16:
				;
				i14 = i14 + 1
			}
		case int32(_VEC0_METADATA_OPERATOR_NE):
			i15 = 0
			for {
				if !(i15 < size) {
					break
				}
				Xbitmap_set(tls, b, i15, libc.BoolInt32(**(**float64)(__ccgo_up(array1 + uintptr(i15)*8)) != target2))
				goto _17
			_17:
				;
				i15 = i15 + 1
			}
		case int32(_VEC0_METADATA_OPERATOR_IN):
			// should never be reached
			break
		}
	case int32(_VEC0_METADATA_COLUMN_KIND_TEXT):
		rc = Xvec0_metadata_filter_text(tls, p, value, buffer, size, op, b, metadata_idx, int32(chunk_rowid), aMetadataIn, argv_idx)
		if rc != m_SQLITE_OK {
			goto done
		}
		break
	}
	goto done
done:
	;
	libsqlite3.Xsqlite3_free(tls, buffer)
	return rc
}

func Xvec0_write_metadata_value(tls *libc.TLS, p uintptr, metadata_column_idx int32, rowid Ti64, chunk_id Ti64, chunk_offset Ti64, v uintptr, isupdate int32) (r int32) {
	bp := tls.Alloc(96)
	defer tls.Free(96)
	var kind Tvec0_metadata_column_kind
	var metadata_column, s, zSql, zSql1 uintptr
	var rc, value, v1 int32
	var _ /* blobValue at bp+0 */ uintptr
	var _ /* block at bp+4 */ Tu8
	var _ /* n at bp+28 */ int32
	var _ /* prev_n at bp+24 */ int32
	var _ /* stmt at bp+48 */ uintptr
	var _ /* stmt at bp+52 */ uintptr
	var _ /* value at bp+16 */ float64
	var _ /* value at bp+8 */ Ti64
	var _ /* view at bp+32 */ [16]Tu8
	_, _, _, _, _, _, _, _ = kind, metadata_column, rc, s, value, zSql, zSql1, v1
	metadata_column = p + 1000 + uintptr(metadata_column_idx)*12
	kind = (*TVec0MetadataColumnDefinition)(unsafe.Pointer(metadata_column)).Fkind
	// verify input value matches column type
	switch kind {
	case int32(_VEC0_METADATA_COLUMN_KIND_BOOLEAN):
		if libsqlite3.Xsqlite3_value_type(tls, v) != int32(m_SQLITE_INTEGER) || libsqlite3.Xsqlite3_value_int(tls, v) != 0 && libsqlite3.Xsqlite3_value_int(tls, v) != int32(1) {
			rc = int32(m_SQLITE_ERROR)
			Xvtab_set_error(tls, p, __ccgo_ts+12625, libc.VaList(bp+64, (*TVec0MetadataColumnDefinition)(unsafe.Pointer(metadata_column)).Fname_length, (*TVec0MetadataColumnDefinition)(unsafe.Pointer(metadata_column)).Fname))
			goto done
		}
	case int32(_VEC0_METADATA_COLUMN_KIND_INTEGER):
		if libsqlite3.Xsqlite3_value_type(tls, v) != int32(m_SQLITE_INTEGER) {
			rc = int32(m_SQLITE_ERROR)
			Xvtab_set_error(tls, p, __ccgo_ts+12674, libc.VaList(bp+64, (*TVec0MetadataColumnDefinition)(unsafe.Pointer(metadata_column)).Fname_length, (*TVec0MetadataColumnDefinition)(unsafe.Pointer(metadata_column)).Fname, Xtype_name(tls, libsqlite3.Xsqlite3_value_type(tls, v))))
			goto done
		}
	case int32(_VEC0_METADATA_COLUMN_KIND_FLOAT):
		if libsqlite3.Xsqlite3_value_type(tls, v) != int32(m_SQLITE_FLOAT) {
			rc = int32(m_SQLITE_ERROR)
			Xvtab_set_error(tls, p, __ccgo_ts+12737, libc.VaList(bp+64, (*TVec0MetadataColumnDefinition)(unsafe.Pointer(metadata_column)).Fname_length, (*TVec0MetadataColumnDefinition)(unsafe.Pointer(metadata_column)).Fname, Xtype_name(tls, libsqlite3.Xsqlite3_value_type(tls, v))))
			goto done
		}
	case int32(_VEC0_METADATA_COLUMN_KIND_TEXT):
		if libsqlite3.Xsqlite3_value_type(tls, v) != int32(m_SQLITE_TEXT) {
			rc = int32(m_SQLITE_ERROR)
			Xvtab_set_error(tls, p, __ccgo_ts+12796, libc.VaList(bp+64, (*TVec0MetadataColumnDefinition)(unsafe.Pointer(metadata_column)).Fname_length, (*TVec0MetadataColumnDefinition)(unsafe.Pointer(metadata_column)).Fname, Xtype_name(tls, libsqlite3.Xsqlite3_value_type(tls, v))))
			goto done
		}
		break
	}
	**(**uintptr)(__ccgo_up(bp)) = libc.UintptrFromInt32(0)
	rc = libsqlite3.Xsqlite3_blob_open(tls, (*Tvec0_vtab)(unsafe.Pointer(p)).Fdb, (*Tvec0_vtab)(unsafe.Pointer(p)).FschemaName, **(**uintptr)(__ccgo_up(p + 376 + uintptr(metadata_column_idx)*4)), __ccgo_ts+4053, chunk_id, int32(1), bp)
	if rc != m_SQLITE_OK {
		goto done
	}
	switch kind {
	case int32(_VEC0_METADATA_COLUMN_KIND_BOOLEAN):
		value = libsqlite3.Xsqlite3_value_int(tls, v)
		rc = libsqlite3.Xsqlite3_blob_read(tls, **(**uintptr)(__ccgo_up(bp)), bp+4, int32(1), int32(chunk_offset/libc.Int64FromInt32(m___CHAR_BIT)))
		if rc != m_SQLITE_OK {
			goto done
		}
		if value != 0 {
			**(**Tu8)(__ccgo_up(bp + 4)) = libc.Uint8FromInt32(int32(**(**Tu8)(__ccgo_up(bp + 4))) | libc.Int32FromInt32(1)<<(chunk_offset%libc.Int64FromInt32(m___CHAR_BIT)))
		} else {
			**(**Tu8)(__ccgo_up(bp + 4)) = libc.Uint8FromInt32(int32(**(**Tu8)(__ccgo_up(bp + 4))) & ^(libc.Int32FromInt32(1) << (chunk_offset % libc.Int64FromInt32(m___CHAR_BIT))))
		}
		rc = libsqlite3.Xsqlite3_blob_write(tls, **(**uintptr)(__ccgo_up(bp)), bp+4, int32(1), int32(chunk_offset/int64(m___CHAR_BIT)))
	case int32(_VEC0_METADATA_COLUMN_KIND_INTEGER):
		**(**Ti64)(__ccgo_up(bp + 8)) = libsqlite3.Xsqlite3_value_int64(tls, v)
		rc = libsqlite3.Xsqlite3_blob_write(tls, **(**uintptr)(__ccgo_up(bp)), bp+8, int32(8), int32(chunk_offset*int64(8)))
	case int32(_VEC0_METADATA_COLUMN_KIND_FLOAT):
		**(**float64)(__ccgo_up(bp + 16)) = libsqlite3.Xsqlite3_value_double(tls, v)
		rc = libsqlite3.Xsqlite3_blob_write(tls, **(**uintptr)(__ccgo_up(bp)), bp+16, int32(8), int32(chunk_offset*int64(8)))
	case int32(_VEC0_METADATA_COLUMN_KIND_TEXT):
		rc = libsqlite3.Xsqlite3_blob_read(tls, **(**uintptr)(__ccgo_up(bp)), bp+24, int32(4), int32(chunk_offset*int64(m_VEC0_METADATA_TEXT_VIEW_BUFFER_LENGTH)))
		if rc != m_SQLITE_OK {
			goto done
		}
		s = libsqlite3.Xsqlite3_value_text(tls, v)
		**(**int32)(__ccgo_up(bp + 28)) = libsqlite3.Xsqlite3_value_bytes(tls, v)
		libc.Xmemset(tls, bp+32, 0, uint32(m_VEC0_METADATA_TEXT_VIEW_BUFFER_LENGTH))
		libc.Xmemcpy(tls, bp+32, bp+28, uint32(4))
		if **(**int32)(__ccgo_up(bp + 28)) <= libc.Int32FromInt32(m_VEC0_METADATA_TEXT_VIEW_BUFFER_LENGTH)-libc.Int32FromInt32(4) {
			v1 = **(**int32)(__ccgo_up(bp + 28))
		} else {
			v1 = libc.Int32FromInt32(m_VEC0_METADATA_TEXT_VIEW_BUFFER_LENGTH) - libc.Int32FromInt32(4)
		}
		libc.Xmemcpy(tls, bp+32+uintptr(4), s, libc.Uint32FromInt32(v1))
		rc = libsqlite3.Xsqlite3_blob_write(tls, **(**uintptr)(__ccgo_up(bp)), bp+32, int32(m_VEC0_METADATA_TEXT_VIEW_BUFFER_LENGTH), int32(chunk_offset*int64(m_VEC0_METADATA_TEXT_VIEW_BUFFER_LENGTH)))
		if **(**int32)(__ccgo_up(bp + 28)) > int32(m_VEC0_METADATA_TEXT_VIEW_DATA_LENGTH) {
			if isupdate != 0 && **(**int32)(__ccgo_up(bp + 24)) > int32(m_VEC0_METADATA_TEXT_VIEW_DATA_LENGTH) {
				zSql = libsqlite3.Xsqlite3_mprintf(tls, __ccgo_ts+12853, libc.VaList(bp+64, (*Tvec0_vtab)(unsafe.Pointer(p)).FschemaName, (*Tvec0_vtab)(unsafe.Pointer(p)).FtableName, metadata_column_idx))
			} else {
				zSql = libsqlite3.Xsqlite3_mprintf(tls, __ccgo_ts+12918, libc.VaList(bp+64, (*Tvec0_vtab)(unsafe.Pointer(p)).FschemaName, (*Tvec0_vtab)(unsafe.Pointer(p)).FtableName, metadata_column_idx))
			}
			if !(zSql != 0) {
				rc = int32(m_SQLITE_NOMEM)
				goto done
			}
			rc = libsqlite3.Xsqlite3_prepare_v2(tls, (*Tvec0_vtab)(unsafe.Pointer(p)).Fdb, zSql, -int32(1), bp+48, libc.UintptrFromInt32(0))
			if rc != m_SQLITE_OK {
				goto done
			}
			libsqlite3.Xsqlite3_bind_int64(tls, **(**uintptr)(__ccgo_up(bp + 48)), int32(1), rowid)
			libsqlite3.Xsqlite3_bind_text(tls, **(**uintptr)(__ccgo_up(bp + 48)), int32(2), s, **(**int32)(__ccgo_up(bp + 28)), libc.UintptrFromInt32(0))
			rc = libsqlite3.Xsqlite3_step(tls, **(**uintptr)(__ccgo_up(bp + 48)))
			libsqlite3.Xsqlite3_finalize(tls, **(**uintptr)(__ccgo_up(bp + 48)))
			if rc != int32(m_SQLITE_DONE) {
				rc = int32(m_SQLITE_ERROR)
				goto done
			}
		} else {
			if **(**int32)(__ccgo_up(bp + 24)) > int32(m_VEC0_METADATA_TEXT_VIEW_DATA_LENGTH) {
				zSql1 = libsqlite3.Xsqlite3_mprintf(tls, __ccgo_ts+12987, libc.VaList(bp+64, (*Tvec0_vtab)(unsafe.Pointer(p)).FschemaName, (*Tvec0_vtab)(unsafe.Pointer(p)).FtableName, metadata_column_idx))
				if !(zSql1 != 0) {
					rc = int32(m_SQLITE_NOMEM)
					goto done
				}
				rc = libsqlite3.Xsqlite3_prepare_v2(tls, (*Tvec0_vtab)(unsafe.Pointer(p)).Fdb, zSql1, -int32(1), bp+52, libc.UintptrFromInt32(0))
				if rc != m_SQLITE_OK {
					goto done
				}
				libsqlite3.Xsqlite3_bind_int64(tls, **(**uintptr)(__ccgo_up(bp + 52)), int32(1), rowid)
				rc = libsqlite3.Xsqlite3_step(tls, **(**uintptr)(__ccgo_up(bp + 52)))
				libsqlite3.Xsqlite3_finalize(tls, **(**uintptr)(__ccgo_up(bp + 52)))
				if rc != int32(m_SQLITE_DONE) {
					rc = int32(m_SQLITE_ERROR)
					goto done
				}
			}
		}
		break
	}
	if rc != m_SQLITE_OK {
	}
	rc = libsqlite3.Xsqlite3_blob_close(tls, **(**uintptr)(__ccgo_up(bp)))
	if rc != m_SQLITE_OK {
		goto done
	}
	goto done
done:
	;
	return rc
	return r
}

func Xvector_byte_size(tls *libc.TLS, element_type _VectorElementType, dimensions Tsize_t) (r Tsize_t) {
	switch element_type {
	case int32(_SQLITE_VEC_ELEMENT_TYPE_FLOAT32):
		return dimensions * uint32(4)
	case int32(_SQLITE_VEC_ELEMENT_TYPE_INT8):
		return dimensions * uint32(1)
	case int32(_SQLITE_VEC_ELEMENT_TYPE_BIT):
		return dimensions / uint32(m___CHAR_BIT)
	}
	return uint32(0)
}

func _bitvec_from_value(tls *libc.TLS, value uintptr, vector uintptr, dimensions uintptr, __ccgo_fp_cleanup uintptr, pzErr uintptr) (r int32) {
	var blob uintptr
	var bytes, value_type int32
	_, _, _ = blob, bytes, value_type
	value_type = libsqlite3.Xsqlite3_value_type(tls, value)
	if value_type == int32(m_SQLITE_BLOB) {
		blob = libsqlite3.Xsqlite3_value_blob(tls, value)
		bytes = libsqlite3.Xsqlite3_value_bytes(tls, value)
		if bytes == 0 {
			**(**uintptr)(__ccgo_up(pzErr)) = libsqlite3.Xsqlite3_mprintf(tls, __ccgo_ts+47, 0)
			return int32(m_SQLITE_ERROR)
		}
		**(**uintptr)(__ccgo_up(vector)) = blob
		**(**Tsize_t)(__ccgo_up(dimensions)) = libc.Uint32FromInt32(bytes * int32(m___CHAR_BIT))
		**(**Tvector_cleanup)(__ccgo_up(__ccgo_fp_cleanup)) = __ccgo_fp(Xvector_cleanup_noop)
		return m_SQLITE_OK
	}
	**(**uintptr)(__ccgo_up(pzErr)) = libsqlite3.Xsqlite3_mprintf(tls, __ccgo_ts+313, 0)
	return int32(m_SQLITE_ERROR)
}

// C documentation
//
//	/**
//	 * @brief Calculate the hamming distance between two bitvectors.
//	 *
//	 * @param a - first bitvector, MUST have d dimensions
//	 * @param b - second bitvector, MUST have d dimensions
//	 * @param d - pointer to size_t, MUST be divisible by CHAR_BIT
//	 * @return f32
//	 */
func _distance_hamming(tls *libc.TLS, a uintptr, b uintptr, d uintptr) (r Tf32) {
	var dimensions Tsize_t
	_ = dimensions
	dimensions = **(**Tsize_t)(__ccgo_up(d))
	if dimensions%uint32(64) == uint32(0) {
		return _distance_hamming_u64(tls, a, b, dimensions/uint32(8)/uint32(m___CHAR_BIT))
	}
	return _distance_hamming_u8(tls, a, b, dimensions/uint32(m___CHAR_BIT))
}

// C documentation
//
//	/**
//	 * @brief Write the vector data into the provided vector blob at the given
//	 * offset
//	 *
//	 * @param blobVectors SQLite BLOB to write to
//	 * @param chunk_offset the "offset" (ie validity bitmap position) to write the
//	 * vector to
//	 * @param bVector pointer to the vector containing data
//	 * @param dimensions how many dimensions the vector has
//	 * @param element_type the vector type
//	 * @return result of sqlite3_blob_write, SQLITE_OK on success, otherwise failure
//	 */
func _vec0_write_vector_to_vector_blob(tls *libc.TLS, blobVectors uintptr, chunk_offset Ti64, bVector uintptr, dimensions Tsize_t, element_type _VectorElementType) (r int32) {
	var n, offset int32
	_, _ = n, offset
	switch element_type {
	case int32(_SQLITE_VEC_ELEMENT_TYPE_FLOAT32):
		n = libc.Int32FromUint32(dimensions * uint32(4))
		offset = int32(chunk_offset * libc.Int64FromUint32(dimensions) * int64(4))
	case int32(_SQLITE_VEC_ELEMENT_TYPE_INT8):
		n = libc.Int32FromUint32(dimensions * uint32(1))
		offset = int32(chunk_offset * libc.Int64FromUint32(dimensions) * int64(1))
	case int32(_SQLITE_VEC_ELEMENT_TYPE_BIT):
		n = libc.Int32FromUint32(dimensions / uint32(m___CHAR_BIT))
		offset = int32(chunk_offset * libc.Int64FromUint32(dimensions) / int64(m___CHAR_BIT))
		break
	}
	return libsqlite3.Xsqlite3_blob_write(tls, blobVectors, bVector, n, offset)
}

func _vec_bit(tls *libc.TLS, context uintptr, argc int32, argv uintptr) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var rc int32
	var _ /* cleanup at bp+8 */ Tvector_cleanup
	var _ /* dimensions at bp+4 */ Tsize_t
	var _ /* errmsg at bp+12 */ uintptr
	var _ /* vector at bp+0 */ uintptr
	_ = rc
	rc = _bitvec_from_value(tls, **(**uintptr)(__ccgo_up(argv)), bp, bp+4, bp+8, bp+12)
	if rc != m_SQLITE_OK {
		libsqlite3.Xsqlite3_result_error(tls, context, **(**uintptr)(__ccgo_up(bp + 12)), -int32(1))
		libsqlite3.Xsqlite3_free(tls, **(**uintptr)(__ccgo_up(bp + 12)))
		return
	}
	libsqlite3.Xsqlite3_result_blob(tls, context, **(**uintptr)(__ccgo_up(bp)), libc.Int32FromUint32(**(**Tsize_t)(__ccgo_up(bp + 4))/uint32(m___CHAR_BIT)), uintptr(-libc.Int32FromInt32(1)))
	libsqlite3.Xsqlite3_result_subtype(tls, context, uint32(_SQLITE_VEC_ELEMENT_TYPE_BIT))
	(*(*func(*libc.TLS, uintptr))(unsafe.Pointer(bp + 8)))(tls, **(**uintptr)(__ccgo_up(bp)))
}

func _vec_quantize_binary(tls *libc.TLS, context uintptr, argc int32, argv uintptr) {
	bp := tls.Alloc(32)
	defer tls.Free(32)
	var i, i1 Tsize_t
	var out, v2 uintptr
	var rc, res, res1, sz int32
	var _ /* dimensions at bp+4 */ Tsize_t
	var _ /* elementType at bp+16 */ _VectorElementType
	var _ /* pzError at bp+12 */ uintptr
	var _ /* vector at bp+0 */ uintptr
	var _ /* vectorCleanup at bp+8 */ Tvector_cleanup
	_, _, _, _, _, _, _, _ = i, i1, out, rc, res, res1, sz, v2
	rc = Xvector_from_value(tls, **(**uintptr)(__ccgo_up(argv)), bp, bp+4, bp+16, bp+8, bp+12)
	if rc != m_SQLITE_OK {
		libsqlite3.Xsqlite3_result_error(tls, context, **(**uintptr)(__ccgo_up(bp + 12)), -int32(1))
		libsqlite3.Xsqlite3_free(tls, **(**uintptr)(__ccgo_up(bp + 12)))
		return
	}
	if **(**Tsize_t)(__ccgo_up(bp + 4)) <= uint32(0) {
		libsqlite3.Xsqlite3_result_error(tls, context, __ccgo_ts+974, -int32(1))
		goto cleanup
		return
	}
	if **(**Tsize_t)(__ccgo_up(bp + 4))%uint32(m___CHAR_BIT) != uint32(0) {
		libsqlite3.Xsqlite3_result_error(tls, context, __ccgo_ts+1013, -int32(1))
		goto cleanup
		return
	}
	sz = libc.Int32FromUint32(**(**Tsize_t)(__ccgo_up(bp + 4)) / uint32(m___CHAR_BIT))
	out = libsqlite3.Xsqlite3_malloc(tls, sz)
	if !(out != 0) {
		libsqlite3.Xsqlite3_result_error_code(tls, context, int32(m_SQLITE_NOMEM))
		goto cleanup
		return
	}
	libc.Xmemset(tls, out, 0, libc.Uint32FromInt32(sz))
	switch **(**_VectorElementType)(__ccgo_up(bp + 16)) {
	case int32(_SQLITE_VEC_ELEMENT_TYPE_FLOAT32):
		i = uint32(0)
		for {
			if !(i < **(**Tsize_t)(__ccgo_up(bp + 4))) {
				break
			}
			res = libc.BoolInt32(float64(**(**Tf32)(__ccgo_up(**(**uintptr)(__ccgo_up(bp)) + uintptr(i)*4))) > float64(0))
			v2 = out + uintptr(i/uint32(8))
			*(*Tu8)(unsafe.Pointer(v2)) = Tu8(int32(*(*Tu8)(unsafe.Pointer(v2))) | res<<(i%libc.Uint32FromInt32(8)))
			goto _1
		_1:
			;
			i = i + 1
		}
	case int32(_SQLITE_VEC_ELEMENT_TYPE_INT8):
		i1 = uint32(0)
		for {
			if !(i1 < **(**Tsize_t)(__ccgo_up(bp + 4))) {
				break
			}
			res1 = libc.BoolInt32(int32(**(**Ti8)(__ccgo_up(**(**uintptr)(__ccgo_up(bp)) + uintptr(i1)))) > 0)
			v2 = out + uintptr(i1/uint32(8))
			*(*Tu8)(unsafe.Pointer(v2)) = Tu8(int32(*(*Tu8)(unsafe.Pointer(v2))) | res1<<(i1%libc.Uint32FromInt32(8)))
			goto _3
		_3:
			;
			i1 = i1 + 1
		}
	case int32(_SQLITE_VEC_ELEMENT_TYPE_BIT):
		libsqlite3.Xsqlite3_result_error(tls, context, __ccgo_ts+1079, -int32(1))
		libsqlite3.Xsqlite3_free(tls, out)
		return
	}
	libsqlite3.Xsqlite3_result_blob(tls, context, out, sz, __ccgo_fp(libsqlite3.Xsqlite3_free))
	libsqlite3.Xsqlite3_result_subtype(tls, context, uint32(_SQLITE_VEC_ELEMENT_TYPE_BIT))
	goto cleanup
cleanup:
	;
	(*(*func(*libc.TLS, uintptr))(unsafe.Pointer(bp + 8)))(tls, **(**uintptr)(__ccgo_up(bp)))
}

func _vec_slice(tls *libc.TLS, context uintptr, argc int32, argv uintptr) {
	bp := tls.Alloc(32)
	defer tls.Free(32)
	var end, outSize, outSize1, outSize2, rc, start int32
	var i, i1, i2, n Tsize_t
	var out, out1, out2 uintptr
	var _ /* cleanup at bp+8 */ Tvector_cleanup
	var _ /* dimensions at bp+4 */ Tsize_t
	var _ /* elementType at bp+16 */ _VectorElementType
	var _ /* err at bp+12 */ uintptr
	var _ /* vector at bp+0 */ uintptr
	_, _, _, _, _, _, _, _, _, _, _, _, _ = end, i, i1, i2, n, out, out1, out2, outSize, outSize1, outSize2, rc, start
	rc = Xvector_from_value(tls, **(**uintptr)(__ccgo_up(argv)), bp, bp+4, bp+16, bp+8, bp+12)
	if rc != m_SQLITE_OK {
		libsqlite3.Xsqlite3_result_error(tls, context, **(**uintptr)(__ccgo_up(bp + 12)), -int32(1))
		libsqlite3.Xsqlite3_free(tls, **(**uintptr)(__ccgo_up(bp + 12)))
		return
	}
	start = libsqlite3.Xsqlite3_value_int(tls, **(**uintptr)(__ccgo_up(argv + 1*4)))
	end = libsqlite3.Xsqlite3_value_int(tls, **(**uintptr)(__ccgo_up(argv + 2*4)))
	if start < 0 {
		libsqlite3.Xsqlite3_result_error(tls, context, __ccgo_ts+1260, -int32(1))
		goto done
	}
	if end < 0 {
		libsqlite3.Xsqlite3_result_error(tls, context, __ccgo_ts+1306, -int32(1))
		goto done
	}
	if libc.Uint32FromInt32(start) > **(**Tsize_t)(__ccgo_up(bp + 4)) {
		libsqlite3.Xsqlite3_result_error(tls, context, __ccgo_ts+1350, -int32(1))
		goto done
	}
	if libc.Uint32FromInt32(end) > **(**Tsize_t)(__ccgo_up(bp + 4)) {
		libsqlite3.Xsqlite3_result_error(tls, context, __ccgo_ts+1411, -int32(1))
		goto done
	}
	if start > end {
		libsqlite3.Xsqlite3_result_error(tls, context, __ccgo_ts+1470, -int32(1))
		goto done
	}
	if start == end {
		libsqlite3.Xsqlite3_result_error(tls, context, __ccgo_ts+1518, -int32(1))
		goto done
	}
	n = libc.Uint32FromInt32(end - start)
	switch **(**_VectorElementType)(__ccgo_up(bp + 16)) {
	case int32(_SQLITE_VEC_ELEMENT_TYPE_FLOAT32):
		outSize = libc.Int32FromUint32(n * uint32(4))
		out = libsqlite3.Xsqlite3_malloc(tls, outSize)
		if !(out != 0) {
			libsqlite3.Xsqlite3_result_error_nomem(tls, context)
			goto done
		}
		libc.Xmemset(tls, out, 0, libc.Uint32FromInt32(outSize))
		i = uint32(0)
		for {
			if !(i < n) {
				break
			}
			**(**Tf32)(__ccgo_up(out + uintptr(i)*4)) = **(**Tf32)(__ccgo_up(**(**uintptr)(__ccgo_up(bp)) + uintptr(libc.Uint32FromInt32(start)+i)*4))
			goto _1
		_1:
			;
			i = i + 1
		}
		libsqlite3.Xsqlite3_result_blob(tls, context, out, outSize, __ccgo_fp(libsqlite3.Xsqlite3_free))
		libsqlite3.Xsqlite3_result_subtype(tls, context, uint32(_SQLITE_VEC_ELEMENT_TYPE_FLOAT32))
		goto done
	case int32(_SQLITE_VEC_ELEMENT_TYPE_INT8):
		outSize1 = libc.Int32FromUint32(n * uint32(1))
		out1 = libsqlite3.Xsqlite3_malloc(tls, outSize1)
		if !(out1 != 0) {
			libsqlite3.Xsqlite3_result_error_nomem(tls, context)
			return
		}
		libc.Xmemset(tls, out1, 0, libc.Uint32FromInt32(outSize1))
		i1 = uint32(0)
		for {
			if !(i1 < n) {
				break
			}
			**(**Ti8)(__ccgo_up(out1 + uintptr(i1))) = **(**Ti8)(__ccgo_up(**(**uintptr)(__ccgo_up(bp)) + uintptr(libc.Uint32FromInt32(start)+i1)))
			goto _2
		_2:
			;
			i1 = i1 + 1
		}
		libsqlite3.Xsqlite3_result_blob(tls, context, out1, outSize1, __ccgo_fp(libsqlite3.Xsqlite3_free))
		libsqlite3.Xsqlite3_result_subtype(tls, context, uint32(_SQLITE_VEC_ELEMENT_TYPE_INT8))
		goto done
	case int32(_SQLITE_VEC_ELEMENT_TYPE_BIT):
		if start%int32(m___CHAR_BIT) != 0 {
			libsqlite3.Xsqlite3_result_error(tls, context, __ccgo_ts+1601, -int32(1))
			goto done
		}
		if end%int32(m___CHAR_BIT) != 0 {
			libsqlite3.Xsqlite3_result_error(tls, context, __ccgo_ts+1637, -int32(1))
			goto done
		}
		outSize2 = libc.Int32FromUint32(n / uint32(m___CHAR_BIT))
		out2 = libsqlite3.Xsqlite3_malloc(tls, outSize2)
		if !(out2 != 0) {
			libsqlite3.Xsqlite3_result_error_nomem(tls, context)
			return
		}
		libc.Xmemset(tls, out2, 0, libc.Uint32FromInt32(outSize2))
		i2 = uint32(0)
		for {
			if !(i2 < n/uint32(m___CHAR_BIT)) {
				break
			}
			**(**Tu8)(__ccgo_up(out2 + uintptr(i2))) = **(**Tu8)(__ccgo_up(**(**uintptr)(__ccgo_up(bp)) + uintptr(libc.Uint32FromInt32(start/libc.Int32FromInt32(m___CHAR_BIT))+i2)))
			goto _3
		_3:
			;
			i2 = i2 + 1
		}
		libsqlite3.Xsqlite3_result_blob(tls, context, out2, outSize2, __ccgo_fp(libsqlite3.Xsqlite3_free))
		libsqlite3.Xsqlite3_result_subtype(tls, context, uint32(_SQLITE_VEC_ELEMENT_TYPE_BIT))
		goto done
	}
	goto done
done:
	;
	(*(*func(*libc.TLS, uintptr))(unsafe.Pointer(bp + 8)))(tls, **(**uintptr)(__ccgo_up(bp)))
}

const m___INTPTR_FMTd__ = "d"

const m___INTPTR_FMTi__ = "i"

const m___INTPTR_MAX__ = 2147483647

const m___LONG_BIT = 32

const m___OFF_MAX = "__LLONG_MAX"

const m___OFF_MIN = "__LLONG_MIN"

const m___POINTER_WIDTH__ = 32

const m___PTRDIFF_FMTd__ = "d"

const m___PTRDIFF_FMTi__ = "i"

const m___PTRDIFF_MAX__ = 2147483647

const m___QUAD_MAX = "__LLONG_MAX"

const m___QUAD_MIN = "__LLONG_MIN"

const m___SIZE_FMTX__ = "X"

const m___SIZE_FMTo__ = "o"

const m___SIZE_FMTu__ = "u"

const m___SIZE_FMTx__ = "x"

const m___SIZE_MAX__ = 4294967295

const m___SIZE_T_MAX = "__UINT_MAX"

const m___SSIZE_MAX = "__INT_MAX"

const m___UINTPTR_FMTX__ = "X"

const m___UINTPTR_FMTo__ = "o"

const m___UINTPTR_FMTu__ = "u"

const m___UINTPTR_FMTx__ = "x"

const m___UINTPTR_MAX__ = 4294967295

const m___UINTPTR_WIDTH__ = 32

const m___ULONG_MAX = 0xffffffff

const m___UQUAD_MAX = "__ULLONG_MAX"

const m___WORDSIZE = 32

type t__clock_t = uint32

type t__critical_t = int32

type t__intfptr_t = int32

type t__intptr_t = int32

type t__key_t = int32

type t__ptrdiff_t = int32

type t__register_t = int32

type t__size_t = uint32

type t__ssize_t = int32

type t__suseconds_t = int32

type t__u_register_t = uint32

type t__uintfptr_t = uint32

type t__uintptr_t = uint32

type t__vm_offset_t = uint32

type t__vm_size_t = uint32
