#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */

/* Variables and functions */
scalar_t__ nullstr(char const *);
int /*<<< orphan*/ strcpy(char *, char const *);
char *stristr(char const *, char const *);
size_t strlen(char const *);
int /*<<< orphan*/ strncpy(char *, char const *, size_t);
const char *str_ireplace(const char *orig, const char *old, const char *pnew) {
  char *buf = NULL;
  const char *ptr = NULL;
  size_t len = 0, a = 0, b = 0;
  static char outbuf[7][2048];
  static int i = 0;
  if (nullstr(orig) || nullstr(old) || pnew == NULL) {
    return orig;
  }
  if ((ptr = stristr(orig, old)) == NULL ||
      (a = ptr - orig) + (len = strlen(pnew)) >= (2048 - 4)) {
    return orig;
  }
  ++i, i %= 7;
  buf = outbuf[i];
  b = 2048 - 4 - a - len;
  strcpy(buf, orig);
  strncpy(buf + a, pnew, len);
  strncpy(buf + a + len, ptr + strlen(old), b);
  buf[2048 - 4] = 0;
  return (buf);
}


x86 assembly (clang, -O0):
<str_ireplace>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
movq   $0x0,-0x40(%rbp)
movq   $0x0,-0x48(%rbp)
mov    -0x10(%rbp),%rdi
call   1070 <nullstr@plt>
cmp    $0x0,%rax
jne    11bd <str_ireplace+0x6d>
mov    -0x18(%rbp),%rdi
call   1070 <nullstr@plt>
cmp    $0x0,%rax
jne    11bd <str_ireplace+0x6d>
cmpq   $0x0,-0x20(%rbp)
jne    11ca <str_ireplace+0x7a>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    12df <str_ireplace+0x18f>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1030 <stristr@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
je     121b <str_ireplace+0xcb>
mov    -0x30(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    %rcx,%rax
mov    %rax,-0x50(%rbp)
mov    %rax,-0x40(%rbp)
mov    -0x20(%rbp),%rdi
call   1060 <strlen@plt>
mov    %rax,%rcx
mov    -0x50(%rbp),%rax
mov    %rcx,-0x38(%rbp)
add    %rcx,%rax
cmp    $0x7fc,%rax
jb     1228 <str_ireplace+0xd8>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    12df <str_ireplace+0x18f>
mov    0x6632(%rip),%eax
add    $0x1,%eax
mov    %eax,0x6629(%rip)
mov    0x6623(%rip),%eax
mov    $0x7,%ecx
cltd
idiv   %ecx
mov    %edx,0x6615(%rip)
movslq 0x660e(%rip),%rcx
lea    0x2e07(%rip),%rax
shl    $0xb,%rcx
add    %rcx,%rax
mov    %rax,-0x28(%rbp)
mov    $0x7fc,%eax
sub    -0x40(%rbp),%rax
sub    -0x38(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1050 <strcpy@plt>
mov    -0x28(%rbp),%rdi
add    -0x40(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x38(%rbp),%rdx
call   1040 <strncpy@plt>
mov    -0x28(%rbp),%rax
add    -0x40(%rbp),%rax
add    -0x38(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x60(%rbp)
mov    -0x18(%rbp),%rdi
call   1060 <strlen@plt>
mov    -0x60(%rbp),%rsi
mov    -0x58(%rbp),%rdi
add    %rax,%rsi
mov    -0x48(%rbp),%rdx
call   1040 <strncpy@plt>
mov    -0x28(%rbp),%rax
movb   $0x0,0x7fc(%rax)
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<str_ireplace>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%r15
mov    %rsi,%r14
mov    %rdi,%rbx
call   1070 <nullstr@plt>
test   %rax,%rax
je     1183 <str_ireplace+0x33>
mov    %rbx,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %r14,%rdi
call   1070 <nullstr@plt>
test   %r15,%r15
je     1171 <str_ireplace+0x21>
test   %rax,%rax
jne    1171 <str_ireplace+0x21>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1030 <stristr@plt>
test   %rax,%rax
je     1171 <str_ireplace+0x21>
mov    %rax,%r12
mov    %rax,%r13
sub    %rbx,%r13
mov    %r15,%rdi
call   1060 <strlen@plt>
lea    (%rax,%r13,1),%rcx
cmp    $0x7fb,%rcx
ja     1171 <str_ireplace+0x21>
mov    0x6697(%rip),%edi
mov    %rax,0x10(%rsp)
mov    %edi,%edx
add    $0x1,%edx
movslq %edx,%rdx
imul   $0xffffffff92492493,%rdx,%rdx
shr    $0x20,%rdx
add    %edi,%edx
add    $0x1,%edx
mov    %edx,%esi
shr    $0x1f,%esi
sar    $0x2,%edx
add    %esi,%edx
lea    0x0(,%rdx,8),%esi
sub    %esi,%edx
add    %edi,%edx
add    $0x1,%edx
mov    %edx,0x665c(%rip)
shl    $0xb,%rdx
mov    %rdx,0x8(%rsp)
lea    0x2e4c(%rip),%rax
lea    (%rax,%rdx,1),%rbp
mov    $0x7fc,%eax
sub    %rcx,%rax
mov    %rax,(%rsp)
mov    %rbp,%rdi
mov    %rbx,%rsi
call   1050 <strcpy@plt>
add    %rbp,%r13
mov    %r13,%rdi
mov    %r15,%rsi
mov    0x10(%rsp),%rbx
mov    %rbx,%rdx
call   1040 <strncpy@plt>
add    %rbx,%r13
mov    %r14,%rdi
call   1060 <strlen@plt>
add    %rax,%r12
mov    %r13,%rdi
mov    %r12,%rsi
mov    (%rsp),%rdx
call   1040 <strncpy@plt>
lea    0x2df7(%rip),%rax
mov    0x8(%rsp),%rcx
movb   $0x0,0x7fc(%rcx,%rax,1)
mov    %rbp,%rbx
jmp    1171 <str_ireplace+0x21>

LLVM IR (-O0):
@str_ireplace.outbuf = internal global [7 x [2048 x i8]] zeroinitializer
@str_ireplace.i = internal global i32 0
define i8* @str_ireplace(i8* %0, i8* %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i64
%11 = alloca i64
%12 = alloca i64
store i8* %0, i8** %5
store i8* %1, i8** %6
store i8* %2, i8** %7
store i8* null, i8** %8
store i8* null, i8** %9
store i64 0, i64* %10
store i64 0, i64* %11
store i64 0, i64* %12
%13 = load i8*, i8** %5
%14 = call i64 @nullstr(i8* %13)
%15 = icmp ne i64 %14, 0
br i1 %15, label %23, label %16
16: ; preds = %3
%17 = load i8*, i8** %6
%18 = call i64 @nullstr(i8* %17)
%19 = icmp ne i64 %18, 0
br i1 %19, label %23, label %20
20: ; preds = %16
%21 = load i8*, i8** %7
%22 = icmp eq i8* %21, null
br i1 %22, label %23, label %25
23: ; preds = %20, %16, %3
%24 = load i8*, i8** %5
store i8* %24, i8** %4
br label %78
25: ; preds = %20
%26 = load i8*, i8** %5
%27 = load i8*, i8** %6
%28 = call i8* @stristr(i8* %26, i8* %27)
store i8* %28, i8** %9
%29 = icmp eq i8* %28, null
br i1 %29, label %40, label %30
30: ; preds = %25
%31 = load i8*, i8** %9
%32 = load i8*, i8** %5
%33 = ptrtoint i8* %31 to i64
%34 = ptrtoint i8* %32 to i64
%35 = sub i64 %33, %34
store i64 %35, i64* %11
%36 = load i8*, i8** %7
%37 = call i64 @strlen(i8* %36)
store i64 %37, i64* %10
%38 = add i64 %35, %37
%39 = icmp uge i64 %38, 2044
br i1 %39, label %40, label %42
40: ; preds = %30, %25
%41 = load i8*, i8** %5
store i8* %41, i8** %4
br label %78
42: ; preds = %30
%43 = load i32, i32* @str_ireplace.i
%44 = add nsw i32 %43, 1
store i32 %44, i32* @str_ireplace.i
%45 = load i32, i32* @str_ireplace.i
%46 = srem i32 %45, 7
store i32 %46, i32* @str_ireplace.i
%47 = load i32, i32* @str_ireplace.i
%48 = sext i32 %47 to i64
%49 = getelementptr inbounds [7 x [2048 x i8]], [7 x [2048 x i8]]* @str_ireplace.outbuf, i64 0, i64 %48
%50 = getelementptr inbounds [2048 x i8], [2048 x i8]* %49, i64 0, i64 0
store i8* %50, i8** %8
%51 = load i64, i64* %11
%52 = sub i64 2044, %51
%53 = load i64, i64* %10
%54 = sub i64 %52, %53
store i64 %54, i64* %12
%55 = load i8*, i8** %8
%56 = load i8*, i8** %5
%57 = call i32 @strcpy(i8* %55, i8* %56)
%58 = load i8*, i8** %8
%59 = load i64, i64* %11
%60 = getelementptr inbounds i8, i8* %58, i64 %59
%61 = load i8*, i8** %7
%62 = load i64, i64* %10
%63 = call i32 @strncpy(i8* %60, i8* %61, i64 %62)
%64 = load i8*, i8** %8
%65 = load i64, i64* %11
%66 = getelementptr inbounds i8, i8* %64, i64 %65
%67 = load i64, i64* %10
%68 = getelementptr inbounds i8, i8* %66, i64 %67
%69 = load i8*, i8** %9
%70 = load i8*, i8** %6
%71 = call i64 @strlen(i8* %70)
%72 = getelementptr inbounds i8, i8* %69, i64 %71
%73 = load i64, i64* %12
%74 = call i32 @strncpy(i8* %68, i8* %72, i64 %73)
%75 = load i8*, i8** %8
%76 = getelementptr inbounds i8, i8* %75, i64 2044
store i8 0, i8* %76
%77 = load i8*, i8** %8
store i8* %77, i8** %4
br label %78
78: ; preds = %42, %40, %23
%79 = load i8*, i8** %4
ret i8* %79
}

LLVM IR (-O2):
@str_ireplace.outbuf = internal global [7 x [2048 x i8]] zeroinitializer
@str_ireplace.i = internal unnamed_addr global i32 0
define i8* @str_ireplace(i8* %0, i8* %1, i8* %2) {
%4 = tail call i64 @nullstr(i8* %0)
%5 = icmp eq i64 %4, 0
br i1 %5, label %6, label %36
6: ; preds = %3
%7 = tail call i64 @nullstr(i8* %1)
%8 = icmp ne i64 %7, 0
%9 = icmp eq i8* %2, null
%10 = or i1 %9, %8
br i1 %10, label %36, label %11
11: ; preds = %6
%12 = tail call i8* @stristr(i8* %0, i8* %1)
%13 = icmp eq i8* %12, null
br i1 %13, label %36, label %14
14: ; preds = %11
%15 = ptrtoint i8* %12 to i64
%16 = ptrtoint i8* %0 to i64
%17 = sub i64 %15, %16
%18 = tail call i64 @strlen(i8* dereferenceable(1) %2)
%19 = add i64 %18, %17
%20 = icmp ugt i64 %19, 2043
br i1 %20, label %36, label %21
21: ; preds = %14
%22 = load i32, i32* @str_ireplace.i
%23 = add nsw i32 %22, 1
%24 = srem i32 %23, 7
store i32 %24, i32* @str_ireplace.i
%25 = zext i32 %24 to i64
%26 = getelementptr inbounds [7 x [2048 x i8]], [7 x [2048 x i8]]* @str_ireplace.outbuf, i64 0, i64 %25, i64 0
%27 = sub nuw nsw i64 2044, %19
%28 = tail call i32 @strcpy(i8* %26, i8* %0)
%29 = getelementptr inbounds [7 x [2048 x i8]], [7 x [2048 x i8]]* @str_ireplace.outbuf, i64 0, i64 %25, i64 %17
%30 = tail call i32 @strncpy(i8* %29, i8* %2, i64 %18)
%31 = getelementptr inbounds i8, i8* %29, i64 %18
%32 = tail call i64 @strlen(i8* dereferenceable(1) %1)
%33 = getelementptr inbounds i8, i8* %12, i64 %32
%34 = tail call i32 @strncpy(i8* %31, i8* %33, i64 %27)
%35 = getelementptr inbounds [7 x [2048 x i8]], [7 x [2048 x i8]]* @str_ireplace.outbuf, i64 0, i64 %25, i64 2044
store i8 0, i8* %35
br label %36
36: ; preds = %11, %14, %3, %6, %21
%37 = phi i8* [ %26, %21 ], [ %0, %6 ], [ %0, %3 ], [ %0, %14 ], [ %0, %11 ]
ret i8* %37
}