// File: /workspace/github_c_makefile_star2_O1/barabo[P]advanced-shell-history/src/sqlite3.c
// Function: renameTriggerFunc
static void renameTriggerFunc(
  sqlite3_context *context,
  int NotUsed,
  sqlite3_value **argv
){
  unsigned char const *zSql = sqlite3_value_text(argv[0]);
  unsigned char const *zTableName = sqlite3_value_text(argv[1]);

  int token;
  Token tname;
  int dist = 3;
  unsigned char const *zCsr = zSql;
  int len = 0;
  char *zRet;
  sqlite3 *db = sqlite3_context_db_handle(context);

  UNUSED_PARAMETER(NotUsed);

  /* The principle used to locate the table name in the CREATE TRIGGER 
  ** statement is that the table name is the first token that is immediatedly
  ** preceded by either TK_ON or TK_DOT and immediatedly followed by one
  ** of TK_WHEN, TK_BEGIN or TK_FOR.
  */
  if( zSql ){
    do {

      if( !*zCsr ){
        /* Ran out of input before finding the table name. Return NULL. */
        return;
      }

      /* Store the token that zCsr points to in tname. */
      tname.z = (char*)zCsr;
      tname.n = len;

      /* Advance zCsr to the next token. Store that token type in 'token',
      ** and its length in 'len' (to be used next iteration of this loop).
      */
      do {
        zCsr += len;
        len = sqlite3GetToken(zCsr, &token);
      }while( token==TK_SPACE );
      assert( len>0 );

      /* Variable 'dist' stores the number of tokens read since the most
      ** recent TK_DOT or TK_ON. This means that when a WHEN, FOR or BEGIN 
      ** token is read and 'dist' equals 2, the condition stated above
      ** to be met.
      **
      ** Note that ON cannot be a database, table or column name, so
      ** there is no need to worry about syntax like 
      ** "CREATE TRIGGER ... ON ON.ON BEGIN ..." etc.
      */
      dist++;
      if( token==TK_DOT || token==TK_ON ){
        dist = 0;
      }
    } while( dist!=2 || (token!=TK_WHEN && token!=TK_FOR && token!=TK_BEGIN) );

    /* Variable tname now contains the token that is the old table-name
    ** in the CREATE TRIGGER statement.
    */
    zRet = sqlite3MPrintf(db, "%.*s\"%w\"%s", ((u8*)tname.z) - zSql, zSql, 
       zTableName, tname.z+tname.n);
    sqlite3_result_text(context, zRet, -1, SQLITE_DYNAMIC);
  }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rdx, %r14
movq %rdi, -0x40(%rbp)
movq (%rdx), %rdi
movl $0x1, %esi
callq 0xee49
movq %rax, %rbx
movq 0x8(%r14), %rdi
movl $0x1, %esi
callq 0xee49
movq %rax, -0x50(%rbp)
movq %rbx, -0x30(%rbp)
testq %rbx, %rbx
je 0x66f88
movq -0x30(%rbp), %rax
cmpb $0x0, (%rax)
je 0x66f88
movq -0x40(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x48(%rbp)
movl $0x3, %r12d
leaq -0x34(%rbp), %r13
movq -0x30(%rbp), %r14
xorl %eax, %eax
movl %eax, %r15d
movq %r14, %rbx
cltq
addq %rax, %r14
movq %r14, %rdi
movq %r13, %rsi
callq 0x3590c
movl -0x34(%rbp), %ecx
cmpl $0x95, %ecx
je 0x66f33
incl %r12d
cmpl $0x68, %ecx
movl $0x0, %edx
cmovel %edx, %r12d
cmpl $0x76, %ecx
cmovel %edx, %r12d
cmpl $0x2, %r12d
jne 0x66f82
cmpl $0x5, %ecx
je 0x66f97
cmpl $0x87, %ecx
je 0x66f97
movl $0x2, %r12d
cmpl $0x2d, %ecx
je 0x66f97
cmpb $0x0, (%r14)
jne 0x66f2d
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rbx, %rdx
movq -0x30(%rbp), %rcx
subq %rcx, %rdx
movl %r15d, %r9d
addq %rbx, %r9
leaq 0x1603c(%rip), %rsi     # 0x7cfea
movq -0x48(%rbp), %rdi
movq -0x50(%rbp), %r8
xorl %eax, %eax
callq 0x117e2
leaq -0x5d6f0(%rip), %r8     # 0x98d4
movq -0x40(%rbp), %rdi
movq %rax, %rsi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
movl $0x1, %ecx
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xef1b