// File: /workspace/github_c_makefile_star2_O1/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 $0x58, %rsp
movq %rdx, %r15
movq %rdi, -0x38(%rbp)
movq (%rdx), %rdi
movl $0x1, %esi
callq 0xee49
testq %rax, %rax
je 0x64c74
movq %rax, %r14
movq (%r15), %rdi
movl $0x1, %esi
callq 0xed28
movq %rax, -0x40(%rbp)
movq 0x8(%r15), %rdi
movl $0x1, %esi
callq 0xee49
testq %rax, %rax
je 0x64c74
movq %rax, %r12
cmpb $0x0, (%rax)
je 0x64c83
movq 0x8(%r15), %rdi
movl $0x1, %esi
callq 0xed28
movl %eax, %ebx
movq 0x10(%r15), %rdi
movl $0x1, %esi
callq 0xee49
movq %rax, -0x68(%rbp)
testq %rax, %rax
je 0x64c74
movq 0x10(%r15), %rdi
movl $0x1, %esi
callq 0xed28
movl %eax, %r13d
movq -0x40(%rbp), %rax
incl %eax
movslq %eax, %rsi
movq -0x38(%rbp), %rdi
movq %rsi, -0x58(%rbp)
callq 0x65256
movq %rax, -0x30(%rbp)
testq %rax, %rax
je 0x64c74
movq -0x40(%rbp), %rax
subl %ebx, %eax
movl %eax, -0x48(%rbp)
js 0x64ca1
movslq %ebx, %rax
movq %rax, -0x78(%rbp)
movq -0x38(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x50(%rbp)
movslq %r13d, %rax
movq %rax, -0x60(%rbp)
subl %ebx, %r13d
movslq %r13d, %rax
movq %rax, -0x70(%rbp)
decl %ebx
movl %ebx, -0x44(%rbp)
xorl %ebx, %ebx
xorl %r13d, %r13d
movslq %r13d, %rdi
movb (%r14,%rdi), %r15b
cmpb (%r12), %r15b
jne 0x64b78
addq %r14, %rdi
movq %r12, %rsi
movq -0x78(%rbp), %rdx
callq 0x8410
testl %eax, %eax
je 0x64b97
movslq %ebx, %rax
incl %ebx
movq -0x30(%rbp), %rcx
movb %r15b, (%rcx,%rax)
leal 0x1(%r13), %eax
cmpl -0x48(%rbp), %r13d
movl %eax, %r13d
jl 0x64b58
jmp 0x64ca5
movq -0x50(%rbp), %rdi
movq (%rdi), %rax
movq -0x58(%rbp), %r15
movq -0x70(%rbp), %rdx
leaq (%r15,%rdx), %rcx
decq %rcx
addq %rdx, %r15
movslq 0x68(%rax), %rax
cmpq %rax, %rcx
movq %r15, -0x58(%rbp)
jle 0x64bf3
movq -0x38(%rbp), %rax
movl $0x12, 0x5c(%rax)
movb $0x1, 0x61(%rax)
xorl %r15d, %r15d
leaq 0x148a0(%rip), %rsi     # 0x79476
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
movl $0x1, %ecx
xorl %r8d, %r8d
callq 0xeff4
movq -0x30(%rbp), %rdi
callq 0xa2e8
jmp 0x64c6b
callq 0x9d1f
testl %eax, %eax
je 0x64c00
xorl %eax, %eax
jmp 0x64c0c
movq -0x30(%rbp), %rdi
movl %r15d, %esi
callq 0xa37d
testq %rax, %rax
je 0x64c37
movslq %ebx, %rdi
addq %rax, %rdi
movq -0x68(%rbp), %rsi
movq -0x60(%rbp), %r15
movq %r15, %rdx
movq %rax, -0x30(%rbp)
callq 0x82f0
addl %r15d, %ebx
addl -0x44(%rbp), %r13d
movb $0x1, %r15b
jmp 0x64c6b
movq -0x50(%rbp), %rdi
callq 0xf2d0
movq -0x38(%rbp), %rax
movl $0x7, 0x5c(%rax)
movb $0x1, %cl
movb %cl, 0x61(%rax)
movq 0x8(%rax), %rax
movb %cl, 0x50(%rax)
movq -0x30(%rbp), %rdi
callq 0xa2e8
xorl %r15d, %r15d
movq $0x0, -0x30(%rbp)
testb %r15b, %r15b
jne 0x64b85
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq (%r15), %rsi
movq -0x38(%rbp), %rdi
addq $0x8, %rdi
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xf373
xorl %eax, %eax
xorl %ebx, %ebx
movslq %ebx, %rbx
movq -0x30(%rbp), %r15
leaq (%r15,%rbx), %rdi
cltq
addq %rax, %r14
movq -0x40(%rbp), %rcx
subl %eax, %ecx
movslq %ecx, %r12
movq %r14, %rsi
movq %r12, %rdx
callq 0x82f0
addl %ebx, %r12d
movslq %r12d, %rdx
movb $0x0, (%r15,%rdx)
leaq -0x5a9f3(%rip), %r8     # 0xa2e8
movq -0x38(%rbp), %rdi
movq %r15, %rsi
movl $0x1, %ecx
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xef1b