// File: /workspace/github_c_makefile_star2_O2/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, %ebx
movl %ecx, -0x2c(%rbp)
movl %edx, %ecx
movq %rsi, %r8
movzbl (%rsi), %eax
leaq 0x545f8(%rip), %rdx     # 0x68070
movzbl (%rax,%rdx), %eax
addl %ecx, %eax
pushq $0x17
popq %rsi
cltd
idivl %esi
movl %edx, %r14d
movq %rdi, -0x48(%rbp)
addq $0x1d8, %rdi            # imm = 0x1D8
movq %rdi, -0x58(%rbp)
movl %edx, %esi
movq %r8, -0x40(%rbp)
movq %r8, %rdx
movq %rcx, -0x50(%rbp)
callq 0x595db
movq %rax, %r13
xorl %r12d, %r12d
movl %ebx, -0x30(%rbp)
movzbl %bl, %r15d
xorl %ebx, %ebx
testq %r13, %r13
je 0x13adb
movq %r13, %rdi
movl -0x2c(%rbp), %esi
movl %r15d, %edx
callq 0x5b84f
cmpl %r12d, %eax
cmovgl %eax, %r12d
cmovgq %r13, %rbx
movq 0x10(%r13), %r13
jmp 0x13ab7
movl -0x34(%rbp), %r13d
testb %r13b, %r13b
je 0x13b4e
testb %r13b, %r13b
setne %al
je 0x13b47
cmpl $0x5, %r12d
jg 0x13b47
movq -0x50(%rbp), %r14
leal 0x49(%r14), %esi
movq -0x48(%rbp), %rdi
callq 0xf4ae
testq %rax, %rax
je 0x13b72
movq %rax, %rbx
movslq %r14d, %r14
movq %rax, %rdi
addq $0x48, %rdi
movq %rdi, 0x30(%rax)
movl -0x2c(%rbp), %eax
movw %ax, (%rbx)
movl -0x30(%rbp), %eax
movb %al, 0x2(%rbx)
movq -0x40(%rbp), %rsi
movq %r14, %rdx
callq 0x82c0
movb $0x0, 0x48(%rbx,%r14)
movq -0x58(%rbp), %rdi
movq %rbx, %rsi
callq 0x59527
movb $0x1, %al
jmp 0x13b5f
testq %rbx, %rbx
jne 0x13b5f
jmp 0x13b72
testq %rbx, %rbx
je 0x13b86
movq -0x48(%rbp), %rax
testb $0x20, 0x2e(%rax)
jne 0x13b86
xorl %eax, %eax
cmpq $0x0, 0x20(%rbx)
jne 0x13b74
cmpq $0x0, 0x18(%rbx)
setne %cl
orb %cl, %al
jne 0x13b74
xorl %ebx, %ebx
movq %rbx, %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x7530b(%rip), %rdi     # 0x88e98
movl %r14d, %esi
movq -0x40(%rbp), %rdx
movq -0x50(%rbp), %rcx
callq 0x595db
movq %rax, %r14
xorl %r12d, %r12d
testq %r14, %r14
je 0x13ae4
movq %r14, %rdi
movl -0x2c(%rbp), %esi
movl %r15d, %edx
callq 0x5b84f
cmpl %r12d, %eax
cmovgl %eax, %r12d
cmovgq %r14, %rbx
movq 0x10(%r14), %r14
jmp 0x13ba3