// File: /workspace/github_c_makefile_star2_O0/barabo[P]advanced-shell-history/src/sqlite3.c
// Function: sqlite3CreateForeignKey
SQLITE_PRIVATE void sqlite3CreateForeignKey(
  Parse *pParse,       /* Parsing context */
  ExprList *pFromCol,  /* Columns in this table that point to other table */
  Token *pTo,          /* Name of the other table */
  ExprList *pToCol,    /* Columns in the other table */
  int flags            /* Conflict resolution algorithms. */
){
  sqlite3 *db = pParse->db;
#ifndef SQLITE_OMIT_FOREIGN_KEY
  FKey *pFKey = 0;
  FKey *pNextTo;
  Table *p = pParse->pNewTable;
  int nByte;
  int i;
  int nCol;
  char *z;

  assert( pTo!=0 );
  if( p==0 || IN_DECLARE_VTAB ) goto fk_end;
  if( pFromCol==0 ){
    int iCol = p->nCol-1;
    if( NEVER(iCol<0) ) goto fk_end;
    if( pToCol && pToCol->nExpr!=1 ){
      sqlite3ErrorMsg(pParse, "foreign key on %s"
         " should reference only one column of table %T",
         p->aCol[iCol].zName, pTo);
      goto fk_end;
    }
    nCol = 1;
  }else if( pToCol && pToCol->nExpr!=pFromCol->nExpr ){
    sqlite3ErrorMsg(pParse,
        "number of columns in foreign key does not match the number of "
        "columns in the referenced table");
    goto fk_end;
  }else{
    nCol = pFromCol->nExpr;
  }
  nByte = sizeof(*pFKey) + (nCol-1)*sizeof(pFKey->aCol[0]) + pTo->n + 1;
  if( pToCol ){
    for(i=0; i<pToCol->nExpr; i++){
      nByte += sqlite3Strlen30(pToCol->a[i].zName) + 1;
    }
  }
  pFKey = sqlite3DbMallocZero(db, nByte );
  if( pFKey==0 ){
    goto fk_end;
  }
  pFKey->pFrom = p;
  pFKey->pNextFrom = p->pFKey;
  z = (char*)&pFKey->aCol[nCol];
  pFKey->zTo = z;
  memcpy(z, pTo->z, pTo->n);
  z[pTo->n] = 0;
  sqlite3Dequote(z);
  z += pTo->n+1;
  pFKey->nCol = nCol;
  if( pFromCol==0 ){
    pFKey->aCol[0].iFrom = p->nCol-1;
  }else{
    for(i=0; i<nCol; i++){
      int j;
      for(j=0; j<p->nCol; j++){
        if( sqlite3StrICmp(p->aCol[j].zName, pFromCol->a[i].zName)==0 ){
          pFKey->aCol[i].iFrom = j;
          break;
        }
      }
      if( j>=p->nCol ){
        sqlite3ErrorMsg(pParse, 
          "unknown column \"%s\" in foreign key definition", 
          pFromCol->a[i].zName);
        goto fk_end;
      }
    }
  }
  if( pToCol ){
    for(i=0; i<nCol; i++){
      int n = sqlite3Strlen30(pToCol->a[i].zName);
      pFKey->aCol[i].zCol = z;
      memcpy(z, pToCol->a[i].zName, n);
      z[n] = 0;
      z += n+1;
    }
  }
  pFKey->isDeferred = 0;
  pFKey->aAction[0] = (u8)(flags & 0xff);            /* ON DELETE action */
  pFKey->aAction[1] = (u8)((flags >> 8 ) & 0xff);    /* ON UPDATE action */

  assert( sqlite3SchemaMutexHeld(db, 0, p->pSchema) );
  pNextTo = (FKey *)sqlite3HashInsert(&p->pSchema->fkeyHash, 
      pFKey->zTo, sqlite3Strlen30(pFKey->zTo), (void *)pFKey
  );
  if( pNextTo==pFKey ){
    db->mallocFailed = 1;
    goto fk_end;
  }
  if( pNextTo ){
    assert( pNextTo->pPrevTo==0 );
    pFKey->pNextTo = pNextTo;
    pNextTo->pPrevTo = pFKey;
  }

  /* Link the foreign key to the table as the last step.
  */
  p->pFKey = pFKey;
  pFKey = 0;

fk_end:
  sqlite3DbFree(db, pFKey);
#endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */
  sqlite3ExprListDelete(db, pFromCol);
  sqlite3ExprListDelete(db, pToCol);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x80, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movl %r8d, -0x24(%rbp)
movq -0x8(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x30(%rbp)
movq $0x0, -0x38(%rbp)
movq -0x8(%rbp), %rax
movq 0x238(%rax), %rax
movq %rax, -0x48(%rbp)
cmpq $0x0, -0x48(%rbp)
je 0x51e38
movq -0x8(%rbp), %rax
movzbl 0x201(%rax), %eax
cmpl $0x0, %eax
je 0x51e3d
jmp 0x5224a
cmpq $0x0, -0x10(%rbp)
jne 0x51ea7
movq -0x48(%rbp), %rax
movswl 0x42(%rax), %eax
subl $0x1, %eax
movl %eax, -0x64(%rbp)
cmpl $0x0, -0x64(%rbp)
jge 0x51e5d
jmp 0x5224a
cmpq $0x0, -0x20(%rbp)
je 0x51e9e
movq -0x20(%rbp), %rax
cmpl $0x1, (%rax)
je 0x51e9e
movq -0x8(%rbp), %rdi
movq -0x48(%rbp), %rax
movq 0x8(%rax), %rax
movslq -0x64(%rbp), %rcx
imulq $0x30, %rcx, %rcx
addq %rcx, %rax
movq (%rax), %rdx
movq -0x18(%rbp), %rcx
leaq 0x5e323(%rip), %rsi     # 0xb01b5
movb $0x0, %al
callq 0x136f0
jmp 0x5224a
movl $0x1, -0x54(%rbp)
jmp 0x51ede
cmpq $0x0, -0x20(%rbp)
je 0x51ed3
movq -0x20(%rbp), %rax
movl (%rax), %eax
movq -0x10(%rbp), %rcx
cmpl (%rcx), %eax
je 0x51ed3
movq -0x8(%rbp), %rdi
leaq 0x5e32d(%rip), %rsi     # 0xb01f4
movb $0x0, %al
callq 0x136f0
jmp 0x5224a
movq -0x10(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x54(%rbp)
jmp 0x51ede
movl -0x54(%rbp), %eax
subl $0x1, %eax
cltq
shlq $0x4, %rax
addq $0x50, %rax
movq -0x18(%rbp), %rcx
movl 0x8(%rcx), %ecx
addq %rcx, %rax
addq $0x1, %rax
movl %eax, -0x4c(%rbp)
cmpq $0x0, -0x20(%rbp)
je 0x51f4a
movl $0x0, -0x50(%rbp)
movl -0x50(%rbp), %eax
movq -0x20(%rbp), %rcx
cmpl (%rcx), %eax
jge 0x51f48
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rax
movslq -0x50(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rdi
callq 0x92f0
addl $0x1, %eax
addl -0x4c(%rbp), %eax
movl %eax, -0x4c(%rbp)
movl -0x50(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x50(%rbp)
jmp 0x51f0d
jmp 0x51f4a
movq -0x30(%rbp), %rdi
movl -0x4c(%rbp), %esi
callq 0x11ca0
movq %rax, -0x38(%rbp)
cmpq $0x0, -0x38(%rbp)
jne 0x51f66
jmp 0x5224a
movq -0x48(%rbp), %rcx
movq -0x38(%rbp), %rax
movq %rcx, (%rax)
movq -0x48(%rbp), %rax
movq 0x20(%rax), %rcx
movq -0x38(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x38(%rbp), %rax
addq $0x40, %rax
movslq -0x54(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq %rax, -0x60(%rbp)
movq -0x60(%rbp), %rcx
movq -0x38(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x60(%rbp), %rdi
movq -0x18(%rbp), %rax
movq (%rax), %rsi
movq -0x18(%rbp), %rax
movl 0x8(%rax), %eax
movl %eax, %edx
callq 0x82f0
movq -0x60(%rbp), %rax
movq -0x18(%rbp), %rcx
movl 0x8(%rcx), %ecx
movb $0x0, (%rax,%rcx)
movq -0x60(%rbp), %rdi
callq 0x5e890
movq -0x18(%rbp), %rax
movl 0x8(%rax), %ecx
addl $0x1, %ecx
movq -0x60(%rbp), %rax
movl %ecx, %ecx
addq %rcx, %rax
movq %rax, -0x60(%rbp)
movl -0x54(%rbp), %ecx
movq -0x38(%rbp), %rax
movl %ecx, 0x28(%rax)
cmpq $0x0, -0x10(%rbp)
jne 0x52014
movq -0x48(%rbp), %rax
movswl 0x42(%rax), %ecx
subl $0x1, %ecx
movq -0x38(%rbp), %rax
movl %ecx, 0x40(%rax)
jmp 0x520ea
movl $0x0, -0x50(%rbp)
movl -0x50(%rbp), %eax
cmpl -0x54(%rbp), %eax
jge 0x520e8
movl $0x0, -0x68(%rbp)
movl -0x68(%rbp), %eax
movq -0x48(%rbp), %rcx
movswl 0x42(%rcx), %ecx
cmpl %ecx, %eax
jge 0x5209b
movq -0x48(%rbp), %rax
movq 0x8(%rax), %rax
movslq -0x68(%rbp), %rcx
imulq $0x30, %rcx, %rcx
addq %rcx, %rax
movq (%rax), %rdi
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movslq -0x50(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rsi
callq 0xc970
cmpl $0x0, %eax
jne 0x5208e
movl -0x68(%rbp), %ecx
movq -0x38(%rbp), %rax
addq $0x40, %rax
movslq -0x50(%rbp), %rdx
shlq $0x4, %rdx
addq %rdx, %rax
movl %ecx, (%rax)
jmp 0x5209b
jmp 0x52090
movl -0x68(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x68(%rbp)
jmp 0x5202e
movl -0x68(%rbp), %eax
movq -0x48(%rbp), %rcx
movswl 0x42(%rcx), %ecx
cmpl %ecx, %eax
jl 0x520d8
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movslq -0x50(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rdx
leaq 0x5e186(%rip), %rsi     # 0xb0252
movb $0x0, %al
callq 0x136f0
jmp 0x5224a
jmp 0x520da
movl -0x50(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x50(%rbp)
jmp 0x5201b
jmp 0x520ea
cmpq $0x0, -0x20(%rbp)
je 0x52196
movl $0x0, -0x50(%rbp)
movl -0x50(%rbp), %eax
cmpl -0x54(%rbp), %eax
jge 0x52194
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rax
movslq -0x50(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rdi
callq 0x92f0
movl %eax, -0x6c(%rbp)
movq -0x60(%rbp), %rcx
movq -0x38(%rbp), %rax
addq $0x40, %rax
movslq -0x50(%rbp), %rdx
shlq $0x4, %rdx
addq %rdx, %rax
movq %rcx, 0x8(%rax)
movq -0x60(%rbp), %rdi
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rax
movslq -0x50(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rsi
movslq -0x6c(%rbp), %rdx
callq 0x82f0
movq -0x60(%rbp), %rax
movslq -0x6c(%rbp), %rcx
movb $0x0, (%rax,%rcx)
movl -0x6c(%rbp), %ecx
addl $0x1, %ecx
movq -0x60(%rbp), %rax
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0x60(%rbp)
movl -0x50(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x50(%rbp)
jmp 0x520fc
jmp 0x52196
movq -0x38(%rbp), %rax
movb $0x0, 0x2c(%rax)
movl -0x24(%rbp), %eax
andl $0xff, %eax
movb %al, %cl
movq -0x38(%rbp), %rax
movb %cl, 0x2d(%rax)
movl -0x24(%rbp), %eax
sarl $0x8, %eax
andl $0xff, %eax
movb %al, %cl
movq -0x38(%rbp), %rax
movb %cl, 0x2e(%rax)
movq -0x48(%rbp), %rax
movq 0x68(%rax), %rax
addq $0x50, %rax
movq %rax, -0x80(%rbp)
movq -0x38(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x78(%rbp)
movq -0x38(%rbp), %rax
movq 0x10(%rax), %rdi
callq 0x92f0
movq -0x80(%rbp), %rdi
movq -0x78(%rbp), %rsi
movl %eax, %edx
movq -0x38(%rbp), %rcx
callq 0x46e00
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rax
cmpq -0x38(%rbp), %rax
jne 0x52217
movq -0x30(%rbp), %rax
movb $0x1, 0x50(%rax)
jmp 0x5224a
cmpq $0x0, -0x40(%rbp)
je 0x52236
movq -0x40(%rbp), %rcx
movq -0x38(%rbp), %rax
movq %rcx, 0x18(%rax)
movq -0x38(%rbp), %rcx
movq -0x40(%rbp), %rax
movq %rcx, 0x20(%rax)
movq -0x38(%rbp), %rcx
movq -0x48(%rbp), %rax
movq %rcx, 0x20(%rax)
movq $0x0, -0x38(%rbp)
movq -0x30(%rbp), %rdi
movq -0x38(%rbp), %rsi
callq 0x9cd0
movq -0x30(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x47460
movq -0x30(%rbp), %rdi
movq -0x20(%rbp), %rsi
callq 0x47460
addq $0x80, %rsp
popq %rbp
retq
nopw (%rax,%rax)