// File: /workspace/github_c_makefile_star2_O0/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
subq $0x70, %rsp
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movq %rdx, -0x18(%rbp)
movq -0x18(%rbp), %rax
movq (%rax), %rdi
callq 0x10350
movq %rax, -0x20(%rbp)
cmpq $0x0, -0x20(%rbp)
jne 0x86aff
jmp 0x86d40
movq -0x18(%rbp), %rax
movq (%rax), %rdi
callq 0x10370
movl %eax, -0x3c(%rbp)
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x10350
movq %rax, -0x28(%rbp)
cmpq $0x0, -0x28(%rbp)
jne 0x86b2b
jmp 0x86d40
movq -0x28(%rbp), %rax
movzbl (%rax), %eax
cmpl $0x0, %eax
jne 0x86b4c
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq (%rax), %rsi
callq 0x10e80
jmp 0x86d40
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x10370
movl %eax, -0x40(%rbp)
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rdi
callq 0x10350
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x30(%rbp)
jne 0x86b79
jmp 0x86d40
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rdi
callq 0x10370
movl %eax, -0x44(%rbp)
movl -0x3c(%rbp), %eax
addl $0x1, %eax
cltq
movq %rax, -0x50(%rbp)
movq -0x8(%rbp), %rdi
movq -0x50(%rbp), %rsi
callq 0x873e0
movq %rax, -0x38(%rbp)
cmpq $0x0, -0x38(%rbp)
jne 0x86bb2
jmp 0x86d40
movl -0x3c(%rbp), %eax
subl -0x40(%rbp), %eax
movl %eax, -0x54(%rbp)
movl $0x0, -0x5c(%rbp)
movl $0x0, -0x58(%rbp)
movl -0x58(%rbp), %eax
cmpl -0x54(%rbp), %eax
jg 0x86ced
movq -0x20(%rbp), %rax
movslq -0x58(%rbp), %rcx
movzbl (%rax,%rcx), %eax
movq -0x28(%rbp), %rcx
movzbl (%rcx), %ecx
cmpl %ecx, %eax
jne 0x86c09
movq -0x20(%rbp), %rdi
movslq -0x58(%rbp), %rax
addq %rax, %rdi
movq -0x28(%rbp), %rsi
movslq -0x40(%rbp), %rdx
callq 0x81c0
cmpl $0x0, %eax
je 0x86c2e
movq -0x20(%rbp), %rax
movslq -0x58(%rbp), %rcx
movb (%rax,%rcx), %dl
movq -0x38(%rbp), %rax
movl -0x5c(%rbp), %ecx
movl %ecx, %esi
addl $0x1, %esi
movl %esi, -0x5c(%rbp)
movslq %ecx, %rcx
movb %dl, (%rax,%rcx)
jmp 0x86cdd
movq -0x8(%rbp), %rdi
callq 0x117b0
movq %rax, -0x70(%rbp)
movl -0x44(%rbp), %eax
subl -0x40(%rbp), %eax
cltq
addq -0x50(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rax
subq $0x1, %rax
movq -0x70(%rbp), %rcx
movslq 0x68(%rcx), %rcx
cmpq %rcx, %rax
jle 0x86c77
movq -0x8(%rbp), %rdi
callq 0x110d0
movq -0x38(%rbp), %rdi
callq 0xa420
jmp 0x86d40
movq -0x38(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0x38(%rbp), %rdi
movq -0x50(%rbp), %rax
movl %eax, %esi
callq 0xa500
movq %rax, -0x38(%rbp)
cmpq $0x0, -0x38(%rbp)
jne 0x86cb0
movq -0x8(%rbp), %rdi
callq 0x11120
movq -0x68(%rbp), %rdi
callq 0xa420
jmp 0x86d40
movq -0x38(%rbp), %rdi
movslq -0x5c(%rbp), %rax
addq %rax, %rdi
movq -0x30(%rbp), %rsi
movslq -0x44(%rbp), %rdx
callq 0x82f0
movl -0x44(%rbp), %eax
addl -0x5c(%rbp), %eax
movl %eax, -0x5c(%rbp)
movl -0x40(%rbp), %eax
subl $0x1, %eax
addl -0x58(%rbp), %eax
movl %eax, -0x58(%rbp)
jmp 0x86cdf
movl -0x58(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x58(%rbp)
jmp 0x86bc9
movq -0x38(%rbp), %rdi
movslq -0x5c(%rbp), %rax
addq %rax, %rdi
movq -0x20(%rbp), %rsi
movslq -0x58(%rbp), %rax
addq %rax, %rsi
movl -0x3c(%rbp), %eax
subl -0x58(%rbp), %eax
movslq %eax, %rdx
callq 0x82f0
movl -0x3c(%rbp), %eax
subl -0x58(%rbp), %eax
addl -0x5c(%rbp), %eax
movl %eax, -0x5c(%rbp)
movq -0x38(%rbp), %rax
movslq -0x5c(%rbp), %rcx
movb $0x0, (%rax,%rcx)
movq -0x8(%rbp), %rdi
movq -0x38(%rbp), %rsi
movl -0x5c(%rbp), %edx
leaq -0x7c91b(%rip), %rcx    # 0xa420
callq 0x10d80
addq $0x70, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)