#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*/ uint8_t;
struct stat {
  int dummy;
};
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ cart_make_nes(int /*<<< orphan*/ *, long,
                                 int /*<<< orphan*/ *, long,
                                 int /*<<< orphan*/ *, int /*<<< orphan*/,
                                 int /*<<< orphan*/);
int /*<<< orphan*/ fread(int /*<<< orphan*/ *, long, int, int /*<<< orphan*/ *);
long get_filesize(int /*<<< orphan*/ *);
scalar_t__ malloc(long);
int cart_fmake_nes(FILE *output, FILE *prg, FILE *chr, uint8_t mapper,
                   uint8_t mirroring_mask) {
  struct stat sb;
  long prg_size_in_bytes = 0;
  long chr_size_in_bytes = 0;
  int errorcode = 0;
  if (!prg)
    return -1;
  prg_size_in_bytes = get_filesize(prg);
  if (prg_size_in_bytes < 0)
    return -1;
  if (chr) {
    chr_size_in_bytes = get_filesize(chr);
    if (chr_size_in_bytes < 0)
      return -1;
  }
  {
    uint8_t *prg_mem = NULL;
    uint8_t *chr_mem = NULL;
    prg_mem = (uint8_t *)malloc(prg_size_in_bytes);
    if (!prg_mem)
      return -2;
    chr_mem = (uint8_t *)malloc(chr_size_in_bytes);
    if (!chr_mem)
      return -2;
    fread(prg_mem, prg_size_in_bytes, 1, prg);
    if (chr) {
      fread(chr_mem, chr_size_in_bytes, 1, chr);
    }
    cart_make_nes(output, prg_size_in_bytes, prg_mem, chr_size_in_bytes,
                  chr_mem, mapper, mirroring_mask);
  }
  return 0;
}


x86 assembly (clang, -O0):
<cart_fmake_nes>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
mov    %r8d,-0x28(%rbp)
movq   $0x0,-0x38(%rbp)
movq   $0x0,-0x40(%rbp)
movl   $0x0,-0x44(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1189 <cart_fmake_nes+0x49>
movl   $0xffffffff,-0x4(%rbp)
jmp    129a <cart_fmake_nes+0x15a>
mov    -0x18(%rbp),%rdi
call   1060 <get_filesize@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jge    11ad <cart_fmake_nes+0x6d>
movl   $0xffffffff,-0x4(%rbp)
jmp    129a <cart_fmake_nes+0x15a>
cmpq   $0x0,-0x20(%rbp)
je     11e1 <cart_fmake_nes+0xa1>
mov    -0x20(%rbp),%rdi
call   1060 <get_filesize@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jge    11dc <cart_fmake_nes+0x9c>
movl   $0xffffffff,-0x4(%rbp)
jmp    129a <cart_fmake_nes+0x15a>
jmp    11e1 <cart_fmake_nes+0xa1>
movq   $0x0,-0x50(%rbp)
movq   $0x0,-0x58(%rbp)
mov    -0x38(%rbp),%rdi
call   1050 <malloc@plt>
mov    %rax,-0x50(%rbp)
cmpq   $0x0,-0x50(%rbp)
jne    1215 <cart_fmake_nes+0xd5>
movl   $0xfffffffe,-0x4(%rbp)
jmp    129a <cart_fmake_nes+0x15a>
mov    -0x40(%rbp),%rdi
call   1050 <malloc@plt>
mov    %rax,-0x58(%rbp)
cmpq   $0x0,-0x58(%rbp)
jne    1239 <cart_fmake_nes+0xf9>
movl   $0xfffffffe,-0x4(%rbp)
jmp    129a <cart_fmake_nes+0x15a>
mov    -0x50(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    -0x18(%rbp),%rcx
mov    $0x1,%edx
call   1030 <fread@plt>
cmpq   $0x0,-0x20(%rbp)
je     1270 <cart_fmake_nes+0x130>
mov    -0x58(%rbp),%rdi
mov    -0x40(%rbp),%rsi
mov    -0x20(%rbp),%rcx
mov    $0x1,%edx
call   1030 <fread@plt>
mov    -0x10(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    -0x50(%rbp),%rdx
mov    -0x40(%rbp),%rcx
mov    -0x58(%rbp),%r8
mov    -0x24(%rbp),%r9d
mov    -0x28(%rbp),%eax
mov    %eax,(%rsp)
call   1040 <cart_make_nes@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cart_fmake_nes>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    $0xffffffff,%r15d
test   %rsi,%rsi
je     121f <cart_fmake_nes+0xdf>
mov    %r8d,%ebp
mov    %ecx,%ebx
mov    %rdx,%r13
mov    %rsi,%r12
mov    %rdi,0x20(%rsp)
mov    %rsi,%rdi
call   1060 <get_filesize@plt>
test   %rax,%rax
js     121f <cart_fmake_nes+0xdf>
mov    %rax,%r14
mov    %ebp,0x10(%rsp)
mov    %ebx,0x14(%rsp)
test   %r13,%r13
je     11a0 <cart_fmake_nes+0x60>
mov    %r13,%rdi
call   1060 <get_filesize@plt>
mov    %rax,%rbx
test   %rax,%rax
jns    11a2 <cart_fmake_nes+0x62>
jmp    121f <cart_fmake_nes+0xdf>
xor    %ebx,%ebx
mov    %r14,%rdi
call   1050 <malloc@plt>
mov    $0xfffffffe,%r15d
test   %rax,%rax
je     121f <cart_fmake_nes+0xdf>
mov    %rax,%rbp
mov    %rbx,%rdi
call   1050 <malloc@plt>
mov    %rax,0x18(%rsp)
test   %rax,%rax
je     121f <cart_fmake_nes+0xdf>
mov    %rbp,%rdi
mov    %r14,%rsi
mov    $0x1,%edx
mov    %r12,%rcx
call   1030 <fread@plt>
test   %r13,%r13
mov    0x18(%rsp),%r15
je     11fa <cart_fmake_nes+0xba>
mov    %r15,%rdi
mov    %rbx,%rsi
mov    $0x1,%edx
mov    %r13,%rcx
call   1030 <fread@plt>
mov    0x10(%rsp),%eax
mov    %eax,(%rsp)
mov    0x20(%rsp),%rdi
mov    %r14,%rsi
mov    %rbp,%rdx
mov    %rbx,%rcx
mov    %r15,%r8
mov    0x14(%rsp),%r9d
call   1040 <cart_make_nes@plt>
xor    %r15d,%r15d
mov    %r15d,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
define i32 @cart_fmake_nes(i32* %0, i32* %1, i32* %2, i32 %3, i32 %4) {
%6 = alloca i32
%7 = alloca i32*
%8 = alloca i32*
%9 = alloca i32*
%10 = alloca i32
%11 = alloca i32
%12 = alloca %struct.stat
%13 = alloca i64
%14 = alloca i64
%15 = alloca i32
%16 = alloca i32*
%17 = alloca i32*
store i32* %0, i32** %7
store i32* %1, i32** %8
store i32* %2, i32** %9
store i32 %3, i32* %10
store i32 %4, i32* %11
store i64 0, i64* %13
store i64 0, i64* %14
store i32 0, i32* %15
%18 = load i32*, i32** %8
%19 = icmp ne i32* %18, null
br i1 %19, label %21, label %20
20: ; preds = %5
store i32 -1, i32* %6
br label %72
21: ; preds = %5
%22 = load i32*, i32** %8
%23 = call i64 @get_filesize(i32* %22)
store i64 %23, i64* %13
%24 = load i64, i64* %13
%25 = icmp slt i64 %24, 0
br i1 %25, label %26, label %27
26: ; preds = %21
store i32 -1, i32* %6
br label %72
27: ; preds = %21
%28 = load i32*, i32** %9
%29 = icmp ne i32* %28, null
br i1 %29, label %30, label %37
30: ; preds = %27
%31 = load i32*, i32** %9
%32 = call i64 @get_filesize(i32* %31)
store i64 %32, i64* %14
%33 = load i64, i64* %14
%34 = icmp slt i64 %33, 0
br i1 %34, label %35, label %36
35: ; preds = %30
store i32 -1, i32* %6
br label %72
36: ; preds = %30
br label %37
37: ; preds = %36, %27
store i32* null, i32** %16
store i32* null, i32** %17
%38 = load i64, i64* %13
%39 = call i64 @malloc(i64 %38)
%40 = inttoptr i64 %39 to i32*
store i32* %40, i32** %16
%41 = load i32*, i32** %16
%42 = icmp ne i32* %41, null
br i1 %42, label %44, label %43
43: ; preds = %37
store i32 -2, i32* %6
br label %72
44: ; preds = %37
%45 = load i64, i64* %14
%46 = call i64 @malloc(i64 %45)
%47 = inttoptr i64 %46 to i32*
store i32* %47, i32** %17
%48 = load i32*, i32** %17
%49 = icmp ne i32* %48, null
br i1 %49, label %51, label %50
50: ; preds = %44
store i32 -2, i32* %6
br label %72
51: ; preds = %44
%52 = load i32*, i32** %16
%53 = load i64, i64* %13
%54 = load i32*, i32** %8
%55 = call i32 @fread(i32* %52, i64 %53, i32 1, i32* %54)
%56 = load i32*, i32** %9
%57 = icmp ne i32* %56, null
br i1 %57, label %58, label %63
58: ; preds = %51
%59 = load i32*, i32** %17
%60 = load i64, i64* %14
%61 = load i32*, i32** %9
%62 = call i32 @fread(i32* %59, i64 %60, i32 1, i32* %61)
br label %63
63: ; preds = %58, %51
%64 = load i32*, i32** %7
%65 = load i64, i64* %13
%66 = load i32*, i32** %16
%67 = load i64, i64* %14
%68 = load i32*, i32** %17
%69 = load i32, i32* %10
%70 = load i32, i32* %11
%71 = call i32 @cart_make_nes(i32* %64, i64 %65, i32* %66, i64 %67, i32* %68, i32 %69, i32 %70)
store i32 0, i32* %6
br label %72
72: ; preds = %63, %50, %43, %35, %26, %20
%73 = load i32, i32* %6
ret i32 %73
}

LLVM IR (-O2):
define i32 @cart_fmake_nes(i32* %0, i32* %1, i32* %2, i32 %3, i32 %4) {
%6 = icmp eq i32* %1, null
br i1 %6, label %30, label %7
7: ; preds = %5
%8 = tail call i64 @get_filesize(i32* %1)
%9 = icmp slt i64 %8, 0
br i1 %9, label %30, label %10
10: ; preds = %7
%11 = icmp eq i32* %2, null
br i1 %11, label %15, label %12
12: ; preds = %10
%13 = tail call i64 @get_filesize(i32* %2)
%14 = icmp slt i64 %13, 0
br i1 %14, label %30, label %15
15: ; preds = %12, %10
%16 = phi i64 [ %13, %12 ], [ 0, %10 ]
%17 = tail call i64 @malloc(i64 %8)
%18 = inttoptr i64 %17 to i32*
%19 = icmp eq i64 %17, 0
br i1 %19, label %30, label %20
20: ; preds = %15
%21 = tail call i64 @malloc(i64 %16)
%22 = inttoptr i64 %21 to i32*
%23 = icmp eq i64 %21, 0
br i1 %23, label %30, label %24
24: ; preds = %20
%25 = tail call i32 @fread(i32* %18, i64 %8, i32 1, i32* %1)
br i1 %11, label %28, label %26
26: ; preds = %24
%27 = tail call i32 @fread(i32* %22, i64 %16, i32 1, i32* %2)
br label %28
28: ; preds = %26, %24
%29 = tail call i32 @cart_make_nes(i32* %0, i64 %8, i32* %18, i64 %16, i32* %22, i32 %3, i32 %4)
br label %30
30: ; preds = %20, %15, %28, %12, %7, %5
%31 = phi i32 [ -1, %5 ], [ -1, %7 ], [ -1, %12 ], [ 0, %28 ], [ -2, %15 ], [ -2, %20 ]
ret i32 %31
}