#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 errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int fread(char *, int, int, int /*<<< orphan*/ *);
int ex_check_file_type(const char *path, int *type) {
  char magic[4];
  *type = 0;
  {
    FILE *fp;
    int i;
    if (!(fp = fopen(path, "r")))
      return errno;
    i = fread(magic, 4, 1, fp);
    fclose(fp);
    if (i != 1)
      return errno;
  }
  if (magic[1] == 'H' && magic[2] == 'D' && magic[3] == 'F')
    *type = 5;
  else if (magic[0] == 'C' && magic[1] == 'D' && magic[2] == 'F') {
    if (magic[3] == '\001')
      *type = 1;
    else if (magic[3] == '\002')
      *type = 2;
  }
  return 0;
}


x86 assembly (clang, -O0):
<ex_check_file_type>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rax
movl   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
lea    0xeab(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    1179 <ex_check_file_type+0x49>
mov    0x2e71(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    125d <ex_check_file_type+0x12d>
lea    -0x1c(%rbp),%rdi
mov    -0x28(%rbp),%rcx
mov    $0x4,%esi
mov    $0x1,%edx
call   1030 <fread@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
cmpl   $0x1,-0x2c(%rbp)
je     11b7 <ex_check_file_type+0x87>
mov    0x2e33(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    125d <ex_check_file_type+0x12d>
movsbl -0x1b(%rbp),%eax
cmp    $0x48,%eax
jne    11ed <ex_check_file_type+0xbd>
movsbl -0x1a(%rbp),%eax
cmp    $0x44,%eax
jne    11ed <ex_check_file_type+0xbd>
movsbl -0x19(%rbp),%eax
cmp    $0x46,%eax
jne    11ed <ex_check_file_type+0xbd>
mov    -0x18(%rbp),%rax
movl   $0x5,(%rax)
jmp    1256 <ex_check_file_type+0x126>
movsbl -0x1c(%rbp),%eax
cmp    $0x43,%eax
jne    1251 <ex_check_file_type+0x121>
movsbl -0x1b(%rbp),%eax
cmp    $0x44,%eax
jne    1251 <ex_check_file_type+0x121>
movsbl -0x1a(%rbp),%eax
cmp    $0x46,%eax
jne    1251 <ex_check_file_type+0x121>
movsbl -0x19(%rbp),%eax
cmp    $0x1,%eax
jne    1230 <ex_check_file_type+0x100>
mov    -0x18(%rbp),%rax
movl   $0x1,(%rax)
jmp    124c <ex_check_file_type+0x11c>
movsbl -0x19(%rbp),%eax
cmp    $0x2,%eax
jne    1247 <ex_check_file_type+0x117>
mov    -0x18(%rbp),%rax
movl   $0x2,(%rax)
jmp    124c <ex_check_file_type+0x11c>
jmp    1251 <ex_check_file_type+0x121>
jmp    1256 <ex_check_file_type+0x126>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ex_check_file_type>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
movl   $0x0,(%rsi)
lea    0xeb8(%rip),%rsi
call   1050 <fopen@plt>
test   %rax,%rax
je     11a1 <ex_check_file_type+0x71>
mov    %rax,%rbx
lea    0xc(%rsp),%rdi
mov    $0x4,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   1030 <fread@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1040 <fclose@plt>
cmp    $0x1,%ebp
jne    11a1 <ex_check_file_type+0x71>
mov    0xd(%rsp),%bl
mov    0xe(%rsp),%dl
mov    0xf(%rsp),%cl
cmp    $0x48,%bl
jne    11b3 <ex_check_file_type+0x83>
cmp    $0x44,%dl
jne    11b3 <ex_check_file_type+0x83>
cmp    $0x46,%cl
jne    11b3 <ex_check_file_type+0x83>
movl   $0x5,(%r14)
xor    %eax,%eax
jmp    11aa <ex_check_file_type+0x7a>
mov    0x2e38(%rip),%rax
mov    (%rax),%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
xor    %eax,%eax
cmpb   $0x43,0xc(%rsp)
jne    11aa <ex_check_file_type+0x7a>
cmp    $0x44,%bl
jne    11aa <ex_check_file_type+0x7a>
cmp    $0x46,%dl
jne    11aa <ex_check_file_type+0x7a>
cmp    $0x2,%cl
je     11d9 <ex_check_file_type+0xa9>
cmp    $0x1,%cl
jne    11aa <ex_check_file_type+0x7a>
movl   $0x1,(%r14)
jmp    11aa <ex_check_file_type+0x7a>
movl   $0x2,(%r14)
jmp    11aa <ex_check_file_type+0x7a>

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@errno = global i32 0
define i32 @ex_check_file_type(i8* %0, i32* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca [4 x i8]
%7 = alloca i32*
%8 = alloca i32
store i8* %0, i8** %4
store i32* %1, i32** %5
%9 = load i32*, i32** %5
store i32 0, i32* %9
%10 = load i8*, i8** %4
%11 = call i32* @fopen(i8* %10, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %11, i32** %7
%12 = icmp ne i32* %11, null
br i1 %12, label %15, label %13
13: ; preds = %2
%14 = load i32, i32* @errno
store i32 %14, i32* %3
br label %75
15: ; preds = %2
%16 = getelementptr inbounds [4 x i8], [4 x i8]* %6, i64 0, i64 0
%17 = load i32*, i32** %7
%18 = call i32 @fread(i8* %16, i32 4, i32 1, i32* %17)
store i32 %18, i32* %8
%19 = load i32*, i32** %7
%20 = call i32 @fclose(i32* %19)
%21 = load i32, i32* %8
%22 = icmp ne i32 %21, 1
br i1 %22, label %23, label %25
23: ; preds = %15
%24 = load i32, i32* @errno
store i32 %24, i32* %3
br label %75
25: ; preds = %15
%26 = getelementptr inbounds [4 x i8], [4 x i8]* %6, i64 0, i64 1
%27 = load i8, i8* %26
%28 = sext i8 %27 to i32
%29 = icmp eq i32 %28, 72
br i1 %29, label %30, label %42
30: ; preds = %25
%31 = getelementptr inbounds [4 x i8], [4 x i8]* %6, i64 0, i64 2
%32 = load i8, i8* %31
%33 = sext i8 %32 to i32
%34 = icmp eq i32 %33, 68
br i1 %34, label %35, label %42
35: ; preds = %30
%36 = getelementptr inbounds [4 x i8], [4 x i8]* %6, i64 0, i64 3
%37 = load i8, i8* %36
%38 = sext i8 %37 to i32
%39 = icmp eq i32 %38, 70
br i1 %39, label %40, label %42
40: ; preds = %35
%41 = load i32*, i32** %5
store i32 5, i32* %41
br label %74
42: ; preds = %35, %30, %25
%43 = getelementptr inbounds [4 x i8], [4 x i8]* %6, i64 0, i64 0
%44 = load i8, i8* %43
%45 = sext i8 %44 to i32
%46 = icmp eq i32 %45, 67
br i1 %46, label %47, label %73
47: ; preds = %42
%48 = getelementptr inbounds [4 x i8], [4 x i8]* %6, i64 0, i64 1
%49 = load i8, i8* %48
%50 = sext i8 %49 to i32
%51 = icmp eq i32 %50, 68
br i1 %51, label %52, label %73
52: ; preds = %47
%53 = getelementptr inbounds [4 x i8], [4 x i8]* %6, i64 0, i64 2
%54 = load i8, i8* %53
%55 = sext i8 %54 to i32
%56 = icmp eq i32 %55, 70
br i1 %56, label %57, label %73
57: ; preds = %52
%58 = getelementptr inbounds [4 x i8], [4 x i8]* %6, i64 0, i64 3
%59 = load i8, i8* %58
%60 = sext i8 %59 to i32
%61 = icmp eq i32 %60, 1
br i1 %61, label %62, label %64
62: ; preds = %57
%63 = load i32*, i32** %5
store i32 1, i32* %63
br label %72
64: ; preds = %57
%65 = getelementptr inbounds [4 x i8], [4 x i8]* %6, i64 0, i64 3
%66 = load i8, i8* %65
%67 = sext i8 %66 to i32
%68 = icmp eq i32 %67, 2
br i1 %68, label %69, label %71
69: ; preds = %64
%70 = load i32*, i32** %5
store i32 2, i32* %70
br label %71
71: ; preds = %69, %64
br label %72
72: ; preds = %71, %62
br label %73
73: ; preds = %72, %52, %47, %42
br label %74
74: ; preds = %73, %40
store i32 0, i32* %3
br label %75
75: ; preds = %74, %23, %13
%76 = load i32, i32* %3
ret i32 %76
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@errno = global i32 0
define i32 @ex_check_file_type(i8* nocapture %0, i32* nocapture writeonly %1) {
%3 = alloca [4 x i8]
%4 = getelementptr inbounds [4 x i8], [4 x i8]* %3, i64 0, i64 0
store i32 0, i32* %1
%5 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %7, label %9
7: ; preds = %2
%8 = load i32, i32* @errno
br label %37
9: ; preds = %2
%10 = call i32 @fread(i8* %4, i32 4, i32 1, i32* %5)
%11 = tail call i32 @fclose(i32* %5)
%12 = icmp eq i32 %10, 1
%13 = load i32, i32* @errno
br i1 %12, label %14, label %37
14: ; preds = %9
%15 = getelementptr inbounds [4 x i8], [4 x i8]* %3, i64 0, i64 1
%16 = load i8, i8* %15
%17 = icmp eq i8 %16, 72
%18 = getelementptr inbounds [4 x i8], [4 x i8]* %3, i64 0, i64 2
%19 = load i8, i8* %18
%20 = icmp eq i8 %19, 68
%21 = select i1 %17, i1 %20, i1 false
%22 = getelementptr inbounds [4 x i8], [4 x i8]* %3, i64 0, i64 3
%23 = load i8, i8* %22
%24 = icmp eq i8 %23, 70
%25 = select i1 %21, i1 %24, i1 false
br i1 %25, label %26, label %27
26: ; preds = %14
store i32 5, i32* %1
br label %37
27: ; preds = %14
%28 = load i8, i8* %4
%29 = icmp eq i8 %28, 67
%30 = icmp eq i8 %16, 68
%31 = select i1 %29, i1 %30, i1 false
%32 = icmp eq i8 %19, 70
%33 = select i1 %31, i1 %32, i1 false
br i1 %33, label %34, label %37
34: ; preds = %27
switch i8 %23, label %37 [
i8 1, label %35
i8 2, label %36
]
35: ; preds = %34
store i32 1, i32* %1
br label %37
36: ; preds = %34
store i32 2, i32* %1
br label %37
37: ; preds = %7, %26, %35, %36, %27, %34, %9
%38 = phi i32 [ %13, %9 ], [ 0, %34 ], [ 0, %27 ], [ 0, %36 ], [ 0, %35 ], [ 0, %26 ], [ %8, %7 ]
ret i32 %38
}