#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_lsearch(const uint8_t *image, size_t len) {
  unsigned long int offset;
  int fmap_found = 0;
  for (offset = 0; offset < len - strlen("__FMAP__"); offset++) {
    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_lsearch>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0x0,-0x24(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x38(%rbp)
lea    0xe9a(%rip),%rdi
call   1030 <strlen@plt>
mov    -0x38(%rbp),%rcx
mov    %rax,%rdx
mov    -0x30(%rbp),%rax
sub    %rdx,%rcx
cmp    %rcx,%rax
jae    11c2 <fmap_lsearch+0x92>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rdi
call   1050 <is_valid_fmap@plt>
cmp    $0x0,%rax
je     11ac <fmap_lsearch+0x7c>
movl   $0x1,-0x24(%rbp)
jmp    11c2 <fmap_lsearch+0x92>
jmp    11b1 <fmap_lsearch+0x81>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    114f <fmap_lsearch+0x1f>
cmpl   $0x0,-0x24(%rbp)
jne    11d9 <fmap_lsearch+0xa9>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    121e <fmap_lsearch+0xee>
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    1216 <fmap_lsearch+0xe6>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    121e <fmap_lsearch+0xee>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fmap_lsearch>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    $0xffffffffffffffff,%r15
cmp    $0x8,%rsi
je     1188 <fmap_lsearch+0x68>
mov    %rsi,%r14
mov    %rdi,%rbx
mov    $0x8,%r13d
sub    %rsi,%r13
xor    %r12d,%r12d
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   1040 <is_valid_fmap@plt>
test   %rax,%rax
jne    116c <fmap_lsearch+0x4c>
add    $0xffffffffffffffff,%r12
add    $0x4,%rbx
cmp    %r12,%r13
jne    1150 <fmap_lsearch+0x30>
jmp    1188 <fmap_lsearch+0x68>
mov    %rbx,%rdi
call   1030 <fmap_size@plt>
sub    %r12,%rax
neg    %r12
cmp    %r14,%rax
mov    $0xffffffffffffffff,%r15
cmovbe %r12,%r15
mov    %r15,%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.fmap = type { i32 }
@.str = [9 x i8] c"__FMAP__\00"
define i64 @fmap_lsearch(i32* %0, i64 %1) {
%3 = alloca i64
%4 = alloca i32*
%5 = alloca i64
%6 = alloca i64
%7 = alloca i32
store i32* %0, i32** %4
store i64 %1, i64* %5
store i32 0, i32* %7
store i64 0, i64* %6
br label %8
8: ; preds = %23, %2
%9 = load i64, i64* %6
%10 = load i64, i64* %5
%11 = call i64 @strlen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
%12 = sub i64 %10, %11
%13 = icmp ult i64 %9, %12
br i1 %13, label %14, label %26
14: ; preds = %8
%15 = load i32*, i32** %4
%16 = load i64, i64* %6
%17 = getelementptr inbounds i32, i32* %15, i64 %16
%18 = bitcast i32* %17 to %struct.fmap*
%19 = call i64 @is_valid_fmap(%struct.fmap* %18)
%20 = icmp ne i64 %19, 0
br i1 %20, label %21, label %22
21: ; preds = %14
store i32 1, i32* %7
br label %26
22: ; preds = %14
br label %23
23: ; preds = %22
%24 = load i64, i64* %6
%25 = add i64 %24, 1
store i64 %25, i64* %6
br label %8
26: ; preds = %21, %8
%27 = load i32, i32* %7
%28 = icmp ne i32 %27, 0
br i1 %28, label %30, label %29
29: ; preds = %26
store i64 -1, i64* %3
br label %43
30: ; preds = %26
%31 = load i64, i64* %6
%32 = load i32*, i32** %4
%33 = load i64, i64* %6
%34 = getelementptr inbounds i32, i32* %32, i64 %33
%35 = bitcast i32* %34 to %struct.fmap*
%36 = call i64 @fmap_size(%struct.fmap* %35)
%37 = add i64 %31, %36
%38 = load i64, i64* %5
%39 = icmp ugt i64 %37, %38
br i1 %39, label %40, label %41
40: ; preds = %30
store i64 -1, i64* %3
br label %43
41: ; preds = %30
%42 = load i64, i64* %6
store i64 %42, i64* %3
br label %43
43: ; preds = %41, %40, %29
%44 = load i64, i64* %3
ret i64 %44
}

LLVM IR (-O2):
%struct.fmap = type { i32 }
define i64 @fmap_lsearch(i32* %0, i64 %1) {
%3 = add i64 %1, -8
%4 = icmp eq i64 %3, 0
br i1 %4, label %20, label %5
5: ; preds = %2, %11
%6 = phi i64 [ %12, %11 ], [ 0, %2 ]
%7 = getelementptr inbounds i32, i32* %0, i64 %6
%8 = bitcast i32* %7 to %struct.fmap*
%9 = tail call i64 @is_valid_fmap(%struct.fmap* %8)
%10 = icmp eq i64 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %5
%12 = add nuw i64 %6, 1
%13 = icmp eq i64 %12, %3
br i1 %13, label %20, label %5
14: ; preds = %5
%15 = bitcast i32* %7 to %struct.fmap*
%16 = tail call i64 @fmap_size(%struct.fmap* %15)
%17 = add i64 %16, %6
%18 = icmp ugt i64 %17, %1
%19 = select i1 %18, i64 -1, i64 %6
br label %20
20: ; preds = %11, %2, %14
%21 = phi i64 [ %19, %14 ], [ -1, %2 ], [ -1, %11 ]
ret i64 %21
}