#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 rand();
unsigned long randbits(int limit) {
  unsigned int num = rand() % limit / 2;
  unsigned int i;
  unsigned long r = 0;
  for (i = 0; i < num; i++)
    r |= (1 << (rand() % (limit - 1)));
  return r;
}


x86 assembly (clang, -O0):
<randbits>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    $0x0,%al
call   1030 <rand@plt>
cltd
idivl  -0x4(%rbp)
mov    %edx,%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,-0x8(%rbp)
movq   $0x0,-0x18(%rbp)
movl   $0x0,-0xc(%rbp)
mov    -0xc(%rbp),%eax
cmp    -0x8(%rbp),%eax
jae    117f <randbits+0x6f>
mov    $0x0,%al
call   1030 <rand@plt>
mov    -0x4(%rbp),%ecx
sub    $0x1,%ecx
cltd
idiv   %ecx
mov    %edx,%ecx
mov    $0x1,%eax
shl    %cl,%eax
cltq
or     -0x18(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    1142 <randbits+0x32>
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<randbits>:
push   %rbp
push   %r14
push   %rbx
mov    %edi,%r14d
xor    %ebx,%ebx
xor    %eax,%eax
call   1030 <rand@plt>
cltd
idiv   %r14d
lea    0x1(%rdx),%eax
cmp    $0x3,%eax
jb     116e <randbits+0x5e>
mov    %edx,%ebp
shr    $0x1f,%ebp
add    %edx,%ebp
sar    %ebp
add    $0xffffffff,%r14d
cmp    $0x2,%ebp
mov    $0x1,%eax
cmovb  %eax,%ebp
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
xor    %eax,%eax
call   1030 <rand@plt>
cltd
idiv   %r14d
mov    %edx,%ecx
mov    $0x1,%eax
shl    %cl,%eax
cltq
or     %rax,%rbx
add    $0xffffffff,%ebp
jne    1150 <randbits+0x40>
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define i64 @randbits(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i64
store i32 %0, i32* %2
%6 = call i32 (...) @rand()
%7 = load i32, i32* %2
%8 = srem i32 %6, %7
%9 = sdiv i32 %8, 2
store i32 %9, i32* %3
store i64 0, i64* %5
store i32 0, i32* %4
br label %10
10: ; preds = %23, %1
%11 = load i32, i32* %4
%12 = load i32, i32* %3
%13 = icmp ult i32 %11, %12
br i1 %13, label %14, label %26
14: ; preds = %10
%15 = call i32 (...) @rand()
%16 = load i32, i32* %2
%17 = sub nsw i32 %16, 1
%18 = srem i32 %15, %17
%19 = shl i32 1, %18
%20 = sext i32 %19 to i64
%21 = load i64, i64* %5
%22 = or i64 %21, %20
store i64 %22, i64* %5
br label %23
23: ; preds = %14
%24 = load i32, i32* %4
%25 = add i32 %24, 1
store i32 %25, i32* %4
br label %10
26: ; preds = %10
%27 = load i64, i64* %5
ret i64 %27
}

LLVM IR (-O2):
define i64 @randbits(i32 %0) {
%2 = tail call i32 (...) @rand()
%3 = srem i32 %2, %0
%4 = add i32 %3, 1
%5 = icmp ult i32 %4, 3
br i1 %5, label %20, label %6
6: ; preds = %1
%7 = sdiv i32 %3, 2
%8 = add nsw i32 %0, -1
%9 = call i32 @llvm.umax.i32(i32 %7, i32 1)
br label %10
10: ; preds = %6, %10
%11 = phi i64 [ 0, %6 ], [ %17, %10 ]
%12 = phi i32 [ 0, %6 ], [ %18, %10 ]
%13 = tail call i32 (...) @rand()
%14 = srem i32 %13, %8
%15 = shl nuw i32 1, %14
%16 = sext i32 %15 to i64
%17 = or i64 %11, %16
%18 = add nuw i32 %12, 1
%19 = icmp eq i32 %18, %9
br i1 %19, label %20, label %10
20: ; preds = %10, %1
%21 = phi i64 [ 0, %1 ], [ %17, %10 ]
ret i64 %21
}