#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 */
unsigned int RAND_MAX;
int rand();
int rand_range(unsigned int n) {
  long end;
  int ret = -1;
  if ((n - 1) == RAND_MAX) {
    ret = rand();
    goto out;
  }
  end = RAND_MAX / n;
  if (end <= 0) {
    goto out;
  };
  while ((ret = rand()) >= end)
    ;
  ret %= n;
out:
  return ret;
}


x86 assembly (clang, -O0):
<rand_range>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
movl   $0xffffffff,-0x14(%rbp)
mov    -0x4(%rbp),%eax
sub    $0x1,%eax
mov    0x2eb9(%rip),%rcx
cmp    (%rcx),%eax
jne    1146 <rand_range+0x36>
mov    $0x0,%al
call   1030 <rand@plt>
mov    %eax,-0x14(%rbp)
jmp    1197 <rand_range+0x87>
mov    0x2e9b(%rip),%rax
mov    (%rax),%eax
xor    %edx,%edx
divl   -0x4(%rbp)
mov    %eax,%eax
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jg     116a <rand_range+0x5a>
jmp    1197 <rand_range+0x87>
jmp    116f <rand_range+0x5f>
mov    $0x0,%al
call   1030 <rand@plt>
mov    %eax,-0x14(%rbp)
cltq
cmp    -0x10(%rbp),%rax
jl     118a <rand_range+0x7a>
jmp    116f <rand_range+0x5f>
mov    -0x4(%rbp),%ecx
mov    -0x14(%rbp),%eax
xor    %edx,%edx
div    %ecx
mov    %edx,-0x14(%rbp)
mov    -0x14(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rand_range>:
push   %r14
push   %rbx
push   %rax
mov    %edi,%r14d
lea    -0x1(%r14),%eax
mov    0x2ec6(%rip),%rcx
mov    (%rcx),%ecx
cmp    %ecx,%eax
jne    1136 <rand_range+0x26>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1030 <rand@plt>
mov    %ecx,%eax
xor    %edx,%edx
div    %r14d
mov    %eax,%ebx
mov    $0xffffffff,%eax
cmp    %r14d,%ecx
jb     1166 <rand_range+0x56>
nopl   0x0(%rax)
xor    %eax,%eax
call   1030 <rand@plt>
movslq %eax,%rcx
cmp    %rbx,%rcx
jge    1150 <rand_range+0x40>
xor    %edx,%edx
div    %r14d
mov    %edx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

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

LLVM IR (-O2):
@RAND_MAX = global i32 0
define i32 @rand_range(i32 %0) {
%2 = add i32 %0, -1
%3 = load i32, i32* @RAND_MAX
%4 = icmp eq i32 %2, %3
br i1 %4, label %5, label %7
5: ; preds = %1
%6 = tail call i32 (...) @rand()
br label %17
7: ; preds = %1
%8 = udiv i32 %3, %0
%9 = zext i32 %8 to i64
%10 = icmp ult i32 %3, %0
br i1 %10, label %17, label %11
11: ; preds = %7, %11
%12 = tail call i32 (...) @rand()
%13 = sext i32 %12 to i64
%14 = icmp slt i64 %13, %9
br i1 %14, label %15, label %11
15: ; preds = %11
%16 = urem i32 %12, %0
br label %17
17: ; preds = %7, %15, %5
%18 = phi i32 [ %6, %5 ], [ -1, %7 ], [ %16, %15 ]
ret i32 %18
}