// File: /workspace/github_c_makefile_star2_O1/barabo[P]advanced-shell-history/src/sqlite3.c
// Function: sqlite3LeaveMutexAndCloseZombie
SQLITE_PRIVATE void sqlite3LeaveMutexAndCloseZombie(sqlite3 *db){
  HashElem *i;                    /* Hash table iterator */
  int j;

  /* If there are outstanding sqlite3_stmt or sqlite3_backup objects
  ** or if the connection has not yet been closed by sqlite3_close_v2(),
  ** then just leave the mutex and return.
  */
  if( db->magic!=SQLITE_MAGIC_ZOMBIE || connectionIsBusy(db) ){
    sqlite3_mutex_leave(db->mutex);
    return;
  }

  /* If we reach this point, it means that the database connection has
  ** closed all sqlite3_stmt and sqlite3_backup objects and has been
  ** passed to sqlite3_close (meaning that it is a zombie).  Therefore,
  ** go ahead and free all resources.
  */

  /* If a transaction is open, roll it back. This also ensures that if
  ** any database schemas have been modified by an uncommitted transaction
  ** they are reset. And that the required b-tree mutex is held to make
  ** the pager rollback and schema reset an atomic operation. */
  sqlite3RollbackAll(db, SQLITE_OK);

  /* Free any outstanding Savepoint structures. */
  sqlite3CloseSavepoints(db);

  /* Close all database connections */
  for(j=0; j<db->nDb; j++){
    struct Db *pDb = &db->aDb[j];
    if( pDb->pBt ){
      sqlite3BtreeClose(pDb->pBt);
      pDb->pBt = 0;
      if( j!=1 ){
        pDb->pSchema = 0;
      }
    }
  }
  /* Clear the TEMP schema separately and last */
  if( db->aDb[1].pSchema ){
    sqlite3SchemaClear(db->aDb[1].pSchema);
  }
  sqlite3VtabUnlockList(db);

  /* Free up the array of auxiliary databases */
  sqlite3CollapseDatabaseArray(db);
  assert( db->nDb<=2 );
  assert( db->aDb==db->aDbStatic );

  /* Tell the code in notify.c that the connection no longer holds any
  ** locks and does not require any further unlock-notify callbacks.
  */
  sqlite3ConnectionClosed(db);

  for(j=0; j<ArraySize(db->aFunc.a); j++){
    FuncDef *pNext, *pHash, *p;
    for(p=db->aFunc.a[j]; p; p=pHash){
      pHash = p->pHash;
      while( p ){
        functionDestroy(db, p);
        pNext = p->pNext;
        sqlite3DbFree(db, p);
        p = pNext;
      }
    }
  }
  for(i=sqliteHashFirst(&db->aCollSeq); i; i=sqliteHashNext(i)){
    CollSeq *pColl = (CollSeq *)sqliteHashData(i);
    /* Invoke any destructors registered for collation sequence user data. */
    for(j=0; j<3; j++){
      if( pColl[j].xDel ){
        pColl[j].xDel(pColl[j].pUser);
      }
    }
    sqlite3DbFree(db, pColl);
  }
  sqlite3HashClear(&db->aCollSeq);
#ifndef SQLITE_OMIT_VIRTUALTABLE
  for(i=sqliteHashFirst(&db->aModule); i; i=sqliteHashNext(i)){
    Module *pMod = (Module *)sqliteHashData(i);
    if( pMod->xDestroy ){
      pMod->xDestroy(pMod->pAux);
    }
    sqlite3DbFree(db, pMod);
  }
  sqlite3HashClear(&db->aModule);
#endif

  sqlite3Error(db, SQLITE_OK, 0); /* Deallocates any cached error strings. */
  if( db->pErr ){
    sqlite3ValueFree(db->pErr);
  }
  sqlite3CloseExtensions(db);

  db->magic = SQLITE_MAGIC_ERROR;

  /* The temp-database schema is allocated differently from the other schema
  ** objects (using sqliteMalloc() directly, instead of sqlite3BtreeSchema()).
  ** So it needs to be freed here. Todo: Why not roll the temp schema into
  ** the same sqliteMalloc() as the one that allocates the database 
  ** structure?
  */
  sqlite3DbFree(db, db->aDb[1].pSchema);
  sqlite3_mutex_leave(db->mutex);
  db->magic = SQLITE_MAGIC_CLOSED;
  sqlite3_mutex_free(db->mutex);
  assert( db->lookaside.nOut==0 );  /* Fails on a lookaside memory leak */
  if( db->lookaside.bMalloced ){
    sqlite3_free(db->lookaside.pStart);
  }
  sqlite3_free(db);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
cmpl $0x64cffc7f, 0x5c(%rdi) # imm = 0x64CFFC7F
jne 0xe74a
movq %rdi, %rbx
callq 0x67a76
testl %eax, %eax
je 0xe759
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rbx, %rdi
xorl %esi, %esi
callq 0x237b9
movq %rbx, %rdi
callq 0x238ef
cmpl $0x0, 0x28(%rbx)
jle 0xe7b3
movl $0x8, %r14d
xorl %r15d, %r15d
movq 0x20(%rbx), %r12
movq (%r12,%r14), %rdi
testq %rdi, %rdi
je 0xe7a3
callq 0x243ae
movq $0x0, (%r12,%r14)
cmpq $0x1, %r15
je 0xe7a3
movq $0x0, 0x10(%r12,%r14)
incq %r15
movslq 0x28(%rbx), %rax
addq $0x20, %r14
cmpq %rax, %r15
jl 0xe77a
movq 0x20(%rbx), %rax
movq 0x38(%rax), %rdi
testq %rdi, %rdi
je 0xe7c5
callq 0x30334
movq %rbx, %rdi
callq 0x3668e
movq %rbx, %rdi
callq 0x366dc
xorl %r15d, %r15d
movq 0x1d8(%rbx,%r15,8), %r12
testq %r12, %r12
je 0xe811
movq %r12, %r14
movq 0x38(%r12), %r12
movq %rbx, %rdi
movq %r14, %rsi
callq 0x67aba
movq 0x10(%r14), %r13
movq %rbx, %rdi
movq %r14, %rsi
callq 0x9c3a
movq %r13, %r14
testq %r13, %r13
jne 0xe7ed
jmp 0xe7e0
incq %r15
cmpq $0x17, %r15
jne 0xe7d8
leaq 0x290(%rbx), %r14
movq 0x298(%rbx), %r12
testq %r12, %r12
je 0xe866
movq 0x10(%r12), %r15
movl $0x20, %r13d
movq (%r15,%r13), %rax
testq %rax, %rax
je 0xe848
movq -0x10(%r15,%r13), %rdi
callq *%rax
addq $0x28, %r13
cmpq $0x98, %r13
jne 0xe838
movq %rbx, %rdi
movq %r15, %rsi
callq 0x9c3a
movq (%r12), %r12
jmp 0xe828
movq %r14, %rdi
callq 0x30403
leaq 0x1a8(%rbx), %r14
movq 0x1b0(%rbx), %r12
testq %r12, %r12
je 0xe8a6
movq 0x10(%r12), %r15
movq 0x18(%r15), %rax
testq %rax, %rax
je 0xe895
movq 0x10(%r15), %rdi
callq *%rax
movq %rbx, %rdi
movq %r15, %rsi
callq 0x9c3a
movq (%r12), %r12
jmp 0xe87c
movq %r14, %rdi
callq 0x30403
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
xorl %eax, %eax
callq 0xc8f4
movq 0x130(%rbx), %r14
testq %r14, %r14
je 0xe8db
movq %r14, %rdi
callq 0xec23
movq (%r14), %rdi
movq %r14, %rsi
callq 0x9c3a
movl $0xb5357930, 0x5c(%rbx) # imm = 0xB5357930
movq 0x20(%rbx), %rax
movq 0x38(%rax), %rsi
movq %rbx, %rdi
callq 0x9c3a
movl $0x9f3c2d33, 0x5c(%rbx) # imm = 0x9F3C2D33
cmpb $0x0, 0x153(%rbx)
je 0xe90e
movq 0x170(%rbx), %rdi
callq 0xa2e8
movq %rbx, %rdi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xa2e8