// File: /workspace/github_c_makefile_star2_O2/genometools[P]genometools/src/external/sqlite-3.33.0/sqlite3.c
// Function: sqlite3FindFunction
SQLITE_PRIVATE FuncDef *sqlite3FindFunction(
  sqlite3 *db,       /* An open database */
  const char *zName, /* Name of the function.  zero-terminated */
  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 */
  int nName;          /* Length of the name */

  assert( nArg>=(-2) );
  assert( nArg>=(-1) || createFlag==0 );
  nName = sqlite3Strlen30(zName);

  /* First search for a match amongst the application-defined functions.
  */
  p = (FuncDef*)sqlite3HashFind(&db->aFunc, zName);
  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 DBFLAG_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->mDbFlags & DBFLAG_PreferBuiltin)!=0) ){
    bestScore = 0;
    h = SQLITE_FUNC_HASH(sqlite3UpperToLower[(u8)zName[0]], nName);
    p = sqlite3FunctionSearch(h, zName);
    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 ){
    FuncDef *pOther;
    u8 *z;
    pBest->zName = (const char*)&pBest[1];
    pBest->nArg = (u16)nArg;
    pBest->funcFlags = enc;
    memcpy((char*)&pBest[1], zName, nName+1);
    for(z=(u8*)pBest->zName; *z; z++) *z = sqlite3UpperToLower[*z];
    pOther = (FuncDef*)sqlite3HashInsert(&db->aFunc, pBest->zName, pBest);
    if( pOther==pBest ){
      sqlite3DbFree(db, pBest);
      sqlite3OomFault(db);
      return 0;
    }else{
      pBest->pNext = pOther;
    }
  }

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

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movl %r8d, 0xc(%rsp)
movl %ecx, %ebx
movl %edx, %ebp
movq %rsi, %r12
movq %rdi, %r14
movq %rsi, %rdi
callq 0x2211ac
movq %rax, 0x18(%rsp)
movq %r14, 0x10(%rsp)
leaq 0x220(%r14), %rdi
movq %rdi, 0x20(%rsp)
movq %r12, %rsi
callq 0x24a8e6
movq %rax, %r13
xorl %r14d, %r14d
movzbl %bl, %r15d
xorl %ebx, %ebx
testq %r13, %r13
je 0x22ad4e
movq %r13, %rdi
movl %ebp, %esi
movl %r15d, %edx
callq 0x28a864
cmpl %r14d, %eax
cmovgl %eax, %r14d
cmovgq %r13, %rbx
movq 0x10(%r13), %r13
jmp 0x22ad2b
movl 0xc(%rsp), %eax
testb %al, %al
jne 0x22adbf
testq %rbx, %rbx
je 0x22ad6d
movq 0x10(%rsp), %rax
testb $0x2, 0x2c(%rax)
jne 0x22ad6d
xorl %eax, %eax
jmp 0x22ae2a
movzbl (%r12), %eax
leaq 0x146977(%rip), %rcx    # 0x3716f0
movzbl (%rax,%rcx), %eax
addl 0x18(%rsp), %eax
xorl %r14d, %r14d
pushq $0x17
popq %rcx
xorl %edx, %edx
divl %ecx
movl %edx, %edi
movq %r12, %rsi
callq 0x289f26
movq %rax, %r13
testq %r13, %r13
je 0x22adbb
movq %r13, %rdi
movl %ebp, %esi
movl %r15d, %edx
callq 0x28a864
cmpl %r14d, %eax
cmovgl %eax, %r14d
cmovgq %r13, %rbx
movq 0x10(%r13), %r13
jmp 0x22ad98
movl 0xc(%rsp), %eax
testb %al, %al
setne %al
je 0x22ae25
cmpl $0x5, %r14d
jg 0x22ae25
movq 0x18(%rsp), %r14
leal 0x49(%r14), %esi
movq 0x10(%rsp), %rdi
callq 0x2258ed
testq %rax, %rax
je 0x22ae6b
movq %rax, %rbx
movq %r14, %rdx
leaq 0x48(%rax), %r14
movq %r14, 0x38(%rax)
movb %bpl, (%rax)
movl %r15d, 0x4(%rax)
incl %edx
movq %r14, %rdi
movq %r12, %rsi
callq 0x17630
leaq 0x1468df(%rip), %rax    # 0x3716f0
movzbl (%r14), %ecx
testq %rcx, %rcx
je 0x22ae37
movb (%rcx,%rax), %cl
movb %cl, (%r14)
incq %r14
jmp 0x22ae11
testq %rbx, %rbx
je 0x22ae6b
testb %al, %al
jne 0x22ae6d
cmpq $0x0, 0x18(%rbx)
je 0x22ae6b
jmp 0x22ae6d
movq 0x38(%rbx), %rsi
movq 0x20(%rsp), %rdi
movq %rbx, %rdx
callq 0x24a8f4
cmpq %rbx, %rax
je 0x22ae53
movq %rax, 0x10(%rbx)
jmp 0x22ae6d
movq 0x10(%rsp), %r14
movq %r14, %rdi
movq %rbx, %rsi
callq 0x22d0fc
movq %r14, %rdi
callq 0x224bb1
xorl %ebx, %ebx
movq %rbx, %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq