#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 /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ feof(int /*<<< orphan*/ *);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ fwrite(char *, int, int, int /*<<< orphan*/);
scalar_t__ malloc(int);
char *search(char *, int, char *, int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ *stdin;
int /*<<< orphan*/ stdout;
int strlen(char *);
int /*<<< orphan*/ strncpy(char *, char *, int);
int main(int argc, char *argv[]) {
  FILE *FIn;
  int plen, slen, rlen;
  char *buf;
  char *pbuf, *sbuf;
  int r, w;
  if (argc < 3 || argc > 4) {
    fprintf(stderr, "Syntax: replace search_str replace_str [src_file]\n");
    exit(0);
  }
  if (argc == 3)
    FIn = stdin;
  else {
    if (!(FIn = fopen(argv[3], "rb"))) {
      fprintf(stderr, "Error opening %s\n", argv[3]);
      exit(0);
    }
  }
  plen = strlen(argv[1]);
  rlen = strlen(argv[2]);
  slen = plen + 4096;
  pbuf = NULL;
  w = 0;
  buf = (char *)malloc(slen + 1);
  if (!buf) {
    fprintf(stderr, "Error allocating memory for buffer.\n");
    exit(0);
  }
  r = fread(buf, 1, slen, FIn);
  while (r > 0) {
    buf[r] = '\0';
    pbuf = buf;
    while (sbuf = search(pbuf, r, argv[1], plen)) {
      w = sbuf - pbuf;
      if (w) {
        fwrite(pbuf, 1, w, stdout);
        r -= w;
      }
      fwrite(argv[2], 1, rlen, stdout);
      pbuf = sbuf + plen;
      r -= plen;
    }
    if (r > plen) {
      fwrite(pbuf, 1, r - plen, stdout);
      pbuf += r - plen;
      w = plen;
    } else {
      w = r;
    }
    if (w) {
      strncpy(buf, pbuf, w);
    }
    if (feof(FIn))
      break;
    r = fread(buf + w, 1, slen - w, FIn);
    if (r >= 0) {
      w += r;
      r = w;
    }
  }
  if (w) {
    fwrite(buf, 1, w, stdout);
  }
  free(buf);
  fflush(stdout);
  if (argc == 4)
    fclose(FIn);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
jl     11fa <main+0x2a>
cmpl   $0x4,-0x8(%rbp)
jle    1218 <main+0x48>
mov    0x2dd7(%rip),%rax
mov    (%rax),%edi
lea    0xdf6(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
xor    %edi,%edi
call   10e0 <exit@plt>
cmpl   $0x3,-0x8(%rbp)
jne    1235 <main+0x65>
mov    0x2dbf(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x18(%rbp)
jmp    1282 <main+0xb2>
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
lea    0xdef(%rip),%rsi
call   10d0 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    127d <main+0xad>
mov    0x2d7a(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdx
lea    0xdc7(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
xor    %edi,%edi
call   10e0 <exit@plt>
jmp    1282 <main+0xb2>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1070 <strlen@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   1070 <strlen@plt>
mov    %eax,-0x24(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x1000,%eax
mov    %eax,-0x20(%rbp)
movq   $0x0,-0x38(%rbp)
movl   $0x0,-0x48(%rbp)
mov    -0x20(%rbp),%edi
add    $0x1,%edi
call   10b0 <malloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    12f4 <main+0x124>
mov    0x2cfb(%rip),%rax
mov    (%rax),%edi
lea    0xd62(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
xor    %edi,%edi
call   10e0 <exit@plt>
mov    -0x30(%rbp),%rdi
mov    -0x20(%rbp),%edx
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
call   1050 <fread@plt>
mov    %eax,-0x44(%rbp)
cmpl   $0x0,-0x44(%rbp)
jle    1489 <main+0x2b9>
mov    -0x30(%rbp),%rax
movslq -0x44(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x30(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
mov    -0x44(%rbp),%esi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
mov    -0x1c(%rbp),%ecx
call   1080 <search@plt>
mov    %rax,-0x40(%rbp)
cmp    $0x0,%rax
je     13c9 <main+0x1f9>
mov    -0x40(%rbp),%rax
mov    -0x38(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,-0x48(%rbp)
cmpl   $0x0,-0x48(%rbp)
je     138c <main+0x1bc>
mov    -0x38(%rbp),%rdi
mov    -0x48(%rbp),%edx
mov    0x2c5b(%rip),%rax
mov    (%rax),%ecx
mov    $0x1,%esi
call   10f0 <fwrite@plt>
mov    -0x48(%rbp),%ecx
mov    -0x44(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x44(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
mov    -0x24(%rbp),%edx
mov    0x2c32(%rip),%rax
mov    (%rax),%ecx
mov    $0x1,%esi
call   10f0 <fwrite@plt>
mov    -0x40(%rbp),%rax
movslq -0x1c(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x38(%rbp)
mov    -0x1c(%rbp),%ecx
mov    -0x44(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x44(%rbp)
jmp    132a <main+0x15a>
mov    -0x44(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jle    1411 <main+0x241>
mov    -0x38(%rbp),%rdi
mov    -0x44(%rbp),%edx
sub    -0x1c(%rbp),%edx
mov    0x2bea(%rip),%rax
mov    (%rax),%ecx
mov    $0x1,%esi
call   10f0 <fwrite@plt>
mov    -0x44(%rbp),%ecx
sub    -0x1c(%rbp),%ecx
mov    -0x38(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x38(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x48(%rbp)
jmp    1417 <main+0x247>
mov    -0x44(%rbp),%eax
mov    %eax,-0x48(%rbp)
cmpl   $0x0,-0x48(%rbp)
je     1431 <main+0x261>
mov    -0x30(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    -0x48(%rbp),%edx
call   1040 <strncpy@plt>
mov    -0x18(%rbp),%rdi
call   10a0 <feof@plt>
cmp    $0x0,%rax
je     1449 <main+0x279>
jmp    1489 <main+0x2b9>
mov    -0x30(%rbp),%rdi
movslq -0x48(%rbp),%rax
add    %rax,%rdi
mov    -0x20(%rbp),%edx
sub    -0x48(%rbp),%edx
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
call   1050 <fread@plt>
mov    %eax,-0x44(%rbp)
cmpl   $0x0,-0x44(%rbp)
jl     1484 <main+0x2b4>
mov    -0x44(%rbp),%eax
add    -0x48(%rbp),%eax
mov    %eax,-0x48(%rbp)
mov    -0x48(%rbp),%eax
mov    %eax,-0x44(%rbp)
jmp    130c <main+0x13c>
cmpl   $0x0,-0x48(%rbp)
je     14ad <main+0x2dd>
mov    -0x30(%rbp),%rdi
mov    -0x48(%rbp),%edx
mov    0x2b2f(%rip),%rax
mov    (%rax),%ecx
mov    $0x1,%esi
call   10f0 <fwrite@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    0x2b13(%rip),%rax
mov    (%rax),%edi
call   10c0 <fflush@plt>
cmpl   $0x4,-0x8(%rbp)
jne    14d7 <main+0x307>
mov    -0x18(%rbp),%rdi
call   1060 <fclose@plt>
xor    %eax,%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %edi,%ebp
lea    -0x5(%rbp),%eax
cmp    $0xfffffffd,%eax
jbe    1416 <main+0x246>
mov    %rsi,%rbx
cmp    $0x3,%ebp
jne    1200 <main+0x30>
mov    0x2ded(%rip),%rax
mov    (%rax),%r15
jmp    121c <main+0x4c>
mov    0x18(%rbx),%rdi
lea    0xe28(%rip),%rsi
call   10d0 <fopen@plt>
mov    %rax,%r15
test   %rax,%rax
je     1446 <main+0x276>
mov    %rbp,0x18(%rsp)
mov    0x8(%rbx),%rdi
call   1070 <strlen@plt>
mov    %eax,%r13d
mov    0x10(%rbx),%rdi
call   1070 <strlen@plt>
mov    %eax,0xc(%rsp)
lea    0x1001(%r13),%edi
call   10b0 <malloc@plt>
test   %rax,%rax
je     1428 <main+0x258>
mov    %rax,%r12
lea    0x1000(%r13),%edx
mov    %rax,%rdi
mov    $0x1,%esi
mov    %edx,0x8(%rsp)
mov    %r15,%rcx
call   1050 <fread@plt>
test   %eax,%eax
jle    13e0 <main+0x210>
mov    %eax,%r14d
movslq %r13d,%rax
mov    %rax,0x20(%rsp)
mov    %r12,(%rsp)
mov    %r15,0x10(%rsp)
nopl   0x0(%rax)
mov    %r14d,%eax
movb   $0x0,(%r12,%rax,1)
mov    0x8(%rbx),%rdx
mov    %r12,%rdi
mov    %r14d,%esi
mov    %r13d,%ecx
call   1080 <search@plt>
test   %rax,%rax
je     1330 <main+0x160>
mov    %rax,%rbp
jmp    12f9 <main+0x129>
nopl   0x0(%rax,%rax,1)
mov    0x10(%rbx),%rdi
mov    (%rax),%ecx
mov    $0x1,%esi
mov    0xc(%rsp),%edx
call   10f0 <fwrite@plt>
mov    %rbp,%r12
add    0x20(%rsp),%r12
sub    %r13d,%r14d
mov    0x8(%rbx),%rdx
mov    %r12,%rdi
mov    %r14d,%esi
mov    %r13d,%ecx
call   1080 <search@plt>
mov    %rax,%rbp
test   %rax,%rax
je     1330 <main+0x160>
mov    %ebp,%r15d
mov    0x2ccd(%rip),%rax
sub    %r12d,%r15d
je     12c0 <main+0xf0>
mov    (%rax),%ecx
mov    %r12,%rdi
mov    $0x1,%esi
mov    %r15d,%edx
call   10f0 <fwrite@plt>
mov    0x2caf(%rip),%rax
sub    %r15d,%r14d
jmp    12c0 <main+0xf0>
cs nopw 0x0(%rax,%rax,1)
mov    %r14d,%ebp
sub    %r13d,%ebp
jle    1360 <main+0x190>
mov    0x2c91(%rip),%rax
mov    (%rax),%ecx
mov    %r12,%rdi
mov    $0x1,%esi
mov    %ebp,%edx
call   10f0 <fwrite@plt>
movslq %ebp,%rax
add    %rax,%r12
mov    %r13d,%ebp
jmp    1363 <main+0x193>
nopl   0x0(%rax,%rax,1)
mov    %r14d,%ebp
mov    0x10(%rsp),%r15
test   %ebp,%ebp
je     137a <main+0x1aa>
mov    (%rsp),%rdi
mov    %r12,%rsi
mov    %ebp,%edx
call   1040 <strncpy@plt>
mov    %r15,%rdi
call   10a0 <feof@plt>
test   %rax,%rax
jne    13c0 <main+0x1f0>
movslq %ebp,%rdi
mov    (%rsp),%r12
add    %r12,%rdi
mov    0x8(%rsp),%edx
sub    %ebp,%edx
mov    $0x1,%esi
mov    %r15,%rcx
call   1050 <fread@plt>
lea    (%rax,%rbp,1),%ecx
test   %eax,%eax
mov    %eax,%r14d
cmovns %ecx,%r14d
test   %r14d,%r14d
jg     1290 <main+0xc0>
test   %eax,%eax
cmovns %ecx,%ebp
jmp    13c4 <main+0x1f4>
mov    (%rsp),%r12
test   %ebp,%ebp
mov    0x2c03(%rip),%rax
je     13e0 <main+0x210>
mov    (%rax),%ecx
mov    %r12,%rdi
mov    $0x1,%esi
mov    %ebp,%edx
call   10f0 <fwrite@plt>
mov    %r12,%rdi
call   1030 <free@plt>
mov    0x2be1(%rip),%rax
mov    (%rax),%edi
call   10c0 <fflush@plt>
cmpl   $0x4,0x18(%rsp)
jne    1405 <main+0x235>
mov    %r15,%rdi
call   1060 <fclose@plt>
xor    %eax,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2bbb(%rip),%rax
mov    (%rax),%edi
lea    0xbda(%rip),%rsi
jmp    1438 <main+0x268>
mov    0x2ba9(%rip),%rax
mov    (%rax),%edi
lea    0xc10(%rip),%rsi
xor    %eax,%eax
call   1090 <fprintf@plt>
xor    %edi,%edi
call   10e0 <exit@plt>
mov    0x2b8b(%rip),%rax
mov    (%rax),%edi
mov    0x18(%rbx),%rdx
lea    0xbdc(%rip),%rsi
xor    %eax,%eax
call   1090 <fprintf@plt>
xor    %edi,%edi
call   10e0 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [51 x i8] c"Syntax: replace search_str replace_str [src_file]\0A\00"
@stdin = global i32* null
@.str.1 = [3 x i8] c"rb\00"
@.str.2 = [18 x i8] c"Error opening %s\0A\00"
@.str.3 = [37 x i8] c"Error allocating memory for buffer.\0A\00"
@stdout = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i8*
%12 = alloca i8*
%13 = alloca i32
%14 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%15 = load i32, i32* %4
%16 = icmp slt i32 %15, 3
br i1 %16, label %20, label %17
17: ; preds = %2
%18 = load i32, i32* %4
%19 = icmp sgt i32 %18, 4
br i1 %19, label %20, label %24
20: ; preds = %17, %2
%21 = load i32, i32* @stderr
%22 = call i32 (i32, i8*, ...) @fprintf(i32 %21, i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str, i64 0, i64 0))
%23 = call i32 @exit(i32 0)
unreachable
24: ; preds = %17
%25 = load i32, i32* %4
%26 = icmp eq i32 %25, 3
br i1 %26, label %27, label %29
27: ; preds = %24
%28 = load i32*, i32** @stdin
store i32* %28, i32** %6
br label %43
29: ; preds = %24
%30 = load i8**, i8*** %5
%31 = getelementptr inbounds i8*, i8** %30, i64 3
%32 = load i8*, i8** %31
%33 = call i32* @fopen(i8* %32, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %33, i32** %6
%34 = icmp ne i32* %33, null
br i1 %34, label %42, label %35
35: ; preds = %29
%36 = load i32, i32* @stderr
%37 = load i8**, i8*** %5
%38 = getelementptr inbounds i8*, i8** %37, i64 3
%39 = load i8*, i8** %38
%40 = call i32 (i32, i8*, ...) @fprintf(i32 %36, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i8* %39)
%41 = call i32 @exit(i32 0)
unreachable
42: ; preds = %29
br label %43
43: ; preds = %42, %27
%44 = load i8**, i8*** %5
%45 = getelementptr inbounds i8*, i8** %44, i64 1
%46 = load i8*, i8** %45
%47 = call i32 @strlen(i8* %46)
store i32 %47, i32* %7
%48 = load i8**, i8*** %5
%49 = getelementptr inbounds i8*, i8** %48, i64 2
%50 = load i8*, i8** %49
%51 = call i32 @strlen(i8* %50)
store i32 %51, i32* %9
%52 = load i32, i32* %7
%53 = add nsw i32 %52, 4096
store i32 %53, i32* %8
store i8* null, i8** %11
store i32 0, i32* %14
%54 = load i32, i32* %8
%55 = add nsw i32 %54, 1
%56 = call i64 @malloc(i32 %55)
%57 = inttoptr i64 %56 to i8*
store i8* %57, i8** %10
%58 = load i8*, i8** %10
%59 = icmp ne i8* %58, null
br i1 %59, label %64, label %60
60: ; preds = %43
%61 = load i32, i32* @stderr
%62 = call i32 (i32, i8*, ...) @fprintf(i32 %61, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.3, i64 0, i64 0))
%63 = call i32 @exit(i32 0)
unreachable
64: ; preds = %43
%65 = load i8*, i8** %10
%66 = load i32, i32* %8
%67 = load i32*, i32** %6
%68 = call i32 @fread(i8* %65, i32 1, i32 %66, i32* %67)
store i32 %68, i32* %13
br label %69
69: ; preds = %168, %64
%70 = load i32, i32* %13
%71 = icmp sgt i32 %70, 0
br i1 %71, label %72, label %169
72: ; preds = %69
%73 = load i8*, i8** %10
%74 = load i32, i32* %13
%75 = sext i32 %74 to i64
%76 = getelementptr inbounds i8, i8* %73, i64 %75
store i8 0, i8* %76
%77 = load i8*, i8** %10
store i8* %77, i8** %11
br label %78
78: ; preds = %104, %72
%79 = load i8*, i8** %11
%80 = load i32, i32* %13
%81 = load i8**, i8*** %5
%82 = getelementptr inbounds i8*, i8** %81, i64 1
%83 = load i8*, i8** %82
%84 = load i32, i32* %7
%85 = call i8* @search(i8* %79, i32 %80, i8* %83, i32 %84)
store i8* %85, i8** %12
%86 = icmp ne i8* %85, null
br i1 %86, label %87, label %118
87: ; preds = %78
%88 = load i8*, i8** %12
%89 = load i8*, i8** %11
%90 = ptrtoint i8* %88 to i64
%91 = ptrtoint i8* %89 to i64
%92 = sub i64 %90, %91
%93 = trunc i64 %92 to i32
store i32 %93, i32* %14
%94 = load i32, i32* %14
%95 = icmp ne i32 %94, 0
br i1 %95, label %96, label %104
96: ; preds = %87
%97 = load i8*, i8** %11
%98 = load i32, i32* %14
%99 = load i32, i32* @stdout
%100 = call i32 @fwrite(i8* %97, i32 1, i32 %98, i32 %99)
%101 = load i32, i32* %14
%102 = load i32, i32* %13
%103 = sub nsw i32 %102, %101
store i32 %103, i32* %13
br label %104
104: ; preds = %96, %87
%105 = load i8**, i8*** %5
%106 = getelementptr inbounds i8*, i8** %105, i64 2
%107 = load i8*, i8** %106
%108 = load i32, i32* %9
%109 = load i32, i32* @stdout
%110 = call i32 @fwrite(i8* %107, i32 1, i32 %108, i32 %109)
%111 = load i8*, i8** %12
%112 = load i32, i32* %7
%113 = sext i32 %112 to i64
%114 = getelementptr inbounds i8, i8* %111, i64 %113
store i8* %114, i8** %11
%115 = load i32, i32* %7
%116 = load i32, i32* %13
%117 = sub nsw i32 %116, %115
store i32 %117, i32* %13
br label %78
118: ; preds = %78
%119 = load i32, i32* %13
%120 = load i32, i32* %7
%121 = icmp sgt i32 %119, %120
br i1 %121, label %122, label %136
122: ; preds = %118
%123 = load i8*, i8** %11
%124 = load i32, i32* %13
%125 = load i32, i32* %7
%126 = sub nsw i32 %124, %125
%127 = load i32, i32* @stdout
%128 = call i32 @fwrite(i8* %123, i32 1, i32 %126, i32 %127)
%129 = load i32, i32* %13
%130 = load i32, i32* %7
%131 = sub nsw i32 %129, %130
%132 = load i8*, i8** %11
%133 = sext i32 %131 to i64
%134 = getelementptr inbounds i8, i8* %132, i64 %133
store i8* %134, i8** %11
%135 = load i32, i32* %7
store i32 %135, i32* %14
br label %138
136: ; preds = %118
%137 = load i32, i32* %13
store i32 %137, i32* %14
br label %138
138: ; preds = %136, %122
%139 = load i32, i32* %14
%140 = icmp ne i32 %139, 0
br i1 %140, label %141, label %146
141: ; preds = %138
%142 = load i8*, i8** %10
%143 = load i8*, i8** %11
%144 = load i32, i32* %14
%145 = call i32 @strncpy(i8* %142, i8* %143, i32 %144)
br label %146
146: ; preds = %141, %138
%147 = load i32*, i32** %6
%148 = call i64 @feof(i32* %147)
%149 = icmp ne i64 %148, 0
br i1 %149, label %150, label %151
150: ; preds = %146
br label %169
151: ; preds = %146
%152 = load i8*, i8** %10
%153 = load i32, i32* %14
%154 = sext i32 %153 to i64
%155 = getelementptr inbounds i8, i8* %152, i64 %154
%156 = load i32, i32* %8
%157 = load i32, i32* %14
%158 = sub nsw i32 %156, %157
%159 = load i32*, i32** %6
%160 = call i32 @fread(i8* %155, i32 1, i32 %158, i32* %159)
store i32 %160, i32* %13
%161 = load i32, i32* %13
%162 = icmp sge i32 %161, 0
br i1 %162, label %163, label %168
163: ; preds = %151
%164 = load i32, i32* %13
%165 = load i32, i32* %14
%166 = add nsw i32 %165, %164
store i32 %166, i32* %14
%167 = load i32, i32* %14
store i32 %167, i32* %13
br label %168
168: ; preds = %163, %151
br label %69
169: ; preds = %150, %69
%170 = load i32, i32* %14
%171 = icmp ne i32 %170, 0
br i1 %171, label %172, label %177
172: ; preds = %169
%173 = load i8*, i8** %10
%174 = load i32, i32* %14
%175 = load i32, i32* @stdout
%176 = call i32 @fwrite(i8* %173, i32 1, i32 %174, i32 %175)
br label %177
177: ; preds = %172, %169
%178 = load i8*, i8** %10
%179 = call i32 @free(i8* %178)
%180 = load i32, i32* @stdout
%181 = call i32 @fflush(i32 %180)
%182 = load i32, i32* %4
%183 = icmp eq i32 %182, 4
br i1 %183, label %184, label %187
184: ; preds = %177
%185 = load i32*, i32** %6
%186 = call i32 @fclose(i32* %185)
br label %187
187: ; preds = %184, %177
ret i32 0
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [51 x i8] c"Syntax: replace search_str replace_str [src_file]\0A\00"
@stdin = global i32* null
@.str.1 = [3 x i8] c"rb\00"
@.str.2 = [18 x i8] c"Error opening %s\0A\00"
@.str.3 = [37 x i8] c"Error allocating memory for buffer.\0A\00"
@stdout = global i32 0
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 %9
5: ; preds = %2
%6 = load i32, i32* @stderr
%7 = tail call i32 (i32, i8*, ...) @fprintf(i32 %6, i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str, i64 0, i64 0))
%8 = tail call i32 @exit(i32 0)
unreachable
9: ; preds = %2
%10 = icmp eq i32 %0, 3
br i1 %10, label %11, label %13
11: ; preds = %9
%12 = load i32*, i32** @stdin
br label %23
13: ; preds = %9
%14 = getelementptr inbounds i8*, i8** %1, i64 3
%15 = load i8*, i8** %14
%16 = tail call i32* @fopen(i8* %15, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%17 = icmp eq i32* %16, null
br i1 %17, label %18, label %23
18: ; preds = %13
%19 = load i32, i32* @stderr
%20 = load i8*, i8** %14
%21 = tail call i32 (i32, i8*, ...) @fprintf(i32 %19, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i8* %20)
%22 = tail call i32 @exit(i32 0)
unreachable
23: ; preds = %13, %11
%24 = phi i32* [ %12, %11 ], [ %16, %13 ]
%25 = getelementptr inbounds i8*, i8** %1, i64 1
%26 = load i8*, i8** %25
%27 = tail call i32 @strlen(i8* %26)
%28 = getelementptr inbounds i8*, i8** %1, i64 2
%29 = load i8*, i8** %28
%30 = tail call i32 @strlen(i8* %29)
%31 = add nsw i32 %27, 4096
%32 = add nsw i32 %27, 4097
%33 = tail call i64 @malloc(i32 %32)
%34 = inttoptr i64 %33 to i8*
%35 = icmp eq i64 %33, 0
br i1 %35, label %36, label %40
36: ; preds = %23
%37 = load i32, i32* @stderr
%38 = tail call i32 (i32, i8*, ...) @fprintf(i32 %37, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.3, i64 0, i64 0))
%39 = tail call i32 @exit(i32 0)
unreachable
40: ; preds = %23
%41 = tail call i32 @fread(i8* %34, i32 1, i32 %31, i32* %24)
%42 = icmp sgt i32 %41, 0
br i1 %42, label %43, label %111
43: ; preds = %40
%44 = sext i32 %27 to i64
br label %45
45: ; preds = %43, %94
%46 = phi i32 [ %41, %43 ], [ %101, %94 ]
%47 = zext i32 %46 to i64
%48 = getelementptr inbounds i8, i8* %34, i64 %47
store i8 0, i8* %48
%49 = load i8*, i8** %25
%50 = tail call i8* @search(i8* %34, i32 %46, i8* %49, i32 %27)
%51 = icmp eq i8* %50, null
br i1 %51, label %75, label %52
52: ; preds = %45, %65
%53 = phi i8* [ %73, %65 ], [ %50, %45 ]
%54 = phi i32 [ %71, %65 ], [ %46, %45 ]
%55 = phi i8* [ %70, %65 ], [ %34, %45 ]
%56 = ptrtoint i8* %53 to i64
%57 = ptrtoint i8* %55 to i64
%58 = sub i64 %56, %57
%59 = trunc i64 %58 to i32
%60 = icmp eq i32 %59, 0
br i1 %60, label %65, label %61
61: ; preds = %52
%62 = load i32, i32* @stdout
%63 = tail call i32 @fwrite(i8* %55, i32 1, i32 %59, i32 %62)
%64 = sub nsw i32 %54, %59
br label %65
65: ; preds = %61, %52
%66 = phi i32 [ %64, %61 ], [ %54, %52 ]
%67 = load i8*, i8** %28
%68 = load i32, i32* @stdout
%69 = tail call i32 @fwrite(i8* %67, i32 1, i32 %30, i32 %68)
%70 = getelementptr inbounds i8, i8* %53, i64 %44
%71 = sub nsw i32 %66, %27
%72 = load i8*, i8** %25
%73 = tail call i8* @search(i8* %70, i32 %71, i8* %72, i32 %27)
%74 = icmp eq i8* %73, null
br i1 %74, label %75, label %52
75: ; preds = %65, %45
%76 = phi i8* [ %34, %45 ], [ %70, %65 ]
%77 = phi i32 [ %46, %45 ], [ %71, %65 ]
%78 = icmp sgt i32 %77, %27
br i1 %78, label %79, label %85
79: ; preds = %75
%80 = sub nsw i32 %77, %27
%81 = load i32, i32* @stdout
%82 = tail call i32 @fwrite(i8* %76, i32 1, i32 %80, i32 %81)
%83 = sext i32 %80 to i64
%84 = getelementptr inbounds i8, i8* %76, i64 %83
br label %85
85: ; preds = %75, %79
%86 = phi i8* [ %84, %79 ], [ %76, %75 ]
%87 = phi i32 [ %27, %79 ], [ %77, %75 ]
%88 = icmp eq i32 %87, 0
br i1 %88, label %91, label %89
89: ; preds = %85
%90 = tail call i32 @strncpy(i8* %34, i8* %86, i32 %87)
br label %91
91: ; preds = %89, %85
%92 = tail call i64 @feof(i32* %24)
%93 = icmp eq i64 %92, 0
br i1 %93, label %94, label %105
94: ; preds = %91
%95 = sext i32 %87 to i64
%96 = getelementptr inbounds i8, i8* %34, i64 %95
%97 = sub nsw i32 %31, %87
%98 = tail call i32 @fread(i8* %96, i32 1, i32 %97, i32* %24)
%99 = icmp sgt i32 %98, -1
%100 = add nsw i32 %98, %87
%101 = select i1 %99, i32 %100, i32 %98
%102 = icmp sgt i32 %101, 0
br i1 %102, label %45, label %103
103: ; preds = %94
%104 = select i1 %99, i32 %100, i32 %87
br label %105
105: ; preds = %91, %103
%106 = phi i32 [ %104, %103 ], [ %87, %91 ]
%107 = icmp eq i32 %106, 0
br i1 %107, label %111, label %108
108: ; preds = %105
%109 = load i32, i32* @stdout
%110 = tail call i32 @fwrite(i8* %34, i32 1, i32 %106, i32 %109)
br label %111
111: ; preds = %40, %108, %105
%112 = tail call i32 @free(i8* %34)
%113 = load i32, i32* @stdout
%114 = tail call i32 @fflush(i32 %113)
%115 = icmp eq i32 %0, 4
br i1 %115, label %116, label %118
116: ; preds = %111
%117 = tail call i32 @fclose(i32* %24)
br label %118
118: ; preds = %116, %111
ret i32 0
}