#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*/ errx(int, char *);
unsigned long intuit_page_offset(unsigned char *img, unsigned long len) {
  unsigned int i, possibilities[256] = {0};
  for (i = 0; i + 4 < len; i++) {
    if (img[i] == 0xA1 && ++possibilities[img[i + 4]] > 3)
      return (unsigned long)img[i + 4] << 24;
  }
  errx(1, "could not determine page offset");
}


x86 assembly (clang, -O0):
<intuit_page_offset>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
lea    -0x420(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1040 <memset@plt>
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x4,%eax
mov    %eax,%eax
cmp    -0x18(%rbp),%rax
jae    11d1 <intuit_page_offset+0xb1>
mov    -0x10(%rbp),%rax
mov    -0x1c(%rbp),%ecx
movzbl (%rax,%rcx,1),%eax
cmp    $0xa1,%eax
jne    11be <intuit_page_offset+0x9e>
mov    -0x10(%rbp),%rax
mov    -0x1c(%rbp),%ecx
add    $0x4,%ecx
mov    %ecx,%ecx
movzbl (%rax,%rcx,1),%eax
mov    %eax,%ecx
mov    -0x420(%rbp,%rcx,4),%eax
add    $0x1,%eax
mov    %eax,-0x420(%rbp,%rcx,4)
cmp    $0x3,%eax
jbe    11be <intuit_page_offset+0x9e>
mov    -0x10(%rbp),%rax
mov    -0x1c(%rbp),%ecx
add    $0x4,%ecx
mov    %ecx,%ecx
movzbl (%rax,%rcx,1),%eax
shl    $0x18,%rax
mov    %rax,-0x8(%rbp)
jmp    11e2 <intuit_page_offset+0xc2>
jmp    11c3 <intuit_page_offset+0xa3>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    114d <intuit_page_offset+0x2d>
mov    $0x1,%edi
lea    0xe23(%rip),%rsi
call   1030 <errx@plt>
mov    -0x8(%rbp),%rax
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<intuit_page_offset>:
push   %r14
push   %rbx
sub    $0x408,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
mov    %rsp,%rdi
mov    $0x400,%edx
xor    %esi,%esi
call   1040 <memset@plt>
cmp    $0x5,%r14
jb     118b <intuit_page_offset+0x6b>
mov    $0x4,%eax
mov    $0x5,%ecx
jmp    116a <intuit_page_offset+0x4a>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %ecx,%eax
add    $0x1,%ecx
cmp    %r14,%rax
jae    118b <intuit_page_offset+0x6b>
lea    -0x5(%rcx),%edx
cmpb   $0xa1,(%rbx,%rdx,1)
jne    1160 <intuit_page_offset+0x40>
movzbl (%rbx,%rax,1),%eax
mov    (%rsp,%rax,4),%edx
add    $0x1,%edx
mov    %edx,(%rsp,%rax,4)
cmp    $0x4,%edx
jb     1160 <intuit_page_offset+0x40>
shl    $0x18,%rax
jmp    119c <intuit_page_offset+0x7c>
lea    0xe6e(%rip),%rsi
mov    $0x1,%edi
call   1030 <errx@plt>
add    $0x408,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [32 x i8] c"could not determine page offset\00"
define i64 @intuit_page_offset(i8* %0, i64 %1) {
%3 = alloca i64
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i32
%7 = alloca [256 x i32]
store i8* %0, i8** %4
store i64 %1, i64* %5
%8 = bitcast [256 x i32]* %7 to i8*
store i32 0, i32* %6
br label %9
9: ; preds = %45, %2
%10 = load i32, i32* %6
%11 = add i32 %10, 4
%12 = zext i32 %11 to i64
%13 = load i64, i64* %5
%14 = icmp ult i64 %12, %13
br i1 %14, label %15, label %48
15: ; preds = %9
%16 = load i8*, i8** %4
%17 = load i32, i32* %6
%18 = zext i32 %17 to i64
%19 = getelementptr inbounds i8, i8* %16, i64 %18
%20 = load i8, i8* %19
%21 = zext i8 %20 to i32
%22 = icmp eq i32 %21, 161
br i1 %22, label %23, label %44
23: ; preds = %15
%24 = load i8*, i8** %4
%25 = load i32, i32* %6
%26 = add i32 %25, 4
%27 = zext i32 %26 to i64
%28 = getelementptr inbounds i8, i8* %24, i64 %27
%29 = load i8, i8* %28
%30 = zext i8 %29 to i64
%31 = getelementptr inbounds [256 x i32], [256 x i32]* %7, i64 0, i64 %30
%32 = load i32, i32* %31
%33 = add i32 %32, 1
store i32 %33, i32* %31
%34 = icmp ugt i32 %33, 3
br i1 %34, label %35, label %44
35: ; preds = %23
%36 = load i8*, i8** %4
%37 = load i32, i32* %6
%38 = add i32 %37, 4
%39 = zext i32 %38 to i64
%40 = getelementptr inbounds i8, i8* %36, i64 %39
%41 = load i8, i8* %40
%42 = zext i8 %41 to i64
%43 = shl i64 %42, 24
store i64 %43, i64* %3
br label %50
44: ; preds = %23, %15
br label %45
45: ; preds = %44
%46 = load i32, i32* %6
%47 = add i32 %46, 1
store i32 %47, i32* %6
br label %9
48: ; preds = %9
%49 = call i32 @errx(i32 1, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
br label %50
50: ; preds = %48, %35
%51 = load i64, i64* %3
ret i64 %51
}

LLVM IR (-O2):
@.str = [32 x i8] c"could not determine page offset\00"
define i64 @intuit_page_offset(i8* nocapture %0, i64 %1) {
%3 = alloca [256 x i32]
%4 = bitcast [256 x i32]* %3 to i8*
%5 = icmp ugt i64 %1, 4
br i1 %5, label %6, label %29
6: ; preds = %2, %24
%7 = phi i64 [ %27, %24 ], [ 4, %2 ]
%8 = phi i32 [ %25, %24 ], [ 0, %2 ]
%9 = zext i32 %8 to i64
%10 = getelementptr inbounds i8, i8* %0, i64 %9
%11 = load i8, i8* %10
%12 = icmp eq i8 %11, -95
br i1 %12, label %13, label %24
13: ; preds = %6
%14 = getelementptr inbounds i8, i8* %0, i64 %7
%15 = load i8, i8* %14
%16 = zext i8 %15 to i64
%17 = getelementptr inbounds [256 x i32], [256 x i32]* %3, i64 0, i64 %16
%18 = load i32, i32* %17
%19 = add i32 %18, 1
store i32 %19, i32* %17
%20 = icmp ugt i32 %19, 3
br i1 %20, label %21, label %24
21: ; preds = %13
%22 = zext i8 %15 to i64
%23 = shl nuw nsw i64 %22, 24
br label %31
24: ; preds = %6, %13
%25 = add i32 %8, 1
%26 = add i32 %8, 5
%27 = zext i32 %26 to i64
%28 = icmp ult i64 %27, %1
br i1 %28, label %6, label %29
29: ; preds = %24, %2
%30 = tail call i32 @errx(i32 1, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
br label %31
31: ; preds = %29, %21
%32 = phi i64 [ %23, %21 ], [ undef, %29 ]
ret i64 %32
}