#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*/ free(char *);
scalar_t__ malloc(int);
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
char *strstr(char *, char *);
int replace(char *b, char *n, char *r) {
  char *t, *save;
  int i = 0;
  while (t = strstr(b, n)) {
    save = (char *)malloc(strlen(t) - strlen(n) + 1);
    strcpy(save, t + strlen(n));
    *t = 0;
    strcat(b, r);
    strcat(b, save);
    free(save);
    i++;
  }
}


x86 assembly (clang, -O0):
<replace>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movl   $0x0,-0x34(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1080 <strstr@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
je     121f <replace+0xbf>
mov    -0x28(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x4c(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x4c(%rbp),%edi
sub    %eax,%edi
add    $0x1,%edi
call   1060 <malloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x48(%rbp),%rsi
mov    -0x40(%rbp),%rdi
cltq
add    %rax,%rsi
call   1040 <strcpy@plt>
mov    -0x28(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1070 <strcat@plt>
mov    -0x10(%rbp),%rdi
mov    -0x30(%rbp),%rsi
call   1070 <strcat@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    117b <replace+0x1b>
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<replace>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%r12
call   1080 <strstr@plt>
test   %rax,%rax
je     11eb <replace+0x8b>
mov    %rax,%rbx
xchg   %ax,%ax
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %eax,%ebp
mov    %r15,%rdi
call   1050 <strlen@plt>
sub    %eax,%ebp
add    $0x1,%ebp
mov    %ebp,%edi
call   1060 <malloc@plt>
mov    %rax,%rbp
mov    %r15,%rdi
call   1050 <strlen@plt>
movslq %eax,%rsi
add    %rbx,%rsi
mov    %rbp,%rdi
call   1040 <strcpy@plt>
movb   $0x0,(%rbx)
mov    %r12,%rdi
mov    %r14,%rsi
call   1070 <strcat@plt>
mov    %r12,%rdi
mov    %rbp,%rsi
call   1070 <strcat@plt>
mov    %rbp,%rdi
call   1030 <free@plt>
mov    %r12,%rdi
mov    %r15,%rsi
call   1080 <strstr@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    1180 <replace+0x20>
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define i32 @replace(i8* %0, i8* %1, i8* %2) {
%4 = tail call i8* @strstr(i8* dereferenceable(1) %0, i8* dereferenceable(1) %1)
%5 = icmp eq i8* %4, null
br i1 %5, label %23, label %6
6: ; preds = %3, %6
%7 = phi i8* [ %21, %6 ], [ %4, %3 ]
%8 = tail call i32 @strlen(i8* %7)
%9 = tail call i32 @strlen(i8* %1)
%10 = add i32 %8, 1
%11 = sub i32 %10, %9
%12 = tail call i64 @malloc(i32 %11)
%13 = inttoptr i64 %12 to i8*
%14 = tail call i32 @strlen(i8* %1)
%15 = sext i32 %14 to i64
%16 = getelementptr inbounds i8, i8* %7, i64 %15
%17 = tail call i32 @strcpy(i8* %13, i8* %16)
store i8 0, i8* %7
%18 = tail call i32 @strcat(i8* %0, i8* %2)
%19 = tail call i32 @strcat(i8* %0, i8* %13)
%20 = tail call i32 @free(i8* %13)
%21 = tail call i8* @strstr(i8* dereferenceable(1) %0, i8* dereferenceable(1) %1)
%22 = icmp eq i8* %21, null
br i1 %22, label %23, label %6
23: ; preds = %6, %3
ret i32 undef
}