#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;
struct fmap {
  int dummy;
};

/* Variables and functions */
unsigned long fmap_size(struct fmap const *);
scalar_t__ is_valid_fmap(struct fmap const *);
size_t strlen(char *);
long int fmap_bsearch(const uint8_t *image, size_t len) {
  unsigned long int offset = -1;
  int fmap_found = 0, stride;
  for (stride = len / 2; stride >= 16; stride /= 2) {
    if (fmap_found)
      break;
    for (offset = 0; offset < len - strlen("__FMAP__"); offset += stride) {
      if ((offset % (stride * 2) == 0) && (offset != 0))
        continue;
      if (is_valid_fmap((const struct fmap *)&image[offset])) {
        fmap_found = 1;
        break;
      }
    }
  }
  if (!fmap_found)
    return -1;
  if (offset + fmap_size((const struct fmap *)&image[offset]) > len)
    return -1;
  return offset;
}


x86 assembly (clang, -O0):
<fmap_bsearch>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movq   $0xffffffffffffffff,-0x20(%rbp)
ff
movl   $0x0,-0x24(%rbp)
mov    -0x18(%rbp),%rax
shr    $0x1,%rax
mov    %eax,-0x28(%rbp)
cmpl   $0x10,-0x28(%rbp)
jl     1232 <fmap_bsearch+0x102>
cmpl   $0x0,-0x24(%rbp)
je     1173 <fmap_bsearch+0x43>
jmp    1232 <fmap_bsearch+0x102>
movq   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x38(%rbp)
lea    0xe6e(%rip),%rdi
call   1030 <strlen@plt>
mov    -0x38(%rbp),%rcx
mov    %rax,%rdx
mov    -0x30(%rbp),%rax
sub    %rdx,%rcx
cmp    %rcx,%rax
jae    121a <fmap_bsearch+0xea>
mov    -0x20(%rbp),%rax
mov    -0x28(%rbp),%ecx
shl    $0x1,%ecx
movslq %ecx,%rcx
xor    %edx,%edx
div    %rcx
cmp    $0x0,%rdx
jne    11da <fmap_bsearch+0xaa>
cmpq   $0x0,-0x20(%rbp)
je     11da <fmap_bsearch+0xaa>
jmp    1209 <fmap_bsearch+0xd9>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rdi
call   1050 <is_valid_fmap@plt>
cmp    $0x0,%rax
je     1204 <fmap_bsearch+0xd4>
movl   $0x1,-0x24(%rbp)
jmp    121a <fmap_bsearch+0xea>
jmp    1209 <fmap_bsearch+0xd9>
movslq -0x28(%rbp),%rax
add    -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
jmp    117b <fmap_bsearch+0x4b>
jmp    121f <fmap_bsearch+0xef>
mov    -0x28(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,-0x28(%rbp)
jmp    115a <fmap_bsearch+0x2a>
cmpl   $0x0,-0x24(%rbp)
jne    1249 <fmap_bsearch+0x119>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    128e <fmap_bsearch+0x15e>
mov    -0x20(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rdi
call   1040 <fmap_size@plt>
mov    %rax,%rcx
mov    -0x40(%rbp),%rax
add    %rcx,%rax
cmp    -0x18(%rbp),%rax
jbe    1286 <fmap_bsearch+0x156>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    128e <fmap_bsearch+0x15e>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fmap_bsearch>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rdi,0x20(%rsp)
mov    %rsi,%rcx
shr    %rcx
mov    $0xffffffffffffffff,%rax
mov    %rcx,0x8(%rsp)
cmp    $0x10,%ecx
jl     121f <fmap_bsearch+0xff>
mov    %rsi,0x18(%rsp)
lea    -0x8(%rsi),%r15
jmp    117c <fmap_bsearch+0x5c>
nopl   0x0(%rax)
mov    0x8(%rsp),%rcx
mov    %ecx,%eax
shr    $0x1f,%eax
add    %ecx,%eax
sar    %eax
cmp    $0x20,%ecx
mov    %rax,0x8(%rsp)
jl     122e <fmap_bsearch+0x10e>
test   %r15,%r15
je     1160 <fmap_bsearch+0x40>
mov    0x8(%rsp),%rax
lea    (%rax,%rax,1),%ecx
mov    %ecx,0x14(%rsp)
movslq %ecx,%r12
movslq %eax,%rbp
lea    0x0(,%rbp,4),%r14
mov    0x20(%rsp),%r13
xor    %ebx,%ebx
jmp    11c8 <fmap_bsearch+0xa8>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %r13,%rdi
call   1040 <is_valid_fmap@plt>
test   %rax,%rax
jne    1204 <fmap_bsearch+0xe4>
add    %rbp,%rbx
add    %r14,%r13
cmp    %r15,%rbx
jae    1160 <fmap_bsearch+0x40>
mov    %rbx,%rax
or     %r12,%rax
shr    $0x20,%rax
je     11f0 <fmap_bsearch+0xd0>
mov    %rbx,%rax
xor    %edx,%edx
div    %r12
test   %rbx,%rbx
jne    11fd <fmap_bsearch+0xdd>
jmp    11b0 <fmap_bsearch+0x90>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %ebx,%eax
xor    %edx,%edx
divl   0x14(%rsp)
test   %rbx,%rbx
je     11b0 <fmap_bsearch+0x90>
test   %rdx,%rdx
je     11bd <fmap_bsearch+0x9d>
jmp    11b0 <fmap_bsearch+0x90>
mov    %r13,%rdi
call   1030 <fmap_size@plt>
add    %rbx,%rax
cmp    0x18(%rsp),%rax
mov    $0xffffffffffffffff,%rax
cmovbe %rbx,%rax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0xffffffffffffffff,%rax
jmp    121f <fmap_bsearch+0xff>

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

LLVM IR (-O2):
%struct.fmap = type { i32 }
define i64 @fmap_bsearch(i32* %0, i64 %1) {
%3 = lshr i64 %1, 1
%4 = trunc i64 %3 to i32
%5 = icmp slt i32 %4, 16
br i1 %5, label %39, label %6
6: ; preds = %2
%7 = add i64 %1, -8
%8 = icmp eq i64 %7, 0
br label %9
9: ; preds = %6, %29
%10 = phi i32 [ %4, %6 ], [ %30, %29 ]
br i1 %8, label %29, label %11
11: ; preds = %9
%12 = shl nsw i32 %10, 1
%13 = sext i32 %12 to i64
%14 = sext i32 %10 to i64
br label %15
15: ; preds = %11, %26
%16 = phi i64 [ 0, %11 ], [ %27, %26 ]
%17 = urem i64 %16, %13
%18 = icmp eq i64 %17, 0
%19 = icmp ne i64 %16, 0
%20 = and i1 %19, %18
br i1 %20, label %26, label %21
21: ; preds = %15
%22 = getelementptr inbounds i32, i32* %0, i64 %16
%23 = bitcast i32* %22 to %struct.fmap*
%24 = tail call i64 @is_valid_fmap(%struct.fmap* %23)
%25 = icmp eq i64 %24, 0
br i1 %25, label %26, label %32
26: ; preds = %21, %15
%27 = add i64 %16, %14
%28 = icmp ult i64 %27, %7
br i1 %28, label %15, label %29
29: ; preds = %26, %9
%30 = sdiv i32 %10, 2
%31 = icmp slt i32 %10, 32
br i1 %31, label %39, label %9
32: ; preds = %21
%33 = getelementptr inbounds i32, i32* %0, i64 %16
%34 = bitcast i32* %33 to %struct.fmap*
%35 = tail call i64 @fmap_size(%struct.fmap* %34)
%36 = add i64 %35, %16
%37 = icmp ugt i64 %36, %1
%38 = select i1 %37, i64 -1, i64 %16
br label %39
39: ; preds = %29, %2, %32
%40 = phi i64 [ %38, %32 ], [ -1, %2 ], [ -1, %29 ]
ret i64 %40
}