#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 */
unsigned char ELFMAG0;
unsigned char ELFMAG1;
unsigned char ELFMAG2;
unsigned char ELFMAG3;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
size_t fread(unsigned char **, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
int get_image_type(FILE *fp) {
  unsigned char sig[4];
  size_t size;
  size = fread(&sig, 1, 4, fp);
  if (size != 4) {
    fprintf(stderr, "[-] Error reading file header.\n");
    return -1;
  }
  if (sig[0] == 0x4D && sig[1] == 0x5A && sig[2] == 0x90 && sig[3] == 0x00) {
    return 1;
  } else if (sig[0] == ELFMAG0 && sig[1] == ELFMAG1 && sig[2] == ELFMAG2 &&
             sig[3] == ELFMAG3) {
    return 2;
  } else if (sig[0] == 0xEF && sig[1] == 0xBE && sig[2] == 0xAD &&
             sig[3] == 0xDE) {
    return 3;
  }
  return 0;
}


x86 assembly (clang, -O0):
<get_image_type>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
lea    -0x14(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
mov    $0x4,%edx
call   1030 <fread@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x4,-0x20(%rbp)
je     1173 <get_image_type+0x53>
mov    0x2e6f(%rip),%rax
mov    (%rax),%edi
lea    0xe9e(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1272 <get_image_type+0x152>
movzbl -0x14(%rbp),%eax
cmp    $0x4d,%eax
jne    11b5 <get_image_type+0x95>
movzbl -0x13(%rbp),%eax
cmp    $0x5a,%eax
jne    11b5 <get_image_type+0x95>
movzbl -0x12(%rbp),%eax
cmp    $0x90,%eax
jne    11b5 <get_image_type+0x95>
movzbl -0x11(%rbp),%eax
cmp    $0x0,%eax
jne    11b5 <get_image_type+0x95>
movl   $0x1,-0x4(%rbp)
jmp    1272 <get_image_type+0x152>
movzbl -0x14(%rbp),%eax
mov    0x2e18(%rip),%rcx
movzbl (%rcx),%ecx
cmp    %ecx,%eax
jne    1219 <get_image_type+0xf9>
movzbl -0x13(%rbp),%eax
mov    0x2e1a(%rip),%rcx
movzbl (%rcx),%ecx
cmp    %ecx,%eax
jne    1219 <get_image_type+0xf9>
movzbl -0x12(%rbp),%eax
mov    0x2dd4(%rip),%rcx
movzbl (%rcx),%ecx
cmp    %ecx,%eax
jne    1219 <get_image_type+0xf9>
movzbl -0x11(%rbp),%eax
mov    0x2df6(%rip),%rcx
movzbl (%rcx),%ecx
cmp    %ecx,%eax
jne    1219 <get_image_type+0xf9>
movl   $0x2,-0x4(%rbp)
jmp    1272 <get_image_type+0x152>
movzbl -0x14(%rbp),%eax
cmp    $0xef,%eax
jne    1261 <get_image_type+0x141>
movzbl -0x13(%rbp),%eax
cmp    $0xbe,%eax
jne    1261 <get_image_type+0x141>
movzbl -0x12(%rbp),%eax
cmp    $0xad,%eax
jne    1261 <get_image_type+0x141>
movzbl -0x11(%rbp),%eax
cmp    $0xde,%eax
jne    1261 <get_image_type+0x141>
movl   $0x3,-0x4(%rbp)
jmp    1272 <get_image_type+0x152>
jmp    1266 <get_image_type+0x146>
jmp    126b <get_image_type+0x14b>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_image_type>:
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rcx
lea    0xc(%rsp),%rdi
mov    $0x1,%ebx
mov    $0x1,%esi
mov    $0x4,%edx
call   1030 <fread@plt>
cmp    $0x4,%rax
jne    11ba <get_image_type+0x9a>
mov    0xc(%rsp),%cl
mov    0xd(%rsp),%dl
mov    0xe(%rsp),%al
mov    0xf(%rsp),%sil
cmp    $0x4d,%cl
jne    116b <get_image_type+0x4b>
cmp    $0x5a,%dl
jne    116b <get_image_type+0x4b>
cmp    $0x90,%al
jne    116b <get_image_type+0x4b>
test   %sil,%sil
je     11d4 <get_image_type+0xb4>
mov    0x2e66(%rip),%rdi
cmp    (%rdi),%cl
jne    119d <get_image_type+0x7d>
mov    0x2e73(%rip),%rdi
cmp    (%rdi),%dl
jne    119d <get_image_type+0x7d>
mov    0x2e38(%rip),%rdi
cmp    (%rdi),%al
jne    119d <get_image_type+0x7d>
mov    0x2e65(%rip),%rdi
mov    $0x2,%ebx
cmp    (%rdi),%sil
je     11d4 <get_image_type+0xb4>
xor    $0xef,%cl
xor    $0xbe,%dl
or     %cl,%dl
xor    $0xad,%al
or     %dl,%al
xor    $0xde,%sil
xor    %ecx,%ecx
or     %al,%sil
sete   %cl
lea    (%rcx,%rcx,2),%ebx
jmp    11d4 <get_image_type+0xb4>
mov    0x2e07(%rip),%rax
mov    (%rax),%edi
lea    0xe36(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [32 x i8] c"[-] Error reading file header.\0A\00"
@ELFMAG0 = global i8 0
@ELFMAG1 = global i8 0
@ELFMAG2 = global i8 0
@ELFMAG3 = global i8 0
define i32 @get_image_type(i32* %0) {
%2 = alloca i32
%3 = alloca i32*
%4 = alloca [4 x i8]
%5 = alloca i64
store i32* %0, i32** %3
%6 = bitcast [4 x i8]* %4 to i8**
%7 = load i32*, i32** %3
%8 = call i64 @fread(i8** %6, i32 1, i32 4, i32* %7)
store i64 %8, i64* %5
%9 = load i64, i64* %5
%10 = icmp ne i64 %9, 4
br i1 %10, label %11, label %14
11: ; preds = %1
%12 = load i32, i32* @stderr
%13 = call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %2
br label %88
14: ; preds = %1
%15 = getelementptr inbounds [4 x i8], [4 x i8]* %4, i64 0, i64 0
%16 = load i8, i8* %15
%17 = zext i8 %16 to i32
%18 = icmp eq i32 %17, 77
br i1 %18, label %19, label %35
19: ; preds = %14
%20 = getelementptr inbounds [4 x i8], [4 x i8]* %4, i64 0, i64 1
%21 = load i8, i8* %20
%22 = zext i8 %21 to i32
%23 = icmp eq i32 %22, 90
br i1 %23, label %24, label %35
24: ; preds = %19
%25 = getelementptr inbounds [4 x i8], [4 x i8]* %4, i64 0, i64 2
%26 = load i8, i8* %25
%27 = zext i8 %26 to i32
%28 = icmp eq i32 %27, 144
br i1 %28, label %29, label %35
29: ; preds = %24
%30 = getelementptr inbounds [4 x i8], [4 x i8]* %4, i64 0, i64 3
%31 = load i8, i8* %30
%32 = zext i8 %31 to i32
%33 = icmp eq i32 %32, 0
br i1 %33, label %34, label %35
34: ; preds = %29
store i32 1, i32* %2
br label %88
35: ; preds = %29, %24, %19, %14
%36 = getelementptr inbounds [4 x i8], [4 x i8]* %4, i64 0, i64 0
%37 = load i8, i8* %36
%38 = zext i8 %37 to i32
%39 = load i8, i8* @ELFMAG0
%40 = zext i8 %39 to i32
%41 = icmp eq i32 %38, %40
br i1 %41, label %42, label %64
42: ; preds = %35
%43 = getelementptr inbounds [4 x i8], [4 x i8]* %4, i64 0, i64 1
%44 = load i8, i8* %43
%45 = zext i8 %44 to i32
%46 = load i8, i8* @ELFMAG1
%47 = zext i8 %46 to i32
%48 = icmp eq i32 %45, %47
br i1 %48, label %49, label %64
49: ; preds = %42
%50 = getelementptr inbounds [4 x i8], [4 x i8]* %4, i64 0, i64 2
%51 = load i8, i8* %50
%52 = zext i8 %51 to i32
%53 = load i8, i8* @ELFMAG2
%54 = zext i8 %53 to i32
%55 = icmp eq i32 %52, %54
br i1 %55, label %56, label %64
56: ; preds = %49
%57 = getelementptr inbounds [4 x i8], [4 x i8]* %4, i64 0, i64 3
%58 = load i8, i8* %57
%59 = zext i8 %58 to i32
%60 = load i8, i8* @ELFMAG3
%61 = zext i8 %60 to i32
%62 = icmp eq i32 %59, %61
br i1 %62, label %63, label %64
63: ; preds = %56
store i32 2, i32* %2
br label %88
64: ; preds = %56, %49, %42, %35
%65 = getelementptr inbounds [4 x i8], [4 x i8]* %4, i64 0, i64 0
%66 = load i8, i8* %65
%67 = zext i8 %66 to i32
%68 = icmp eq i32 %67, 239
br i1 %68, label %69, label %85
69: ; preds = %64
%70 = getelementptr inbounds [4 x i8], [4 x i8]* %4, i64 0, i64 1
%71 = load i8, i8* %70
%72 = zext i8 %71 to i32
%73 = icmp eq i32 %72, 190
br i1 %73, label %74, label %85
74: ; preds = %69
%75 = getelementptr inbounds [4 x i8], [4 x i8]* %4, i64 0, i64 2
%76 = load i8, i8* %75
%77 = zext i8 %76 to i32
%78 = icmp eq i32 %77, 173
br i1 %78, label %79, label %85
79: ; preds = %74
%80 = getelementptr inbounds [4 x i8], [4 x i8]* %4, i64 0, i64 3
%81 = load i8, i8* %80
%82 = zext i8 %81 to i32
%83 = icmp eq i32 %82, 222
br i1 %83, label %84, label %85
84: ; preds = %79
store i32 3, i32* %2
br label %88
85: ; preds = %79, %74, %69, %64
br label %86
86: ; preds = %85
br label %87
87: ; preds = %86
store i32 0, i32* %2
br label %88
88: ; preds = %87, %84, %63, %34, %11
%89 = load i32, i32* %2
ret i32 %89
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [32 x i8] c"[-] Error reading file header.\0A\00"
@ELFMAG0 = global i8 0
@ELFMAG1 = global i8 0
@ELFMAG2 = global i8 0
@ELFMAG3 = global i8 0
define i32 @get_image_type(i32* nocapture %0) {
%2 = alloca [4 x i8]
%3 = getelementptr inbounds [4 x i8], [4 x i8]* %2, i64 0, i64 0
%4 = bitcast [4 x i8]* %2 to i8**
%5 = call i64 @fread(i8** %4, i32 1, i32 4, i32* %0)
%6 = icmp eq i64 %5, 4
br i1 %6, label %10, label %7
7: ; preds = %1
%8 = load i32, i32* @stderr
%9 = tail call i32 @fprintf(i32 %8, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
br label %46
10: ; preds = %1
%11 = load i8, i8* %3
%12 = icmp eq i8 %11, 77
%13 = getelementptr inbounds [4 x i8], [4 x i8]* %2, i64 0, i64 1
%14 = load i8, i8* %13
%15 = icmp eq i8 %14, 90
%16 = select i1 %12, i1 %15, i1 false
%17 = getelementptr inbounds [4 x i8], [4 x i8]* %2, i64 0, i64 2
%18 = load i8, i8* %17
%19 = icmp eq i8 %18, -112
%20 = select i1 %16, i1 %19, i1 false
%21 = getelementptr inbounds [4 x i8], [4 x i8]* %2, i64 0, i64 3
%22 = load i8, i8* %21
%23 = icmp eq i8 %22, 0
%24 = select i1 %20, i1 %23, i1 false
br i1 %24, label %46, label %25
25: ; preds = %10
%26 = load i8, i8* @ELFMAG0
%27 = icmp eq i8 %11, %26
%28 = load i8, i8* @ELFMAG1
%29 = icmp eq i8 %14, %28
%30 = select i1 %27, i1 %29, i1 false
%31 = load i8, i8* @ELFMAG2
%32 = icmp eq i8 %18, %31
%33 = select i1 %30, i1 %32, i1 false
%34 = load i8, i8* @ELFMAG3
%35 = icmp eq i8 %22, %34
%36 = select i1 %33, i1 %35, i1 false
br i1 %36, label %46, label %37
37: ; preds = %25
%38 = icmp eq i8 %11, -17
%39 = icmp eq i8 %14, -66
%40 = select i1 %38, i1 %39, i1 false
%41 = icmp eq i8 %18, -83
%42 = select i1 %40, i1 %41, i1 false
%43 = icmp eq i8 %22, -34
%44 = select i1 %42, i1 %43, i1 false
%45 = select i1 %44, i32 3, i32 0
br label %46
46: ; preds = %37, %25, %10, %7
%47 = phi i32 [ -1, %7 ], [ 1, %10 ], [ 2, %25 ], [ %45, %37 ]
ret i32 %47
}