#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 */
char *initial_strstr(char const *, char const *);
int /*<<< orphan*/ strcpy(char *, char const *);
size_t strlen(char const *);
int /*<<< orphan*/ strncpy(char *, char const *, size_t);
void replace_initial_substrings(char *dest_str, const char *src_str,
                                const char *old_sub_str,
                                const char *new_sub_str) {
  const char *prev_src_str = src_str;
  char *prev_dest_str = dest_str;
  const char *cur_src_str = src_str;
  size_t old_sub_str_len = strlen(old_sub_str);
  size_t new_sub_str_len = strlen(new_sub_str);
  while ((cur_src_str = initial_strstr(prev_src_str, old_sub_str)) != NULL) {
    size_t prev_segm_len = cur_src_str - prev_src_str;
    strncpy(prev_dest_str, prev_src_str, prev_segm_len);
    prev_dest_str += prev_segm_len;
    strncpy(prev_dest_str, new_sub_str, new_sub_str_len);
    prev_dest_str += new_sub_str_len;
    prev_src_str = cur_src_str + old_sub_str_len;
  }
  strcpy(prev_dest_str, prev_src_str);
}


x86 assembly (clang, -O0):
<replace_initial_substrings>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,-0x40(%rbp)
mov    -0x20(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,-0x48(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1060 <initial_strstr@plt>
mov    %rax,-0x38(%rbp)
cmp    $0x0,%rax
je     11ff <replace_initial_substrings+0xbf>
mov    -0x38(%rbp),%rax
mov    -0x28(%rbp),%rcx
sub    %rcx,%rax
mov    %rax,-0x50(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x50(%rbp),%rdx
call   1030 <strncpy@plt>
mov    -0x50(%rbp),%rax
add    -0x30(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x48(%rbp),%rdx
call   1030 <strncpy@plt>
mov    -0x48(%rbp),%rax
add    -0x30(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x38(%rbp),%rax
add    -0x40(%rbp),%rax
mov    %rax,-0x28(%rbp)
jmp    118a <replace_initial_substrings+0x4a>
mov    -0x30(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1040 <strcpy@plt>
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<replace_initial_substrings>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rcx,%r15
mov    %rdx,%rbp
mov    %rsi,%rbx
mov    %rdi,%r14
mov    %rdx,%rdi
call   1050 <strlen@plt>
mov    %rax,(%rsp)
mov    %r15,%rdi
call   1050 <strlen@plt>
mov    %rax,%r13
mov    %rbx,%rdi
mov    %rbp,%rsi
call   1060 <initial_strstr@plt>
test   %rax,%rax
je     11ea <replace_initial_substrings+0xaa>
mov    %rax,%r12
mov    %r15,0x10(%rsp)
mov    %rbp,0x8(%rsp)
mov    (%rsp),%r15
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %r12,%rbp
sub    %rbx,%rbp
mov    %r14,%rdi
mov    %rbx,%rsi
mov    %rbp,%rdx
call   1030 <strncpy@plt>
add    %r14,%rbp
mov    %rbp,%rdi
mov    0x10(%rsp),%rsi
mov    %r13,%rdx
call   1030 <strncpy@plt>
add    %r13,%rbp
mov    %r12,%rbx
add    %r15,%rbx
mov    %rbx,%rdi
mov    0x8(%rsp),%rsi
call   1060 <initial_strstr@plt>
mov    %rax,%r12
mov    %rbp,%r14
test   %rax,%rax
jne    11a0 <replace_initial_substrings+0x60>
jmp    11ed <replace_initial_substrings+0xad>
mov    %r14,%rbp
mov    %rbp,%rdi
mov    %rbx,%rsi
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <strcpy@plt>

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

LLVM IR (-O2):
define void @replace_initial_substrings(i8* %0, i8* %1, i8* %2, i8* %3) {
%5 = tail call i64 @strlen(i8* dereferenceable(1) %2)
%6 = tail call i64 @strlen(i8* dereferenceable(1) %3)
%7 = tail call i8* @initial_strstr(i8* %1, i8* %2)
%8 = icmp eq i8* %7, null
br i1 %8, label %23, label %9
9: ; preds = %4, %9
%10 = phi i8* [ %21, %9 ], [ %7, %4 ]
%11 = phi i8* [ %20, %9 ], [ %1, %4 ]
%12 = phi i8* [ %19, %9 ], [ %0, %4 ]
%13 = ptrtoint i8* %10 to i64
%14 = ptrtoint i8* %11 to i64
%15 = sub i64 %13, %14
%16 = tail call i32 @strncpy(i8* %12, i8* %11, i64 %15)
%17 = getelementptr inbounds i8, i8* %12, i64 %15
%18 = tail call i32 @strncpy(i8* %17, i8* %3, i64 %6)
%19 = getelementptr inbounds i8, i8* %17, i64 %6
%20 = getelementptr inbounds i8, i8* %10, i64 %5
%21 = tail call i8* @initial_strstr(i8* %20, i8* %2)
%22 = icmp eq i8* %21, null
br i1 %22, label %23, label %9
23: ; preds = %9, %4
%24 = phi i8* [ %0, %4 ], [ %19, %9 ]
%25 = phi i8* [ %1, %4 ], [ %20, %9 ]
%26 = tail call i32 @strcpy(i8* %24, i8* %25)
ret void
}