// File: /workspace/github_c_makefile_star2_O2/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
callq 0xdf1e
movq %rax, %rbx
movq 0x8(%r14), %rdi
callq 0xdf1e
movq %rax, -0x50(%rbp)
movq %rbx, -0x38(%rbp)
testq %rbx, %rbx
je 0x5ae84
movq -0x40(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x48(%rbp)
pushq $0x3
popq %r15
leaq -0x2c(%rbp), %r13
xorl %eax, %eax
movq -0x38(%rbp), %r14
cmpb $0x0, (%r14)
je 0x5ae84
movq %r14, %r12
movl %eax, %ebx
cltq
addq %rax, %r14
movq %r14, %rdi
movq %r13, %rsi
callq 0x30a47
movl -0x2c(%rbp), %ecx
cmpl $0x95, %ecx
je 0x5adf1
incl %r15d
cmpl $0x68, %ecx
movl $0x0, %edx
cmovel %edx, %r15d
cmpl $0x76, %ecx
cmovel %edx, %r15d
cmpl $0x2, %r15d
jne 0x5ade2
cmpl $0x5, %ecx
je 0x5ae3a
cmpl $0x87, %ecx
je 0x5ae3a
cmpl $0x2d, %ecx
jne 0x5ade2
movq %r12, %rdx
movq -0x38(%rbp), %rcx
subq %rcx, %rdx
movl %ebx, %r9d
addq %r12, %r9
leaq 0x141b9(%rip), %rsi     # 0x6f00a
movq -0x48(%rbp), %rdi
movq -0x50(%rbp), %r8
xorl %eax, %eax
callq 0x10594
leaq -0x51af0(%rip), %rcx    # 0x9377
pushq $-0x1
popq %rdx
movq -0x40(%rbp), %rdi
movq %rax, %rsi
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xe48d
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq