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

/* Variables and functions */
int RAND_MAX;
int rand();
uint32_t rnd32() {
  uint32_t r = 0;
  int h = RAND_MAX >> 1;
  for (int i = 0; i < 32; i++) {
    int n = rand();
    int b = 0;
    if (n > h) {
      b = 1;
    }
    r = r << 1;
    r += b;
  }
  return r;
}


x86 assembly (clang, -O0):
<rnd32>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2ec2(%rip),%rax
mov    (%rax),%eax
sar    $0x1,%eax
mov    %eax,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
cmpl   $0x20,-0xc(%rbp)
jge    1183 <rnd32+0x73>
mov    $0x0,%al
call   1030 <rand@plt>
mov    %eax,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x10(%rbp),%eax
cmp    -0x8(%rbp),%eax
jle    1163 <rnd32+0x53>
movl   $0x1,-0x14(%rbp)
mov    -0x4(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x4(%rbp)
mov    -0x14(%rbp),%eax
add    -0x4(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    1135 <rnd32+0x25>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rnd32>:
push   %rbp
push   %r14
push   %rbx
mov    0x2ecd(%rip),%rax
mov    (%rax),%r14d
sar    %r14d
mov    $0x20,%ebp
xor    %ebx,%ebx
nopl   0x0(%rax,%rax,1)
xor    %eax,%eax
call   1030 <rand@plt>
xor    %ecx,%ecx
cmp    %r14d,%eax
setg   %cl
lea    (%rcx,%rbx,2),%ebx
add    $0xffffffff,%ebp
jne    1130 <rnd32+0x20>
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@RAND_MAX = global i32 0
define i32 @rnd32() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store i32 0, i32* %1
%6 = load i32, i32* @RAND_MAX
%7 = ashr i32 %6, 1
store i32 %7, i32* %2
store i32 0, i32* %3
br label %8
8: ; preds = %23, %0
%9 = load i32, i32* %3
%10 = icmp slt i32 %9, 32
br i1 %10, label %11, label %26
11: ; preds = %8
%12 = call i32 (...) @rand()
store i32 %12, i32* %4
store i32 0, i32* %5
%13 = load i32, i32* %4
%14 = load i32, i32* %2
%15 = icmp sgt i32 %13, %14
br i1 %15, label %16, label %17
16: ; preds = %11
store i32 1, i32* %5
br label %17
17: ; preds = %16, %11
%18 = load i32, i32* %1
%19 = shl i32 %18, 1
store i32 %19, i32* %1
%20 = load i32, i32* %5
%21 = load i32, i32* %1
%22 = add nsw i32 %21, %20
store i32 %22, i32* %1
br label %23
23: ; preds = %17
%24 = load i32, i32* %3
%25 = add nsw i32 %24, 1
store i32 %25, i32* %3
br label %8
26: ; preds = %8
%27 = load i32, i32* %1
ret i32 %27
}

LLVM IR (-O2):
@RAND_MAX = global i32 0
define i32 @rnd32() {
%1 = load i32, i32* @RAND_MAX
%2 = ashr i32 %1, 1
br label %4
3: ; preds = %4
ret i32 %11
4: ; preds = %0, %4
%5 = phi i32 [ 0, %0 ], [ %11, %4 ]
%6 = phi i32 [ 0, %0 ], [ %12, %4 ]
%7 = tail call i32 (...) @rand()
%8 = icmp sgt i32 %7, %2
%9 = zext i1 %8 to i32
%10 = shl i32 %5, 1
%11 = or i32 %10, %9
%12 = add nuw nsw i32 %6, 1
%13 = icmp eq i32 %12, 32
br i1 %13, label %3, label %4
}