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


x86 assembly (clang, -O0):
<random_at_most>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%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)
movl   $0x0,-0x2c(%rbp)
mov    $0x0,%al
call   1030 <real_rand@plt>
mov    %rax,-0x38(%rbp)
mov    -0x18(%rbp),%rcx
sub    -0x28(%rbp),%rcx
xor    %eax,%eax
cmp    -0x38(%rbp),%rcx
mov    %al,-0x39(%rbp)
ja     118e <random_at_most+0x7e>
mov    -0x2c(%rbp),%eax
mov    %eax,%ecx
add    $0x1,%ecx
mov    %ecx,-0x2c(%rbp)
cmp    $0x64,%eax
setl   %al
mov    %al,-0x39(%rbp)
mov    -0x39(%rbp),%al
test   $0x1,%al
jne    1158 <random_at_most+0x48>
mov    -0x38(%rbp),%rax
xor    %ecx,%ecx
mov    %ecx,%edx
divq   -0x20(%rbp)
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<random_at_most>:
push   %r15
push   %r14
push   %rbx
add    $0x1,%rdi
mov    $0x80000000,%r15d
cmp    %r15,%rdi
ja     1132 <random_at_most+0x22>
mov    $0x80000000,%eax
xor    %edx,%edx
div    %edi
mov    %eax,%r14d
jmp    113a <random_at_most+0x2a>
xor    %r14d,%r14d
mov    $0x80000000,%edx
sub    %rdx,%r15
xor    %ebx,%ebx
nop
xor    %eax,%eax
call   1030 <real_rand@plt>
cmp    %rax,%r15
ja     1156 <random_at_most+0x46>
lea    0x1(%rbx),%ecx
cmp    $0x64,%ebx
mov    %ecx,%ebx
jb     1140 <random_at_most+0x30>
mov    %rax,%rcx
shr    $0x20,%rcx
je     1166 <random_at_most+0x56>
xor    %edx,%edx
div    %r14
jmp    116b <random_at_most+0x5b>
xor    %edx,%edx
div    %r14d
pop    %rbx
pop    %r14
pop    %r15
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 i32
%8 = alloca i64
store i64 %0, i64* %2
%9 = load i64, i64* %2
%10 = add i64 %9, 1
store i64 %10, i64* %3
store i64 2147483648, i64* %4
%11 = load i64, i64* %4
%12 = load i64, i64* %3
%13 = udiv i64 %11, %12
store i64 %13, i64* %5
%14 = load i64, i64* %4
%15 = load i64, i64* %3
%16 = urem i64 %14, %15
store i64 %16, i64* %6
store i32 0, i32* %7
br label %17
17: ; preds = %29, %1
%18 = call i64 (...) @real_rand()
store i64 %18, i64* %8
br label %19
19: ; preds = %17
%20 = load i64, i64* %4
%21 = load i64, i64* %6
%22 = sub i64 %20, %21
%23 = load i64, i64* %8
%24 = icmp ule i64 %22, %23
br i1 %24, label %25, label %29
25: ; preds = %19
%26 = load i32, i32* %7
%27 = add nsw i32 %26, 1
store i32 %27, i32* %7
%28 = icmp slt i32 %26, 100
br label %29
29: ; preds = %25, %19
%30 = phi i1 [ false, %19 ], [ %28, %25 ]
br i1 %30, label %17, label %31
31: ; preds = %29
%32 = load i64, i64* %8
%33 = load i64, i64* %5
%34 = udiv i64 %32, %33
ret i64 %34
}

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 = phi i32 [ 0, %1 ], [ %10, %6 ]
%8 = tail call i64 (...) @real_rand()
%9 = icmp ule i64 %5, %8
%10 = add nuw nsw i32 %7, 1
%11 = icmp ult i32 %7, 100
%12 = select i1 %9, i1 %11, i1 false
br i1 %12, label %6, label %13
13: ; preds = %6
%14 = udiv i64 %8, %4
ret i64 %14
}