#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*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fwrite(unsigned char *, int, size_t, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *);
int apply_changes(unsigned char *buf, char *rom, size_t s) {
  FILE *overwrite = fopen(rom, "wb");
  if (overwrite) {
    fwrite(buf, 1, s, overwrite);
    fclose(overwrite);
    return 0;
  } else {
    printf("no such file??\n");
    fclose(overwrite);
    return 1;
  }
}


x86 assembly (clang, -O0):
<apply_changes>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xea1(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     119e <apply_changes+0x5e>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    -0x28(%rbp),%rcx
mov    $0x1,%esi
call   1060 <fwrite@plt>
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x0,-0x4(%rbp)
jmp    11ba <apply_changes+0x7a>
lea    0xe5e(%rip),%rdi
call   1040 <printf@plt>
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<apply_changes>:
push   %r15
push   %r14
push   %rbx
mov    %rdx,%r14
mov    %rsi,%rax
mov    %rdi,%r15
lea    0xeab(%rip),%rsi
mov    %rax,%rdi
call   1050 <fopen@plt>
test   %rax,%rax
je     1184 <apply_changes+0x44>
mov    %rax,%rbx
mov    %r15,%rdi
mov    $0x1,%esi
mov    %r14,%rdx
mov    %rax,%rcx
call   1060 <fwrite@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
jmp    119c <apply_changes+0x5c>
lea    0xe78(%rip),%rdi
call   1030 <puts@plt>
xor    %edi,%edi
call   1040 <fclose@plt>
mov    $0x1,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [3 x i8] c"wb\00"
@.str.1 = [16 x i8] c"no such file??\0A\00"
define i32 @apply_changes(i8* %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32*
store i8* %0, i8** %5
store i8* %1, i8** %6
store i64 %2, i64* %7
%9 = load i8*, i8** %6
%10 = call i32* @fopen(i8* %9, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %10, i32** %8
%11 = load i32*, i32** %8
%12 = icmp ne i32* %11, null
br i1 %12, label %13, label %20
13: ; preds = %3
%14 = load i8*, i8** %5
%15 = load i64, i64* %7
%16 = load i32*, i32** %8
%17 = call i32 @fwrite(i8* %14, i32 1, i64 %15, i32* %16)
%18 = load i32*, i32** %8
%19 = call i32 @fclose(i32* %18)
store i32 0, i32* %4
br label %24
20: ; preds = %3
%21 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
%22 = load i32*, i32** %8
%23 = call i32 @fclose(i32* %22)
store i32 1, i32* %4
br label %24
24: ; preds = %20, %13
%25 = load i32, i32* %4
ret i32 %25
}

LLVM IR (-O2):
@.str = [3 x i8] c"wb\00"
@str = [15 x i8] c"no such file??\00"
define i32 @apply_changes(i8* nocapture %0, i8* nocapture %1, i64 %2) {
%4 = tail call i32* @fopen(i8* %1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %9, label %6
6: ; preds = %3
%7 = tail call i32 @fwrite(i8* %0, i32 1, i64 %2, i32* %4)
%8 = tail call i32 @fclose(i32* %4)
br label %12
9: ; preds = %3
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str, i64 0, i64 0))
%11 = tail call i32 @fclose(i32* null)
br label %12
12: ; preds = %9, %6
%13 = phi i32 [ 0, %6 ], [ 1, %9 ]
ret i32 %13
}