#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 MOA_Random_32();
uint32_t MOA_Random_W(int w, int zero_ok) {
  uint32_t b;
  do {
    b = MOA_Random_32();
    if (w == 31)
      b &= 0x7fffffff;
    if (w < 31)
      b %= (1 << w);
  } while (!zero_ok && b == 0);
  return b;
}


x86 assembly (clang, -O0):
<MOA_Random_W>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    $0x0,%al
call   1030 <MOA_Random_32@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x1f,-0x4(%rbp)
jne    113d <MOA_Random_W+0x2d>
mov    -0xc(%rbp),%eax
and    $0x7fffffff,%eax
mov    %eax,-0xc(%rbp)
cmpl   $0x1f,-0x4(%rbp)
jge    115c <MOA_Random_W+0x4c>
mov    -0x4(%rbp),%ecx
mov    $0x1,%eax
shl    %cl,%eax
mov    %eax,%ecx
mov    -0xc(%rbp),%eax
cltd
idiv   %ecx
mov    %edx,-0xc(%rbp)
jmp    1161 <MOA_Random_W+0x51>
xor    %eax,%eax
cmpl   $0x0,-0x8(%rbp)
mov    %al,-0xd(%rbp)
jne    117a <MOA_Random_W+0x6a>
cmpl   $0x0,-0xc(%rbp)
sete   %al
mov    %al,-0xd(%rbp)
mov    -0xd(%rbp),%al
test   $0x1,%al
jne    111e <MOA_Random_W+0xe>
mov    -0xc(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<MOA_Random_W>:
push   %rbp
push   %r14
push   %rbx
mov    %esi,%ebx
mov    %edi,%ebp
mov    $0x1,%r14d
mov    %ebp,%ecx
shl    %cl,%r14d
jmp    1136 <MOA_Random_W+0x26>
cs nopw 0x0(%rax,%rax,1)
nop
mov    %ebx,%ecx
or     %eax,%ecx
jne    1154 <MOA_Random_W+0x44>
xor    %eax,%eax
call   1030 <MOA_Random_32@plt>
mov    %eax,%ecx
and    $0x7fffffff,%eax
cmp    $0x1f,%ebp
cmovne %ecx,%eax
jge    1130 <MOA_Random_W+0x20>
cltd
idiv   %r14d
mov    %edx,%eax
jmp    1130 <MOA_Random_W+0x20>
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define i32 @MOA_Random_W(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store i32 %0, i32* %3
store i32 %1, i32* %4
br label %6
6: ; preds = %28, %2
%7 = call i32 (...) @MOA_Random_32()
store i32 %7, i32* %5
%8 = load i32, i32* %3
%9 = icmp eq i32 %8, 31
br i1 %9, label %10, label %13
10: ; preds = %6
%11 = load i32, i32* %5
%12 = and i32 %11, 2147483647
store i32 %12, i32* %5
br label %13
13: ; preds = %10, %6
%14 = load i32, i32* %3
%15 = icmp slt i32 %14, 31
br i1 %15, label %16, label %21
16: ; preds = %13
%17 = load i32, i32* %3
%18 = shl i32 1, %17
%19 = load i32, i32* %5
%20 = srem i32 %19, %18
store i32 %20, i32* %5
br label %21
21: ; preds = %16, %13
br label %22
22: ; preds = %21
%23 = load i32, i32* %4
%24 = icmp ne i32 %23, 0
br i1 %24, label %28, label %25
25: ; preds = %22
%26 = load i32, i32* %5
%27 = icmp eq i32 %26, 0
br label %28
28: ; preds = %25, %22
%29 = phi i1 [ false, %22 ], [ %27, %25 ]
br i1 %29, label %6, label %30
30: ; preds = %28
%31 = load i32, i32* %5
ret i32 %31
}

LLVM IR (-O2):
define i32 @MOA_Random_W(i32 %0, i32 %1) {
%3 = icmp eq i32 %0, 31
%4 = icmp slt i32 %0, 31
%5 = shl nuw nsw i32 1, %0
%6 = icmp eq i32 %1, 0
br label %7
7: ; preds = %13, %2
%8 = tail call i32 (...) @MOA_Random_32()
%9 = and i32 %8, 2147483647
%10 = select i1 %3, i32 %9, i32 %8
br i1 %4, label %11, label %13
11: ; preds = %7
%12 = srem i32 %10, %5
br label %13
13: ; preds = %7, %11
%14 = phi i32 [ %12, %11 ], [ %10, %7 ]
%15 = icmp eq i32 %14, 0
%16 = select i1 %6, i1 %15, i1 false
br i1 %16, label %7, label %17
17: ; preds = %13
ret i32 %14
}