#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 */
scalar_t__ EAGAIN;
scalar_t__ EINTR;
int /*<<< orphan*/ EXIT_FAILURE;
scalar_t__ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int get_random_fd();
unsigned int getpid();
int /*<<< orphan*/ perror(char *);
scalar_t__ read(int, unsigned char *, int);
unsigned int randgen(unsigned int max) {
  int fd = get_random_fd();
  unsigned int val;
  do {
    unsigned char buf;
    while (1) {
      if (read(fd, &buf, 1) <= 0) {
        if (errno == EINTR || errno == EAGAIN)
          continue;
        perror("read random device failed");
        exit(EXIT_FAILURE);
      }
      break;
    }
    val = buf;
  } while (val >= max + getpid());
  return val % max;
}


x86 assembly (clang, -O0):
<randgen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    $0x0,%al
call   1050 <get_random_fd@plt>
mov    %eax,-0x8(%rbp)
jmp    116a <randgen+0x1a>
mov    -0x8(%rbp),%edi
lea    -0xd(%rbp),%rsi
mov    $0x1,%edx
call   1040 <read@plt>
cmp    $0x0,%rax
jg     11d8 <randgen+0x88>
mov    0x2e54(%rip),%rax
mov    (%rax),%rax
mov    0x2e3a(%rip),%rcx
cmp    (%rcx),%rax
je     11b9 <randgen+0x69>
mov    0x2e3a(%rip),%rax
mov    (%rax),%rax
mov    0x2e10(%rip),%rcx
cmp    (%rcx),%rax
jne    11be <randgen+0x6e>
jmp    116a <randgen+0x1a>
lea    0xe3b(%rip),%rdi
call   1060 <perror@plt>
mov    0x2e07(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
jmp    11dd <randgen+0x8d>
movzbl -0xd(%rbp),%eax
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    -0x18(%rbp),%ecx
mov    %eax,%edx
mov    -0x14(%rbp),%eax
add    %edx,%ecx
cmp    %ecx,%eax
jae    1165 <randgen+0x15>
mov    -0xc(%rbp),%eax
xor    %edx,%edx
divl   -0x4(%rbp)
mov    %edx,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<randgen>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edi,0x4(%rsp)
xor    %eax,%eax
call   1050 <get_random_fd@plt>
mov    %eax,%ebp
lea    0x3(%rsp),%r13
mov    0x2e6c(%rip),%r12
mov    0x2e55(%rip),%rbx
mov    0x2e3e(%rip),%r14
jmp    11a0 <randgen+0x50>
movzbl 0x3(%rsp),%r15d
xor    %eax,%eax
call   1030 <getpid@plt>
add    0x4(%rsp),%eax
cmp    %r15d,%eax
ja     11dc <randgen+0x8c>
nopw   0x0(%rax,%rax,1)
mov    %ebp,%edi
mov    %r13,%rsi
mov    $0x1,%edx
call   1040 <read@plt>
test   %rax,%rax
jg     1184 <randgen+0x34>
mov    (%r12),%rax
cmp    (%rbx),%rax
je     11a0 <randgen+0x50>
cmp    (%r14),%rax
je     11a0 <randgen+0x50>
lea    0xe37(%rip),%rdi
call   1060 <perror@plt>
mov    0x2e03(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
mov    %r15d,%eax
xor    %edx,%edx
divl   0x4(%rsp)
mov    %edx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@errno = global i64 0
@EINTR = global i64 0
@EAGAIN = global i64 0
@.str = [26 x i8] c"read random device failed\00"
@EXIT_FAILURE = global i32 0
define i32 @randgen(i32 %0) {
%2 = alloca i8
%3 = tail call i32 (...) @get_random_fd()
br label %4
4: ; preds = %19, %1
br label %5
5: ; preds = %8, %4
%6 = call i64 @read(i32 %3, i8* %2, i32 1)
%7 = icmp slt i64 %6, 1
br i1 %7, label %8, label %19
8: ; preds = %5
%9 = load i64, i64* @errno
%10 = load i64, i64* @EINTR
%11 = icmp eq i64 %9, %10
%12 = load i64, i64* @EAGAIN
%13 = icmp eq i64 %9, %12
%14 = select i1 %11, i1 true, i1 %13
br i1 %14, label %5, label %15
15: ; preds = %8
%16 = tail call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
%17 = load i32, i32* @EXIT_FAILURE
%18 = tail call i32 @exit(i32 %17)
unreachable
19: ; preds = %5
%20 = load i8, i8* %2
%21 = zext i8 %20 to i32
%22 = tail call i32 (...) @getpid()
%23 = add i32 %22, %0
%24 = icmp ugt i32 %23, %21
br i1 %24, label %25, label %4
25: ; preds = %19
%26 = zext i8 %20 to i32
%27 = urem i32 %26, %0
ret i32 %27
}