#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 unsigned long long uint64_t;

/* Variables and functions */
int pop_1st_bit(unsigned long long *);
uint64_t index_to_uint64_t(int index, int bits, uint64_t m) {
  int i, j, k;
  uint64_t result = 0ULL;
  for (i = 0; i < bits; i++) {
    j = pop_1st_bit(&m);
    if (index & (1 << i))
      result |= (1ULL << j);
  }
  return result;
}


x86 assembly (clang, -O0):
<index_to_uint64_t>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %rdx,-0x10(%rbp)
movq   $0x0,-0x28(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    118b <index_to_uint64_t+0x7b>
lea    -0x10(%rbp),%rdi
call   1030 <pop_1st_bit@plt>
mov    %eax,-0x18(%rbp)
mov    -0x4(%rbp),%eax
mov    -0x14(%rbp),%ecx
mov    $0x1,%edx
shl    %cl,%edx
mov    %edx,%ecx
and    %ecx,%eax
cmp    $0x0,%eax
je     1178 <index_to_uint64_t+0x68>
mov    -0x18(%rbp),%eax
mov    %eax,%ecx
mov    $0x1,%eax
shl    %cl,%rax
or     -0x28(%rbp),%rax
mov    %rax,-0x28(%rbp)
jmp    117d <index_to_uint64_t+0x6d>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1131 <index_to_uint64_t+0x21>
mov    -0x28(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<index_to_uint64_t>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rdx,0x8(%rsp)
xor    %ebx,%ebx
test   %esi,%esi
jle    116c <index_to_uint64_t+0x5c>
mov    %esi,%r12d
mov    %edi,%r14d
lea    0x8(%rsp),%r15
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %r15,%rdi
call   1030 <pop_1st_bit@plt>
mov    $0x1,%edx
mov    %eax,%ecx
shl    %cl,%rdx
bt     %ebx,%r14d
mov    $0x0,%eax
cmovb  %rdx,%rax
or     %rax,%rbp
add    $0x1,%ebx
cmp    %ebx,%r12d
jne    1140 <index_to_uint64_t+0x30>
jmp    116e <index_to_uint64_t+0x5e>
xor    %ebp,%ebp
mov    %rbp,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i64 @index_to_uint64_t(i32 %0, i32 %1, i64 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i64
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i64
store i32 %0, i32* %4
store i32 %1, i32* %5
store i64 %2, i64* %6
store i64 0, i64* %10
store i32 0, i32* %7
br label %11
11: ; preds = %29, %3
%12 = load i32, i32* %7
%13 = load i32, i32* %5
%14 = icmp slt i32 %12, %13
br i1 %14, label %15, label %32
15: ; preds = %11
%16 = call i32 @pop_1st_bit(i64* %6)
store i32 %16, i32* %8
%17 = load i32, i32* %4
%18 = load i32, i32* %7
%19 = shl i32 1, %18
%20 = and i32 %17, %19
%21 = icmp ne i32 %20, 0
br i1 %21, label %22, label %28
22: ; preds = %15
%23 = load i32, i32* %8
%24 = zext i32 %23 to i64
%25 = shl i64 1, %24
%26 = load i64, i64* %10
%27 = or i64 %26, %25
store i64 %27, i64* %10
br label %28
28: ; preds = %22, %15
br label %29
29: ; preds = %28
%30 = load i32, i32* %7
%31 = add nsw i32 %30, 1
store i32 %31, i32* %7
br label %11
32: ; preds = %11
%33 = load i64, i64* %10
ret i64 %33
}

LLVM IR (-O2):
define i64 @index_to_uint64_t(i32 %0, i32 %1, i64 %2) {
%4 = alloca i64
store i64 %2, i64* %4
%5 = icmp sgt i32 %1, 0
br i1 %5, label %6, label %19
6: ; preds = %3, %6
%7 = phi i64 [ %16, %6 ], [ 0, %3 ]
%8 = phi i32 [ %17, %6 ], [ 0, %3 ]
%9 = call i32 @pop_1st_bit(i64* %4)
%10 = shl nuw i32 1, %8
%11 = and i32 %10, %0
%12 = icmp eq i32 %11, 0
%13 = zext i32 %9 to i64
%14 = shl nuw i64 1, %13
%15 = select i1 %12, i64 0, i64 %14
%16 = or i64 %15, %7
%17 = add nuw nsw i32 %8, 1
%18 = icmp eq i32 %17, %1
br i1 %18, label %19, label %6
19: ; preds = %6, %3
%20 = phi i64 [ 0, %3 ], [ %16, %6 ]
ret i64 %20
}