#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;
scalar_t__ calloc(int, int);
int /*<<< orphan*/ exit(int);
char fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ memmove(char *, char *, int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ *stdin;
int /*<<< orphan*/ strcmp(char *, char *);
int strlen(char *);
main(int argc, char **argv) {
  FILE *fp;
  char *buf;
  int size;
  if ((argc < 3) || (argc > 4)) {
    fprintf(stderr, "usage: %s <string> <substitute string> [filename]\n",
            *argv);
    exit(-1);
  }
  if (argc > 3)
    fp = fopen(argv[3], "r");
  else
    fp = stdin;
  size = strlen(argv[1]);
  buf = (char *)calloc(size + 1, 1);
  while ((buf[size - 1] = fgetc(fp)) != EOF) {
    if (!strcmp(buf, argv[1])) {
      printf("\n%s", argv[2]);
      memset(buf, 0, size);
      continue;
    }
    if (*buf) {
      printf("%c", *buf);
    }
    memmove(buf, &buf[1], size);
  }
  buf[size - 1] = 0;
  printf("%s", buf);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
jl     11ca <main+0x2a>
cmpl   $0x4,-0x8(%rbp)
jle    11f0 <main+0x50>
mov    0x2dff(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe1f(%rip),%rsi
call   1090 <fprintf@plt>
mov    $0xffffffff,%edi
call   10c0 <exit@plt>
cmpl   $0x3,-0x8(%rbp)
jle    1217 <main+0x77>
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
lea    0xe2a(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x18(%rbp)
jmp    1225 <main+0x85>
mov    0x2dca(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%edi
add    $0x1,%edi
mov    $0x1,%esi
call   1070 <calloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
call   1060 <fgetc@plt>
mov    -0x20(%rbp),%rcx
mov    -0x24(%rbp),%edx
sub    $0x1,%edx
movslq %edx,%rdx
mov    %al,(%rcx,%rdx,1)
movsbl %al,%eax
mov    0x2d74(%rip),%rcx
movsbl (%rcx),%ecx
cmp    %ecx,%eax
je     12f5 <main+0x155>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
call   1080 <strcmp@plt>
cmp    $0x0,%eax
jne    12ba <main+0x11a>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rsi
lea    0xd95(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x20(%rbp),%rdi
mov    -0x24(%rbp),%edx
xor    %esi,%esi
call   1050 <memset@plt>
jmp    1249 <main+0xa9>
mov    -0x20(%rbp),%rax
cmpb   $0x0,(%rax)
je     12dc <main+0x13c>
mov    -0x20(%rbp),%rax
movsbl (%rax),%esi
lea    0xd64(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x20(%rbp),%rdi
mov    -0x20(%rbp),%rsi
add    $0x1,%rsi
mov    -0x24(%rbp),%edx
call   10a0 <memmove@plt>
jmp    1249 <main+0xa9>
mov    -0x20(%rbp),%rax
mov    -0x24(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rsi
lea    0xd25(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r15
lea    -0x5(%rdi),%eax
cmp    $0xfffffffd,%eax
jbe    12c2 <main+0x112>
cmp    $0x4,%edi
jb     11e4 <main+0x34>
mov    0x18(%r15),%rdi
lea    0xe59(%rip),%rsi
call   10c0 <fopen@plt>
mov    %rax,%r14
jmp    11ee <main+0x3e>
mov    0x2dfd(%rip),%rax
mov    (%rax),%r14
mov    0x8(%r15),%rdi
call   1040 <strlen@plt>
mov    %eax,%r12d
lea    0x1(%r12),%edi
mov    $0x1,%esi
call   1080 <calloc@plt>
mov    %rax,%rbx
mov    %r14,%rdi
call   1070 <fgetc@plt>
movslq %r12d,%rbp
mov    %al,-0x1(%rbp,%rbx,1)
mov    0x2dbe(%rip),%rcx
cmp    (%rcx),%al
jne    124d <main+0x9d>
movb   $0x0,-0x1(%rbx,%rbp,1)
lea    0xe04(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %rbx,%r13
add    $0x1,%r13
jmp    1296 <main+0xe6>
cs nopw 0x0(%rax,%rax,1)
mov    0x10(%r15),%rsi
lea    0xdca(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rbx,%rdi
xor    %esi,%esi
mov    %r12d,%edx
call   1060 <memset@plt>
mov    %r14,%rdi
call   1070 <fgetc@plt>
mov    %al,-0x1(%rbx,%rbp,1)
mov    0x2d4e(%rip),%rcx
cmp    (%rcx),%al
je     1226 <main+0x76>
mov    0x8(%r15),%rsi
mov    %rbx,%rdi
call   1090 <strcmp@plt>
test   %eax,%eax
je     1260 <main+0xb0>
movsbl (%rbx),%edi
test   %edi,%edi
je     12b2 <main+0x102>
call   1030 <putchar@plt>
mov    %rbx,%rdi
mov    %r13,%rsi
mov    %r12d,%edx
call   10b0 <memmove@plt>
jmp    127f <main+0xcf>
mov    0x2d07(%rip),%rax
mov    (%rax),%edi
mov    (%r15),%rdx
lea    0xd2b(%rip),%rsi
call   10a0 <fprintf@plt>
mov    $0xffffffff,%edi
call   10d0 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [51 x i8] c"usage: %s <string> <substitute string> [filename]\0A\00"
@.str.1 = [2 x i8] c"r\00"
@stdin = global i32* null
@EOF = global i8 0
@.str.2 = [4 x i8] c"\0A%s\00"
@.str.3 = [3 x i8] c"%c\00"
@.str.4 = [3 x i8] c"%s\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca i8*
%8 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i32, i32* %4
%10 = icmp slt i32 %9, 3
br i1 %10, label %14, label %11
11: ; preds = %2
%12 = load i32, i32* %4
%13 = icmp sgt i32 %12, 4
br i1 %13, label %14, label %20
14: ; preds = %11, %2
%15 = load i32, i32* @stderr
%16 = load i8**, i8*** %5
%17 = load i8*, i8** %16
%18 = call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str, i64 0, i64 0), i8* %17)
%19 = call i32 @exit(i32 -1)
unreachable
20: ; preds = %11
%21 = load i32, i32* %4
%22 = icmp sgt i32 %21, 3
br i1 %22, label %23, label %28
23: ; preds = %20
%24 = load i8**, i8*** %5
%25 = getelementptr inbounds i8*, i8** %24, i64 3
%26 = load i8*, i8** %25
%27 = call i32* @fopen(i8* %26, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %27, i32** %6
br label %30
28: ; preds = %20
%29 = load i32*, i32** @stdin
store i32* %29, i32** %6
br label %30
30: ; preds = %28, %23
%31 = load i8**, i8*** %5
%32 = getelementptr inbounds i8*, i8** %31, i64 1
%33 = load i8*, i8** %32
%34 = call i32 @strlen(i8* %33)
store i32 %34, i32* %8
%35 = load i32, i32* %8
%36 = add nsw i32 %35, 1
%37 = call i64 @calloc(i32 %36, i32 1)
%38 = inttoptr i64 %37 to i8*
store i8* %38, i8** %7
br label %39
39: ; preds = %75, %58, %30
%40 = load i32*, i32** %6
%41 = call signext i8 @fgetc(i32* %40)
%42 = load i8*, i8** %7
%43 = load i32, i32* %8
%44 = sub nsw i32 %43, 1
%45 = sext i32 %44 to i64
%46 = getelementptr inbounds i8, i8* %42, i64 %45
store i8 %41, i8* %46
%47 = sext i8 %41 to i32
%48 = load i8, i8* @EOF
%49 = sext i8 %48 to i32
%50 = icmp ne i32 %47, %49
br i1 %50, label %51, label %81
51: ; preds = %39
%52 = load i8*, i8** %7
%53 = load i8**, i8*** %5
%54 = getelementptr inbounds i8*, i8** %53, i64 1
%55 = load i8*, i8** %54
%56 = call i32 @strcmp(i8* %52, i8* %55)
%57 = icmp ne i32 %56, 0
br i1 %57, label %66, label %58
58: ; preds = %51
%59 = load i8**, i8*** %5
%60 = getelementptr inbounds i8*, i8** %59, i64 2
%61 = load i8*, i8** %60
%62 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* %61)
%63 = load i8*, i8** %7
%64 = load i32, i32* %8
%65 = call i32 @memset(i8* %63, i32 0, i32 %64)
br label %39
66: ; preds = %51
%67 = load i8*, i8** %7
%68 = load i8, i8* %67
%69 = icmp ne i8 %68, 0
br i1 %69, label %70, label %75
70: ; preds = %66
%71 = load i8*, i8** %7
%72 = load i8, i8* %71
%73 = sext i8 %72 to i32
%74 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32 %73)
br label %75
75: ; preds = %70, %66
%76 = load i8*, i8** %7
%77 = load i8*, i8** %7
%78 = getelementptr inbounds i8, i8* %77, i64 1
%79 = load i32, i32* %8
%80 = call i32 @memmove(i8* %76, i8* %78, i32 %79)
br label %39
81: ; preds = %39
%82 = load i8*, i8** %7
%83 = load i32, i32* %8
%84 = sub nsw i32 %83, 1
%85 = sext i32 %84 to i64
%86 = getelementptr inbounds i8, i8* %82, i64 %85
store i8 0, i8* %86
%87 = load i8*, i8** %7
%88 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8* %87)
%89 = load i32, i32* %3
ret i32 %89
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [51 x i8] c"usage: %s <string> <substitute string> [filename]\0A\00"
@.str.1 = [2 x i8] c"r\00"
@stdin = global i32* null
@EOF = global i8 0
@.str.2 = [4 x i8] c"\0A%s\00"
@.str.4 = [3 x i8] c"%s\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = add i32 %0, -5
%4 = icmp ult i32 %3, -2
br i1 %4, label %5, label %10
5: ; preds = %2
%6 = load i32, i32* @stderr
%7 = load i8*, i8** %1
%8 = tail call i32 @fprintf(i32 %6, i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str, i64 0, i64 0), i8* %7)
%9 = tail call i32 @exit(i32 -1)
unreachable
10: ; preds = %2
%11 = icmp ugt i32 %0, 3
br i1 %11, label %12, label %16
12: ; preds = %10
%13 = getelementptr inbounds i8*, i8** %1, i64 3
%14 = load i8*, i8** %13
%15 = tail call i32* @fopen(i8* %14, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
br label %18
16: ; preds = %10
%17 = load i32*, i32** @stdin
br label %18
18: ; preds = %16, %12
%19 = phi i32* [ %15, %12 ], [ %17, %16 ]
%20 = getelementptr inbounds i8*, i8** %1, i64 1
%21 = load i8*, i8** %20
%22 = tail call i32 @strlen(i8* %21)
%23 = add nsw i32 %22, 1
%24 = tail call i64 @calloc(i32 %23, i32 1)
%25 = inttoptr i64 %24 to i8*
%26 = tail call signext i8 @fgetc(i32* %19)
%27 = add nsw i32 %22, -1
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds i8, i8* %25, i64 %28
store i8 %26, i8* %29
%30 = load i8, i8* @EOF
%31 = icmp eq i8 %26, %30
br i1 %31, label %55, label %32
32: ; preds = %18
%33 = getelementptr inbounds i8, i8* %25, i64 1
%34 = getelementptr inbounds i8*, i8** %1, i64 2
br label %35
35: ; preds = %32, %43
%36 = load i8*, i8** %20
%37 = tail call i32 @strcmp(i8* dereferenceable(1) %25, i8* dereferenceable(1) %36)
%38 = icmp eq i32 %37, 0
br i1 %38, label %39, label %47
39: ; preds = %35
%40 = load i8*, i8** %34
%41 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* %40)
%42 = tail call i32 @memset(i8* %25, i32 0, i32 %22)
br label %43
43: ; preds = %39, %53
%44 = tail call signext i8 @fgetc(i32* %19)
store i8 %44, i8* %29
%45 = load i8, i8* @EOF
%46 = icmp eq i8 %44, %45
br i1 %46, label %55, label %35
47: ; preds = %35
%48 = load i8, i8* %25
%49 = icmp eq i8 %48, 0
br i1 %49, label %53, label %50
50: ; preds = %47
%51 = sext i8 %48 to i32
%52 = tail call i32 @putchar(i32 %51)
br label %53
53: ; preds = %50, %47
%54 = tail call i32 @memmove(i8* %25, i8* %33, i32 %22)
br label %43
55: ; preds = %43, %18
store i8 0, i8* %29
%56 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8* %25)
ret i32 0
}