#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 genrand();
long random_at_most(long max) {
  unsigned long num_bins = (unsigned long)max + 1,
                num_rand = (unsigned long)0x7fffffff + 1,
                bin_size = num_rand / num_bins, defect = num_rand % num_bins;
  long x;
  do {
    x = genrand();
  } while (num_rand - defect <= (unsigned long)x);
  return x / bin_size;
}


x86 assembly (clang, -O0):
<random_at_most>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
mov    $0x80000000,%eax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
xor    %ecx,%ecx
mov    %ecx,%edx
divq   -0x10(%rbp)
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rax
xor    %ecx,%ecx
mov    %ecx,%edx
divq   -0x10(%rbp)
mov    %rdx,-0x28(%rbp)
mov    $0x0,%al
call   1030 <genrand@plt>
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rax
sub    -0x28(%rbp),%rax
cmp    -0x30(%rbp),%rax
jbe    1151 <random_at_most+0x41>
mov    -0x30(%rbp),%rax
xor    %ecx,%ecx
mov    %ecx,%edx
divq   -0x20(%rbp)
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<random_at_most>:
push   %r14
push   %rbx
push   %rax
add    $0x1,%rdi
mov    $0x80000000,%ebx
cmp    %rbx,%rdi
ja     1130 <random_at_most+0x20>
mov    $0x80000000,%eax
xor    %edx,%edx
div    %edi
mov    %eax,%r14d
jmp    1138 <random_at_most+0x28>
xor    %r14d,%r14d
mov    $0x80000000,%edx
sub    %rdx,%rbx
nopl   0x0(%rax,%rax,1)
xor    %eax,%eax
call   1030 <genrand@plt>
cmp    %rax,%rbx
jbe    1140 <random_at_most+0x30>
mov    %rax,%rcx
shr    $0x20,%rcx
je     115c <random_at_most+0x4c>
xor    %edx,%edx
div    %r14
jmp    1161 <random_at_most+0x51>
xor    %edx,%edx
div    %r14d
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
define i64 @random_at_most(i64 %0) {
%2 = alloca i64
%3 = alloca i64
%4 = alloca i64
%5 = alloca i64
%6 = alloca i64
%7 = alloca i64
store i64 %0, i64* %2
%8 = load i64, i64* %2
%9 = add i64 %8, 1
store i64 %9, i64* %3
store i64 2147483648, i64* %4
%10 = load i64, i64* %4
%11 = load i64, i64* %3
%12 = udiv i64 %10, %11
store i64 %12, i64* %5
%13 = load i64, i64* %4
%14 = load i64, i64* %3
%15 = urem i64 %13, %14
store i64 %15, i64* %6
br label %16
16: ; preds = %18, %1
%17 = call i64 (...) @genrand()
store i64 %17, i64* %7
br label %18
18: ; preds = %16
%19 = load i64, i64* %4
%20 = load i64, i64* %6
%21 = sub i64 %19, %20
%22 = load i64, i64* %7
%23 = icmp ule i64 %21, %22
br i1 %23, label %16, label %24
24: ; preds = %18
%25 = load i64, i64* %7
%26 = load i64, i64* %5
%27 = udiv i64 %25, %26
ret i64 %27
}

LLVM IR (-O2):
define i64 @random_at_most(i64 %0) {
%2 = add i64 %0, 1
%3 = urem i64 2147483648, %2
%4 = udiv i64 2147483648, %2
%5 = sub nuw nsw i64 2147483648, %3
br label %6
6: ; preds = %6, %1
%7 = tail call i64 (...) @genrand()
%8 = icmp ugt i64 %5, %7
br i1 %8, label %9, label %6
9: ; preds = %6
%10 = udiv i64 %7, %4
ret i64 %10
}