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


x86 assembly (clang, -O0):
<random_at_most>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
mov    0x2ebd(%rip),%rax
mov    (%rax),%rax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%eax
xor    %edx,%edx
divl   -0x8(%rbp)
mov    %eax,-0x10(%rbp)
mov    -0xc(%rbp),%eax
xor    %edx,%edx
divl   -0x8(%rbp)
mov    %edx,-0x14(%rbp)
mov    $0x0,%al
call   1030 <random@plt>
mov    %eax,-0x18(%rbp)
mov    -0xc(%rbp),%eax
sub    -0x14(%rbp),%eax
cmp    -0x18(%rbp),%eax
jbe    114a <random_at_most+0x3a>
mov    -0x18(%rbp),%eax
xor    %edx,%edx
divl   -0x10(%rbp)
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<random_at_most>:
push   %rbp
push   %rbx
push   %rax
add    $0x1,%edi
mov    0x2ecb(%rip),%rax
mov    (%rax),%ebx
add    $0x1,%ebx
mov    %ebx,%eax
xor    %edx,%edx
div    %edi
mov    %eax,%ebp
sub    %edx,%ebx
nopl   0x0(%rax)
xor    %eax,%eax
call   1030 <random@plt>
cmp    %eax,%ebx
jbe    1130 <random_at_most+0x20>
xor    %edx,%edx
div    %ebp
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

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

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