#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 */
int RAND_MAX;
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ assert_failed(char *);
long random();
int draw_int(int bound) {
  assert(bound > 0);
  if (bound > RAND_MAX)
    assert_failed("random() doesn't have sufficient range");
  int draw = RAND_MAX / bound * bound;
  long r;
  do {
    r = random();
    if (r > RAND_MAX)
      assert_failed("random() > RAND_MAX");
  } while (r > draw);
  return r % bound;
}


x86 assembly (clang, -O0):
<draw_int>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
cmpl   $0x0,-0x4(%rbp)
setg   %al
and    $0x1,%al
movzbl %al,%edi
call   1030 <assert@plt>
mov    -0x4(%rbp),%eax
mov    0x2e92(%rip),%rcx
cmp    (%rcx),%eax
jle    116a <draw_int+0x3a>
lea    0xe9b(%rip),%rdi
call   1050 <assert_failed@plt>
mov    0x2e77(%rip),%rax
mov    (%rax),%eax
cltd
idivl  -0x4(%rbp)
imul   -0x4(%rbp),%eax
mov    %eax,-0x8(%rbp)
mov    $0x0,%al
call   1040 <random@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    0x2e54(%rip),%rcx
movslq (%rcx),%rcx
cmp    %rcx,%rax
jle    11ac <draw_int+0x7c>
lea    0xe80(%rip),%rdi
call   1050 <assert_failed@plt>
jmp    11b1 <draw_int+0x81>
mov    -0x10(%rbp),%rax
movslq -0x8(%rbp),%rcx
cmp    %rcx,%rax
jg     117e <draw_int+0x4e>
mov    -0x10(%rbp),%rax
movslq -0x4(%rbp),%rcx
cqto
idiv   %rcx
mov    %edx,%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<draw_int>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %edi,%r14d
xor    %edi,%edi
test   %r14d,%r14d
setg   %dil
call   1030 <assert@plt>
mov    0x2e98(%rip),%r12
mov    (%r12),%ecx
cmp    %r14d,%ecx
jge    1169 <draw_int+0x39>
lea    0xea0(%rip),%rdi
call   1050 <assert_failed@plt>
mov    (%r12),%ecx
mov    %ecx,%eax
cltd
idiv   %r14d
sub    %edx,%ecx
movslq %ecx,%rbp
lea    0xeac(%rip),%r15
jmp    1185 <draw_int+0x55>
nopl   (%rax)
cmp    %rbp,%rbx
jle    11a2 <draw_int+0x72>
xor    %eax,%eax
call   1040 <random@plt>
mov    %rax,%rbx
movslq (%r12),%rax
cmp    %rax,%rbx
jle    1180 <draw_int+0x50>
mov    %r15,%rdi
call   1050 <assert_failed@plt>
jmp    1180 <draw_int+0x50>
movslq %r14d,%rcx
mov    %rbx,%rax
or     %rcx,%rax
shr    $0x20,%rax
je     11be <draw_int+0x8e>
mov    %rbx,%rax
cqto
idiv   %rcx
mov    %rdx,%rax
jmp    11c7 <draw_int+0x97>
mov    %ebx,%eax
xor    %edx,%edx
div    %r14d
mov    %edx,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@RAND_MAX = global i32 0
@.str = [39 x i8] c"random() doesn't have sufficient range\00"
@.str.1 = [20 x i8] c"random() > RAND_MAX\00"
define i32 @draw_int(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i64
store i32 %0, i32* %2
%5 = load i32, i32* %2
%6 = icmp sgt i32 %5, 0
%7 = zext i1 %6 to i32
%8 = call i32 @assert(i32 %7)
%9 = load i32, i32* %2
%10 = load i32, i32* @RAND_MAX
%11 = icmp sgt i32 %9, %10
br i1 %11, label %12, label %14
12: ; preds = %1
%13 = call i32 @assert_failed(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0))
br label %14
14: ; preds = %12, %1
%15 = load i32, i32* @RAND_MAX
%16 = load i32, i32* %2
%17 = sdiv i32 %15, %16
%18 = load i32, i32* %2
%19 = mul nsw i32 %17, %18
store i32 %19, i32* %3
br label %20
20: ; preds = %29, %14
%21 = call i64 (...) @random()
store i64 %21, i64* %4
%22 = load i64, i64* %4
%23 = load i32, i32* @RAND_MAX
%24 = sext i32 %23 to i64
%25 = icmp sgt i64 %22, %24
br i1 %25, label %26, label %28
26: ; preds = %20
%27 = call i32 @assert_failed(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
br label %28
28: ; preds = %26, %20
br label %29
29: ; preds = %28
%30 = load i64, i64* %4
%31 = load i32, i32* %3
%32 = sext i32 %31 to i64
%33 = icmp sgt i64 %30, %32
br i1 %33, label %20, label %34
34: ; preds = %29
%35 = load i64, i64* %4
%36 = load i32, i32* %2
%37 = sext i32 %36 to i64
%38 = srem i64 %35, %37
%39 = trunc i64 %38 to i32
ret i32 %39
}

LLVM IR (-O2):
@RAND_MAX = global i32 0
@.str = [39 x i8] c"random() doesn't have sufficient range\00"
@.str.1 = [20 x i8] c"random() > RAND_MAX\00"
define i32 @draw_int(i32 %0) {
%2 = icmp sgt i32 %0, 0
%3 = zext i1 %2 to i32
%4 = tail call i32 @assert(i32 %3)
%5 = load i32, i32* @RAND_MAX
%6 = icmp slt i32 %5, %0
br i1 %6, label %7, label %10
7: ; preds = %1
%8 = tail call i32 @assert_failed(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0))
%9 = load i32, i32* @RAND_MAX
br label %10
10: ; preds = %7, %1
%11 = phi i32 [ %9, %7 ], [ %5, %1 ]
%12 = srem i32 %11, %0
%13 = sub i32 %11, %12
%14 = sext i32 %13 to i64
br label %15
15: ; preds = %22, %10
%16 = tail call i64 (...) @random()
%17 = load i32, i32* @RAND_MAX
%18 = sext i32 %17 to i64
%19 = icmp sgt i64 %16, %18
br i1 %19, label %20, label %22
20: ; preds = %15
%21 = tail call i32 @assert_failed(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
br label %22
22: ; preds = %15, %20
%23 = icmp sgt i64 %16, %14
br i1 %23, label %15, label %24
24: ; preds = %22
%25 = sext i32 %0 to i64
%26 = srem i64 %16, %25
%27 = trunc i64 %26 to i32
ret i32 %27
}