#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 int /*<<< orphan*/ random_value;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ assert(int);
int open(char *, int /*<<< orphan*/);
int read(int, char *, int);
int random_number(int min, int max) {
  static int dev_random_fd = -1;
  char *next_random_byte;
  int bytes_to_read;
  unsigned random_value;
  assert(max > min);
  if (dev_random_fd == -1) {
    dev_random_fd = open("/dev/random", O_RDONLY);
    assert(dev_random_fd != -1);
  }
  next_random_byte = (char *)&random_value;
  bytes_to_read = sizeof(random_value);
  ;
  do {
    int bytes_read;
    bytes_read = read(dev_random_fd, next_random_byte, bytes_to_read);
    bytes_to_read -= bytes_read;
    next_random_byte += bytes_read;
  } while (bytes_to_read > 0);
  return min + (random_value % (max - min + 1));
}


x86 assembly (clang, -O0):
<random_number>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    -0x8(%rbp),%eax
cmp    -0x4(%rbp),%eax
setg   %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
cmpl   $0xffffffff,0x2ee0(%rip)
jne    118d <random_number+0x5d>
mov    0x2e83(%rip),%rax
mov    (%rax),%esi
lea    0xe92(%rip),%rdi
call   1050 <open@plt>
mov    %eax,0x2ebf(%rip)
cmpl   $0xffffffff,0x2eb8(%rip)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
lea    -0x18(%rbp),%rax
mov    %rax,-0x10(%rbp)
movl   $0x4,-0x14(%rbp)
mov    0x2e96(%rip),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <read@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%ecx
mov    -0x14(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x14(%rbp)
mov    -0x1c(%rbp),%ecx
mov    -0x10(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
cmpl   $0x0,-0x14(%rbp)
jg     119c <random_number+0x6c>
mov    -0x4(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x18(%rbp),%eax
mov    -0x8(%rbp),%ecx
sub    -0x4(%rbp),%ecx
add    $0x1,%ecx
xor    %edx,%edx
div    %ecx
mov    -0x20(%rbp),%eax
add    %edx,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<random_number>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%r15d
mov    %edi,%r14d
xor    %edi,%edi
sub    %r14d,%r15d
setg   %dil
call   1040 <assert@plt>
cmpl   $0xffffffff,0x2ee6(%rip)
jne    117d <random_number+0x4d>
mov    0x2e8d(%rip),%rax
mov    (%rax),%esi
lea    0xe9c(%rip),%rdi
call   1050 <open@plt>
mov    %eax,0x2ec9(%rip)
xor    %edi,%edi
cmp    $0xffffffff,%eax
setne  %dil
call   1040 <assert@plt>
mov    $0x4,%ebx
lea    0x4(%rsp),%rbp
nopw   0x0(%rax,%rax,1)
mov    0x2ea2(%rip),%edi
mov    %rbp,%rsi
mov    %ebx,%edx
call   1030 <read@plt>
cltq
add    %rax,%rbp
sub    %eax,%ebx
jg     1190 <random_number+0x60>
mov    0x4(%rsp),%eax
add    $0x1,%r15d
xor    %edx,%edx
div    %r15d
add    %edx,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@random_number.dev_random_fd = internal global i32 -1
@.str = [12 x i8] c"/dev/random\00"
@O_RDONLY = global i32 0
define i32 @random_number(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %3
store i32 %1, i32* %4
%9 = load i32, i32* %4
%10 = load i32, i32* %3
%11 = icmp sgt i32 %9, %10
%12 = zext i1 %11 to i32
%13 = call i32 @assert(i32 %12)
%14 = load i32, i32* @random_number.dev_random_fd
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %23
16: ; preds = %2
%17 = load i32, i32* @O_RDONLY
%18 = call i32 @open(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %17)
store i32 %18, i32* @random_number.dev_random_fd
%19 = load i32, i32* @random_number.dev_random_fd
%20 = icmp ne i32 %19, -1
%21 = zext i1 %20 to i32
%22 = call i32 @assert(i32 %21)
br label %23
23: ; preds = %16, %2
%24 = bitcast i32* %7 to i8*
store i8* %24, i8** %5
store i32 4, i32* %6
br label %25
25: ; preds = %37, %23
%26 = load i32, i32* @random_number.dev_random_fd
%27 = load i8*, i8** %5
%28 = load i32, i32* %6
%29 = call i32 @read(i32 %26, i8* %27, i32 %28)
store i32 %29, i32* %8
%30 = load i32, i32* %8
%31 = load i32, i32* %6
%32 = sub nsw i32 %31, %30
store i32 %32, i32* %6
%33 = load i32, i32* %8
%34 = load i8*, i8** %5
%35 = sext i32 %33 to i64
%36 = getelementptr inbounds i8, i8* %34, i64 %35
store i8* %36, i8** %5
br label %37
37: ; preds = %25
%38 = load i32, i32* %6
%39 = icmp sgt i32 %38, 0
br i1 %39, label %25, label %40
40: ; preds = %37
%41 = load i32, i32* %3
%42 = load i32, i32* %7
%43 = load i32, i32* %4
%44 = load i32, i32* %3
%45 = sub nsw i32 %43, %44
%46 = add nsw i32 %45, 1
%47 = urem i32 %42, %46
%48 = add i32 %41, %47
ret i32 %48
}

LLVM IR (-O2):
@random_number.dev_random_fd = internal unnamed_addr global i32 -1
@.str = [12 x i8] c"/dev/random\00"
@O_RDONLY = global i32 0
define i32 @random_number(i32 %0, i32 %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = icmp sgt i32 %1, %0
%6 = zext i1 %5 to i32
%7 = tail call i32 @assert(i32 %6)
%8 = load i32, i32* @random_number.dev_random_fd
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %16
10: ; preds = %2
%11 = load i32, i32* @O_RDONLY
%12 = tail call i32 @open(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %11)
store i32 %12, i32* @random_number.dev_random_fd
%13 = icmp ne i32 %12, -1
%14 = zext i1 %13 to i32
%15 = tail call i32 @assert(i32 %14)
br label %16
16: ; preds = %10, %2
br label %17
17: ; preds = %16, %17
%18 = phi i32 [ %22, %17 ], [ 4, %16 ]
%19 = phi i8* [ %24, %17 ], [ %4, %16 ]
%20 = load i32, i32* @random_number.dev_random_fd
%21 = call i32 @read(i32 %20, i8* %19, i32 %18)
%22 = sub nsw i32 %18, %21
%23 = sext i32 %21 to i64
%24 = getelementptr inbounds i8, i8* %19, i64 %23
%25 = icmp sgt i32 %22, 0
br i1 %25, label %17, label %26
26: ; preds = %17
%27 = load i32, i32* %3
%28 = sub i32 1, %0
%29 = add i32 %28, %1
%30 = urem i32 %27, %29
%31 = add i32 %30, %0
ret i32 %31
}