#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 uint16_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int *alloc(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ free(int *);
long fwrite(int *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
bool write_rom_file(char *fn, uint16_t *ROM) {
  bool result = false;
  FILE *f = NULL;
  uint16_t *ROM2 = NULL;
  long size_written;
  int i;
  f = fopen(fn, "wb");
  if (!f) {
    fprintf(stderr, "ERROR: File Open Failed: %s\n", fn);
    goto done;
  }
  ROM2 = alloc(sizeof(uint16_t) * 0x1000);
  for (i = 0; i < 0x1000; i++)
    ROM2[i] = (ROM[i] << 8) | (ROM[i] >> 8);
  size_written = fwrite(ROM2, 1, 8192, f);
  if (size_written != 8192) {
    fprintf(stderr, "ERROR: File Write Failed: %s\n", fn);
    goto done;
  }
  result = true;
done:
  if (f)
    fclose(f);
  if (ROM2)
    free(ROM2);
  return result;
}


x86 assembly (clang, -O0):
<write_rom_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe6e(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11c4 <write_rom_file+0x64>
mov    0x2e33(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xe49(%rip),%rsi
call   1050 <fprintf@plt>
jmp    126b <write_rom_file+0x10b>
mov    $0x4000,%edi
call   1060 <alloc@plt>
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x34(%rbp)
cmpl   $0x1000,-0x34(%rbp)
jge    121d <write_rom_file+0xbd>
mov    -0x10(%rbp),%rax
movslq -0x34(%rbp),%rcx
mov    (%rax,%rcx,4),%edx
shl    $0x8,%edx
mov    -0x10(%rbp),%rax
movslq -0x34(%rbp),%rcx
mov    (%rax,%rcx,4),%eax
sar    $0x8,%eax
or     %eax,%edx
mov    -0x28(%rbp),%rax
movslq -0x34(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    11d9 <write_rom_file+0x79>
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
mov    $0x2000,%edx
call   1080 <fwrite@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x2000,-0x30(%rbp)
je     1264 <write_rom_file+0x104>
mov    0x2d93(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xdc6(%rip),%rsi
call   1050 <fprintf@plt>
jmp    126b <write_rom_file+0x10b>
movl   $0x1,-0x14(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     127f <write_rom_file+0x11f>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
cmpq   $0x0,-0x28(%rbp)
je     1293 <write_rom_file+0x133>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x14(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_rom_file>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %rdi,%r14
lea    0xe8b(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     11e5 <write_rom_file+0x85>
mov    %rax,%r15
mov    $0x4000,%edi
call   1060 <alloc@plt>
mov    %rax,%r12
lea    0x4000(%rbx),%rax
cmp    %rax,%r12
jae    1204 <write_rom_file+0xa4>
mov    %r12,%rax
add    $0x4000,%rax
cmp    %rbx,%rax
jbe    1204 <write_rom_file+0xa4>
xor    %eax,%eax
nopl   0x0(%rax,%rax,1)
mov    (%rbx,%rax,4),%ecx
mov    %ecx,%edx
shl    $0x8,%edx
sar    $0x8,%ecx
or     %edx,%ecx
mov    %ecx,(%r12,%rax,4)
mov    0x4(%rbx,%rax,4),%ecx
mov    %ecx,%edx
shl    $0x8,%edx
sar    $0x8,%ecx
or     %edx,%ecx
mov    %ecx,0x4(%r12,%rax,4)
add    $0x2,%rax
cmp    $0x1000,%rax
jne    11b0 <write_rom_file+0x50>
jmp    129a <write_rom_file+0x13a>
mov    0x2df4(%rip),%rax
mov    (%rax),%edi
lea    0xe0e(%rip),%rsi
mov    %r14,%rdx
call   1050 <fprintf@plt>
xor    %ebx,%ebx
jmp    12e6 <write_rom_file+0x186>
xor    %eax,%eax
cs nopw 0x0(%rax,%rax,1)
movdqu (%rbx,%rax,4),%xmm0
movdqu 0x10(%rbx,%rax,4),%xmm1
movdqa %xmm0,%xmm2
pslld  $0x8,%xmm2
movdqa %xmm1,%xmm3
pslld  $0x8,%xmm3
psrad  $0x8,%xmm0
por    %xmm2,%xmm0
psrad  $0x8,%xmm1
por    %xmm3,%xmm1
movdqu %xmm0,(%r12,%rax,4)
movdqu %xmm1,0x10(%r12,%rax,4)
movdqu 0x20(%rbx,%rax,4),%xmm0
movdqu 0x30(%rbx,%rax,4),%xmm1
movdqa %xmm0,%xmm2
pslld  $0x8,%xmm2
movdqa %xmm1,%xmm3
pslld  $0x8,%xmm3
psrad  $0x8,%xmm0
por    %xmm2,%xmm0
psrad  $0x8,%xmm1
por    %xmm3,%xmm1
movdqu %xmm0,0x20(%r12,%rax,4)
movdqu %xmm1,0x30(%r12,%rax,4)
add    $0x10,%rax
cmp    $0x1000,%rax
jne    1210 <write_rom_file+0xb0>
mov    $0x1,%ebx
mov    %r12,%rdi
mov    $0x1,%esi
mov    $0x2000,%edx
mov    %r15,%rcx
call   1080 <fwrite@plt>
cmp    $0x2000,%rax
je     12d6 <write_rom_file+0x176>
mov    0x2d1d(%rip),%rax
mov    (%rax),%edi
lea    0xd54(%rip),%rsi
mov    %r14,%rdx
call   1050 <fprintf@plt>
xor    %ebx,%ebx
mov    %r15,%rdi
call   1040 <fclose@plt>
mov    %r12,%rdi
call   1030 <free@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [3 x i8] c"wb\00"
@stderr = global i32 0
@.str.1 = [29 x i8] c"ERROR: File Open Failed: %s\0A\00"
@.str.2 = [30 x i8] c"ERROR: File Write Failed: %s\0A\00"
define i32 @write_rom_file(i8* %0, i32* %1) {
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca i64
%9 = alloca i32
store i8* %0, i8** %3
store i32* %1, i32** %4
store i32 0, i32* %5
store i32* null, i32** %6
store i32* null, i32** %7
%10 = load i8*, i8** %3
%11 = call i32* @fopen(i8* %10, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %11, i32** %6
%12 = load i32*, i32** %6
%13 = icmp ne i32* %12, null
br i1 %13, label %18, label %14
14: ; preds = %2
%15 = load i32, i32* @stderr
%16 = load i8*, i8** %3
%17 = call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i8* %16)
br label %55
18: ; preds = %2
%19 = call i32* @alloc(i32 16384)
store i32* %19, i32** %7
store i32 0, i32* %9
br label %20
20: ; preds = %41, %18
%21 = load i32, i32* %9
%22 = icmp slt i32 %21, 4096
br i1 %22, label %23, label %44
23: ; preds = %20
%24 = load i32*, i32** %4
%25 = load i32, i32* %9
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds i32, i32* %24, i64 %26
%28 = load i32, i32* %27
%29 = shl i32 %28, 8
%30 = load i32*, i32** %4
%31 = load i32, i32* %9
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds i32, i32* %30, i64 %32
%34 = load i32, i32* %33
%35 = ashr i32 %34, 8
%36 = or i32 %29, %35
%37 = load i32*, i32** %7
%38 = load i32, i32* %9
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds i32, i32* %37, i64 %39
store i32 %36, i32* %40
br label %41
41: ; preds = %23
%42 = load i32, i32* %9
%43 = add nsw i32 %42, 1
store i32 %43, i32* %9
br label %20
44: ; preds = %20
%45 = load i32*, i32** %7
%46 = load i32*, i32** %6
%47 = call i64 @fwrite(i32* %45, i32 1, i32 8192, i32* %46)
store i64 %47, i64* %8
%48 = load i64, i64* %8
%49 = icmp ne i64 %48, 8192
br i1 %49, label %50, label %54
50: ; preds = %44
%51 = load i32, i32* @stderr
%52 = load i8*, i8** %3
%53 = call i32 @fprintf(i32 %51, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0), i8* %52)
br label %55
54: ; preds = %44
store i32 1, i32* %5
br label %55
55: ; preds = %54, %50, %14
%56 = load i32*, i32** %6
%57 = icmp ne i32* %56, null
br i1 %57, label %58, label %61
58: ; preds = %55
%59 = load i32*, i32** %6
%60 = call i32 @fclose(i32* %59)
br label %61
61: ; preds = %58, %55
%62 = load i32*, i32** %7
%63 = icmp ne i32* %62, null
br i1 %63, label %64, label %67
64: ; preds = %61
%65 = load i32*, i32** %7
%66 = call i32 @free(i32* %65)
br label %67
67: ; preds = %64, %61
%68 = load i32, i32* %5
ret i32 %68
}

LLVM IR (-O2):
@.str = [3 x i8] c"wb\00"
@stderr = global i32 0
@.str.1 = [29 x i8] c"ERROR: File Open Failed: %s\0A\00"
@.str.2 = [30 x i8] c"ERROR: File Write Failed: %s\0A\00"
define i32 @write_rom_file(i8* %0, i32* nocapture %1) {
%3 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %5, label %8
5: ; preds = %2
%6 = load i32, i32* @stderr
%7 = tail call i32 @fprintf(i32 %6, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %79
8: ; preds = %2
%9 = tail call i32* @alloc(i32 16384)
%10 = getelementptr i32, i32* %9, i64 4096
%11 = getelementptr i32, i32* %1, i64 4096
%12 = icmp ult i32* %9, %11
%13 = icmp ugt i32* %10, %1
%14 = and i1 %12, %13
br i1 %14, label %52, label %15
15: ; preds = %8, %15
%16 = phi i64 [ %50, %15 ], [ 0, %8 ]
%17 = getelementptr inbounds i32, i32* %1, i64 %16
%18 = bitcast i32* %17 to <4 x i32>*
%19 = load <4 x i32>, <4 x i32>* %18
%20 = getelementptr inbounds i32, i32* %17, i64 4
%21 = bitcast i32* %20 to <4 x i32>*
%22 = load <4 x i32>, <4 x i32>* %21
%23 = shl <4 x i32> %19, <i32 8, i32 8, i32 8, i32 8>
%24 = shl <4 x i32> %22, <i32 8, i32 8, i32 8, i32 8>
%25 = ashr <4 x i32> %19, <i32 8, i32 8, i32 8, i32 8>
%26 = ashr <4 x i32> %22, <i32 8, i32 8, i32 8, i32 8>
%27 = or <4 x i32> %23, %25
%28 = or <4 x i32> %24, %26
%29 = getelementptr inbounds i32, i32* %9, i64 %16
%30 = bitcast i32* %29 to <4 x i32>*
store <4 x i32> %27, <4 x i32>* %30
%31 = getelementptr inbounds i32, i32* %29, i64 4
%32 = bitcast i32* %31 to <4 x i32>*
store <4 x i32> %28, <4 x i32>* %32
%33 = or i64 %16, 8
%34 = getelementptr inbounds i32, i32* %1, i64 %33
%35 = bitcast i32* %34 to <4 x i32>*
%36 = load <4 x i32>, <4 x i32>* %35
%37 = getelementptr inbounds i32, i32* %34, i64 4
%38 = bitcast i32* %37 to <4 x i32>*
%39 = load <4 x i32>, <4 x i32>* %38
%40 = shl <4 x i32> %36, <i32 8, i32 8, i32 8, i32 8>
%41 = shl <4 x i32> %39, <i32 8, i32 8, i32 8, i32 8>
%42 = ashr <4 x i32> %36, <i32 8, i32 8, i32 8, i32 8>
%43 = ashr <4 x i32> %39, <i32 8, i32 8, i32 8, i32 8>
%44 = or <4 x i32> %40, %42
%45 = or <4 x i32> %41, %43
%46 = getelementptr inbounds i32, i32* %9, i64 %33
%47 = bitcast i32* %46 to <4 x i32>*
store <4 x i32> %44, <4 x i32>* %47
%48 = getelementptr inbounds i32, i32* %46, i64 4
%49 = bitcast i32* %48 to <4 x i32>*
store <4 x i32> %45, <4 x i32>* %49
%50 = add nuw nsw i64 %16, 16
%51 = icmp eq i64 %50, 4096
br i1 %51, label %69, label %15
52: ; preds = %8, %52
%53 = phi i64 [ %67, %52 ], [ 0, %8 ]
%54 = getelementptr inbounds i32, i32* %1, i64 %53
%55 = load i32, i32* %54
%56 = shl i32 %55, 8
%57 = ashr i32 %55, 8
%58 = or i32 %56, %57
%59 = getelementptr inbounds i32, i32* %9, i64 %53
store i32 %58, i32* %59
%60 = or i64 %53, 1
%61 = getelementptr inbounds i32, i32* %1, i64 %60
%62 = load i32, i32* %61
%63 = shl i32 %62, 8
%64 = ashr i32 %62, 8
%65 = or i32 %63, %64
%66 = getelementptr inbounds i32, i32* %9, i64 %60
store i32 %65, i32* %66
%67 = add nuw nsw i64 %53, 2
%68 = icmp eq i64 %67, 4096
br i1 %68, label %69, label %52
69: ; preds = %15, %52
%70 = tail call i64 @fwrite(i32* %9, i32 1, i32 8192, i32* %3)
%71 = icmp eq i64 %70, 8192
br i1 %71, label %75, label %72
72: ; preds = %69
%73 = load i32, i32* @stderr
%74 = tail call i32 @fprintf(i32 %73, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0), i8* %0)
br label %75
75: ; preds = %72, %69
%76 = phi i32 [ 0, %72 ], [ 1, %69 ]
%77 = tail call i32 @fclose(i32* %3)
%78 = tail call i32 @free(i32* %9)
br label %79
79: ; preds = %5, %75
%80 = phi i32 [ 0, %5 ], [ %76, %75 ]
ret i32 %80
}