#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 */
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
char *strstr(char *, char *);
void replaceFirst(char *str1, char *str2, char *str3) {
  char str4[strlen(str1) + 1];
  char *p;
  strcpy(str4, str1);
  if ((p = strstr(str1, str2)) != NULL) {
    while (str1 != p && str1 != NULL) {
      str1++;
    }
    str1[0] = '\0';
    strcat(str1, str3);
    strcat(str1, strstr(str4, str2) + strlen(str2));
  }
}


x86 assembly (clang, -O0):
<replaceFirst>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
add    $0x1,%eax
mov    %rsp,%rcx
mov    %rcx,-0x20(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x38(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1060 <strstr@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
je     1233 <replaceFirst+0xf3>
jmp    11ac <replaceFirst+0x6c>
mov    -0x8(%rbp),%rcx
xor    %eax,%eax
cmp    -0x30(%rbp),%rcx
mov    %al,-0x39(%rbp)
je     11ca <replaceFirst+0x8a>
cmpq   $0x0,-0x8(%rbp)
setne  %al
mov    %al,-0x39(%rbp)
mov    -0x39(%rbp),%al
test   $0x1,%al
jne    11da <replaceFirst+0x9a>
jmp    11eb <replaceFirst+0xab>
mov    -0x8(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x8(%rbp)
jmp    11ac <replaceFirst+0x6c>
mov    -0x8(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1050 <strcat@plt>
mov    -0x38(%rbp),%rdi
mov    -0x8(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x10(%rbp),%rsi
call   1060 <strstr@plt>
mov    %rax,-0x50(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x50(%rbp),%rsi
mov    -0x48(%rbp),%rdi
cltq
add    %rax,%rsi
call   1050 <strcat@plt>
mov    -0x20(%rbp),%rax
mov    %rax,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<replaceFirst>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%rbx
call   1040 <strlen@plt>
add    $0x1,%eax
mov    %rsp,%r12
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r12
mov    %r12,%rsp
mov    %r12,%rdi
mov    %rbx,%rsi
call   1030 <strcpy@plt>
mov    %rbx,%rdi
mov    %r15,%rsi
call   1060 <strstr@plt>
test   %rax,%rax
je     11d2 <replaceFirst+0x92>
mov    %rbx,%rcx
neg    %rcx
sub    %rbx,%rax
cmp    %rax,%rcx
cmovb  %rcx,%rax
lea    (%rbx,%rax,1),%r13
movb   $0x0,(%rbx,%rax,1)
mov    %r13,%rdi
mov    %r14,%rsi
call   1050 <strcat@plt>
mov    %r12,%rdi
mov    %r15,%rsi
call   1060 <strstr@plt>
mov    %rax,%rbx
mov    %r15,%rdi
call   1040 <strlen@plt>
movslq %eax,%rsi
add    %rbx,%rsi
mov    %r13,%rdi
call   1050 <strcat@plt>
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @replaceFirst(i8* %0, i8* %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i8*
store i8* %0, i8** %4
store i8* %1, i8** %5
store i8* %2, i8** %6
%10 = load i8*, i8** %4
%11 = call i32 @strlen(i8* %10)
%12 = add nsw i32 %11, 1
%13 = zext i32 %12 to i64
%14 = call i8* @llvm.stacksave()
store i8* %14, i8** %7
%15 = alloca i8, i64 %13
store i64 %13, i64* %8
%16 = load i8*, i8** %4
%17 = call i32 @strcpy(i8* %15, i8* %16)
%18 = load i8*, i8** %4
%19 = load i8*, i8** %5
%20 = call i8* @strstr(i8* %18, i8* %19)
store i8* %20, i8** %9
%21 = icmp ne i8* %20, null
br i1 %21, label %22, label %49
22: ; preds = %3
br label %23
23: ; preds = %32, %22
%24 = load i8*, i8** %4
%25 = load i8*, i8** %9
%26 = icmp ne i8* %24, %25
br i1 %26, label %27, label %30
27: ; preds = %23
%28 = load i8*, i8** %4
%29 = icmp ne i8* %28, null
br label %30
30: ; preds = %27, %23
%31 = phi i1 [ false, %23 ], [ %29, %27 ]
br i1 %31, label %32, label %35
32: ; preds = %30
%33 = load i8*, i8** %4
%34 = getelementptr inbounds i8, i8* %33, i32 1
store i8* %34, i8** %4
br label %23
35: ; preds = %30
%36 = load i8*, i8** %4
%37 = getelementptr inbounds i8, i8* %36, i64 0
store i8 0, i8* %37
%38 = load i8*, i8** %4
%39 = load i8*, i8** %6
%40 = call i32 @strcat(i8* %38, i8* %39)
%41 = load i8*, i8** %4
%42 = load i8*, i8** %5
%43 = call i8* @strstr(i8* %15, i8* %42)
%44 = load i8*, i8** %5
%45 = call i32 @strlen(i8* %44)
%46 = sext i32 %45 to i64
%47 = getelementptr inbounds i8, i8* %43, i64 %46
%48 = call i32 @strcat(i8* %41, i8* %47)
br label %49
49: ; preds = %35, %3
%50 = load i8*, i8** %7
ret void
}

LLVM IR (-O2):
define void @replaceFirst(i8* %0, i8* %1, i8* %2) {
%4 = ptrtoint i8* %0 to i64
%5 = tail call i32 @strlen(i8* %0)
%6 = add nsw i32 %5, 1
%7 = zext i32 %6 to i64
%8 = alloca i8, i64 %7
%9 = call i32 @strcpy(i8* %8, i8* %0)
%10 = call i8* @strstr(i8* dereferenceable(1) %0, i8* dereferenceable(1) %1)
%11 = icmp eq i8* %10, null
br i1 %11, label %24, label %12
12: ; preds = %3
%13 = ptrtoint i8* %10 to i64
%14 = sub i64 0, %4
%15 = sub i64 %13, %4
%16 = call i64 @llvm.umin.i64(i64 %14, i64 %15)
%17 = getelementptr i8, i8* %0, i64 %16
store i8 0, i8* %17
%18 = call i32 @strcat(i8* %17, i8* %2)
%19 = call i8* @strstr(i8* %8, i8* dereferenceable(1) %1)
%20 = call i32 @strlen(i8* %1)
%21 = sext i32 %20 to i64
%22 = getelementptr inbounds i8, i8* %19, i64 %21
%23 = call i32 @strcat(i8* %17, i8* %22)
br label %24
24: ; preds = %12, %3
ret void
}