#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 const RAND_MAX;
int rand();
unsigned int random_uint(unsigned int min, unsigned int max) {
  int candidate;
  const unsigned int range = max - min + 1;
  const int wells = RAND_MAX / range;
  const int limit = wells * range;
  if (range > RAND_MAX) {
    return max + 1;
  }
  while (1) {
    candidate = rand();
    if (candidate < limit) {
      break;
    }
  }
  return min + (candidate / wells);
}


x86 assembly (clang, -O0):
<random_uint>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0xc(%rbp),%eax
sub    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
mov    0x2eb7(%rip),%rax
mov    (%rax),%eax
xor    %edx,%edx
divl   -0x14(%rbp)
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
imul   -0x14(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x14(%rbp),%eax
mov    0x2e99(%rip),%rcx
cmp    (%rcx),%eax
jbe    1165 <random_uint+0x55>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4(%rbp)
jmp    11a1 <random_uint+0x91>
jmp    116a <random_uint+0x5a>
mov    $0x0,%al
call   1030 <rand@plt>
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    1185 <random_uint+0x75>
jmp    118a <random_uint+0x7a>
jmp    116a <random_uint+0x5a>
mov    -0x8(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%eax
cltd
idivl  -0x18(%rbp)
mov    %eax,%ecx
mov    -0x20(%rbp),%eax
add    %ecx,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<random_uint>:
push   %rax
mov    %esi,%ecx
sub    %edi,%ecx
cmp    $0xffffffff,%ecx
je     1130 <random_uint+0x20>
mov    %esi,%eax
add    $0x1,%eax
pop    %rcx
ret
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
xor    %eax,%eax
call   1030 <rand@plt>
mov    %eax,%ecx
test   %ecx,%ecx
jns    1130 <random_uint+0x20>
pop    %rcx
ret

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

LLVM IR (-O2):
@RAND_MAX = constant i32 0
define i32 @random_uint(i32 %0, i32 %1) {
%3 = sub i32 %1, %0
%4 = icmp eq i32 %3, -1
br i1 %4, label %7, label %5
5: ; preds = %2
%6 = add i32 %1, 1
br label %10
7: ; preds = %2, %7
%8 = tail call i32 (...) @rand()
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %7
10: ; preds = %7, %5
%11 = phi i32 [ %6, %5 ], [ poison, %7 ]
ret i32 %11
}