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

/* Variables and functions */
int /*<<< orphan*/ memcmp(unsigned char *, char const *, int);
int read_short(unsigned char *);
unsigned char *find_ref_list(unsigned char *map, const char *type,
                             int *num_resources) {
  int i, num_types;
  unsigned char *type_list;
  *num_resources = 0;
  type_list = map + 28;
  num_types = read_short(type_list) + 1;
  for (i = 0, type_list += 2; i < num_types; i++) {
    if (!memcmp(type_list, type, 4)) {
      *num_resources = read_short(type_list + 4) + 1;
      return map + 28 + read_short(type_list + 6);
    }
    type_list += 8;
  }
  return NULL;
}


x86 assembly (clang, -O0):
<find_ref_list>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x20(%rbp),%rax
movl   $0x0,(%rax)
mov    -0x10(%rbp),%rax
add    $0x1c,%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
call   1030 <read_short@plt>
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x30(%rbp),%rax
add    $0x2,%rax
mov    %rax,-0x30(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x28(%rbp),%eax
jge    11f3 <find_ref_list+0xd3>
mov    -0x30(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    $0x4,%edx
call   1040 <memcmp@plt>
cmp    $0x0,%eax
jne    11d9 <find_ref_list+0xb9>
mov    -0x30(%rbp),%rdi
add    $0x4,%rdi
call   1030 <read_short@plt>
mov    %eax,%ecx
add    $0x1,%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x10(%rbp),%rax
add    $0x1c,%rax
mov    %rax,-0x38(%rbp)
mov    -0x30(%rbp),%rdi
add    $0x6,%rdi
call   1030 <read_short@plt>
mov    %eax,%ecx
mov    -0x38(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x8(%rbp)
jmp    11fb <find_ref_list+0xdb>
mov    -0x30(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x30(%rbp)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    116c <find_ref_list+0x4c>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<find_ref_list>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdx,%r14
mov    %rsi,%r12
mov    %rdi,%rbx
movl   $0x0,(%rdx)
lea    0x1c(%rdi),%r15
mov    %r15,%rdi
call   1030 <read_short@plt>
test   %eax,%eax
js     116e <find_ref_list+0x4e>
mov    %eax,%ebp
add    $0x24,%rbx
add    $0x1,%ebp
lea    -0x6(%rbx),%rdi
mov    %r12,%rsi
mov    $0x4,%edx
call   1040 <memcmp@plt>
test   %eax,%eax
je     1173 <find_ref_list+0x53>
add    $0x8,%rbx
add    $0xffffffff,%ebp
jne    1150 <find_ref_list+0x30>
xor    %r15d,%r15d
jmp    118f <find_ref_list+0x6f>
lea    -0x2(%rbx),%rdi
call   1030 <read_short@plt>
add    $0x1,%eax
mov    %eax,(%r14)
mov    %rbx,%rdi
call   1030 <read_short@plt>
cltq
add    %rax,%r15
mov    %r15,%rax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i8* @find_ref_list(i8* %0, i8* %1, i32* %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8*
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32* %2, i32** %7
%11 = load i32*, i32** %7
store i32 0, i32* %11
%12 = load i8*, i8** %5
%13 = getelementptr inbounds i8, i8* %12, i64 28
store i8* %13, i8** %10
%14 = load i8*, i8** %10
%15 = call i32 @read_short(i8* %14)
%16 = add nsw i32 %15, 1
store i32 %16, i32* %9
store i32 0, i32* %8
%17 = load i8*, i8** %10
%18 = getelementptr inbounds i8, i8* %17, i64 2
store i8* %18, i8** %10
br label %19
19: ; preds = %44, %3
%20 = load i32, i32* %8
%21 = load i32, i32* %9
%22 = icmp slt i32 %20, %21
br i1 %22, label %23, label %47
23: ; preds = %19
%24 = load i8*, i8** %10
%25 = load i8*, i8** %6
%26 = call i32 @memcmp(i8* %24, i8* %25, i32 4)
%27 = icmp ne i32 %26, 0
br i1 %27, label %41, label %28
28: ; preds = %23
%29 = load i8*, i8** %10
%30 = getelementptr inbounds i8, i8* %29, i64 4
%31 = call i32 @read_short(i8* %30)
%32 = add nsw i32 %31, 1
%33 = load i32*, i32** %7
store i32 %32, i32* %33
%34 = load i8*, i8** %5
%35 = getelementptr inbounds i8, i8* %34, i64 28
%36 = load i8*, i8** %10
%37 = getelementptr inbounds i8, i8* %36, i64 6
%38 = call i32 @read_short(i8* %37)
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds i8, i8* %35, i64 %39
store i8* %40, i8** %4
br label %48
41: ; preds = %23
%42 = load i8*, i8** %10
%43 = getelementptr inbounds i8, i8* %42, i64 8
store i8* %43, i8** %10
br label %44
44: ; preds = %41
%45 = load i32, i32* %8
%46 = add nsw i32 %45, 1
store i32 %46, i32* %8
br label %19
47: ; preds = %19
store i8* null, i8** %4
br label %48
48: ; preds = %47, %28
%49 = load i8*, i8** %4
ret i8* %49
}

LLVM IR (-O2):
define i8* @find_ref_list(i8* %0, i8* %1, i32* nocapture writeonly %2) {
store i32 0, i32* %2
%4 = getelementptr inbounds i8, i8* %0, i64 28
%5 = tail call i32 @read_short(i8* %4)
%6 = icmp slt i32 %5, 0
br i1 %6, label %26, label %7
7: ; preds = %3
%8 = getelementptr inbounds i8, i8* %0, i64 30
br label %9
9: ; preds = %7, %22
%10 = phi i8* [ %23, %22 ], [ %8, %7 ]
%11 = phi i32 [ %24, %22 ], [ 0, %7 ]
%12 = tail call i32 @memcmp(i8* %10, i8* %1, i32 4)
%13 = icmp eq i32 %12, 0
br i1 %13, label %14, label %22
14: ; preds = %9
%15 = getelementptr inbounds i8, i8* %10, i64 4
%16 = tail call i32 @read_short(i8* %15)
%17 = add nsw i32 %16, 1
store i32 %17, i32* %2
%18 = getelementptr inbounds i8, i8* %10, i64 6
%19 = tail call i32 @read_short(i8* %18)
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds i8, i8* %4, i64 %20
br label %26
22: ; preds = %9
%23 = getelementptr inbounds i8, i8* %10, i64 8
%24 = add nuw i32 %11, 1
%25 = icmp eq i32 %11, %5
br i1 %25, label %26, label %9
26: ; preds = %22, %3, %14
%27 = phi i8* [ %21, %14 ], [ null, %3 ], [ null, %22 ]
ret i8* %27
}