// File: /workspace/github_c_makefile_star2_O3/barabo[P]advanced-shell-history/src/sqlite3.c
// Function: sqlite3FindFunction
SQLITE_PRIVATE FuncDef *sqlite3FindFunction(
  sqlite3 *db,       /* An open database */
  const char *zName, /* Name of the function.  Not null-terminated */
  int nName,         /* Number of characters in the name */
  int nArg,          /* Number of arguments.  -1 means any number */
  u8 enc,            /* Preferred text encoding */
  u8 createFlag      /* Create new entry if true and does not otherwise exist */
){
  FuncDef *p;         /* Iterator variable */
  FuncDef *pBest = 0; /* Best match found so far */
  int bestScore = 0;  /* Score of best match */
  int h;              /* Hash value */

  assert( nArg>=(-2) );
  assert( nArg>=(-1) || createFlag==0 );
  assert( enc==SQLITE_UTF8 || enc==SQLITE_UTF16LE || enc==SQLITE_UTF16BE );
  h = (sqlite3UpperToLower[(u8)zName[0]] + nName) % ArraySize(db->aFunc.a);

  /* First search for a match amongst the application-defined functions.
  */
  p = functionSearch(&db->aFunc, h, zName, nName);
  while( p ){
    int score = matchQuality(p, nArg, enc);
    if( score>bestScore ){
      pBest = p;
      bestScore = score;
    }
    p = p->pNext;
  }

  /* If no match is found, search the built-in functions.
  **
  ** If the SQLITE_PreferBuiltin flag is set, then search the built-in
  ** functions even if a prior app-defined function was found.  And give
  ** priority to built-in functions.
  **
  ** Except, if createFlag is true, that means that we are trying to
  ** install a new function.  Whatever FuncDef structure is returned it will
  ** have fields overwritten with new information appropriate for the
  ** new function.  But the FuncDefs for built-in functions are read-only.
  ** So we must not search for built-ins when creating a new function.
  */ 
  if( !createFlag && (pBest==0 || (db->flags & SQLITE_PreferBuiltin)!=0) ){
    FuncDefHash *pHash = &GLOBAL(FuncDefHash, sqlite3GlobalFunctions);
    bestScore = 0;
    p = functionSearch(pHash, h, zName, nName);
    while( p ){
      int score = matchQuality(p, nArg, enc);
      if( score>bestScore ){
        pBest = p;
        bestScore = score;
      }
      p = p->pNext;
    }
  }

  /* If the createFlag parameter is true and the search did not reveal an
  ** exact match for the name, number of arguments and encoding, then add a
  ** new entry to the hash table and return it.
  */
  if( createFlag && bestScore<FUNC_PERFECT_MATCH && 
      (pBest = sqlite3DbMallocZero(db, sizeof(*pBest)+nName+1))!=0 ){
    pBest->zName = (char *)&pBest[1];
    pBest->nArg = (u16)nArg;
    pBest->iPrefEnc = enc;
    memcpy(pBest->zName, zName, nName);
    pBest->zName[nName] = 0;
    sqlite3FuncDefInsert(&db->aFunc, pBest);
  }

  if( pBest && (pBest->xStep || pBest->xFunc || createFlag) ){
    return pBest;
  }
  return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movl %r9d, %r15d
movl %r8d, %r12d
movl %ecx, -0x2c(%rbp)
movl %edx, %ecx
movq %rsi, %rbx
movzbl (%rsi), %eax
leaq 0x60bf4(%rip), %rdx     # 0x760b0
movzbl (%rax,%rdx), %eax
addl %ecx, %eax
cltq
imulq $-0x4de9bd37, %rax, %r13 # imm = 0xB21642C9
shrq $0x20, %r13
addl %eax, %r13d
movl %r13d, %edx
shrl $0x1f, %edx
sarl $0x4, %r13d
addl %edx, %r13d
leal (%r13,%r13,2), %edx
shll $0x3, %edx
subl %edx, %r13d
addl %eax, %r13d
movq %rdi, -0x48(%rbp)
addq $0x1d8, %rdi            # imm = 0x1D8
movq %rdi, -0x60(%rbp)
movl %r13d, %esi
movq %rbx, %rdx
movq %rcx, -0x58(%rbp)
callq 0x6511b
testq %rax, %rax
movl %r15d, -0x34(%rbp)
movq %rbx, -0x50(%rbp)
movl %r12d, -0x30(%rbp)
je 0x15576
movq %rax, %r14
movq %r13, -0x40(%rbp)
xorl %ebx, %ebx
movzbl %r12b, %r13d
xorl %r12d, %r12d
movl -0x2c(%rbp), %r15d
movq %r14, %rdi
movl %r15d, %esi
movl %r13d, %edx
callq 0x678ef
cmpl %r12d, %eax
cmovgl %eax, %r12d
cmovgq %r14, %rbx
movq 0x10(%r14), %r14
testq %r14, %r14
jne 0x15530
cmpb $0x0, -0x34(%rbp)
jne 0x155dc
testq %rbx, %rbx
je 0x15585
movq -0x48(%rbp), %rax
testb $0x20, 0x2e(%rax)
movq -0x40(%rbp), %r13
jne 0x1558b
xorl %eax, %eax
jmp 0x15657
xorl %ebx, %ebx
movl $0x0, %r12d
testb %r15b, %r15b
jne 0x155dc
jmp 0x1558b
xorl %ebx, %ebx
movq -0x40(%rbp), %r13
leaq 0x80906(%rip), %rdi     # 0x95e98
movl %r13d, %esi
movq -0x50(%rbp), %rdx
movq -0x58(%rbp), %rcx
callq 0x6511b
xorl %r12d, %r12d
testq %rax, %rax
je 0x15650
movq %rax, %r14
movzbl -0x30(%rbp), %r15d
movl -0x2c(%rbp), %r13d
movq %r14, %rdi
movl %r13d, %esi
movl %r15d, %edx
callq 0x678ef
cmpl %r12d, %eax
cmovgl %eax, %r12d
cmovgq %r14, %rbx
movq 0x10(%r14), %r14
testq %r14, %r14
jne 0x155ba
cmpb $0x0, -0x34(%rbp)
setne %al
je 0x15652
cmpl $0x5, %r12d
jg 0x15652
movq -0x58(%rbp), %r15
leal 0x49(%r15), %r14d
movq -0x48(%rbp), %rdi
movl %r14d, %esi
callq 0x11721
testq %rax, %rax
je 0x1566a
movq %rax, %rbx
movslq %r14d, %rdx
movq %rax, %rdi
xorl %esi, %esi
callq 0x8200
movslq %r15d, %r14
movq %rbx, %rdi
addq $0x48, %rdi
movq %rdi, 0x30(%rbx)
movl -0x2c(%rbp), %eax
movw %ax, (%rbx)
movl -0x30(%rbp), %eax
movb %al, 0x2(%rbx)
movq -0x50(%rbp), %rsi
movq %r14, %rdx
callq 0x82f0
movb $0x0, 0x48(%rbx,%r14)
movq -0x60(%rbp), %rdi
movq %rbx, %rsi
callq 0x64ff1
movb $0x1, %al
jmp 0x15657
xorl %eax, %eax
testq %rbx, %rbx
je 0x1566a
cmpq $0x0, 0x20(%rbx)
jne 0x1566c
cmpq $0x0, 0x18(%rbx)
setne %cl
orb %cl, %al
jne 0x1566c
xorl %ebx, %ebx
movq %rbx, %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq