#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 uint64_t;
typedef scalar_t__ uint;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fread(scalar_t__ *, int, int, int /*<<< orphan*/ *);
uint get_random_num(const uint min, const uint max) {
  if (min == max)
    return (min);
  const uint low = max - min;
  if (low == 0)
    return (0);
  uint data;
  FILE *fp = fopen("/dev/urandom", "rb");
  if (fp == NULL)
    exit(1);
  if ((fread(&data, 1, sizeof(uint), fp)) != sizeof(uint)) {
    exit(-1);
  }
  fclose(fp);
  uint64_t r = data % low;
  r += min;
  if (r > 0xffffffff) {
    exit(-1);
  }
  return (uint)r;
}


x86 assembly (clang, -O0):
<get_random_num>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rax
cmp    -0x18(%rbp),%rax
jne    116b <get_random_num+0x2b>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    122a <get_random_num+0xea>
mov    -0x18(%rbp),%rax
sub    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    118f <get_random_num+0x4f>
movq   $0x0,-0x8(%rbp)
jmp    122a <get_random_num+0xea>
lea    0xe6a(%rip),%rdi
lea    0xe70(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11bb <get_random_num+0x7b>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    -0x30(%rbp),%rcx
lea    -0x28(%rbp),%rdi
mov    $0x1,%esi
mov    $0x8,%edx
call   1030 <fread@plt>
cltq
cmp    $0x8,%rax
je     11e8 <get_random_num+0xa8>
mov    $0xffffffff,%edi
call   1060 <exit@plt>
mov    -0x30(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x28(%rbp),%rax
cqto
idivq  -0x20(%rbp)
mov    %edx,%eax
mov    %eax,-0x34(%rbp)
mov    -0x10(%rbp),%rcx
movslq -0x34(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x34(%rbp)
cmpl   $0xffffffff,-0x34(%rbp)
jbe    1222 <get_random_num+0xe2>
mov    $0xffffffff,%edi
call   1060 <exit@plt>
movslq -0x34(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_random_num>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rbx
cmp    %rdi,%rsi
je     11be <get_random_num+0x7e>
mov    %rdi,%r14
sub    %rdi,%rbx
je     11b0 <get_random_num+0x70>
lea    0xea0(%rip),%rdi
lea    0xea6(%rip),%rsi
call   1050 <fopen@plt>
test   %rax,%rax
je     11cb <get_random_num+0x8b>
mov    %rax,%r15
lea    0x8(%rsp),%rdi
mov    $0x1,%esi
mov    $0x8,%edx
mov    %rax,%rcx
call   1030 <fread@plt>
cmp    $0x8,%eax
jne    11d5 <get_random_num+0x95>
mov    %r15,%rdi
call   1040 <fclose@plt>
mov    0x8(%rsp),%rax
mov    %rax,%rcx
or     %rbx,%rcx
shr    $0x20,%rcx
je     11b4 <get_random_num+0x74>
cqto
idiv   %rbx
jmp    11b8 <get_random_num+0x78>
xor    %ebx,%ebx
jmp    11be <get_random_num+0x7e>
xor    %edx,%edx
div    %ebx
add    %r14d,%edx
movslq %edx,%rbx
mov    %rbx,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
mov    $0x1,%edi
call   1060 <exit@plt>
mov    $0xffffffff,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@.str = [13 x i8] c"/dev/urandom\00"
@.str.1 = [3 x i8] c"rb\00"
define i64 @get_random_num(i64 %0, i64 %1) {
%3 = alloca i64
%4 = alloca i64
%5 = alloca i64
%6 = alloca i64
%7 = alloca i64
%8 = alloca i32*
%9 = alloca i32
store i64 %0, i64* %4
store i64 %1, i64* %5
%10 = load i64, i64* %4
%11 = load i64, i64* %5
%12 = icmp eq i64 %10, %11
br i1 %12, label %13, label %15
13: ; preds = %2
%14 = load i64, i64* %4
store i64 %14, i64* %3
br label %54
15: ; preds = %2
%16 = load i64, i64* %5
%17 = load i64, i64* %4
%18 = sub nsw i64 %16, %17
store i64 %18, i64* %6
%19 = load i64, i64* %6
%20 = icmp eq i64 %19, 0
br i1 %20, label %21, label %22
21: ; preds = %15
store i64 0, i64* %3
br label %54
22: ; preds = %15
%23 = call i32* @fopen(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %23, i32** %8
%24 = load i32*, i32** %8
%25 = icmp eq i32* %24, null
br i1 %25, label %26, label %28
26: ; preds = %22
%27 = call i32 @exit(i32 1)
unreachable
28: ; preds = %22
%29 = load i32*, i32** %8
%30 = call i32 @fread(i64* %7, i32 1, i32 8, i32* %29)
%31 = sext i32 %30 to i64
%32 = icmp ne i64 %31, 8
br i1 %32, label %33, label %35
33: ; preds = %28
%34 = call i32 @exit(i32 -1)
unreachable
35: ; preds = %28
%36 = load i32*, i32** %8
%37 = call i32 @fclose(i32* %36)
%38 = load i64, i64* %7
%39 = load i64, i64* %6
%40 = srem i64 %38, %39
%41 = trunc i64 %40 to i32
store i32 %41, i32* %9
%42 = load i64, i64* %4
%43 = load i32, i32* %9
%44 = sext i32 %43 to i64
%45 = add nsw i64 %44, %42
%46 = trunc i64 %45 to i32
store i32 %46, i32* %9
%47 = load i32, i32* %9
%48 = icmp ugt i32 %47, -1
br i1 %48, label %49, label %51
49: ; preds = %35
%50 = call i32 @exit(i32 -1)
unreachable
51: ; preds = %35
%52 = load i32, i32* %9
%53 = sext i32 %52 to i64
store i64 %53, i64* %3
br label %54
54: ; preds = %51, %21, %13
%55 = load i64, i64* %3
ret i64 %55
}

LLVM IR (-O2):
@.str = [13 x i8] c"/dev/urandom\00"
@.str.1 = [3 x i8] c"rb\00"
define i64 @get_random_num(i64 %0, i64 %1) {
%3 = alloca i64
%4 = icmp eq i64 %1, %0
br i1 %4, label %26, label %5
5: ; preds = %2
%6 = sub nsw i64 %1, %0
%7 = icmp eq i64 %6, 0
br i1 %7, label %26, label %8
8: ; preds = %5
%9 = bitcast i64* %3 to i8*
%10 = tail call i32* @fopen(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%11 = icmp eq i32* %10, null
br i1 %11, label %12, label %14
12: ; preds = %8
%13 = tail call i32 @exit(i32 1)
unreachable
14: ; preds = %8
%15 = call i32 @fread(i64* %3, i32 1, i32 8, i32* %10)
%16 = icmp eq i32 %15, 8
br i1 %16, label %19, label %17
17: ; preds = %14
%18 = tail call i32 @exit(i32 -1)
unreachable
19: ; preds = %14
%20 = tail call i32 @fclose(i32* %10)
%21 = load i64, i64* %3
%22 = srem i64 %21, %6
%23 = add i64 %22, %0
%24 = shl i64 %23, 32
%25 = ashr exact i64 %24, 32
br label %26
26: ; preds = %19, %5, %2
%27 = phi i64 [ %1, %2 ], [ %25, %19 ], [ 0, %5 ]
ret i64 %27
}