// File: /workspace/github_c_makefile_star2_O1/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, -0x58(%rbp)
movq 0x8(%rdi), %r14
xorl %r15d, %r15d
movq %r15, -0x50(%rbp)
movq %r15, -0x60(%rbp)
movq %r15, -0x30(%rbp)
movq (%rdx), %rdi
movl $0x1, %esi
callq 0xee49
movq %rax, %r12
movq 0x8(%r13), %rdi
movl $0x1, %esi
callq 0xee49
testq %r12, %r12
leaq 0x1d02c(%rip), %rcx     # 0x7e360
cmoveq %rcx, %r12
movq %rax, %r13
testq %rax, %rax
cmoveq %rcx, %r13
movl 0x84(%r14), %edx
leal 0x2(%rdx), %eax
cmpl %eax, 0x28(%r14)
jge 0x613ff
cmpb $0x0, 0x4e(%r14)
je 0x61412
movslq 0x28(%r14), %rdx
testq %rdx, %rdx
jle 0x613cf
movq 0x20(%r14), %rax
xorl %ecx, %ecx
leaq 0x14ce9(%rip), %rsi     # 0x76060
movq %rcx, %rdi
shlq $0x5, %rdi
movq (%rax,%rdi), %r9
movb (%r9), %dil
testb %dil, %dil
je 0x613b2
incq %r9
movq %r13, %r8
movzbl %dil, %r10d
movb (%r10,%rsi), %r10b
movzbl (%r8), %r11d
cmpb (%r11,%rsi), %r10b
jne 0x613b5
incq %r8
movb (%r9), %dil
incq %r9
testb %dil, %dil
jne 0x61390
jmp 0x613b5
movq %r13, %r8
movzbl %dil, %edi
movb (%rdi,%rsi), %dil
movzbl (%r8), %r8d
cmpb (%r8,%rsi), %dil
je 0x61428
incq %rcx
cmpq %rdx, %rcx
jne 0x61377
movq 0x20(%r14), %rsi
leaq 0x2c0(%r14), %rax
cmpq %rax, %rsi
je 0x6149c
shll $0x5, %edx
addl $0x20, %edx
movq %r14, %rdi
callq 0x174d4
testq %rax, %rax
jne 0x614d0
jmp 0x6148d
leaq 0x1b25e(%rip), %rsi     # 0x7c664
movq %r14, %rdi
xorl %eax, %eax
callq 0x117e2
jmp 0x6143f
leaq 0x1b270(%rip), %rsi     # 0x7c689
xorl %r15d, %r15d
movq %r14, %rdi
xorl %eax, %eax
callq 0x117e2
jmp 0x6143f
leaq 0x1b284(%rip), %rsi     # 0x7c6b3
xorl %r15d, %r15d
movq %r14, %rdi
movq %r13, %rdx
xorl %eax, %eax
callq 0x117e2
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rsi
testq %rsi, %rsi
je 0x6147d
movl $0x1, 0x5c(%rbx)
movb $0x1, 0x61(%rbx)
movq -0x58(%rbp), %rdi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
movl $0x1, %ecx
movq $-0x1, %r8
callq 0xeff4
movq -0x30(%rbp), %rsi
movq %r14, %rdi
callq 0x9c3a
testl %r15d, %r15d
je 0x6148d
movq %rbx, %rdi
movl %r15d, %esi
callq 0xf414
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r14, %rdi
movl $0x60, %esi
callq 0x115e9
testq %rax, %rax
je 0x6148d
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 -0x50(%rbp), %r8
leaq -0x60(%rbp), %r9
movq %r12, %rsi
callq 0x6199f
testl %eax, %eax
je 0x61563
cmpl $0x7, %eax
jne 0x61521
movb $0x1, 0x50(%r14)
movq -0x60(%rbp), %r14
movl $0x1, 0x5c(%rbx)
movb $0x1, 0x61(%rbx)
movq -0x58(%rbp), %rdi
movq %r14, %rsi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
movl $0x1, %ecx
movq $-0x1, %r8
callq 0xeff4
movq %r14, %rdi
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xa2e8
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 -0x50(%rbp), %rsi
movq %rax, -0x38(%rbp)
leaq 0x8(%rax), %rcx
movq %r14, %rdx
movq %rcx, -0x40(%rbp)
xorl %r8d, %r8d
callq 0x1976e
movl %eax, %r15d
movq -0x50(%rbp), %rdi
callq 0xa2e8
incl 0x28(%r14)
testl %r15d, %r15d
je 0x615d6
cmpl $0x13, %r15d
jne 0x616a1
leaq 0x1b114(%rip), %rsi     # 0x7c6d1
movq %r14, %rdi
xorl %eax, %eax
callq 0x117e2
movq %rax, -0x30(%rbp)
movl $0x1, %r15d
jmp 0x616a1
movq -0x38(%rbp), %r15
movq 0x8(%r15), %rsi
movq %r14, %rdi
callq 0x61fda
movq %rax, 0x18(%r15)
testq %rax, %rax
je 0x6162d
xorl %r15d, %r15d
cmpb $0x0, 0x70(%rax)
movq -0x40(%rbp), %rdx
je 0x61637
movb 0x71(%rax), %al
movq 0x20(%r14), %rcx
movq 0x18(%rcx), %rcx
cmpb 0x71(%rcx), %al
je 0x61637
leaq 0x19408(%rip), %rsi     # 0x7aa1b
movq %r14, %rdi
xorl %eax, %eax
callq 0x117e2
movq -0x40(%rbp), %rdx
movq %rax, -0x30(%rbp)
movl $0x1, %r15d
jmp 0x61637
movl $0x7, %r15d
movq -0x40(%rbp), %rdx
movq (%rdx), %rdi
movq 0x8(%rdi), %rax
movq (%rax), %rax
cmpb $0x0, 0x10(%rax)
jne 0x61660
movb 0x51(%r14), %cl
movq 0x118(%rax), %rdx
testq %rdx, %rdx
je 0x6165d
cmpb $0x2, 0x3f(%rdx)
je 0x61660
movb %cl, 0x8(%rax)
movq 0x20(%r14), %rax
movq 0x8(%rax), %rax
testq %rax, %rax
je 0x61684
movq (%rax), %rcx
movq 0x8(%rax), %rax
movq %rcx, 0x8(%rax)
movzwl 0x26(%rax), %esi
shrl $0x2, %esi
andl $0x1, %esi
jmp 0x61686
xorl %esi, %esi
callq 0x6095a
movq -0x40(%rbp), %rax
movq (%rax), %rdi
movl 0x2c(%r14), %esi
andl $0x1c, %esi
orl $0x3, %esi
callq 0x60ff5
movl %r15d, -0x40(%rbp)
movq -0x38(%rbp), %rax
movb $0x3, 0x10(%rax)
xorl %r15d, %r15d
cmpb $0x0, (%r13,%r15)
leaq 0x1(%r15), %r15
jne 0x616b0
decl %r15d
andl $0x3fffffff, %r15d      # imm = 0x3FFFFFFF
incl %r15d
movq %r14, %rdi
movl %r15d, %esi
callq 0x115e9
movq %rax, %rcx
testq %rax, %rax
je 0x616f5
movl %r15d, %edx
movq %rcx, %r15
movq %rcx, %rdi
movq %r13, %rsi
callq 0x82f0
movq %r15, %rcx
xorl %r15d, %r15d
jmp 0x616fb
movl $0x7, %r15d
movq -0x38(%rbp), %rax
movq %rcx, (%rax)
movl -0x40(%rbp), %eax
testl %eax, %eax
cmovnel %eax, %r15d
testl %r15d, %r15d
jne 0x61750
movslq 0x28(%r14), %rax
testq %rax, %rax
jle 0x61741
movq 0x20(%r14), %rcx
shlq $0x5, %rax
xorl %edx, %edx
movq 0x8(%rcx,%rdx), %rsi
testq %rsi, %rsi
je 0x61738
movq (%rsi), %rdi
movq 0x8(%rsi), %rsi
movq %rdi, 0x8(%rsi)
addq $0x20, %rdx
cmpq %rdx, %rax
jne 0x61723
leaq -0x30(%rbp), %rsi
movq %r14, %rdi
callq 0x361a6
movl %eax, %r15d
testl %r15d, %r15d
je 0x6148d
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 0x6178b
callq 0x243ae
movq 0x20(%r14), %rax
xorl %ecx, %ecx
movq %rcx, 0x8(%rax,%r13)
movq %rcx, 0x18(%rax,%r13)
movq %r14, %rdi
callq 0xdda5
movq -0x38(%rbp), %rax
movl %eax, 0x28(%r14)
cmpl $0xc0a, %r15d           # imm = 0xC0A
je 0x617aa
cmpl $0x7, %r15d
jne 0x617c7
movb $0x1, 0x50(%r14)
movq -0x30(%rbp), %rsi
movq %r14, %rdi
callq 0x9c3a
leaq 0x17fa2(%rip), %rsi     # 0x79764
jmp 0x6141c
cmpq $0x0, -0x30(%rbp)
jne 0x61443
leaq 0x1af15(%rip), %rsi     # 0x7c6ee
movq %r14, %rdi
movq %r12, %rdx
jmp 0x61438