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

/* Variables and functions */
long fmap_bsearch(int /*<<< orphan*/ const *, unsigned int);
long fmap_lsearch(int /*<<< orphan*/ const *, unsigned int);
int popcnt(unsigned int);
long int fmap_find(const uint8_t *image, unsigned int image_len) {
  long int ret = -1;
  if ((image == NULL) || (image_len == 0))
    return -1;
  if (popcnt(image_len) == 1)
    ret = fmap_bsearch(image, image_len);
  else
    ret = fmap_lsearch(image, image_len);
  return ret;
}


x86 assembly (clang, -O0):
<fmap_find>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
movq   $0xffffffffffffffff,-0x20(%rbp)
ff
cmpq   $0x0,-0x10(%rbp)
je     115c <fmap_find+0x2c>
cmpl   $0x0,-0x14(%rbp)
jne    1169 <fmap_find+0x39>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    11a7 <fmap_find+0x77>
mov    -0x14(%rbp),%edi
call   1030 <popcnt@plt>
cmp    $0x1,%eax
jne    118f <fmap_find+0x5f>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1040 <fmap_bsearch@plt>
mov    %rax,-0x20(%rbp)
jmp    119f <fmap_find+0x6f>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1050 <fmap_lsearch@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fmap_find>:
push   %r14
push   %rbx
push   %rax
test   %rdi,%rdi
je     115f <fmap_find+0x2f>
mov    %esi,%ebx
test   %esi,%esi
je     115f <fmap_find+0x2f>
mov    %rdi,%r14
mov    %ebx,%edi
call   1030 <popcnt@plt>
mov    %r14,%rdi
mov    %ebx,%esi
add    $0x8,%rsp
cmp    $0x1,%eax
jne    116e <fmap_find+0x3e>
pop    %rbx
pop    %r14
jmp    1040 <fmap_bsearch@plt>
mov    $0xffffffffffffffff,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
pop    %rbx
pop    %r14
jmp    1050 <fmap_lsearch@plt>

LLVM IR (-O0):
define i64 @fmap_find(i32* %0, i32 %1) {
%3 = alloca i64
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i64
store i32* %0, i32** %4
store i32 %1, i32* %5
store i64 -1, i64* %6
%7 = load i32*, i32** %4
%8 = icmp eq i32* %7, null
br i1 %8, label %12, label %9
9: ; preds = %2
%10 = load i32, i32* %5
%11 = icmp eq i32 %10, 0
br i1 %11, label %12, label %13
12: ; preds = %9, %2
store i64 -1, i64* %3
br label %27
13: ; preds = %9
%14 = load i32, i32* %5
%15 = call i32 @popcnt(i32 %14)
%16 = icmp eq i32 %15, 1
br i1 %16, label %17, label %21
17: ; preds = %13
%18 = load i32*, i32** %4
%19 = load i32, i32* %5
%20 = call i64 @fmap_bsearch(i32* %18, i32 %19)
store i64 %20, i64* %6
br label %25
21: ; preds = %13
%22 = load i32*, i32** %4
%23 = load i32, i32* %5
%24 = call i64 @fmap_lsearch(i32* %22, i32 %23)
store i64 %24, i64* %6
br label %25
25: ; preds = %21, %17
%26 = load i64, i64* %6
store i64 %26, i64* %3
br label %27
27: ; preds = %25, %12
%28 = load i64, i64* %3
ret i64 %28
}

LLVM IR (-O2):
define i64 @fmap_find(i32* %0, i32 %1) {
%3 = icmp eq i32* %0, null
%4 = icmp eq i32 %1, 0
%5 = or i1 %3, %4
br i1 %5, label %13, label %6
6: ; preds = %2
%7 = tail call i32 @popcnt(i32 %1)
%8 = icmp eq i32 %7, 1
br i1 %8, label %9, label %11
9: ; preds = %6
%10 = tail call i64 @fmap_bsearch(i32* %0, i32 %1)
br label %13
11: ; preds = %6
%12 = tail call i64 @fmap_lsearch(i32* %0, i32 %1)
br label %13
13: ; preds = %9, %11, %2
%14 = phi i64 [ -1, %2 ], [ %10, %9 ], [ %12, %11 ]
ret i64 %14
}