#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_6__ TYPE_2__;
typedef struct TYPE_5__ TYPE_1__;

/* Type definitions */
struct TYPE_6__ {
  int e_shnum;
};
struct TYPE_5__ {
  unsigned int sh_name;
};
typedef TYPE_1__ Elf64_Shdr;
typedef TYPE_2__ Elf64_Ehdr;

/* Variables and functions */
TYPE_1__ *get_s_header(char *, int);
char *get_sh_strtab(char *);
int /*<<< orphan*/ strcmp(char *, char const *);
Elf64_Shdr *find_shdr_by_name(char *elf, const char *sect_name) {
  if (!elf || !sect_name)
    return 0;
  Elf64_Ehdr *ehdr;
  ehdr = (Elf64_Ehdr *)elf;
  char *strtab;
  strtab = get_sh_strtab(elf);
  if (!strtab)
    return 0;
  int i;
  unsigned int str_ind;
  char *name;
  Elf64_Shdr *shdr;
  for (i = 0; i < ehdr->e_shnum; i++) {
    shdr = get_s_header(elf, i);
    str_ind = shdr->sh_name;
    name = &strtab[str_ind];
    if (!strcmp(name, sect_name))
      break;
  }
  if (i == ehdr->e_shnum)
    return 0;
  return shdr;
}


x86 assembly (clang, -O0):
<find_shdr_by_name>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1156 <find_shdr_by_name+0x26>
cmpq   $0x0,-0x18(%rbp)
jne    1163 <find_shdr_by_name+0x33>
movq   $0x0,-0x8(%rbp)
jmp    121f <find_shdr_by_name+0xef>
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <get_sh_strtab@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1190 <find_shdr_by_name+0x60>
movq   $0x0,-0x8(%rbp)
jmp    121f <find_shdr_by_name+0xef>
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
mov    -0x20(%rbp),%rcx
cmp    (%rcx),%eax
jge    11fb <find_shdr_by_name+0xcb>
mov    -0x10(%rbp),%rdi
mov    -0x2c(%rbp),%esi
call   1040 <get_s_header@plt>
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    -0x28(%rbp),%rax
mov    -0x30(%rbp),%ecx
add    %rcx,%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1030 <strcmp@plt>
cmp    $0x0,%eax
jne    11e8 <find_shdr_by_name+0xb8>
jmp    11fb <find_shdr_by_name+0xcb>
jmp    11ed <find_shdr_by_name+0xbd>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    1197 <find_shdr_by_name+0x67>
mov    -0x2c(%rbp),%eax
mov    -0x20(%rbp),%rcx
cmp    (%rcx),%eax
jne    1217 <find_shdr_by_name+0xe7>
movq   $0x0,-0x8(%rbp)
jmp    121f <find_shdr_by_name+0xef>
mov    -0x40(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<find_shdr_by_name>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
xor    %eax,%eax
test   %rdi,%rdi
je     11ab <find_shdr_by_name+0x7b>
mov    %rsi,%r14
test   %rsi,%rsi
je     11ab <find_shdr_by_name+0x7b>
mov    %rdi,%r15
call   1050 <get_sh_strtab@plt>
xor    %ebx,%ebx
test   %rax,%rax
je     119a <find_shdr_by_name+0x6a>
mov    (%r15),%ecx
test   %ecx,%ecx
jle    119e <find_shdr_by_name+0x6e>
mov    %rax,%r12
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %r15,%rdi
mov    %ebx,%esi
call   1040 <get_s_header@plt>
mov    %rax,%r13
mov    (%rax),%edi
add    %r12,%rdi
mov    %r14,%rsi
call   1030 <strcmp@plt>
test   %eax,%eax
je     11a0 <find_shdr_by_name+0x70>
add    $0x1,%ebx
mov    (%r15),%ecx
cmp    %ecx,%ebx
jl     1170 <find_shdr_by_name+0x40>
jmp    11a3 <find_shdr_by_name+0x73>
xor    %eax,%eax
jmp    11ab <find_shdr_by_name+0x7b>
jmp    11a3 <find_shdr_by_name+0x73>
mov    (%r15),%ecx
xor    %eax,%eax
cmp    %ecx,%ebx
cmovne %r13,%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.TYPE_5__ = type { i32 }
%struct.TYPE_6__ = type { i32 }
define %struct.TYPE_5__* @find_shdr_by_name(i8* %0, i8* %1) {
%3 = alloca %struct.TYPE_5__*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca %struct.TYPE_6__*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8*
%11 = alloca %struct.TYPE_5__*
store i8* %0, i8** %4
store i8* %1, i8** %5
%12 = load i8*, i8** %4
%13 = icmp ne i8* %12, null
br i1 %13, label %14, label %17
14: ; preds = %2
%15 = load i8*, i8** %5
%16 = icmp ne i8* %15, null
br i1 %16, label %18, label %17
17: ; preds = %14, %2
store %struct.TYPE_5__* null, %struct.TYPE_5__** %3
br label %62
18: ; preds = %14
%19 = load i8*, i8** %4
%20 = bitcast i8* %19 to %struct.TYPE_6__*
store %struct.TYPE_6__* %20, %struct.TYPE_6__** %6
%21 = load i8*, i8** %4
%22 = call i8* @get_sh_strtab(i8* %21)
store i8* %22, i8** %7
%23 = load i8*, i8** %7
%24 = icmp ne i8* %23, null
br i1 %24, label %26, label %25
25: ; preds = %18
store %struct.TYPE_5__* null, %struct.TYPE_5__** %3
br label %62
26: ; preds = %18
store i32 0, i32* %8
br label %27
27: ; preds = %50, %26
%28 = load i32, i32* %8
%29 = load %struct.TYPE_6__*, %struct.TYPE_6__** %6
%30 = getelementptr inbounds %struct.TYPE_6__, %struct.TYPE_6__* %29, i32 0, i32 0
%31 = load i32, i32* %30
%32 = icmp slt i32 %28, %31
br i1 %32, label %33, label %53
33: ; preds = %27
%34 = load i8*, i8** %4
%35 = load i32, i32* %8
%36 = call %struct.TYPE_5__* @get_s_header(i8* %34, i32 %35)
store %struct.TYPE_5__* %36, %struct.TYPE_5__** %11
%37 = load %struct.TYPE_5__*, %struct.TYPE_5__** %11
%38 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %37, i32 0, i32 0
%39 = load i32, i32* %38
store i32 %39, i32* %9
%40 = load i8*, i8** %7
%41 = load i32, i32* %9
%42 = zext i32 %41 to i64
%43 = getelementptr inbounds i8, i8* %40, i64 %42
store i8* %43, i8** %10
%44 = load i8*, i8** %10
%45 = load i8*, i8** %5
%46 = call i32 @strcmp(i8* %44, i8* %45)
%47 = icmp ne i32 %46, 0
br i1 %47, label %49, label %48
48: ; preds = %33
br label %53
49: ; preds = %33
br label %50
50: ; preds = %49
%51 = load i32, i32* %8
%52 = add nsw i32 %51, 1
store i32 %52, i32* %8
br label %27
53: ; preds = %48, %27
%54 = load i32, i32* %8
%55 = load %struct.TYPE_6__*, %struct.TYPE_6__** %6
%56 = getelementptr inbounds %struct.TYPE_6__, %struct.TYPE_6__* %55, i32 0, i32 0
%57 = load i32, i32* %56
%58 = icmp eq i32 %54, %57
br i1 %58, label %59, label %60
59: ; preds = %53
store %struct.TYPE_5__* null, %struct.TYPE_5__** %3
br label %62
60: ; preds = %53
%61 = load %struct.TYPE_5__*, %struct.TYPE_5__** %11
store %struct.TYPE_5__* %61, %struct.TYPE_5__** %3
br label %62
62: ; preds = %60, %59, %25, %17
%63 = load %struct.TYPE_5__*, %struct.TYPE_5__** %3
ret %struct.TYPE_5__* %63
}

LLVM IR (-O2):
%struct.TYPE_5__ = type { i32 }
define %struct.TYPE_5__* @find_shdr_by_name(i8* %0, i8* %1) {
%3 = icmp ne i8* %0, null
%4 = icmp ne i8* %1, null
%5 = and i1 %3, %4
br i1 %5, label %6, label %34
6: ; preds = %2
%7 = tail call i8* @get_sh_strtab(i8* %0)
%8 = icmp eq i8* %7, null
br i1 %8, label %34, label %9
9: ; preds = %6
%10 = bitcast i8* %0 to i32*
%11 = load i32, i32* %10
%12 = icmp sgt i32 %11, 0
br i1 %12, label %13, label %28
13: ; preds = %9, %24
%14 = phi i32 [ %25, %24 ], [ 0, %9 ]
%15 = tail call %struct.TYPE_5__* @get_s_header(i8* %0, i32 %14)
%16 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %15, i64 0, i32 0
%17 = load i32, i32* %16
%18 = zext i32 %17 to i64
%19 = getelementptr inbounds i8, i8* %7, i64 %18
%20 = tail call i32 @strcmp(i8* %19, i8* %1)
%21 = icmp eq i32 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %13
%23 = load i32, i32* %10
br label %28
24: ; preds = %13
%25 = add nuw nsw i32 %14, 1
%26 = load i32, i32* %10
%27 = icmp slt i32 %25, %26
br i1 %27, label %13, label %28
28: ; preds = %24, %22, %9
%29 = phi i32 [ %11, %9 ], [ %23, %22 ], [ %26, %24 ]
%30 = phi i32 [ 0, %9 ], [ %14, %22 ], [ %25, %24 ]
%31 = phi %struct.TYPE_5__* [ undef, %9 ], [ %15, %22 ], [ %15, %24 ]
%32 = icmp eq i32 %30, %29
%33 = select i1 %32, %struct.TYPE_5__* null, %struct.TYPE_5__* %31
br label %34
34: ; preds = %28, %6, %2
%35 = phi %struct.TYPE_5__* [ null, %2 ], [ %33, %28 ], [ null, %6 ]
ret %struct.TYPE_5__* %35
}