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

/* Variables and functions */
int /*<<< orphan*/ bitmap_zero(unsigned long *, int);
int find_first_bit(unsigned long const *, int);
int find_next_bit(unsigned long const *, int, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ set_bit(int, unsigned long *);
void bitmap_fold(unsigned long *dst, const unsigned long *orig, int sz,
                 int bits) {
  int oldbit;
  if (dst == orig)
    return;
  bitmap_zero(dst, bits);
  for ((oldbit) = find_first_bit((orig), (bits)); (oldbit) < (bits);
       (oldbit) = find_next_bit((orig), (bits), (oldbit) + 1)) {
    printf("oldbit = %d\n", oldbit);
    set_bit(oldbit % sz, dst);
  }
}


x86 assembly (clang, -O0):
<bitmap_fold>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
mov    -0x8(%rbp),%rax
cmp    -0x10(%rbp),%rax
jne    1179 <bitmap_fold+0x29>
jmp    11db <bitmap_fold+0x8b>
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%esi
call   1040 <bitmap_zero@plt>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%esi
call   1070 <find_first_bit@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x18(%rbp),%eax
jge    11db <bitmap_fold+0x8b>
mov    -0x1c(%rbp),%esi
lea    0xe56(%rip),%rdi
call   1030 <printf@plt>
mov    -0x1c(%rbp),%eax
cltd
idivl  -0x14(%rbp)
mov    %edx,%edi
mov    -0x8(%rbp),%rsi
call   1050 <set_bit@plt>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%esi
mov    -0x1c(%rbp),%edx
add    $0x1,%edx
call   1060 <find_next_bit@plt>
mov    %eax,-0x1c(%rbp)
jmp    1194 <bitmap_fold+0x44>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bitmap_fold>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
cmp    %rsi,%rdi
je     11bf <bitmap_fold+0x6f>
mov    %ecx,%ebx
mov    %edx,%r14d
mov    %rsi,%r15
mov    %rdi,%r12
mov    %ecx,%esi
call   1040 <bitmap_zero@plt>
mov    %r15,%rdi
mov    %ebx,%esi
call   1070 <find_first_bit@plt>
cmp    %ebx,%eax
jge    11bf <bitmap_fold+0x6f>
mov    %eax,%ebp
lea    0xe77(%rip),%r13
nopl   0x0(%rax)
mov    %r13,%rdi
mov    %ebp,%esi
call   1030 <printf@plt>
mov    %ebp,%eax
cltd
idiv   %r14d
mov    %edx,%edi
mov    %r12,%rsi
call   1050 <set_bit@plt>
add    $0x1,%ebp
mov    %r15,%rdi
mov    %ebx,%esi
mov    %ebp,%edx
call   1060 <find_next_bit@plt>
mov    %eax,%ebp
cmp    %ebx,%eax
jl     1190 <bitmap_fold+0x40>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [13 x i8] c"oldbit = %d\0A\00"
define void @bitmap_fold(i64* %0, i64* %1, i32 %2, i32 %3) {
%5 = icmp eq i64* %0, %1
br i1 %5, label %18, label %6
6: ; preds = %4
%7 = tail call i32 @bitmap_zero(i64* %0, i32 %3)
%8 = tail call i32 @find_first_bit(i64* %1, i32 %3)
%9 = icmp slt i32 %8, %3
br i1 %9, label %10, label %18
10: ; preds = %6, %10
%11 = phi i32 [ %16, %10 ], [ %8, %6 ]
%12 = tail call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %11)
%13 = srem i32 %11, %2
%14 = tail call i32 @set_bit(i32 %13, i64* %0)
%15 = add nsw i32 %11, 1
%16 = tail call i32 @find_next_bit(i64* %1, i32 %3, i32 %15)
%17 = icmp slt i32 %16, %3
br i1 %17, label %10, label %18
18: ; preds = %10, %6, %4
ret void
}