// File: /workspace/github_c_makefile_star2_O3/barabo[P]advanced-shell-history/src/sqlite3.c
// Function: replaceFunc
static void replaceFunc(
  sqlite3_context *context,
  int argc,
  sqlite3_value **argv
){
  const unsigned char *zStr;        /* The input string A */
  const unsigned char *zPattern;    /* The pattern string B */
  const unsigned char *zRep;        /* The replacement string C */
  unsigned char *zOut;              /* The output */
  int nStr;                /* Size of zStr */
  int nPattern;            /* Size of zPattern */
  int nRep;                /* Size of zRep */
  i64 nOut;                /* Maximum size of zOut */
  int loopLimit;           /* Last zStr[] that might match zPattern[] */
  int i, j;                /* Loop counters */

  assert( argc==3 );
  UNUSED_PARAMETER(argc);
  zStr = sqlite3_value_text(argv[0]);
  if( zStr==0 ) return;
  nStr = sqlite3_value_bytes(argv[0]);
  assert( zStr==sqlite3_value_text(argv[0]) );  /* No encoding change */
  zPattern = sqlite3_value_text(argv[1]);
  if( zPattern==0 ){
    assert( sqlite3_value_type(argv[1])==SQLITE_NULL
            || sqlite3_context_db_handle(context)->mallocFailed );
    return;
  }
  if( zPattern[0]==0 ){
    assert( sqlite3_value_type(argv[1])!=SQLITE_NULL );
    sqlite3_result_value(context, argv[0]);
    return;
  }
  nPattern = sqlite3_value_bytes(argv[1]);
  assert( zPattern==sqlite3_value_text(argv[1]) );  /* No encoding change */
  zRep = sqlite3_value_text(argv[2]);
  if( zRep==0 ) return;
  nRep = sqlite3_value_bytes(argv[2]);
  assert( zRep==sqlite3_value_text(argv[2]) );
  nOut = nStr + 1;
  assert( nOut<SQLITE_MAX_LENGTH );
  zOut = contextMalloc(context, (i64)nOut);
  if( zOut==0 ){
    return;
  }
  loopLimit = nStr - nPattern;  
  for(i=j=0; i<=loopLimit; i++){
    if( zStr[i]!=zPattern[0] || memcmp(&zStr[i], zPattern, nPattern) ){
      zOut[j++] = zStr[i];
    }else{
      u8 *zOld;
      sqlite3 *db = sqlite3_context_db_handle(context);
      nOut += nRep - nPattern;
      testcase( nOut-1==db->aLimit[SQLITE_LIMIT_LENGTH] );
      testcase( nOut-2==db->aLimit[SQLITE_LIMIT_LENGTH] );
      if( nOut-1>db->aLimit[SQLITE_LIMIT_LENGTH] ){
        sqlite3_result_error_toobig(context);
        sqlite3_free(zOut);
        return;
      }
      zOld = zOut;
      zOut = sqlite3_realloc(zOut, (int)nOut);
      if( zOut==0 ){
        sqlite3_result_error_nomem(context);
        sqlite3_free(zOld);
        return;
      }
      memcpy(&zOut[j], zRep, nRep);
      j += nRep;
      i += nPattern-1;
    }
  }
  assert( j+nStr-i+1==nOut );
  memcpy(&zOut[j], &zStr[i], nStr-i);
  j += nStr - i;
  assert( j<=nOut );
  zOut[j] = 0;
  sqlite3_result_text(context, (char*)zOut, j, sqlite3_free);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %rdx, %r15
movq %rdi, %r13
movq (%rdx), %rdi
movl $0x1, %esi
callq 0xef29
testq %rax, %rax
je 0x64a47
movq %rax, %r14
movq (%r15), %rdi
movl $0x1, %esi
callq 0xee02
movq %rax, -0x58(%rbp)
movq 0x8(%r15), %rdi
movl $0x1, %esi
callq 0xef29
testq %rax, %rax
je 0x64a47
movq %rax, %r12
cmpb $0x0, (%rax)
je 0x64a56
movq 0x8(%r15), %rdi
movl $0x1, %esi
callq 0xee02
movl %eax, -0x2c(%rbp)
movq 0x10(%r15), %rdi
movl $0x1, %esi
callq 0xef29
movq %rax, -0x80(%rbp)
testq %rax, %rax
je 0x64a47
movq 0x10(%r15), %rdi
movl $0x1, %esi
callq 0xee02
movl %eax, %ebx
movq -0x58(%rbp), %rax
incl %eax
movslq %eax, %rsi
movq %r13, %rdi
movq %rsi, -0x50(%rbp)
callq 0x65096
movq %rax, -0x38(%rbp)
testq %rax, %rax
je 0x64a47
movq -0x58(%rbp), %rax
movl %eax, %ecx
movl -0x2c(%rbp), %eax
subl %eax, %ecx
movl %ecx, -0x5c(%rbp)
movq %r13, -0x40(%rbp)
js 0x64a73
movslq %eax, %rcx
movq %rcx, -0x88(%rbp)
leaq 0x8(%r13), %rcx
movq %rcx, -0x48(%rbp)
movslq %ebx, %rcx
movq %rcx, -0x78(%rbp)
subl %eax, %ebx
movslq %ebx, %rcx
movq %rcx, -0x68(%rbp)
decl %eax
movl %eax, -0x2c(%rbp)
xorl %r13d, %r13d
xorl %r15d, %r15d
movq %r14, -0x70(%rbp)
movslq %r15d, %rdi
movb (%r14,%rdi), %bl
cmpb (%r12), %bl
jne 0x649ae
addq %r14, %rdi
movq %r12, %rsi
movq -0x88(%rbp), %rdx
callq 0x8410
testl %eax, %eax
je 0x649cd
movslq %r13d, %rax
incl %r13d
movq -0x38(%rbp), %rcx
movb %bl, (%rcx,%rax)
leal 0x1(%r15), %eax
cmpl -0x5c(%rbp), %r15d
movl %eax, %r15d
jl 0x6498b
jmp 0x64a78
movq -0x48(%rbp), %rax
movq (%rax), %rax
movq -0x50(%rbp), %rcx
movq -0x68(%rbp), %rdx
addq %rdx, %rcx
decq %rcx
movslq 0x68(%rax), %rax
cmpq %rax, %rcx
jg 0x64aef
callq 0x9d86
testl %eax, %eax
jne 0x64acd
movq -0x50(%rbp), %rsi
addq -0x68(%rbp), %rsi
movq -0x38(%rbp), %rdi
movq %rsi, -0x50(%rbp)
callq 0xa45c
testq %rax, %rax
je 0x64acd
movq %rax, %rbx
movslq %r13d, %rdi
addq %rax, %rdi
movq -0x80(%rbp), %rsi
movq -0x78(%rbp), %r14
movq %r14, %rdx
callq 0x82f0
addl %r14d, %r13d
movq -0x70(%rbp), %r14
addl -0x2c(%rbp), %r15d
movq %rbx, -0x38(%rbp)
jmp 0x649bb
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq (%r15), %rsi
addq $0x8, %r13
movq %r13, %rdi
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xf417
xorl %eax, %eax
xorl %r13d, %r13d
movslq %r13d, %r15
movq -0x38(%rbp), %r12
leaq (%r12,%r15), %rdi
cltq
addq %rax, %r14
movq -0x58(%rbp), %rcx
subl %eax, %ecx
movslq %ecx, %rbx
movq %r14, %rsi
movq %rbx, %rdx
callq 0x82f0
addl %r15d, %ebx
movslq %ebx, %rdx
movb $0x0, (%r12,%rdx)
leaq -0x5a6e7(%rip), %r8     # 0xa3c7
movq -0x40(%rbp), %rdi
movq %r12, %rsi
movl $0x1, %ecx
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xeffe
movq -0x48(%rbp), %rdi
callq 0xf373
movq -0x40(%rbp), %rcx
movl $0x7, 0x5c(%rcx)
movb $0x1, %al
movb %al, 0x61(%rcx)
movq 0x8(%rcx), %rcx
movb %al, 0x50(%rcx)
jmp 0x64b1b
movq -0x40(%rbp), %rax
movl $0x12, 0x5c(%rax)
movb $0x1, 0x61(%rax)
leaq 0x14971(%rip), %rsi     # 0x79476
movq -0x48(%rbp), %rdi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
movl $0x1, %ecx
xorl %r8d, %r8d
callq 0xf0d7
movq -0x38(%rbp), %rdi
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xa3c7