#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 */
scalar_t__ RAND_MAX;
long random();
long random_at_most(long limit) {
  unsigned long num_bins = (unsigned long)limit + 1,
                num_rand = (unsigned long)RAND_MAX + 1,
                bin_size = num_rand / num_bins, defect = num_rand % num_bins;
  long x;
  do {
    x = random();
  } 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    0x2eb9(%rip),%rax
mov    (%rax),%rax
add    $0x1,%rax
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 <random@plt>
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rax
sub    -0x28(%rbp),%rax
cmp    -0x30(%rbp),%rax
jbe    115a <random_at_most+0x4a>
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    0x2ec9(%rip),%rax
mov    (%rax),%rbx
add    $0x1,%rbx
mov    %rbx,%rax
or     %rdi,%rax
shr    $0x20,%rax
je     113f <random_at_most+0x2f>
mov    %rbx,%rax
xor    %edx,%edx
div    %rdi
mov    %rax,%r14
jmp    1148 <random_at_most+0x38>
mov    %ebx,%eax
xor    %edx,%edx
div    %edi
mov    %eax,%r14d
sub    %rdx,%rbx
nopl   0x0(%rax,%rax,1)
xor    %eax,%eax
call   1030 <random@plt>
cmp    %rax,%rbx
jbe    1150 <random_at_most+0x40>
mov    %rax,%rcx
or     %r14,%rcx
shr    $0x20,%rcx
je     116f <random_at_most+0x5f>
xor    %edx,%edx
div    %r14
jmp    1174 <random_at_most+0x64>
xor    %edx,%edx
div    %r14d
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@RAND_MAX = global i64 0
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
%10 = load i64, i64* @RAND_MAX
%11 = add i64 %10, 1
store i64 %11, i64* %4
%12 = load i64, i64* %4
%13 = load i64, i64* %3
%14 = udiv i64 %12, %13
store i64 %14, i64* %5
%15 = load i64, i64* %4
%16 = load i64, i64* %3
%17 = urem i64 %15, %16
store i64 %17, i64* %6
br label %18
18: ; preds = %20, %1
%19 = call i64 (...) @random()
store i64 %19, i64* %7
br label %20
20: ; preds = %18
%21 = load i64, i64* %4
%22 = load i64, i64* %6
%23 = sub i64 %21, %22
%24 = load i64, i64* %7
%25 = icmp ule i64 %23, %24
br i1 %25, label %18, label %26
26: ; preds = %20
%27 = load i64, i64* %7
%28 = load i64, i64* %5
%29 = udiv i64 %27, %28
ret i64 %29
}

LLVM IR (-O2):
@RAND_MAX = global i64 0
define i64 @random_at_most(i64 %0) {
%2 = add i64 %0, 1
%3 = load i64, i64* @RAND_MAX
%4 = add i64 %3, 1
%5 = urem i64 %4, %2
%6 = udiv i64 %4, %2
%7 = sub i64 %4, %5
br label %8
8: ; preds = %8, %1
%9 = tail call i64 (...) @random()
%10 = icmp ugt i64 %7, %9
br i1 %10, label %11, label %8
11: ; preds = %8
%12 = udiv i64 %9, %6
ret i64 %12
}