#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*/ SEEK_END;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fread(char *, int, int, int /*<<< orphan*/ *);
int fseek(int /*<<< orphan*/ *, int, int /*<<< orphan*/);
int ftell(int /*<<< orphan*/ *);
int fwrite(char *, int, int, int /*<<< orphan*/ *);
char *malloc(int);
int /*<<< orphan*/ printf(char *, ...);
int strlen(char *);
void binpatch(char *oldstr, char *newstr, char *filename) {
  int oldlen = strlen(oldstr) + 1;
  int newlen = strlen(newstr) + 1;
  int step = 65536;
  int buflen = step + oldlen - 1;
  int pos;
  int len;
  char *buf;
  FILE *f;
  buf = malloc(buflen);
  if (buf == NULL) {
    printf("Could not allocate buffer\n");
    exit(1);
  }
  f = fopen(filename, "r+b");
  if (f == NULL) {
    printf("Could not open file \"%s\"\n", filename);
    exit(1);
  }
  fseek(f, 0, SEEK_END);
  len = ftell(f);
  if (len < 0) {
    fclose(f);
    printf("Could not seek\n");
    exit(1);
  }
  pos = 0;
  while (pos < len) {
    int i;
    int n = len - pos;
    if (n > buflen)
      n = buflen;
    int x = fseek(f, pos, SEEK_SET);
    if (x < 0) {
      fclose(f);
      printf("Could not seek\n");
      exit(1);
    }
    if (fread(buf, 1, n, f) != n) {
      fclose(f);
      printf("Could not read\n");
      exit(1);
    }
    n = n - oldlen + 1;
    for (i = 0; i < n; i++)
      if (buf[i] == oldstr[0]) {
        int j;
        for (j = 1; j < oldlen; j++)
          if (buf[i + j] != oldstr[j])
            goto fail;
        pos = pos + i;
        x = fseek(f, pos, SEEK_SET);
        if (x < 0) {
          fclose(f);
          printf("Could not seek\n");
          exit(1);
        }
        if (fwrite(newstr, 1, newlen, f) != newlen) {
          fclose(f);
          printf("Could not write\n");
          exit(1);
        }
        fclose(f);
        return;
      fail:;
      }
    pos += step;
  }
  fclose(f);
  printf("Did not find string in file \"%s\"\n", filename);
  exit(1);
}


x86 assembly (clang, -O0):
<binpatch>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <strlen@plt>
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
movl   $0x10000,-0x24(%rbp)
mov    -0x24(%rbp),%eax
add    -0x1c(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%edi
call   1080 <malloc@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    1214 <binpatch+0x74>
lea    0xdfd(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x18(%rbp),%rdi
lea    0xdfc(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jne    124f <binpatch+0xaf>
mov    -0x18(%rbp),%rsi
lea    0xde1(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x40(%rbp),%rdi
mov    0x2d8e(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1090 <fseek@plt>
mov    -0x40(%rbp),%rdi
call   1070 <ftell@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0x0,-0x30(%rbp)
jge    129a <binpatch+0xfa>
mov    -0x40(%rbp),%rdi
call   1040 <fclose@plt>
lea    0xdb0(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x30(%rbp),%eax
jge    1494 <binpatch+0x2f4>
mov    -0x30(%rbp),%eax
sub    -0x2c(%rbp),%eax
mov    %eax,-0x48(%rbp)
mov    -0x48(%rbp),%eax
cmp    -0x28(%rbp),%eax
jle    12c8 <binpatch+0x128>
mov    -0x28(%rbp),%eax
mov    %eax,-0x48(%rbp)
mov    -0x40(%rbp),%rdi
mov    -0x2c(%rbp),%esi
mov    0x2d02(%rip),%rax
mov    (%rax),%edx
call   1090 <fseek@plt>
mov    %eax,-0x4c(%rbp)
cmpl   $0x0,-0x4c(%rbp)
jge    130b <binpatch+0x16b>
mov    -0x40(%rbp),%rdi
call   1040 <fclose@plt>
lea    0xd3f(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x38(%rbp),%rdi
mov    -0x48(%rbp),%edx
mov    -0x40(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
cmp    -0x48(%rbp),%eax
je     134a <binpatch+0x1aa>
mov    -0x40(%rbp),%rdi
call   1040 <fclose@plt>
lea    0xd10(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x48(%rbp),%eax
sub    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x48(%rbp)
movl   $0x0,-0x44(%rbp)
mov    -0x44(%rbp),%eax
cmp    -0x48(%rbp),%eax
jge    1486 <binpatch+0x2e6>
mov    -0x38(%rbp),%rax
movslq -0x44(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
mov    -0x8(%rbp),%rcx
movsbl (%rcx),%ecx
cmp    %ecx,%eax
jne    1473 <binpatch+0x2d3>
movl   $0x1,-0x50(%rbp)
mov    -0x50(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    13d4 <binpatch+0x234>
mov    -0x38(%rbp),%rax
mov    -0x44(%rbp),%ecx
add    -0x50(%rbp),%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
mov    -0x8(%rbp),%rcx
movslq -0x50(%rbp),%rdx
movsbl (%rcx,%rdx,1),%ecx
cmp    %ecx,%eax
je     13c1 <binpatch+0x221>
jmp    146e <binpatch+0x2ce>
jmp    13c6 <binpatch+0x226>
mov    -0x50(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x50(%rbp)
jmp    138b <binpatch+0x1eb>
mov    -0x2c(%rbp),%eax
add    -0x44(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x40(%rbp),%rdi
mov    -0x2c(%rbp),%esi
mov    0x2bed(%rip),%rax
mov    (%rax),%edx
call   1090 <fseek@plt>
mov    %eax,-0x4c(%rbp)
cmpl   $0x0,-0x4c(%rbp)
jge    1420 <binpatch+0x280>
mov    -0x40(%rbp),%rdi
call   1040 <fclose@plt>
lea    0xc2a(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%edx
mov    -0x40(%rbp),%rcx
mov    $0x1,%esi
call   10c0 <fwrite@plt>
cmp    -0x20(%rbp),%eax
je     145f <binpatch+0x2bf>
mov    -0x40(%rbp),%rdi
call   1040 <fclose@plt>
lea    0xc0b(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x40(%rbp),%rdi
call   1040 <fclose@plt>
add    $0x50,%rsp
pop    %rbp
ret
jmp    1473 <binpatch+0x2d3>
jmp    1478 <binpatch+0x2d8>
mov    -0x44(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x44(%rbp)
jmp    135d <binpatch+0x1bd>
mov    -0x24(%rbp),%eax
add    -0x2c(%rbp),%eax
mov    %eax,-0x2c(%rbp)
jmp    12a1 <binpatch+0x101>
mov    -0x40(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x18(%rbp),%rsi
lea    0xbc2(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>

x86 assembly (clang, -O2):
<binpatch>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x48,%rsp
mov    %rdx,%r15
mov    %rsi,%r14
mov    %rdi,%r12
call   1060 <strlen@plt>
mov    %eax,%ebp
mov    %r14,0x40(%rsp)
mov    %r14,%rdi
call   1060 <strlen@plt>
mov    %eax,0xc(%rsp)
mov    %rbp,0x18(%rsp)
lea    0x10000(%rbp),%edi
mov    %edi,0x28(%rsp)
call   1090 <malloc@plt>
test   %rax,%rax
je     1436 <binpatch+0x286>
mov    %rax,%r14
lea    0xdfa(%rip),%rsi
mov    %r15,%rdi
call   10b0 <fopen@plt>
test   %rax,%rax
je     143f <binpatch+0x28f>
mov    %rax,%rbx
mov    0x2dc7(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   10a0 <fseek@plt>
mov    %rbx,0x10(%rsp)
mov    %rbx,%rdi
call   1080 <ftell@plt>
test   %eax,%eax
js     144b <binpatch+0x29b>
mov    %r15,0x30(%rsp)
je     13d2 <binpatch+0x222>
mov    %eax,%ecx
mov    0x18(%rsp),%rax
mov    %eax,%edx
not    %edx
mov    %edx,0x24(%rsp)
addl   $0x1,0xc(%rsp)
lea    0x1(%rax),%r13d
xor    %ebx,%ebx
mov    %ecx,0x20(%rsp)
mov    %r14,0x38(%rsp)
jmp    128c <binpatch+0xdc>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    0x20(%rsp),%ecx
cmp    %ebx,%ecx
jle    13d2 <binpatch+0x222>
mov    %ecx,%ebp
sub    %ebx,%ebp
mov    0x28(%rsp),%eax
cmp    %eax,%ebp
cmovg  %eax,%ebp
mov    0x2d38(%rip),%rax
mov    (%rax),%edx
mov    0x10(%rsp),%r15
mov    %r15,%rdi
mov    %ebx,%esi
call   10a0 <fseek@plt>
test   %eax,%eax
js     13f9 <binpatch+0x249>
mov    %r14,%rdi
mov    $0x1,%esi
mov    %ebp,%edx
mov    %r15,%rcx
call   1040 <fread@plt>
cmp    %ebp,%eax
jne    13fe <binpatch+0x24e>
mov    %ebp,%ecx
add    0x24(%rsp),%ecx
js     13b0 <binpatch+0x200>
mov    (%r12),%al
mov    %ecx,%r8d
sub    0x18(%rsp),%ebp
xor    %edx,%edx
mov    %r14,%rsi
xor    %r15d,%r15d
jmp    131b <binpatch+0x16b>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
cmp    %r8,%rdx
lea    0x1(%rdx),%rcx
setae  %r15b
add    $0x1,%rsi
mov    %rcx,%rdx
cmp    %rbp,%rcx
je     13b3 <binpatch+0x203>
cmp    %al,(%r14,%rdx,1)
jne    1300 <binpatch+0x150>
cmpl   $0x0,0x18(%rsp)
jle    1343 <binpatch+0x193>
mov    $0x1,%edi
nopl   (%rax)
movzbl (%rsi,%rdi,1),%ecx
cmp    (%r12,%rdi,1),%cl
jne    1300 <binpatch+0x150>
add    $0x1,%rdi
cmp    %rdi,%r13
jne    1330 <binpatch+0x180>
mov    %ebx,%esi
add    %edx,%esi
mov    0x2c8a(%rip),%rax
mov    (%rax),%edx
mov    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    %esi,%ebp
call   10a0 <fseek@plt>
test   %eax,%eax
js     140f <binpatch+0x25f>
mov    0x40(%rsp),%rdi
mov    $0x1,%esi
mov    0xc(%rsp),%r14d
mov    %r14d,%edx
mov    %rbx,%rcx
call   10d0 <fwrite@plt>
mov    %eax,0x2c(%rsp)
mov    %rbx,%rdi
call   1050 <fclose@plt>
cmp    %r14d,0x2c(%rsp)
jne    1420 <binpatch+0x270>
test   $0x1,%r15b
mov    0x38(%rsp),%r14
mov    %ebp,%ebx
jne    1280 <binpatch+0xd0>
jmp    13c3 <binpatch+0x213>
nopl   0x0(%rax,%rax,1)
mov    $0x1,%r15b
test   %r15b,%r15b
je     13c3 <binpatch+0x213>
add    $0x10000,%ebx
jmp    1280 <binpatch+0xd0>
add    $0x48,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x10(%rsp),%rdi
call   1050 <fclose@plt>
lea    0xc3b(%rip),%rdi
mov    0x30(%rsp),%rsi
xor    %eax,%eax
call   1070 <printf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    %r15,%rdi
jmp    1412 <binpatch+0x262>
mov    %r15,%rdi
call   1050 <fclose@plt>
lea    0xc43(%rip),%rdi
jmp    1427 <binpatch+0x277>
mov    %rbx,%rdi
call   1050 <fclose@plt>
lea    0xc41(%rip),%rdi
jmp    1427 <binpatch+0x277>
lea    0xc19(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
lea    0xc31(%rip),%rdi
jmp    1427 <binpatch+0x277>
lea    0xbbe(%rip),%rdi
mov    %r15,%rsi
jmp    13e8 <binpatch+0x238>
mov    0x10(%rsp),%rdi
jmp    1412 <binpatch+0x262>

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

LLVM IR (-O2):
@.str.1 = [4 x i8] c"r+b\00"
@.str.2 = [26 x i8] c"Could not open file \22%s\22\0A\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@.str.6 = [34 x i8] c"Did not find string in file \22%s\22\0A\00"
@str = [16 x i8] c"Could not write\00"
@str.8 = [15 x i8] c"Could not read\00"
@str.10 = [15 x i8] c"Could not seek\00"
@str.11 = [26 x i8] c"Could not allocate buffer\00"
define void @binpatch(i8* %0, i8* %1, i8* %2) {
%4 = tail call i32 @strlen(i8* %0)
%5 = xor i32 %4, -1
%6 = tail call i32 @strlen(i8* %1)
%7 = add nsw i32 %6, 1
%8 = add nsw i32 %4, 65536
%9 = tail call i8* @malloc(i32 %8)
%10 = icmp eq i8* %9, null
br i1 %10, label %11, label %14
11: ; preds = %3
%12 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @str.11, i64 0, i64 0))
%13 = tail call i32 @exit(i32 1)
unreachable
14: ; preds = %3
%15 = tail call i32* @fopen(i8* %2, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%16 = icmp eq i32* %15, null
br i1 %16, label %17, label %20
17: ; preds = %14
%18 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i8* %2)
%19 = tail call i32 @exit(i32 1)
unreachable
20: ; preds = %14
%21 = load i32, i32* @SEEK_END
%22 = tail call i32 @fseek(i32* %15, i32 0, i32 %21)
%23 = tail call i32 @ftell(i32* %15)
%24 = icmp slt i32 %23, 0
br i1 %24, label %30, label %25
25: ; preds = %20
%26 = icmp slt i32 %4, 1
%27 = add i32 %4, 1
%28 = zext i32 %27 to i64
%29 = icmp eq i32 %23, 0
br i1 %29, label %107, label %34
30: ; preds = %20
%31 = tail call i32 @fclose(i32* %15)
%32 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str.10, i64 0, i64 0))
%33 = tail call i32 @exit(i32 1)
unreachable
34: ; preds = %25, %104
%35 = phi i32 [ %105, %104 ], [ 0, %25 ]
%36 = sub nsw i32 %23, %35
%37 = icmp sgt i32 %36, %8
%38 = select i1 %37, i32 %8, i32 %36
%39 = load i32, i32* @SEEK_SET
%40 = tail call i32 @fseek(i32* %15, i32 %35, i32 %39)
%41 = icmp slt i32 %40, 0
br i1 %41, label %42, label %46
42: ; preds = %34
%43 = tail call i32 @fclose(i32* %15)
%44 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str.10, i64 0, i64 0))
%45 = tail call i32 @exit(i32 1)
unreachable
46: ; preds = %34
%47 = tail call i32 @fread(i8* %9, i32 1, i32 %38, i32* %15)
%48 = icmp eq i32 %47, %38
br i1 %48, label %53, label %49
49: ; preds = %46
%50 = tail call i32 @fclose(i32* %15)
%51 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str.8, i64 0, i64 0))
%52 = tail call i32 @exit(i32 1)
unreachable
53: ; preds = %46
%54 = add i32 %38, %5
%55 = icmp slt i32 %54, 0
br i1 %55, label %100, label %56
56: ; preds = %53
%57 = load i8, i8* %0
%58 = zext i32 %54 to i64
%59 = sub i32 %38, %4
%60 = zext i32 %59 to i64
br label %61
61: ; preds = %56, %96
%62 = phi i64 [ 0, %56 ], [ %97, %96 ]
%63 = phi i1 [ false, %56 ], [ %98, %96 ]
%64 = getelementptr inbounds i8, i8* %9, i64 %62
%65 = load i8, i8* %64
%66 = icmp eq i8 %65, %57
br i1 %66, label %67, label %96
67: ; preds = %61
br i1 %26, label %79, label %71
68: ; preds = %71
%69 = add nuw nsw i64 %72, 1
%70 = icmp eq i64 %69, %28
br i1 %70, label %79, label %71
71: ; preds = %67, %68
%72 = phi i64 [ %69, %68 ], [ 1, %67 ]
%73 = add nuw nsw i64 %72, %62
%74 = getelementptr inbounds i8, i8* %9, i64 %73
%75 = load i8, i8* %74
%76 = getelementptr inbounds i8, i8* %0, i64 %72
%77 = load i8, i8* %76
%78 = icmp eq i8 %75, %77
br i1 %78, label %68, label %96
79: ; preds = %67, %68
%80 = trunc i64 %62 to i32
%81 = add nsw i32 %35, %80
%82 = load i32, i32* @SEEK_SET
%83 = tail call i32 @fseek(i32* %15, i32 %81, i32 %82)
%84 = icmp slt i32 %83, 0
br i1 %84, label %85, label %89
85: ; preds = %79
%86 = tail call i32 @fclose(i32* %15)
%87 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str.10, i64 0, i64 0))
%88 = tail call i32 @exit(i32 1)
unreachable
89: ; preds = %79
%90 = tail call i32 @fwrite(i8* %1, i32 1, i32 %7, i32* %15)
%91 = icmp eq i32 %90, %7
%92 = tail call i32 @fclose(i32* %15)
br i1 %91, label %103, label %93
93: ; preds = %89
%94 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str, i64 0, i64 0))
%95 = tail call i32 @exit(i32 1)
unreachable
96: ; preds = %71, %61
%97 = add nuw nsw i64 %62, 1
%98 = icmp uge i64 %62, %58
%99 = icmp eq i64 %97, %60
br i1 %99, label %100, label %61
100: ; preds = %96, %53
%101 = phi i1 [ true, %53 ], [ %98, %96 ]
%102 = add nsw i32 %35, 65536
br i1 %101, label %104, label %111
103: ; preds = %89
br i1 %63, label %104, label %111
104: ; preds = %103, %100
%105 = phi i32 [ %81, %103 ], [ %102, %100 ]
%106 = icmp sgt i32 %23, %105
br i1 %106, label %34, label %107
107: ; preds = %104, %25
%108 = tail call i32 @fclose(i32* %15)
%109 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @.str.6, i64 0, i64 0), i8* %2)
%110 = tail call i32 @exit(i32 1)
unreachable
111: ; preds = %100, %103
ret void
}