#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 */
long binary_search_bytes_in_file(int /*<<< orphan*/ *, char *, size_t);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
long fwrite(char *, size_t, int, int /*<<< orphan*/ *);
int file_patch(char *filename, char *original, size_t szOriginal, char *patch,
               size_t szPatch) {
  long offsetOriginal, writtenBytes;
  FILE *fp;
  if (!filename)
    return -1;
  fp = fopen(filename, "r+");
  if (!fp)
    return -1;
  offsetOriginal = binary_search_bytes_in_file(fp, original, szOriginal);
  if (!offsetOriginal)
    return -1;
  writtenBytes = fwrite(patch, szPatch, 1, fp);
  fclose(fp);
  return writtenBytes;
}


x86 assembly (clang, -O0):
<file_patch>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8,-0x30(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1173 <file_patch+0x33>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f4 <file_patch+0xb4>
mov    -0x10(%rbp),%rdi
lea    0xe82(%rip),%rsi
call   1040 <fopen@plt>
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
jne    119e <file_patch+0x5e>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f4 <file_patch+0xb4>
mov    -0x48(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1050 <binary_search_bytes_in_file@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    11ca <file_patch+0x8a>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f4 <file_patch+0xb4>
mov    -0x28(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x48(%rbp),%rcx
mov    $0x1,%edx
call   1060 <fwrite@plt>
mov    %rax,-0x40(%rbp)
mov    -0x48(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x40(%rbp),%rax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<file_patch>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    $0xffffffff,%r12d
test   %rdi,%rdi
je     11a7 <file_patch+0x67>
mov    %r8,%r14
mov    %rcx,%r15
mov    %rdx,%r13
mov    %rsi,%rbp
lea    0xe97(%rip),%rsi
call   1040 <fopen@plt>
test   %rax,%rax
je     11a7 <file_patch+0x67>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %rbp,%rsi
mov    %r13,%rdx
call   1050 <binary_search_bytes_in_file@plt>
test   %rax,%rax
je     11a7 <file_patch+0x67>
mov    %r15,%rdi
mov    %r14,%rsi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1060 <fwrite@plt>
mov    %rax,%r12
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    %r12d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"r+\00"
define i32 @file_patch(i8* %0, i8* %1, i64 %2, i8* %3, i64 %4) {
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i8*
%11 = alloca i64
%12 = alloca i64
%13 = alloca i64
%14 = alloca i32*
store i8* %0, i8** %7
store i8* %1, i8** %8
store i64 %2, i64* %9
store i8* %3, i8** %10
store i64 %4, i64* %11
%15 = load i8*, i8** %7
%16 = icmp ne i8* %15, null
br i1 %16, label %18, label %17
17: ; preds = %5
store i32 -1, i32* %6
br label %41
18: ; preds = %5
%19 = load i8*, i8** %7
%20 = call i32* @fopen(i8* %19, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %20, i32** %14
%21 = load i32*, i32** %14
%22 = icmp ne i32* %21, null
br i1 %22, label %24, label %23
23: ; preds = %18
store i32 -1, i32* %6
br label %41
24: ; preds = %18
%25 = load i32*, i32** %14
%26 = load i8*, i8** %8
%27 = load i64, i64* %9
%28 = call i64 @binary_search_bytes_in_file(i32* %25, i8* %26, i64 %27)
store i64 %28, i64* %12
%29 = load i64, i64* %12
%30 = icmp ne i64 %29, 0
br i1 %30, label %32, label %31
31: ; preds = %24
store i32 -1, i32* %6
br label %41
32: ; preds = %24
%33 = load i8*, i8** %10
%34 = load i64, i64* %11
%35 = load i32*, i32** %14
%36 = call i64 @fwrite(i8* %33, i64 %34, i32 1, i32* %35)
store i64 %36, i64* %13
%37 = load i32*, i32** %14
%38 = call i32 @fclose(i32* %37)
%39 = load i64, i64* %13
%40 = trunc i64 %39 to i32
store i32 %40, i32* %6
br label %41
41: ; preds = %32, %31, %23, %17
%42 = load i32, i32* %6
ret i32 %42
}

LLVM IR (-O2):
@.str = [3 x i8] c"r+\00"
define i32 @file_patch(i8* %0, i8* %1, i64 %2, i8* nocapture %3, i64 %4) {
%6 = icmp eq i8* %0, null
br i1 %6, label %17, label %7
7: ; preds = %5
%8 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%9 = icmp eq i32* %8, null
br i1 %9, label %17, label %10
10: ; preds = %7
%11 = tail call i64 @binary_search_bytes_in_file(i32* %8, i8* %1, i64 %2)
%12 = icmp eq i64 %11, 0
br i1 %12, label %17, label %13
13: ; preds = %10
%14 = tail call i64 @fwrite(i8* %3, i64 %4, i32 1, i32* %8)
%15 = tail call i32 @fclose(i32* %8)
%16 = trunc i64 %14 to i32
br label %17
17: ; preds = %10, %7, %5, %13
%18 = phi i32 [ %16, %13 ], [ -1, %5 ], [ -1, %7 ], [ -1, %10 ]
ret i32 %18
}