#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*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int string_length(char *);
int strncmp(char *, char *, int);
int /*<<< orphan*/ strncpy(char *, char *, int);
int main(int argc, char *argv[]) {
  printf("Number of arguments: %d\n", argc);
  printf("Command: %s\n", argv[0]);
  printf("Old string: %s\n", argv[1]);
  printf("New string: %s\n", argv[2]);
  printf("File name: %s\n", argv[3]);
  char inBuffer[5000];
  char outBuffer[5000];
  int file = open(argv[3], O_RDONLY);
  int count;
  int oldLength = string_length(argv[1]);
  int newLength = string_length(argv[2]);
  char *oldString = argv[1];
  char *newString = argv[2];
  int inIndex;
  int outIndex;
  while (1) {
    count = read(file, inBuffer, 5000);
    for (inIndex = 0; inIndex < count;) {
      int result = strncmp(inBuffer + inIndex, oldString, oldLength);
      if (result == 0) {
        strncpy(outBuffer + outIndex, newString, newLength);
        inIndex += oldLength;
        outIndex += newLength;
      } else {
        outBuffer[outIndex] = inBuffer[inIndex];
        inIndex++;
        outIndex++;
      }
    }
    if (count == 0)
      break;
  }
  printf("Input file: %s \n", inBuffer);
  printf("Output file: %s \n", outBuffer);
  close(file);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x2760,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%esi
lea    0xe6d(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe71(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xe68(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rsi
lea    0xe62(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rsi
lea    0xe5c(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
mov    0x2de8(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0x2734(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1080 <string_length@plt>
mov    %eax,-0x273c(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   1080 <string_length@plt>
mov    %eax,-0x2740(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x2748(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x2750(%rbp)
mov    -0x2734(%rbp),%edi
lea    -0x13a0(%rbp),%rsi
mov    $0x1388,%edx
call   1070 <read@plt>
mov    %eax,-0x2738(%rbp)
movl   $0x0,-0x2754(%rbp)
mov    -0x2754(%rbp),%eax
cmp    -0x2738(%rbp),%eax
jge    134b <main+0x1db>
lea    -0x13a0(%rbp),%rdi
movslq -0x2754(%rbp),%rax
add    %rax,%rdi
mov    -0x2748(%rbp),%rsi
mov    -0x273c(%rbp),%edx
call   1040 <strncmp@plt>
mov    %eax,-0x275c(%rbp)
cmpl   $0x0,-0x275c(%rbp)
jne    130c <main+0x19c>
lea    -0x2730(%rbp),%rdi
movslq -0x2758(%rbp),%rax
add    %rax,%rdi
mov    -0x2750(%rbp),%rsi
mov    -0x2740(%rbp),%edx
call   1030 <strncpy@plt>
mov    -0x273c(%rbp),%eax
add    -0x2754(%rbp),%eax
mov    %eax,-0x2754(%rbp)
mov    -0x2740(%rbp),%eax
add    -0x2758(%rbp),%eax
mov    %eax,-0x2758(%rbp)
jmp    1346 <main+0x1d6>
movslq -0x2754(%rbp),%rax
mov    -0x13a0(%rbp,%rax,1),%cl
movslq -0x2758(%rbp),%rax
mov    %cl,-0x2730(%rbp,%rax,1)
mov    -0x2754(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2754(%rbp)
mov    -0x2758(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2758(%rbp)
jmp    1278 <main+0x108>
cmpl   $0x0,-0x2738(%rbp)
jne    135d <main+0x1ed>
jmp    1362 <main+0x1f2>
jmp    1251 <main+0xe1>
lea    -0x13a0(%rbp),%rsi
lea    0xce5(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    -0x2730(%rbp),%rsi
lea    0xce1(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x2734(%rbp),%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x2760,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x2728,%rsp
mov    %rsi,%r12
mov    %edi,%esi
lea    0xe73(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    (%r12),%rsi
lea    0xe7a(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x8(%r12),%rsi
lea    0xe74(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x10(%r12),%rsi
lea    0xe71(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x18(%r12),%rsi
lea    0xe6e(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x18(%r12),%rdi
mov    0x2dfd(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
mov    %eax,(%rsp)
mov    0x8(%r12),%rdi
call   1080 <string_length@plt>
mov    %eax,%ebp
mov    0x10(%r12),%rdi
call   1080 <string_length@plt>
mov    %eax,0x4(%rsp)
mov    0x8(%r12),%rbx
mov    0x10(%r12),%rax
mov    %rax,0x8(%rsp)
jmp    1229 <main+0xb9>
test   %r13d,%r13d
je     12bd <main+0x14d>
mov    (%rsp),%edi
lea    0x13a0(%rsp),%rsi
mov    $0x1388,%edx
call   1070 <read@plt>
mov    %eax,%r13d
test   %eax,%eax
jle    1220 <main+0xb0>
xor    %r15d,%r15d
jmp    1270 <main+0x100>
nopw   0x0(%rax,%rax,1)
movzbl (%r14),%eax
movslq %r12d,%rcx
mov    %al,0x10(%rsp,%rcx,1)
mov    $0x1,%eax
mov    $0x1,%ecx
add    %ecx,%r15d
add    %eax,%r12d
cmp    %r13d,%r15d
jge    1220 <main+0xb0>
movslq %r15d,%rax
lea    (%rsp,%rax,1),%r14
add    $0x13a0,%r14
mov    %r14,%rdi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1040 <strncmp@plt>
test   %eax,%eax
jne    1250 <main+0xe0>
movslq %r12d,%rax
lea    (%rsp,%rax,1),%rdi
add    $0x10,%rdi
mov    0x8(%rsp),%rsi
mov    %ebp,%r14d
mov    %rbx,%rbp
mov    0x4(%rsp),%ebx
mov    %ebx,%edx
call   1030 <strncpy@plt>
mov    %ebx,%eax
mov    %rbp,%rbx
mov    %r14d,%ebp
mov    %r14d,%ecx
jmp    1265 <main+0xf5>
lea    0xd91(%rip),%rdi
lea    0x13a0(%rsp),%rsi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xd8c(%rip),%rdi
lea    0x10(%rsp),%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    (%rsp),%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x2728,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [25 x i8] c"Number of arguments: %d\0A\00"
@.str.1 = [13 x i8] c"Command: %s\0A\00"
@.str.2 = [16 x i8] c"Old string: %s\0A\00"
@.str.3 = [16 x i8] c"New string: %s\0A\00"
@.str.4 = [15 x i8] c"File name: %s\0A\00"
@O_RDONLY = global i32 0
@.str.5 = [17 x i8] c"Input file: %s \0A\00"
@.str.6 = [18 x i8] c"Output file: %s \0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [5000 x i8]
%7 = alloca [5000 x i8]
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i8*
%13 = alloca i8*
%14 = alloca i32
%15 = alloca i32
%16 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%17 = load i32, i32* %4
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i32 %17)
%19 = load i8**, i8*** %5
%20 = getelementptr inbounds i8*, i8** %19, i64 0
%21 = load i8*, i8** %20
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i8* %21)
%23 = load i8**, i8*** %5
%24 = getelementptr inbounds i8*, i8** %23, i64 1
%25 = load i8*, i8** %24
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i8* %25)
%27 = load i8**, i8*** %5
%28 = getelementptr inbounds i8*, i8** %27, i64 2
%29 = load i8*, i8** %28
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), i8* %29)
%31 = load i8**, i8*** %5
%32 = getelementptr inbounds i8*, i8** %31, i64 3
%33 = load i8*, i8** %32
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0), i8* %33)
%35 = load i8**, i8*** %5
%36 = getelementptr inbounds i8*, i8** %35, i64 3
%37 = load i8*, i8** %36
%38 = load i32, i32* @O_RDONLY
%39 = call i32 @open(i8* %37, i32 %38)
store i32 %39, i32* %8
%40 = load i8**, i8*** %5
%41 = getelementptr inbounds i8*, i8** %40, i64 1
%42 = load i8*, i8** %41
%43 = call i32 @string_length(i8* %42)
store i32 %43, i32* %10
%44 = load i8**, i8*** %5
%45 = getelementptr inbounds i8*, i8** %44, i64 2
%46 = load i8*, i8** %45
%47 = call i32 @string_length(i8* %46)
store i32 %47, i32* %11
%48 = load i8**, i8*** %5
%49 = getelementptr inbounds i8*, i8** %48, i64 1
%50 = load i8*, i8** %49
store i8* %50, i8** %12
%51 = load i8**, i8*** %5
%52 = getelementptr inbounds i8*, i8** %51, i64 2
%53 = load i8*, i8** %52
store i8* %53, i8** %13
br label %54
54: ; preds = %2, %103
%55 = load i32, i32* %8
%56 = getelementptr inbounds [5000 x i8], [5000 x i8]* %6, i64 0, i64 0
%57 = call i32 @read(i32 %55, i8* %56, i32 5000)
store i32 %57, i32* %9
store i32 0, i32* %14
br label %58
58: ; preds = %98, %54
%59 = load i32, i32* %14
%60 = load i32, i32* %9
%61 = icmp slt i32 %59, %60
br i1 %61, label %62, label %99
62: ; preds = %58
%63 = getelementptr inbounds [5000 x i8], [5000 x i8]* %6, i64 0, i64 0
%64 = load i32, i32* %14
%65 = sext i32 %64 to i64
%66 = getelementptr inbounds i8, i8* %63, i64 %65
%67 = load i8*, i8** %12
%68 = load i32, i32* %10
%69 = call i32 @strncmp(i8* %66, i8* %67, i32 %68)
store i32 %69, i32* %16
%70 = load i32, i32* %16
%71 = icmp eq i32 %70, 0
br i1 %71, label %72, label %86
72: ; preds = %62
%73 = getelementptr inbounds [5000 x i8], [5000 x i8]* %7, i64 0, i64 0
%74 = load i32, i32* %15
%75 = sext i32 %74 to i64
%76 = getelementptr inbounds i8, i8* %73, i64 %75
%77 = load i8*, i8** %13
%78 = load i32, i32* %11
%79 = call i32 @strncpy(i8* %76, i8* %77, i32 %78)
%80 = load i32, i32* %10
%81 = load i32, i32* %14
%82 = add nsw i32 %81, %80
store i32 %82, i32* %14
%83 = load i32, i32* %11
%84 = load i32, i32* %15
%85 = add nsw i32 %84, %83
store i32 %85, i32* %15
br label %98
86: ; preds = %62
%87 = load i32, i32* %14
%88 = sext i32 %87 to i64
%89 = getelementptr inbounds [5000 x i8], [5000 x i8]* %6, i64 0, i64 %88
%90 = load i8, i8* %89
%91 = load i32, i32* %15
%92 = sext i32 %91 to i64
%93 = getelementptr inbounds [5000 x i8], [5000 x i8]* %7, i64 0, i64 %92
store i8 %90, i8* %93
%94 = load i32, i32* %14
%95 = add nsw i32 %94, 1
store i32 %95, i32* %14
%96 = load i32, i32* %15
%97 = add nsw i32 %96, 1
store i32 %97, i32* %15
br label %98
98: ; preds = %86, %72
br label %58
99: ; preds = %58
%100 = load i32, i32* %9
%101 = icmp eq i32 %100, 0
br i1 %101, label %102, label %103
102: ; preds = %99
br label %104
103: ; preds = %99
br label %54
104: ; preds = %102
%105 = getelementptr inbounds [5000 x i8], [5000 x i8]* %6, i64 0, i64 0
%106 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.5, i64 0, i64 0), i8* %105)
%107 = getelementptr inbounds [5000 x i8], [5000 x i8]* %7, i64 0, i64 0
%108 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.6, i64 0, i64 0), i8* %107)
%109 = load i32, i32* %8
%110 = call i32 @close(i32 %109)
ret i32 0
}

LLVM IR (-O2):
@.str = [25 x i8] c"Number of arguments: %d\0A\00"
@.str.1 = [13 x i8] c"Command: %s\0A\00"
@.str.2 = [16 x i8] c"Old string: %s\0A\00"
@.str.3 = [16 x i8] c"New string: %s\0A\00"
@.str.4 = [15 x i8] c"File name: %s\0A\00"
@O_RDONLY = global i32 0
@.str.5 = [17 x i8] c"Input file: %s \0A\00"
@.str.6 = [18 x i8] c"Output file: %s \0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [5000 x i8]
%4 = alloca [5000 x i8]
%5 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i32 %0)
%6 = load i8*, i8** %1
%7 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i8* %6)
%8 = getelementptr inbounds i8*, i8** %1, i64 1
%9 = load i8*, i8** %8
%10 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i8* %9)
%11 = getelementptr inbounds i8*, i8** %1, i64 2
%12 = load i8*, i8** %11
%13 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), i8* %12)
%14 = getelementptr inbounds i8*, i8** %1, i64 3
%15 = load i8*, i8** %14
%16 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0), i8* %15)
%17 = getelementptr inbounds [5000 x i8], [5000 x i8]* %3, i64 0, i64 0
%18 = getelementptr inbounds [5000 x i8], [5000 x i8]* %4, i64 0, i64 0
%19 = load i8*, i8** %14
%20 = load i32, i32* @O_RDONLY
%21 = tail call i32 @open(i8* %19, i32 %20)
%22 = load i8*, i8** %8
%23 = tail call i32 @string_length(i8* %22)
%24 = load i8*, i8** %11
%25 = tail call i32 @string_length(i8* %24)
%26 = load i8*, i8** %8
%27 = load i8*, i8** %11
br label %28
28: ; preds = %53, %2
%29 = phi i32 [ undef, %2 ], [ %54, %53 ]
%30 = call i32 @read(i32 %21, i8* %17, i32 5000)
%31 = icmp sgt i32 %30, 0
br i1 %31, label %32, label %53
32: ; preds = %28, %47
%33 = phi i32 [ %50, %47 ], [ 0, %28 ]
%34 = phi i32 [ %51, %47 ], [ %29, %28 ]
%35 = sext i32 %33 to i64
%36 = getelementptr inbounds [5000 x i8], [5000 x i8]* %3, i64 0, i64 %35
%37 = call i32 @strncmp(i8* %36, i8* %26, i32 %23)
%38 = icmp eq i32 %37, 0
br i1 %38, label %39, label %43
39: ; preds = %32
%40 = sext i32 %34 to i64
%41 = getelementptr inbounds [5000 x i8], [5000 x i8]* %4, i64 0, i64 %40
%42 = call i32 @strncpy(i8* %41, i8* %27, i32 %25)
br label %47
43: ; preds = %32
%44 = load i8, i8* %36
%45 = sext i32 %34 to i64
%46 = getelementptr inbounds [5000 x i8], [5000 x i8]* %4, i64 0, i64 %45
store i8 %44, i8* %46
br label %47
47: ; preds = %43, %39
%48 = phi i32 [ %25, %39 ], [ 1, %43 ]
%49 = phi i32 [ %23, %39 ], [ 1, %43 ]
%50 = add nsw i32 %49, %33
%51 = add nsw i32 %48, %34
%52 = icmp slt i32 %50, %30
br i1 %52, label %32, label %53
53: ; preds = %47, %28
%54 = phi i32 [ %29, %28 ], [ %51, %47 ]
%55 = icmp eq i32 %30, 0
br i1 %55, label %56, label %28
56: ; preds = %53
%57 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.5, i64 0, i64 0), i8* %17)
%58 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.6, i64 0, i64 0), i8* %18)
%59 = call i32 @close(i32 %21)
ret i32 0
}