#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;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ ewf_file_header_check_signature(int /*<<< orphan*/ *);
int /*<<< orphan*/ libewf_fatal_print(char *, char const *);
int open(char const *, int /*<<< orphan*/);
int read(int, int /*<<< orphan*/ *, int);
uint8_t libewf_check_file_signature(const char *filename) {
  uint8_t signature[8];
  int file_descriptor;
  if (filename == NULL) {
    return (0);
  }
  file_descriptor = open(filename, O_RDONLY);
  if (file_descriptor < 0) {
    libewf_fatal_print(
        "libewf_check_file_signature: unable to open file: %s: %m", filename);
  }
  if (read(file_descriptor, signature, 8) != 8) {
    libewf_fatal_print("libewf_check_file_signature: unable to read signature "
                       "from file: %s: %m",
                       filename);
  }
  close(file_descriptor);
  return (ewf_file_header_check_signature(signature));
}


x86 assembly (clang, -O0):
<libewf_check_file_signature>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1173 <libewf_check_file_signature+0x23>
movl   $0x0,-0x4(%rbp)
jmp    11e0 <libewf_check_file_signature+0x90>
mov    -0x10(%rbp),%rdi
mov    0x2e6a(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jge    11a2 <libewf_check_file_signature+0x52>
mov    -0x10(%rbp),%rsi
lea    0xe63(%rip),%rdi
call   1030 <libewf_fatal_print@plt>
mov    -0x34(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0x8,%edx
call   1050 <read@plt>
cmp    $0x8,%eax
je     11cc <libewf_check_file_signature+0x7c>
mov    -0x10(%rbp),%rsi
lea    0xe72(%rip),%rdi
call   1030 <libewf_fatal_print@plt>
mov    -0x34(%rbp),%edi
call   1040 <close@plt>
lea    -0x30(%rbp),%rdi
call   1070 <ewf_file_header_check_signature@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<libewf_check_file_signature>:
test   %rdi,%rdi
je     11ba <libewf_check_file_signature+0x6a>
push   %rbp
push   %rbx
sub    $0x28,%rsp
mov    %rdi,%rbx
mov    0x2e83(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    1181 <libewf_check_file_signature+0x31>
lea    0xe87(%rip),%rdi
mov    %rbx,%rsi
call   1030 <libewf_fatal_print@plt>
mov    %rsp,%rsi
mov    %ebp,%edi
mov    $0x8,%edx
call   1050 <read@plt>
cmp    $0x8,%eax
je     11a4 <libewf_check_file_signature+0x54>
lea    0xe9d(%rip),%rdi
mov    %rbx,%rsi
call   1030 <libewf_fatal_print@plt>
mov    %ebp,%edi
call   1040 <close@plt>
mov    %rsp,%rdi
call   1070 <ewf_file_header_check_signature@plt>
add    $0x28,%rsp
pop    %rbx
pop    %rbp
ret
xor    %eax,%eax
ret

LLVM IR (-O0):
@O_RDONLY = global i32 0
@.str = [57 x i8] c"libewf_check_file_signature: unable to open file: %s: %m\00"
@.str.1 = [72 x i8] c"libewf_check_file_signature: unable to read signature from file: %s: %m\00"
define i32 @libewf_check_file_signature(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca [8 x i32]
%5 = alloca i32
store i8* %0, i8** %3
%6 = load i8*, i8** %3
%7 = icmp eq i8* %6, null
br i1 %7, label %8, label %9
8: ; preds = %1
store i32 0, i32* %2
br label %31
9: ; preds = %1
%10 = load i8*, i8** %3
%11 = load i32, i32* @O_RDONLY
%12 = call i32 @open(i8* %10, i32 %11)
store i32 %12, i32* %5
%13 = load i32, i32* %5
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %9
%16 = load i8*, i8** %3
%17 = call i32 @libewf_fatal_print(i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str, i64 0, i64 0), i8* %16)
br label %18
18: ; preds = %15, %9
%19 = load i32, i32* %5
%20 = getelementptr inbounds [8 x i32], [8 x i32]* %4, i64 0, i64 0
%21 = call i32 @read(i32 %19, i32* %20, i32 8)
%22 = icmp ne i32 %21, 8
br i1 %22, label %23, label %26
23: ; preds = %18
%24 = load i8*, i8** %3
%25 = call i32 @libewf_fatal_print(i8* getelementptr inbounds ([72 x i8], [72 x i8]* @.str.1, i64 0, i64 0), i8* %24)
br label %26
26: ; preds = %23, %18
%27 = load i32, i32* %5
%28 = call i32 @close(i32 %27)
%29 = getelementptr inbounds [8 x i32], [8 x i32]* %4, i64 0, i64 0
%30 = call i32 @ewf_file_header_check_signature(i32* %29)
store i32 %30, i32* %2
br label %31
31: ; preds = %26, %8
%32 = load i32, i32* %2
ret i32 %32
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@.str = [57 x i8] c"libewf_check_file_signature: unable to open file: %s: %m\00"
@.str.1 = [72 x i8] c"libewf_check_file_signature: unable to read signature from file: %s: %m\00"
define i32 @libewf_check_file_signature(i8* %0) {
%2 = alloca [8 x i32]
%3 = bitcast [8 x i32]* %2 to i8*
%4 = icmp eq i8* %0, null
br i1 %4, label %20, label %5
5: ; preds = %1
%6 = load i32, i32* @O_RDONLY
%7 = tail call i32 @open(i8* %0, i32 %6)
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %5
%10 = tail call i32 @libewf_fatal_print(i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %11
11: ; preds = %9, %5
%12 = getelementptr inbounds [8 x i32], [8 x i32]* %2, i64 0, i64 0
%13 = call i32 @read(i32 %7, i32* %12, i32 8)
%14 = icmp eq i32 %13, 8
br i1 %14, label %17, label %15
15: ; preds = %11
%16 = tail call i32 @libewf_fatal_print(i8* getelementptr inbounds ([72 x i8], [72 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %17
17: ; preds = %15, %11
%18 = tail call i32 @close(i32 %7)
%19 = call i32 @ewf_file_header_check_signature(i32* %12)
br label %20
20: ; preds = %1, %17
%21 = phi i32 [ %19, %17 ], [ 0, %1 ]
ret i32 %21
}