#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 next(int);
int nextInt(int n) {
  if ((n & -n) == n)
    return (int)((n * (long long)next(31)) >> 31);
  int bits, val;
  do {
    bits = next(31);
    val = bits % n;
  } while (bits - val + (n - 1) < 0);
  return val;
}


x86 assembly (clang, -O0):
<nextInt>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    -0x8(%rbp),%eax
xor    %ecx,%ecx
sub    -0x8(%rbp),%ecx
and    %ecx,%eax
cmp    -0x8(%rbp),%eax
jne    1159 <nextInt+0x49>
movslq -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    $0x1f,%edi
call   1030 <next@plt>
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
movslq %ecx,%rcx
imul   %rcx,%rax
sar    $0x1f,%rax
mov    %eax,-0x4(%rbp)
jmp    1192 <nextInt+0x82>
jmp    115e <nextInt+0x4e>
mov    $0x1f,%edi
call   1030 <next@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%eax
cltd
idivl  -0x8(%rbp)
mov    %edx,-0x10(%rbp)
mov    -0xc(%rbp),%eax
sub    -0x10(%rbp),%eax
mov    -0x8(%rbp),%ecx
sub    $0x1,%ecx
add    %ecx,%eax
cmp    $0x0,%eax
jl     115e <nextInt+0x4e>
mov    -0x10(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<nextInt>:
push   %rbp
push   %rbx
push   %rax
mov    %edi,%ebx
lea    -0x1(%rbx),%ebp
test   %ebp,%edi
jne    1140 <nextInt+0x30>
movslq %ebx,%rbx
mov    $0x1f,%edi
call   1030 <next@plt>
movslq %eax,%rdx
imul   %rbx,%rdx
shr    $0x1f,%rdx
jmp    1155 <nextInt+0x45>
cs nopw 0x0(%rax,%rax,1)
mov    $0x1f,%edi
call   1030 <next@plt>
mov    %eax,%ecx
cltd
idiv   %ebx
add    %ebp,%ecx
cmp    %edx,%ecx
js     1140 <nextInt+0x30>
mov    %edx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
define i32 @nextInt(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store i32 %0, i32* %3
%6 = load i32, i32* %3
%7 = load i32, i32* %3
%8 = sub nsw i32 0, %7
%9 = and i32 %6, %8
%10 = load i32, i32* %3
%11 = icmp eq i32 %9, %10
br i1 %11, label %12, label %20
12: ; preds = %1
%13 = load i32, i32* %3
%14 = sext i32 %13 to i64
%15 = call i32 @next(i32 31)
%16 = sext i32 %15 to i64
%17 = mul nsw i64 %14, %16
%18 = ashr i64 %17, 31
%19 = trunc i64 %18 to i32
store i32 %19, i32* %2
br label %36
20: ; preds = %1
br label %21
21: ; preds = %26, %20
%22 = call i32 @next(i32 31)
store i32 %22, i32* %4
%23 = load i32, i32* %4
%24 = load i32, i32* %3
%25 = srem i32 %23, %24
store i32 %25, i32* %5
br label %26
26: ; preds = %21
%27 = load i32, i32* %4
%28 = load i32, i32* %5
%29 = sub nsw i32 %27, %28
%30 = load i32, i32* %3
%31 = sub nsw i32 %30, 1
%32 = add nsw i32 %29, %31
%33 = icmp slt i32 %32, 0
br i1 %33, label %21, label %34
34: ; preds = %26
%35 = load i32, i32* %5
store i32 %35, i32* %2
br label %36
36: ; preds = %34, %12
%37 = load i32, i32* %2
ret i32 %37
}

LLVM IR (-O2):
define i32 @nextInt(i32 %0) {
%2 = tail call i32 @llvm.ctpop.i32(i32 %0)
%3 = icmp ult i32 %2, 2
br i1 %3, label %6, label %4
4: ; preds = %1
%5 = add i32 %0, -1
br label %13
6: ; preds = %1
%7 = sext i32 %0 to i64
%8 = tail call i32 @next(i32 31)
%9 = sext i32 %8 to i64
%10 = mul nsw i64 %9, %7
%11 = lshr i64 %10, 31
%12 = trunc i64 %11 to i32
br label %19
13: ; preds = %4, %13
%14 = tail call i32 @next(i32 31)
%15 = srem i32 %14, %0
%16 = add i32 %5, %14
%17 = sub i32 %16, %15
%18 = icmp slt i32 %17, 0
br i1 %18, label %13, label %19
19: ; preds = %13, %6
%20 = phi i32 [ %12, %6 ], [ %15, %13 ]
ret i32 %20
}