// File: /workspace/github_c_makefile_star2_O0/barabo[P]advanced-shell-history/src/sqlite3.c
// Function: attachFunc
static void attachFunc(
  sqlite3_context *context,
  int NotUsed,
  sqlite3_value **argv
){
  int i;
  int rc = 0;
  sqlite3 *db = sqlite3_context_db_handle(context);
  const char *zName;
  const char *zFile;
  char *zPath = 0;
  char *zErr = 0;
  unsigned int flags;
  Db *aNew;
  char *zErrDyn = 0;
  sqlite3_vfs *pVfs;

  UNUSED_PARAMETER(NotUsed);

  zFile = (const char *)sqlite3_value_text(argv[0]);
  zName = (const char *)sqlite3_value_text(argv[1]);
  if( zFile==0 ) zFile = "";
  if( zName==0 ) zName = "";

  /* Check for the following errors:
  **
  **     * Too many attached databases,
  **     * Transaction currently open
  **     * Specified database name already being used.
  */
  if( db->nDb>=db->aLimit[SQLITE_LIMIT_ATTACHED]+2 ){
    zErrDyn = sqlite3MPrintf(db, "too many attached databases - max %d", 
      db->aLimit[SQLITE_LIMIT_ATTACHED]
    );
    goto attach_error;
  }
  if( !db->autoCommit ){
    zErrDyn = sqlite3MPrintf(db, "cannot ATTACH database within transaction");
    goto attach_error;
  }
  for(i=0; i<db->nDb; i++){
    char *z = db->aDb[i].zName;
    assert( z && zName );
    if( sqlite3StrICmp(z, zName)==0 ){
      zErrDyn = sqlite3MPrintf(db, "database %s is already in use", zName);
      goto attach_error;
    }
  }

  /* Allocate the new entry in the db->aDb[] array and initialize the schema
  ** hash tables.
  */
  if( db->aDb==db->aDbStatic ){
    aNew = sqlite3DbMallocRaw(db, sizeof(db->aDb[0])*3 );
    if( aNew==0 ) return;
    memcpy(aNew, db->aDb, sizeof(db->aDb[0])*2);
  }else{
    aNew = sqlite3DbRealloc(db, db->aDb, sizeof(db->aDb[0])*(db->nDb+1) );
    if( aNew==0 ) return;
  }
  db->aDb = aNew;
  aNew = &db->aDb[db->nDb];
  memset(aNew, 0, sizeof(*aNew));

  /* Open the database file. If the btree is successfully opened, use
  ** it to obtain the database schema. At this point the schema may
  ** or may not be initialized.
  */
  flags = db->openFlags;
  rc = sqlite3ParseUri(db->pVfs->zName, zFile, &flags, &pVfs, &zPath, &zErr);
  if( rc!=SQLITE_OK ){
    if( rc==SQLITE_NOMEM ) db->mallocFailed = 1;
    sqlite3_result_error(context, zErr, -1);
    sqlite3_free(zErr);
    return;
  }
  assert( pVfs );
  flags |= SQLITE_OPEN_MAIN_DB;
  rc = sqlite3BtreeOpen(pVfs, zPath, db, &aNew->pBt, 0, flags);
  sqlite3_free( zPath );
  db->nDb++;
  if( rc==SQLITE_CONSTRAINT ){
    rc = SQLITE_ERROR;
    zErrDyn = sqlite3MPrintf(db, "database is already attached");
  }else if( rc==SQLITE_OK ){
    Pager *pPager;
    aNew->pSchema = sqlite3SchemaGet(db, aNew->pBt);
    if( !aNew->pSchema ){
      rc = SQLITE_NOMEM;
    }else if( aNew->pSchema->file_format && aNew->pSchema->enc!=ENC(db) ){
      zErrDyn = sqlite3MPrintf(db, 
        "attached databases must use the same text encoding as main database");
      rc = SQLITE_ERROR;
    }
    pPager = sqlite3BtreePager(aNew->pBt);
    sqlite3PagerLockingMode(pPager, db->dfltLockMode);
    sqlite3BtreeSecureDelete(aNew->pBt,
                             sqlite3BtreeSecureDelete(db->aDb[0].pBt,-1) );
#ifndef SQLITE_OMIT_PAGER_PRAGMAS
    sqlite3BtreeSetPagerFlags(aNew->pBt, 3 | (db->flags & PAGER_FLAGS_MASK));
#endif
  }
  aNew->safety_level = 3;
  aNew->zName = sqlite3DbStrDup(db, zName);
  if( rc==SQLITE_OK && aNew->zName==0 ){
    rc = SQLITE_NOMEM;
  }


#ifdef SQLITE_HAS_CODEC
  if( rc==SQLITE_OK ){
    extern int sqlite3CodecAttach(sqlite3*, int, const void*, int);
    extern void sqlite3CodecGetKey(sqlite3*, int, void**, int*);
    int nKey;
    char *zKey;
    int t = sqlite3_value_type(argv[2]);
    switch( t ){
      case SQLITE_INTEGER:
      case SQLITE_FLOAT:
        zErrDyn = sqlite3DbStrDup(db, "Invalid key value");
        rc = SQLITE_ERROR;
        break;
        
      case SQLITE_TEXT:
      case SQLITE_BLOB:
        nKey = sqlite3_value_bytes(argv[2]);
        zKey = (char *)sqlite3_value_blob(argv[2]);
        rc = sqlite3CodecAttach(db, db->nDb-1, zKey, nKey);
        break;

      case SQLITE_NULL:
        /* No key specified.  Use the key from the main database */
        sqlite3CodecGetKey(db, 0, (void**)&zKey, &nKey);
        if( nKey>0 || sqlite3BtreeGetReserve(db->aDb[0].pBt)>0 ){
          rc = sqlite3CodecAttach(db, db->nDb-1, zKey, nKey);
        }
        break;
    }
  }
#endif

  /* If the file was opened successfully, read the schema for the new database.
  ** If this fails, or if opening the file failed, then close the file and 
  ** remove the entry from the db->aDb[] array. i.e. put everything back the way
  ** we found it.
  */
  if( rc==SQLITE_OK ){
    sqlite3BtreeEnterAll(db);
    rc = sqlite3Init(db, &zErrDyn);
    sqlite3BtreeLeaveAll(db);
  }
  if( rc ){
    int iDb = db->nDb - 1;
    assert( iDb>=2 );
    if( db->aDb[iDb].pBt ){
      sqlite3BtreeClose(db->aDb[iDb].pBt);
      db->aDb[iDb].pBt = 0;
      db->aDb[iDb].pSchema = 0;
    }
    sqlite3ResetAllSchemasOfConnection(db);
    db->nDb = iDb;
    if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ){
      db->mallocFailed = 1;
      sqlite3DbFree(db, zErrDyn);
      zErrDyn = sqlite3MPrintf(db, "out of memory");
    }else if( zErrDyn==0 ){
      zErrDyn = sqlite3MPrintf(db, "unable to open database: %s", zFile);
    }
    goto attach_error;
  }
  
  return;

attach_error:
  /* Return an error if we get here */
  if( zErrDyn ){
    sqlite3_result_error(context, zErrDyn, -1);
    sqlite3DbFree(db, zErrDyn);
  }
  if( rc ) sqlite3_result_error_code(context, rc);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x90, %rsp
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movq %rdx, -0x18(%rbp)
movl $0x0, -0x20(%rbp)
movq -0x8(%rbp), %rdi
callq 0x117b0
movq %rax, -0x28(%rbp)
movq $0x0, -0x40(%rbp)
movq $0x0, -0x48(%rbp)
movq $0x0, -0x60(%rbp)
movq -0x18(%rbp), %rax
movq (%rax), %rdi
callq 0x10350
movq %rax, -0x38(%rbp)
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x10350
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x38(%rbp)
jne 0x82975
leaq 0x30484(%rip), %rax     # 0xb2df5
movq %rax, -0x38(%rbp)
cmpq $0x0, -0x30(%rbp)
jne 0x82987
leaq 0x30472(%rip), %rax     # 0xb2df5
movq %rax, -0x30(%rbp)
movq -0x28(%rbp), %rax
movl 0x28(%rax), %eax
movq -0x28(%rbp), %rcx
movl 0x84(%rcx), %ecx
addl $0x2, %ecx
cmpl %ecx, %eax
jl 0x829c4
movq -0x28(%rbp), %rdi
movq -0x28(%rbp), %rax
movl 0x84(%rax), %edx
leaq 0x2e7ac(%rip), %rsi     # 0xb1160
movb $0x0, %al
callq 0x137f0
movq %rax, -0x60(%rbp)
jmp 0x82e28
movq -0x28(%rbp), %rax
cmpb $0x0, 0x4e(%rax)
jne 0x829e9
movq -0x28(%rbp), %rdi
leaq 0x2e7ac(%rip), %rsi     # 0xb1185
movb $0x0, %al
callq 0x137f0
movq %rax, -0x60(%rbp)
jmp 0x82e28
movl $0x0, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
movq -0x28(%rbp), %rcx
cmpl 0x28(%rcx), %eax
jge 0x82a54
movq -0x28(%rbp), %rax
movq 0x20(%rax), %rax
movslq -0x1c(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movq (%rax), %rax
movq %rax, -0x70(%rbp)
movq -0x70(%rbp), %rdi
movq -0x30(%rbp), %rsi
callq 0xc970
cmpl $0x0, %eax
jne 0x82a47
movq -0x28(%rbp), %rdi
movq -0x30(%rbp), %rdx
leaq 0x2e778(%rip), %rsi     # 0xb11af
movb $0x0, %al
callq 0x137f0
movq %rax, -0x60(%rbp)
jmp 0x82e28
jmp 0x82a49
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0x829f0
movq -0x28(%rbp), %rax
movq 0x20(%rax), %rax
movq -0x28(%rbp), %rcx
addq $0x2c0, %rcx            # imm = 0x2C0
cmpq %rcx, %rax
jne 0x82aa2
movq -0x28(%rbp), %rdi
movl $0x60, %esi
callq 0x134e0
movq %rax, -0x58(%rbp)
cmpq $0x0, -0x58(%rbp)
jne 0x82a8a
jmp 0x82e60
movq -0x58(%rbp), %rdi
movq -0x28(%rbp), %rax
movq 0x20(%rax), %rsi
movl $0x40, %edx
callq 0x82f0
jmp 0x82ad7
movq -0x28(%rbp), %rdi
movq -0x28(%rbp), %rax
movq 0x20(%rax), %rsi
movq -0x28(%rbp), %rax
movl 0x28(%rax), %eax
addl $0x1, %eax
cltq
shlq $0x5, %rax
movl %eax, %edx
callq 0x1bee0
movq %rax, -0x58(%rbp)
cmpq $0x0, -0x58(%rbp)
jne 0x82ad5
jmp 0x82e60
jmp 0x82ad7
movq -0x58(%rbp), %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x20(%rax)
movq -0x28(%rbp), %rax
movq 0x20(%rax), %rax
movq -0x28(%rbp), %rcx
movslq 0x28(%rcx), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movq %rax, -0x58(%rbp)
movq -0x58(%rbp), %rdi
xorl %esi, %esi
movl $0x20, %edx
callq 0x8200
movq -0x28(%rbp), %rax
movl 0x40(%rax), %eax
movl %eax, -0x4c(%rbp)
movq -0x28(%rbp), %rax
movq (%rax), %rax
movq 0x18(%rax), %rdi
movq -0x38(%rbp), %rsi
leaq -0x4c(%rbp), %rdx
leaq -0x68(%rbp), %rcx
leaq -0x40(%rbp), %r8
leaq -0x48(%rbp), %r9
callq 0x83080
movl %eax, -0x20(%rbp)
cmpl $0x0, -0x20(%rbp)
je 0x82b73
cmpl $0x7, -0x20(%rbp)
jne 0x82b53
movq -0x28(%rbp), %rax
movb $0x1, 0x50(%rax)
movq -0x8(%rbp), %rdi
movq -0x48(%rbp), %rsi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
callq 0x108b0
movq -0x48(%rbp), %rdi
callq 0xa420
jmp 0x82e60
movl -0x4c(%rbp), %eax
orl $0x100, %eax            # imm = 0x100
movl %eax, -0x4c(%rbp)
movq -0x68(%rbp), %rdi
movq -0x40(%rbp), %rsi
movq -0x28(%rbp), %rdx
movq -0x58(%rbp), %rcx
addq $0x8, %rcx
movl -0x4c(%rbp), %r9d
xorl %r8d, %r8d
callq 0x1f8a0
movl %eax, -0x20(%rbp)
movq -0x40(%rbp), %rdi
callq 0xa420
movq -0x28(%rbp), %rax
movl 0x28(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x28(%rax)
cmpl $0x13, -0x20(%rbp)
jne 0x82bdf
movl $0x1, -0x20(%rbp)
movq -0x28(%rbp), %rdi
leaq 0x2e5fe(%rip), %rsi     # 0xb11cd
movb $0x0, %al
callq 0x137f0
movq %rax, -0x60(%rbp)
jmp 0x82cda
cmpl $0x0, -0x20(%rbp)
jne 0x82cd8
movq -0x28(%rbp), %rdi
movq -0x58(%rbp), %rax
movq 0x8(%rax), %rsi
callq 0x83930
movq %rax, %rcx
movq -0x58(%rbp), %rax
movq %rcx, 0x18(%rax)
movq -0x58(%rbp), %rax
cmpq $0x0, 0x18(%rax)
jne 0x82c19
movl $0x7, -0x20(%rbp)
jmp 0x82c69
movq -0x58(%rbp), %rax
movq 0x18(%rax), %rax
movzbl 0x70(%rax), %eax
cmpl $0x0, %eax
je 0x82c67
movq -0x58(%rbp), %rax
movq 0x18(%rax), %rax
movzbl 0x71(%rax), %eax
movq -0x28(%rbp), %rcx
movq 0x20(%rcx), %rcx
movq 0x18(%rcx), %rcx
movzbl 0x71(%rcx), %ecx
cmpl %ecx, %eax
je 0x82c67
movq -0x28(%rbp), %rdi
leaq 0x2c8c2(%rip), %rsi     # 0xaf517
movb $0x0, %al
callq 0x137f0
movq %rax, -0x60(%rbp)
movl $0x1, -0x20(%rbp)
jmp 0x82c69
movq -0x58(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x98f0
movq %rax, -0x78(%rbp)
movq -0x78(%rbp), %rdi
movq -0x28(%rbp), %rax
movzbl 0x51(%rax), %esi
callq 0x81cd0
movq -0x58(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x88(%rbp)
movq -0x28(%rbp), %rax
movq 0x20(%rax), %rax
movq 0x8(%rax), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x81bd0
movq -0x88(%rbp), %rdi
movl %eax, %esi
callq 0x81bd0
movq -0x58(%rbp), %rax
movq 0x8(%rax), %rdi
movq -0x28(%rbp), %rax
movl 0x2c(%rax), %esi
andl $0x1c, %esi
orl $0x3, %esi
callq 0x824c0
jmp 0x82cda
movq -0x58(%rbp), %rax
movb $0x3, 0x10(%rax)
movq -0x28(%rbp), %rdi
movq -0x30(%rbp), %rsi
callq 0x11720
movq %rax, %rcx
movq -0x58(%rbp), %rax
movq %rcx, (%rax)
cmpl $0x0, -0x20(%rbp)
jne 0x82d10
movq -0x58(%rbp), %rax
cmpq $0x0, (%rax)
jne 0x82d10
movl $0x7, -0x20(%rbp)
cmpl $0x0, -0x20(%rbp)
jne 0x82d2f
movq -0x28(%rbp), %rdi
callq 0x9890
movq -0x28(%rbp), %rdi
leaq -0x60(%rbp), %rsi
callq 0x4ab90
movl %eax, -0x20(%rbp)
cmpl $0x0, -0x20(%rbp)
je 0x82e26
movq -0x28(%rbp), %rax
movl 0x28(%rax), %eax
subl $0x1, %eax
movl %eax, -0x7c(%rbp)
movq -0x28(%rbp), %rax
movq 0x20(%rax), %rax
movslq -0x7c(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
cmpq $0x0, 0x8(%rax)
je 0x82db2
movq -0x28(%rbp), %rax
movq 0x20(%rax), %rax
movslq -0x7c(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rdi
callq 0x2fe80
movq -0x28(%rbp), %rax
movq 0x20(%rax), %rax
movslq -0x7c(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movq $0x0, 0x8(%rax)
movq -0x28(%rbp), %rax
movq 0x20(%rax), %rax
movslq -0x7c(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movq $0x0, 0x18(%rax)
movq -0x28(%rbp), %rdi
callq 0xedb0
movl -0x7c(%rbp), %ecx
movq -0x28(%rbp), %rax
movl %ecx, 0x28(%rax)
cmpl $0x7, -0x20(%rbp)
je 0x82dd4
cmpl $0xc0a, -0x20(%rbp)     # imm = 0xC0A
jne 0x82e01
movq -0x28(%rbp), %rax
movb $0x1, 0x50(%rax)
movq -0x28(%rbp), %rdi
movq -0x60(%rbp), %rsi
callq 0x9cd0
movq -0x28(%rbp), %rdi
leaq 0x2b467(%rip), %rsi     # 0xae25b
movb $0x0, %al
callq 0x137f0
movq %rax, -0x60(%rbp)
jmp 0x82e24
cmpq $0x0, -0x60(%rbp)
jne 0x82e22
movq -0x28(%rbp), %rdi
movq -0x38(%rbp), %rdx
leaq 0x2e3d3(%rip), %rsi     # 0xb11ea
movb $0x0, %al
callq 0x137f0
movq %rax, -0x60(%rbp)
jmp 0x82e24
jmp 0x82e28
jmp 0x82e60
cmpq $0x0, -0x60(%rbp)
je 0x82e4e
movq -0x8(%rbp), %rdi
movq -0x60(%rbp), %rsi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
callq 0x108b0
movq -0x28(%rbp), %rdi
movq -0x60(%rbp), %rsi
callq 0x9cd0
cmpl $0x0, -0x20(%rbp)
je 0x82e60
movq -0x8(%rbp), %rdi
movl -0x20(%rbp), %esi
callq 0x10ff0
addq $0x90, %rsp
popq %rbp
retq
nopl (%rax)