#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 */
char EOF;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
char fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ findAndChange(int /*<<< orphan*/ *, char *, char *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fputc(char, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ puts(char *);
int /*<<< orphan*/ scanf(char *, char *);
int main(int argc, char *argv[]) {
  char ch;
  printf("Enter the file name: \n");
  char searchingWord[100];
  char replacingWord[100];
  if (argc != 2) {
    printf("usage : %s filename\n", argv[0]);
  } else {
    FILE *userFile = fopen(argv[1], "r");
    FILE *tempFile = fopen("temp.txt", "w");
    if (userFile == NULL || tempFile == NULL) {
      puts("fail to open file");
      return -1;
    }
    while ((ch = fgetc(userFile)) != EOF)
      fputc(ch, tempFile);
    fclose(userFile);
    puts("Enter the word you want to find:");
    scanf("%s", searchingWord);
    puts("Enter the word replacing found word: ");
    scanf("%s", replacingWord);
    findAndChange(tempFile, searchingWord, replacingWord);
    FILE *modifiedFile = fopen("modified.txt", "w");
    while ((ch = fgetc(tempFile)) != EOF)
      fputc(ch, modifiedFile);
    fclose(tempFile);
    fclose(modifiedFile);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x110,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe60(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
cmpl   $0x2,-0x8(%rbp)
je     11cb <main+0x4b>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe58(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    133f <main+0x1bf>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe52(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0xf8(%rbp)
lea    0xe41(%rip),%rdi
lea    0xe43(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x100(%rbp)
cmpq   $0x0,-0xf8(%rbp)
je     121c <main+0x9c>
cmpq   $0x0,-0x100(%rbp)
jne    1234 <main+0xb4>
lea    0xe16(%rip),%rdi
call   1030 <puts@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1346 <main+0x1c6>
jmp    1239 <main+0xb9>
mov    -0xf8(%rbp),%rdi
call   1070 <fgetc@plt>
mov    %al,-0x11(%rbp)
movsbl %al,%eax
mov    0x2d96(%rip),%rcx
movsbl (%rcx),%ecx
cmp    %ecx,%eax
je     1274 <main+0xf4>
mov    -0x11(%rbp),%al
mov    -0x100(%rbp),%rsi
movsbl %al,%edi
call   1080 <fputc@plt>
jmp    1239 <main+0xb9>
mov    -0xf8(%rbp),%rdi
call   1040 <fclose@plt>
lea    0xdc4(%rip),%rdi
call   1030 <puts@plt>
lea    -0x80(%rbp),%rsi
lea    0xdd5(%rip),%rdi
call   1090 <scanf@plt>
lea    0xdcc(%rip),%rdi
call   1030 <puts@plt>
lea    -0xf0(%rbp),%rsi
lea    0xdb6(%rip),%rdi
call   1090 <scanf@plt>
mov    -0x100(%rbp),%rdi
lea    -0x80(%rbp),%rsi
lea    -0xf0(%rbp),%rdx
call   1060 <findAndChange@plt>
lea    0xdbc(%rip),%rdi
lea    0xd57(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x108(%rbp)
mov    -0x100(%rbp),%rdi
call   1070 <fgetc@plt>
mov    %al,-0x11(%rbp)
movsbl %al,%eax
mov    0x2ce3(%rip),%rcx
movsbl (%rcx),%ecx
cmp    %ecx,%eax
je     1327 <main+0x1a7>
mov    -0x11(%rbp),%al
mov    -0x108(%rbp),%rsi
movsbl %al,%edi
call   1080 <fputc@plt>
jmp    12ec <main+0x16c>
mov    -0x100(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x108(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x110,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0xe0,%rsp
mov    %rsi,%rbx
mov    %edi,%ebp
lea    0xef0(%rip),%rdi
call   1030 <puts@plt>
cmp    $0x2,%ebp
jne    12aa <main+0x12a>
mov    0x8(%rbx),%rdi
lea    0xe61(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,%rbx
lea    0xe54(%rip),%rdi
lea    0xe56(%rip),%rsi
call   10a0 <fopen@plt>
test   %rbx,%rbx
je     12bf <main+0x13f>
mov    %rax,%r14
test   %rax,%rax
je     12bf <main+0x13f>
mov    %rbx,%rdi
call   1070 <fgetc@plt>
mov    0x2df5(%rip),%rbp
cmp    0x0(%rbp),%al
je     1218 <main+0x98>
nopl   0x0(%rax,%rax,1)
movsbl %al,%edi
mov    %r14,%rsi
call   1080 <fputc@plt>
mov    %rbx,%rdi
call   1070 <fgetc@plt>
cmp    0x0(%rbp),%al
jne    1200 <main+0x80>
mov    %rbx,%rdi
call   1040 <fclose@plt>
lea    0xe0d(%rip),%rdi
call   1030 <puts@plt>
lea    0xe22(%rip),%r12
lea    0x70(%rsp),%r15
mov    %r12,%rdi
mov    %r15,%rsi
call   1090 <scanf@plt>
lea    0xe0e(%rip),%rdi
call   1030 <puts@plt>
mov    %rsp,%rbx
mov    %r12,%rdi
mov    %rbx,%rsi
call   1090 <scanf@plt>
mov    %r14,%rdi
mov    %r15,%rsi
mov    %rbx,%rdx
call   1060 <findAndChange@plt>
lea    0xe0c(%rip),%rdi
lea    0xda7(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %r14,%rdi
call   1070 <fgetc@plt>
cmp    0x0(%rbp),%al
je     12d2 <main+0x152>
movsbl %al,%edi
mov    %rbx,%rsi
call   1080 <fputc@plt>
jmp    1290 <main+0x110>
mov    (%rbx),%rsi
lea    0xd4c(%rip),%rdi
xor    %ebx,%ebx
xor    %eax,%eax
call   1050 <printf@plt>
jmp    12e4 <main+0x164>
lea    0xd5c(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%ebx
jmp    12e4 <main+0x164>
mov    %r14,%rdi
call   1040 <fclose@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
xor    %ebx,%ebx
mov    %ebx,%eax
add    $0xe0,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [23 x i8] c"Enter the file name: \0A\00"
@.str.1 = [21 x i8] c"usage : %s filename\0A\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [9 x i8] c"temp.txt\00"
@.str.4 = [2 x i8] c"w\00"
@.str.5 = [18 x i8] c"fail to open file\00"
@EOF = global i8 0
@.str.6 = [33 x i8] c"Enter the word you want to find:\00"
@.str.7 = [3 x i8] c"%s\00"
@.str.8 = [38 x i8] c"Enter the word replacing found word: \00"
@.str.9 = [13 x i8] c"modified.txt\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8
%7 = alloca [100 x i8]
%8 = alloca [100 x i8]
%9 = alloca i32*
%10 = alloca i32*
%11 = alloca i32*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
%13 = load i32, i32* %4
%14 = icmp ne i32 %13, 2
br i1 %14, label %15, label %20
15: ; preds = %2
%16 = load i8**, i8*** %5
%17 = getelementptr inbounds i8*, i8** %16, i64 0
%18 = load i8*, i8** %17
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i8* %18)
br label %75
20: ; preds = %2
%21 = load i8**, i8*** %5
%22 = getelementptr inbounds i8*, i8** %21, i64 1
%23 = load i8*, i8** %22
%24 = call i32* @fopen(i8* %23, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %24, i32** %9
%25 = call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
store i32* %25, i32** %10
%26 = load i32*, i32** %9
%27 = icmp eq i32* %26, null
br i1 %27, label %31, label %28
28: ; preds = %20
%29 = load i32*, i32** %10
%30 = icmp eq i32* %29, null
br i1 %30, label %31, label %33
31: ; preds = %28, %20
%32 = call i32 @puts(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.5, i64 0, i64 0))
store i32 -1, i32* %3
br label %76
33: ; preds = %28
br label %34
34: ; preds = %41, %33
%35 = load i32*, i32** %9
%36 = call signext i8 @fgetc(i32* %35)
store i8 %36, i8* %6
%37 = sext i8 %36 to i32
%38 = load i8, i8* @EOF
%39 = sext i8 %38 to i32
%40 = icmp ne i32 %37, %39
br i1 %40, label %41, label %45
41: ; preds = %34
%42 = load i8, i8* %6
%43 = load i32*, i32** %10
%44 = call i32 @fputc(i8 signext %42, i32* %43)
br label %34
45: ; preds = %34
%46 = load i32*, i32** %9
%47 = call i32 @fclose(i32* %46)
%48 = call i32 @puts(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.6, i64 0, i64 0))
%49 = getelementptr inbounds [100 x i8], [100 x i8]* %7, i64 0, i64 0
%50 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.7, i64 0, i64 0), i8* %49)
%51 = call i32 @puts(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.8, i64 0, i64 0))
%52 = getelementptr inbounds [100 x i8], [100 x i8]* %8, i64 0, i64 0
%53 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.7, i64 0, i64 0), i8* %52)
%54 = load i32*, i32** %10
%55 = getelementptr inbounds [100 x i8], [100 x i8]* %7, i64 0, i64 0
%56 = getelementptr inbounds [100 x i8], [100 x i8]* %8, i64 0, i64 0
%57 = call i32 @findAndChange(i32* %54, i8* %55, i8* %56)
%58 = call i32* @fopen(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.9, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
store i32* %58, i32** %11
br label %59
59: ; preds = %66, %45
%60 = load i32*, i32** %10
%61 = call signext i8 @fgetc(i32* %60)
store i8 %61, i8* %6
%62 = sext i8 %61 to i32
%63 = load i8, i8* @EOF
%64 = sext i8 %63 to i32
%65 = icmp ne i32 %62, %64
br i1 %65, label %66, label %70
66: ; preds = %59
%67 = load i8, i8* %6
%68 = load i32*, i32** %11
%69 = call i32 @fputc(i8 signext %67, i32* %68)
br label %59
70: ; preds = %59
%71 = load i32*, i32** %10
%72 = call i32 @fclose(i32* %71)
%73 = load i32*, i32** %11
%74 = call i32 @fclose(i32* %73)
br label %75
75: ; preds = %70, %15
store i32 0, i32* %3
br label %76
76: ; preds = %75, %31
%77 = load i32, i32* %3
ret i32 %77
}

LLVM IR (-O2):
@.str.1 = [21 x i8] c"usage : %s filename\0A\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [9 x i8] c"temp.txt\00"
@.str.4 = [2 x i8] c"w\00"
@.str.5 = [18 x i8] c"fail to open file\00"
@EOF = global i8 0
@.str.6 = [33 x i8] c"Enter the word you want to find:\00"
@.str.7 = [3 x i8] c"%s\00"
@.str.8 = [38 x i8] c"Enter the word replacing found word: \00"
@.str.9 = [13 x i8] c"modified.txt\00"
@str = [22 x i8] c"Enter the file name: \00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [100 x i8]
%4 = alloca [100 x i8]
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str, i64 0, i64 0))
%6 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
%7 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 0
%8 = icmp eq i32 %0, 2
br i1 %8, label %12, label %9
9: ; preds = %2
%10 = load i8*, i8** %1
%11 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i8* %10)
br label %52
12: ; preds = %2
%13 = getelementptr inbounds i8*, i8** %1, i64 1
%14 = load i8*, i8** %13
%15 = tail call i32* @fopen(i8* %14, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%16 = tail call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%17 = icmp eq i32* %15, null
%18 = icmp eq i32* %16, null
%19 = or i1 %17, %18
br i1 %19, label %24, label %20
20: ; preds = %12
%21 = tail call signext i8 @fgetc(i32* %15)
%22 = load i8, i8* @EOF
%23 = icmp eq i8 %21, %22
br i1 %23, label %32, label %26
24: ; preds = %12
%25 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.5, i64 0, i64 0))
br label %52
26: ; preds = %20, %26
%27 = phi i8 [ %29, %26 ], [ %21, %20 ]
%28 = tail call i32 @fputc(i8 signext %27, i32* %16)
%29 = tail call signext i8 @fgetc(i32* %15)
%30 = load i8, i8* @EOF
%31 = icmp eq i8 %29, %30
br i1 %31, label %32, label %26
32: ; preds = %26, %20
%33 = tail call i32 @fclose(i32* %15)
%34 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @.str.6, i64 0, i64 0))
%35 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.7, i64 0, i64 0), i8* %6)
%36 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @.str.8, i64 0, i64 0))
%37 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.7, i64 0, i64 0), i8* %7)
%38 = call i32 @findAndChange(i32* %16, i8* %6, i8* %7)
%39 = call i32* @fopen(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.9, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%40 = call signext i8 @fgetc(i32* %16)
%41 = load i8, i8* @EOF
%42 = icmp eq i8 %40, %41
br i1 %42, label %49, label %43
43: ; preds = %32, %43
%44 = phi i8 [ %46, %43 ], [ %40, %32 ]
%45 = call i32 @fputc(i8 signext %44, i32* %39)
%46 = call signext i8 @fgetc(i32* %16)
%47 = load i8, i8* @EOF
%48 = icmp eq i8 %46, %47
br i1 %48, label %49, label %43
49: ; preds = %43, %32
%50 = call i32 @fclose(i32* %16)
%51 = call i32 @fclose(i32* %39)
br label %52
52: ; preds = %49, %9, %24
%53 = phi i32 [ -1, %24 ], [ 0, %49 ], [ 0, %9 ]
ret i32 %53
}