#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 e_shnum;
  size_t const sh_type;
};
typedef TYPE_1__ Elf64_Shdr;
typedef TYPE_1__ Elf64_Ehdr;

/* Variables and functions */
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_type(int d, size_t const section_type, 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;
  for (i = 0; i < header->e_shnum; ++i)
    if (section_type == sections[i].sh_type) {
      *section = (Elf64_Shdr *)malloc(sizeof(Elf64_Shdr));
      if (NULL == *section) {
        free(header);
        free(sections);
        return errno;
      }
      memcpy(*section, sections + i, sizeof(Elf64_Shdr));
      break;
    }
  free(header);
  free(sections);
  return 0;
}


x86 assembly (clang, -O0):
<section_by_type>:
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_type+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_type+0x6f>
mov    0x2e2b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    128f <section_by_type+0x13f>
movq   $0x0,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    -0x20(%rbp),%rcx
cmp    (%rcx),%rax
jae    1276 <section_by_type+0x126>
mov    -0x10(%rbp),%rax
mov    -0x28(%rbp),%rcx
mov    -0x30(%rbp),%rdx
shl    $0x4,%rdx
add    %rdx,%rcx
cmp    0x8(%rcx),%rax
jne    1260 <section_by_type+0x110>
mov    $0x10,%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    123b <section_by_type+0xeb>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    0x2daf(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    128f <section_by_type+0x13f>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x30(%rbp),%rax
shl    $0x4,%rax
add    %rax,%rsi
mov    $0x10,%edx
call   1050 <memcpy@plt>
jmp    1276 <section_by_type+0x126>
jmp    1265 <section_by_type+0x115>
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
jmp    11c7 <section_by_type+0x77>
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_type>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%r14
mov    %rsi,%r15
mov    %edi,%ebx
movq   $0x0,0x8(%rsp)
movq   $0x0,(%rsp)
movq   $0x0,(%rdx)
lea    0x8(%rsp),%rsi
call   1070 <read_header@plt>
test   %rax,%rax
jne    119c <section_by_type+0x4c>
mov    0x8(%rsp),%rsi
mov    %rsp,%rdx
mov    %ebx,%edi
call   1040 <read_section_table@plt>
test   %rax,%rax
je     11af <section_by_type+0x5f>
mov    0x2e3d(%rip),%rax
mov    (%rax),%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
mov    0x8(%rsp),%rdi
mov    (%rdi),%rax
test   %rax,%rax
je     120e <section_by_type+0xbe>
mov    (%rsp),%rcx
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
cmp    %r15,0x8(%rcx,%rbx,1)
je     11e3 <section_by_type+0x93>
add    $0x10,%rbx
add    $0xffffffffffffffff,%rax
jne    11d0 <section_by_type+0x80>
jmp    120e <section_by_type+0xbe>
mov    $0x10,%edi
call   1060 <malloc@plt>
mov    %rax,(%r14)
test   %rax,%rax
je     1220 <section_by_type+0xd0>
add    (%rsp),%rbx
mov    %rax,%rdi
mov    %rbx,%rsi
mov    $0x10,%edx
call   1050 <memcpy@plt>
mov    0x8(%rsp),%rdi
call   1030 <free@plt>
mov    (%rsp),%rdi
call   1030 <free@plt>
xor    %eax,%eax
jmp    11a5 <section_by_type+0x55>
mov    0x8(%rsp),%rdi
call   1030 <free@plt>
mov    (%rsp),%rdi
call   1030 <free@plt>
jmp    119c <section_by_type+0x4c>

LLVM IR (-O0):
%struct.TYPE_10__ = type { i64, i64 }
@errno = global i32 0
define i32 @section_by_type(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 %66
22: ; preds = %15
store i64 0, i64* %10
br label %23
23: ; preds = %58, %22
%24 = load i64, i64* %10
%25 = load %struct.TYPE_10__*, %struct.TYPE_10__** %8
%26 = getelementptr inbounds %struct.TYPE_10__, %struct.TYPE_10__* %25, i32 0, i32 0
%27 = load i64, i64* %26
%28 = icmp ult i64 %24, %27
br i1 %28, label %29, label %61
29: ; preds = %23
%30 = load i64, i64* %6
%31 = load %struct.TYPE_10__*, %struct.TYPE_10__** %9
%32 = load i64, i64* %10
%33 = getelementptr inbounds %struct.TYPE_10__, %struct.TYPE_10__* %31, i64 %32
%34 = getelementptr inbounds %struct.TYPE_10__, %struct.TYPE_10__* %33, i32 0, i32 1
%35 = load i64, i64* %34
%36 = icmp eq i64 %30, %35
br i1 %36, label %37, label %57
37: ; preds = %29
%38 = call i64 @malloc(i32 16)
%39 = inttoptr i64 %38 to %struct.TYPE_10__*
%40 = load %struct.TYPE_10__**, %struct.TYPE_10__*** %7
store %struct.TYPE_10__* %39, %struct.TYPE_10__** %40
%41 = load %struct.TYPE_10__**, %struct.TYPE_10__*** %7
%42 = load %struct.TYPE_10__*, %struct.TYPE_10__** %41
%43 = icmp eq %struct.TYPE_10__* null, %42
br i1 %43, label %44, label %50
44: ; preds = %37
%45 = load %struct.TYPE_10__*, %struct.TYPE_10__** %8
%46 = call i32 @free(%struct.TYPE_10__* %45)
%47 = load %struct.TYPE_10__*, %struct.TYPE_10__** %9
%48 = call i32 @free(%struct.TYPE_10__* %47)
%49 = load i32, i32* @errno
store i32 %49, i32* %4
br label %66
50: ; preds = %37
%51 = load %struct.TYPE_10__**, %struct.TYPE_10__*** %7
%52 = load %struct.TYPE_10__*, %struct.TYPE_10__** %51
%53 = load %struct.TYPE_10__*, %struct.TYPE_10__** %9
%54 = load i64, i64* %10
%55 = getelementptr inbounds %struct.TYPE_10__, %struct.TYPE_10__* %53, i64 %54
%56 = call i32 @memcpy(%struct.TYPE_10__* %52, %struct.TYPE_10__* %55, i32 16)
br label %61
57: ; preds = %29
br label %58
58: ; preds = %57
%59 = load i64, i64* %10
%60 = add i64 %59, 1
store i64 %60, i64* %10
br label %23
61: ; preds = %50, %23
%62 = load %struct.TYPE_10__*, %struct.TYPE_10__** %8
%63 = call i32 @free(%struct.TYPE_10__* %62)
%64 = load %struct.TYPE_10__*, %struct.TYPE_10__** %9
%65 = call i32 @free(%struct.TYPE_10__* %64)
store i32 0, i32* %4
br label %66
66: ; preds = %61, %44, %20
%67 = load i32, i32* %4
ret i32 %67
}

LLVM IR (-O2):
%struct.TYPE_10__ = type { i64, i64 }
@errno = global i32 0
define i32 @section_by_type(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 %21
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 %14, label %21
14: ; preds = %10
%15 = load %struct.TYPE_10__*, %struct.TYPE_10__** %4
%16 = getelementptr inbounds %struct.TYPE_10__, %struct.TYPE_10__* %15, i64 0, i32 0
%17 = load i64, i64* %16
%18 = icmp eq i64 %17, 0
br i1 %18, label %46, label %19
19: ; preds = %14
%20 = load %struct.TYPE_10__*, %struct.TYPE_10__** %5
br label %23
21: ; preds = %10, %3
%22 = load i32, i32* @errno
br label %51
23: ; preds = %19, %43
%24 = phi i64 [ 0, %19 ], [ %44, %43 ]
%25 = getelementptr inbounds %struct.TYPE_10__, %struct.TYPE_10__* %20, i64 %24, i32 1
%26 = load i64, i64* %25
%27 = icmp eq i64 %26, %1
br i1 %27, label %28, label %43
28: ; preds = %23
%29 = call i64 @malloc(i32 16)
%30 = inttoptr i64 %29 to %struct.TYPE_10__*
store %struct.TYPE_10__* %30, %struct.TYPE_10__** %2
%31 = icmp eq i64 %29, 0
br i1 %31, label %32, label %38
32: ; preds = %28
%33 = load %struct.TYPE_10__*, %struct.TYPE_10__** %4
%34 = call i32 @free(%struct.TYPE_10__* %33)
%35 = load %struct.TYPE_10__*, %struct.TYPE_10__** %5
%36 = call i32 @free(%struct.TYPE_10__* %35)
%37 = load i32, i32* @errno
br label %51
38: ; preds = %28
%39 = load %struct.TYPE_10__*, %struct.TYPE_10__** %5
%40 = getelementptr inbounds %struct.TYPE_10__, %struct.TYPE_10__* %39, i64 %24
%41 = call i32 @memcpy(%struct.TYPE_10__* %30, %struct.TYPE_10__* %40, i32 16)
%42 = load %struct.TYPE_10__*, %struct.TYPE_10__** %4
br label %46
43: ; preds = %23
%44 = add nuw i64 %24, 1
%45 = icmp eq i64 %44, %17
br i1 %45, label %46, label %23
46: ; preds = %43, %14, %38
%47 = phi %struct.TYPE_10__* [ %15, %14 ], [ %42, %38 ], [ %15, %43 ]
%48 = call i32 @free(%struct.TYPE_10__* %47)
%49 = load %struct.TYPE_10__*, %struct.TYPE_10__** %5
%50 = call i32 @free(%struct.TYPE_10__* %49)
br label %51
51: ; preds = %46, %32, %21
%52 = phi i32 [ %22, %21 ], [ %37, %32 ], [ 0, %46 ]
ret i32 %52
}