// File: /workspace/github_c_makefile_star2_O2/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 $0x48, %rsp
movq %rdx, %rbx
movq %rdi, %r15
movq (%rdx), %rdi
callq 0xdf1e
testq %rax, %rax
je 0x59157
movq %rax, %r14
movq (%rbx), %rdi
callq 0xdf2d
movl %eax, %r13d
movq 0x8(%rbx), %rdi
callq 0xdf1e
testq %rax, %rax
je 0x59157
cmpb $0x0, (%rax)
je 0x59166
movq 0x8(%rbx), %rdi
movq %rax, -0x38(%rbp)
callq 0xdf2d
movl %eax, %r12d
movq 0x10(%rbx), %rdi
callq 0xdf1e
movq %rax, -0x68(%rbp)
testq %rax, %rax
je 0x59157
movq 0x10(%rbx), %rdi
callq 0xdf2d
movl %eax, %ebx
leal 0x1(%r13), %eax
movslq %eax, %rsi
movq %r15, %rdi
movq %rsi, -0x48(%rbp)
callq 0x5959e
movq -0x38(%rbp), %rcx
testq %rax, %rax
je 0x59157
movq %rax, %rdx
movq %r15, -0x30(%rbp)
movq %r13, -0x50(%rbp)
movl %r13d, %eax
subl %r12d, %eax
movl %eax, -0x40(%rbp)
movslq %r12d, %rax
movslq %ebx, %rsi
movq %rsi, -0x58(%rbp)
subl %eax, %ebx
movslq %ebx, %rsi
movq %rsi, -0x60(%rbp)
movq %rax, -0x70(%rbp)
decl %eax
movl %eax, -0x3c(%rbp)
xorl %r13d, %r13d
xorl %eax, %eax
movslq %eax, %r15
movslq %r13d, %rdi
cmpl -0x40(%rbp), %r13d
jg 0x59183
movb (%r14,%rdi), %bl
cmpb (%rcx), %bl
jne 0x590dc
addq %r14, %rdi
movq %r14, %r12
movq %rdx, %r14
movq -0x38(%rbp), %rsi
movq -0x70(%rbp), %rdx
callq 0x83e0
movq -0x38(%rbp), %rcx
movq %r14, %rdx
movq %r12, %r14
testl %eax, %eax
je 0x590e9
leal 0x1(%r15), %eax
movb %bl, (%rdx,%r15)
incl %r13d
jmp 0x590a0
movq -0x30(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x48(%rbp), %rsi
movq -0x60(%rbp), %rdi
leaq (%rsi,%rdi), %rcx
decq %rcx
movslq 0x68(%rax), %rax
cmpq %rax, %rcx
jg 0x591d0
addq %rdi, %rsi
movq %rdx, %rdi
movq %rdx, %r14
movq %rsi, -0x48(%rbp)
callq 0x9ce9
testq %rax, %rax
je 0x591e1
movq %rax, %rbx
movq %rax, %rdi
addq %r15, %rdi
movq -0x68(%rbp), %rsi
movq -0x58(%rbp), %r14
movq %r14, %rdx
callq 0x82c0
addl %r14d, %r15d
addl -0x3c(%rbp), %r13d
movl %r15d, %eax
movq %rbx, %rdx
movq %r12, %r14
movq -0x38(%rbp), %rcx
jmp 0x590e4
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq (%rbx), %rsi
addq $0x8, %r15
movq %r15, %rdi
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xe4e0
leaq (%rdx,%r15), %rax
addq %rdi, %r14
movq -0x50(%rbp), %rcx
subl %edi, %ecx
movslq %ecx, %rbx
movq %rax, %rdi
movq %r14, %rsi
movq %rdx, %r14
movq %rbx, %rdx
callq 0x82c0
addl %r15d, %ebx
movslq %ebx, %rdx
movb $0x0, (%r14,%rdx)
leaq -0x4f53b(%rip), %rcx    # 0x9c7b
movq -0x30(%rbp), %rdi
movq %r14, %rsi
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xe48d
movq -0x30(%rbp), %rdi
movq %rdx, %rbx
callq 0xe604
movq %rbx, %rdi
jmp 0x591ed
movq -0x30(%rbp), %rdi
callq 0xe631
movq %r14, %rdi
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x9c7b