#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 ssize_t;
typedef int /*<<< orphan*/ rand;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
size_t UINT_MAX;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int read(int, unsigned int *, int);
unsigned int random_num(int urandom_fd, size_t rand_max) {
  ssize_t ret;
  unsigned int limit, rand;
  limit = UINT_MAX - (UINT_MAX % rand_max);
  do {
    ret = read(urandom_fd, &rand, sizeof(rand));
    if (ret != sizeof(rand)) {
      perror("read failed");
      close(urandom_fd);
      exit(EXIT_FAILURE);
    }
  } while (rand >= limit);
  return (rand % rand_max);
}


x86 assembly (clang, -O0):
<random_num>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e92(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
mov    0x2e84(%rip),%rax
mov    (%rax),%rax
xor    %ecx,%ecx
mov    %ecx,%edx
divq   -0x10(%rbp)
mov    -0x28(%rbp),%rax
sub    %rdx,%rax
mov    %eax,-0x18(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x1c(%rbp),%rsi
mov    $0x4,%edx
call   1040 <read@plt>
mov    %eax,-0x14(%rbp)
movslq -0x14(%rbp),%rax
cmp    $0x4,%rax
je     11bd <random_num+0x7d>
lea    0xe5e(%rip),%rdi
call   1050 <perror@plt>
mov    -0x4(%rbp),%edi
call   1030 <close@plt>
mov    0x2e22(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
jmp    11c2 <random_num+0x82>
mov    -0x1c(%rbp),%eax
cmp    -0x18(%rbp),%eax
jae    1179 <random_num+0x39>
mov    -0x1c(%rbp),%eax
xor    %ecx,%ecx
mov    %ecx,%edx
divq   -0x10(%rbp)
mov    %edx,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<random_num>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %edi,%ebp
mov    0x2e95(%rip),%rax
mov    (%rax),%rbx
mov    %rbx,%rax
or     %rsi,%rax
shr    $0x20,%rax
je     116c <random_num+0x2c>
mov    %rbx,%rax
xor    %edx,%edx
div    %r14
jmp    1173 <random_num+0x33>
mov    %ebx,%eax
xor    %edx,%edx
div    %r14d
sub    %edx,%ebx
lea    0x4(%rsp),%r15
nopw   0x0(%rax,%rax,1)
mov    %ebp,%edi
mov    %r15,%rsi
mov    $0x4,%edx
call   1040 <read@plt>
cmp    $0x4,%eax
jne    11b3 <random_num+0x73>
mov    0x4(%rsp),%eax
cmp    %ebx,%eax
jae    1180 <random_num+0x40>
cmp    %r14,%rax
jb     11a8 <random_num+0x68>
xor    %edx,%edx
div    %r14d
mov    %edx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe46(%rip),%rdi
call   1050 <perror@plt>
mov    %ebp,%edi
call   1030 <close@plt>
mov    0x2e0b(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@UINT_MAX = global i64 0
@.str = [12 x i8] c"read failed\00"
@EXIT_FAILURE = global i32 0
define i32 @random_num(i32 %0, i64 %1) {
%3 = alloca i32
%4 = alloca i64
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i32 %0, i32* %3
store i64 %1, i64* %4
%8 = load i64, i64* @UINT_MAX
%9 = load i64, i64* @UINT_MAX
%10 = load i64, i64* %4
%11 = urem i64 %9, %10
%12 = sub i64 %8, %11
%13 = trunc i64 %12 to i32
store i32 %13, i32* %6
br label %14
14: ; preds = %27, %2
%15 = load i32, i32* %3
%16 = call i32 @read(i32 %15, i32* %7, i32 4)
store i32 %16, i32* %5
%17 = load i32, i32* %5
%18 = sext i32 %17 to i64
%19 = icmp ne i64 %18, 4
br i1 %19, label %20, label %26
20: ; preds = %14
%21 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%22 = load i32, i32* %3
%23 = call i32 @close(i32 %22)
%24 = load i32, i32* @EXIT_FAILURE
%25 = call i32 @exit(i32 %24)
unreachable
26: ; preds = %14
br label %27
27: ; preds = %26
%28 = load i32, i32* %7
%29 = load i32, i32* %6
%30 = icmp uge i32 %28, %29
br i1 %30, label %14, label %31
31: ; preds = %27
%32 = load i32, i32* %7
%33 = zext i32 %32 to i64
%34 = load i64, i64* %4
%35 = urem i64 %33, %34
%36 = trunc i64 %35 to i32
ret i32 %36
}

LLVM IR (-O2):
@UINT_MAX = global i64 0
@.str = [12 x i8] c"read failed\00"
@EXIT_FAILURE = global i32 0
define i32 @random_num(i32 %0, i64 %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = load i64, i64* @UINT_MAX
%6 = urem i64 %5, %1
%7 = sub i64 %5, %6
%8 = trunc i64 %7 to i32
br label %9
9: ; preds = %17, %2
%10 = call i32 @read(i32 %0, i32* %3, i32 4)
%11 = icmp eq i32 %10, 4
br i1 %11, label %17, label %12
12: ; preds = %9
%13 = tail call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%14 = tail call i32 @close(i32 %0)
%15 = load i32, i32* @EXIT_FAILURE
%16 = tail call i32 @exit(i32 %15)
unreachable
17: ; preds = %9
%18 = load i32, i32* %3
%19 = icmp ult i32 %18, %8
br i1 %19, label %20, label %9
20: ; preds = %17
%21 = zext i32 %18 to i64
%22 = urem i64 %21, %1
%23 = trunc i64 %22 to i32
ret i32 %23
}