// File: /workspace/github_c_makefile_star2_O2/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 %rdi, %r15
movq 0x8(%rdi), %r14
andq $0x0, -0x68(%rbp)
movq %rdx, %rbx
andq $0x0, -0x60(%rbp)
andq $0x0, -0x30(%rbp)
movq (%rdx), %rdi
callq 0xdf1e
movq %rax, %r12
movq 0x8(%rbx), %rdi
callq 0xdf1e
movq %r12, %rsi
testq %r12, %r12
leaq 0x19f18(%rip), %rcx     # 0x70345
cmoveq %rcx, %rsi
movq %rax, %r12
testq %rax, %rax
cmoveq %rcx, %r12
movl 0x28(%r14), %r13d
movl 0x84(%r14), %edx
leal 0x2(%rdx), %eax
cmpl %eax, %r13d
jge 0x564c5
cmpb $0x0, 0x4e(%r14)
je 0x564db
movq %rsi, -0x50(%rbp)
movq %r15, -0x38(%rbp)
xorl %ecx, %ecx
testl %r13d, %r13d
movl $0x0, %r15d
cmovgl %r13d, %r15d
movq %r14, -0x58(%rbp)
movq 0x20(%r14), %rbx
shlq $0x5, %r15
addq $0x20, %r15
leaq 0x20(%rcx), %r14
cmpq %r14, %r15
je 0x56531
movq (%rbx,%rcx), %rdi
movq %r12, %rsi
callq 0xb1bf
movq %r14, %rcx
testl %eax, %eax
jne 0x56480
leaq 0x1822c(%rip), %rsi     # 0x6e6d3
xorl %r13d, %r13d
movq -0x58(%rbp), %r14
movq %r14, %rdi
movq %r12, %rdx
xorl %eax, %eax
callq 0x10594
movq %rax, -0x30(%rbp)
movq -0x38(%rbp), %r15
jmp 0x564f3
leaq 0x181b8(%rip), %rsi     # 0x6e684
xorl %r13d, %r13d
movq %r14, %rdi
xorl %eax, %eax
callq 0x10594
jmp 0x564ef
leaq 0x181c7(%rip), %rsi     # 0x6e6a9
xorl %r13d, %r13d
movq %r14, %rdi
xorl %eax, %eax
callq 0x10594
movq %rax, -0x30(%rbp)
testq %rax, %rax
je 0x56512
pushq $-0x1
popq %rdx
movq %r15, %rdi
movq %rax, %rsi
callq 0xe1ae
movq -0x30(%rbp), %rsi
movq %r14, %rdi
callq 0x968b
testl %r13d, %r13d
je 0x56522
movq %r15, %rdi
movl %r13d, %esi
callq 0xe587
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq -0x58(%rbp), %r14
leaq 0x2c0(%r14), %rax
cmpq %rax, %rbx
je 0x56565
shll $0x5, %r13d
addl $0x20, %r13d
movq %r14, %rdi
movq %rbx, %rsi
movl %r13d, %edx
callq 0x157c3
movq %rax, %r13
testq %rax, %rax
movq -0x38(%rbp), %r15
jne 0x565a3
jmp 0x56522
pushq $0x60
popq %rsi
movq %r14, %rdi
callq 0x103b4
testq %rax, %rax
movq -0x38(%rbp), %r15
je 0x56522
movq %rax, %r13
movq 0x20(%r14), %rax
movups (%rax), %xmm0
movups 0x10(%rax), %xmm1
movups 0x20(%rax), %xmm2
movups 0x30(%rax), %xmm3
movups %xmm3, 0x30(%r13)
movups %xmm2, 0x20(%r13)
movups %xmm1, 0x10(%r13)
movups %xmm0, (%r13)
movq %r13, 0x20(%r14)
movslq 0x28(%r14), %rbx
shlq $0x5, %rbx
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%r13,%rbx)
movups %xmm0, (%r13,%rbx)
movl 0x40(%r14), %eax
leaq -0x3c(%rbp), %rdx
movl %eax, (%rdx)
movq (%r14), %rax
movq 0x18(%rax), %rdi
leaq -0x70(%rbp), %rcx
leaq -0x68(%rbp), %r8
leaq -0x60(%rbp), %r9
movq -0x50(%rbp), %rsi
callq 0x56990
testl %eax, %eax
je 0x5661a
cmpl $0x7, %eax
jne 0x565f2
movb $0x1, 0x50(%r14)
movq -0x60(%rbp), %rbx
pushq $-0x1
popq %rdx
movq %r15, %rdi
movq %rbx, %rsi
callq 0xe1ae
movq %rbx, %rdi
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x9c7b
addq %rbx, %r13
movl $0x100, %r9d            # imm = 0x100
orl -0x3c(%rbp), %r9d
movl %r9d, -0x3c(%rbp)
movq -0x70(%rbp), %rdi
movq -0x68(%rbp), %rbx
leaq 0x8(%r13), %rcx
movq %rbx, %rsi
movq %r14, %rdx
movq %rcx, -0x48(%rbp)
xorl %r8d, %r8d
callq 0x175b9
movl %eax, -0x38(%rbp)
movq %rbx, %rdi
callq 0x9c7b
movl -0x38(%rbp), %eax
incl 0x28(%r14)
pushq $0x7
popq %rbx
testl %eax, %eax
je 0x5668b
cmpl $0x13, %eax
jne 0x5672a
leaq 0x1807f(%rip), %rsi     # 0x6e6f1
movq %r14, %rdi
xorl %eax, %eax
callq 0x10594
movq %rax, -0x30(%rbp)
pushq $0x1
popq %rax
movl %eax, -0x38(%rbp)
jmp 0x5672a
movq 0x8(%r13), %rsi
movq %r14, %rdi
callq 0x56e89
movq %rax, 0x18(%r13)
testq %rax, %rax
movq -0x48(%rbp), %rdx
je 0x566da
xorl %ebx, %ebx
cmpb $0x0, 0x70(%rax)
je 0x566da
movb 0x71(%rax), %al
movq 0x20(%r14), %rcx
movq 0x18(%rcx), %rcx
cmpb 0x71(%rcx), %al
je 0x566da
leaq 0x16378(%rip), %rsi     # 0x6ca3b
movq %r14, %rdi
xorl %eax, %eax
movq %rdx, %rbx
callq 0x10594
movq %rbx, %rdx
movq %rax, -0x30(%rbp)
pushq $0x1
popq %rbx
movl %ebx, -0x38(%rbp)
movq (%rdx), %rax
movq 0x8(%rax), %rax
movq (%rax), %rdi
movzbl 0x51(%r14), %esi
movq %rdx, %rbx
callq 0x56016
movq (%rbx), %rbx
movq 0x20(%r14), %rax
movq 0x8(%rax), %rdi
pushq $-0x1
popq %rsi
callq 0x55fd7
movq %rbx, %rdi
pushq $0x7
popq %rbx
movl %eax, %esi
callq 0x55fd7
movq -0x48(%rbp), %rax
movq (%rax), %rdi
movl 0x2c(%r14), %esi
andl $0x1c, %esi
orl $0x3, %esi
callq 0x561b8
movb $0x3, 0x10(%r13)
movq %r14, %rdi
movq %r12, %rsi
callq 0xf173
movq %rax, (%r13)
xorl %r13d, %r13d
testq %rax, %rax
cmovel %ebx, %r13d
movl -0x38(%rbp), %eax
testl %eax, %eax
cmovnel %eax, %r13d
testl %r13d, %r13d
jne 0x56775
movq %r14, %rdi
callq 0x933b
leaq -0x30(%rbp), %rsi
movq %r14, %rdi
callq 0x31276
movl %eax, %r13d
testl %eax, %eax
je 0x56522
movslq 0x28(%r14), %rbx
decq %rbx
movq 0x20(%r14), %rax
movq %r14, %r12
movq %rbx, %r14
shlq $0x5, %r14
movq 0x8(%rax,%r14), %rdi
testq %rdi, %rdi
je 0x567aa
callq 0x20ae6
movq 0x20(%r12), %rax
andq $0x0, 0x8(%rax,%r14)
andq $0x0, 0x18(%rax,%r14)
movq %r12, %rdi
callq 0xd023
movl %ebx, 0x28(%r12)
cmpl $0xc0a, %r13d           # imm = 0xC0A
movq %r12, %r14
je 0x567c9
cmpl $0x7, %r13d
jne 0x567e6
movb $0x1, 0x50(%r14)
movq -0x30(%rbp), %rsi
movq %r14, %rdi
callq 0x968b
leaq 0x14fa3(%rip), %rsi     # 0x6b784
jmp 0x564e5
movq -0x30(%rbp), %rax
testq %rax, %rax
jne 0x564f8
leaq 0x17f14(%rip), %rsi     # 0x6e70e
movq %r14, %rdi
movq -0x50(%rbp), %rdx
xorl %eax, %eax
callq 0x10594
jmp 0x564ef