// File: /workspace/github_c_makefile_star2_O1/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 $0x48, %rsp
movq %rdi, %r8
movq 0x1c8(%rdi), %r13
movq $0x0, 0x1c8(%rdi)
cmpl $0x0, 0x1a4(%rdi)
movq %rdi, -0x30(%rbp)
jle 0x23a0f
movq %rsi, %r14
xorl %ebx, %ebx
movq (%r13,%rbx,8), %rax
movq 0x10(%rax), %r15
movl $0x0, %r12d
testq %r15, %r15
je 0x239f9
movq (%r15), %rax
movq 0x78(%rax), %rax
testq %rax, %rax
je 0x239f9
movq %r15, %rdi
callq *%rax
movl %eax, %r12d
movq %r14, %rdi
movq %r15, %rsi
callq 0x2474a
movq -0x30(%rbp), %r8
testl %r12d, %r12d
jne 0x23a12
incq %rbx
movslq 0x1a4(%r8), %rax
cmpq %rax, %rbx
jl 0x239c2
jmp 0x23a12
xorl %r12d, %r12d
movq %r13, 0x1c8(%r8)
testl %r12d, %r12d
jne 0x23e7d
xorl %r15d, %r15d
movl $0x8, %r13d
xorl %r14d, %r14d
xorl %eax, %eax
movslq 0x28(%r8), %rcx
cmpq %rcx, %r15
jge 0x23a8b
movq 0x20(%r8), %rcx
movq (%rcx,%r13), %rcx
xorl %r12d, %r12d
testq %rcx, %rcx
je 0x23a7a
cmpb $0x2, 0x10(%rcx)
jne 0x23a7a
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 0x1c923
movq -0x30(%rbp), %r8
movl %eax, %r12d
movl $0x1, %eax
incq %r15
addq $0x20, %r13
testl %r12d, %r12d
je 0x23a30
jmp 0x23e7d
testl %eax, %eax
je 0x23ab6
movq 0xe0(%r8), %rax
testq %rax, %rax
je 0x23ab6
movq 0xd8(%r8), %rdi
callq *%rax
movq -0x30(%rbp), %r8
movl $0x213, %r12d           # imm = 0x213
testl %eax, %eax
jne 0x23e7d
movq 0x20(%r8), %rax
movq 0x8(%rax), %rax
movq 0x8(%rax), %rax
movq (%rax), %rax
movb 0x12(%rax), %cl
testb %cl, %cl
je 0x23ad5
leaq 0x5a88d(%rip), %rdx     # 0x7e360
jmp 0x23adc
movq 0xd0(%rax), %rdx
testq %rdx, %rdx
je 0x23afc
movq $-0x1, %rsi
cmpb $0x0, 0x1(%rdx,%rsi)
leaq 0x1(%rsi), %rsi
jne 0x23ae8
shlq $0x22, %rsi
sete %dl
jmp 0x23afe
movb $0x1, %dl
testb %dl, %dl
jne 0x23b1c
cmpl $0x1, %r14d
jle 0x23b1c
testb %cl, %cl
je 0x23bba
leaq 0x5a849(%rip), %rdx     # 0x7e360
jmp 0x23bc1
cmpl $0x0, 0x28(%r8)
jle 0x23b63
movl $0x8, %ebx
xorl %r14d, %r14d
movq 0x20(%r8), %rax
movq (%rax,%rbx), %rdi
movl $0x0, %r12d
testq %rdi, %rdi
je 0x23b4c
xorl %esi, %esi
callq 0xe383
movq -0x30(%rbp), %r8
movl %eax, %r12d
testl %r12d, %r12d
jne 0x23b66
incq %r14
movslq 0x28(%r8), %rax
addq $0x20, %rbx
cmpq %rax, %r14
jl 0x23b2b
jmp 0x23b66
xorl %r12d, %r12d
testl %r12d, %r12d
jne 0x23e7d
movq $-0x1, %rbx
movl $0x8, %r14d
movslq 0x28(%r8), %rax
incq %rbx
cmpq %rax, %rbx
jge 0x23e6d
movq 0x20(%r8), %rax
movq (%rax,%r14), %rdi
testq %rdi, %rdi
je 0x23ba9
xorl %esi, %esi
callq 0xe2d0
movq -0x30(%rbp), %r8
movl %eax, %r12d
jmp 0x23bac
xorl %r12d, %r12d
addq $0x20, %r14
testl %r12d, %r12d
je 0x23b7c
jmp 0x23e7d
movq 0xd0(%rax), %rdx
movq (%r8), %r14
movq $0x0, -0x48(%rbp)
testq %rdx, %rdx
je 0x23bed
movq $-0x1, %r13
cmpb $0x0, 0x1(%rdx,%r13)
leaq 0x1(%r13), %r13
jne 0x23bd8
andl $0x3fffffff, %r13d      # imm = 0x3FFFFFFF
jmp 0x23bf0
xorl %r13d, %r13d
leaq 0x55cb1(%rip), %rsi     # 0x798a8
movq %r8, %rdi
xorl %eax, %eax
callq 0x117e2
testq %rax, %rax
je 0x23cff
movq %rax, -0x38(%rbp)
addq %rax, %r13
xorl %r15d, %r15d
testl %r15d, %r15d
movq -0x38(%rbp), %r12
je 0x23c68
cmpl $0x65, %r15d
jl 0x23c4c
xorl %ebx, %ebx
movl $0xd, %edi
leaq 0x55c87(%rip), %rsi     # 0x798b8
movq %r12, %rdx
xorl %eax, %eax
callq 0xb8cc
movq %r14, %rdi
movq %r12, %rsi
xorl %edx, %edx
callq *0x30(%r14)
xorl %r12d, %r12d
jmp 0x23cae
cmpl $0x1, %r15d
jne 0x23c68
movl $0xd, %edi
leaq 0x55c68(%rip), %rsi     # 0x798c6
movq %r12, %rdx
xorl %eax, %eax
callq 0xb8cc
incl %r15d
movl $0x4, %edi
leaq -0x58(%rbp), %rsi
callq 0xb9d9
movl -0x58(%rbp), %ecx
movzbl %cl, %r8d
shrl $0x8, %ecx
movl $0xd, %edi
movq %r13, %rsi
leaq 0x55c43(%rip), %rdx     # 0x798d5
xorl %eax, %eax
callq 0xb859
movq %r14, %rdi
movq %r12, %rsi
xorl %edx, %edx
leaq -0x5c(%rbp), %rcx
callq *0x38(%r14)
movl %eax, %r12d
movb $0x1, %bl
testb %bl, %bl
je 0x23cc1
testl %r12d, %r12d
jne 0x23cc1
cmpl $0x0, -0x5c(%rbp)
jne 0x23c14
testl %r12d, %r12d
movq -0x30(%rbp), %rcx
jne 0x23ce9
leaq -0x48(%rbp), %rdx
movq %r14, %rdi
movq -0x38(%rbp), %rsi
movl $0x4016, %ecx           # imm = 0x4016
xorl %r8d, %r8d
callq 0x246b6
movq -0x30(%rbp), %rcx
movl %eax, %r12d
testl %r12d, %r12d
je 0x23d0a
movq %rcx, %rdi
movq -0x38(%rbp), %rsi
callq 0x9c3a
jmp 0x23e7d
movl $0x7, %r12d
jmp 0x23e7d
cmpl $0x0, 0x28(%rcx)
jle 0x23e8f
xorl %r13d, %r13d
movq -0x48(%rbp), %rax
movq %rax, -0x68(%rbp)
xorl %r12d, %r12d
movq $0x0, -0x50(%rbp)
xorl %esi, %esi
movq -0x38(%rbp), %rbx
movq 0x20(%rcx), %rax
movq %r13, %rcx
shlq $0x5, %rcx
movq 0x8(%rax,%rcx), %rax
testq %rax, %rax
je 0x23e2a
cmpb $0x2, 0x10(%rax)
jne 0x23e2a
movq 0x8(%rax), %rdx
movq (%rdx), %rcx
movq 0xd8(%rcx), %r15
testq %r15, %r15
je 0x23e31
testl %esi, %esi
movl $0x1, %esi
jne 0x23d84
movq (%rax), %rax
movq %rax, 0x8(%rdx)
xorl %esi, %esi
cmpb $0x0, 0xb(%rcx)
sete %sil
movl %esi, -0x3c(%rbp)
xorl %edx, %edx
movq -0x68(%rbp), %r12
cmpb $0x0, (%r15,%rdx)
leaq 0x1(%rdx), %rdx
jne 0x23d8d
decl %edx
andl $0x3fffffff, %edx       # imm = 0x3FFFFFFF
incl %edx
movq (%r12), %rax
movq %r12, %rdi
movq %r15, %rsi
movq -0x50(%rbp), %rcx
callq *0x18(%rax)
movl %eax, %edx
movq $-0x1, %rax
cmpb $0x0, 0x1(%r15,%rax)
leaq 0x1(%rax), %rax
jne 0x23dbc
andl $0x3fffffff, %eax       # imm = 0x3FFFFFFF
movq -0x50(%rbp), %rcx
addq %rax, %rcx
incq %rcx
movq %rcx, -0x50(%rbp)
testl %edx, %edx
je 0x23e3b
movl %edx, %r15d
movq (%r12), %rax
testq %rax, %rax
je 0x23df9
movq %r12, %rdi
callq *0x8(%rax)
movq $0x0, (%r12)
movq %r12, %rdi
callq 0xa2e8
movq %r14, %rdi
movq %rbx, %rsi
xorl %edx, %edx
callq *0x30(%r14)
movq -0x30(%rbp), %rdi
movq %rbx, %rsi
callq 0x9c3a
movl $0x1, %eax
movl %r15d, %ecx
movl %r15d, %r12d
movl %r15d, -0x54(%rbp)
jmp 0x23e42
movl %r12d, %ecx
xorl %eax, %eax
jmp 0x23e45
movl $0xf, %eax
movl -0x54(%rbp), %ecx
jmp 0x23e45
xorl %ecx, %ecx
xorl %eax, %eax
xorl %r12d, %r12d
movl -0x3c(%rbp), %esi
cmpl $0xf, %eax
je 0x23e52
testl %eax, %eax
jne 0x23efd
incq %r13
movq -0x30(%rbp), %rcx
movslq 0x28(%rcx), %rax
cmpq %rax, %r13
jl 0x23d30
testl %esi, %esi
sete %al
jmp 0x23e94
movq %r8, %rdi
movl $0x80, %esi
callq 0x2457d
xorl %r12d, %r12d
movl %r12d, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %r12d, %r12d
movb $0x1, %al
testb %al, %al
jne 0x23f0c
movq -0x48(%rbp), %r15
movq (%r15), %rax
movq %r15, %rdi
callq *0x60(%rax)
movq -0x30(%rbp), %rcx
btl $0xa, %eax
jb 0x23f0c
movq (%r15), %rax
movq %r15, %rdi
movl $0x2, %esi
callq *0x28(%rax)
testl %eax, %eax
je 0x23f05
movl %eax, %r12d
movq (%r15), %rax
testq %rax, %rax
je 0x23ed9
movq %r15, %rdi
callq *0x8(%rax)
movq $0x0, (%r15)
movq %r15, %rdi
callq 0xa2e8
movq %r14, %rdi
movq -0x38(%rbp), %rbx
movq %rbx, %rsi
xorl %edx, %edx
callq *0x30(%r14)
movq -0x30(%rbp), %rdi
movq %rbx, %rsi
jmp 0x23cf5
movl %ecx, %r12d
jmp 0x23e7d
xorl %r12d, %r12d
movq -0x30(%rbp), %rcx
testl %r12d, %r12d
sete %r13b
je 0x23fdc
movq -0x48(%rbp), %r15
movq (%r15), %rax
testq %rax, %rax
je 0x23f32
movq %r15, %rdi
callq *0x8(%rax)
movq $0x0, (%r15)
movq %r15, %rdi
callq 0xa2e8
testb %r13b, %r13b
je 0x23fd3
movq %r14, %rdi
movq -0x38(%rbp), %rbx
movq %rbx, %rsi
movl $0x1, %edx
callq *0x30(%r14)
movl %eax, %r12d
movq -0x30(%rbp), %rdi
movq %rbx, %rsi
callq 0x9c3a
testl %r12d, %r12d
jne 0x23e7d
movq 0x71feb(%rip), %rax     # 0x95f60
testq %rax, %rax
je 0x23f7c
callq *%rax
movq -0x30(%rbp), %rcx
cmpl $0x0, 0x28(%rcx)
jle 0x23fb9
movl $0x8, %ebx
xorl %r14d, %r14d
movq 0x20(%rcx), %rax
movq (%rax,%rbx), %rdi
testq %rdi, %rdi
je 0x23fa9
movl $0x1, %esi
callq 0xe2d0
movq -0x30(%rbp), %rcx
incq %r14
movslq 0x28(%rcx), %rax
addq $0x20, %rbx
cmpq %rax, %r14
jl 0x23f8e
movq 0x71fa8(%rip), %rax     # 0x95f68
testq %rax, %rax
je 0x23fcb
callq *%rax
movq -0x30(%rbp), %rcx
movq %rcx, %rdi
jmp 0x23e70
movq -0x30(%rbp), %rdi
jmp 0x23cf1
cmpl $0x0, 0x28(%rcx)
jle 0x23f19
movl $0x8, %ebx
xorl %r15d, %r15d
movq 0x20(%rcx), %rax
movq (%rax,%rbx), %rdi
movl $0x0, %r12d
testq %rdi, %rdi
je 0x24011
movq -0x38(%rbp), %rsi
callq 0xe383
movq -0x30(%rbp), %rcx
movl %eax, %r12d
testl %r12d, %r12d
sete %r13b
jne 0x23f19
incq %r15
movslq 0x28(%rcx), %rax
addq $0x20, %rbx
cmpq %rax, %r15
jl 0x23fee
jmp 0x23f19