#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 */

/* Type definitions */
typedef int /*<<< orphan*/ uint8_t;

/* Variables and functions */
int get_int32_be(int /*<<< orphan*/ *);
int imports_ar_find_lookup_table(int /*<<< orphan*/ *, int);
int /*<<< orphan*/ printf(char *);
scalar_t__ strcmp(char const *, char const *);
scalar_t__ strlen(char *);
int imports_ar_find_symbol_section_offset(uint8_t *buffer, int file_size,
                                          const char *symbol) {
  int entry_count;
  int ptr_offset;
  int ptr_name;
  int n;
  int lookup_table = imports_ar_find_lookup_table(buffer, file_size);
  if (lookup_table == -1) {
    printf("Can't find lookup table\n");
    return -1;
  }
  buffer = buffer + lookup_table + 60;
  entry_count = get_int32_be(buffer);
  ptr_offset = 4;
  ptr_name = ptr_offset + (entry_count * 4);
  for (n = 0; n < entry_count; n++) {
    int offset = get_int32_be(buffer + ptr_offset);
    ptr_offset += 4;
    ptr_name += strlen((char *)(buffer + ptr_name)) + 1;
    if (strcmp((const char *)(buffer + ptr_name), symbol) == 0) {
      return offset;
    }
  }
  return -1;
}


x86 assembly (clang, -O0):
<imports_ar_find_symbol_section_offset>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1070 <imports_ar_find_lookup_table@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0xffffffff,-0x34(%rbp)
jne    1194 <imports_ar_find_symbol_section_offset+0x44>
lea    0xe7d(%rip),%rdi
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    126d <imports_ar_find_symbol_section_offset+0x11d>
mov    -0x10(%rbp),%rax
movslq -0x34(%rbp),%rcx
shl    $0x2,%rcx
add    %rcx,%rax
add    $0xf0,%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <get_int32_be@plt>
mov    %eax,-0x24(%rbp)
movl   $0x4,-0x28(%rbp)
mov    -0x28(%rbp),%eax
mov    -0x24(%rbp),%ecx
shl    $0x2,%ecx
add    %ecx,%eax
mov    %eax,-0x2c(%rbp)
movl   $0x0,-0x30(%rbp)
mov    -0x30(%rbp),%eax
cmp    -0x24(%rbp),%eax
jge    1266 <imports_ar_find_symbol_section_offset+0x116>
mov    -0x10(%rbp),%rdi
movslq -0x28(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rdi
call   1050 <get_int32_be@plt>
mov    %eax,-0x38(%rbp)
mov    -0x28(%rbp),%eax
add    $0x4,%eax
mov    %eax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
movslq -0x2c(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rdi
call   1030 <strlen@plt>
mov    %rax,%rcx
add    $0x1,%rcx
movslq -0x2c(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%rdi
movslq -0x2c(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rdi
mov    -0x20(%rbp),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
jne    1253 <imports_ar_find_symbol_section_offset+0x103>
mov    -0x38(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    126d <imports_ar_find_symbol_section_offset+0x11d>
jmp    1258 <imports_ar_find_symbol_section_offset+0x108>
mov    -0x30(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x30(%rbp)
jmp    11d5 <imports_ar_find_symbol_section_offset+0x85>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<imports_ar_find_symbol_section_offset>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,(%rsp)
mov    %rdi,%rbp
call   1070 <imports_ar_find_lookup_table@plt>
cmp    $0xffffffff,%eax
je     11e0 <imports_ar_find_symbol_section_offset+0x90>
movslq %eax,%r13
lea    0xf0(,%r13,4),%r12
add    %rbp,%r12
mov    %r12,%rdi
call   1050 <get_int32_be@plt>
mov    %eax,%r15d
mov    $0xffffffff,%eax
test   %r15d,%r15d
jle    11f6 <imports_ar_find_symbol_section_offset+0xa6>
lea    0x4(,%r15,4),%r14d
lea    0x100(%rbp,%r13,4),%rbp
nop
mov    %rbp,%rdi
call   1050 <get_int32_be@plt>
mov    %eax,%r13d
movslq %r14d,%rbx
lea    (%r12,%rbx,4),%rdi
call   1040 <strlen@plt>
lea    (%rbx,%rax,1),%r14d
add    $0x1,%r14d
movslq %r14d,%rax
lea    (%r12,%rax,4),%rdi
mov    (%rsp),%rsi
call   1060 <strcmp@plt>
test   %rax,%rax
je     11f3 <imports_ar_find_symbol_section_offset+0xa3>
add    $0x10,%rbp
add    $0xffffffff,%r15d
jne    11a0 <imports_ar_find_symbol_section_offset+0x50>
jmp    11ec <imports_ar_find_symbol_section_offset+0x9c>
lea    0xe19(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%eax
jmp    11f6 <imports_ar_find_symbol_section_offset+0xa6>
mov    %r13d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [25 x i8] c"Can't find lookup table\0A\00"
define i32 @imports_ar_find_symbol_section_offset(i32* %0, i32 %1, i8* %2) {
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
store i32* %0, i32** %5
store i32 %1, i32* %6
store i8* %2, i8** %7
%14 = load i32*, i32** %5
%15 = load i32, i32* %6
%16 = call i32 @imports_ar_find_lookup_table(i32* %14, i32 %15)
store i32 %16, i32* %12
%17 = load i32, i32* %12
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %21
19: ; preds = %3
%20 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %4
br label %71
21: ; preds = %3
%22 = load i32*, i32** %5
%23 = load i32, i32* %12
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds i32, i32* %22, i64 %24
%26 = getelementptr inbounds i32, i32* %25, i64 60
store i32* %26, i32** %5
%27 = load i32*, i32** %5
%28 = call i32 @get_int32_be(i32* %27)
store i32 %28, i32* %8
store i32 4, i32* %9
%29 = load i32, i32* %9
%30 = load i32, i32* %8
%31 = mul nsw i32 %30, 4
%32 = add nsw i32 %29, %31
store i32 %32, i32* %10
store i32 0, i32* %11
br label %33
33: ; preds = %67, %21
%34 = load i32, i32* %11
%35 = load i32, i32* %8
%36 = icmp slt i32 %34, %35
br i1 %36, label %37, label %70
37: ; preds = %33
%38 = load i32*, i32** %5
%39 = load i32, i32* %9
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds i32, i32* %38, i64 %40
%42 = call i32 @get_int32_be(i32* %41)
store i32 %42, i32* %13
%43 = load i32, i32* %9
%44 = add nsw i32 %43, 4
store i32 %44, i32* %9
%45 = load i32*, i32** %5
%46 = load i32, i32* %10
%47 = sext i32 %46 to i64
%48 = getelementptr inbounds i32, i32* %45, i64 %47
%49 = bitcast i32* %48 to i8*
%50 = call i64 @strlen(i8* %49)
%51 = add nsw i64 %50, 1
%52 = load i32, i32* %10
%53 = sext i32 %52 to i64
%54 = add nsw i64 %53, %51
%55 = trunc i64 %54 to i32
store i32 %55, i32* %10
%56 = load i32*, i32** %5
%57 = load i32, i32* %10
%58 = sext i32 %57 to i64
%59 = getelementptr inbounds i32, i32* %56, i64 %58
%60 = bitcast i32* %59 to i8*
%61 = load i8*, i8** %7
%62 = call i64 @strcmp(i8* %60, i8* %61)
%63 = icmp eq i64 %62, 0
br i1 %63, label %64, label %66
64: ; preds = %37
%65 = load i32, i32* %13
store i32 %65, i32* %4
br label %71
66: ; preds = %37
br label %67
67: ; preds = %66
%68 = load i32, i32* %11
%69 = add nsw i32 %68, 1
store i32 %69, i32* %11
br label %33
70: ; preds = %33
store i32 -1, i32* %4
br label %71
71: ; preds = %70, %64, %19
%72 = load i32, i32* %4
ret i32 %72
}

LLVM IR (-O2):
@str = [24 x i8] c"Can't find lookup table\00"
define i32 @imports_ar_find_symbol_section_offset(i32* %0, i32 %1, i8* %2) {
%4 = tail call i32 @imports_ar_find_lookup_table(i32* %0, i32 %1)
%5 = icmp eq i32 %4, -1
br i1 %5, label %6, label %8
6: ; preds = %3
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @str, i64 0, i64 0))
br label %39
8: ; preds = %3
%9 = sext i32 %4 to i64
%10 = getelementptr inbounds i32, i32* %0, i64 %9
%11 = getelementptr inbounds i32, i32* %10, i64 60
%12 = tail call i32 @get_int32_be(i32* %11)
%13 = icmp sgt i32 %12, 0
br i1 %13, label %14, label %39
14: ; preds = %8
%15 = shl nsw i32 %12, 2
%16 = add nsw i32 %15, 4
br label %17
17: ; preds = %14, %35
%18 = phi i64 [ 4, %14 ], [ %36, %35 ]
%19 = phi i32 [ 0, %14 ], [ %37, %35 ]
%20 = phi i32 [ %16, %14 ], [ %29, %35 ]
%21 = getelementptr inbounds i32, i32* %11, i64 %18
%22 = tail call i32 @get_int32_be(i32* %21)
%23 = sext i32 %20 to i64
%24 = getelementptr inbounds i32, i32* %11, i64 %23
%25 = bitcast i32* %24 to i8*
%26 = tail call i64 @strlen(i8* %25)
%27 = trunc i64 %26 to i32
%28 = add i32 %20, 1
%29 = add i32 %28, %27
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds i32, i32* %11, i64 %30
%32 = bitcast i32* %31 to i8*
%33 = tail call i64 @strcmp(i8* %32, i8* %2)
%34 = icmp eq i64 %33, 0
br i1 %34, label %39, label %35
35: ; preds = %17
%36 = add nuw nsw i64 %18, 4
%37 = add nuw nsw i32 %19, 1
%38 = icmp eq i32 %37, %12
br i1 %38, label %39, label %17
39: ; preds = %17, %35, %8, %6
%40 = phi i32 [ -1, %6 ], [ -1, %8 ], [ %22, %17 ], [ -1, %35 ]
ret i32 %40
}