#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 */
size_t strlen(char *);
int /*<<< orphan*/ strncpy(char *, char *, size_t);
char *strstr(char *, char *);
char *replace_string(char *s, char *match, char *subst) {
  char *sp = s, *p;
  size_t slen = strlen(subst);
  if (strlen(match) != slen)
    return NULL;
  while ((p = strstr(sp, match)) != NULL) {
    strncpy(p, subst, slen);
    sp = p + slen;
  }
  return s;
}


x86 assembly (clang, -O0):
<replace_string>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x38(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
cmp    -0x38(%rbp),%rax
je     1179 <replace_string+0x49>
movq   $0x0,-0x8(%rbp)
jmp    11c3 <replace_string+0x93>
jmp    117e <replace_string+0x4e>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1050 <strstr@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
je     11bb <replace_string+0x8b>
mov    -0x30(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x38(%rbp),%rdx
call   1030 <strncpy@plt>
mov    -0x30(%rbp),%rax
add    -0x38(%rbp),%rax
mov    %rax,-0x28(%rbp)
jmp    117e <replace_string+0x4e>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<replace_string>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rdx,%r15
mov    %rsi,%r12
mov    %rdi,%r14
mov    %rdx,%rdi
call   1040 <strlen@plt>
mov    %rax,%r13
mov    %r12,%rdi
call   1040 <strlen@plt>
cmp    %r13,%rax
jne    1196 <replace_string+0x66>
mov    %r14,%rdi
mov    %r12,%rsi
call   1050 <strstr@plt>
test   %rax,%rax
je     1199 <replace_string+0x69>
mov    %rax,%rbx
nopl   (%rax)
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %r13,%rdx
call   1030 <strncpy@plt>
add    %r13,%rbx
mov    %rbx,%rdi
mov    %r12,%rsi
call   1050 <strstr@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    1170 <replace_string+0x40>
jmp    1199 <replace_string+0x69>
xor    %r14d,%r14d
mov    %r14,%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i8* @replace_string(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
store i8* %0, i8** %5
store i8* %1, i8** %6
store i8* %2, i8** %7
%11 = load i8*, i8** %5
store i8* %11, i8** %8
%12 = load i8*, i8** %7
%13 = call i64 @strlen(i8* %12)
store i64 %13, i64* %10
%14 = load i8*, i8** %6
%15 = call i64 @strlen(i8* %14)
%16 = load i64, i64* %10
%17 = icmp ne i64 %15, %16
br i1 %17, label %18, label %19
18: ; preds = %3
store i8* null, i8** %4
br label %35
19: ; preds = %3
br label %20
20: ; preds = %25, %19
%21 = load i8*, i8** %8
%22 = load i8*, i8** %6
%23 = call i8* @strstr(i8* %21, i8* %22)
store i8* %23, i8** %9
%24 = icmp ne i8* %23, null
br i1 %24, label %25, label %33
25: ; preds = %20
%26 = load i8*, i8** %9
%27 = load i8*, i8** %7
%28 = load i64, i64* %10
%29 = call i32 @strncpy(i8* %26, i8* %27, i64 %28)
%30 = load i8*, i8** %9
%31 = load i64, i64* %10
%32 = getelementptr inbounds i8, i8* %30, i64 %31
store i8* %32, i8** %8
br label %20
33: ; preds = %20
%34 = load i8*, i8** %5
store i8* %34, i8** %4
br label %35
35: ; preds = %33, %18
%36 = load i8*, i8** %4
ret i8* %36
}

LLVM IR (-O2):
define i8* @replace_string(i8* %0, i8* nocapture %1, i8* %2) {
%4 = tail call i64 @strlen(i8* dereferenceable(1) %2)
%5 = tail call i64 @strlen(i8* dereferenceable(1) %1)
%6 = icmp eq i64 %5, %4
br i1 %6, label %7, label %16
7: ; preds = %3
%8 = tail call i8* @strstr(i8* dereferenceable(1) %0, i8* dereferenceable(1) %1)
%9 = icmp eq i8* %8, null
br i1 %9, label %16, label %10
10: ; preds = %7, %10
%11 = phi i8* [ %14, %10 ], [ %8, %7 ]
%12 = tail call i32 @strncpy(i8* %11, i8* %2, i64 %4)
%13 = getelementptr inbounds i8, i8* %11, i64 %4
%14 = tail call i8* @strstr(i8* dereferenceable(1) %13, i8* dereferenceable(1) %1)
%15 = icmp eq i8* %14, null
br i1 %15, label %16, label %10
16: ; preds = %10, %7, %3
%17 = phi i8* [ null, %3 ], [ %0, %7 ], [ %0, %10 ]
ret i8* %17
}