#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_4__ TYPE_1__;

/* Type definitions */
struct TYPE_4__ {
  int /*<<< orphan*/ e_ident;
};
typedef TYPE_1__ Elf32_Ehdr;

/* Variables and functions */
int /*<<< orphan*/ ELFMAG;
int /*<<< orphan*/ SELFMAG;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ perror(char *);
int read(int, TYPE_1__ *, int);
int /*<<< orphan*/ stderr;
scalar_t__ strncmp(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
void lelf(int fd, Elf32_Ehdr *ehdr) {
  if (read(fd, ehdr, sizeof(*ehdr)) != sizeof(*ehdr)) {
    perror("read");
    exit(1);
  }
  if (strncmp(ehdr->e_ident, ELFMAG, SELFMAG)) {
    fprintf(stderr, "Not ELF\n");
    exit(1);
  }
}


x86 assembly (clang, -O0):
<lelf>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    $0x4,%edx
call   1040 <read@plt>
cltq
cmp    $0x4,%rax
je     1192 <lelf+0x42>
lea    0xe7d(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
mov    0x2e49(%rip),%rax
mov    (%rax),%esi
mov    0x2e28(%rip),%rax
mov    (%rax),%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
je     11d8 <lelf+0x88>
mov    0x2e18(%rip),%rax
mov    (%rax),%edi
lea    0xe3c(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lelf>:
push   %rbx
mov    %rsi,%rbx
mov    $0x4,%edx
call   1040 <read@plt>
cmp    $0x4,%eax
jne    1183 <lelf+0x33>
mov    (%rbx),%edi
mov    0x2e7c(%rip),%rax
mov    (%rax),%esi
mov    0x2e5b(%rip),%rax
mov    (%rax),%edx
call   1030 <strncmp@plt>
test   %rax,%rax
jne    1199 <lelf+0x49>
pop    %rbx
ret
lea    0xe76(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    0x2e38(%rip),%rax
mov    (%rax),%edi
lea    0xe5c(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
%struct.TYPE_4__ = type { i32 }
@.str = [5 x i8] c"read\00"
@ELFMAG = global i32 0
@SELFMAG = global i32 0
@stderr = global i32 0
@.str.1 = [9 x i8] c"Not ELF\0A\00"
define void @lelf(i32 %0, %struct.TYPE_4__* %1) {
%3 = alloca i32
%4 = alloca %struct.TYPE_4__*
store i32 %0, i32* %3
store %struct.TYPE_4__* %1, %struct.TYPE_4__** %4
%5 = load i32, i32* %3
%6 = load %struct.TYPE_4__*, %struct.TYPE_4__** %4
%7 = call i32 @read(i32 %5, %struct.TYPE_4__* %6, i32 4)
%8 = sext i32 %7 to i64
%9 = icmp ne i64 %8, 4
br i1 %9, label %10, label %13
10: ; preds = %2
%11 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%12 = call i32 @exit(i32 1)
unreachable
13: ; preds = %2
%14 = load %struct.TYPE_4__*, %struct.TYPE_4__** %4
%15 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %14, i32 0, i32 0
%16 = load i32, i32* %15
%17 = load i32, i32* @ELFMAG
%18 = load i32, i32* @SELFMAG
%19 = call i64 @strncmp(i32 %16, i32 %17, i32 %18)
%20 = icmp ne i64 %19, 0
br i1 %20, label %21, label %25
21: ; preds = %13
%22 = load i32, i32* @stderr
%23 = call i32 @fprintf(i32 %22, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
%24 = call i32 @exit(i32 1)
unreachable
25: ; preds = %13
ret void
}

LLVM IR (-O2):
%struct.TYPE_4__ = type { i32 }
@.str = [5 x i8] c"read\00"
@ELFMAG = global i32 0
@SELFMAG = global i32 0
@stderr = global i32 0
@.str.1 = [9 x i8] c"Not ELF\0A\00"
define void @lelf(i32 %0, %struct.TYPE_4__* nocapture %1) {
%3 = tail call i32 @read(i32 %0, %struct.TYPE_4__* %1, i32 4)
%4 = icmp eq i32 %3, 4
br i1 %4, label %8, label %5
5: ; preds = %2
%6 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%7 = tail call i32 @exit(i32 1)
unreachable
8: ; preds = %2
%9 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %1, i64 0, i32 0
%10 = load i32, i32* %9
%11 = load i32, i32* @ELFMAG
%12 = load i32, i32* @SELFMAG
%13 = tail call i64 @strncmp(i32 %10, i32 %11, i32 %12)
%14 = icmp eq i64 %13, 0
br i1 %14, label %19, label %15
15: ; preds = %8
%16 = load i32, i32* @stderr
%17 = tail call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
%18 = tail call i32 @exit(i32 1)
unreachable
19: ; preds = %8
ret void
}