#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 */
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, char *);
int /*<<< orphan*/ *open_memstream(char **, size_t *);
int /*<<< orphan*/ putc(char, int /*<<< orphan*/ *);
scalar_t__ strcmp(char *, char *);
char *word_replace(char *orig, char *rep, char *with) {
  size_t size;
  char *outstring;
  FILE *stream = open_memstream(&outstring, &size);
  char *tmp = orig;
  int found_end = 0;
  while (!found_end) {
    while (*tmp != ' ' && *tmp != '\0') {
      tmp++;
    }
    if (*tmp == '\0')
      found_end = 1;
    *tmp = '\0';
    if (strcmp(orig, rep)) {
      fprintf(stream, "%s", orig);
    } else {
      fprintf(stream, "%s", with);
    }
    if (!found_end)
      *tmp = ' ';
    putc(*tmp, stream);
    orig = ++tmp;
  }
  fclose(stream);
  return outstring;
}


x86 assembly (clang, -O0):
<word_replace>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
lea    -0x28(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1030 <open_memstream@plt>
mov    %rax,-0x30(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x38(%rbp)
movl   $0x0,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    119a <word_replace+0x4a>
jmp    127f <word_replace+0x12f>
jmp    119f <word_replace+0x4f>
mov    -0x38(%rbp),%rax
movsbl (%rax),%ecx
xor    %eax,%eax
cmp    $0x20,%ecx
mov    %al,-0x3d(%rbp)
je     11c4 <word_replace+0x74>
mov    -0x38(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
setne  %al
mov    %al,-0x3d(%rbp)
mov    -0x3d(%rbp),%al
test   $0x1,%al
jne    11d4 <word_replace+0x84>
jmp    11e5 <word_replace+0x95>
mov    -0x38(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x38(%rbp)
jmp    119f <word_replace+0x4f>
mov    -0x38(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    11fc <word_replace+0xac>
movl   $0x1,-0x3c(%rbp)
mov    -0x38(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%rax
je     1233 <word_replace+0xe3>
mov    -0x30(%rbp),%rdi
mov    -0x8(%rbp),%rdx
lea    0xdd7(%rip),%rsi
call   1070 <fprintf@plt>
jmp    1247 <word_replace+0xf7>
mov    -0x30(%rbp),%rdi
mov    -0x18(%rbp),%rdx
lea    0xdbe(%rip),%rsi
call   1070 <fprintf@plt>
cmpl   $0x0,-0x3c(%rbp)
jne    1258 <word_replace+0x108>
mov    -0x38(%rbp),%rax
movb   $0x20,(%rax)
mov    -0x38(%rbp),%rax
mov    (%rax),%al
mov    -0x30(%rbp),%rsi
movsbl %al,%edi
call   1060 <putc@plt>
mov    -0x38(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x38(%rbp)
mov    %rax,-0x8(%rbp)
jmp    1184 <word_replace+0x34>
mov    -0x30(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x28(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<word_replace>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%r13
lea    0x8(%rsp),%rdi
lea    0x10(%rsp),%rsi
call   1030 <open_memstream@plt>
mov    %rax,%r12
nopl   0x0(%rax)
lea    0x1(%r13),%rbp
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
movzbl -0x1(%rbp),%ebx
test   $0xdf,%bl
je     11a0 <word_replace+0x50>
add    $0x1,%rbp
jmp    1190 <word_replace+0x40>
nop
movb   $0x0,-0x1(%rbp)
mov    %r13,%rdi
mov    %r15,%rsi
call   1060 <strcmp@plt>
test   %rax,%rax
cmove  %r14,%r13
mov    %r13,%rdi
mov    %r12,%rsi
call   1050 <fputs@plt>
test   %bl,%bl
je     11db <word_replace+0x8b>
movb   $0x20,-0x1(%rbp)
mov    $0x20,%edi
mov    %r12,%rsi
call   1070 <putc@plt>
mov    %rbp,%r13
jmp    1180 <word_replace+0x30>
movsbl -0x1(%rbp),%edi
mov    %r12,%rsi
call   1070 <putc@plt>
mov    %r12,%rdi
call   1040 <fclose@plt>
mov    0x8(%rsp),%rax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"%s\00"
define i8* @word_replace(i8* %0, i8* %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i8*
%9 = alloca i32*
%10 = alloca i8*
%11 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
store i8* %2, i8** %6
%12 = call i32* @open_memstream(i8** %8, i64* %7)
store i32* %12, i32** %9
%13 = load i8*, i8** %4
store i8* %13, i8** %10
store i32 0, i32* %11
br label %14
14: ; preds = %59, %3
%15 = load i32, i32* %11
%16 = icmp ne i32 %15, 0
%17 = xor i1 %16, true
br i1 %17, label %18, label %66
18: ; preds = %14
br label %19
19: ; preds = %31, %18
%20 = load i8*, i8** %10
%21 = load i8, i8* %20
%22 = sext i8 %21 to i32
%23 = icmp ne i32 %22, 32
br i1 %23, label %24, label %29
24: ; preds = %19
%25 = load i8*, i8** %10
%26 = load i8, i8* %25
%27 = sext i8 %26 to i32
%28 = icmp ne i32 %27, 0
br label %29
29: ; preds = %24, %19
%30 = phi i1 [ false, %19 ], [ %28, %24 ]
br i1 %30, label %31, label %34
31: ; preds = %29
%32 = load i8*, i8** %10
%33 = getelementptr inbounds i8, i8* %32, i32 1
store i8* %33, i8** %10
br label %19
34: ; preds = %29
%35 = load i8*, i8** %10
%36 = load i8, i8* %35
%37 = sext i8 %36 to i32
%38 = icmp eq i32 %37, 0
br i1 %38, label %39, label %40
39: ; preds = %34
store i32 1, i32* %11
br label %40
40: ; preds = %39, %34
%41 = load i8*, i8** %10
store i8 0, i8* %41
%42 = load i8*, i8** %4
%43 = load i8*, i8** %5
%44 = call i64 @strcmp(i8* %42, i8* %43)
%45 = icmp ne i64 %44, 0
br i1 %45, label %46, label %50
46: ; preds = %40
%47 = load i32*, i32** %9
%48 = load i8*, i8** %4
%49 = call i32 @fprintf(i32* %47, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %48)
br label %54
50: ; preds = %40
%51 = load i32*, i32** %9
%52 = load i8*, i8** %6
%53 = call i32 @fprintf(i32* %51, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %52)
br label %54
54: ; preds = %50, %46
%55 = load i32, i32* %11
%56 = icmp ne i32 %55, 0
br i1 %56, label %59, label %57
57: ; preds = %54
%58 = load i8*, i8** %10
store i8 32, i8* %58
br label %59
59: ; preds = %57, %54
%60 = load i8*, i8** %10
%61 = load i8, i8* %60
%62 = load i32*, i32** %9
%63 = call i32 @putc(i8 signext %61, i32* %62)
%64 = load i8*, i8** %10
%65 = getelementptr inbounds i8, i8* %64, i32 1
store i8* %65, i8** %10
store i8* %65, i8** %4
br label %14
66: ; preds = %14
%67 = load i32*, i32** %9
%68 = call i32 @fclose(i32* %67)
%69 = load i8*, i8** %8
ret i8* %69
}

LLVM IR (-O2):
define i8* @word_replace(i8* %0, i8* %1, i8* nocapture %2) {
%4 = alloca i64
%5 = alloca i8*
%6 = bitcast i64* %4 to i8*
%7 = bitcast i8** %5 to i8*
%8 = call i32* @open_memstream(i8** %5, i64* %4)
br label %9
9: ; preds = %22, %3
%10 = phi i8* [ %0, %3 ], [ %24, %22 ]
br label %11
11: ; preds = %9, %14
%12 = phi i8* [ %15, %14 ], [ %10, %9 ]
%13 = load i8, i8* %12
switch i8 %13, label %14 [
i8 32, label %16
i8 0, label %16
]
14: ; preds = %11
%15 = getelementptr inbounds i8, i8* %12, i64 1
br label %11
16: ; preds = %11, %11
%17 = icmp eq i8 %13, 0
store i8 0, i8* %12
%18 = call i64 @strcmp(i8* %10, i8* %1)
%19 = icmp eq i64 %18, 0
%20 = select i1 %19, i8* %2, i8* %10
%21 = call i32 @fputs(i8* %20, i32* %8)
br i1 %17, label %25, label %22
22: ; preds = %16
store i8 32, i8* %12
%23 = call i32 @putc(i8 signext 32, i32* %8)
%24 = getelementptr inbounds i8, i8* %12, i64 1
br label %9
25: ; preds = %16
%26 = load i8, i8* %12
%27 = call i32 @putc(i8 signext %26, i32* %8)
%28 = call i32 @fclose(i32* %8)
%29 = load i8*, i8** %5
ret i8* %29
}