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

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ read(int, char *, int);
int main(int ac, const char *av[]) {
  char data[32] = {0};
  int fd = -1;
  fd = open("/etc/passwd", O_RDONLY);
  if (fd < 0)
    return -1;
  read(fd, data, sizeof(data));
  if (*(unsigned int *)data == 0x464c457f)
    printf("[+] ELF binary\n");
  else
    printf("[+] Not an ELF binary\n");
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1040 <memset@plt>
movl   $0xffffffff,-0x34(%rbp)
mov    0x2e64(%rip),%rax
mov    (%rax),%esi
lea    0xe73(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jge    11ab <main+0x5b>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f5 <main+0xa5>
mov    -0x34(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0x20,%edx
call   1060 <read@plt>
cmpl   $0x464c457f,-0x30(%rbp)
jne    11da <main+0x8a>
lea    0xe3c(%rip),%rdi
call   1030 <printf@plt>
jmp    11e6 <main+0x96>
lea    0xe3b(%rip),%rdi
call   1030 <printf@plt>
mov    -0x34(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x20,%rsp
xorps  %xmm0,%xmm0
movaps %xmm0,0x10(%rsp)
movaps %xmm0,(%rsp)
mov    0x2e90(%rip),%rax
mov    (%rax),%esi
lea    0xe9f(%rip),%rdi
call   1060 <open@plt>
test   %eax,%eax
js     11a4 <main+0x64>
mov    %eax,%ebx
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0x20,%edx
call   1050 <read@plt>
cmpl   $0x464c457f,(%rsp)
lea    0xe99(%rip),%rax
lea    0xe7c(%rip),%rdi
cmove  %rax,%rdi
call   1030 <puts@plt>
mov    %ebx,%edi
call   1040 <close@plt>
xor    %eax,%eax
jmp    11a9 <main+0x69>
mov    $0xffffffff,%eax
add    $0x20,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [12 x i8] c"/etc/passwd\00"
@O_RDONLY = global i32 0
@.str.1 = [16 x i8] c"[+] ELF binary\0A\00"
@.str.2 = [23 x i8] c"[+] Not an ELF binary\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [32 x i8]
%7 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = bitcast [32 x i8]* %6 to i8*
store i32 -1, i32* %7
%9 = load i32, i32* @O_RDONLY
%10 = call i32 @open(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %9)
store i32 %10, i32* %7
%11 = load i32, i32* %7
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %14
13: ; preds = %2
store i32 -1, i32* %3
br label %29
14: ; preds = %2
%15 = load i32, i32* %7
%16 = getelementptr inbounds [32 x i8], [32 x i8]* %6, i64 0, i64 0
%17 = call i32 @read(i32 %15, i8* %16, i32 32)
%18 = getelementptr inbounds [32 x i8], [32 x i8]* %6, i64 0, i64 0
%19 = bitcast i8* %18 to i32*
%20 = load i32, i32* %19
%21 = icmp eq i32 %20, 1179403647
br i1 %21, label %22, label %24
22: ; preds = %14
%23 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
br label %26
24: ; preds = %14
%25 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
br label %26
26: ; preds = %24, %22
%27 = load i32, i32* %7
%28 = call i32 @close(i32 %27)
store i32 0, i32* %3
br label %29
29: ; preds = %26, %13
%30 = load i32, i32* %3
ret i32 %30
}

LLVM IR (-O2):
@.str = [12 x i8] c"/etc/passwd\00"
@O_RDONLY = global i32 0
@str = [22 x i8] c"[+] Not an ELF binary\00"
@str.3 = [15 x i8] c"[+] ELF binary\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [32 x i8]
%4 = getelementptr inbounds [32 x i8], [32 x i8]* %3, i64 0, i64 0
%5 = load i32, i32* @O_RDONLY
%6 = tail call i32 @open(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %5)
%7 = icmp slt i32 %6, 0
br i1 %7, label %16, label %8
8: ; preds = %2
%9 = call i32 @read(i32 %6, i8* %4, i32 32)
%10 = bitcast [32 x i8]* %3 to i32*
%11 = load i32, i32* %10
%12 = icmp eq i32 %11, 1179403647
%13 = select i1 %12, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @str.3, i64 0, i64 0), i8* getelementptr inbounds ([22 x i8], [22 x i8]* @str, i64 0, i64 0)
%14 = tail call i32 @puts(i8* dereferenceable(1) %13)
%15 = tail call i32 @close(i32 %6)
br label %16
16: ; preds = %2, %8
%17 = phi i32 [ 0, %8 ], [ -1, %2 ]
ret i32 %17
}