// File: /workspace/github_c_makefile_star2_O3/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rdx, %r13
movq %rdi, %rbx
leaq 0x8(%rdi), %rax
movq %rax, -0x50(%rbp)
movq 0x8(%rdi), %r14
xorl %r15d, %r15d
movq %r15, -0x60(%rbp)
movq %r15, -0x58(%rbp)
movq %r15, -0x30(%rbp)
movq (%rdx), %rdi
movl $0x1, %esi
callq 0xef29
movq %rax, %r12
movq 0x8(%r13), %rdi
movl $0x1, %esi
callq 0xef29
testq %r12, %r12
leaq 0x1d170(%rip), %rcx     # 0x7e3b0
cmoveq %rcx, %r12
movq %rax, %r13
testq %rax, %rax
cmoveq %rcx, %r13
movl 0x28(%r14), %edx
movl 0x84(%r14), %eax
leal 0x2(%rax), %ecx
cmpl %ecx, %edx
jge 0x61302
cmpb $0x0, 0x4e(%r14)
je 0x61317
movq 0x20(%r14), %rsi
testl %edx, %edx
jle 0x612d6
xorl %eax, %eax
leaq 0x14e30(%rip), %rcx     # 0x760b0
movq %rax, %rdi
shlq $0x5, %rdi
movq (%rsi,%rdi), %r8
movb (%r8), %r9b
testb %r9b, %r9b
je 0x612bb
incq %r8
movq %r13, %rdi
movzbl %r9b, %r9d
movb (%r9,%rcx), %r10b
movzbl (%rdi), %r11d
cmpb (%r11,%rcx), %r10b
jne 0x612c1
incq %rdi
movb (%r8), %r9b
incq %r8
testb %r9b, %r9b
jne 0x61299
jmp 0x612be
movq %r13, %rdi
xorl %r9d, %r9d
movb (%r9,%rcx), %r8b
movzbl (%rdi), %edi
cmpb (%rdi,%rcx), %r8b
je 0x6132d
incq %rax
cmpq %rdx, %rax
jne 0x61280
leaq 0x2c0(%r14), %rax
cmpq %rax, %rsi
je 0x613a0
shll $0x5, %edx
addl $0x20, %edx
movq %r14, %rdi
callq 0x174d6
testq %rax, %rax
jne 0x613d4
jmp 0x61391
leaq 0x1b35b(%rip), %rsi     # 0x7c664
movq %r14, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0x11900
jmp 0x61344
leaq 0x1b36b(%rip), %rsi     # 0x7c689
xorl %r15d, %r15d
movq %r14, %rdi
xorl %eax, %eax
callq 0x11900
jmp 0x61344
leaq 0x1b37f(%rip), %rsi     # 0x7c6b3
xorl %r15d, %r15d
movq %r14, %rdi
movq %r13, %rdx
xorl %eax, %eax
callq 0x11900
movq %rax, -0x30(%rbp)
testq %rax, %rax
je 0x61381
movl $0x1, 0x5c(%rbx)
movb $0x1, 0x61(%rbx)
movq -0x50(%rbp), %rdi
movq %rax, %rsi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
movl $0x1, %ecx
movq $-0x1, %r8
callq 0xf0d7
movq -0x30(%rbp), %rsi
movq %r14, %rdi
callq 0x9ca4
testl %r15d, %r15d
je 0x61391
movq %rbx, %rdi
movl %r15d, %esi
callq 0xf4b8
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r14, %rdi
movl $0x60, %esi
callq 0x11721
testq %rax, %rax
je 0x61391
movq 0x20(%r14), %rcx
movups (%rcx), %xmm0
movups 0x10(%rcx), %xmm1
movups 0x20(%rcx), %xmm2
movups 0x30(%rcx), %xmm3
movups %xmm3, 0x30(%rax)
movups %xmm2, 0x20(%rax)
movups %xmm1, 0x10(%rax)
movups %xmm0, (%rax)
movq %rax, -0x38(%rbp)
movq %rax, 0x20(%r14)
movslq 0x28(%r14), %r15
shlq $0x5, %r15
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%rax,%r15)
movups %xmm0, (%rax,%r15)
movl 0x40(%r14), %ecx
leaq -0x44(%rbp), %rdx
movl %ecx, (%rdx)
movq (%r14), %rcx
movq 0x18(%rcx), %rdi
leaq -0x68(%rbp), %rcx
leaq -0x60(%rbp), %r8
leaq -0x58(%rbp), %r9
movq %r12, %rsi
callq 0x618a6
testl %eax, %eax
je 0x61467
cmpl $0x7, %eax
jne 0x61425
movb $0x1, 0x50(%r14)
movq -0x58(%rbp), %r14
movl $0x1, 0x5c(%rbx)
movb $0x1, 0x61(%rbx)
movq -0x50(%rbp), %rdi
movq %r14, %rsi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
movl $0x1, %ecx
movq $-0x1, %r8
callq 0xf0d7
movq %r14, %rdi
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xa3c7
movq -0x38(%rbp), %rax
addq %r15, %rax
movl $0x100, %r9d            # imm = 0x100
orl -0x44(%rbp), %r9d
movl %r9d, -0x44(%rbp)
movq -0x68(%rbp), %rdi
movq -0x60(%rbp), %r15
movq %rax, -0x38(%rbp)
leaq 0x8(%rax), %rcx
movq %r15, %rsi
movq %r14, %rdx
movq %rcx, -0x40(%rbp)
xorl %r8d, %r8d
callq 0x195fb
movq %r15, %rdi
movl %eax, %r15d
callq 0xa3c7
incl 0x28(%r14)
testl %r15d, %r15d
je 0x614dc
cmpl $0x13, %r15d
jne 0x615ae
leaq 0x1b20e(%rip), %rsi     # 0x7c6d1
movq %r14, %rdi
xorl %eax, %eax
callq 0x11900
movq %rax, -0x30(%rbp)
movl $0x1, %r15d
jmp 0x615ae
movq -0x38(%rbp), %r15
movq 0x8(%r15), %rsi
movq %r14, %rdi
callq 0x61e7c
movq %rax, 0x18(%r15)
testq %rax, %rax
je 0x61533
xorl %r15d, %r15d
cmpb $0x0, 0x70(%rax)
movq -0x40(%rbp), %rdx
je 0x6153d
movb 0x71(%rax), %al
movq 0x20(%r14), %rcx
movq 0x18(%rcx), %rcx
cmpb 0x71(%rcx), %al
je 0x6153d
leaq 0x19502(%rip), %rsi     # 0x7aa1b
movq %r14, %rdi
xorl %eax, %eax
callq 0x11900
movq -0x40(%rbp), %rdx
movq %rax, -0x30(%rbp)
movl $0x1, %r15d
jmp 0x6153d
movl $0x7, %r15d
movq -0x40(%rbp), %rdx
movq (%rdx), %rdi
movq 0x8(%rdi), %rax
movq (%rax), %rax
cmpb $0x0, 0x10(%rax)
jne 0x61566
movb 0x51(%r14), %cl
movq 0x118(%rax), %rdx
testq %rdx, %rdx
je 0x61563
cmpb $0x2, 0x3f(%rdx)
je 0x61566
movb %cl, 0x8(%rax)
movq 0x20(%r14), %rax
movq 0x8(%rax), %rax
testq %rax, %rax
je 0x6158a
movq (%rax), %rcx
movq 0x8(%rax), %rax
movq %rcx, 0x8(%rax)
movzwl 0x26(%rax), %esi
shrl $0x2, %esi
andl $0x1, %esi
jmp 0x6158c
xorl %esi, %esi
callq 0x6083f
movq -0x40(%rbp), %rax
movq (%rax), %rax
movl 0x2c(%r14), %edx
andl $0x1c, %edx
orl $0x3, %edx
movq (%rax), %rdi
movq 0x8(%rax), %rsi
callq 0x60f12
movl %r15d, -0x40(%rbp)
movq -0x38(%rbp), %rax
movb $0x3, 0x10(%rax)
xorl %r15d, %r15d
cmpb $0x0, (%r13,%r15)
leaq 0x1(%r15), %r15
jne 0x615bd
decl %r15d
andl $0x3fffffff, %r15d      # imm = 0x3FFFFFFF
incl %r15d
movq %r14, %rdi
movl %r15d, %esi
callq 0x11721
movq %rax, %rcx
testq %rax, %rax
je 0x61602
movl %r15d, %edx
movq %rcx, %r15
movq %rcx, %rdi
movq %r13, %rsi
callq 0x82f0
movq %r15, %rcx
xorl %r15d, %r15d
jmp 0x61608
movl $0x7, %r15d
movq -0x38(%rbp), %rax
movq %rcx, (%rax)
movl -0x40(%rbp), %eax
testl %eax, %eax
cmovnel %eax, %r15d
testl %r15d, %r15d
jne 0x61665
movslq 0x28(%r14), %rax
testq %rax, %rax
jle 0x6164e
movq 0x20(%r14), %rcx
shlq $0x5, %rax
xorl %edx, %edx
movq 0x8(%rcx,%rdx), %rsi
testq %rsi, %rsi
je 0x61645
movq (%rsi), %rdi
movq 0x8(%rsi), %rsi
movq %rdi, 0x8(%rsi)
addq $0x20, %rdx
cmpq %rdx, %rax
jne 0x61630
leaq -0x30(%rbp), %rsi
movq %r14, %rdi
callq 0x368ba
movl %eax, %r15d
testl %eax, %eax
je 0x61391
movslq 0x28(%r14), %r13
decq %r13
movq 0x20(%r14), %rax
movq %r13, -0x38(%rbp)
shlq $0x5, %r13
movq 0x8(%rax,%r13), %rdi
testq %rdi, %rdi
je 0x61697
callq 0x23ec5
movq 0x20(%r14), %rax
xorl %ecx, %ecx
movq %rcx, 0x8(%rax,%r13)
movq %rcx, 0x18(%rax,%r13)
movq %r14, %rdi
callq 0xdea0
movq -0x38(%rbp), %rax
movl %eax, 0x28(%r14)
cmpl $0xc0a, %r15d           # imm = 0xC0A
je 0x616b6
cmpl $0x7, %r15d
jne 0x616d3
movb $0x1, 0x50(%r14)
movq -0x30(%rbp), %rsi
movq %r14, %rdi
callq 0x9ca4
leaq 0x18096(%rip), %rsi     # 0x79764
jmp 0x61321
movq -0x30(%rbp), %rax
testq %rax, %rax
jne 0x6134d
leaq 0x1b007(%rip), %rsi     # 0x7c6ee
movq %r14, %rdi
movq %r12, %rdx
jmp 0x6133d