// File: /workspace/github_c_makefile_star2_O1/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, -0x34(%rbp)
movl %r8d, %r12d
movl %ecx, -0x2c(%rbp)
movl %edx, %ecx
movq %rsi, %rdx
movzbl (%rsi), %eax
leaq 0x60b96(%rip), %rsi     # 0x76060
movzbl (%rax,%rsi), %eax
addl %ecx, %eax
cltq
imulq $-0x4de9bd37, %rax, %rsi # imm = 0xB21642C9
shrq $0x20, %rsi
addl %eax, %esi
movl %esi, %r8d
shrl $0x1f, %r8d
sarl $0x4, %esi
addl %r8d, %esi
leal (%rsi,%rsi,2), %r8d
shll $0x3, %r8d
subl %r8d, %esi
addl %eax, %esi
movq %rdi, -0x48(%rbp)
addq $0x1d8, %rdi            # imm = 0x1D8
movq %rdi, -0x60(%rbp)
movq %rsi, -0x58(%rbp)
movq %rdx, -0x40(%rbp)
movq %rcx, -0x50(%rbp)
callq 0x652db
testq %rax, %rax
movl %r12d, -0x30(%rbp)
je 0x15555
movq %rax, %r14
xorl %r15d, %r15d
movzbl %r12b, %r13d
xorl %r12d, %r12d
movl -0x2c(%rbp), %ebx
movq %r14, %rdi
movl %ebx, %esi
movl %r13d, %edx
callq 0x67aec
cmpl %r12d, %eax
cmovgl %eax, %r12d
cmovgq %r14, %r15
movq 0x10(%r14), %r14
testq %r14, %r14
jne 0x15532
jmp 0x1555b
xorl %r12d, %r12d
xorl %r15d, %r15d
movl -0x34(%rbp), %ecx
testb %cl, %cl
jne 0x155c4
testq %r15, %r15
je 0x15571
movq -0x48(%rbp), %rax
testb $0x20, 0x2e(%rax)
je 0x155c4
leaq 0x80920(%rip), %rdi     # 0x95e98
movq -0x58(%rbp), %rsi
movq -0x40(%rbp), %rdx
movq -0x50(%rbp), %rcx
callq 0x652db
testq %rax, %rax
je 0x155c1
movq %rax, %rbx
xorl %r12d, %r12d
movzbl -0x30(%rbp), %r14d
movl -0x2c(%rbp), %r13d
movq %rbx, %rdi
movl %r13d, %esi
movl %r14d, %edx
callq 0x67aec
cmpl %r12d, %eax
cmovgl %eax, %r12d
cmovgq %rbx, %r15
movq 0x10(%rbx), %rbx
testq %rbx, %rbx
jne 0x1559d
jmp 0x155c4
xorl %r12d, %r12d
movl -0x34(%rbp), %r13d
testb %r13b, %r13b
je 0x1563a
cmpl $0x5, %r12d
jg 0x1563a
movq -0x50(%rbp), %r14
leal 0x49(%r14), %ebx
movq -0x48(%rbp), %rdi
movl %ebx, %esi
callq 0x115e9
testq %rax, %rax
je 0x15637
movq %rax, %r15
movslq %ebx, %rdx
movq %rax, %rdi
xorl %esi, %esi
callq 0x8200
movslq %r14d, %rbx
movq %r15, %rdi
addq $0x48, %rdi
movq %rdi, 0x30(%r15)
movl -0x2c(%rbp), %eax
movw %ax, (%r15)
movl -0x30(%rbp), %eax
movb %al, 0x2(%r15)
movq -0x40(%rbp), %rsi
movq %rbx, %rdx
callq 0x82f0
movb $0x0, 0x48(%r15,%rbx)
movq -0x60(%rbp), %rdi
movq %r15, %rsi
callq 0x651b1
jmp 0x1563a
xorl %r15d, %r15d
testq %r15, %r15
je 0x15652
cmpq $0x0, 0x20(%r15)
jne 0x15655
testb %r13b, %r13b
jne 0x15655
cmpq $0x0, 0x18(%r15)
jne 0x15655
xorl %r15d, %r15d
movq %r15, %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq