#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 */

/* Variables and functions */
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int read(int, unsigned char *, int);
int /*<<< orphan*/ stderr;
void check_magic_num(int fd) {
  unsigned char magic_num[4] = {0x4C, 0x70, 0xF0, 0x7C};
  unsigned char ptr[1] = {0};
  int i = 0, ret = 0;
  for (i = 0; i < 4; i++) {
    if ((ret = read(fd, ptr, sizeof(char))) != 1) {
      fprintf(stderr, "Failure to read magic number.\n");
      exit(1);
    }
    if (*ptr != magic_num[i]) {
      fprintf(stderr,
              "Bad magic number in file. 0x%x does not match required 0x%x at "
              "byte %d\n",
              *ptr, magic_num[i], i);
      exit(1);
    }
  }
  return;
}


x86 assembly (clang, -O0):
<check_magic_num>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    0xeaf(%rip),%eax
mov    %eax,-0x8(%rbp)
lea    -0x9(%rbp),%rdi
xor    %esi,%esi
mov    $0x1,%edx
call   1030 <memset@plt>
movl   $0x0,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0x0,-0x10(%rbp)
cmpl   $0x4,-0x10(%rbp)
jge    121b <check_magic_num+0xdb>
mov    -0x4(%rbp),%edi
lea    -0x9(%rbp),%rsi
mov    $0x1,%edx
call   1040 <read@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x1,%eax
je     11c1 <check_magic_num+0x81>
mov    0x2e39(%rip),%rax
mov    (%rax),%edi
lea    0xe54(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
movzbl -0x9(%rbp),%eax
movslq -0x10(%rbp),%rcx
movzbl -0x8(%rbp,%rcx,1),%ecx
cmp    %ecx,%eax
je     1208 <check_magic_num+0xc8>
mov    0x2e03(%rip),%rax
mov    (%rax),%edi
movzbl -0x9(%rbp),%edx
movslq -0x10(%rbp),%rax
movzbl -0x8(%rbp,%rax,1),%ecx
mov    -0x10(%rbp),%r8d
lea    0xe2c(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
jmp    120d <check_magic_num+0xcd>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    1179 <check_magic_num+0x39>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<check_magic_num>:
push   %rbp
push   %rbx
push   %rax
mov    %edi,%ebx
movb   $0x0,0x7(%rsp)
lea    0x7(%rsp),%rsi
mov    $0x1,%edx
call   1030 <read@plt>
cmp    $0x1,%eax
jne    11c0 <check_magic_num+0x90>
mov    0x7(%rsp),%al
cmp    $0x4c,%al
jne    11e1 <check_magic_num+0xb1>
lea    0x7(%rsp),%rsi
mov    $0x1,%ebp
mov    %ebx,%edi
mov    $0x1,%edx
call   1030 <read@plt>
cmp    $0x1,%eax
jne    11c0 <check_magic_num+0x90>
mov    0x7(%rsp),%al
cmp    $0x70,%al
jne    11ea <check_magic_num+0xba>
lea    0x7(%rsp),%rsi
mov    %ebx,%edi
mov    $0x1,%edx
call   1030 <read@plt>
cmp    $0x1,%eax
jne    11c0 <check_magic_num+0x90>
mov    0x7(%rsp),%al
cmp    $0xf0,%al
jne    11f1 <check_magic_num+0xc1>
lea    0x7(%rsp),%rsi
mov    %ebx,%edi
mov    $0x1,%edx
call   1030 <read@plt>
cmp    $0x1,%eax
jne    11c0 <check_magic_num+0x90>
mov    0x7(%rsp),%al
cmp    $0x7c,%al
jne    11fd <check_magic_num+0xcd>
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
mov    0x2e19(%rip),%rax
mov    (%rax),%edi
lea    0xe30(%rip),%rsi
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1050 <exit@plt>
xor    %ebp,%ebp
mov    $0x4c,%ecx
jmp    1207 <check_magic_num+0xd7>
mov    $0x70,%ecx
jmp    1207 <check_magic_num+0xd7>
mov    $0x2,%ebp
mov    $0xf0,%ecx
jmp    1207 <check_magic_num+0xd7>
mov    $0x3,%ebp
mov    $0x7c,%ecx
movzbl %al,%edx
mov    0x2dcf(%rip),%rax
mov    (%rax),%edi
lea    0xe05(%rip),%rsi
mov    %ebp,%r8d
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1050 <exit@plt>

LLVM IR (-O0):
@__const.check_magic_num.magic_num = [4 x i8] c"Lp\F0|"
@stderr = global i32 0
@.str = [31 x i8] c"Failure to read magic number.\0A\00"
@.str.1 = [72 x i8] c"Bad magic number in file. 0x%x does not match required 0x%x at byte %d\0A\00"
define void @check_magic_num(i32 %0) {
%2 = alloca i32
%3 = alloca [4 x i8]
%4 = alloca [1 x i8]
%5 = alloca i32
%6 = alloca i32
store i32 %0, i32* %2
%7 = bitcast [4 x i8]* %3 to i8*
%8 = bitcast [1 x i8]* %4 to i8*
store i32 0, i32* %5
store i32 0, i32* %6
store i32 0, i32* %5
br label %9
9: ; preds = %45, %1
%10 = load i32, i32* %5
%11 = icmp slt i32 %10, 4
br i1 %11, label %12, label %48
12: ; preds = %9
%13 = load i32, i32* %2
%14 = getelementptr inbounds [1 x i8], [1 x i8]* %4, i64 0, i64 0
%15 = call i32 @read(i32 %13, i8* %14, i32 1)
store i32 %15, i32* %6
%16 = icmp ne i32 %15, 1
br i1 %16, label %17, label %21
17: ; preds = %12
%18 = load i32, i32* @stderr
%19 = call i32 (i32, i8*, ...) @fprintf(i32 %18, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0))
%20 = call i32 @exit(i32 1)
unreachable
21: ; preds = %12
%22 = getelementptr inbounds [1 x i8], [1 x i8]* %4, i64 0, i64 0
%23 = load i8, i8* %22
%24 = zext i8 %23 to i32
%25 = load i32, i32* %5
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds [4 x i8], [4 x i8]* %3, i64 0, i64 %26
%28 = load i8, i8* %27
%29 = zext i8 %28 to i32
%30 = icmp ne i32 %24, %29
br i1 %30, label %31, label %44
31: ; preds = %21
%32 = load i32, i32* @stderr
%33 = getelementptr inbounds [1 x i8], [1 x i8]* %4, i64 0, i64 0
%34 = load i8, i8* %33
%35 = zext i8 %34 to i32
%36 = load i32, i32* %5
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds [4 x i8], [4 x i8]* %3, i64 0, i64 %37
%39 = load i8, i8* %38
%40 = zext i8 %39 to i32
%41 = load i32, i32* %5
%42 = call i32 (i32, i8*, ...) @fprintf(i32 %32, i8* getelementptr inbounds ([72 x i8], [72 x i8]* @.str.1, i64 0, i64 0), i32 %35, i32 %40, i32 %41)
%43 = call i32 @exit(i32 1)
unreachable
44: ; preds = %21
br label %45
45: ; preds = %44
%46 = load i32, i32* %5
%47 = add nsw i32 %46, 1
store i32 %47, i32* %5
br label %9
48: ; preds = %9
ret void
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [31 x i8] c"Failure to read magic number.\0A\00"
@.str.1 = [72 x i8] c"Bad magic number in file. 0x%x does not match required 0x%x at byte %d\0A\00"
define void @check_magic_num(i32 %0) {
%2 = alloca [1 x i8]
%3 = getelementptr inbounds [1 x i8], [1 x i8]* %2, i64 0, i64 0
store i8 0, i8* %3
%4 = call i32 @read(i32 %0, i8* %3, i32 1)
%5 = icmp eq i32 %4, 1
br i1 %5, label %10, label %6
6: ; preds = %33, %27, %21, %1
%7 = load i32, i32* @stderr
%8 = tail call i32 (i32, i8*, ...) @fprintf(i32 %7, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0))
%9 = tail call i32 @exit(i32 1)
unreachable
10: ; preds = %1
%11 = load i8, i8* %3
%12 = icmp eq i8 %11, 76
br i1 %12, label %21, label %13
13: ; preds = %36, %30, %24, %10
%14 = phi i8 [ %11, %10 ], [ %25, %24 ], [ %31, %30 ], [ %37, %36 ]
%15 = phi i32 [ 76, %10 ], [ 112, %24 ], [ 240, %30 ], [ 124, %36 ]
%16 = phi i32 [ 0, %10 ], [ 1, %24 ], [ 2, %30 ], [ 3, %36 ]
%17 = zext i8 %14 to i32
%18 = load i32, i32* @stderr
%19 = tail call i32 (i32, i8*, ...) @fprintf(i32 %18, i8* getelementptr inbounds ([72 x i8], [72 x i8]* @.str.1, i64 0, i64 0), i32 %17, i32 %15, i32 %16)
%20 = tail call i32 @exit(i32 1)
unreachable
21: ; preds = %10
%22 = call i32 @read(i32 %0, i8* %3, i32 1)
%23 = icmp eq i32 %22, 1
br i1 %23, label %24, label %6
24: ; preds = %21
%25 = load i8, i8* %3
%26 = icmp eq i8 %25, 112
br i1 %26, label %27, label %13
27: ; preds = %24
%28 = call i32 @read(i32 %0, i8* %3, i32 1)
%29 = icmp eq i32 %28, 1
br i1 %29, label %30, label %6
30: ; preds = %27
%31 = load i8, i8* %3
%32 = icmp eq i8 %31, -16
br i1 %32, label %33, label %13
33: ; preds = %30
%34 = call i32 @read(i32 %0, i8* %3, i32 1)
%35 = icmp eq i32 %34, 1
br i1 %35, label %36, label %6
36: ; preds = %33
%37 = load i8, i8* %3
%38 = icmp eq i8 %37, 124
br i1 %38, label %39, label %13
39: ; preds = %36
ret void
}