// File: /workspace/github_c_makefile_star2_O2/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 $0x48, %rsp
movl %r8d, (%rsp)
movq %rcx, %r14
movq %rdx, %r15
movq %rsi, %r13
movq %rdi, %r12
movq %rcx, %rdi
callq 0x2211ac
movl %eax, %ebp
movq %r15, 0x28(%rsp)
movq %r15, %rdi
callq 0x2211ac
movl %eax, %ebx
movq %r12, 0x30(%rsp)
movq (%r12), %rax
movq 0x28(%rax), %r12
leaq 0xefeb9(%rip), %rsi     # 0x377fa0
movq %r12, %rdi
movq %r14, %rdx
xorl %eax, %eax
callq 0x226ec3
testq %rax, %rax
je 0x288282
movq %rax, %r15
movq %rax, %rdi
callq 0x2211ac
cmpl $0x0, (%rsp)
cmovnel %eax, %ebp
movl %ebp, 0x14(%rsp)
cmovneq %r15, %r14
movl 0x8(%r13), %ecx
movl %eax, 0x10(%rsp)
imull %eax, %ecx
leal (%rbx,%rcx), %eax
incl %eax
movslq %eax, %rsi
movq %r12, %rdi
callq 0x2258ed
testq %rax, %rax
je 0x288287
movq %r12, 0x18(%rsp)
movq %r15, 0x20(%rsp)
movl %ebx, %edx
movq %rax, 0x8(%rsp)
movq %rax, %rdi
movq 0x28(%rsp), %rsi
callq 0x17630
leaq 0xe96f0(%rip), %r15     # 0x371850
movq %r13, 0x40(%rsp)
movq %r14, 0x38(%rsp)
movq (%r13), %r12
testq %r12, %r12
je 0x288254
movq %r12, %rax
movq 0x18(%rax), %rax
testq %rax, %rax
je 0x288192
movq 0x8(%rax), %rcx
cmpq 0x8(%r12), %rcx
cmovaq %rax, %r12
jmp 0x28817a
movq %rbx, (%rsp)
movq %r13, %rax
movq (%r13), %rdx
leaq 0x18(%rdx), %r13
cmpq %r12, %rdx
jne 0x288196
movq 0x18(%r12), %rcx
movq %rcx, (%rax)
movq 0x8(%r12), %rbx
movzbl (%rbx), %eax
testb $0x46, (%rax,%r15)
movl 0x14(%rsp), %eax
movl %eax, %r13d
cmovel 0x10(%rsp), %r13d
cmoveq 0x20(%rsp), %r14
subq 0x28(%rsp), %rbx
movl 0x10(%r12), %eax
cmpl %r13d, %eax
jne 0x2881e4
movq 0x8(%rsp), %rax
jmp 0x288223
leal (%rbx,%r13), %edi
movq 0x8(%rsp), %r15
addq %r15, %rdi
addl %ebx, %eax
leaq (%rax,%r15), %rsi
movq (%rsp), %rbp
movl %ebp, %edx
subl %eax, %edx
callq 0x17c50
movl %r13d, %eax
subl 0x10(%r12), %eax
addl %eax, %ebp
movq %rbp, (%rsp)
movslq %ebp, %rax
movb $0x0, (%r15,%rax)
movq %r15, %rax
leaq 0xe962d(%rip), %r15     # 0x371850
movslq %ebx, %rdi
addq %rax, %rdi
movl %r13d, %edx
movq %r14, %rsi
callq 0x17630
movq 0x18(%rsp), %rdi
movq %r12, %rsi
callq 0x21f26b
movq 0x38(%rsp), %r14
movq 0x40(%rsp), %r13
movq (%rsp), %rbx
jmp 0x28816a
pushq $-0x1
popq %rdx
pushq $-0x1
popq %rcx
movq 0x30(%rsp), %rdi
movq 0x8(%rsp), %rbx
movq %rbx, %rsi
callq 0x2249c3
movq 0x18(%rsp), %rdi
movq %rbx, %rsi
callq 0x22d0fc
xorl %ebx, %ebx
movq 0x20(%rsp), %r15
jmp 0x28828a
pushq $0x7
popq %rbx
jmp 0x288292
pushq $0x7
popq %rbx
movq %r15, %rdi
callq 0x21f7f9
movl %ebx, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq