#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_5__ TYPE_2__;
typedef struct TYPE_4__ TYPE_1__;

/* Type definitions */
struct TYPE_5__ {
  int e_shoff;
  size_t e_shstrndx;
  int e_shnum;
};
struct TYPE_4__ {
  int sh_offset;
  int sh_name;
};
typedef TYPE_1__ Elf64_Shdr;
typedef TYPE_2__ Elf64_Ehdr;

/* Variables and functions */
int /*<<< orphan*/ printf(char *, int, char *);
int /*<<< orphan*/ strcmp(char *, char *);
Elf64_Shdr *elfi_find_section(void *data, char *name) {
  char *sname;
  int i;
  Elf64_Ehdr *elf_hdr = (Elf64_Ehdr *)data;
  Elf64_Shdr *shdr = (Elf64_Shdr *)(data + elf_hdr->e_shoff);
  Elf64_Shdr *sh_strtab = &shdr[elf_hdr->e_shstrndx];
  const char *const sh_strtab_p = data + sh_strtab->sh_offset;
  printf("+ %d section in file. Looking for section '%s'\n", elf_hdr->e_shnum,
         name);
  for (i = 0; i < elf_hdr->e_shnum; i++) {
    sname = (char *)(sh_strtab_p + shdr[i].sh_name);
    if (!strcmp(sname, name))
      return &shdr[i];
  }
  return NULL;
}


x86 assembly (clang, -O0):
<elfi_find_section>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x30(%rbp),%rcx
movslq (%rcx),%rcx
add    %rcx,%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
mov    -0x30(%rbp),%rcx
mov    0x8(%rcx),%rcx
shl    $0x3,%rcx
add    %rcx,%rax
mov    %rax,-0x40(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x40(%rbp),%rcx
movslq (%rcx),%rcx
add    %rcx,%rax
mov    %rax,-0x48(%rbp)
mov    -0x30(%rbp),%rax
mov    0x10(%rax),%esi
mov    -0x18(%rbp),%rdx
lea    0xe7b(%rip),%rdi
call   1030 <printf@plt>
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    -0x30(%rbp),%rcx
cmp    0x10(%rcx),%eax
jge    11fa <elfi_find_section+0xda>
mov    -0x48(%rbp),%rax
mov    -0x38(%rbp),%rcx
movslq -0x24(%rbp),%rdx
movslq 0x4(%rcx,%rdx,8),%rcx
add    %rcx,%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1040 <strcmp@plt>
cmp    $0x0,%eax
jne    11e7 <elfi_find_section+0xc7>
mov    -0x38(%rbp),%rax
movslq -0x24(%rbp),%rcx
shl    $0x3,%rcx
add    %rcx,%rax
mov    %rax,-0x8(%rbp)
jmp    1202 <elfi_find_section+0xe2>
jmp    11ec <elfi_find_section+0xcc>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    1191 <elfi_find_section+0x71>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<elfi_find_section>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rsi,%r14
mov    %rdi,%r15
movslq (%rdi),%r12
add    %rdi,%r12
mov    0x8(%rdi),%rax
movslq (%r12,%rax,8),%r13
mov    0x10(%rdi),%esi
lea    0xeb9(%rip),%rdi
mov    %r14,%rdx
call   1030 <printf@plt>
mov    0x10(%r15),%ebx
test   %ebx,%ebx
jle    117e <elfi_find_section+0x5e>
add    %r13,%r15
nopw   0x0(%rax,%rax,1)
movslq 0x4(%r12),%rdi
add    %r15,%rdi
mov    %r14,%rsi
call   1040 <strcmp@plt>
test   %eax,%eax
je     1181 <elfi_find_section+0x61>
add    $0x8,%r12
add    $0xffffffffffffffff,%rbx
jne    1160 <elfi_find_section+0x40>
xor    %r12d,%r12d
mov    %r12,%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.TYPE_4__ = type { i32, i32 }
%struct.TYPE_5__ = type { i32, i64, i32 }
@.str = [48 x i8] c"+ %d section in file. Looking for section '%s'\0A\00"
define %struct.TYPE_4__* @elfi_find_section(i8* %0, i8* %1) {
%3 = alloca %struct.TYPE_4__*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca %struct.TYPE_5__*
%9 = alloca %struct.TYPE_4__*
%10 = alloca %struct.TYPE_4__*
%11 = alloca i8*
store i8* %0, i8** %4
store i8* %1, i8** %5
%12 = load i8*, i8** %4
%13 = bitcast i8* %12 to %struct.TYPE_5__*
store %struct.TYPE_5__* %13, %struct.TYPE_5__** %8
%14 = load i8*, i8** %4
%15 = load %struct.TYPE_5__*, %struct.TYPE_5__** %8
%16 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %15, i32 0, i32 0
%17 = load i32, i32* %16
%18 = sext i32 %17 to i64
%19 = getelementptr i8, i8* %14, i64 %18
%20 = bitcast i8* %19 to %struct.TYPE_4__*
store %struct.TYPE_4__* %20, %struct.TYPE_4__** %9
%21 = load %struct.TYPE_4__*, %struct.TYPE_4__** %9
%22 = load %struct.TYPE_5__*, %struct.TYPE_5__** %8
%23 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %22, i32 0, i32 1
%24 = load i64, i64* %23
%25 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %21, i64 %24
store %struct.TYPE_4__* %25, %struct.TYPE_4__** %10
%26 = load i8*, i8** %4
%27 = load %struct.TYPE_4__*, %struct.TYPE_4__** %10
%28 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %27, i32 0, i32 0
%29 = load i32, i32* %28
%30 = sext i32 %29 to i64
%31 = getelementptr i8, i8* %26, i64 %30
store i8* %31, i8** %11
%32 = load %struct.TYPE_5__*, %struct.TYPE_5__** %8
%33 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %32, i32 0, i32 2
%34 = load i32, i32* %33
%35 = load i8*, i8** %5
%36 = call i32 @printf(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str, i64 0, i64 0), i32 %34, i8* %35)
store i32 0, i32* %7
br label %37
37: ; preds = %63, %2
%38 = load i32, i32* %7
%39 = load %struct.TYPE_5__*, %struct.TYPE_5__** %8
%40 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %39, i32 0, i32 2
%41 = load i32, i32* %40
%42 = icmp slt i32 %38, %41
br i1 %42, label %43, label %66
43: ; preds = %37
%44 = load i8*, i8** %11
%45 = load %struct.TYPE_4__*, %struct.TYPE_4__** %9
%46 = load i32, i32* %7
%47 = sext i32 %46 to i64
%48 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %45, i64 %47
%49 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %48, i32 0, i32 1
%50 = load i32, i32* %49
%51 = sext i32 %50 to i64
%52 = getelementptr inbounds i8, i8* %44, i64 %51
store i8* %52, i8** %6
%53 = load i8*, i8** %6
%54 = load i8*, i8** %5
%55 = call i32 @strcmp(i8* %53, i8* %54)
%56 = icmp ne i32 %55, 0
br i1 %56, label %62, label %57
57: ; preds = %43
%58 = load %struct.TYPE_4__*, %struct.TYPE_4__** %9
%59 = load i32, i32* %7
%60 = sext i32 %59 to i64
%61 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %58, i64 %60
store %struct.TYPE_4__* %61, %struct.TYPE_4__** %3
br label %67
62: ; preds = %43
br label %63
63: ; preds = %62
%64 = load i32, i32* %7
%65 = add nsw i32 %64, 1
store i32 %65, i32* %7
br label %37
66: ; preds = %37
store %struct.TYPE_4__* null, %struct.TYPE_4__** %3
br label %67
67: ; preds = %66, %57
%68 = load %struct.TYPE_4__*, %struct.TYPE_4__** %3
ret %struct.TYPE_4__* %68
}

LLVM IR (-O2):
%struct.TYPE_4__ = type { i32, i32 }
@.str = [48 x i8] c"+ %d section in file. Looking for section '%s'\0A\00"
define %struct.TYPE_4__* @elfi_find_section(i8* %0, i8* %1) {
%3 = bitcast i8* %0 to i32*
%4 = load i32, i32* %3
%5 = sext i32 %4 to i64
%6 = getelementptr i8, i8* %0, i64 %5
%7 = bitcast i8* %6 to %struct.TYPE_4__*
%8 = getelementptr inbounds i8, i8* %0, i64 8
%9 = bitcast i8* %8 to i64*
%10 = load i64, i64* %9
%11 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %7, i64 %10, i32 0
%12 = load i32, i32* %11
%13 = sext i32 %12 to i64
%14 = getelementptr i8, i8* %0, i64 %13
%15 = getelementptr inbounds i8, i8* %0, i64 16
%16 = bitcast i8* %15 to i32*
%17 = load i32, i32* %16
%18 = tail call i32 @printf(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str, i64 0, i64 0), i32 %17, i8* %1)
%19 = load i32, i32* %16
%20 = icmp sgt i32 %19, 0
br i1 %20, label %21, label %37
21: ; preds = %2
%22 = zext i32 %19 to i64
br label %23
23: ; preds = %21, %34
%24 = phi i64 [ 0, %21 ], [ %35, %34 ]
%25 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %7, i64 %24, i32 1
%26 = load i32, i32* %25
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds i8, i8* %14, i64 %27
%29 = tail call i32 @strcmp(i8* dereferenceable(1) %28, i8* dereferenceable(1) %1)
%30 = icmp eq i32 %29, 0
br i1 %30, label %31, label %34
31: ; preds = %23
%32 = and i64 %24, 4294967295
%33 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %7, i64 %32
br label %37
34: ; preds = %23
%35 = add nuw nsw i64 %24, 1
%36 = icmp eq i64 %35, %22
br i1 %36, label %37, label %23
37: ; preds = %34, %2, %31
%38 = phi %struct.TYPE_4__* [ %33, %31 ], [ null, %2 ], [ null, %34 ]
ret %struct.TYPE_4__* %38
}