#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 */
typedef scalar_t__ uint32_t;

/* Variables and functions */
scalar_t__ RAND_MAX;
int /*<<< orphan*/ assert(int);
scalar_t__ rand();
uint32_t rand_int_range(uint32_t min, uint32_t max) {
  assert(max < RAND_MAX);
  assert(min < max);
  uint32_t res;
  uint32_t valid_range = RAND_MAX - (RAND_MAX % (max - min + 1));
  do {
    res = rand();
  } while (res >= valid_range);
  return min + res / (valid_range / (max - min + 1));
}


x86 assembly (clang, -O0):
<rand_int_range>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    0x2ead(%rip),%rcx
cmp    (%rcx),%rax
setl   %al
and    $0x1,%al
movzbl %al,%edi
call   1030 <assert@plt>
mov    -0x8(%rbp),%rax
cmp    -0x10(%rbp),%rax
setl   %al
and    $0x1,%al
movzbl %al,%edi
call   1030 <assert@plt>
mov    0x2e81(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
mov    0x2e73(%rip),%rax
mov    (%rax),%rax
mov    -0x10(%rbp),%rcx
sub    -0x8(%rbp),%rcx
add    $0x1,%rcx
cqto
idiv   %rcx
mov    -0x28(%rbp),%rax
sub    %rdx,%rax
mov    %rax,-0x20(%rbp)
mov    $0x0,%al
call   1040 <rand@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
cmp    -0x20(%rbp),%rax
jge    1194 <rand_int_range+0x74>
mov    -0x8(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x20(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    -0x8(%rbp),%rcx
add    $0x1,%rcx
cqto
idiv   %rcx
mov    %rax,%rcx
mov    -0x38(%rbp),%rax
cqto
idiv   %rcx
mov    %rax,%rcx
mov    -0x30(%rbp),%rax
add    %rcx,%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rand_int_range>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r15
mov    %rdi,%r14
mov    0x2eb6(%rip),%rbx
xor    %edi,%edi
cmp    %rsi,(%rbx)
setg   %dil
call   1030 <assert@plt>
xor    %edi,%edi
sub    %r14,%r15
setg   %dil
call   1030 <assert@plt>
mov    (%rbx),%rbx
add    $0x1,%r15
mov    %rbx,%rax
or     %r15,%rax
shr    $0x20,%rax
je     116e <rand_int_range+0x4e>
mov    %rbx,%rax
cqto
idiv   %r15
mov    %rax,%r15
jmp    1178 <rand_int_range+0x58>
mov    %ebx,%eax
xor    %edx,%edx
div    %r15d
mov    %eax,%r15d
sub    %rdx,%rbx
nopl   0x0(%rax,%rax,1)
xor    %eax,%eax
call   1040 <rand@plt>
cmp    %rbx,%rax
jge    1180 <rand_int_range+0x60>
mov    %rax,%rcx
or     %r15,%rcx
shr    $0x20,%rcx
je     119f <rand_int_range+0x7f>
cqto
idiv   %r15
jmp    11a4 <rand_int_range+0x84>
xor    %edx,%edx
div    %r15d
add    %r14,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@RAND_MAX = global i64 0
define i64 @rand_int_range(i64 %0, i64 %1) {
%3 = alloca i64
%4 = alloca i64
%5 = alloca i64
%6 = alloca i64
store i64 %0, i64* %3
store i64 %1, i64* %4
%7 = load i64, i64* %4
%8 = load i64, i64* @RAND_MAX
%9 = icmp slt i64 %7, %8
%10 = zext i1 %9 to i32
%11 = call i32 @assert(i32 %10)
%12 = load i64, i64* %3
%13 = load i64, i64* %4
%14 = icmp slt i64 %12, %13
%15 = zext i1 %14 to i32
%16 = call i32 @assert(i32 %15)
%17 = load i64, i64* @RAND_MAX
%18 = load i64, i64* @RAND_MAX
%19 = load i64, i64* %4
%20 = load i64, i64* %3
%21 = sub nsw i64 %19, %20
%22 = add nsw i64 %21, 1
%23 = srem i64 %18, %22
%24 = sub nsw i64 %17, %23
store i64 %24, i64* %6
br label %25
25: ; preds = %27, %2
%26 = call i64 (...) @rand()
store i64 %26, i64* %5
br label %27
27: ; preds = %25
%28 = load i64, i64* %5
%29 = load i64, i64* %6
%30 = icmp sge i64 %28, %29
br i1 %30, label %25, label %31
31: ; preds = %27
%32 = load i64, i64* %3
%33 = load i64, i64* %5
%34 = load i64, i64* %6
%35 = load i64, i64* %4
%36 = load i64, i64* %3
%37 = sub nsw i64 %35, %36
%38 = add nsw i64 %37, 1
%39 = sdiv i64 %34, %38
%40 = sdiv i64 %33, %39
%41 = add nsw i64 %32, %40
ret i64 %41
}

LLVM IR (-O2):
@RAND_MAX = global i64 0
define i64 @rand_int_range(i64 %0, i64 %1) {
%3 = load i64, i64* @RAND_MAX
%4 = icmp sgt i64 %3, %1
%5 = zext i1 %4 to i32
%6 = tail call i32 @assert(i32 %5)
%7 = icmp sgt i64 %1, %0
%8 = zext i1 %7 to i32
%9 = tail call i32 @assert(i32 %8)
%10 = load i64, i64* @RAND_MAX
%11 = sub nsw i64 %1, %0
%12 = add nsw i64 %11, 1
%13 = srem i64 %10, %12
%14 = sdiv i64 %10, %12
%15 = sub nsw i64 %10, %13
br label %16
16: ; preds = %16, %2
%17 = tail call i64 (...) @rand()
%18 = icmp slt i64 %17, %15
br i1 %18, label %19, label %16
19: ; preds = %16
%20 = sdiv i64 %17, %14
%21 = add nsw i64 %20, %0
ret i64 %21
}