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

/* Variables and functions */
int EINVAL;
int /*<<< orphan*/ SEEK_SET;
int errno;
int /*<<< orphan*/ free(int /*<<< orphan*/ *);
scalar_t__ lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
scalar_t__ malloc(int);
scalar_t__ read(int, int /*<<< orphan*/ *, int);
int read_header(int d, Elf64_Ehdr **header) {
  *header = (Elf64_Ehdr *)malloc(sizeof(Elf64_Ehdr));
  if (lseek(d, 0, SEEK_SET) < 0) {
    free(*header);
    return errno;
  }
  if (read(d, *header, sizeof(Elf64_Ehdr)) <= 0) {
    free(*header);
    return errno = EINVAL;
  }
  return 0;
}


x86 assembly (clang, -O0):
<read_header>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x4,%edi
call   1060 <malloc@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x8(%rbp),%edi
mov    0x2e6b(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1040 <lseek@plt>
cmp    $0x0,%rax
jge    119d <read_header+0x5d>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1030 <free@plt>
mov    0x2e4d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    11e8 <read_header+0xa8>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
mov    $0x4,%edx
call   1050 <read@plt>
cmp    $0x0,%rax
jg     11e1 <read_header+0xa1>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1030 <free@plt>
mov    0x2e02(%rip),%rax
mov    (%rax),%eax
mov    0x2e09(%rip),%rcx
mov    %eax,(%rcx)
mov    %eax,-0x4(%rbp)
jmp    11e8 <read_header+0xa8>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_header>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%rbx
mov    %edi,%ebp
mov    $0x4,%edi
call   1060 <malloc@plt>
mov    %rax,(%rbx)
mov    0x2e7b(%rip),%rax
mov    (%rax),%edx
xor    %r14d,%r14d
mov    %ebp,%edi
xor    %esi,%esi
call   1040 <lseek@plt>
mov    (%rbx),%rsi
test   %rax,%rax
js     11a2 <read_header+0x62>
mov    %ebp,%edi
mov    $0x4,%edx
call   1050 <read@plt>
test   %rax,%rax
jg     11b4 <read_header+0x74>
mov    (%rbx),%rdi
call   1030 <free@plt>
mov    0x2e3d(%rip),%rax
mov    (%rax),%r14d
mov    0x2e43(%rip),%rax
mov    %r14d,(%rax)
jmp    11b4 <read_header+0x74>
mov    %rsi,%rdi
call   1030 <free@plt>
mov    0x2e2f(%rip),%rax
mov    (%rax),%r14d
mov    %r14d,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@SEEK_SET = global i32 0
@errno = global i32 0
@EINVAL = global i32 0
define i32 @read_header(i32 %0, i32** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32**
store i32 %0, i32* %4
store i32** %1, i32*** %5
%6 = call i64 @malloc(i32 4)
%7 = inttoptr i64 %6 to i32*
%8 = load i32**, i32*** %5
store i32* %7, i32** %8
%9 = load i32, i32* %4
%10 = load i32, i32* @SEEK_SET
%11 = call i64 @lseek(i32 %9, i32 0, i32 %10)
%12 = icmp slt i64 %11, 0
br i1 %12, label %13, label %18
13: ; preds = %2
%14 = load i32**, i32*** %5
%15 = load i32*, i32** %14
%16 = call i32 @free(i32* %15)
%17 = load i32, i32* @errno
store i32 %17, i32* %3
br label %30
18: ; preds = %2
%19 = load i32, i32* %4
%20 = load i32**, i32*** %5
%21 = load i32*, i32** %20
%22 = call i64 @read(i32 %19, i32* %21, i32 4)
%23 = icmp sle i64 %22, 0
br i1 %23, label %24, label %29
24: ; preds = %18
%25 = load i32**, i32*** %5
%26 = load i32*, i32** %25
%27 = call i32 @free(i32* %26)
%28 = load i32, i32* @EINVAL
store i32 %28, i32* @errno
store i32 %28, i32* %3
br label %30
29: ; preds = %18
store i32 0, i32* %3
br label %30
30: ; preds = %29, %24, %13
%31 = load i32, i32* %3
ret i32 %31
}

LLVM IR (-O2):
@SEEK_SET = global i32 0
@errno = global i32 0
@EINVAL = global i32 0
define i32 @read_header(i32 %0, i32** nocapture %1) {
%3 = tail call i64 @malloc(i32 4)
%4 = inttoptr i64 %3 to i32*
store i32* %4, i32** %1
%5 = load i32, i32* @SEEK_SET
%6 = tail call i64 @lseek(i32 %0, i32 0, i32 %5)
%7 = icmp slt i64 %6, 0
%8 = load i32*, i32** %1
br i1 %7, label %9, label %12
9: ; preds = %2
%10 = tail call i32 @free(i32* %8)
%11 = load i32, i32* @errno
br label %19
12: ; preds = %2
%13 = tail call i64 @read(i32 %0, i32* %8, i32 4)
%14 = icmp slt i64 %13, 1
br i1 %14, label %15, label %19
15: ; preds = %12
%16 = load i32*, i32** %1
%17 = tail call i32 @free(i32* %16)
%18 = load i32, i32* @EINVAL
store i32 %18, i32* @errno
br label %19
19: ; preds = %12, %15, %9
%20 = phi i32 [ %11, %9 ], [ %18, %15 ], [ 0, %12 ]
ret i32 %20
}