#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 find_next_bit(unsigned long const *, int, int);
scalar_t__ snprintf(char *, unsigned int, char *, int);
int bitmap_scneachprintf(char *buf, unsigned int buflen,
                         const unsigned long *mask, int nbits) {
  int len = 0, b = -1;
  if (buflen == 0)
    return 0;
  buf[0] = 0;
  while ((b = find_next_bit(mask, nbits, b + 1)) < nbits)
    len += snprintf(buf + len, buflen - len, "%d ", b);
  return len;
}


x86 assembly (clang, -O0):
<bitmap_scneachprintf>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
movl   $0x0,-0x28(%rbp)
movl   $0xffffffff,-0x2c(%rbp)
cmpl   $0x0,-0x14(%rbp)
jne    115a <bitmap_scneachprintf+0x3a>
movl   $0x0,-0x4(%rbp)
jmp    11b7 <bitmap_scneachprintf+0x97>
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x20(%rbp),%rdi
mov    -0x24(%rbp),%esi
mov    -0x2c(%rbp),%edx
add    $0x1,%edx
call   1040 <find_next_bit@plt>
mov    %eax,-0x2c(%rbp)
cmp    -0x24(%rbp),%eax
jge    11b1 <bitmap_scneachprintf+0x91>
mov    -0x10(%rbp),%rdi
movslq -0x28(%rbp),%rax
add    %rax,%rdi
mov    -0x14(%rbp),%esi
sub    -0x28(%rbp),%esi
mov    -0x2c(%rbp),%ecx
lea    0xe66(%rip),%rdx
call   1030 <snprintf@plt>
mov    %rax,%rcx
movslq -0x28(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x28(%rbp)
jmp    1161 <bitmap_scneachprintf+0x41>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bitmap_scneachprintf>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
test   %esi,%esi
je     119e <bitmap_scneachprintf+0x7e>
mov    %ecx,%r13d
mov    %rdx,%r15
mov    %esi,%r12d
movb   $0x0,(%rdi)
xor    %ebx,%ebx
mov    %rdi,(%rsp)
mov    %rdx,%rdi
mov    %ecx,%esi
xor    %edx,%edx
call   1040 <find_next_bit@plt>
cmp    %r13d,%eax
jge    11a0 <bitmap_scneachprintf+0x80>
mov    %eax,%ebp
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
movslq %ebx,%r14
mov    (%rsp),%rax
lea    (%rax,%r14,1),%rdi
mov    %r12d,%esi
sub    %r14d,%esi
lea    0xe88(%rip),%rdx
mov    %ebp,%ecx
call   1030 <snprintf@plt>
mov    %rax,%rbx
add    %r14d,%ebx
add    $0x1,%ebp
mov    %r15,%rdi
mov    %r13d,%esi
mov    %ebp,%edx
call   1040 <find_next_bit@plt>
mov    %eax,%ebp
cmp    %r13d,%eax
jl     1160 <bitmap_scneachprintf+0x40>
jmp    11a0 <bitmap_scneachprintf+0x80>
xor    %ebx,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [4 x i8] c"%d \00"
define i32 @bitmap_scneachprintf(i8* %0, i32 %1, i64* %2, i32 %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i64*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i8* %0, i8** %6
store i32 %1, i32* %7
store i64* %2, i64** %8
store i32 %3, i32* %9
store i32 0, i32* %10
store i32 -1, i32* %11
%12 = load i32, i32* %7
%13 = icmp eq i32 %12, 0
br i1 %13, label %14, label %15
14: ; preds = %4
store i32 0, i32* %5
br label %42
15: ; preds = %4
%16 = load i8*, i8** %6
%17 = getelementptr inbounds i8, i8* %16, i64 0
store i8 0, i8* %17
br label %18
18: ; preds = %26, %15
%19 = load i64*, i64** %8
%20 = load i32, i32* %9
%21 = load i32, i32* %11
%22 = add nsw i32 %21, 1
%23 = call i32 @find_next_bit(i64* %19, i32 %20, i32 %22)
store i32 %23, i32* %11
%24 = load i32, i32* %9
%25 = icmp slt i32 %23, %24
br i1 %25, label %26, label %40
26: ; preds = %18
%27 = load i8*, i8** %6
%28 = load i32, i32* %10
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds i8, i8* %27, i64 %29
%31 = load i32, i32* %7
%32 = load i32, i32* %10
%33 = sub i32 %31, %32
%34 = load i32, i32* %11
%35 = call i64 @snprintf(i8* %30, i32 %33, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %34)
%36 = load i32, i32* %10
%37 = sext i32 %36 to i64
%38 = add nsw i64 %37, %35
%39 = trunc i64 %38 to i32
store i32 %39, i32* %10
br label %18
40: ; preds = %18
%41 = load i32, i32* %10
store i32 %41, i32* %5
br label %42
42: ; preds = %40, %14
%43 = load i32, i32* %5
ret i32 %43
}

LLVM IR (-O2):
@.str = [4 x i8] c"%d \00"
define i32 @bitmap_scneachprintf(i8* %0, i32 %1, i64* %2, i32 %3) {
%5 = icmp eq i32 %1, 0
br i1 %5, label %21, label %6
6: ; preds = %4
store i8 0, i8* %0
%7 = tail call i32 @find_next_bit(i64* %2, i32 %3, i32 0)
%8 = icmp slt i32 %7, %3
br i1 %8, label %9, label %21
9: ; preds = %6, %9
%10 = phi i32 [ %19, %9 ], [ %7, %6 ]
%11 = phi i32 [ %17, %9 ], [ 0, %6 ]
%12 = sext i32 %11 to i64
%13 = getelementptr inbounds i8, i8* %0, i64 %12
%14 = sub i32 %1, %11
%15 = tail call i64 @snprintf(i8* %13, i32 %14, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %10)
%16 = trunc i64 %15 to i32
%17 = add i32 %11, %16
%18 = add nsw i32 %10, 1
%19 = tail call i32 @find_next_bit(i64* %2, i32 %3, i32 %18)
%20 = icmp slt i32 %19, %3
br i1 %20, label %9, label %21
21: ; preds = %9, %6, %4
%22 = phi i32 [ 0, %4 ], [ 0, %6 ], [ %17, %9 ]
ret i32 %22
}