#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 */

/* Variables and functions */
int bitmap_find_first_bit(unsigned long const *, int);
scalar_t__ bitmap_test_bit(int, unsigned long const *);
int bitmap_find_next_bit(const unsigned long *maskp, int nmaskbits,
                         int offset) {
  int n;
  if (offset % (8 * sizeof(long)) != 0) {
    n = (((offset) / (8 * sizeof(long))) + 1) * (8 * sizeof(long));
    n = ((n) < (nmaskbits) ? (n) : (nmaskbits));
    while (offset < n) {
      if (bitmap_test_bit(offset, maskp))
        return offset;
      offset++;
    }
  }
  if (offset >= nmaskbits)
    return nmaskbits;
  return offset + bitmap_find_first_bit(maskp + ((offset) / (8 * sizeof(long))),
                                        nmaskbits - offset);
}


x86 assembly (clang, -O0):
<bitmap_find_next_bit>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
movslq -0x18(%rbp),%rax
and    $0x3f,%rax
cmp    $0x0,%rax
je     11ba <bitmap_find_next_bit+0x9a>
movslq -0x18(%rbp),%rax
shr    $0x6,%rax
add    $0x1,%rax
shl    $0x6,%rax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    116e <bitmap_find_next_bit+0x4e>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x20(%rbp)
jmp    1174 <bitmap_find_next_bit+0x54>
mov    -0x14(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    11b5 <bitmap_find_next_bit+0x95>
mov    -0x18(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   1030 <bitmap_test_bit@plt>
cmp    $0x0,%rax
je     11a7 <bitmap_find_next_bit+0x87>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11ff <bitmap_find_next_bit+0xdf>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    117a <bitmap_find_next_bit+0x5a>
jmp    11ba <bitmap_find_next_bit+0x9a>
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jl     11d1 <bitmap_find_next_bit+0xb1>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11ff <bitmap_find_next_bit+0xdf>
mov    -0x18(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
movslq -0x18(%rbp),%rax
shr    $0x6,%rax
shl    $0x3,%rax
add    %rax,%rdi
mov    -0x14(%rbp),%esi
sub    -0x18(%rbp),%esi
call   1040 <bitmap_find_first_bit@plt>
mov    %eax,%ecx
mov    -0x24(%rbp),%eax
add    %ecx,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bitmap_find_next_bit>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%ebx
mov    %esi,%r14d
mov    %rdi,%r15
test   $0x3f,%bl
je     1168 <bitmap_find_next_bit+0x48>
lea    0x40(%rbx),%ebp
and    $0xffffffc0,%ebp
cmp    %r14d,%ebp
cmovge %r14d,%ebp
cmp    %ebx,%ebp
jle    1168 <bitmap_find_next_bit+0x48>
cs nopw 0x0(%rax,%rax,1)
nop
mov    %ebx,%edi
mov    %r15,%rsi
call   1030 <bitmap_test_bit@plt>
test   %rax,%rax
jne    118c <bitmap_find_next_bit+0x6c>
add    $0x1,%ebx
cmp    %ebx,%ebp
jne    1150 <bitmap_find_next_bit+0x30>
mov    %ebp,%ebx
cmp    %r14d,%ebx
jge    1189 <bitmap_find_next_bit+0x69>
movslq %ebx,%rax
shr    $0x6,%rax
lea    (%r15,%rax,8),%rdi
sub    %ebx,%r14d
mov    %r14d,%esi
call   1040 <bitmap_find_first_bit@plt>
add    %ebx,%eax
mov    %eax,%ebx
jmp    118c <bitmap_find_next_bit+0x6c>
mov    %r14d,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @bitmap_find_next_bit(i64* %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i64*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i64* %0, i64** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
%9 = load i32, i32* %7
%10 = sext i32 %9 to i64
%11 = urem i64 %10, 64
%12 = icmp ne i64 %11, 0
br i1 %12, label %13, label %44
13: ; preds = %3
%14 = load i32, i32* %7
%15 = sext i32 %14 to i64
%16 = udiv i64 %15, 64
%17 = add i64 %16, 1
%18 = mul i64 %17, 64
%19 = trunc i64 %18 to i32
store i32 %19, i32* %8
%20 = load i32, i32* %8
%21 = load i32, i32* %6
%22 = icmp slt i32 %20, %21
br i1 %22, label %23, label %25
23: ; preds = %13
%24 = load i32, i32* %8
br label %27
25: ; preds = %13
%26 = load i32, i32* %6
br label %27
27: ; preds = %25, %23
%28 = phi i32 [ %24, %23 ], [ %26, %25 ]
store i32 %28, i32* %8
br label %29
29: ; preds = %40, %27
%30 = load i32, i32* %7
%31 = load i32, i32* %8
%32 = icmp slt i32 %30, %31
br i1 %32, label %33, label %43
33: ; preds = %29
%34 = load i32, i32* %7
%35 = load i64*, i64** %5
%36 = call i64 @bitmap_test_bit(i32 %34, i64* %35)
%37 = icmp ne i64 %36, 0
br i1 %37, label %38, label %40
38: ; preds = %33
%39 = load i32, i32* %7
store i32 %39, i32* %4
br label %62
40: ; preds = %33
%41 = load i32, i32* %7
%42 = add nsw i32 %41, 1
store i32 %42, i32* %7
br label %29
43: ; preds = %29
br label %44
44: ; preds = %43, %3
%45 = load i32, i32* %7
%46 = load i32, i32* %6
%47 = icmp sge i32 %45, %46
br i1 %47, label %48, label %50
48: ; preds = %44
%49 = load i32, i32* %6
store i32 %49, i32* %4
br label %62
50: ; preds = %44
%51 = load i32, i32* %7
%52 = load i64*, i64** %5
%53 = load i32, i32* %7
%54 = sext i32 %53 to i64
%55 = udiv i64 %54, 64
%56 = getelementptr inbounds i64, i64* %52, i64 %55
%57 = load i32, i32* %6
%58 = load i32, i32* %7
%59 = sub nsw i32 %57, %58
%60 = call i32 @bitmap_find_first_bit(i64* %56, i32 %59)
%61 = add nsw i32 %51, %60
store i32 %61, i32* %4
br label %62
62: ; preds = %50, %48, %38
%63 = load i32, i32* %4
ret i32 %63
}

LLVM IR (-O2):
define i32 @bitmap_find_next_bit(i64* %0, i32 %1, i32 %2) {
%4 = and i32 %2, 63
%5 = icmp eq i32 %4, 0
br i1 %5, label %19, label %6
6: ; preds = %3
%7 = add i32 %2, 64
%8 = and i32 %7, -64
%9 = icmp slt i32 %8, %1
%10 = select i1 %9, i32 %8, i32 %1
%11 = icmp sgt i32 %10, %2
br i1 %11, label %12, label %19
12: ; preds = %6, %16
%13 = phi i32 [ %17, %16 ], [ %2, %6 ]
%14 = tail call i64 @bitmap_test_bit(i32 %13, i64* %0)
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %29
16: ; preds = %12
%17 = add nsw i32 %13, 1
%18 = icmp eq i32 %17, %10
br i1 %18, label %19, label %12
19: ; preds = %16, %6, %3
%20 = phi i32 [ %2, %3 ], [ %2, %6 ], [ %10, %16 ]
%21 = icmp slt i32 %20, %1
br i1 %21, label %22, label %29
22: ; preds = %19
%23 = sext i32 %20 to i64
%24 = lshr i64 %23, 6
%25 = getelementptr inbounds i64, i64* %0, i64 %24
%26 = sub nsw i32 %1, %20
%27 = tail call i32 @bitmap_find_first_bit(i64* %25, i32 %26)
%28 = add nsw i32 %27, %20
br label %29
29: ; preds = %12, %19, %22
%30 = phi i32 [ %28, %22 ], [ %1, %19 ], [ %13, %12 ]
ret i32 %30
}