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

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
}