// File: /workspace/github_c_makefile_star2_O3/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rsi, %r14
movq %rdi, %r8
movq 0x1c8(%rdi), %r13
movq $0x0, 0x1c8(%rdi)
xorl %ebx, %ebx
movq %rdi, -0x30(%rbp)
movslq 0x1a4(%r8), %rax
cmpq %rax, %rbx
jge 0x235cf
movq (%r13,%rbx,8), %rax
movq 0x10(%rax), %r15
xorl %r12d, %r12d
testq %r15, %r15
je 0x235bb
movq (%r15), %rax
movq 0x78(%rax), %rax
testq %rax, %rax
je 0x235bb
movq %r15, %rdi
callq *%rax
movl %eax, %r12d
movq %r14, %rdi
movq %r15, %rsi
callq 0x2425d
movq -0x30(%rbp), %r8
incq %rbx
testl %r12d, %r12d
je 0x2357b
movq %r13, 0x1c8(%r8)
jmp 0x238b1
movq %r13, 0x1c8(%r8)
xorl %r15d, %r15d
movl $0x8, %r13d
xorl %r14d, %r14d
xorl %eax, %eax
movslq 0x28(%r8), %rcx
cmpq %rcx, %r15
jge 0x2363f
movq 0x20(%r8), %rcx
movq (%rcx,%r13), %rcx
xorl %r12d, %r12d
testq %rcx, %rcx
je 0x2362e
cmpb $0x2, 0x10(%rcx)
jne 0x2362e
xorl %eax, %eax
cmpq $0x1, %r15
setne %al
addl %eax, %r14d
movq (%rcx), %rax
movq 0x8(%rcx), %rcx
movq %rax, 0x8(%rcx)
movq (%rcx), %rdi
callq 0x1c78d
movq -0x30(%rbp), %r8
movl %eax, %r12d
movl $0x1, %eax
incq %r15
addq $0x20, %r13
testl %r12d, %r12d
je 0x235e4
jmp 0x238b1
testl %eax, %eax
je 0x2366a
movq 0xe0(%r8), %rax
testq %rax, %rax
je 0x2366a
movq 0xd8(%r8), %rdi
callq *%rax
movq -0x30(%rbp), %r8
movl $0x213, %r12d           # imm = 0x213
testl %eax, %eax
jne 0x238b1
movq 0x20(%r8), %rax
movq 0x8(%rax), %rax
movq 0x8(%rax), %rax
movq (%rax), %rax
movb 0x12(%rax), %cl
testb %cl, %cl
je 0x23689
leaq 0x5ad29(%rip), %rdx     # 0x7e3b0
jmp 0x23695
movq 0xd0(%rax), %rdx
testq %rdx, %rdx
je 0x236cf
movq $-0x1, %rsi
cmpb $0x0, 0x1(%rdx,%rsi)
leaq 0x1(%rsi), %rsi
jne 0x2369c
shlq $0x22, %rsi
je 0x236cf
cmpl $0x1, %r14d
jle 0x236cf
testb %cl, %cl
je 0x23761
movq $0x0, -0x40(%rbp)
leaq 0x5ace6(%rip), %rdx     # 0x7e3b0
jmp 0x23779
movq $-0x1, %rbx
movl $0x8, %r14d
movslq 0x28(%r8), %rax
incq %rbx
cmpq %rax, %rbx
jge 0x23716
movq 0x20(%r8), %rax
movq (%rax,%r14), %rdi
testq %rdi, %rdi
je 0x23705
xorl %esi, %esi
callq 0xe473
movq -0x30(%rbp), %r8
movl %eax, %r12d
jmp 0x23708
xorl %r12d, %r12d
addq $0x20, %r14
testl %r12d, %r12d
je 0x236dc
jmp 0x238b1
movq $-0x1, %rbx
movl $0x8, %r14d
movslq 0x28(%r8), %rax
incq %rbx
cmpq %rax, %rbx
jge 0x2384c
movq 0x20(%r8), %rax
movq (%rax,%r14), %rdi
testq %rdi, %rdi
je 0x23750
xorl %esi, %esi
callq 0xe3c0
movq -0x30(%rbp), %r8
movl %eax, %r12d
jmp 0x23753
xorl %r12d, %r12d
addq $0x20, %r14
testl %r12d, %r12d
je 0x23723
jmp 0x238b1
movq 0xd0(%rax), %rdx
movq $0x0, -0x40(%rbp)
testq %rdx, %rdx
je 0x239dd
movq $-0x1, %r13
cmpb $0x0, 0x1(%rdx,%r13)
leaq 0x1(%r13), %r13
jne 0x23780
andl $0x3fffffff, %r13d      # imm = 0x3FFFFFFF
movq (%r8), %r14
leaq 0x5610b(%rip), %rsi     # 0x798a8
movq %r8, %rdi
xorl %eax, %eax
callq 0x11900
testq %rax, %rax
je 0x23844
movq %rax, %rbx
movq %r14, -0x38(%rbp)
addq %rax, %r13
leaq -0x4c(%rbp), %r14
xorl %r15d, %r15d
testl %r15d, %r15d
je 0x237ec
cmpl $0x64, %r15d
ja 0x2385e
cmpl $0x1, %r15d
jne 0x237ec
movl $0xd, %edi
leaq 0x560e4(%rip), %rsi     # 0x798c6
movq %rbx, %rdx
xorl %eax, %eax
callq 0xb9c7
movl $0x4, %edi
leaq -0x48(%rbp), %rsi
callq 0xbad4
movl -0x48(%rbp), %ecx
movzbl %cl, %r8d
shrl $0x8, %ecx
movl $0xd, %edi
movq %r13, %rsi
leaq 0x560c2(%rip), %rdx     # 0x798d5
xorl %eax, %eax
callq 0xb954
movq -0x38(%rbp), %rdi
movq %rbx, %rsi
xorl %edx, %edx
movq %r14, %rcx
callq *0x38(%rdi)
movl %eax, %r12d
testl %eax, %eax
jne 0x23839
incl %r15d
cmpl $0x0, -0x4c(%rbp)
jne 0x237c1
testl %r12d, %r12d
movq -0x38(%rbp), %r14
jne 0x238a2
jmp 0x23884
movl $0x7, %r12d
jmp 0x238b1
movq %r8, %rdi
movl $0x80, %esi
callq 0x2408c
xorl %r12d, %r12d
jmp 0x238b1
leaq 0x56053(%rip), %rsi     # 0x798b8
movl $0xd, %edi
movq %rbx, %rdx
xorl %eax, %eax
callq 0xb9c7
movq -0x38(%rbp), %r14
movq %r14, %rdi
movq %rbx, %rsi
xorl %edx, %edx
callq *0x30(%r14)
leaq -0x40(%rbp), %rdx
movq %r14, %rdi
movq %rbx, %rsi
movl $0x4016, %ecx           # imm = 0x4016
xorl %r8d, %r8d
callq 0x241c9
testl %eax, %eax
je 0x238c3
movl %eax, %r12d
movq -0x30(%rbp), %rax
movq %rax, %rdi
movq %rbx, %rsi
callq 0x9ca4
movl %r12d, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq -0x30(%rbp), %r8
movl 0x28(%r8), %eax
testl %eax, %eax
jle 0x239e7
xorl %r15d, %r15d
movq -0x40(%rbp), %rcx
movq %rcx, -0x58(%rbp)
xorl %ecx, %ecx
xorl %r9d, %r9d
movq 0x20(%r8), %rsi
movq %r15, %rdx
shlq $0x5, %rdx
movq 0x8(%rsi,%rdx), %rdi
testq %rdi, %rdi
je 0x23994
cmpb $0x2, 0x10(%rdi)
jne 0x23994
movq 0x8(%rdi), %rsi
movq (%rsi), %rdx
movq 0xd8(%rdx), %r14
testq %r14, %r14
je 0x23994
testl %r9d, %r9d
movl $0x1, %eax
jne 0x23933
movq (%rdi), %rax
movq %rax, 0x8(%rsi)
xorl %eax, %eax
cmpb $0x0, 0xb(%rdx)
sete %al
movl %eax, -0x44(%rbp)
xorl %edx, %edx
cmpb $0x0, (%r14,%rdx)
leaq 0x1(%rdx), %rdx
jne 0x23938
decl %edx
andl $0x3fffffff, %edx       # imm = 0x3FFFFFFF
incl %edx
movq -0x58(%rbp), %rdi
movq (%rdi), %rax
movq %r14, %rsi
movq %rcx, %r13
callq *0x18(%rax)
movl %eax, %r12d
movq $-0x1, %rax
cmpb $0x0, 0x1(%r14,%rax)
leaq 0x1(%rax), %rax
jne 0x23967
testl %r12d, %r12d
movq -0x30(%rbp), %r8
jne 0x23af6
andl $0x3fffffff, %eax       # imm = 0x3FFFFFFF
leaq (%rax,%r13), %rcx
incq %rcx
movl 0x28(%r8), %eax
movl -0x44(%rbp), %r9d
incq %r15
movslq %eax, %rdx
cmpq %rdx, %r15
jl 0x238e3
testl %r9d, %r9d
je 0x239e7
movq -0x40(%rbp), %r14
movq (%r14), %rax
movq %r14, %rdi
callq *0x60(%rax)
movq -0x30(%rbp), %r8
btl $0xa, %eax
jb 0x239e7
movq (%r14), %rax
movq %r14, %rdi
movl $0x2, %esi
callq *0x28(%rax)
movq -0x30(%rbp), %r8
testl %eax, %eax
je 0x239e7
movl %eax, %r12d
jmp 0x23afa
xorl %edx, %edx
xorl %r13d, %r13d
jmp 0x23793
movq %rbx, %r14
movq $-0x1, %r15
movl $0x8, %ebx
movslq 0x28(%r8), %r13
incq %r15
cmpq %r13, %r15
jge 0x23a2e
movq 0x20(%r8), %rax
movq (%rax,%rbx), %rdi
testq %rdi, %rdi
je 0x23a20
movq %r14, %rsi
callq 0xe473
movq -0x30(%rbp), %r8
movl %eax, %r12d
jmp 0x23a23
xorl %r12d, %r12d
addq $0x20, %rbx
testl %r12d, %r12d
je 0x239f6
jmp 0x23a31
xorl %r12d, %r12d
movq -0x40(%rbp), %rdi
movq (%rdi), %rax
testq %rax, %rax
movq %r14, %rbx
je 0x23a50
movq %rdi, %r14
callq *0x8(%rax)
movq %r14, %rdi
movq $0x0, (%r14)
callq 0xa3c7
cmpq %r13, %r15
movq -0x30(%rbp), %rax
jl 0x238a6
movq -0x38(%rbp), %rdi
movq %rbx, %rsi
movl $0x1, %edx
movq %rbx, %r14
movq %rax, %rbx
callq *0x30(%rdi)
movl %eax, %r12d
movq %rbx, %rdi
movq %r14, %rsi
callq 0x9ca4
testl %r12d, %r12d
jne 0x238b1
movq 0x724cb(%rip), %rax     # 0x95f60
testq %rax, %rax
je 0x23a9c
callq *%rax
movq -0x30(%rbp), %rdx
movl 0x28(%rdx), %eax
testl %eax, %eax
jle 0x23adc
movl $0x8, %ebx
xorl %r14d, %r14d
movq 0x20(%rdx), %rcx
movq (%rcx,%rbx), %rdi
testq %rdi, %rdi
je 0x23acd
movl $0x1, %esi
callq 0xe3c0
movq -0x30(%rbp), %rdx
movl 0x28(%rdx), %eax
incq %r14
movslq %eax, %rcx
addq $0x20, %rbx
cmpq %rcx, %r14
jl 0x23aaf
movq 0x72485(%rip), %rax     # 0x95f68
testq %rax, %rax
je 0x23aee
callq *%rax
movq -0x30(%rbp), %rdx
movq %rdx, %rdi
jmp 0x2384f
movq -0x58(%rbp), %r14
movq (%r14), %rax
testq %rax, %rax
je 0x23b0f
movq %r14, %rdi
callq *0x8(%rax)
movq $0x0, (%r14)
movq %r14, %rdi
callq 0xa3c7
movq -0x38(%rbp), %rdi
movq %rbx, %rsi
xorl %edx, %edx
callq *0x30(%rdi)
jmp 0x238a2