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


x86 assembly (clang, -O0):
<util_rng_next_int_max>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
xor    %ecx,%ecx
sub    -0x10(%rbp),%rcx
and    %rcx,%rax
cmp    -0x10(%rbp),%rax
jne    115d <util_rng_next_int_max+0x4d>
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    $0x1f,%edi
call   1030 <util_rng_next@plt>
mov    %rax,%rcx
mov    -0x28(%rbp),%rax
imul   %rcx,%rax
sar    $0x1f,%rax
mov    %rax,-0x8(%rbp)
jmp    11a3 <util_rng_next_int_max+0x93>
jmp    1162 <util_rng_next_int_max+0x52>
mov    $0x1f,%edi
call   1030 <util_rng_next@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
cqto
idivq  -0x10(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x18(%rbp),%rax
sub    -0x20(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    $0x1,%rcx
add    %rcx,%rax
cmp    $0x0,%rax
jl     1162 <util_rng_next_int_max+0x52>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<util_rng_next_int_max>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
lea    -0x1(%rdi),%r14
test   %r14,%rdi
jne    1150 <util_rng_next_int_max+0x40>
mov    $0x1f,%edi
call   1030 <util_rng_next@plt>
mov    %rax,%rdx
imul   %rbx,%rdx
sar    $0x1f,%rdx
mov    %rdx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
mov    %rcx,%rax
cqto
idiv   %rbx
add    %r14,%rcx
cmp    %rdx,%rcx
jns    1135 <util_rng_next_int_max+0x25>
mov    $0x1f,%edi
call   1030 <util_rng_next@plt>
mov    %rax,%rcx
or     %rbx,%rax
shr    $0x20,%rax
jne    1140 <util_rng_next_int_max+0x30>
mov    %ecx,%eax
xor    %edx,%edx
div    %ebx
jmp    1148 <util_rng_next_int_max+0x38>

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

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