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

/* Variables and functions */
char *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fputs(char const *, int /*<<< orphan*/ *);
size_t strlen(char const *);
char *strstr(char *, char const *);
void Replace(FILE *in, FILE *out, const char *search, const char *repl) {
  char buf[512];
  char *p;
  size_t slen = strlen(search);
  if (slen == 0)
    return;
  while (p = fgets(buf, sizeof(buf), in)) {
    char *str;
    while (str = strstr(p, search)) {
      *str = 0;
      fputs(p, out);
      fputs(repl, out);
      p = str + slen;
    }
    fputs(p, out);
  }
}


x86 assembly (clang, -O0):
<Replace>:
push   %rbp
mov    %rsp,%rbp
sub    $0x240,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x230(%rbp)
cmpq   $0x0,-0x230(%rbp)
jne    117e <Replace+0x3e>
jmp    1225 <Replace+0xe5>
jmp    1183 <Replace+0x43>
lea    -0x220(%rbp),%rdi
mov    -0x8(%rbp),%rdx
mov    $0x200,%esi
call   1050 <fgets@plt>
mov    %rax,-0x228(%rbp)
cmp    $0x0,%rax
je     1225 <Replace+0xe5>
jmp    11ae <Replace+0x6e>
mov    -0x228(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1060 <strstr@plt>
mov    %rax,-0x238(%rbp)
cmp    $0x0,%rax
je     1210 <Replace+0xd0>
mov    -0x238(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x228(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1040 <fputs@plt>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1040 <fputs@plt>
mov    -0x238(%rbp),%rax
add    -0x230(%rbp),%rax
mov    %rax,-0x228(%rbp)
jmp    11ae <Replace+0x6e>
mov    -0x228(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1040 <fputs@plt>
jmp    1183 <Replace+0x43>
add    $0x240,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<Replace>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x218,%rsp
mov    %rcx,%r14
mov    %rdx,%r15
mov    %rsi,%rbx
mov    %rdi,0x8(%rsp)
mov    %rdx,%rdi
call   1030 <strlen@plt>
test   %rax,%rax
je     1216 <Replace+0xd6>
mov    %rax,%r13
lea    0x10(%rsp),%rdi
mov    $0x200,%esi
mov    0x8(%rsp),%rdx
call   1050 <fgets@plt>
test   %rax,%rax
je     1216 <Replace+0xd6>
mov    %rax,%rbp
jmp    11c7 <Replace+0x87>
cs nopw 0x0(%rax,%rax,1)
nop
mov    %rbp,%rdi
mov    %rbx,%rsi
call   1040 <fputs@plt>
lea    0x10(%rsp),%rdi
mov    $0x200,%esi
mov    0x8(%rsp),%rdx
call   1050 <fgets@plt>
mov    %rax,%rbp
test   %rax,%rax
je     1216 <Replace+0xd6>
mov    %rbp,%rdi
mov    %r15,%rsi
call   1060 <strstr@plt>
test   %rax,%rax
je     11a0 <Replace+0x60>
mov    %rax,%r12
nopw   0x0(%rax,%rax,1)
movb   $0x0,(%r12)
mov    %rbp,%rdi
mov    %rbx,%rsi
call   1040 <fputs@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
call   1040 <fputs@plt>
mov    %r12,%rbp
add    %r13,%rbp
mov    %rbp,%rdi
mov    %r15,%rsi
call   1060 <strstr@plt>
mov    %rax,%r12
test   %rax,%rax
jne    11e0 <Replace+0xa0>
jmp    11a0 <Replace+0x60>
add    $0x218,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @Replace(i32* %0, i32* %1, i8* %2, i8* %3) {
%5 = alloca i32*
%6 = alloca i32*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca [512 x i8]
%10 = alloca i8*
%11 = alloca i64
%12 = alloca i8*
store i32* %0, i32** %5
store i32* %1, i32** %6
store i8* %2, i8** %7
store i8* %3, i8** %8
%13 = load i8*, i8** %7
%14 = call i64 @strlen(i8* %13)
store i64 %14, i64* %11
%15 = load i64, i64* %11
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %18
17: ; preds = %4
br label %45
18: ; preds = %4
br label %19
19: ; preds = %41, %18
%20 = getelementptr inbounds [512 x i8], [512 x i8]* %9, i64 0, i64 0
%21 = load i32*, i32** %5
%22 = call i8* @fgets(i8* %20, i32 512, i32* %21)
store i8* %22, i8** %10
%23 = icmp ne i8* %22, null
br i1 %23, label %24, label %45
24: ; preds = %19
br label %25
25: ; preds = %30, %24
%26 = load i8*, i8** %10
%27 = load i8*, i8** %7
%28 = call i8* @strstr(i8* %26, i8* %27)
store i8* %28, i8** %12
%29 = icmp ne i8* %28, null
br i1 %29, label %30, label %41
30: ; preds = %25
%31 = load i8*, i8** %12
store i8 0, i8* %31
%32 = load i8*, i8** %10
%33 = load i32*, i32** %6
%34 = call i32 @fputs(i8* %32, i32* %33)
%35 = load i8*, i8** %8
%36 = load i32*, i32** %6
%37 = call i32 @fputs(i8* %35, i32* %36)
%38 = load i8*, i8** %12
%39 = load i64, i64* %11
%40 = getelementptr inbounds i8, i8* %38, i64 %39
store i8* %40, i8** %10
br label %25
41: ; preds = %25
%42 = load i8*, i8** %10
%43 = load i32*, i32** %6
%44 = call i32 @fputs(i8* %42, i32* %43)
br label %19
45: ; preds = %17, %19
ret void
}

LLVM IR (-O2):
define void @Replace(i32* nocapture %0, i32* nocapture %1, i8* nocapture %2, i8* nocapture %3) {
%5 = alloca [512 x i8]
%6 = getelementptr inbounds [512 x i8], [512 x i8]* %5, i64 0, i64 0
%7 = tail call i64 @strlen(i8* dereferenceable(1) %2)
%8 = icmp eq i64 %7, 0
br i1 %8, label %29, label %9
9: ; preds = %4
%10 = call i8* @fgets(i8* %6, i32 512, i32* %0)
%11 = icmp eq i8* %10, null
br i1 %11, label %29, label %12
12: ; preds = %9, %24
%13 = phi i8* [ %27, %24 ], [ %10, %9 ]
%14 = call i8* @strstr(i8* dereferenceable(1) %13, i8* dereferenceable(1) %2)
%15 = icmp eq i8* %14, null
br i1 %15, label %24, label %16
16: ; preds = %12, %16
%17 = phi i8* [ %22, %16 ], [ %14, %12 ]
%18 = phi i8* [ %21, %16 ], [ %13, %12 ]
store i8 0, i8* %17
%19 = call i32 @fputs(i8* %18, i32* %1)
%20 = call i32 @fputs(i8* %3, i32* %1)
%21 = getelementptr inbounds i8, i8* %17, i64 %7
%22 = call i8* @strstr(i8* dereferenceable(1) %21, i8* dereferenceable(1) %2)
%23 = icmp eq i8* %22, null
br i1 %23, label %24, label %16
24: ; preds = %16, %12
%25 = phi i8* [ %13, %12 ], [ %21, %16 ]
%26 = call i32 @fputs(i8* %25, i32* %1)
%27 = call i8* @fgets(i8* %6, i32 512, i32* %0)
%28 = icmp eq i8* %27, null
br i1 %28, label %29, label %12
29: ; preds = %24, %9, %4
ret void
}