// File: /workspace/github_c_makefile_star2_O0/barabo[P]advanced-shell-history/src/sqlite3.c
// Function: vdbeCommit
static int vdbeCommit(sqlite3 *db, Vdbe *p){
  int i;
  int nTrans = 0;  /* Number of databases with an active write-transaction */
  int rc = SQLITE_OK;
  int needXcommit = 0;

#ifdef SQLITE_OMIT_VIRTUALTABLE
  /* With this option, sqlite3VtabSync() is defined to be simply 
  ** SQLITE_OK so p is not used. 
  */
  UNUSED_PARAMETER(p);
#endif

  /* Before doing anything else, call the xSync() callback for any
  ** virtual module tables written in this transaction. This has to
  ** be done before determining whether a master journal file is 
  ** required, as an xSync() callback may add an attached database
  ** to the transaction.
  */
  rc = sqlite3VtabSync(db, p);

  /* This loop determines (a) if the commit hook should be invoked and
  ** (b) how many database files have open write transactions, not 
  ** including the temp database. (b) is important because if more than 
  ** one database file has an open write transaction, a master journal
  ** file is required for an atomic commit.
  */ 
  for(i=0; rc==SQLITE_OK && i<db->nDb; i++){ 
    Btree *pBt = db->aDb[i].pBt;
    if( sqlite3BtreeIsInTrans(pBt) ){
      needXcommit = 1;
      if( i!=1 ) nTrans++;
      sqlite3BtreeEnter(pBt);
      rc = sqlite3PagerExclusiveLock(sqlite3BtreePager(pBt));
      sqlite3BtreeLeave(pBt);
    }
  }
  if( rc!=SQLITE_OK ){
    return rc;
  }

  /* If there are any write-transactions at all, invoke the commit hook */
  if( needXcommit && db->xCommitCallback ){
    rc = db->xCommitCallback(db->pCommitArg);
    if( rc ){
      return SQLITE_CONSTRAINT_COMMITHOOK;
    }
  }

  /* The simple case - no more than one database file (not counting the
  ** TEMP database) has a transaction active.   There is no need for the
  ** master-journal.
  **
  ** If the return value of sqlite3BtreeGetFilename() is a zero length
  ** string, it means the main database is :memory: or a temp file.  In 
  ** that case we do not support atomic multi-file commits, so use the 
  ** simple case then too.
  */
  if( 0==sqlite3Strlen30(sqlite3BtreeGetFilename(db->aDb[0].pBt))
   || nTrans<=1
  ){
    for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
      Btree *pBt = db->aDb[i].pBt;
      if( pBt ){
        rc = sqlite3BtreeCommitPhaseOne(pBt, 0);
      }
    }

    /* Do the commit only if all databases successfully complete phase 1. 
    ** If one of the BtreeCommitPhaseOne() calls fails, this indicates an
    ** IO error while deleting or truncating a journal file. It is unlikely,
    ** but could happen. In this case abandon processing and return the error.
    */
    for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
      Btree *pBt = db->aDb[i].pBt;
      if( pBt ){
        rc = sqlite3BtreeCommitPhaseTwo(pBt, 0);
      }
    }
    if( rc==SQLITE_OK ){
      sqlite3VtabCommit(db);
    }
  }

  /* The complex case - There is a multi-file write-transaction active.
  ** This requires a master journal file to ensure the transaction is
  ** committed atomicly.
  */
#ifndef SQLITE_OMIT_DISKIO
  else{
    sqlite3_vfs *pVfs = db->pVfs;
    int needSync = 0;
    char *zMaster = 0;   /* File-name for the master journal */
    char const *zMainFile = sqlite3BtreeGetFilename(db->aDb[0].pBt);
    sqlite3_file *pMaster = 0;
    i64 offset = 0;
    int res;
    int retryCount = 0;
    int nMainFile;

    /* Select a master journal file name */
    nMainFile = sqlite3Strlen30(zMainFile);
    zMaster = sqlite3MPrintf(db, "%s-mjXXXXXX9XXz", zMainFile);
    if( zMaster==0 ) return SQLITE_NOMEM;
    do {
      u32 iRandom;
      if( retryCount ){
        if( retryCount>100 ){
          sqlite3_log(SQLITE_FULL, "MJ delete: %s", zMaster);
          sqlite3OsDelete(pVfs, zMaster, 0);
          break;
        }else if( retryCount==1 ){
          sqlite3_log(SQLITE_FULL, "MJ collide: %s", zMaster);
        }
      }
      retryCount++;
      sqlite3_randomness(sizeof(iRandom), &iRandom);
      sqlite3_snprintf(13, &zMaster[nMainFile], "-mj%06X9%02X",
                               (iRandom>>8)&0xffffff, iRandom&0xff);
      /* The antipenultimate character of the master journal name must
      ** be "9" to avoid name collisions when using 8+3 filenames. */
      assert( zMaster[sqlite3Strlen30(zMaster)-3]=='9' );
      sqlite3FileSuffix3(zMainFile, zMaster);
      rc = sqlite3OsAccess(pVfs, zMaster, SQLITE_ACCESS_EXISTS, &res);
    }while( rc==SQLITE_OK && res );
    if( rc==SQLITE_OK ){
      /* Open the master journal. */
      rc = sqlite3OsOpenMalloc(pVfs, zMaster, &pMaster, 
          SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|
          SQLITE_OPEN_EXCLUSIVE|SQLITE_OPEN_MASTER_JOURNAL, 0
      );
    }
    if( rc!=SQLITE_OK ){
      sqlite3DbFree(db, zMaster);
      return rc;
    }
 
    /* Write the name of each database file in the transaction into the new
    ** master journal file. If an error occurs at this point close
    ** and delete the master journal file. All the individual journal files
    ** still have 'null' as the master journal pointer, so they will roll
    ** back independently if a failure occurs.
    */
    for(i=0; i<db->nDb; i++){
      Btree *pBt = db->aDb[i].pBt;
      if( sqlite3BtreeIsInTrans(pBt) ){
        char const *zFile = sqlite3BtreeGetJournalname(pBt);
        if( zFile==0 ){
          continue;  /* Ignore TEMP and :memory: databases */
        }
        assert( zFile[0]!=0 );
        if( !needSync && !sqlite3BtreeSyncDisabled(pBt) ){
          needSync = 1;
        }
        rc = sqlite3OsWrite(pMaster, zFile, sqlite3Strlen30(zFile)+1, offset);
        offset += sqlite3Strlen30(zFile)+1;
        if( rc!=SQLITE_OK ){
          sqlite3OsCloseFree(pMaster);
          sqlite3OsDelete(pVfs, zMaster, 0);
          sqlite3DbFree(db, zMaster);
          return rc;
        }
      }
    }

    /* Sync the master journal file. If the IOCAP_SEQUENTIAL device
    ** flag is set this is not required.
    */
    if( needSync 
     && 0==(sqlite3OsDeviceCharacteristics(pMaster)&SQLITE_IOCAP_SEQUENTIAL)
     && SQLITE_OK!=(rc = sqlite3OsSync(pMaster, SQLITE_SYNC_NORMAL))
    ){
      sqlite3OsCloseFree(pMaster);
      sqlite3OsDelete(pVfs, zMaster, 0);
      sqlite3DbFree(db, zMaster);
      return rc;
    }

    /* Sync all the db files involved in the transaction. The same call
    ** sets the master journal pointer in each individual journal. If
    ** an error occurs here, do not delete the master journal file.
    **
    ** If the error occurs during the first call to
    ** sqlite3BtreeCommitPhaseOne(), then there is a chance that the
    ** master journal file will be orphaned. But we cannot delete it,
    ** in case the master journal file name was written into the journal
    ** file before the failure occurred.
    */
    for(i=0; rc==SQLITE_OK && i<db->nDb; i++){ 
      Btree *pBt = db->aDb[i].pBt;
      if( pBt ){
        rc = sqlite3BtreeCommitPhaseOne(pBt, zMaster);
      }
    }
    sqlite3OsCloseFree(pMaster);
    assert( rc!=SQLITE_BUSY );
    if( rc!=SQLITE_OK ){
      sqlite3DbFree(db, zMaster);
      return rc;
    }

    /* Delete the master journal file. This commits the transaction. After
    ** doing this the directory is synced again before any individual
    ** transaction files are deleted.
    */
    rc = sqlite3OsDelete(pVfs, zMaster, 1);
    sqlite3DbFree(db, zMaster);
    zMaster = 0;
    if( rc ){
      return rc;
    }

    /* All files and directories have already been synced, so the following
    ** calls to sqlite3BtreeCommitPhaseTwo() are only closing files and
    ** deleting or truncating journals. If something goes wrong while
    ** this is happening we don't really care. The integrity of the
    ** transaction is already guaranteed, but some stray 'cold' journals
    ** may be lying around. Returning an error code won't help matters.
    */
    disable_simulated_io_errors();
    sqlite3BeginBenignMalloc();
    for(i=0; i<db->nDb; i++){ 
      Btree *pBt = db->aDb[i].pBt;
      if( pBt ){
        sqlite3BtreeCommitPhaseTwo(pBt, 1);
      }
    }
    sqlite3EndBenignMalloc();
    enable_simulated_io_errors();

    sqlite3VtabCommit(db);
  }
#endif

  return rc;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xc0, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl $0x0, -0x20(%rbp)
movl $0x0, -0x24(%rbp)
movl $0x0, -0x28(%rbp)
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x30420
movl %eax, -0x24(%rbp)
movl $0x0, -0x1c(%rbp)
xorl %eax, %eax
cmpl $0x0, -0x24(%rbp)
movb %al, -0xa1(%rbp)
jne 0x2f250
movl -0x1c(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl 0x28(%rcx), %eax
setl %al
movb %al, -0xa1(%rbp)
movb -0xa1(%rbp), %al
testb $0x1, %al
jne 0x2f25c
jmp 0x2f2c8
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movslq -0x1c(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rdi
callq 0x30220
cmpl $0x0, %eax
je 0x2f2b8
movl $0x1, -0x28(%rbp)
cmpl $0x1, -0x1c(%rbp)
je 0x2f29b
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
movq -0x30(%rbp), %rdi
callq 0xe0f0
movq -0x30(%rbp), %rdi
callq 0x98f0
movq %rax, %rdi
callq 0x24470
movl %eax, -0x24(%rbp)
jmp 0x2f2ba
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0x2f22f
cmpl $0x0, -0x24(%rbp)
je 0x2f2d9
movl -0x24(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x2f8c7
cmpl $0x0, -0x28(%rbp)
je 0x2f31c
movq -0x10(%rbp), %rax
cmpq $0x0, 0xe0(%rax)
je 0x2f31c
movq -0x10(%rbp), %rax
movq 0xe0(%rax), %rax
movq -0x10(%rbp), %rcx
movq 0xd8(%rcx), %rdi
callq *%rax
movl %eax, -0x24(%rbp)
cmpl $0x0, -0x24(%rbp)
je 0x2f31a
movl $0x213, -0x4(%rbp)      # imm = 0x213
jmp 0x2f8c7
jmp 0x2f31c
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movq 0x8(%rax), %rdi
callq 0x1b840
movq %rax, %rdi
callq 0x92f0
movl %eax, %ecx
xorl %eax, %eax
cmpl %ecx, %eax
je 0x2f347
cmpl $0x1, -0x20(%rbp)
jg 0x2f43f
movl $0x0, -0x1c(%rbp)
xorl %eax, %eax
cmpl $0x0, -0x24(%rbp)
movb %al, -0xa2(%rbp)
jne 0x2f36f
movl -0x1c(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl 0x28(%rcx), %eax
setl %al
movb %al, -0xa2(%rbp)
movb -0xa2(%rbp), %al
testb $0x1, %al
jne 0x2f37b
jmp 0x2f3ba
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movslq -0x1c(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rax
movq %rax, -0x38(%rbp)
cmpq $0x0, -0x38(%rbp)
je 0x2f3ad
movq -0x38(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0xf600
movl %eax, -0x24(%rbp)
jmp 0x2f3af
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0x2f34e
movl $0x0, -0x1c(%rbp)
xorl %eax, %eax
cmpl $0x0, -0x24(%rbp)
movb %al, -0xa3(%rbp)
jne 0x2f3e2
movl -0x1c(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl 0x28(%rcx), %eax
setl %al
movb %al, -0xa3(%rbp)
movb -0xa3(%rbp), %al
testb $0x1, %al
jne 0x2f3ee
jmp 0x2f42b
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movslq -0x1c(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rax
movq %rax, -0x40(%rbp)
cmpq $0x0, -0x40(%rbp)
je 0x2f41e
movq -0x40(%rbp), %rdi
xorl %esi, %esi
callq 0xf560
movl %eax, -0x24(%rbp)
jmp 0x2f420
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0x2f3c1
cmpl $0x0, -0x24(%rbp)
jne 0x2f43a
movq -0x10(%rbp), %rdi
callq 0x30500
jmp 0x2f8c1
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x48(%rbp)
movl $0x0, -0x4c(%rbp)
movq $0x0, -0x58(%rbp)
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movq 0x8(%rax), %rdi
callq 0x1b840
movq %rax, -0x60(%rbp)
movq $0x0, -0x68(%rbp)
movq $0x0, -0x70(%rbp)
movl $0x0, -0x78(%rbp)
movq -0x60(%rbp), %rdi
callq 0x92f0
movl %eax, -0x7c(%rbp)
movq -0x10(%rbp), %rdi
movq -0x60(%rbp), %rdx
leaq 0x7eeff(%rip), %rsi     # 0xae39f
movb $0x0, %al
callq 0x137f0
movq %rax, -0x58(%rbp)
cmpq $0x0, -0x58(%rbp)
jne 0x2f4be
movl $0x7, -0x4(%rbp)
jmp 0x2f8c7
jmp 0x2f4c0
cmpl $0x0, -0x78(%rbp)
je 0x2f518
cmpl $0x64, -0x78(%rbp)
jle 0x2f4f7
movq -0x58(%rbp), %rdx
movl $0xd, %edi
leaq 0x7eed3(%rip), %rsi     # 0xae3af
movb $0x0, %al
callq 0xc5f0
movq -0x48(%rbp), %rdi
movq -0x58(%rbp), %rsi
xorl %edx, %edx
callq 0x259d0
jmp 0x2f5a3
cmpl $0x1, -0x78(%rbp)
jne 0x2f514
movq -0x58(%rbp), %rdx
movl $0xd, %edi
leaq 0x7eeb0(%rip), %rsi     # 0xae3bd
movb $0x0, %al
callq 0xc5f0
jmp 0x2f516
jmp 0x2f518
movl -0x78(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x78(%rbp)
movl $0x4, %edi
leaq -0x80(%rbp), %rsi
callq 0xc740
movq -0x58(%rbp), %rsi
movslq -0x7c(%rbp), %rax
addq %rax, %rsi
movl -0x80(%rbp), %ecx
shrl $0x8, %ecx
andl $0xffffff, %ecx         # imm = 0xFFFFFF
movl -0x80(%rbp), %r8d
andl $0xff, %r8d
movl $0xd, %edi
leaq 0x7ee6f(%rip), %rdx     # 0xae3cc
movb $0x0, %al
callq 0xc540
movq -0x48(%rbp), %rdi
movq -0x58(%rbp), %rsi
xorl %edx, %edx
leaq -0x74(%rbp), %rcx
callq 0x28b70
movl %eax, -0x24(%rbp)
xorl %eax, %eax
cmpl $0x0, -0x24(%rbp)
movb %al, -0xa4(%rbp)
jne 0x2f595
cmpl $0x0, -0x74(%rbp)
setne %al
movb %al, -0xa4(%rbp)
movb -0xa4(%rbp), %al
testb $0x1, %al
jne 0x2f4c0
cmpl $0x0, -0x24(%rbp)
jne 0x2f5c7
movq -0x48(%rbp), %rdi
movq -0x58(%rbp), %rsi
leaq -0x68(%rbp), %rdx
movl $0x4016, %ecx           # imm = 0x4016
xorl %eax, %eax
movl %eax, %r8d
callq 0x30530
movl %eax, -0x24(%rbp)
cmpl $0x0, -0x24(%rbp)
je 0x2f5e5
movq -0x10(%rbp), %rdi
movq -0x58(%rbp), %rsi
callq 0x9cd0
movl -0x24(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x2f8c7
movl $0x0, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl 0x28(%rcx), %eax
jge 0x2f714
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movslq -0x1c(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rax
movq %rax, -0x88(%rbp)
movq -0x88(%rbp), %rdi
callq 0x30220
cmpl $0x0, %eax
je 0x2f704
movq -0x88(%rbp), %rdi
callq 0x305b0
movq %rax, -0x90(%rbp)
cmpq $0x0, -0x90(%rbp)
jne 0x2f651
jmp 0x2f706
cmpl $0x0, -0x4c(%rbp)
jne 0x2f66f
movq -0x88(%rbp), %rdi
callq 0x305e0
cmpl $0x0, %eax
jne 0x2f66f
movl $0x1, -0x4c(%rbp)
movq -0x68(%rbp), %rax
movq %rax, -0xb8(%rbp)
movq -0x90(%rbp), %rax
movq %rax, -0xb0(%rbp)
movq -0x90(%rbp), %rdi
callq 0x92f0
movq -0xb8(%rbp), %rdi
movq -0xb0(%rbp), %rsi
movl %eax, %edx
addl $0x1, %edx
movq -0x70(%rbp), %rcx
callq 0xf430
movl %eax, -0x24(%rbp)
movq -0x90(%rbp), %rdi
callq 0x92f0
addl $0x1, %eax
cltq
addq -0x70(%rbp), %rax
movq %rax, -0x70(%rbp)
cmpl $0x0, -0x24(%rbp)
je 0x2f702
movq -0x68(%rbp), %rdi
callq 0x300e0
movq -0x48(%rbp), %rdi
movq -0x58(%rbp), %rsi
xorl %edx, %edx
callq 0x259d0
movq -0x10(%rbp), %rdi
movq -0x58(%rbp), %rsi
callq 0x9cd0
movl -0x24(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x2f8c7
jmp 0x2f704
jmp 0x2f706
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0x2f5ec
cmpl $0x0, -0x4c(%rbp)
je 0x2f77a
movq -0x68(%rbp), %rdi
callq 0x210d0
movl %eax, %ecx
andl $0x400, %ecx            # imm = 0x400
xorl %eax, %eax
cmpl %ecx, %eax
jne 0x2f77a
movq -0x68(%rbp), %rdi
movl $0x2, %esi
callq 0x22ce0
movl %eax, %ecx
movl %ecx, -0x24(%rbp)
xorl %eax, %eax
cmpl %ecx, %eax
je 0x2f77a
movq -0x68(%rbp), %rdi
callq 0x300e0
movq -0x48(%rbp), %rdi
movq -0x58(%rbp), %rsi
xorl %edx, %edx
callq 0x259d0
movq -0x10(%rbp), %rdi
movq -0x58(%rbp), %rsi
callq 0x9cd0
movl -0x24(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x2f8c7
movl $0x0, -0x1c(%rbp)
xorl %eax, %eax
cmpl $0x0, -0x24(%rbp)
movb %al, -0xb9(%rbp)
jne 0x2f7a2
movl -0x1c(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl 0x28(%rcx), %eax
setl %al
movb %al, -0xb9(%rbp)
movb -0xb9(%rbp), %al
testb $0x1, %al
jne 0x2f7ae
jmp 0x2f7f6
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movslq -0x1c(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rax
movq %rax, -0x98(%rbp)
cmpq $0x0, -0x98(%rbp)
je 0x2f7e9
movq -0x98(%rbp), %rdi
movq -0x58(%rbp), %rsi
callq 0xf600
movl %eax, -0x24(%rbp)
jmp 0x2f7eb
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0x2f781
movq -0x68(%rbp), %rdi
callq 0x300e0
cmpl $0x0, -0x24(%rbp)
je 0x2f81d
movq -0x10(%rbp), %rdi
movq -0x58(%rbp), %rsi
callq 0x9cd0
movl -0x24(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x2f8c7
movq -0x48(%rbp), %rdi
movq -0x58(%rbp), %rsi
movl $0x1, %edx
callq 0x259d0
movl %eax, -0x24(%rbp)
movq -0x10(%rbp), %rdi
movq -0x58(%rbp), %rsi
callq 0x9cd0
movq $0x0, -0x58(%rbp)
cmpl $0x0, -0x24(%rbp)
je 0x2f855
movl -0x24(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x2f8c7
callq 0x241b0
movl $0x0, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl 0x28(%rcx), %eax
jge 0x2f8b3
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movslq -0x1c(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rax
movq %rax, -0xa0(%rbp)
cmpq $0x0, -0xa0(%rbp)
je 0x2f8a6
movq -0xa0(%rbp), %rdi
movl $0x1, %esi
callq 0xf560
jmp 0x2f8a8
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0x2f861
callq 0x24200
movq -0x10(%rbp), %rdi
callq 0x30500
movl -0x24(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0xc0, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)