#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 append_bit(int, int);
int kth_bit_from_right(int, int);
int bits_in_range(int n, int k1, int k2) {
  int i;
  int result = 0;
  for (i = k2 - 1; i >= k1; i--) {
    int b = kth_bit_from_right(n, i);
    result = append_bit(result, b);
  }
  return result;
}


x86 assembly (clang, -O0):
<bits_in_range>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %edx,-0xc(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0xc(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%eax
cmp    -0x8(%rbp),%eax
jl     1177 <bits_in_range+0x57>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%esi
call   1040 <kth_bit_from_right@plt>
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%edi
mov    -0x18(%rbp),%esi
call   1030 <append_bit@plt>
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x10(%rbp)
jmp    1141 <bits_in_range+0x21>
mov    -0x14(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bits_in_range>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
xor    %ebp,%ebp
cmp    %esi,%edx
jle    115d <bits_in_range+0x3d>
mov    %edx,%ebx
mov    %esi,%r15d
mov    %edi,%r14d
xor    %ebp,%ebp
nopw   0x0(%rax,%rax,1)
add    $0xffffffff,%ebx
mov    %r14d,%edi
mov    %ebx,%esi
call   1040 <kth_bit_from_right@plt>
mov    %ebp,%edi
mov    %eax,%esi
call   1030 <append_bit@plt>
mov    %eax,%ebp
cmp    %r15d,%ebx
jg     1140 <bits_in_range+0x20>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @bits_in_range(i32 %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
store i32 %2, i32* %6
store i32 0, i32* %8
%10 = load i32, i32* %6
%11 = sub nsw i32 %10, 1
store i32 %11, i32* %7
br label %12
12: ; preds = %23, %3
%13 = load i32, i32* %7
%14 = load i32, i32* %5
%15 = icmp sge i32 %13, %14
br i1 %15, label %16, label %26
16: ; preds = %12
%17 = load i32, i32* %4
%18 = load i32, i32* %7
%19 = call i32 @kth_bit_from_right(i32 %17, i32 %18)
store i32 %19, i32* %9
%20 = load i32, i32* %8
%21 = load i32, i32* %9
%22 = call i32 @append_bit(i32 %20, i32 %21)
store i32 %22, i32* %8
br label %23
23: ; preds = %16
%24 = load i32, i32* %7
%25 = add nsw i32 %24, -1
store i32 %25, i32* %7
br label %12
26: ; preds = %12
%27 = load i32, i32* %8
ret i32 %27
}

LLVM IR (-O2):
define i32 @bits_in_range(i32 %0, i32 %1, i32 %2) {
%4 = icmp sgt i32 %2, %1
br i1 %4, label %5, label %12
5: ; preds = %3, %5
%6 = phi i32 [ %8, %5 ], [ %2, %3 ]
%7 = phi i32 [ %10, %5 ], [ 0, %3 ]
%8 = add nsw i32 %6, -1
%9 = tail call i32 @kth_bit_from_right(i32 %0, i32 %8)
%10 = tail call i32 @append_bit(i32 %7, i32 %9)
%11 = icmp sgt i32 %8, %1
br i1 %11, label %5, label %12
12: ; preds = %5, %3
%13 = phi i32 [ 0, %3 ], [ %10, %5 ]
ret i32 %13
}