#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 tlsf_fls(size_t);
void tlsf_mapping_search(size_t size, uint32_t *first_level,
                         uint32_t *second_level) {
  if (size < (1 << 7)) {
    *first_level = 0;
    *second_level = size / ((1 << 7) / (1 << 5));
  } else {
    size += (1 << (tlsf_fls(size) - 5)) - 1;
    *first_level = tlsf_fls(size);
    *second_level = (size >> (*first_level - 5)) - (1 << 5);
    *first_level = *first_level - (7 - 1);
  }
}


x86 assembly (clang, -O0):
<tlsf_mapping_search>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
cmpq   $0x80,-0x8(%rbp)
jae    1151 <tlsf_mapping_search+0x41>
mov    -0x10(%rbp),%rax
movl   $0x0,(%rax)
mov    -0x8(%rbp),%rax
shr    $0x2,%rax
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
jmp    11b1 <tlsf_mapping_search+0xa1>
mov    -0x8(%rbp),%rdi
call   1030 <tlsf_fls@plt>
mov    %eax,%ecx
sub    $0x5,%ecx
mov    $0x1,%eax
shl    %cl,%eax
sub    $0x1,%eax
cltq
add    -0x8(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <tlsf_fls@plt>
mov    %eax,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x8(%rbp),%rax
mov    -0x10(%rbp),%rcx
mov    (%rcx),%ecx
sub    $0x5,%ecx
mov    %ecx,%ecx
shr    %cl,%rax
sub    $0x20,%rax
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x10(%rbp),%rax
mov    (%rax),%ecx
sub    $0x6,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tlsf_mapping_search>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%r12
cmp    $0x7f,%rdi
ja     1137 <tlsf_mapping_search+0x27>
movl   $0x0,(%r15)
shr    $0x2,%r12
mov    %r12d,(%r14)
jmp    116c <tlsf_mapping_search+0x5c>
mov    %r12,%rdi
call   1030 <tlsf_fls@plt>
lea    -0x5(%rax),%ecx
mov    $0xffffffff,%eax
shl    %cl,%eax
not    %eax
movslq %eax,%rbx
add    %r12,%rbx
mov    %rbx,%rdi
call   1030 <tlsf_fls@plt>
mov    %eax,(%r15)
lea    -0x5(%rax),%ecx
shr    %cl,%rbx
add    $0xffffffe0,%ebx
mov    %ebx,(%r14)
addl   $0xfffffffa,(%r15)
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define void @tlsf_mapping_search(i64 %0, i32* %1, i32* %2) {
%4 = alloca i64
%5 = alloca i32*
%6 = alloca i32*
store i64 %0, i64* %4
store i32* %1, i32** %5
store i32* %2, i32** %6
%7 = load i64, i64* %4
%8 = icmp ult i64 %7, 128
br i1 %8, label %9, label %15
9: ; preds = %3
%10 = load i32*, i32** %5
store i32 0, i32* %10
%11 = load i64, i64* %4
%12 = udiv i64 %11, 4
%13 = trunc i64 %12 to i32
%14 = load i32*, i32** %6
store i32 %13, i32* %14
br label %40
15: ; preds = %3
%16 = load i64, i64* %4
%17 = call i32 @tlsf_fls(i64 %16)
%18 = sub nsw i32 %17, 5
%19 = shl i32 1, %18
%20 = sub nsw i32 %19, 1
%21 = sext i32 %20 to i64
%22 = load i64, i64* %4
%23 = add i64 %22, %21
store i64 %23, i64* %4
%24 = load i64, i64* %4
%25 = call i32 @tlsf_fls(i64 %24)
%26 = load i32*, i32** %5
store i32 %25, i32* %26
%27 = load i64, i64* %4
%28 = load i32*, i32** %5
%29 = load i32, i32* %28
%30 = sub nsw i32 %29, 5
%31 = zext i32 %30 to i64
%32 = lshr i64 %27, %31
%33 = sub i64 %32, 32
%34 = trunc i64 %33 to i32
%35 = load i32*, i32** %6
store i32 %34, i32* %35
%36 = load i32*, i32** %5
%37 = load i32, i32* %36
%38 = sub nsw i32 %37, 6
%39 = load i32*, i32** %5
store i32 %38, i32* %39
br label %40
40: ; preds = %15, %9
ret void
}

LLVM IR (-O2):
define void @tlsf_mapping_search(i64 %0, i32* nocapture %1, i32* nocapture writeonly %2) {
%4 = icmp ult i64 %0, 128
br i1 %4, label %5, label %8
5: ; preds = %3
store i32 0, i32* %1
%6 = lshr i64 %0, 2
%7 = trunc i64 %6 to i32
store i32 %7, i32* %2
br label %23
8: ; preds = %3
%9 = tail call i32 @tlsf_fls(i64 %0)
%10 = add nsw i32 %9, -5
%11 = shl nsw i32 -1, %10
%12 = xor i32 %11, -1
%13 = sext i32 %12 to i64
%14 = add i64 %13, %0
%15 = tail call i32 @tlsf_fls(i64 %14)
store i32 %15, i32* %1
%16 = add nsw i32 %15, -5
%17 = zext i32 %16 to i64
%18 = lshr i64 %14, %17
%19 = trunc i64 %18 to i32
%20 = add i32 %19, -32
store i32 %20, i32* %2
%21 = load i32, i32* %1
%22 = add nsw i32 %21, -6
store i32 %22, i32* %1
br label %23
23: ; preds = %8, %5
ret void
}