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

/* Type definitions */
struct TYPE_2__ {
  int e_shnum;
  int e_shoff;
  int e_shentsize;
};
typedef int /*<<< orphan*/ Elf32_Shdr;
typedef TYPE_1__ Elf32_Ehdr;

/* Variables and functions */
Elf32_Shdr *elf_sh_lookup(unsigned char *elf_image, int n) {
  Elf32_Ehdr *ehdr = (Elf32_Ehdr *)elf_image;
  return (n < ehdr->e_shnum)
             ? (Elf32_Shdr *)(elf_image + ehdr->e_shoff + n * ehdr->e_shentsize)
             : 0;
}


x86 assembly (clang, -O0):
<elf_sh_lookup>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0xc(%rbp),%eax
mov    -0x18(%rbp),%rcx
cmp    (%rcx),%eax
jge    114b <elf_sh_lookup+0x4b>
mov    -0x8(%rbp),%rax
mov    -0x18(%rbp),%rcx
movslq 0x4(%rcx),%rcx
add    %rcx,%rax
mov    -0xc(%rbp),%ecx
mov    -0x18(%rbp),%rdx
imul   0x8(%rdx),%ecx
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x20(%rbp)
jmp    1156 <elf_sh_lookup+0x56>
xor    %eax,%eax
mov    %rax,-0x20(%rbp)
jmp    1156 <elf_sh_lookup+0x56>
mov    -0x20(%rbp),%rax
pop    %rbp
ret

x86 assembly (clang, -O2):
<elf_sh_lookup>:
cmp    %esi,(%rdi)
jle    111a <elf_sh_lookup+0x1a>
movslq 0x4(%rdi),%rcx
add    %rdi,%rcx
movslq 0x8(%rdi),%rdx
movslq %esi,%rax
imul   %rdx,%rax
add    %rcx,%rax
ret
xor    %eax,%eax
ret

LLVM IR (-O0):
%struct.TYPE_2__ = type { i32, i32, i32 }
define i32* @elf_sh_lookup(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca %struct.TYPE_2__*
store i8* %0, i8** %3
store i32 %1, i32* %4
%6 = load i8*, i8** %3
%7 = bitcast i8* %6 to %struct.TYPE_2__*
store %struct.TYPE_2__* %7, %struct.TYPE_2__** %5
%8 = load i32, i32* %4
%9 = load %struct.TYPE_2__*, %struct.TYPE_2__** %5
%10 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %9, i32 0, i32 0
%11 = load i32, i32* %10
%12 = icmp slt i32 %8, %11
br i1 %12, label %13, label %28
13: ; preds = %2
%14 = load i8*, i8** %3
%15 = load %struct.TYPE_2__*, %struct.TYPE_2__** %5
%16 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %15, i32 0, i32 1
%17 = load i32, i32* %16
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds i8, i8* %14, i64 %18
%20 = load i32, i32* %4
%21 = load %struct.TYPE_2__*, %struct.TYPE_2__** %5
%22 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %21, i32 0, i32 2
%23 = load i32, i32* %22
%24 = mul nsw i32 %20, %23
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds i8, i8* %19, i64 %25
%27 = bitcast i8* %26 to i32*
br label %29
28: ; preds = %2
br label %29
29: ; preds = %28, %13
%30 = phi i32* [ %27, %13 ], [ null, %28 ]
ret i32* %30
}

LLVM IR (-O2):
define i32* @elf_sh_lookup(i8* %0, i32 %1) {
%3 = bitcast i8* %0 to i32*
%4 = load i32, i32* %3
%5 = icmp sgt i32 %4, %1
br i1 %5, label %6, label %19
6: ; preds = %2
%7 = getelementptr inbounds i8, i8* %0, i64 4
%8 = bitcast i8* %7 to i32*
%9 = load i32, i32* %8
%10 = sext i32 %9 to i64
%11 = getelementptr inbounds i8, i8* %0, i64 %10
%12 = getelementptr inbounds i8, i8* %0, i64 8
%13 = bitcast i8* %12 to i32*
%14 = load i32, i32* %13
%15 = mul nsw i32 %14, %1
%16 = sext i32 %15 to i64
%17 = getelementptr inbounds i8, i8* %11, i64 %16
%18 = bitcast i8* %17 to i32*
br label %19
19: ; preds = %2, %6
%20 = phi i32* [ %18, %6 ], [ null, %2 ]
ret i32* %20
}