#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 */
struct radix_tree_root {
  int dummy;
};
typedef int /*<<< orphan*/ gang;

/* Variables and functions */
int find_next_bit(unsigned long *, int, int);
int radix_tree_gang_lookup(struct radix_tree_root *, void *, unsigned long,
                           int);
int find_first_radix_bit(struct radix_tree_root *radix, unsigned long *retbits,
                         unsigned long start, int nr) {
  unsigned long *bits;
  unsigned long *gang[4];
  int found;
  int ret;
  int i;
  int total_found = 0;
  unsigned long slot;
  slot = start / ((256 - sizeof(unsigned long)) * 8);
  ret = radix_tree_gang_lookup(radix, (void *)gang, slot,
                               (sizeof(gang) / sizeof((gang)[0])));
  found = start % ((256 - sizeof(unsigned long)) * 8);
  for (i = 0; i < ret && nr > 0; i++) {
    bits = gang[i];
    while (nr > 0) {
      found =
          find_next_bit(bits + 1, ((256 - sizeof(unsigned long)) * 8), found);
      if (found < ((256 - sizeof(unsigned long)) * 8)) {
        *retbits = bits[0] * ((256 - sizeof(unsigned long)) * 8) + found;
        retbits++;
        nr--;
        total_found++;
        found++;
      } else
        break;
    }
    found = 0;
  }
  return total_found;
}


x86 assembly (clang, -O0):
<find_first_radix_bit>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
movl   $0x0,-0x60(%rbp)
mov    -0x18(%rbp),%rax
mov    $0x7c0,%ecx
xor    %edx,%edx
div    %rcx
mov    %rax,-0x68(%rbp)
mov    -0x8(%rbp),%rdi
lea    -0x50(%rbp),%rsi
mov    -0x68(%rbp),%rdx
mov    $0x4,%ecx
call   1030 <radix_tree_gang_lookup@plt>
mov    %eax,-0x58(%rbp)
mov    -0x18(%rbp),%rax
mov    $0x7c0,%ecx
xor    %edx,%edx
div    %rcx
mov    %edx,%eax
mov    %eax,-0x54(%rbp)
movl   $0x0,-0x5c(%rbp)
mov    -0x5c(%rbp),%ecx
xor    %eax,%eax
cmp    -0x58(%rbp),%ecx
mov    %al,-0x69(%rbp)
jge    119e <find_first_radix_bit+0x7e>
cmpl   $0x0,-0x1c(%rbp)
setg   %al
mov    %al,-0x69(%rbp)
mov    -0x69(%rbp),%al
test   $0x1,%al
jne    11ae <find_first_radix_bit+0x8e>
jmp    1251 <find_first_radix_bit+0x131>
movslq -0x5c(%rbp),%rax
mov    -0x50(%rbp,%rax,8),%rax
mov    %rax,-0x28(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jle    123c <find_first_radix_bit+0x11c>
mov    -0x28(%rbp),%rdi
add    $0x8,%rdi
mov    -0x54(%rbp),%edx
mov    $0x7c0,%esi
call   1040 <find_next_bit@plt>
mov    %eax,-0x54(%rbp)
movslq -0x54(%rbp),%rax
cmp    $0x7c0,%rax
jae    1232 <find_first_radix_bit+0x112>
mov    -0x28(%rbp),%rax
imul   $0x7c0,(%rax),%rcx
movslq -0x54(%rbp),%rax
add    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x10(%rbp)
mov    -0x1c(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x60(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x60(%rbp)
mov    -0x54(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x54(%rbp)
jmp    1237 <find_first_radix_bit+0x117>
jmp    123c <find_first_radix_bit+0x11c>
jmp    11bb <find_first_radix_bit+0x9b>
movl   $0x0,-0x54(%rbp)
mov    -0x5c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x5c(%rbp)
jmp    1183 <find_first_radix_bit+0x63>
mov    -0x60(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<find_first_radix_bit>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x48,%rsp
mov    %ecx,%r14d
mov    %rdx,%rbp
mov    %rsi,%r15
mov    %rdx,%rax
shr    $0x6,%rax
movabs $0x842108421084211,%rcx
10 42 08
mul    %rcx
mov    %rdx,%r12
lea    0x20(%rsp),%rsi
mov    $0x4,%ecx
call   1030 <radix_tree_gang_lookup@plt>
xor    %ebx,%ebx
test   %eax,%eax
jle    1214 <find_first_radix_bit+0xf4>
test   %r14d,%r14d
jle    1214 <find_first_radix_bit+0xf4>
imul   $0x7c0,%r12,%rcx
sub    %rcx,%rbp
mov    %eax,%eax
mov    %rax,0x10(%rsp)
xor    %eax,%eax
movl   $0x0,0x8(%rsp)
xor    %ebx,%ebx
mov    %rax,0x18(%rsp)
mov    0x20(%rsp,%rax,8),%r13
lea    0x8(%r13),%r12
mov    %r14d,0xc(%rsp)
mov    %r12,%rdi
mov    $0x7c0,%esi
mov    %ebp,%edx
call   1040 <find_next_bit@plt>
cmp    $0x7bf,%eax
ja     11f0 <find_first_radix_bit+0xd0>
mov    %eax,%ebp
imul   $0x7c0,0x0(%r13),%rax
mov    %ebp,%ecx
add    %rcx,%rax
mov    %rax,(%r15)
add    $0x8,%r15
lea    -0x1(%r14),%eax
add    $0x1,%ebx
add    $0x1,%ebp
cmp    $0x1,%r14d
mov    %eax,%r14d
jg     11a0 <find_first_radix_bit+0x80>
jmp    120c <find_first_radix_bit+0xec>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    0x18(%rsp),%rax
add    $0x1,%rax
xor    %ebp,%ebp
mov    %ebx,0x8(%rsp)
cmp    0x10(%rsp),%rax
jb     118d <find_first_radix_bit+0x6d>
jmp    1214 <find_first_radix_bit+0xf4>
mov    0xc(%rsp),%ebx
add    0x8(%rsp),%ebx
mov    %ebx,%eax
add    $0x48,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.radix_tree_root = type { i32 }
define i32 @find_first_radix_bit(%struct.radix_tree_root* %0, i64* nocapture writeonly %1, i64 %2, i32 %3) {
%5 = alloca [4 x i64*]
%6 = bitcast [4 x i64*]* %5 to i8*
%7 = udiv i64 %2, 1984
%8 = urem i64 %2, 1984
%9 = call i32 @radix_tree_gang_lookup(%struct.radix_tree_root* %0, i8* %6, i64 %7, i32 4)
%10 = icmp sgt i32 %9, 0
%11 = icmp sgt i32 %3, 0
%12 = and i1 %10, %11
br i1 %12, label %13, label %47
13: ; preds = %4
%14 = trunc i64 %8 to i32
%15 = zext i32 %9 to i64
br label %16
16: ; preds = %42, %13
%17 = phi i64 [ 0, %13 ], [ %43, %42 ]
%18 = phi i64* [ %1, %13 ], [ %26, %42 ]
%19 = phi i32 [ 0, %13 ], [ %27, %42 ]
%20 = phi i32 [ %3, %13 ], [ %28, %42 ]
%21 = phi i32 [ %14, %13 ], [ 0, %42 ]
%22 = getelementptr inbounds [4 x i64*], [4 x i64*]* %5, i64 0, i64 %17
%23 = load i64*, i64** %22
%24 = getelementptr inbounds i64, i64* %23, i64 1
br label %25
25: ; preds = %16, %32
%26 = phi i64* [ %37, %32 ], [ %18, %16 ]
%27 = phi i32 [ %39, %32 ], [ %19, %16 ]
%28 = phi i32 [ %38, %32 ], [ %20, %16 ]
%29 = phi i32 [ %40, %32 ], [ %21, %16 ]
%30 = call i32 @find_next_bit(i64* %24, i32 1984, i32 %29)
%31 = icmp ult i32 %30, 1984
br i1 %31, label %32, label %42
32: ; preds = %25
%33 = zext i32 %30 to i64
%34 = load i64, i64* %23
%35 = mul i64 %34, 1984
%36 = add i64 %35, %33
store i64 %36, i64* %26
%37 = getelementptr inbounds i64, i64* %26, i64 1
%38 = add nsw i32 %28, -1
%39 = add nsw i32 %27, 1
%40 = add nuw nsw i32 %30, 1
%41 = icmp sgt i32 %28, 1
br i1 %41, label %25, label %45
42: ; preds = %25
%43 = add nuw nsw i64 %17, 1
%44 = icmp ult i64 %43, %15
br i1 %44, label %16, label %47
45: ; preds = %32
%46 = add i32 %20, %19
br label %47
47: ; preds = %42, %45, %4
%48 = phi i32 [ 0, %4 ], [ %46, %45 ], [ %27, %42 ]
ret i32 %48
}