#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 int_least64_t;

/* Variables and functions */
scalar_t__ floor(int);
int unif_rand();
double rbits(int bits) {
  int_least64_t v = 0;
  for (int n = 0; n <= bits; n += 16) {
    int v1 = (int)floor(unif_rand() * 65536);
    v = 65536 * v + v1;
  }
  return (double)(v & ((1L << bits) - 1));
}


x86 assembly (clang, -O0):
<rbits>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
movl   $0x0,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
mov    -0xc(%rbp),%eax
cmp    -0x4(%rbp),%eax
jg     1173 <rbits+0x53>
mov    $0x0,%al
call   1030 <unif_rand@plt>
mov    %eax,%edi
shl    $0x10,%edi
call   1040 <floor@plt>
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%eax
shl    $0x10,%eax
add    -0x10(%rbp),%eax
mov    %eax,-0x8(%rbp)
mov    -0xc(%rbp),%eax
add    $0x10,%eax
mov    %eax,-0xc(%rbp)
jmp    1139 <rbits+0x19>
movslq -0x8(%rbp),%rax
mov    -0x4(%rbp),%ecx
mov    $0x1,%edx
shl    %cl,%rdx
mov    %rdx,%rcx
sub    $0x1,%rcx
and    %rcx,%rax
cvtsi2sd %rax,%xmm0
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rbits>:
push   %rbp
push   %r14
push   %rbx
mov    %edi,%r14d
xor    %eax,%eax
test   %edi,%edi
js     1161 <rbits+0x41>
xor    %ebp,%ebp
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
xor    %eax,%eax
call   1030 <unif_rand@plt>
shl    $0x10,%eax
mov    %eax,%edi
call   1040 <floor@plt>
shl    $0x10,%ebp
add    %eax,%ebp
add    $0x10,%ebx
cmp    %r14d,%ebx
jle    1140 <rbits+0x20>
movslq %ebp,%rax
mov    $0xffffffffffffffff,%rdx
mov    %r14d,%ecx
shl    %cl,%rdx
not    %rdx
and    %rax,%rdx
cvtsi2sd %rdx,%xmm0
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define double @rbits(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store i32 %0, i32* %2
store i32 0, i32* %3
store i32 0, i32* %4
br label %6
6: ; preds = %19, %1
%7 = load i32, i32* %4
%8 = load i32, i32* %2
%9 = icmp sle i32 %7, %8
br i1 %9, label %10, label %22
10: ; preds = %6
%11 = call i32 (...) @unif_rand()
%12 = mul nsw i32 %11, 65536
%13 = call i64 @floor(i32 %12)
%14 = trunc i64 %13 to i32
store i32 %14, i32* %5
%15 = load i32, i32* %3
%16 = mul nsw i32 65536, %15
%17 = load i32, i32* %5
%18 = add nsw i32 %16, %17
store i32 %18, i32* %3
br label %19
19: ; preds = %10
%20 = load i32, i32* %4
%21 = add nsw i32 %20, 16
store i32 %21, i32* %4
br label %6
22: ; preds = %6
%23 = load i32, i32* %3
%24 = sext i32 %23 to i64
%25 = load i32, i32* %2
%26 = zext i32 %25 to i64
%27 = shl i64 1, %26
%28 = sub nsw i64 %27, 1
%29 = and i64 %24, %28
%30 = sitofp i64 %29 to double
ret double %30
}

LLVM IR (-O2):
define double @rbits(i32 %0) {
%2 = icmp slt i32 %0, 0
br i1 %2, label %5, label %12
3: ; preds = %12
%4 = sext i32 %20 to i64
br label %5
5: ; preds = %3, %1
%6 = phi i64 [ 0, %1 ], [ %4, %3 ]
%7 = zext i32 %0 to i64
%8 = shl nsw i64 -1, %7
%9 = xor i64 %8, -1
%10 = and i64 %6, %9
%11 = sitofp i64 %10 to double
ret double %11
12: ; preds = %1, %12
%13 = phi i32 [ %20, %12 ], [ 0, %1 ]
%14 = phi i32 [ %21, %12 ], [ 0, %1 ]
%15 = tail call i32 (...) @unif_rand()
%16 = shl nsw i32 %15, 16
%17 = tail call i64 @floor(i32 %16)
%18 = trunc i64 %17 to i32
%19 = shl nsw i32 %13, 16
%20 = add nsw i32 %19, %18
%21 = add nuw nsw i32 %14, 16
%22 = icmp sgt i32 %21, %0
br i1 %22, label %3, label %12
}