#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 uint32_t;

/* Variables and functions */
int decode_immediate_shifter_operand(int);
scalar_t__ strcmp(char *, char *);
uint32_t find_func_called_before_string_ref(char *ref_string) {
  int found = 0;
  uint32_t answer = 0;
  for (uint32_t i = 0xFFFE0000; i < 0xFFFFFFF0; i += 4) {
    uint32_t this = (*(volatile uint32_t *)(i));
    uint32_t next = (*(volatile uint32_t *)(i + 4));
    int is_bl = ((this & 0xFF000000) == 0xEB000000);
    int is_string_ref = ((next & 0xFFFFF000) == 0xe28f0000);
    if (is_bl && is_string_ref) {
      uint32_t string_offset = decode_immediate_shifter_operand(next);
      uint32_t pc = i + 4;
      char *string_addr = (char *)(pc + string_offset + 8);
      if (strcmp(string_addr, ref_string) == 0) {
        found++;
        uint32_t func_offset = (this & 0x00FFFFFF) << 2;
        uint32_t pc = i;
        uint32_t func_addr = pc + func_offset + 8;
        if (func_addr > 0xFFFE0000) {
          answer = func_addr;
        }
      }
    }
  }
  if (found == 1) {
    return answer;
  }
  return 0;
}


x86 assembly (clang, -O0):
<find_func_called_before_string_ref>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
movl   $0xfffe0000,-0x1c(%rbp)
cmpl   $0xfffffff0,-0x1c(%rbp)
jae    1238 <find_func_called_before_string_ref+0x118>
movslq -0x1c(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x4,%eax
cltq
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%eax
and    $0xff000000,%eax
cmp    $0xeb000000,%eax
sete   %al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x28(%rbp)
mov    -0x24(%rbp),%eax
and    $0xfffff000,%eax
cmp    $0xe28f0000,%eax
sete   %al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x28(%rbp)
je     1225 <find_func_called_before_string_ref+0x105>
cmpl   $0x0,-0x2c(%rbp)
je     1225 <find_func_called_before_string_ref+0x105>
mov    -0x24(%rbp),%edi
call   1040 <decode_immediate_shifter_operand@plt>
mov    %eax,-0x30(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x4,%eax
mov    %eax,-0x34(%rbp)
mov    -0x34(%rbp),%eax
add    -0x30(%rbp),%eax
add    $0x8,%eax
cltq
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <strcmp@plt>
cmp    $0x0,%rax
jne    1220 <find_func_called_before_string_ref+0x100>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
mov    -0x20(%rbp),%eax
and    $0xffffff,%eax
shl    $0x2,%eax
mov    %eax,-0x44(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x48(%rbp)
mov    -0x48(%rbp),%eax
add    -0x44(%rbp),%eax
add    $0x8,%eax
mov    %eax,-0x4c(%rbp)
cmpl   $0xfffe0000,-0x4c(%rbp)
jbe    121b <find_func_called_before_string_ref+0xfb>
mov    -0x4c(%rbp),%eax
mov    %eax,-0x18(%rbp)
jmp    1220 <find_func_called_before_string_ref+0x100>
jmp    1225 <find_func_called_before_string_ref+0x105>
jmp    122a <find_func_called_before_string_ref+0x10a>
mov    -0x1c(%rbp),%eax
add    $0x4,%eax
mov    %eax,-0x1c(%rbp)
jmp    1141 <find_func_called_before_string_ref+0x21>
cmpl   $0x1,-0x14(%rbp)
jne    124d <find_func_called_before_string_ref+0x12d>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1254 <find_func_called_before_string_ref+0x134>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<find_func_called_before_string_ref>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r12
xor    %r14d,%r14d
mov    $0xfffffffffffe0000,%rbp
mov    $0xfffe0008,%ebx
xor    %r15d,%r15d
jmp    115b <find_func_called_before_string_ref+0x3b>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
add    $0x4,%ebx
lea    -0x4(%rbp),%eax
cmp    $0xffffffec,%eax
jae    11b7 <find_func_called_before_string_ref+0x97>
mov    0x0(%rbp),%r13d
mov    0x4(%rbp),%edi
add    $0x4,%rbp
mov    %r13d,%eax
and    $0xff000000,%eax
cmp    $0xeb000000,%eax
jne    1150 <find_func_called_before_string_ref+0x30>
mov    %edi,%eax
and    $0xfffff000,%eax
cmp    $0xe28f0000,%eax
jne    1150 <find_func_called_before_string_ref+0x30>
call   1040 <decode_immediate_shifter_operand@plt>
add    %ebx,%eax
add    $0x4,%eax
movslq %eax,%rdi
mov    %r12,%rsi
call   1030 <strcmp@plt>
test   %rax,%rax
jne    1150 <find_func_called_before_string_ref+0x30>
add    $0x1,%r14d
and    $0xffffff,%r13d
lea    (%rbx,%r13,4),%eax
cmp    $0xfffe0001,%eax
cmovae %eax,%r15d
jmp    1150 <find_func_called_before_string_ref+0x30>
xor    %eax,%eax
cmp    $0x1,%r14d
cmove  %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @find_func_called_before_string_ref(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i8*
%14 = alloca i32
%15 = alloca i32
%16 = alloca i32
store i8* %0, i8** %3
store i32 0, i32* %4
store i32 0, i32* %5
store i32 -131072, i32* %6
br label %17
17: ; preds = %76, %1
%18 = load i32, i32* %6
%19 = icmp ult i32 %18, -16
br i1 %19, label %20, label %79
20: ; preds = %17
%21 = load i32, i32* %6
%22 = sext i32 %21 to i64
%23 = inttoptr i64 %22 to i32*
%24 = load volatile i32, i32* %23
store i32 %24, i32* %7
%25 = load i32, i32* %6
%26 = add nsw i32 %25, 4
%27 = sext i32 %26 to i64
%28 = inttoptr i64 %27 to i32*
%29 = load volatile i32, i32* %28
store i32 %29, i32* %8
%30 = load i32, i32* %7
%31 = and i32 %30, -16777216
%32 = icmp eq i32 %31, -352321536
%33 = zext i1 %32 to i32
store i32 %33, i32* %9
%34 = load i32, i32* %8
%35 = and i32 %34, -4096
%36 = icmp eq i32 %35, -493944832
%37 = zext i1 %36 to i32
store i32 %37, i32* %10
%38 = load i32, i32* %9
%39 = icmp ne i32 %38, 0
br i1 %39, label %40, label %75
40: ; preds = %20
%41 = load i32, i32* %10
%42 = icmp ne i32 %41, 0
br i1 %42, label %43, label %75
43: ; preds = %40
%44 = load i32, i32* %8
%45 = call i32 @decode_immediate_shifter_operand(i32 %44)
store i32 %45, i32* %11
%46 = load i32, i32* %6
%47 = add nsw i32 %46, 4
store i32 %47, i32* %12
%48 = load i32, i32* %12
%49 = load i32, i32* %11
%50 = add nsw i32 %48, %49
%51 = add nsw i32 %50, 8
%52 = sext i32 %51 to i64
%53 = inttoptr i64 %52 to i8*
store i8* %53, i8** %13
%54 = load i8*, i8** %13
%55 = load i8*, i8** %3
%56 = call i64 @strcmp(i8* %54, i8* %55)
%57 = icmp eq i64 %56, 0
br i1 %57, label %58, label %74
58: ; preds = %43
%59 = load i32, i32* %4
%60 = add nsw i32 %59, 1
store i32 %60, i32* %4
%61 = load i32, i32* %7
%62 = and i32 %61, 16777215
%63 = shl i32 %62, 2
store i32 %63, i32* %14
%64 = load i32, i32* %6
store i32 %64, i32* %15
%65 = load i32, i32* %15
%66 = load i32, i32* %14
%67 = add nsw i32 %65, %66
%68 = add nsw i32 %67, 8
store i32 %68, i32* %16
%69 = load i32, i32* %16
%70 = icmp ugt i32 %69, -131072
br i1 %70, label %71, label %73
71: ; preds = %58
%72 = load i32, i32* %16
store i32 %72, i32* %5
br label %73
73: ; preds = %71, %58
br label %74
74: ; preds = %73, %43
br label %75
75: ; preds = %74, %40, %20
br label %76
76: ; preds = %75
%77 = load i32, i32* %6
%78 = add nsw i32 %77, 4
store i32 %78, i32* %6
br label %17
79: ; preds = %17
%80 = load i32, i32* %4
%81 = icmp eq i32 %80, 1
br i1 %81, label %82, label %84
82: ; preds = %79
%83 = load i32, i32* %5
store i32 %83, i32* %2
br label %85
84: ; preds = %79
store i32 0, i32* %2
br label %85
85: ; preds = %84, %82
%86 = load i32, i32* %2
ret i32 %86
}

LLVM IR (-O2):
define i32 @find_func_called_before_string_ref(i8* %0) {
br label %5
2: ; preds = %37
%3 = icmp eq i32 %39, 1
%4 = select i1 %3, i32 %38, i32 0
ret i32 %4
5: ; preds = %1, %37
%6 = phi i64 [ -131072, %1 ], [ %11, %37 ]
%7 = phi i32 [ 0, %1 ], [ %39, %37 ]
%8 = phi i32 [ 0, %1 ], [ %38, %37 ]
%9 = inttoptr i64 %6 to i32*
%10 = load volatile i32, i32* %9
%11 = add nuw nsw i64 %6, 4
%12 = inttoptr i64 %11 to i32*
%13 = load volatile i32, i32* %12
%14 = and i32 %10, -16777216
%15 = icmp eq i32 %14, -352321536
%16 = and i32 %13, -4096
%17 = icmp eq i32 %16, -493944832
%18 = select i1 %15, i1 %17, i1 false
br i1 %18, label %19, label %37
19: ; preds = %5
%20 = tail call i32 @decode_immediate_shifter_operand(i32 %13)
%21 = trunc i64 %6 to i32
%22 = add i32 %21, 12
%23 = add i32 %22, %20
%24 = sext i32 %23 to i64
%25 = inttoptr i64 %24 to i8*
%26 = tail call i64 @strcmp(i8* %25, i8* %0)
%27 = icmp eq i64 %26, 0
br i1 %27, label %28, label %37
28: ; preds = %19
%29 = add nsw i32 %7, 1
%30 = shl nsw i32 %10, 2
%31 = and i32 %30, 67108860
%32 = trunc i64 %6 to i32
%33 = add i32 %32, 8
%34 = add i32 %33, %31
%35 = icmp ugt i32 %34, -131072
%36 = select i1 %35, i32 %34, i32 %8
br label %37
37: ; preds = %19, %28, %5
%38 = phi i32 [ %8, %5 ], [ %36, %28 ], [ %8, %19 ]
%39 = phi i32 [ %7, %5 ], [ %29, %28 ], [ %7, %19 ]
%40 = trunc i64 %6 to i32
%41 = icmp ult i32 %40, -20
br i1 %41, label %5, label %2
}