// File: /workspace/github_c_makefile_star2_O3/genometools[P]genometools/src/external/sqlite-3.33.0/sqlite3.c
// Function: renameEditSql
static int renameEditSql(
  sqlite3_context *pCtx,          /* Return result here */
  RenameCtx *pRename,             /* Rename context */
  const char *zSql,               /* SQL statement to edit */
  const char *zNew,               /* New token text */
  int bQuote                      /* True to always quote token */
){
  int nNew = sqlite3Strlen30(zNew);
  int nSql = sqlite3Strlen30(zSql);
  sqlite3 *db = sqlite3_context_db_handle(pCtx);
  int rc = SQLITE_OK;
  char *zQuot;
  char *zOut;
  int nQuot;

  /* Set zQuot to point to a buffer containing a quoted copy of the
  ** identifier zNew. If the corresponding identifier in the original
  ** ALTER TABLE statement was quoted (bQuote==1), then set zNew to
  ** point to zQuot so that all substitutions are made using the
  ** quoted version of the new column name.  */
  zQuot = sqlite3MPrintf(db, "\"%w\"", zNew);
  if( zQuot==0 ){
    return SQLITE_NOMEM;
  }else{
    nQuot = sqlite3Strlen30(zQuot);
  }
  if( bQuote ){
    zNew = zQuot;
    nNew = nQuot;
  }

  /* At this point pRename->pList contains a list of RenameToken objects
  ** corresponding to all tokens in the input SQL that must be replaced
  ** with the new column name. All that remains is to construct and
  ** return the edited SQL string. */
  assert( nQuot>=nNew );
  zOut = sqlite3DbMallocZero(db, nSql + pRename->nList*nQuot + 1);
  if( zOut ){
    int nOut = nSql;
    memcpy(zOut, zSql, nSql);
    while( pRename->pList ){
      int iOff;                   /* Offset of token to replace in zOut */
      RenameToken *pBest = renameColumnTokenNext(pRename);

      u32 nReplace;
      const char *zReplace;
      if( sqlite3IsIdChar(*pBest->t.z) ){
        nReplace = nNew;
        zReplace = zNew;
      }else{
        nReplace = nQuot;
        zReplace = zQuot;
      }

      iOff = pBest->t.z - zSql;
      if( pBest->t.n!=nReplace ){
        memmove(&zOut[iOff + nReplace], &zOut[iOff + pBest->t.n],
            nOut - (iOff + pBest->t.n)
        );
        nOut += nReplace - pBest->t.n;
        zOut[nOut] = '\0';
      }
      memcpy(&zOut[iOff], zReplace, nReplace);
      sqlite3DbFree(db, pBest);
    }

    sqlite3_result_text(pCtx, zOut, -1, SQLITE_TRANSIENT);
    sqlite3DbFree(db, zOut);
  }else{
    rc = SQLITE_NOMEM;
  }

  sqlite3_free(zQuot);
  return rc;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movl %r8d, 0x8(%rsp)
movq %rcx, %r14
movq %rsi, %r15
movq %rdi, %rbx
xorl %r12d, %r12d
movl $0x0, %ebp
testq %rcx, %rcx
movq %rdx, 0x18(%rsp)
je 0x2ed7af
movq %r14, %rdi
callq 0x172f0
movq 0x18(%rsp), %rdx
movq %rax, %rbp
andl $0x3fffffff, %ebp       # imm = 0x3FFFFFFF
testq %rdx, %rdx
je 0x2ed7c6
movq %rdx, %rdi
callq 0x172f0
movq %rax, %r12
andl $0x3fffffff, %r12d      # imm = 0x3FFFFFFF
movq %rbx, 0x28(%rsp)
movq (%rbx), %rax
movq 0x28(%rax), %rbx
leaq 0xea3a7(%rip), %rsi     # 0x3d7b80
movq %rbx, %rdi
movq %r14, %rdx
xorl %eax, %eax
callq 0x27b56a
testq %rax, %rax
je 0x2ed992
movq %rax, %r13
movq %rax, %rdi
callq 0x172f0
andl $0x3fffffff, %eax       # imm = 0x3FFFFFFF
cmpl $0x0, 0x8(%rsp)
cmovnel %eax, %ebp
cmovneq %r13, %r14
movl 0x8(%r15), %ecx
movq %rax, 0x50(%rsp)
imull %eax, %ecx
leal (%r12,%rcx), %eax
incl %eax
movslq %eax, %rsi
movq %rbx, 0x8(%rsp)
movq %rbx, %rdi
callq 0x279b27
testq %rax, %rax
je 0x2ed999
movq %r14, 0x48(%rsp)
movq %r12, 0x20(%rsp)
movl %r12d, %edx
movq %rax, 0x10(%rsp)
movq %rax, %rdi
movq 0x18(%rsp), %rsi
callq 0x17660
movq (%r15), %r14
testq %r14, %r14
movq 0x8(%rsp), %rbx
je 0x2ed960
movq %r15, 0x40(%rsp)
movq %r13, 0x38(%rsp)
movq %rbp, 0x30(%rsp)
movq 0x18(%r14), %rax
testq %rax, %rax
je 0x2ed896
movq 0x8(%rax), %rcx
cmpq 0x8(%r14), %rcx
cmovaq %rax, %r14
movq 0x18(%rax), %rcx
movq %rcx, %rax
testq %rcx, %rcx
jne 0x2ed87e
movq %r15, %rax
movq (%r15), %rdx
leaq 0x18(%rdx), %r15
cmpq %r14, %rdx
jne 0x2ed896
movq 0x18(%r14), %rcx
movq %rcx, (%rax)
movq 0x8(%r14), %r15
movzbl (%r15), %eax
leaq 0xe3b85(%rip), %rcx     # 0x3d1440
testb $0x46, (%rax,%rcx)
movl %ebp, %r12d
cmovel 0x50(%rsp), %r12d
movq 0x48(%rsp), %rbx
cmoveq %r13, %rbx
subq 0x18(%rsp), %r15
movl 0x10(%r14), %eax
cmpl %r12d, %eax
jne 0x2ed8e6
movq 0x10(%rsp), %rax
jmp 0x2ed92e
leal (%r12,%r15), %edi
movq 0x10(%rsp), %r13
addq %r13, %rdi
addl %r15d, %eax
leaq (%rax,%r13), %rsi
movq 0x20(%rsp), %rbp
movl %ebp, %edx
subl %eax, %edx
callq 0x17cd0
movl %r12d, %eax
subl 0x10(%r14), %eax
addl %eax, %ebp
movq %rbp, 0x20(%rsp)
movslq %ebp, %rax
movq 0x30(%rsp), %rbp
movq 0x38(%rsp), %rcx
movb $0x0, (%r13,%rax)
movq %r13, %rax
movq %rcx, %r13
movslq %r15d, %rdi
addq %rax, %rdi
movl %r12d, %edx
movq %rbx, %rsi
callq 0x17660
movq 0x8(%rsp), %rbx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x282904
movq 0x40(%rsp), %r15
movq (%r15), %r14
testq %r14, %r14
jne 0x2ed875
movq 0x28(%rsp), %rdi
movq 0x10(%rsp), %r14
movq %r14, %rsi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
movl $0x1, %ecx
movq $-0x1, %r8
callq 0x278487
movq %rbx, %rdi
movq %r14, %rsi
callq 0x282904
xorl %ebx, %ebx
jmp 0x2ed99e
movl $0x7, %ebx
jmp 0x2ed9c9
movl $0x7, %ebx
cmpl $0x0, 0x1cdcbb(%rip)    # 0x4bb660
je 0x2ed9c0
movq %r13, %rdi
callq *0x1cdce8(%rip)         # 0x4bb698
cltq
subq %rax, 0x1d113f(%rip)    # 0x4beaf8
decq 0x1d1180(%rip)          # 0x4beb40
movq %r13, %rdi
callq *0x1cdcbf(%rip)         # 0x4bb688
movl %ebx, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq