// File: /workspace/github_c_makefile_star2_O0/ukontainer[P]sqlite-bench/sqlite3.c
// Function: sqlite3RunVacuum
SQLITE_PRIVATE int sqlite3RunVacuum(char **pzErrMsg, sqlite3 *db, int iDb){
  int rc = SQLITE_OK;     /* Return code from service routines */
  Btree *pMain;           /* The database being vacuumed */
  Btree *pTemp;           /* The temporary database we vacuum into */
  u16 saved_mDbFlags;     /* Saved value of db->mDbFlags */
  u32 saved_flags;        /* Saved value of db->flags */
  int saved_nChange;      /* Saved value of db->nChange */
  int saved_nTotalChange; /* Saved value of db->nTotalChange */
  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 */

  if( !db->autoCommit ){
    sqlite3SetString(pzErrMsg, db, "cannot VACUUM from within a transaction");
    return SQLITE_ERROR;
  }
  if( db->nVdbeActive>1 ){
    sqlite3SetString(pzErrMsg, db,"cannot VACUUM - SQL statements in progress");
    return SQLITE_ERROR;
  }

  /* 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 &= ~(SQLITE_ForeignKeys | SQLITE_ReverseOrder | 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 = execSql(db, pzErrMsg, "ATTACH''AS vacuum_db");
  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;

  /* The call to execSql() to attach the temp database has left the file
  ** locked (as there was more than one active statement when the transaction
  ** to read the schema was concluded. Unlock it here so that this doesn't
  ** cause problems for the call to BtreeSetPageSize() below.  */
  sqlite3BtreeCommit(pTemp);

  nRes = sqlite3BtreeGetOptimalReserve(pMain);

  /* A VACUUM cannot change the pagesize of an encrypted database. */
#ifdef SQLITE_HAS_CODEC
  if( db->nextPagesize ){
    extern void sqlite3CodecGetKey(sqlite3*, int, void**, int*);
    int nKey;
    char *zKey;
    sqlite3CodecGetKey(db, iDb, (void**)&zKey, &nKey);
    if( nKey ) db->nextPagesize = 0;
  }
#endif

  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, 2, 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_master"
      " 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_master"
      " 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_master "
      "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 SQLITE_MASTER table.
  */
  rc = execSqlF(db, pzErrMsg,
      "INSERT INTO vacuum_db.sqlite_master"
      " SELECT*FROM \"%w\".sqlite_master"
      " 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( 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;
    }

    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
    sqlite3BtreeSetAutoVacuum(pMain, sqlite3BtreeGetAutoVacuum(pTemp));
#endif
  }

  assert( rc==SQLITE_OK );
  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, -1, 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
movq %rsp, %rbp
subq $0xa0, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl %edx, -0x1c(%rbp)
movl $0x0, -0x20(%rbp)
movq $0x0, -0x50(%rbp)
movq -0x18(%rbp), %rax
cmpb $0x0, 0x5f(%rax)
jne 0x44385f
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
leaq 0x13bc23(%rip), %rdx    # 0x57f471
callq 0x44ec00
movl $0x1, -0x4(%rbp)
jmp 0x443d48
movq -0x18(%rbp), %rax
cmpl $0x1, 0xb8(%rax)
jle 0x44388c
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
leaq 0x13bc1e(%rip), %rdx    # 0x57f499
callq 0x44ec00
movl $0x1, -0x4(%rbp)
jmp 0x443d48
movq -0x18(%rbp), %rax
movl 0x30(%rax), %eax
movl %eax, -0x38(%rbp)
movq -0x18(%rbp), %rax
movl 0x2c(%rax), %eax
movw %ax, -0x32(%rbp)
movq -0x18(%rbp), %rax
movl 0x74(%rax), %eax
movl %eax, -0x3c(%rbp)
movq -0x18(%rbp), %rax
movl 0x78(%rax), %eax
movl %eax, -0x40(%rbp)
movq -0x18(%rbp), %rax
movb 0x68(%rax), %al
movb %al, -0x41(%rbp)
movq -0x18(%rbp), %rax
movl 0x30(%rax), %ecx
orl $0x201, %ecx            # imm = 0x201
movl %ecx, 0x30(%rax)
movq -0x18(%rbp), %rax
movl 0x2c(%rax), %ecx
orl $0x6, %ecx
movl %ecx, 0x2c(%rax)
movq -0x18(%rbp), %rax
movl 0x30(%rax), %ecx
andl $0xffffaf7f, %ecx       # imm = 0xFFFFAF7F
movl %ecx, 0x30(%rax)
movq -0x18(%rbp), %rax
movb $0x0, 0x68(%rax)
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rax
movslq -0x1c(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movq (%rax), %rax
movq %rax, -0x68(%rbp)
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rax
movslq -0x1c(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rax
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rdi
callq 0x405570
movq %rax, %rdi
callq 0x42cf10
movl %eax, -0x54(%rbp)
movq -0x18(%rbp), %rax
movl 0x28(%rax), %eax
movl %eax, -0x5c(%rbp)
movq -0x18(%rbp), %rdi
movq -0x10(%rbp), %rsi
leaq 0x13bb6e(%rip), %rdx    # 0x57f4c4
callq 0x4519a0
movl %eax, -0x20(%rbp)
cmpl $0x0, -0x20(%rbp)
je 0x443969
jmp 0x443cb2
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rax
movslq -0x5c(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rdi
callq 0x44eca0
movq -0x28(%rbp), %rdi
callq 0x451ac0
movl %eax, -0x58(%rbp)
movq -0x30(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rax
movslq -0x1c(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movq 0x18(%rax), %rax
movl 0x74(%rax), %esi
callq 0x44ec50
movq -0x30(%rbp), %rax
movq %rax, -0x78(%rbp)
movq -0x28(%rbp), %rdi
xorl %esi, %esi
callq 0x451b00
movq -0x78(%rbp), %rdi
movl %eax, %esi
callq 0x451b00
movq -0x30(%rbp), %rdi
movl $0x21, %esi
callq 0x451b50
movq -0x18(%rbp), %rdi
movq -0x10(%rbp), %rsi
leaq 0x13bada(%rip), %rdx    # 0x57f4d9
callq 0x4519a0
movl %eax, -0x20(%rbp)
cmpl $0x0, -0x20(%rbp)
je 0x443a12
jmp 0x443cb2
movq -0x28(%rbp), %rdi
movl $0x2, %esi
xorl %eax, %eax
movl %eax, %edx
callq 0x40b3f0
movl %eax, -0x20(%rbp)
cmpl $0x0, -0x20(%rbp)
je 0x443a32
jmp 0x443cb2
movq -0x28(%rbp), %rdi
callq 0x405570
movq %rax, %rdi
callq 0x40b8b0
cmpl $0x5, %eax
jne 0x443a53
movq -0x18(%rbp), %rax
movl $0x0, 0x6c(%rax)
movq -0x30(%rbp), %rax
movq %rax, -0x80(%rbp)
movq -0x28(%rbp), %rdi
callq 0x40b890
movq -0x80(%rbp), %rdi
movl %eax, %esi
movl -0x58(%rbp), %edx
xorl %ecx, %ecx
callq 0x41a7a0
cmpl $0x0, %eax
jne 0x443aa6
cmpl $0x0, -0x54(%rbp)
jne 0x443a99
movq -0x30(%rbp), %rdi
movq -0x18(%rbp), %rax
movl 0x6c(%rax), %esi
movl -0x58(%rbp), %edx
xorl %ecx, %ecx
callq 0x41a7a0
cmpl $0x0, %eax
jne 0x443aa6
movq -0x18(%rbp), %rax
movzbl 0x61(%rax), %eax
cmpl $0x0, %eax
je 0x443ab2
movl $0x7, -0x20(%rbp)
jmp 0x443cb2
movq -0x30(%rbp), %rax
movq %rax, -0x88(%rbp)
movq -0x18(%rbp), %rax
movsbl 0x64(%rax), %eax
cmpl $0x0, %eax
jl 0x443ada
movq -0x18(%rbp), %rax
movsbl 0x64(%rax), %eax
movl %eax, -0x8c(%rbp)
jmp 0x443ae9
movq -0x28(%rbp), %rdi
callq 0x451c50
movl %eax, -0x8c(%rbp)
movq -0x88(%rbp), %rdi
movl -0x8c(%rbp), %esi
callq 0x451ba0
movl -0x5c(%rbp), %eax
movb %al, %cl
movq -0x18(%rbp), %rax
movb %cl, 0xb4(%rax)
movq -0x18(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x68(%rbp), %rcx
leaq 0x13b9c2(%rip), %rdx    # 0x57f4df
movb $0x0, %al
callq 0x451cc0
movl %eax, -0x20(%rbp)
cmpl $0x0, -0x20(%rbp)
je 0x443b32
jmp 0x443cb2
movq -0x18(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x68(%rbp), %rcx
leaq 0x13ba06(%rip), %rdx    # 0x57f54b
movb $0x0, %al
callq 0x451cc0
movl %eax, -0x20(%rbp)
cmpl $0x0, -0x20(%rbp)
je 0x443b5a
jmp 0x443cb2
movq -0x18(%rbp), %rax
movb $0x0, 0xb4(%rax)
movq -0x18(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x68(%rbp), %rcx
leaq 0x13ba09(%rip), %rdx    # 0x57f581
movb $0x0, %al
callq 0x451cc0
movl %eax, -0x20(%rbp)
movq -0x18(%rbp), %rax
movl 0x2c(%rax), %ecx
andl $-0x5, %ecx
movl %ecx, 0x2c(%rax)
cmpl $0x0, -0x20(%rbp)
je 0x443b9a
jmp 0x443cb2
movq -0x18(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x68(%rbp), %rcx
leaq 0x13ba6b(%rip), %rdx    # 0x57f618
movb $0x0, %al
callq 0x451cc0
movl %eax, -0x20(%rbp)
cmpl $0x0, -0x20(%rbp)
je 0x443bc2
jmp 0x443cb2
movl $0x0, -0x70(%rbp)
cmpl $0xa, -0x70(%rbp)
jge 0x443c37
movq -0x28(%rbp), %rdi
movslq -0x70(%rbp), %rcx
leaq 0x136e3a(%rip), %rax    # 0x57aa18
movzbl (%rax,%rcx), %esi
leaq -0x6c(%rbp), %rdx
callq 0x43f5b0
movq -0x30(%rbp), %rdi
movslq -0x70(%rbp), %rcx
leaq 0x136e1e(%rip), %rax    # 0x57aa18
movzbl (%rax,%rcx), %esi
movl -0x6c(%rbp), %edx
movl -0x70(%rbp), %eax
addl $0x1, %eax
movslq %eax, %rcx
leaq 0x136e07(%rip), %rax    # 0x57aa18
movzbl (%rax,%rcx), %eax
addl %eax, %edx
callq 0x40bc50
movl %eax, -0x20(%rbp)
cmpl $0x0, -0x20(%rbp)
je 0x443c2a
jmp 0x443cb2
jmp 0x443c2c
movl -0x70(%rbp), %eax
addl $0x2, %eax
movl %eax, -0x70(%rbp)
jmp 0x443bc9
movq -0x28(%rbp), %rdi
movq -0x30(%rbp), %rsi
callq 0x451da0
movl %eax, -0x20(%rbp)
cmpl $0x0, -0x20(%rbp)
je 0x443c4f
jmp 0x443cb2
movq -0x30(%rbp), %rdi
callq 0x44eca0
movl %eax, -0x20(%rbp)
cmpl $0x0, -0x20(%rbp)
je 0x443c63
jmp 0x443cb2
movq -0x28(%rbp), %rax
movq %rax, -0xa0(%rbp)
movq -0x30(%rbp), %rdi
callq 0x451c50
movq -0xa0(%rbp), %rdi
movl %eax, %esi
callq 0x451ba0
movq -0x28(%rbp), %rax
movq %rax, -0x98(%rbp)
movq -0x30(%rbp), %rdi
callq 0x40b890
movq -0x98(%rbp), %rdi
movl %eax, %esi
movl -0x58(%rbp), %edx
movl $0x1, %ecx
callq 0x41a7a0
movl %eax, -0x20(%rbp)
movq -0x18(%rbp), %rax
movb $0x0, 0xb4(%rax)
movzwl -0x32(%rbp), %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0x2c(%rax)
movl -0x38(%rbp), %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0x30(%rax)
movl -0x3c(%rbp), %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0x74(%rax)
movl -0x40(%rbp), %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0x78(%rax)
movb -0x41(%rbp), %cl
movq -0x18(%rbp), %rax
movb %cl, 0x68(%rax)
movq -0x28(%rbp), %rdi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
movl $0x1, %ecx
movl %edx, %esi
callq 0x41a7a0
movq -0x18(%rbp), %rax
movb $0x1, 0x5f(%rax)
cmpq $0x0, -0x50(%rbp)
je 0x443d39
movq -0x50(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x431e90
movq -0x50(%rbp), %rax
movq $0x0, 0x8(%rax)
movq -0x50(%rbp), %rax
movq $0x0, 0x18(%rax)
movq -0x18(%rbp), %rdi
callq 0x40bcf0
movl -0x20(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0xa0, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)