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

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int fread(int /*<<< orphan*/ *, int, int, int /*<<< orphan*/ *);
int read_file_signature(const char *filename, uint8_t sig[16]) {
  FILE *in = fopen(filename, "rb");
  if (!in)
    return 0;
  if (1 == fread(sig, 8, 1, in)) {
    fclose(in);
    return 1;
  } else {
    fclose(in);
    return 0;
  }
}


x86 assembly (clang, -O0):
<read_file_signature>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xeb5(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    116b <read_file_signature+0x3b>
movl   $0x0,-0x4(%rbp)
jmp    11b6 <read_file_signature+0x86>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rcx
mov    $0x8,%esi
mov    $0x1,%edx
call   1030 <fread@plt>
mov    %eax,%ecx
mov    $0x1,%eax
cmp    %ecx,%eax
jne    11a6 <read_file_signature+0x76>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x1,-0x4(%rbp)
jmp    11b6 <read_file_signature+0x86>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_file_signature>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%r14
lea    0xec2(%rip),%rsi
call   1050 <fopen@plt>
test   %rax,%rax
je     1173 <read_file_signature+0x43>
mov    %rax,%rbx
mov    %r14,%rdi
mov    $0x8,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   1030 <fread@plt>
xor    %ebp,%ebp
cmp    $0x1,%eax
sete   %bpl
mov    %rbx,%rdi
call   1040 <fclose@plt>
jmp    1175 <read_file_signature+0x45>
xor    %ebp,%ebp
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
define i32 @read_file_signature(i8* nocapture %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 %10, label %5
5: ; preds = %2
%6 = tail call i32 @fread(i32* %1, i32 8, i32 1, i32* %3)
%7 = icmp eq i32 %6, 1
%8 = tail call i32 @fclose(i32* %3)
%9 = zext i1 %7 to i32
br label %10
10: ; preds = %5, %2
%11 = phi i32 [ 0, %2 ], [ %9, %5 ]
ret i32 %11
}