// File: /workspace/github_c_makefile_star2_O1/genometools[P]genometools/src/external/sqlite-3.33.0/sqlite3.c
// Function: sqlite3RunVacuum
int sqlite3RunVacuum(
  char **pzErrMsg,        /* Write error message here */
  sqlite3 *db,            /* Database connection */
  int iDb,                /* Which attached DB to vacuum */
  sqlite3_value *pOut     /* Write results here, if not NULL. VACUUM INTO */
){
  int rc = SQLITE_OK;     /* Return code from service routines */
  Btree *pMain;           /* The database being vacuumed */
  Btree *pTemp;           /* The temporary database we vacuum into */
  u32 saved_mDbFlags;     /* Saved value of db->mDbFlags */
  u64 saved_flags;        /* Saved value of db->flags */
  int saved_nChange;      /* Saved value of db->nChange */
  int saved_nTotalChange; /* Saved value of db->nTotalChange */
  u32 saved_openFlags;    /* Saved value of db->openFlags */
  u8 saved_mTrace;        /* Saved trace settings */
  Db *pDb = 0;            /* Database to detach at end of vacuum */
  int isMemDb;            /* True if vacuuming a :memory: database */
  int nRes;               /* Bytes of reserved space at the end of each page */
  int nDb;                /* Number of attached databases */
  const char *zDbMain;    /* Schema name of database to vacuum */
  const char *zOut;       /* Name of output file */

  if( !db->autoCommit ){
    sqlite3SetString(pzErrMsg, db, "cannot VACUUM from within a transaction");
    return SQLITE_ERROR; /* IMP: R-12218-18073 */
  }
  if( db->nVdbeActive>1 ){
    sqlite3SetString(pzErrMsg, db,"cannot VACUUM - SQL statements in progress");
    return SQLITE_ERROR; /* IMP: R-15610-35227 */
  }
  saved_openFlags = db->openFlags;
  if( pOut ){
    if( sqlite3_value_type(pOut)!=SQLITE_TEXT ){
      sqlite3SetString(pzErrMsg, db, "non-text filename");
      return SQLITE_ERROR;
    }
    zOut = (const char*)sqlite3_value_text(pOut);
    db->openFlags &= ~SQLITE_OPEN_READONLY;
    db->openFlags |= SQLITE_OPEN_CREATE|SQLITE_OPEN_READWRITE;
  }else{
    zOut = "";
  }

  /* Save the current value of the database flags so that it can be
  ** restored before returning. Then set the writable-schema flag, and
  ** disable CHECK and foreign key constraints.  */
  saved_flags = db->flags;
  saved_mDbFlags = db->mDbFlags;
  saved_nChange = db->nChange;
  saved_nTotalChange = db->nTotalChange;
  saved_mTrace = db->mTrace;
  db->flags |= SQLITE_WriteSchema | SQLITE_IgnoreChecks;
  db->mDbFlags |= DBFLAG_PreferBuiltin | DBFLAG_Vacuum;
  db->flags &= ~(u64)(SQLITE_ForeignKeys | SQLITE_ReverseOrder
                   | SQLITE_Defensive | SQLITE_CountRows);
  db->mTrace = 0;

  zDbMain = db->aDb[iDb].zDbSName;
  pMain = db->aDb[iDb].pBt;
  isMemDb = sqlite3PagerIsMemdb(sqlite3BtreePager(pMain));

  /* Attach the temporary database as 'vacuum_db'. The synchronous pragma
  ** can be set to 'off' for this file, as it is not recovered if a crash
  ** occurs anyway. The integrity of the database is maintained by a
  ** (possibly synchronous) transaction opened on the main database before
  ** sqlite3BtreeCopyFile() is called.
  **
  ** An optimisation would be to use a non-journaled pager.
  ** (Later:) I tried setting "PRAGMA vacuum_db.journal_mode=OFF" but
  ** that actually made the VACUUM run slower.  Very little journalling
  ** actually occurs when doing a vacuum since the vacuum_db is initially
  ** empty.  Only the journal header is written.  Apparently it takes more
  ** time to parse and run the PRAGMA to turn journalling off than it does
  ** to write the journal header file.
  */
  nDb = db->nDb;
  rc = execSqlF(db, pzErrMsg, "ATTACH %Q AS vacuum_db", zOut);
  db->openFlags = saved_openFlags;
  if( rc!=SQLITE_OK ) goto end_of_vacuum;
  assert( (db->nDb-1)==nDb );
  pDb = &db->aDb[nDb];
  assert( strcmp(pDb->zDbSName,"vacuum_db")==0 );
  pTemp = pDb->pBt;
  if( pOut ){
    sqlite3_file *id = sqlite3PagerFile(sqlite3BtreePager(pTemp));
    i64 sz = 0;
    if( id->pMethods!=0 && (sqlite3OsFileSize(id, &sz)!=SQLITE_OK || sz>0) ){
      rc = SQLITE_ERROR;
      sqlite3SetString(pzErrMsg, db, "output file already exists");
      goto end_of_vacuum;
    }
    db->mDbFlags |= DBFLAG_VacuumInto;
  }
  nRes = sqlite3BtreeGetRequestedReserve(pMain);

  sqlite3BtreeSetCacheSize(pTemp, db->aDb[iDb].pSchema->cache_size);
  sqlite3BtreeSetSpillSize(pTemp, sqlite3BtreeSetSpillSize(pMain,0));
  sqlite3BtreeSetPagerFlags(pTemp, PAGER_SYNCHRONOUS_OFF|PAGER_CACHESPILL);

  /* Begin a transaction and take an exclusive lock on the main database
  ** file. This is done before the sqlite3BtreeGetPageSize(pMain) call below,
  ** to ensure that we do not try to change the page-size on a WAL database.
  */
  rc = execSql(db, pzErrMsg, "BEGIN");
  if( rc!=SQLITE_OK ) goto end_of_vacuum;
  rc = sqlite3BtreeBeginTrans(pMain, pOut==0 ? 2 : 0, 0);
  if( rc!=SQLITE_OK ) goto end_of_vacuum;

  /* Do not attempt to change the page size for a WAL database */
  if( sqlite3PagerGetJournalMode(sqlite3BtreePager(pMain))
                                               ==PAGER_JOURNALMODE_WAL ){
    db->nextPagesize = 0;
  }

  if( sqlite3BtreeSetPageSize(pTemp, sqlite3BtreeGetPageSize(pMain), nRes, 0)
   || (!isMemDb && sqlite3BtreeSetPageSize(pTemp, db->nextPagesize, nRes, 0))
   || NEVER(db->mallocFailed)
  ){
    rc = SQLITE_NOMEM_BKPT;
    goto end_of_vacuum;
  }

#ifndef SQLITE_OMIT_AUTOVACUUM
  sqlite3BtreeSetAutoVacuum(pTemp, db->nextAutovac>=0 ? db->nextAutovac :
                                           sqlite3BtreeGetAutoVacuum(pMain));
#endif

  /* Query the schema of the main database. Create a mirror schema
  ** in the temporary database.
  */
  db->init.iDb = nDb; /* force new CREATE statements into vacuum_db */
  rc = execSqlF(db, pzErrMsg,
      "SELECT sql FROM \"%w\".sqlite_schema"
      " WHERE type='table'AND name<>'sqlite_sequence'"
      " AND coalesce(rootpage,1)>0",
      zDbMain
  );
  if( rc!=SQLITE_OK ) goto end_of_vacuum;
  rc = execSqlF(db, pzErrMsg,
      "SELECT sql FROM \"%w\".sqlite_schema"
      " WHERE type='index'",
      zDbMain
  );
  if( rc!=SQLITE_OK ) goto end_of_vacuum;
  db->init.iDb = 0;

  /* Loop through the tables in the main database. For each, do
  ** an "INSERT INTO vacuum_db.xxx SELECT * FROM main.xxx;" to copy
  ** the contents to the temporary database.
  */
  rc = execSqlF(db, pzErrMsg,
      "SELECT'INSERT INTO vacuum_db.'||quote(name)"
      "||' SELECT*FROM\"%w\".'||quote(name)"
      "FROM vacuum_db.sqlite_schema "
      "WHERE type='table'AND coalesce(rootpage,1)>0",
      zDbMain
  );
  assert( (db->mDbFlags & DBFLAG_Vacuum)!=0 );
  db->mDbFlags &= ~DBFLAG_Vacuum;
  if( rc!=SQLITE_OK ) goto end_of_vacuum;

  /* Copy the triggers, views, and virtual tables from the main database
  ** over to the temporary database.  None of these objects has any
  ** associated storage, so all we have to do is copy their entries
  ** from the schema table.
  */
  rc = execSqlF(db, pzErrMsg,
      "INSERT INTO vacuum_db.sqlite_schema"
      " SELECT*FROM \"%w\".sqlite_schema"
      " WHERE type IN('view','trigger')"
      " OR(type='table'AND rootpage=0)",
      zDbMain
  );
  if( rc ) goto end_of_vacuum;

  /* At this point, there is a write transaction open on both the
  ** vacuum database and the main database. Assuming no error occurs,
  ** both transactions are closed by this block - the main database
  ** transaction by sqlite3BtreeCopyFile() and the other by an explicit
  ** call to sqlite3BtreeCommit().
  */
  {
    u32 meta;
    int i;

    /* This array determines which meta meta values are preserved in the
    ** vacuum.  Even entries are the meta value number and odd entries
    ** are an increment to apply to the meta value after the vacuum.
    ** The increment is used to increase the schema cookie so that other
    ** connections to the same database will know to reread the schema.
    */
    static const unsigned char aCopy[] = {
       BTREE_SCHEMA_VERSION,     1,  /* Add one to the old schema cookie */
       BTREE_DEFAULT_CACHE_SIZE, 0,  /* Preserve the default page cache size */
       BTREE_TEXT_ENCODING,      0,  /* Preserve the text encoding */
       BTREE_USER_VERSION,       0,  /* Preserve the user version */
       BTREE_APPLICATION_ID,     0,  /* Preserve the application id */
    };

    assert( 1==sqlite3BtreeIsInTrans(pTemp) );
    assert( pOut!=0 || 1==sqlite3BtreeIsInTrans(pMain) );

    /* Copy Btree meta values */
    for(i=0; i<ArraySize(aCopy); i+=2){
      /* GetMeta() and UpdateMeta() cannot fail in this context because
      ** we already have page 1 loaded into cache and marked dirty. */
      sqlite3BtreeGetMeta(pMain, aCopy[i], &meta);
      rc = sqlite3BtreeUpdateMeta(pTemp, aCopy[i], meta+aCopy[i+1]);
      if( NEVER(rc!=SQLITE_OK) ) goto end_of_vacuum;
    }

    if( pOut==0 ){
      rc = sqlite3BtreeCopyFile(pMain, pTemp);
    }
    if( rc!=SQLITE_OK ) goto end_of_vacuum;
    rc = sqlite3BtreeCommit(pTemp);
    if( rc!=SQLITE_OK ) goto end_of_vacuum;
#ifndef SQLITE_OMIT_AUTOVACUUM
    if( pOut==0 ){
      sqlite3BtreeSetAutoVacuum(pMain, sqlite3BtreeGetAutoVacuum(pTemp));
    }
#endif
  }

  assert( rc==SQLITE_OK );
  if( pOut==0 ){
    rc = sqlite3BtreeSetPageSize(pMain, sqlite3BtreeGetPageSize(pTemp), nRes,1);
  }

end_of_vacuum:
  /* Restore the original value of db->flags */
  db->init.iDb = 0;
  db->mDbFlags = saved_mDbFlags;
  db->flags = saved_flags;
  db->nChange = saved_nChange;
  db->nTotalChange = saved_nTotalChange;
  db->mTrace = saved_mTrace;
  sqlite3BtreeSetPageSize(pMain, -1, 0, 1);

  /* Currently there is an SQL level transaction open on the vacuum
  ** database. No locks are held on any other files (since the main file
  ** was committed at the btree level). So it safe to end the transaction
  ** by manually setting the autoCommit flag to true and detaching the
  ** vacuum database. The vacuum_db journal file is deleted when the pager
  ** is closed by the DETACH.
  */
  db->autoCommit = 1;

  if( pDb ){
    sqlite3BtreeClose(pDb->pBt);
    pDb->pBt = 0;
    pDb->pSchema = 0;
  }

  /* This both clears the schemas and reduces the size of the db->aDb[]
  ** array. */
  sqlite3ResetAllSchemasOfConnection(db);

  return rc;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %rsi, %rbx
movq %rdi, %r12
cmpb $0x0, 0x5f(%rsi)
je 0x2a7d0c
cmpl $0x2, 0xc0(%rbx)
jl 0x2a7d15
leaq 0x138529(%rip), %rdx    # 0x3e0233
jmp 0x2a7d39
leaq 0x1384f8(%rip), %rdx    # 0x3e020b
jmp 0x2a7d39
movl %edx, %ebp
movl 0x4c(%rbx), %esi
testq %rcx, %rcx
je 0x2a7d4f
movl 0x8(%rcx), %eax
movabsq $0x4000400040004, %rdx  # imm = 0x4000400040004
btq %rax, %rdx
jb 0x2a7d60
leaq 0x138525(%rip), %rdx    # 0x3e025e
movq %r12, %rdi
movq %rbx, %rsi
callq 0x2aebf6
movl $0x1, %r12d
jmp 0x2a7ff6
movl %esi, (%rsp)
movq %rcx, 0x40(%rsp)
leaq 0x12bb32(%rip), %rcx    # 0x3d3890
jmp 0x2a7d84
movl %esi, (%rsp)
movq %rcx, 0x40(%rsp)
movq %rcx, %rdi
movl $0x1, %esi
callq 0x281c96
movq %rax, %rcx
movl 0x4c(%rbx), %eax
andl $-0x8, %eax
orl $0x6, %eax
movl %eax, 0x4c(%rbx)
movq 0x30(%rbx), %rax
movl 0x2c(%rbx), %esi
movl 0x74(%rbx), %edx
movl %edx, 0x38(%rsp)
movl 0x78(%rbx), %edx
movl %edx, 0x34(%rsp)
movb 0x68(%rbx), %dl
movb %dl, 0xf(%rsp)
movq %rax, 0x60(%rsp)
orq $0x201, %rax            # imm = 0x201
movl %esi, 0x30(%rsp)
movl %esi, %edx
orl $0x6, %edx
movl %edx, 0x2c(%rbx)
movabsq $-0x110005001, %rdx     # imm = 0xFFFFFFFEEFFFAFFF
andq %rax, %rdx
movq %rdx, 0x30(%rbx)
movb $0x0, 0x68(%rbx)
movq 0x20(%rbx), %rax
movslq %ebp, %rbp
shlq $0x5, %rbp
movq (%rax,%rbp), %r15
movq 0x8(%rax,%rbp), %rax
movq %rax, 0x18(%rsp)
movq 0x8(%rax), %rax
movq (%rax), %rax
movb 0x10(%rax), %r14b
movslq 0x28(%rbx), %r13
leaq 0x138475(%rip), %rdx    # 0x3e0270
movq %rbx, %rdi
movq %r12, %rsi
xorl %eax, %eax
callq 0x2b10d5
movq %r12, %rcx
movl %eax, %r12d
movl (%rsp), %eax
movl %eax, 0x4c(%rbx)
testl %r12d, %r12d
je 0x2a7e26
xorl %r14d, %r14d
movq 0x18(%rsp), %rbp
jmp 0x2a7f92
movq %rcx, 0x28(%rsp)
movq %r15, 0x58(%rsp)
movq 0x20(%rbx), %rax
movq %r13, 0x50(%rsp)
shlq $0x5, %r13
leaq (%rax,%r13), %rdx
movq %rdx, (%rsp)
movq 0x8(%rax,%r13), %r13
movq 0x40(%rsp), %r15
testq %r15, %r15
je 0x2a7ebd
movq %r13, 0x10(%rsp)
movq 0x8(%r13), %rax
movq (%rax), %rax
movq 0x48(%rax), %rdi
movq $0x0, 0x48(%rsp)
movq (%rdi), %rax
testq %rax, %rax
je 0x2a7e89
leaq 0x48(%rsp), %rsi
callq *0x30(%rax)
testl %eax, %eax
jne 0x2a7e94
cmpq $0x0, 0x48(%rsp)
jg 0x2a7e94
orb $0x8, 0x2c(%rbx)
movb $0x1, %al
xorl %r12d, %r12d
jmp 0x2a7eb0
leaq 0x1383ec(%rip), %rdx    # 0x3e0287
movq 0x28(%rsp), %rdi
movq %rbx, %rsi
callq 0x2aebf6
movl $0x1, %r12d
xorl %eax, %eax
testb %al, %al
movq 0x10(%rsp), %r13
je 0x2a7f89
movq 0x18(%rsp), %rdx
movq (%rdx), %rax
movq 0x8(%rdx), %rcx
movq %rax, 0x8(%rcx)
movl 0x34(%rcx), %esi
subl 0x38(%rcx), %esi
movzbl 0x26(%rcx), %eax
cmpl %eax, %esi
cmovlel %eax, %esi
movl %esi, 0x24(%rsp)
movq 0x20(%rbx), %rax
movq 0x18(%rax,%rbp), %rax
movq %rdx, %rbp
movl 0x74(%rax), %esi
movq %r13, %rdi
callq 0x2aec28
movq (%rbp), %rax
movq 0x8(%rbp), %rcx
movq %rax, 0x8(%rcx)
movq (%rcx), %rax
movq 0x118(%rax), %rcx
movslq 0x1c(%rcx), %rax
testq %rax, %rax
jns 0x2a7f2d
shlq $0xa, %rax
negq %rax
movslq 0x24(%rcx), %rdx
movslq 0x28(%rcx), %rsi
addq %rdx, %rsi
cqto
idivq %rsi
movl 0x20(%rcx), %esi
cmpl %esi, %eax
cmovgl %eax, %esi
movq %r13, %rdi
callq 0x2b1199
movq %r13, %rdi
movl $0x21, %esi
callq 0x2b11d5
leaq 0x138351(%rip), %rdx    # 0x3e02a2
movq %rbx, %rdi
movq 0x28(%rsp), %rsi
callq 0x2b1268
testl %eax, %eax
jne 0x2a7f84
movq %r13, 0x10(%rsp)
xorl %esi, %esi
testq %r15, %r15
sete %sil
addl %esi, %esi
movq %rbp, %rdi
xorl %edx, %edx
callq 0x28036f
testl %eax, %eax
je 0x2a8008
movl %eax, %r12d
jmp 0x2a7f8e
movq 0x18(%rsp), %rbp
movq (%rsp), %r14
movb $0x0, 0xb4(%rbx)
movl 0x30(%rsp), %eax
movl %eax, 0x2c(%rbx)
movq 0x60(%rsp), %rax
movq %rax, 0x30(%rbx)
movl 0x38(%rsp), %eax
movl %eax, 0x74(%rbx)
movl 0x34(%rsp), %eax
movl %eax, 0x78(%rbx)
movb 0xf(%rsp), %al
movb %al, 0x68(%rbx)
movq %rbp, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
xorl %edx, %edx
movl $0x1, %ecx
callq 0x28b2a9
movb $0x1, 0x5f(%rbx)
testq %r14, %r14
je 0x2a7fee
movq 0x8(%r14), %rdi
callq 0x29ac32
xorl %eax, %eax
movq %rax, 0x8(%r14)
movq %rax, 0x18(%r14)
movq %rbx, %rdi
callq 0x280d0f
movl %r12d, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x8(%rbp), %rax
movq (%rax), %rcx
cmpb $0x5, 0x9(%rcx)
jne 0x2a801c
movl $0x0, 0x6c(%rbx)
movl 0x34(%rax), %esi
movq 0x10(%rsp), %rdi
movl 0x24(%rsp), %r13d
movl %r13d, %edx
xorl %ecx, %ecx
callq 0x28b2a9
movl $0x7, %r12d
testl %eax, %eax
jne 0x2a7f8e
testb %r14b, %r14b
je 0x2a8087
cmpb $0x0, 0x61(%rbx)
jne 0x2a7f8e
movsbl 0x64(%rbx), %esi
testl %esi, %esi
movq 0x10(%rsp), %rdi
movq 0x58(%rsp), %r14
movq 0x50(%rsp), %r12
jns 0x2a80a5
movq (%rbp), %rcx
movq 0x8(%rbp), %rax
movq %rcx, 0x8(%rax)
cmpb $0x0, 0x21(%rax)
je 0x2a80a3
cmpb $0x1, 0x22(%rax)
movl $0x2, %esi
sbbl $0x0, %esi
jmp 0x2a80a5
movl 0x6c(%rbx), %esi
movq 0x10(%rsp), %rdi
movl %r13d, %edx
xorl %ecx, %ecx
callq 0x28b2a9
testl %eax, %eax
jne 0x2a7f8e
jmp 0x2a8046
xorl %esi, %esi
callq 0x2b1365
movb %r12b, 0xb4(%rbx)
leaq 0x1381f0(%rip), %rdx    # 0x3e02a8
movq %rbx, %rdi
movq 0x28(%rsp), %r13
movq %r13, %rsi
movq %r14, %rcx
xorl %eax, %eax
callq 0x2b10d5
testl %eax, %eax
jne 0x2a7f84
leaq 0x138238(%rip), %rdx    # 0x3e0314
movq %rbx, %rdi
movq %r13, %rsi
movq %r14, %rcx
xorl %eax, %eax
callq 0x2b10d5
testl %eax, %eax
jne 0x2a7f84
movb $0x0, 0xb4(%rbx)
leaq 0x138248(%rip), %rdx    # 0x3e034a
movq %rbx, %rdi
movq %r13, %rsi
movq %r14, %rcx
xorl %eax, %eax
callq 0x2b10d5
andb $-0x5, 0x2c(%rbx)
testl %eax, %eax
jne 0x2a7f84
leaq 0x1382bc(%rip), %rdx    # 0x3e03e1
movq %rbx, %rdi
movq %r13, %rsi
movq %r14, %rcx
xorl %eax, %eax
callq 0x2b10d5
testl %eax, %eax
je 0x2a8145
movl %eax, %r12d
movq (%rsp), %r14
jmp 0x2a7e1c
movq $-0x2, %r14
leaq 0x133531(%rip), %r13    # 0x3db684
movzbl 0x2(%r14,%r13), %r12d
movq 0x18(%rsp), %rdi
movl %r12d, %esi
leaq 0x3c(%rsp), %rdx
callq 0x2a4c31
movzbl 0x3(%r14,%r13), %edx
addl 0x3c(%rsp), %edx
movq 0x10(%rsp), %rdi
movl %r12d, %esi
callq 0x280cc4
movb $0x1, %bpl
testl %eax, %eax
jne 0x2a8207
addq $0x2, %r14
cmpq $0x7, %r14
jbe 0x2a8153
xorl %r12d, %r12d
testq %r15, %r15
jne 0x2a81ad
movq 0x18(%rsp), %rdi
movq 0x10(%rsp), %rsi
callq 0x2b139a
movl %eax, %r12d
testl %r12d, %r12d
jne 0x2a820a
movq 0x10(%rsp), %rdi
callq 0x2aec70
movl %eax, %r12d
testl %eax, %eax
setne %bpl
testq %r15, %r15
jne 0x2a820a
testl %r12d, %r12d
jne 0x2a820a
movq 0x10(%rsp), %rax
movq (%rax), %rcx
movq 0x8(%rax), %rax
movq %rcx, 0x8(%rax)
xorl %r12d, %r12d
cmpb $0x0, 0x21(%rax)
movl $0x0, %esi
je 0x2a81f9
cmpb $0x1, 0x22(%rax)
movl $0x2, %esi
sbbl $0x0, %esi
movq 0x18(%rsp), %rdi
callq 0x2b1365
xorl %ebp, %ebp
jmp 0x2a820a
movl %eax, %r12d
testq %r15, %r15
jne 0x2a813c
testb %bpl, %bpl
jne 0x2a813c
movq 0x10(%rsp), %rax
movq 0x8(%rax), %rax
movl 0x34(%rax), %esi
movq 0x18(%rsp), %rbp
movq %rbp, %rdi
movl 0x24(%rsp), %edx
movl $0x1, %ecx
callq 0x28b2a9
jmp 0x2a7f84