#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 */
typedef struct TYPE_3__ TYPE_1__;

/* Type definitions */
struct TYPE_3__ {
  scalar_t__ e_type;
  scalar_t__ e_machine;
  int /*<<< orphan*/ e_ident;
};
typedef TYPE_1__ Elf32_Ehdr;

/* Variables and functions */
scalar_t__ EM_386;
scalar_t__ ET_EXEC;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ read(int, TYPE_1__ *, int);
scalar_t__ strncmp(int /*<<< orphan*/, char *, int);
int check_if_elf(char *name) {
  Elf32_Ehdr elf;
  char belf[4] = {'\x7f', 'E', 'L', 'F'};
  int fd;
  if ((fd = open(name, O_RDONLY)) == -1)
    return -1;
  read(fd, &elf, sizeof(Elf32_Ehdr));
  close(fd);
  if ((strncmp(elf.e_ident, belf, 4)) != 0)
    return -1;
  if (elf.e_type != ET_EXEC)
    return -1;
  if (elf.e_machine != EM_386)
    return -1;
  return 0;
}


x86 assembly (clang, -O0):
<check_if_elf>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    0xeae(%rip),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e88(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x30(%rbp)
cmp    $0xffffffff,%eax
jne    117f <check_if_elf+0x3f>
movl   $0xffffffff,-0x4(%rbp)
jmp    1206 <check_if_elf+0xc6>
mov    -0x30(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0x18,%edx
call   1050 <read@plt>
mov    -0x30(%rbp),%edi
call   1040 <close@plt>
mov    -0x18(%rbp),%edi
lea    -0x2c(%rbp),%rsi
mov    $0x4,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
je     11bf <check_if_elf+0x7f>
movl   $0xffffffff,-0x4(%rbp)
jmp    1206 <check_if_elf+0xc6>
mov    -0x28(%rbp),%rax
mov    0x2e0e(%rip),%rcx
cmp    (%rcx),%rax
je     11df <check_if_elf+0x9f>
movl   $0xffffffff,-0x4(%rbp)
jmp    1206 <check_if_elf+0xc6>
mov    -0x20(%rbp),%rax
mov    0x2dde(%rip),%rcx
cmp    (%rcx),%rax
je     11ff <check_if_elf+0xbf>
movl   $0xffffffff,-0x4(%rbp)
jmp    1206 <check_if_elf+0xc6>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<check_if_elf>:
push   %rbp
push   %rbx
sub    $0x28,%rsp
movl   $0x464c457f,0xc(%rsp)
46
mov    0x2e93(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    $0xffffffff,%ebx
cmp    $0xffffffff,%eax
je     11bf <check_if_elf+0x7f>
mov    %eax,%ebp
lea    0x10(%rsp),%rsi
mov    %eax,%edi
mov    $0x18,%edx
call   1050 <read@plt>
mov    %ebp,%edi
call   1040 <close@plt>
mov    0x20(%rsp),%edi
lea    0xc(%rsp),%rsi
mov    $0x4,%edx
call   1030 <strncmp@plt>
test   %rax,%rax
jne    11bf <check_if_elf+0x7f>
mov    0x10(%rsp),%rax
mov    0x2e34(%rip),%rcx
cmp    (%rcx),%rax
jne    11bf <check_if_elf+0x7f>
mov    0x18(%rsp),%rax
mov    0x2e13(%rip),%rcx
xor    %ebx,%ebx
cmp    (%rcx),%rax
setne  %bl
neg    %ebx
mov    %ebx,%eax
add    $0x28,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.TYPE_3__ = type { i64, i64, i32 }
@__const.check_if_elf.belf = [4 x i8] c"\7FELF"
@O_RDONLY = global i32 0
@ET_EXEC = global i64 0
@EM_386 = global i64 0
define i32 @check_if_elf(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca %struct.TYPE_3__
%5 = alloca [4 x i8]
%6 = alloca i32
store i8* %0, i8** %3
%7 = bitcast [4 x i8]* %5 to i8*
%8 = load i8*, i8** %3
%9 = load i32, i32* @O_RDONLY
%10 = call i32 @open(i8* %8, i32 %9)
store i32 %10, i32* %6
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %13
12: ; preds = %1
store i32 -1, i32* %2
br label %37
13: ; preds = %1
%14 = load i32, i32* %6
%15 = call i32 @read(i32 %14, %struct.TYPE_3__* %4, i32 24)
%16 = load i32, i32* %6
%17 = call i32 @close(i32 %16)
%18 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %4, i32 0, i32 2
%19 = load i32, i32* %18
%20 = getelementptr inbounds [4 x i8], [4 x i8]* %5, i64 0, i64 0
%21 = call i64 @strncmp(i32 %19, i8* %20, i32 4)
%22 = icmp ne i64 %21, 0
br i1 %22, label %23, label %24
23: ; preds = %13
store i32 -1, i32* %2
br label %37
24: ; preds = %13
%25 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %4, i32 0, i32 0
%26 = load i64, i64* %25
%27 = load i64, i64* @ET_EXEC
%28 = icmp ne i64 %26, %27
br i1 %28, label %29, label %30
29: ; preds = %24
store i32 -1, i32* %2
br label %37
30: ; preds = %24
%31 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %4, i32 0, i32 1
%32 = load i64, i64* %31
%33 = load i64, i64* @EM_386
%34 = icmp ne i64 %32, %33
br i1 %34, label %35, label %36
35: ; preds = %30
store i32 -1, i32* %2
br label %37
36: ; preds = %30
store i32 0, i32* %2
br label %37
37: ; preds = %36, %35, %29, %23, %12
%38 = load i32, i32* %2
ret i32 %38
}

LLVM IR (-O2):
%struct.TYPE_3__ = type { i64, i64, i32 }
@O_RDONLY = global i32 0
@ET_EXEC = global i64 0
@EM_386 = global i64 0
define i32 @check_if_elf(i8* nocapture %0) {
%2 = alloca %struct.TYPE_3__
%3 = alloca i32
%4 = bitcast %struct.TYPE_3__* %2 to i8*
%5 = bitcast i32* %3 to i8*
store i32 1179403647, i32* %3
%6 = load i32, i32* @O_RDONLY
%7 = tail call i32 @open(i8* %0, i32 %6)
%8 = icmp eq i32 %7, -1
br i1 %8, label %27, label %9
9: ; preds = %1
%10 = call i32 @read(i32 %7, %struct.TYPE_3__* %2, i32 24)
%11 = tail call i32 @close(i32 %7)
%12 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %2, i64 0, i32 2
%13 = load i32, i32* %12
%14 = call i64 @strncmp(i32 %13, i8* %5, i32 4)
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %27
16: ; preds = %9
%17 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %2, i64 0, i32 0
%18 = load i64, i64* %17
%19 = load i64, i64* @ET_EXEC
%20 = icmp eq i64 %18, %19
br i1 %20, label %21, label %27
21: ; preds = %16
%22 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %2, i64 0, i32 1
%23 = load i64, i64* %22
%24 = load i64, i64* @EM_386
%25 = icmp ne i64 %23, %24
%26 = sext i1 %25 to i32
br label %27
27: ; preds = %21, %16, %9, %1
%28 = phi i32 [ -1, %1 ], [ -1, %9 ], [ -1, %16 ], [ %26, %21 ]
ret i32 %28
}