#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*/ printf(char *, char *);
char *search_replace_string(char *, char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
void test_search_replace(void) {
  char source_destination[1024];
  strcpy(source_destination, "stage|this|that");
  printf("should be (stage2|this|that): is = (%s)\n",
         search_replace_string(source_destination, "stage", "stage2"));
  strcpy(source_destination, "this|that|stage");
  printf("should be (this|that|stage2): is = (%s)\n",
         search_replace_string(source_destination, "stage", "stage2"));
  strcpy(source_destination, "this|that|stage2");
  printf("should be (this|that|stage2): is = (%s)\n",
         search_replace_string(source_destination, "stage", "stage"));
  strcpy(source_destination, "this|that|stage2");
  printf("should be (this|that|stage2): is = (%s)\n",
         search_replace_string(source_destination, "stage2", "stage2"));
}


x86 assembly (clang, -O0):
<test_search_replace>:
push   %rbp
mov    %rsp,%rbp
sub    $0x400,%rsp
lea    -0x400(%rbp),%rdi
lea    0xeb7(%rip),%rsi
call   1030 <strcpy@plt>
lea    -0x400(%rbp),%rdi
lea    0xee7(%rip),%rsi
lea    0xf19(%rip),%rdx
call   1040 <search_replace_string@plt>
mov    %rax,%rsi
lea    0xe9e(%rip),%rdi
call   1050 <printf@plt>
lea    -0x400(%rbp),%rdi
lea    0xeb4(%rip),%rsi
call   1030 <strcpy@plt>
lea    -0x400(%rbp),%rdi
lea    0xeab(%rip),%rsi
lea    0xedd(%rip),%rdx
call   1040 <search_replace_string@plt>
mov    %rax,%rsi
lea    0xe9b(%rip),%rdi
call   1050 <printf@plt>
lea    -0x400(%rbp),%rdi
lea    0xeb1(%rip),%rsi
call   1030 <strcpy@plt>
lea    -0x400(%rbp),%rdi
lea    0xe6f(%rip),%rdx
mov    %rdx,%rsi
call   1040 <search_replace_string@plt>
mov    %rax,%rsi
lea    0xe63(%rip),%rdi
call   1050 <printf@plt>
lea    -0x400(%rbp),%rdi
lea    0xe79(%rip),%rsi
call   1030 <strcpy@plt>
lea    -0x400(%rbp),%rdi
lea    0xe70(%rip),%rdx
mov    %rdx,%rsi
call   1040 <search_replace_string@plt>
mov    %rax,%rsi
lea    0xe2b(%rip),%rdi
call   1050 <printf@plt>
add    $0x400,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_search_replace>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x400,%rsp
lea    0xeb9(%rip),%rsi
mov    %rsp,%rbx
mov    %rbx,%rdi
call   1030 <strcpy@plt>
lea    0xeea(%rip),%r15
lea    0xf1c(%rip),%r14
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %r14,%rdx
call   1040 <search_replace_string@plt>
lea    0xe9b(%rip),%rdi
mov    %rax,%rsi
call   1050 <printf@plt>
lea    0xeb5(%rip),%rsi
mov    %rbx,%rdi
call   1030 <strcpy@plt>
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %r14,%rdx
call   1040 <search_replace_string@plt>
lea    0xea8(%rip),%r12
mov    %r12,%rdi
mov    %rax,%rsi
call   1050 <printf@plt>
lea    0xebf(%rip),%r13
mov    %rbx,%rdi
mov    %r13,%rsi
call   1030 <strcpy@plt>
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %r15,%rdx
call   1040 <search_replace_string@plt>
mov    %r12,%rdi
mov    %rax,%rsi
call   1050 <printf@plt>
mov    %rbx,%rdi
mov    %r13,%rsi
call   1030 <strcpy@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %r14,%rdx
call   1040 <search_replace_string@plt>
mov    %r12,%rdi
mov    %rax,%rsi
call   1050 <printf@plt>
add    $0x400,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [16 x i8] c"stage|this|that\00"
@.str.1 = [41 x i8] c"should be (stage2|this|that): is = (%s)\0A\00"
@.str.2 = [6 x i8] c"stage\00"
@.str.3 = [7 x i8] c"stage2\00"
@.str.4 = [16 x i8] c"this|that|stage\00"
@.str.5 = [41 x i8] c"should be (this|that|stage2): is = (%s)\0A\00"
@.str.6 = [17 x i8] c"this|that|stage2\00"
define void @test_search_replace() {
%1 = alloca [1024 x i8]
%2 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 0
%3 = call i32 @strcpy(i8* %2, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%4 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 0
%5 = call i8* @search_replace_string(i8* %4, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%6 = call i32 @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.1, i64 0, i64 0), i8* %5)
%7 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 0
%8 = call i32 @strcpy(i8* %7, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0))
%9 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 0
%10 = call i8* @search_replace_string(i8* %9, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%11 = call i32 @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.5, i64 0, i64 0), i8* %10)
%12 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 0
%13 = call i32 @strcpy(i8* %12, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.6, i64 0, i64 0))
%14 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 0
%15 = call i8* @search_replace_string(i8* %14, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%16 = call i32 @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.5, i64 0, i64 0), i8* %15)
%17 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 0
%18 = call i32 @strcpy(i8* %17, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.6, i64 0, i64 0))
%19 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 0
%20 = call i8* @search_replace_string(i8* %19, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%21 = call i32 @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.5, i64 0, i64 0), i8* %20)
ret void
}

LLVM IR (-O2):
@.str = [16 x i8] c"stage|this|that\00"
@.str.1 = [41 x i8] c"should be (stage2|this|that): is = (%s)\0A\00"
@.str.2 = [6 x i8] c"stage\00"
@.str.3 = [7 x i8] c"stage2\00"
@.str.4 = [16 x i8] c"this|that|stage\00"
@.str.5 = [41 x i8] c"should be (this|that|stage2): is = (%s)\0A\00"
@.str.6 = [17 x i8] c"this|that|stage2\00"
define void @test_search_replace() {
%1 = alloca [1024 x i8]
%2 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 0
%3 = call i32 @strcpy(i8* %2, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%4 = call i8* @search_replace_string(i8* %2, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%5 = call i32 @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.1, i64 0, i64 0), i8* %4)
%6 = call i32 @strcpy(i8* %2, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0))
%7 = call i8* @search_replace_string(i8* %2, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%8 = call i32 @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.5, i64 0, i64 0), i8* %7)
%9 = call i32 @strcpy(i8* %2, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.6, i64 0, i64 0))
%10 = call i8* @search_replace_string(i8* %2, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%11 = call i32 @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.5, i64 0, i64 0), i8* %10)
%12 = call i32 @strcpy(i8* %2, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.6, i64 0, i64 0))
%13 = call i8* @search_replace_string(i8* %2, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%14 = call i32 @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.5, i64 0, i64 0), i8* %13)
ret void
}