#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 /*<<< orphan*/ sh_size;
  int /*<<< orphan*/ sh_offset;
};
typedef TYPE_1__ Elf64_Shdr;
typedef int /*<<< orphan*/ Elf64_Rela;

/* 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 /*<<< orphan*/);
scalar_t__ read(int, int /*<<< orphan*/ *, int /*<<< orphan*/);
int read_relocation_table(int d, Elf64_Shdr const *section,
                          Elf64_Rela **table) {
  if (NULL == section)
    return EINVAL;
  *table = (Elf64_Rela *)malloc(section->sh_size);
  if (lseek(d, section->sh_offset, SEEK_SET) < 0) {
    free(*table);
    return errno;
  }
  if (read(d, *table, section->sh_size) <= 0) {
    free(*table);
    return errno = EINVAL;
  }
  return 0;
}


x86 assembly (clang, -O0):
<read_relocation_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_relocation_table+0x30>
mov    0x2e6a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1210 <read_relocation_table+0xd0>
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
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    0x2e42(%rip),%rax
mov    (%rax),%edx
call   1040 <lseek@plt>
cmp    $0x0,%rax
jge    11c4 <read_relocation_table+0x84>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
call   1030 <free@plt>
mov    0x2e26(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1210 <read_relocation_table+0xd0>
mov    -0x8(%rbp),%edi
mov    -0x18(%rbp),%rax
mov    (%rax),%rsi
mov    -0x10(%rbp),%rax
mov    (%rax),%edx
call   1050 <read@plt>
cmp    $0x0,%rax
jg     1209 <read_relocation_table+0xc9>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
call   1030 <free@plt>
mov    0x2dda(%rip),%rax
mov    (%rax),%eax
mov    0x2de1(%rip),%rcx
mov    %eax,(%rcx)
mov    %eax,-0x4(%rbp)
jmp    1210 <read_relocation_table+0xd0>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_relocation_table>:
push   %rbp
push   %r14
push   %rbx
test   %rsi,%rsi
je     11a5 <read_relocation_table+0x65>
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %edi,%ebp
mov    (%rsi),%edi
call   1060 <malloc@plt>
mov    %rax,(%r14)
mov    0x4(%rbx),%esi
mov    0x2e73(%rip),%rax
mov    (%rax),%edx
mov    %ebp,%edi
call   1040 <lseek@plt>
mov    (%r14),%rsi
test   %rax,%rax
js     11ae <read_relocation_table+0x6e>
mov    (%rbx),%edx
mov    %ebp,%edi
call   1050 <read@plt>
mov    %rax,%rcx
xor    %eax,%eax
test   %rcx,%rcx
jg     11bf <read_relocation_table+0x7f>
mov    (%r14),%rdi
call   1030 <free@plt>
mov    0x2e38(%rip),%rax
mov    (%rax),%eax
mov    0x2e3f(%rip),%rcx
mov    %eax,(%rcx)
jmp    11bf <read_relocation_table+0x7f>
mov    0x2e24(%rip),%rax
jmp    11bd <read_relocation_table+0x7d>
mov    %rsi,%rdi
call   1030 <free@plt>
mov    0x2e23(%rip),%rax
mov    (%rax),%eax
pop    %rbx
pop    %r14
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_relocation_table(i32 %0, %struct.TYPE_3__* %1, i32** %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.TYPE_3__*
%7 = alloca i32**
store i32 %0, i32* %5
store %struct.TYPE_3__* %1, %struct.TYPE_3__** %6
store i32** %2, i32*** %7
%8 = load %struct.TYPE_3__*, %struct.TYPE_3__** %6
%9 = icmp eq %struct.TYPE_3__* null, %8
br i1 %9, label %10, label %12
10: ; preds = %3
%11 = load i32, i32* @EINVAL
store i32 %11, i32* %4
br label %46
12: ; preds = %3
%13 = load %struct.TYPE_3__*, %struct.TYPE_3__** %6
%14 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %13, i32 0, i32 0
%15 = load i32, i32* %14
%16 = call i64 @malloc(i32 %15)
%17 = inttoptr i64 %16 to i32*
%18 = load i32**, i32*** %7
store i32* %17, i32** %18
%19 = load i32, i32* %5
%20 = load %struct.TYPE_3__*, %struct.TYPE_3__** %6
%21 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %20, i32 0, i32 1
%22 = load i32, i32* %21
%23 = load i32, i32* @SEEK_SET
%24 = call i64 @lseek(i32 %19, i32 %22, i32 %23)
%25 = icmp slt i64 %24, 0
br i1 %25, label %26, label %31
26: ; preds = %12
%27 = load i32**, i32*** %7
%28 = load i32*, i32** %27
%29 = call i32 @free(i32* %28)
%30 = load i32, i32* @errno
store i32 %30, i32* %4
br label %46
31: ; preds = %12
%32 = load i32, i32* %5
%33 = load i32**, i32*** %7
%34 = load i32*, i32** %33
%35 = load %struct.TYPE_3__*, %struct.TYPE_3__** %6
%36 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %35, i32 0, i32 0
%37 = load i32, i32* %36
%38 = call i64 @read(i32 %32, i32* %34, i32 %37)
%39 = icmp sle i64 %38, 0
br i1 %39, label %40, label %45
40: ; preds = %31
%41 = load i32**, i32*** %7
%42 = load i32*, i32** %41
%43 = call i32 @free(i32* %42)
%44 = load i32, i32* @EINVAL
store i32 %44, i32* @errno
store i32 %44, i32* %4
br label %46
45: ; preds = %31
store i32 0, i32* %4
br label %46
46: ; preds = %45, %40, %26, %10
%47 = load i32, i32* %4
ret i32 %47
}

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_relocation_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 %29
7: ; preds = %3
%8 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %1, i64 0, i32 0
%9 = load i32, i32* %8
%10 = tail call i64 @malloc(i32 %9)
%11 = inttoptr i64 %10 to i32*
store i32* %11, i32** %2
%12 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %1, i64 0, i32 1
%13 = load i32, i32* %12
%14 = load i32, i32* @SEEK_SET
%15 = tail call i64 @lseek(i32 %0, i32 %13, i32 %14)
%16 = icmp slt i64 %15, 0
%17 = load i32*, i32** %2
br i1 %16, label %18, label %21
18: ; preds = %7
%19 = tail call i32 @free(i32* %17)
%20 = load i32, i32* @errno
br label %29
21: ; preds = %7
%22 = load i32, i32* %8
%23 = tail call i64 @read(i32 %0, i32* %17, i32 %22)
%24 = icmp slt i64 %23, 1
br i1 %24, label %25, label %29
25: ; preds = %21
%26 = load i32*, i32** %2
%27 = tail call i32 @free(i32* %26)
%28 = load i32, i32* @EINVAL
store i32 %28, i32* @errno
br label %29
29: ; preds = %21, %25, %18, %5
%30 = phi i32 [ %6, %5 ], [ %20, %18 ], [ %28, %25 ], [ 0, %21 ]
ret i32 %30
}