#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 */
size_t EI_CLASS;
size_t EI_MAG0;
int EI_NIDENT;
char ELFCLASS64;
int /*<<< orphan*/ ELFMAG;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ SELFMAG;
int /*<<< orphan*/ close(int);
int handle_file32(char const *, int);
int handle_file64(char const *, int);
scalar_t__ memcmp(char *, int /*<<< orphan*/, int /*<<< orphan*/);
int open(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, char const *);
int read(int, char *, int);
int handle_file(const char *fname) {
  int fd = open(fname, O_RDONLY);
  if (fd == -1) {
    printf("cannot open %s: %m\n", fname);
    return 1;
  }
  char ident[EI_NIDENT];
  if (read(fd, ident, EI_NIDENT) != EI_NIDENT) {
    printf("%s: read error: %m\n", fname);
    close(fd);
    return 1;
  }
  if (memcmp(&ident[EI_MAG0], ELFMAG, SELFMAG) != 0) {
    printf("%s: not an ELF file\n", fname);
    close(fd);
    return 1;
  }
  int result;
  if (ident[EI_CLASS] == ELFCLASS64)
    result = handle_file64(fname, fd);
  else
    result = handle_file32(fname, fd);
  close(fd);
  return result;
}


x86 assembly (clang, -O0):
<handle_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e51(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11b7 <handle_file+0x47>
mov    -0x10(%rbp),%rsi
lea    0xe5a(%rip),%rdi
call   1030 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12eb <handle_file+0x17b>
mov    0x2e22(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x20(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rsi
sub    %rcx,%rsi
mov    %rsi,-0x38(%rbp)
mov    %rsi,%rsp
mov    %rax,-0x28(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2df3(%rip),%rax
mov    (%rax),%edx
call   1050 <read@plt>
mov    0x2de5(%rip),%rcx
cmp    (%rcx),%eax
je     122e <handle_file+0xbe>
mov    -0x10(%rbp),%rsi
lea    0xe06(%rip),%rdi
call   1030 <printf@plt>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
movl   $0x1,-0x4(%rbp)
movl   $0x1,-0x2c(%rbp)
jmp    12e4 <handle_file+0x174>
mov    -0x38(%rbp),%rdi
mov    0x2d97(%rip),%rax
add    (%rax),%rdi
mov    0x2da5(%rip),%rax
mov    (%rax),%esi
mov    0x2d64(%rip),%rax
mov    (%rax),%edx
call   1060 <memcmp@plt>
cmp    $0x0,%rax
je     1288 <handle_file+0x118>
mov    -0x10(%rbp),%rsi
lea    0xdc0(%rip),%rdi
call   1030 <printf@plt>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
movl   $0x1,-0x4(%rbp)
movl   $0x1,-0x2c(%rbp)
jmp    12e4 <handle_file+0x174>
mov    -0x38(%rbp),%rax
mov    0x2d25(%rip),%rcx
mov    (%rcx),%rcx
movsbl (%rax,%rcx,1),%eax
mov    0x2d27(%rip),%rcx
movsbl (%rcx),%ecx
cmp    %ecx,%eax
jne    12c0 <handle_file+0x150>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1070 <handle_file64@plt>
mov    %eax,-0x30(%rbp)
jmp    12cf <handle_file+0x15f>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1080 <handle_file32@plt>
mov    %eax,-0x30(%rbp)
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
mov    -0x30(%rbp),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x2c(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<handle_file>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r15
mov    0x2e50(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
cmp    $0xffffffff,%eax
je     11fd <handle_file+0x8d>
mov    %eax,%r14d
mov    %rsp,%r12
mov    0x2e3f(%rip),%r13
mov    0x0(%r13),%eax
mov    %rsp,%rbx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
mov    0x0(%r13),%edx
mov    %r14d,%edi
mov    %rbx,%rsi
call   1050 <read@plt>
cmp    0x0(%r13),%eax
jne    1213 <handle_file+0xa3>
mov    0x2dfe(%rip),%rax
mov    (%rax),%rdi
add    %rbx,%rdi
mov    0x2e09(%rip),%rax
mov    (%rax),%esi
mov    0x2dc8(%rip),%rax
mov    (%rax),%edx
call   1060 <memcmp@plt>
test   %rax,%rax
je     1243 <handle_file+0xd3>
lea    0xe2d(%rip),%rdi
jmp    121a <handle_file+0xaa>
lea    0xdfc(%rip),%rdi
mov    %r15,%rsi
call   1030 <printf@plt>
mov    $0x1,%ebx
jmp    1232 <handle_file+0xc2>
lea    0xdfa(%rip),%rdi
mov    %r15,%rsi
call   1030 <printf@plt>
mov    %r14d,%edi
call   1040 <close@plt>
mov    $0x1,%ebx
mov    %r12,%rsp
mov    %ebx,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d6e(%rip),%rax
mov    (%rax),%rax
mov    (%rbx,%rax,1),%al
mov    0x2d71(%rip),%rcx
cmp    (%rcx),%al
jne    1268 <handle_file+0xf8>
mov    %r15,%rdi
mov    %r14d,%esi
call   1070 <handle_file64@plt>
jmp    1273 <handle_file+0x103>
mov    %r15,%rdi
mov    %r14d,%esi
call   1080 <handle_file32@plt>
mov    %eax,%ebx
mov    %r14d,%edi
call   1040 <close@plt>
jmp    122f <handle_file+0xbf>

LLVM IR (-O0):
@O_RDONLY = global i32 0
@.str = [20 x i8] c"cannot open %s: %m\0A\00"
@EI_NIDENT = global i32 0
@.str.1 = [20 x i8] c"%s: read error: %m\0A\00"
@EI_MAG0 = global i64 0
@ELFMAG = global i32 0
@SELFMAG = global i32 0
@.str.2 = [21 x i8] c"%s: not an ELF file\0A\00"
@EI_CLASS = global i64 0
@ELFCLASS64 = global i8 0
define i32 @handle_file(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i32
%8 = alloca i32
store i8* %0, i8** %3
%9 = load i8*, i8** %3
%10 = load i32, i32* @O_RDONLY
%11 = call i32 @open(i8* %9, i32 %10)
store i32 %11, i32* %4
%12 = load i32, i32* %4
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %17
14: ; preds = %1
%15 = load i8*, i8** %3
%16 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %15)
store i32 1, i32* %2
br label %66
17: ; preds = %1
%18 = load i32, i32* @EI_NIDENT
%19 = zext i32 %18 to i64
%20 = call i8* @llvm.stacksave()
store i8* %20, i8** %5
%21 = alloca i8, i64 %19
store i64 %19, i64* %6
%22 = load i32, i32* %4
%23 = load i32, i32* @EI_NIDENT
%24 = call i32 @read(i32 %22, i8* %21, i32 %23)
%25 = load i32, i32* @EI_NIDENT
%26 = icmp ne i32 %24, %25
br i1 %26, label %27, label %32
27: ; preds = %17
%28 = load i8*, i8** %3
%29 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i8* %28)
%30 = load i32, i32* %4
%31 = call i32 @close(i32 %30)
store i32 1, i32* %2
store i32 1, i32* %7
br label %64
32: ; preds = %17
%33 = load i64, i64* @EI_MAG0
%34 = getelementptr inbounds i8, i8* %21, i64 %33
%35 = load i32, i32* @ELFMAG
%36 = load i32, i32* @SELFMAG
%37 = call i64 @memcmp(i8* %34, i32 %35, i32 %36)
%38 = icmp ne i64 %37, 0
br i1 %38, label %39, label %44
39: ; preds = %32
%40 = load i8*, i8** %3
%41 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i8* %40)
%42 = load i32, i32* %4
%43 = call i32 @close(i32 %42)
store i32 1, i32* %2
store i32 1, i32* %7
br label %64
44: ; preds = %32
%45 = load i64, i64* @EI_CLASS
%46 = getelementptr inbounds i8, i8* %21, i64 %45
%47 = load i8, i8* %46
%48 = sext i8 %47 to i32
%49 = load i8, i8* @ELFCLASS64
%50 = sext i8 %49 to i32
%51 = icmp eq i32 %48, %50
br i1 %51, label %52, label %56
52: ; preds = %44
%53 = load i8*, i8** %3
%54 = load i32, i32* %4
%55 = call i32 @handle_file64(i8* %53, i32 %54)
store i32 %55, i32* %8
br label %60
56: ; preds = %44
%57 = load i8*, i8** %3
%58 = load i32, i32* %4
%59 = call i32 @handle_file32(i8* %57, i32 %58)
store i32 %59, i32* %8
br label %60
60: ; preds = %56, %52
%61 = load i32, i32* %4
%62 = call i32 @close(i32 %61)
%63 = load i32, i32* %8
store i32 %63, i32* %2
store i32 1, i32* %7
br label %64
64: ; preds = %60, %39, %27
%65 = load i8*, i8** %5
br label %66
66: ; preds = %64, %14
%67 = load i32, i32* %2
ret i32 %67
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@.str = [20 x i8] c"cannot open %s: %m\0A\00"
@EI_NIDENT = global i32 0
@.str.1 = [20 x i8] c"%s: read error: %m\0A\00"
@EI_MAG0 = global i64 0
@ELFMAG = global i32 0
@SELFMAG = global i32 0
@.str.2 = [21 x i8] c"%s: not an ELF file\0A\00"
@EI_CLASS = global i64 0
@ELFCLASS64 = global i8 0
define i32 @handle_file(i8* %0) {
%2 = load i32, i32* @O_RDONLY
%3 = tail call i32 @open(i8* %0, i32 %2)
%4 = icmp eq i32 %3, -1
br i1 %4, label %5, label %7
5: ; preds = %1
%6 = tail call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %44
7: ; preds = %1
%8 = load i32, i32* @EI_NIDENT
%9 = zext i32 %8 to i64
%10 = tail call i8* @llvm.stacksave()
%11 = alloca i8, i64 %9
%12 = load i32, i32* @EI_NIDENT
%13 = call i32 @read(i32 %3, i8* %11, i32 %12)
%14 = load i32, i32* @EI_NIDENT
%15 = icmp eq i32 %13, %14
br i1 %15, label %19, label %16
16: ; preds = %7
%17 = tail call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%18 = tail call i32 @close(i32 %3)
br label %42
19: ; preds = %7
%20 = load i64, i64* @EI_MAG0
%21 = getelementptr inbounds i8, i8* %11, i64 %20
%22 = load i32, i32* @ELFMAG
%23 = load i32, i32* @SELFMAG
%24 = call i64 @memcmp(i8* %21, i32 %22, i32 %23)
%25 = icmp eq i64 %24, 0
br i1 %25, label %29, label %26
26: ; preds = %19
%27 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i8* %0)
%28 = call i32 @close(i32 %3)
br label %42
29: ; preds = %19
%30 = load i64, i64* @EI_CLASS
%31 = getelementptr inbounds i8, i8* %11, i64 %30
%32 = load i8, i8* %31
%33 = load i8, i8* @ELFCLASS64
%34 = icmp eq i8 %32, %33
br i1 %34, label %35, label %37
35: ; preds = %29
%36 = call i32 @handle_file64(i8* %0, i32 %3)
br label %39
37: ; preds = %29
%38 = call i32 @handle_file32(i8* %0, i32 %3)
br label %39
39: ; preds = %37, %35
%40 = phi i32 [ %36, %35 ], [ %38, %37 ]
%41 = call i32 @close(i32 %3)
br label %42
42: ; preds = %39, %26, %16
%43 = phi i32 [ 1, %16 ], [ 1, %26 ], [ %40, %39 ]
br label %44
44: ; preds = %42, %5
%45 = phi i32 [ 1, %5 ], [ %43, %42 ]
ret i32 %45
}