#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_SET;
int fread(unsigned char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(unsigned char *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
scalar_t__ malloc(int);
int /*<<< orphan*/ printf(char *);
scalar_t__ strncmp(char *, char const *, int);
int NESVerifyROM(FILE *ifile) {
  if (!ifile)
    return 0;
  fseek(ifile, 0, SEEK_SET);
  unsigned char *four_byte_header = (unsigned char *)malloc(4);
  if (fread(four_byte_header, 1, 4, ifile) != (4)) {
    printf("ERROR READING\n");
    free(four_byte_header);
    return 0;
  }
  if (strncmp("NES\x01a", (const char *)four_byte_header, 4) != 0) {
    free(four_byte_header);
    return 0;
  }
  free(four_byte_header);
  return 1;
}


x86 assembly (clang, -O0):
<NESVerifyROM>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1183 <NESVerifyROM+0x23>
movl   $0x0,-0x4(%rbp)
jmp    122a <NESVerifyROM+0xca>
mov    -0x10(%rbp),%rdi
mov    0x2e52(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1080 <fseek@plt>
mov    $0x4,%edi
call   1070 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
mov    $0x4,%edx
call   1050 <fread@plt>
cmp    $0x4,%eax
je     11e6 <NESVerifyROM+0x86>
lea    0xe34(%rip),%rdi
call   1060 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
jmp    122a <NESVerifyROM+0xca>
mov    -0x18(%rbp),%rsi
lea    0xe1e(%rip),%rdi
mov    $0x4,%edx
call   1040 <strncmp@plt>
cmp    $0x0,%rax
je     121a <NESVerifyROM+0xba>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
jmp    122a <NESVerifyROM+0xca>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<NESVerifyROM>:
push   %rbp
push   %r14
push   %rbx
test   %rdi,%rdi
je     11c4 <NESVerifyROM+0x64>
mov    %rdi,%rbx
mov    0x2e6d(%rip),%rax
mov    (%rax),%edx
xor    %ebp,%ebp
xor    %esi,%esi
call   1080 <fseek@plt>
mov    $0x4,%edi
call   1070 <malloc@plt>
mov    %rax,%r14
mov    %rax,%rdi
mov    $0x1,%esi
mov    $0x4,%edx
mov    %rbx,%rcx
call   1060 <fread@plt>
cmp    $0x4,%eax
jne    11c8 <NESVerifyROM+0x68>
lea    0xe54(%rip),%rdi
mov    %r14,%rsi
mov    $0x4,%edx
call   1040 <strncmp@plt>
xor    %ebp,%ebp
test   %rax,%rax
sete   %bpl
jmp    11d4 <NESVerifyROM+0x74>
xor    %ebp,%ebp
jmp    11dc <NESVerifyROM+0x7c>
lea    0xe36(%rip),%rdi
call   1050 <puts@plt>
mov    %r14,%rdi
call   1030 <free@plt>
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@SEEK_SET = global i32 0
@.str = [15 x i8] c"ERROR READING\0A\00"
@.str.1 = [5 x i8] c"NES\1A\00"
define i32 @NESVerifyROM(i32* %0) {
%2 = alloca i32
%3 = alloca i32*
%4 = alloca i8*
store i32* %0, i32** %3
%5 = load i32*, i32** %3
%6 = icmp ne i32* %5, null
br i1 %6, label %8, label %7
7: ; preds = %1
store i32 0, i32* %2
br label %32
8: ; preds = %1
%9 = load i32*, i32** %3
%10 = load i32, i32* @SEEK_SET
%11 = call i32 @fseek(i32* %9, i32 0, i32 %10)
%12 = call i64 @malloc(i32 4)
%13 = inttoptr i64 %12 to i8*
store i8* %13, i8** %4
%14 = load i8*, i8** %4
%15 = load i32*, i32** %3
%16 = call i32 @fread(i8* %14, i32 1, i32 4, i32* %15)
%17 = icmp ne i32 %16, 4
br i1 %17, label %18, label %22
18: ; preds = %8
%19 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%20 = load i8*, i8** %4
%21 = call i32 @free(i8* %20)
store i32 0, i32* %2
br label %32
22: ; preds = %8
%23 = load i8*, i8** %4
%24 = call i64 @strncmp(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* %23, i32 4)
%25 = icmp ne i64 %24, 0
br i1 %25, label %26, label %29
26: ; preds = %22
%27 = load i8*, i8** %4
%28 = call i32 @free(i8* %27)
store i32 0, i32* %2
br label %32
29: ; preds = %22
%30 = load i8*, i8** %4
%31 = call i32 @free(i8* %30)
store i32 1, i32* %2
br label %32
32: ; preds = %29, %26, %18, %7
%33 = load i32, i32* %2
ret i32 %33
}

LLVM IR (-O2):
@SEEK_SET = global i32 0
@.str.1 = [5 x i8] c"NES\1A\00"
@str = [14 x i8] c"ERROR READING\00"
define i32 @NESVerifyROM(i32* %0) {
%2 = icmp eq i32* %0, null
br i1 %2, label %18, label %3
3: ; preds = %1
%4 = load i32, i32* @SEEK_SET
%5 = tail call i32 @fseek(i32* %0, i32 0, i32 %4)
%6 = tail call i64 @malloc(i32 4)
%7 = inttoptr i64 %6 to i8*
%8 = tail call i32 @fread(i8* %7, i32 1, i32 4, i32* %0)
%9 = icmp eq i32 %8, 4
br i1 %9, label %13, label %10
10: ; preds = %3
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str, i64 0, i64 0))
%12 = tail call i32 @free(i8* %7)
br label %18
13: ; preds = %3
%14 = tail call i64 @strncmp(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* %7, i32 4)
%15 = icmp eq i64 %14, 0
%16 = tail call i32 @free(i8* %7)
%17 = zext i1 %15 to i32
br label %18
18: ; preds = %13, %10, %1
%19 = phi i32 [ 0, %1 ], [ 0, %10 ], [ %17, %13 ]
ret i32 %19
}