#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*/ uint8_t;

/* Variables and functions */
scalar_t__ EAGAIN;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ abort();
scalar_t__ errno;
int open(char *, int /*<<< orphan*/);
int read(int, int /*<<< orphan*/ *, int);
uint8_t get_random_byte(void) {
  static int fd = -1;
  uint8_t buf;
  int rv;
  if (fd < 0)
    fd = open("/dev/urandom", O_RDONLY);
  do {
    errno = 0;
    rv = read(fd, &buf, 1);
    if (rv < 1 && errno != EAGAIN)
      abort();
  } while (rv < 1);
  return buf;
}


x86 assembly (clang, -O0):
<get_random_byte>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
cmpl   $0x0,0x2ef9(%rip)
jge    1160 <get_random_byte+0x30>
mov    0x2e9c(%rip),%rax
mov    (%rax),%esi
lea    0xeab(%rip),%rdi
call   1050 <open@plt>
mov    %eax,0x2ed8(%rip)
jmp    1165 <get_random_byte+0x35>
mov    0x2e6c(%rip),%rax
movq   $0x0,(%rax)
mov    0x2ebf(%rip),%edi
lea    -0x4(%rbp),%rsi
mov    $0x1,%edx
call   1040 <read@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x1,-0x8(%rbp)
jge    11b5 <get_random_byte+0x85>
mov    0x2e3d(%rip),%rax
mov    (%rax),%rax
mov    0x2e23(%rip),%rcx
cmp    (%rcx),%rax
je     11b5 <get_random_byte+0x85>
mov    $0x0,%al
call   1030 <abort@plt>
jmp    11ba <get_random_byte+0x8a>
cmpl   $0x1,-0x8(%rbp)
jl     1165 <get_random_byte+0x35>
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_random_byte>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    0x2ef9(%rip),%edi
test   %edi,%edi
jns    1160 <get_random_byte+0x30>
mov    0x2e9e(%rip),%rax
mov    (%rax),%esi
lea    0xead(%rip),%rdi
call   1050 <open@plt>
mov    %eax,%edi
mov    %eax,0x2ed8(%rip)
mov    0x2e71(%rip),%rbx
movq   $0x0,(%rbx)
lea    0xc(%rsp),%rsi
mov    $0x1,%edx
call   1040 <read@plt>
test   %eax,%eax
jg     11b6 <get_random_byte+0x86>
mov    0x2e40(%rip),%r15
lea    0xc(%rsp),%r14
nopl   (%rax)
mov    (%rbx),%rax
cmp    (%r15),%rax
jne    11c4 <get_random_byte+0x94>
mov    0x2e9a(%rip),%edi
movq   $0x0,(%rbx)
mov    %r14,%rsi
mov    $0x1,%edx
call   1040 <read@plt>
test   %eax,%eax
jle    1190 <get_random_byte+0x60>
mov    0xc(%rsp),%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
xor    %eax,%eax
call   1030 <abort@plt>

LLVM IR (-O0):
@get_random_byte.fd = internal global i32 -1
@.str = [13 x i8] c"/dev/urandom\00"
@O_RDONLY = global i32 0
@errno = global i64 0
@EAGAIN = global i64 0
define i32 @get_random_byte() {
%1 = alloca i32
%2 = alloca i32
%3 = load i32, i32* @get_random_byte.fd
%4 = icmp slt i32 %3, 0
br i1 %4, label %5, label %8
5: ; preds = %0
%6 = load i32, i32* @O_RDONLY
%7 = call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %6)
store i32 %7, i32* @get_random_byte.fd
br label %8
8: ; preds = %5, %0
br label %9
9: ; preds = %21, %8
store i64 0, i64* @errno
%10 = load i32, i32* @get_random_byte.fd
%11 = call i32 @read(i32 %10, i32* %1, i32 1)
store i32 %11, i32* %2
%12 = load i32, i32* %2
%13 = icmp slt i32 %12, 1
br i1 %13, label %14, label %20
14: ; preds = %9
%15 = load i64, i64* @errno
%16 = load i64, i64* @EAGAIN
%17 = icmp ne i64 %15, %16
br i1 %17, label %18, label %20
18: ; preds = %14
%19 = call i32 (...) @abort()
unreachable
20: ; preds = %14, %9
br label %21
21: ; preds = %20
%22 = load i32, i32* %2
%23 = icmp slt i32 %22, 1
br i1 %23, label %9, label %24
24: ; preds = %21
%25 = load i32, i32* %1
ret i32 %25
}

LLVM IR (-O2):
@get_random_byte.fd = internal unnamed_addr global i32 -1
@.str = [13 x i8] c"/dev/urandom\00"
@O_RDONLY = global i32 0
@errno = global i64 0
@EAGAIN = global i64 0
define i32 @get_random_byte() {
%1 = alloca i32
%2 = bitcast i32* %1 to i8*
%3 = load i32, i32* @get_random_byte.fd
%4 = icmp slt i32 %3, 0
br i1 %4, label %5, label %8
5: ; preds = %0
%6 = load i32, i32* @O_RDONLY
%7 = tail call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %6)
store i32 %7, i32* @get_random_byte.fd
br label %8
8: ; preds = %5, %0
%9 = phi i32 [ %7, %5 ], [ %3, %0 ]
store i64 0, i64* @errno
%10 = call i32 @read(i32 %9, i32* %1, i32 1)
%11 = icmp slt i32 %10, 1
br i1 %11, label %12, label %22
12: ; preds = %8, %16
%13 = load i64, i64* @errno
%14 = load i64, i64* @EAGAIN
%15 = icmp eq i64 %13, %14
br i1 %15, label %16, label %20
16: ; preds = %12
%17 = load i32, i32* @get_random_byte.fd
store i64 0, i64* @errno
%18 = call i32 @read(i32 %17, i32* %1, i32 1)
%19 = icmp slt i32 %18, 1
br i1 %19, label %12, label %22
20: ; preds = %12
%21 = tail call i32 (...) @abort()
unreachable
22: ; preds = %16, %8
%23 = load i32, i32* %1
ret i32 %23
}