// File: /workspace/github_c_makefile_star2_O1/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 0x2f6ac3
movq %r14, %rdi
callq 0x172f0
movq 0x18(%rsp), %rdx
movq %rax, %rbp
andl $0x3fffffff, %ebp       # imm = 0x3FFFFFFF
testq %rdx, %rdx
je 0x2f6ada
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 0xea7e8(%rip), %rsi     # 0x3e12d5
movq %rbx, %rdi
movq %r14, %rdx
xorl %eax, %eax
callq 0x284f95
testq %rax, %rax
je 0x2f6cab
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 0x283569
testq %rax, %rax
je 0x2f6cb2
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 0x2f6c79
movq %r15, 0x40(%rsp)
movq %r13, 0x38(%rsp)
movq %rbp, 0x30(%rsp)
movq 0x18(%r14), %rax
testq %rax, %rax
je 0x2f6baa
movq 0x8(%rax), %rcx
cmpq 0x8(%r14), %rcx
cmovaq %rax, %r14
movq 0x18(%rax), %rcx
movq %rcx, %rax
testq %rcx, %rcx
jne 0x2f6b92
movq %r15, %rax
movq (%r15), %rdx
leaq 0x18(%rdx), %r15
cmpq %r14, %rdx
jne 0x2f6baa
movq 0x18(%r14), %rcx
movq %rcx, (%rax)
movq 0x8(%r14), %r15
movzbl (%r15), %eax
leaq 0xe3ff1(%rip), %rcx     # 0x3dabc0
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 0x2f6bfa
movq 0x10(%rsp), %rax
jmp 0x2f6c42
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
testq %r14, %r14
movq 0x8(%rsp), %rbx
je 0x2f6c68
movq %rbx, %rdi
movq %r14, %rsi
callq 0x28c3e7
movq 0x40(%rsp), %r15
movq (%r15), %r14
testq %r14, %r14
jne 0x2f6b89
movq 0x28(%rsp), %rdi
movq 0x10(%rsp), %r14
movq %r14, %rsi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
movl $0x1, %ecx
movq $-0x1, %r8
callq 0x281e1a
movq %rbx, %rdi
movq %r14, %rsi
callq 0x28c3e7
xorl %ebx, %ebx
jmp 0x2f6cb7
movl $0x7, %ebx
jmp 0x2f6cbf
movl $0x7, %ebx
movq %r13, %rdi
callq 0x27c490
movl %ebx, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq