#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 EXIT_SUCCESS;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fputc(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fputs(char const *, int /*<<< orphan*/ *);
size_t strlen(char const *);
char *strstr(char *, char const *);
int main() {
  const char fileOrig[32] = "/etc/passwd";
  const char fileRepl[32] = "output.txt";
  const char text2find[80] = "b3h3m0th";
  const char text2repl[80] = "NOBODY USER";
  char buffer[120 + 2];
  char *buff_ptr, *find_ptr;
  FILE *fp1, *fp2;
  size_t find_len = strlen(text2find);
  fp1 = fopen(fileOrig, "r");
  fp2 = fopen(fileRepl, "w");
  while (fgets(buffer, 120 + 2, fp1)) {
    buff_ptr = buffer;
    while ((find_ptr = strstr(buff_ptr, text2find))) {
      while (buff_ptr < find_ptr)
        fputc((int)*buff_ptr++, fp2);
      fputs(text2repl, fp2);
      buff_ptr += find_len;
    }
    fputs(buff_ptr, fp2);
  }
  fclose(fp2);
  fclose(fp1);
  return (EXIT_SUCCESS);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1a0,%rsp
movl   $0x0,-0x4(%rbp)
mov    0xe67(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    0xe64(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    0xe61(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    0xe5e(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    0xe5b(%rip),%rax
mov    %rax,-0x50(%rbp)
mov    0xe58(%rip),%rax
mov    %rax,-0x48(%rbp)
mov    0xe55(%rip),%rax
mov    %rax,-0x40(%rbp)
mov    0xe52(%rip),%rax
mov    %rax,-0x38(%rbp)
lea    -0xa0(%rbp),%rdi
lea    0xe48(%rip),%rsi
mov    $0x50,%edx
call   1080 <memcpy@plt>
lea    -0xf0(%rbp),%rdi
lea    0xe80(%rip),%rsi
mov    $0x50,%edx
call   1080 <memcpy@plt>
lea    -0xa0(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x198(%rbp)
lea    -0x30(%rbp),%rdi
lea    0xea8(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x188(%rbp)
lea    -0x50(%rbp),%rdi
lea    0xe93(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x190(%rbp)
lea    -0x170(%rbp),%rdi
mov    -0x188(%rbp),%rdx
mov    $0x7a,%esi
call   1070 <fgets@plt>
cmp    $0x0,%rax
je     1336 <main+0x1b6>
lea    -0x170(%rbp),%rax
mov    %rax,-0x178(%rbp)
mov    -0x178(%rbp),%rdi
lea    -0xa0(%rbp),%rsi
call   10a0 <strstr@plt>
mov    %rax,-0x180(%rbp)
cmp    $0x0,%rax
je     131e <main+0x19e>
jmp    12b4 <main+0x134>
mov    -0x178(%rbp),%rax
cmp    -0x180(%rbp),%rax
jae    12f1 <main+0x171>
mov    -0x178(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x178(%rbp)
movsbl (%rax),%edi
mov    -0x190(%rbp),%rsi
call   1060 <fputc@plt>
jmp    12b4 <main+0x134>
lea    -0xf0(%rbp),%rdi
mov    -0x190(%rbp),%rsi
call   1050 <fputs@plt>
mov    -0x198(%rbp),%rax
add    -0x178(%rbp),%rax
mov    %rax,-0x178(%rbp)
jmp    128b <main+0x10b>
mov    -0x178(%rbp),%rdi
mov    -0x190(%rbp),%rsi
call   1050 <fputs@plt>
jmp    125b <main+0xdb>
mov    -0x190(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x188(%rbp),%rdi
call   1030 <fclose@plt>
mov    0x2c8b(%rip),%rax
mov    (%rax),%eax
add    $0x1a0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x128,%rsp
movaps 0xe98(%rip),%xmm0
movaps %xmm0,0x40(%rsp)
movaps 0xe7c(%rip),%xmm0
movaps %xmm0,0x30(%rsp)
movaps 0xea0(%rip),%xmm0
movaps %xmm0,0x20(%rsp)
movaps 0xe84(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
movaps 0xf28(%rip),%xmm0
movaps %xmm0,0x90(%rsp)
movaps 0xf09(%rip),%xmm0
movaps %xmm0,0x80(%rsp)
movaps 0xeea(%rip),%xmm0
movaps %xmm0,0x70(%rsp)
movaps 0xece(%rip),%xmm0
movaps %xmm0,0x60(%rsp)
movaps 0xeb2(%rip),%xmm0
movaps %xmm0,0x50(%rsp)
lea    0xef6(%rip),%rsi
lea    0x30(%rsp),%rdi
call   1070 <fopen@plt>
mov    %rax,%rbp
lea    0xee4(%rip),%rsi
lea    0x10(%rsp),%rdi
call   1070 <fopen@plt>
mov    %rax,%rbx
lea    0xa0(%rsp),%rdi
mov    $0x7a,%esi
mov    %rbp,0x8(%rsp)
mov    %rbp,%rdx
mov    %rdi,%rbp
call   1060 <fgets@plt>
test   %rax,%rax
je     12f6 <main+0x196>
lea    0xe08(%rip),%r12
lea    0x50(%rsp),%r14
jmp    126e <main+0x10e>
nop
mov    %rbp,%rdi
mov    %rbx,%rsi
call   1040 <fputs@plt>
lea    0xa0(%rsp),%rbp
mov    %rbp,%rdi
mov    $0x7a,%esi
mov    0x8(%rsp),%rdx
call   1060 <fgets@plt>
test   %rax,%rax
je     12f6 <main+0x196>
mov    %rbp,%rdi
mov    %r12,%rsi
call   1080 <strstr@plt>
test   %rax,%rax
je     1240 <main+0xe0>
mov    %rax,%r15
lea    0xa0(%rsp),%rbp
jmp    12b2 <main+0x152>
nopl   0x0(%rax,%rax,1)
mov    %r14,%rdi
mov    %rbx,%rsi
call   1040 <fputs@plt>
add    $0x8,%rbp
mov    %rbp,%rdi
mov    %r12,%rsi
call   1080 <strstr@plt>
mov    %rax,%r15
test   %rax,%rax
je     1240 <main+0xe0>
cmp    %r15,%rbp
jae    1290 <main+0x130>
mov    %r14,%r12
mov    %r15,%r14
sub    %rbp,%r14
xor    %r13d,%r13d
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
movsbl 0x0(%rbp,%r13,1),%edi
mov    %rbx,%rsi
call   1050 <fputc@plt>
add    $0x1,%r13
cmp    %r13,%r14
jne    12d0 <main+0x170>
mov    %r15,%rbp
mov    %r12,%r14
lea    0xd4c(%rip),%r12
jmp    1290 <main+0x130>
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    0x8(%rsp),%rdi
call   1030 <fclose@plt>
mov    0x2cd1(%rip),%rax
mov    (%rax),%eax
add    $0x128,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@__const.main.fileOrig = [32 x i8] c"/etc/passwd\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00"
@__const.main.fileRepl = [32 x i8] c"output.txt\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00"
@__const.main.text2find = [80 x i8] c"b3h3m0th\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00"
@__const.main.text2repl = [80 x i8] c"NOBODY USER\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00"
@.str = [2 x i8] c"r\00"
@.str.1 = [2 x i8] c"w\00"
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca [32 x i8]
%3 = alloca [32 x i8]
%4 = alloca [80 x i8]
%5 = alloca [80 x i8]
%6 = alloca [122 x i8]
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32*
%10 = alloca i32*
%11 = alloca i64
store i32 0, i32* %1
%12 = bitcast [32 x i8]* %2 to i8*
%13 = bitcast [32 x i8]* %3 to i8*
%14 = bitcast [80 x i8]* %4 to i8*
%15 = bitcast [80 x i8]* %5 to i8*
%16 = getelementptr inbounds [80 x i8], [80 x i8]* %4, i64 0, i64 0
%17 = call i64 @strlen(i8* %16)
store i64 %17, i64* %11
%18 = getelementptr inbounds [32 x i8], [32 x i8]* %2, i64 0, i64 0
%19 = call i32* @fopen(i8* %18, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %19, i32** %9
%20 = getelementptr inbounds [32 x i8], [32 x i8]* %3, i64 0, i64 0
%21 = call i32* @fopen(i8* %20, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %21, i32** %10
br label %22
22: ; preds = %53, %0
%23 = getelementptr inbounds [122 x i8], [122 x i8]* %6, i64 0, i64 0
%24 = load i32*, i32** %9
%25 = call i64 @fgets(i8* %23, i32 122, i32* %24)
%26 = icmp ne i64 %25, 0
br i1 %26, label %27, label %57
27: ; preds = %22
%28 = getelementptr inbounds [122 x i8], [122 x i8]* %6, i64 0, i64 0
store i8* %28, i8** %7
br label %29
29: ; preds = %46, %27
%30 = load i8*, i8** %7
%31 = getelementptr inbounds [80 x i8], [80 x i8]* %4, i64 0, i64 0
%32 = call i8* @strstr(i8* %30, i8* %31)
store i8* %32, i8** %8
%33 = icmp ne i8* %32, null
br i1 %33, label %34, label %53
34: ; preds = %29
br label %35
35: ; preds = %39, %34
%36 = load i8*, i8** %7
%37 = load i8*, i8** %8
%38 = icmp ult i8* %36, %37
br i1 %38, label %39, label %46
39: ; preds = %35
%40 = load i8*, i8** %7
%41 = getelementptr inbounds i8, i8* %40, i32 1
store i8* %41, i8** %7
%42 = load i8, i8* %40
%43 = sext i8 %42 to i32
%44 = load i32*, i32** %10
%45 = call i32 @fputc(i32 %43, i32* %44)
br label %35
46: ; preds = %35
%47 = getelementptr inbounds [80 x i8], [80 x i8]* %5, i64 0, i64 0
%48 = load i32*, i32** %10
%49 = call i32 @fputs(i8* %47, i32* %48)
%50 = load i64, i64* %11
%51 = load i8*, i8** %7
%52 = getelementptr inbounds i8, i8* %51, i64 %50
store i8* %52, i8** %7
br label %29
53: ; preds = %29
%54 = load i8*, i8** %7
%55 = load i32*, i32** %10
%56 = call i32 @fputs(i8* %54, i32* %55)
br label %22
57: ; preds = %22
%58 = load i32*, i32** %10
%59 = call i32 @fclose(i32* %58)
%60 = load i32*, i32** %9
%61 = call i32 @fclose(i32* %60)
%62 = load i32, i32* @EXIT_SUCCESS
ret i32 %62
}

LLVM IR (-O2):
@__const.main.fileOrig = [32 x i8] c"/etc/passwd\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00"
@__const.main.fileRepl = [32 x i8] c"output.txt\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00"
@__const.main.text2find = [80 x i8] c"b3h3m0th\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00"
@__const.main.text2repl = [80 x i8] c"NOBODY USER\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00"
@.str = [2 x i8] c"r\00"
@.str.1 = [2 x i8] c"w\00"
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = alloca [32 x i8]
%2 = alloca [32 x i8]
%3 = alloca [80 x i8]
%4 = alloca [122 x i8]
%5 = getelementptr inbounds [32 x i8], [32 x i8]* %1, i64 0, i64 0
%6 = getelementptr inbounds [32 x i8], [32 x i8]* %2, i64 0, i64 0
%7 = getelementptr inbounds [80 x i8], [80 x i8]* %3, i64 0, i64 0
%8 = getelementptr inbounds [122 x i8], [122 x i8]* %4, i64 0, i64 0
%9 = call i32* @fopen(i8* %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%10 = call i32* @fopen(i8* %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%11 = call i64 @fgets(i8* %8, i32 122, i32* %9)
%12 = icmp eq i64 %11, 0
br i1 %12, label %43, label %13
13: ; preds = %0, %38
%14 = call i8* @strstr(i8* dereferenceable(1) %8, i8* dereferenceable(1) getelementptr inbounds ([80 x i8], [80 x i8]* @__const.main.text2find, i64 0, i64 0))
%15 = icmp eq i8* %14, null
br i1 %15, label %38, label %16
16: ; preds = %13, %32
%17 = phi i8* [ %36, %32 ], [ %14, %13 ]
%18 = phi i8* [ %35, %32 ], [ %8, %13 ]
%19 = icmp ult i8* %18, %17
br i1 %19, label %20, label %32
20: ; preds = %16
%21 = ptrtoint i8* %18 to i64
%22 = ptrtoint i8* %17 to i64
%23 = sub i64 %22, %21
%24 = getelementptr i8, i8* %18, i64 %23
br label %25
25: ; preds = %20, %25
%26 = phi i8* [ %27, %25 ], [ %18, %20 ]
%27 = getelementptr inbounds i8, i8* %26, i64 1
%28 = load i8, i8* %26
%29 = sext i8 %28 to i32
%30 = call i32 @fputc(i32 %29, i32* %10)
%31 = icmp eq i8* %27, %24
br i1 %31, label %32, label %25
32: ; preds = %25, %16
%33 = phi i8* [ %18, %16 ], [ %24, %25 ]
%34 = call i32 @fputs(i8* %7, i32* %10)
%35 = getelementptr inbounds i8, i8* %33, i64 8
%36 = call i8* @strstr(i8* dereferenceable(1) %35, i8* dereferenceable(1) getelementptr inbounds ([80 x i8], [80 x i8]* @__const.main.text2find, i64 0, i64 0))
%37 = icmp eq i8* %36, null
br i1 %37, label %38, label %16
38: ; preds = %32, %13
%39 = phi i8* [ %8, %13 ], [ %35, %32 ]
%40 = call i32 @fputs(i8* %39, i32* %10)
%41 = call i64 @fgets(i8* %8, i32 122, i32* %9)
%42 = icmp eq i64 %41, 0
br i1 %42, label %43, label %13
43: ; preds = %38, %0
%44 = call i32 @fclose(i32* %10)
%45 = call i32 @fclose(i32* %9)
%46 = load i32, i32* @EXIT_SUCCESS
ret i32 %46
}