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

/* Type definitions */
struct TYPE_10__ {
  size_t const e_shnum;
};
typedef TYPE_1__ Elf64_Shdr;
typedef TYPE_1__ Elf64_Ehdr;

/* Variables and functions */
int EINVAL;
int errno;
int /*<<< orphan*/ free(TYPE_1__ *);
scalar_t__ malloc(int);
int /*<<< orphan*/ memcpy(TYPE_1__ *, TYPE_1__ *, int);
scalar_t__ read_header(int, TYPE_1__ **);
scalar_t__ read_section_table(int, TYPE_1__ *, TYPE_1__ **);
int section_by_index(int d, size_t const index, Elf64_Shdr **section) {
  Elf64_Ehdr *header = NULL;
  Elf64_Shdr *sections = NULL;
  size_t i;
  *section = NULL;
  if (read_header(d, &header) || read_section_table(d, header, &sections))
    return errno;
  if (index < header->e_shnum) {
    *section = (Elf64_Shdr *)malloc(sizeof(Elf64_Shdr));
    if (NULL == *section) {
      free(header);
      free(sections);
      return errno;
    }
    memcpy(*section, sections + index, sizeof(Elf64_Shdr));
  } else
    return EINVAL;
  free(header);
  free(sections);
  return 0;
}


x86 assembly (clang, -O0):
<section_by_index>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x18(%rbp),%rax
movq   $0x0,(%rax)
mov    -0x8(%rbp),%edi
lea    -0x20(%rbp),%rsi
call   1070 <read_header@plt>
cmp    $0x0,%rax
jne    11ae <section_by_index+0x5e>
mov    -0x8(%rbp),%edi
mov    -0x20(%rbp),%rsi
lea    -0x28(%rbp),%rdx
call   1040 <read_section_table@plt>
cmp    $0x0,%rax
je     11bf <section_by_index+0x6f>
mov    0x2e2b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1265 <section_by_index+0x115>
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%rcx
cmp    (%rcx),%rax
jae    123b <section_by_index+0xeb>
mov    $0x8,%edi
call   1060 <malloc@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x18(%rbp),%rcx
xor    %eax,%eax
cmp    (%rcx),%rax
jne    1216 <section_by_index+0xc6>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    0x2dd4(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1265 <section_by_index+0x115>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x10(%rbp),%rax
shl    $0x3,%rax
add    %rax,%rsi
mov    $0x8,%edx
call   1050 <memcpy@plt>
jmp    124c <section_by_index+0xfc>
mov    0x2d96(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1265 <section_by_index+0x115>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<section_by_index>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%rbx
mov    %rsi,%r14
mov    %edi,%ebp
movq   $0x0,(%rsp)
movq   $0x0,0x8(%rsp)
movq   $0x0,(%rdx)
mov    %rsp,%rsi
call   1070 <read_header@plt>
test   %rax,%rax
jne    119a <section_by_index+0x4a>
mov    (%rsp),%rsi
lea    0x8(%rsp),%rdx
mov    %ebp,%edi
call   1040 <read_section_table@plt>
test   %rax,%rax
je     11ac <section_by_index+0x5c>
mov    0x2e3f(%rip),%rax
mov    (%rax),%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    (%rsp),%rax
cmp    %r14,(%rax)
jbe    11f7 <section_by_index+0xa7>
mov    $0x8,%edi
call   1060 <malloc@plt>
mov    %rax,(%rbx)
test   %rax,%rax
je     1200 <section_by_index+0xb0>
shl    $0x3,%r14
add    0x8(%rsp),%r14
mov    %rax,%rdi
mov    %r14,%rsi
mov    $0x8,%edx
call   1050 <memcpy@plt>
mov    (%rsp),%rdi
call   1030 <free@plt>
mov    0x8(%rsp),%rdi
call   1030 <free@plt>
xor    %eax,%eax
jmp    11a3 <section_by_index+0x53>
mov    0x2dda(%rip),%rax
jmp    11a1 <section_by_index+0x51>
mov    (%rsp),%rdi
call   1030 <free@plt>
mov    0x8(%rsp),%rdi
call   1030 <free@plt>
jmp    119a <section_by_index+0x4a>

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

LLVM IR (-O2):
%struct.TYPE_10__ = type { i64 }
@errno = global i32 0
@EINVAL = global i32 0
define i32 @section_by_index(i32 %0, i64 %1, %struct.TYPE_10__** nocapture writeonly %2) {
%4 = alloca %struct.TYPE_10__*
%5 = alloca %struct.TYPE_10__*
%6 = bitcast %struct.TYPE_10__** %4 to i8*
store %struct.TYPE_10__* null, %struct.TYPE_10__** %4
%7 = bitcast %struct.TYPE_10__** %5 to i8*
store %struct.TYPE_10__* null, %struct.TYPE_10__** %5
store %struct.TYPE_10__* null, %struct.TYPE_10__** %2
%8 = call i64 @read_header(i32 %0, %struct.TYPE_10__** %4)
%9 = icmp eq i64 %8, 0
br i1 %9, label %10, label %14
10: ; preds = %3
%11 = load %struct.TYPE_10__*, %struct.TYPE_10__** %4
%12 = call i64 @read_section_table(i32 %0, %struct.TYPE_10__* %11, %struct.TYPE_10__** %5)
%13 = icmp eq i64 %12, 0
br i1 %13, label %16, label %14
14: ; preds = %10, %3
%15 = load i32, i32* @errno
br label %41
16: ; preds = %10
%17 = load %struct.TYPE_10__*, %struct.TYPE_10__** %4
%18 = getelementptr inbounds %struct.TYPE_10__, %struct.TYPE_10__* %17, i64 0, i32 0
%19 = load i64, i64* %18
%20 = icmp ugt i64 %19, %1
br i1 %20, label %21, label %39
21: ; preds = %16
%22 = call i64 @malloc(i32 8)
%23 = inttoptr i64 %22 to %struct.TYPE_10__*
store %struct.TYPE_10__* %23, %struct.TYPE_10__** %2
%24 = icmp eq i64 %22, 0
br i1 %24, label %25, label %31
25: ; preds = %21
%26 = load %struct.TYPE_10__*, %struct.TYPE_10__** %4
%27 = call i32 @free(%struct.TYPE_10__* %26)
%28 = load %struct.TYPE_10__*, %struct.TYPE_10__** %5
%29 = call i32 @free(%struct.TYPE_10__* %28)
%30 = load i32, i32* @errno
br label %41
31: ; preds = %21
%32 = load %struct.TYPE_10__*, %struct.TYPE_10__** %5
%33 = getelementptr inbounds %struct.TYPE_10__, %struct.TYPE_10__* %32, i64 %1
%34 = call i32 @memcpy(%struct.TYPE_10__* %23, %struct.TYPE_10__* %33, i32 8)
%35 = load %struct.TYPE_10__*, %struct.TYPE_10__** %4
%36 = call i32 @free(%struct.TYPE_10__* %35)
%37 = load %struct.TYPE_10__*, %struct.TYPE_10__** %5
%38 = call i32 @free(%struct.TYPE_10__* %37)
br label %41
39: ; preds = %16
%40 = load i32, i32* @EINVAL
br label %41
41: ; preds = %31, %39, %25, %14
%42 = phi i32 [ %15, %14 ], [ %30, %25 ], [ 0, %31 ], [ %40, %39 ]
ret i32 %42
}