#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__ {
  int e_shnum;
  int /*<<< orphan*/ e_shoff;
};
typedef int /*<<< orphan*/ Elf64_Shdr;
typedef TYPE_1__ 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(size_t);
scalar_t__ read(int, int /*<<< orphan*/ *, size_t);
int read_section_table(int d, Elf64_Ehdr const *header, Elf64_Shdr **table) {
  size_t size;
  if (NULL == header)
    return EINVAL;
  size = header->e_shnum * sizeof(Elf64_Shdr);
  *table = (Elf64_Shdr *)malloc(size);
  if (lseek(d, header->e_shoff, SEEK_SET) < 0) {
    free(*table);
    return errno;
  }
  if (read(d, *table, size) <= 0) {
    free(*table);
    return errno = EINVAL;
  }
  return 0;
}


x86 assembly (clang, -O0):
<read_section_table>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
xor    %eax,%eax
cmp    -0x10(%rbp),%rax
jne    1170 <read_section_table+0x30>
mov    0x2e6a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    121b <read_section_table+0xdb>
mov    -0x10(%rbp),%rax
movslq (%rax),%rax
shl    $0x2,%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
call   1060 <malloc@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rax
mov    0x4(%rax),%esi
mov    0x2e35(%rip),%rax
mov    (%rax),%edx
call   1040 <lseek@plt>
cmp    $0x0,%rax
jge    11d1 <read_section_table+0x91>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
call   1030 <free@plt>
mov    0x2e19(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    121b <read_section_table+0xdb>
mov    -0x8(%rbp),%edi
mov    -0x18(%rbp),%rax
mov    (%rax),%rsi
mov    -0x20(%rbp),%rdx
call   1050 <read@plt>
cmp    $0x0,%rax
jg     1214 <read_section_table+0xd4>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
call   1030 <free@plt>
mov    0x2dcf(%rip),%rax
mov    (%rax),%eax
mov    0x2dd6(%rip),%rcx
mov    %eax,(%rcx)
mov    %eax,-0x4(%rbp)
jmp    121b <read_section_table+0xdb>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_section_table>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
test   %rsi,%rsi
je     11b4 <read_section_table+0x74>
mov    %rdx,%r14
mov    %rsi,%rbp
mov    %edi,%r15d
movslq (%rsi),%rbx
shl    $0x2,%rbx
mov    %rbx,%rdi
call   1060 <malloc@plt>
mov    %rax,(%r14)
mov    0x4(%rbp),%esi
mov    0x2e67(%rip),%rax
mov    (%rax),%edx
mov    %r15d,%edi
call   1040 <lseek@plt>
mov    (%r14),%rsi
test   %rax,%rax
js     11bd <read_section_table+0x7d>
mov    %r15d,%edi
mov    %rbx,%rdx
call   1050 <read@plt>
mov    %rax,%rcx
xor    %eax,%eax
test   %rcx,%rcx
jg     11ce <read_section_table+0x8e>
mov    (%r14),%rdi
call   1030 <free@plt>
mov    0x2e29(%rip),%rax
mov    (%rax),%eax
mov    0x2e30(%rip),%rcx
mov    %eax,(%rcx)
jmp    11ce <read_section_table+0x8e>
mov    0x2e15(%rip),%rax
jmp    11cc <read_section_table+0x8c>
mov    %rsi,%rdi
call   1030 <free@plt>
mov    0x2e14(%rip),%rax
mov    (%rax),%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.TYPE_3__ = type { i32, i32 }
@EINVAL = global i32 0
@SEEK_SET = global i32 0
@errno = global i32 0
define i32 @read_section_table(i32 %0, %struct.TYPE_3__* %1, i32** %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.TYPE_3__*
%7 = alloca i32**
%8 = alloca i64
store i32 %0, i32* %5
store %struct.TYPE_3__* %1, %struct.TYPE_3__** %6
store i32** %2, i32*** %7
%9 = load %struct.TYPE_3__*, %struct.TYPE_3__** %6
%10 = icmp eq %struct.TYPE_3__* null, %9
br i1 %10, label %11, label %13
11: ; preds = %3
%12 = load i32, i32* @EINVAL
store i32 %12, i32* %4
br label %48
13: ; preds = %3
%14 = load %struct.TYPE_3__*, %struct.TYPE_3__** %6
%15 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %14, i32 0, i32 0
%16 = load i32, i32* %15
%17 = sext i32 %16 to i64
%18 = mul i64 %17, 4
store i64 %18, i64* %8
%19 = load i64, i64* %8
%20 = call i64 @malloc(i64 %19)
%21 = inttoptr i64 %20 to i32*
%22 = load i32**, i32*** %7
store i32* %21, i32** %22
%23 = load i32, i32* %5
%24 = load %struct.TYPE_3__*, %struct.TYPE_3__** %6
%25 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %24, i32 0, i32 1
%26 = load i32, i32* %25
%27 = load i32, i32* @SEEK_SET
%28 = call i64 @lseek(i32 %23, i32 %26, i32 %27)
%29 = icmp slt i64 %28, 0
br i1 %29, label %30, label %35
30: ; preds = %13
%31 = load i32**, i32*** %7
%32 = load i32*, i32** %31
%33 = call i32 @free(i32* %32)
%34 = load i32, i32* @errno
store i32 %34, i32* %4
br label %48
35: ; preds = %13
%36 = load i32, i32* %5
%37 = load i32**, i32*** %7
%38 = load i32*, i32** %37
%39 = load i64, i64* %8
%40 = call i64 @read(i32 %36, i32* %38, i64 %39)
%41 = icmp sle i64 %40, 0
br i1 %41, label %42, label %47
42: ; preds = %35
%43 = load i32**, i32*** %7
%44 = load i32*, i32** %43
%45 = call i32 @free(i32* %44)
%46 = load i32, i32* @EINVAL
store i32 %46, i32* @errno
store i32 %46, i32* %4
br label %48
47: ; preds = %35
store i32 0, i32* %4
br label %48
48: ; preds = %47, %42, %30, %11
%49 = load i32, i32* %4
ret i32 %49
}

LLVM IR (-O2):
%struct.TYPE_3__ = type { i32, i32 }
@EINVAL = global i32 0
@SEEK_SET = global i32 0
@errno = global i32 0
define i32 @read_section_table(i32 %0, %struct.TYPE_3__* %1, i32** nocapture %2) {
%4 = icmp eq %struct.TYPE_3__* %1, null
br i1 %4, label %5, label %7
5: ; preds = %3
%6 = load i32, i32* @EINVAL
br label %30
7: ; preds = %3
%8 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %1, i64 0, i32 0
%9 = load i32, i32* %8
%10 = sext i32 %9 to i64
%11 = shl nsw i64 %10, 2
%12 = tail call i64 @malloc(i64 %11)
%13 = inttoptr i64 %12 to i32*
store i32* %13, i32** %2
%14 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %1, i64 0, i32 1
%15 = load i32, i32* %14
%16 = load i32, i32* @SEEK_SET
%17 = tail call i64 @lseek(i32 %0, i32 %15, i32 %16)
%18 = icmp slt i64 %17, 0
%19 = load i32*, i32** %2
br i1 %18, label %20, label %23
20: ; preds = %7
%21 = tail call i32 @free(i32* %19)
%22 = load i32, i32* @errno
br label %30
23: ; preds = %7
%24 = tail call i64 @read(i32 %0, i32* %19, i64 %11)
%25 = icmp slt i64 %24, 1
br i1 %25, label %26, label %30
26: ; preds = %23
%27 = load i32*, i32** %2
%28 = tail call i32 @free(i32* %27)
%29 = load i32, i32* @EINVAL
store i32 %29, i32* @errno
br label %30
30: ; preds = %23, %26, %20, %5
%31 = phi i32 [ %6, %5 ], [ %22, %20 ], [ %29, %26 ], [ 0, %23 ]
ret i32 %31
}