#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__ UINT32_MAX;
int /*<<< orphan*/ assert(int);
scalar_t__ rand_uint32();
uint32_t rand_uint32_range(uint32_t start, uint32_t stop) {
  uint32_t x;
  uint32_t n;
  assert(start <= stop);
  n = stop - start;
  if (n == 0) {
    return 0;
  }
  do {
    x = rand_uint32();
  } while (x >= UINT32_MAX - UINT32_MAX % n);
  x %= n;
  x += start;
  return x;
}


x86 assembly (clang, -O0):
<rand_uint32_range>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rax
cmp    -0x18(%rbp),%rax
setle  %al
and    $0x1,%al
movzbl %al,%edi
call   1030 <assert@plt>
mov    -0x18(%rbp),%rax
sub    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1169 <rand_uint32_range+0x49>
movq   $0x0,-0x8(%rbp)
jmp    11d0 <rand_uint32_range+0xb0>
jmp    116e <rand_uint32_range+0x4e>
mov    $0x0,%al
call   1040 <rand_uint32@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    0x2e70(%rip),%rax
mov    (%rax),%rcx
mov    0x2e66(%rip),%rax
mov    (%rax),%rax
cqto
idivq  -0x28(%rbp)
mov    -0x30(%rbp),%rax
sub    %rdx,%rcx
cmp    %rcx,%rax
jge    116e <rand_uint32_range+0x4e>
mov    -0x28(%rbp),%rcx
mov    -0x20(%rbp),%rax
cqto
idiv   %rcx
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rax
add    -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rand_uint32_range>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%rbx
mov    %rdi,%r14
xor    %edi,%edi
cmp    %r14,%rsi
setge  %dil
call   1030 <assert@plt>
sub    %r14,%rbx
je     119a <rand_uint32_range+0x7a>
mov    0x2eb3(%rip),%r15
jmp    1160 <rand_uint32_range+0x40>
nopw   0x0(%rax,%rax,1)
mov    %rsi,%rax
cqto
idiv   %rbx
sub    %rdx,%rsi
cmp    %rsi,%rcx
jl     1181 <rand_uint32_range+0x61>
xor    %eax,%eax
call   1040 <rand_uint32@plt>
mov    %rax,%rcx
mov    (%r15),%rsi
mov    %rsi,%rax
or     %rbx,%rax
shr    $0x20,%rax
jne    1150 <rand_uint32_range+0x30>
mov    %esi,%eax
xor    %edx,%edx
div    %ebx
jmp    1158 <rand_uint32_range+0x38>
mov    %rbx,%rax
or     %rcx,%rax
shr    $0x20,%rax
je     119e <rand_uint32_range+0x7e>
mov    %rcx,%rax
cqto
idiv   %rbx
mov    %rdx,%rax
jmp    11a6 <rand_uint32_range+0x86>
xor    %eax,%eax
jmp    11a9 <rand_uint32_range+0x89>
mov    %ecx,%eax
xor    %edx,%edx
div    %ebx
mov    %edx,%eax
add    %r14,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
@UINT32_MAX = global i64 0
define i64 @rand_uint32_range(i64 %0, i64 %1) {
%3 = icmp sge i64 %1, %0
%4 = zext i1 %3 to i32
%5 = tail call i32 @assert(i32 %4)
%6 = sub nsw i64 %1, %0
%7 = icmp eq i64 %6, 0
br i1 %7, label %17, label %8
8: ; preds = %2, %8
%9 = tail call i64 (...) @rand_uint32()
%10 = load i64, i64* @UINT32_MAX
%11 = srem i64 %10, %6
%12 = sub nsw i64 %10, %11
%13 = icmp slt i64 %9, %12
br i1 %13, label %14, label %8
14: ; preds = %8
%15 = srem i64 %9, %6
%16 = add nsw i64 %15, %0
br label %17
17: ; preds = %2, %14
%18 = phi i64 [ %16, %14 ], [ 0, %2 ]
ret i64 %18
}