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

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int open(char *, int /*<<< orphan*/);
scalar_t__ read(int, int /*<<< orphan*/ *, int);
int /*<<< orphan*/ snprintf(char *, int, char *, char *);
char *strerror(int /*<<< orphan*/);
int /*<<< orphan*/ strlen(char *);
int /*<<< orphan*/ write(int, char *, int /*<<< orphan*/);
uint8_t _rand_u8() {
  uint8_t ret = 0;
  ssize_t callStat = 0;
  char errorMsg[128] = "";
  int fd = open("/dev/urandom", O_RDONLY);
  if (fd < 0) {
    snprintf(errorMsg, sizeof(errorMsg), "Failed to open ramdom device: %s\n",
             strerror(errno));
    goto ENDS;
  }
  callStat = read(fd, &(ret), sizeof(ret));
  if (callStat < 0) {
    snprintf(errorMsg, sizeof(errorMsg),
             "Failed to read from ramdom device: %s\n", strerror(errno));
    goto ENDS;
  }
ENDS:
  if (fd > 0) {
    close(fd);
    fd = -1;
  }
  if (errorMsg[0]) {
    write(2, errorMsg, strlen(errorMsg));
  }
  return ret;
}


x86 assembly (clang, -O0):
<_rand_u8>:
push   %rbp
mov    %rsp,%rbp
sub    $0xb0,%rsp
movl   $0x0,-0x4(%rbp)
movq   $0x0,-0x10(%rbp)
lea    -0x90(%rbp),%rdi
xor    %esi,%esi
mov    $0x80,%edx
call   1060 <memset@plt>
mov    0x2e34(%rip),%rax
mov    (%rax),%esi
lea    0xe43(%rip),%rdi
call   1090 <open@plt>
mov    %eax,-0x94(%rbp)
cmpl   $0x0,-0x94(%rbp)
jge    1211 <_rand_u8+0x91>
lea    -0x90(%rbp),%rax
mov    %rax,-0xa0(%rbp)
mov    0x2dee(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0xa0(%rbp),%rdi
mov    %rax,%rcx
mov    $0x80,%esi
lea    0xe06(%rip),%rdx
call   1050 <snprintf@plt>
jmp    1275 <_rand_u8+0xf5>
mov    -0x94(%rbp),%edi
lea    -0x4(%rbp),%rsi
mov    $0x4,%edx
call   1080 <read@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jge    1270 <_rand_u8+0xf0>
lea    -0x90(%rbp),%rax
mov    %rax,-0xa8(%rbp)
mov    0x2d8f(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0xa8(%rbp),%rdi
mov    %rax,%rcx
mov    $0x80,%esi
lea    0xdc9(%rip),%rdx
call   1050 <snprintf@plt>
jmp    1275 <_rand_u8+0xf5>
jmp    1275 <_rand_u8+0xf5>
cmpl   $0x0,-0x94(%rbp)
jle    1297 <_rand_u8+0x117>
mov    -0x94(%rbp),%edi
call   1070 <close@plt>
movl   $0xffffffff,-0x94(%rbp)
ff ff ff
cmpb   $0x0,-0x90(%rbp)
je     12d1 <_rand_u8+0x151>
lea    -0x90(%rbp),%rax
mov    %rax,-0xb0(%rbp)
lea    -0x90(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0xb0(%rbp),%rsi
mov    %eax,%edx
mov    $0x2,%edi
call   1030 <write@plt>
mov    -0x4(%rbp),%eax
add    $0xb0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_rand_u8>:
push   %rbx
sub    $0x90,%rsp
movl   $0x0,0xc(%rsp)
xorps  %xmm0,%xmm0
movaps %xmm0,0x80(%rsp)
movaps %xmm0,0x70(%rsp)
movaps %xmm0,0x60(%rsp)
movaps %xmm0,0x50(%rsp)
movaps %xmm0,0x40(%rsp)
movaps %xmm0,0x30(%rsp)
movaps %xmm0,0x20(%rsp)
movaps %xmm0,0x10(%rsp)
mov    0x2e33(%rip),%rax
mov    (%rax),%esi
lea    0xe42(%rip),%rdi
call   1080 <open@plt>
test   %eax,%eax
js     11e5 <_rand_u8+0x75>
mov    %eax,%ebx
lea    0xc(%rsp),%rsi
mov    %eax,%edi
mov    $0x4,%edx
call   1070 <read@plt>
test   %rax,%rax
js     120e <_rand_u8+0x9e>
test   %ebx,%ebx
jne    1239 <_rand_u8+0xc9>
jmp    1240 <_rand_u8+0xd0>
mov    0x2dec(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xe13(%rip),%rdx
lea    0x10(%rsp),%rdi
mov    $0x80,%esi
mov    %rax,%rcx
call   1050 <snprintf@plt>
jmp    1240 <_rand_u8+0xd0>
mov    0x2dc3(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xe0c(%rip),%rdx
lea    0x10(%rsp),%rdi
mov    $0x80,%esi
mov    %rax,%rcx
call   1050 <snprintf@plt>
test   %ebx,%ebx
je     1240 <_rand_u8+0xd0>
mov    %ebx,%edi
call   1060 <close@plt>
cmpb   $0x0,0x10(%rsp)
je     1263 <_rand_u8+0xf3>
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    $0x2,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    0xc(%rsp),%eax
add    $0x90,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [13 x i8] c"/dev/urandom\00"
@O_RDONLY = global i32 0
@.str.1 = [34 x i8] c"Failed to open ramdom device: %s\0A\00"
@errno = global i32 0
@.str.2 = [39 x i8] c"Failed to read from ramdom device: %s\0A\00"
define i32 @_rand_u8() {
%1 = alloca i32
%2 = alloca i64
%3 = alloca [128 x i8]
%4 = alloca i32
store i32 0, i32* %1
store i64 0, i64* %2
%5 = bitcast [128 x i8]* %3 to i8*
%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* %4
%8 = load i32, i32* %4
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %15
10: ; preds = %0
%11 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
%12 = load i32, i32* @errno
%13 = call i8* @strerror(i32 %12)
%14 = call i32 @snprintf(i8* %11, i32 128, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0), i8* %13)
br label %26
15: ; preds = %0
%16 = load i32, i32* %4
%17 = call i64 @read(i32 %16, i32* %1, i32 4)
store i64 %17, i64* %2
%18 = load i64, i64* %2
%19 = icmp slt i64 %18, 0
br i1 %19, label %20, label %25
20: ; preds = %15
%21 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
%22 = load i32, i32* @errno
%23 = call i8* @strerror(i32 %22)
%24 = call i32 @snprintf(i8* %21, i32 128, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.2, i64 0, i64 0), i8* %23)
br label %26
25: ; preds = %15
br label %26
26: ; preds = %25, %20, %10
%27 = load i32, i32* %4
%28 = icmp sgt i32 %27, 0
br i1 %28, label %29, label %32
29: ; preds = %26
%30 = load i32, i32* %4
%31 = call i32 @close(i32 %30)
store i32 -1, i32* %4
br label %32
32: ; preds = %29, %26
%33 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
%34 = load i8, i8* %33
%35 = icmp ne i8 %34, 0
br i1 %35, label %36, label %41
36: ; preds = %32
%37 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
%38 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
%39 = call i32 @strlen(i8* %38)
%40 = call i32 @write(i32 2, i8* %37, i32 %39)
br label %41
41: ; preds = %36, %32
%42 = load i32, i32* %1
ret i32 %42
}

LLVM IR (-O2):
@.str = [13 x i8] c"/dev/urandom\00"
@O_RDONLY = global i32 0
@.str.1 = [34 x i8] c"Failed to open ramdom device: %s\0A\00"
@errno = global i32 0
@.str.2 = [39 x i8] c"Failed to read from ramdom device: %s\0A\00"
define i32 @_rand_u8() {
%1 = alloca i32
%2 = alloca [128 x i8]
%3 = bitcast i32* %1 to i8*
store i32 0, i32* %1
%4 = getelementptr inbounds [128 x i8], [128 x i8]* %2, i64 0, i64 0
%5 = load i32, i32* @O_RDONLY
%6 = tail call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %5)
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %12
8: ; preds = %0
%9 = load i32, i32* @errno
%10 = tail call i8* @strerror(i32 %9)
%11 = call i32 @snprintf(i8* %4, i32 128, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0), i8* %10)
br label %23
12: ; preds = %0
%13 = call i64 @read(i32 %6, i32* %1, i32 4)
%14 = icmp slt i64 %13, 0
br i1 %14, label %15, label %19
15: ; preds = %12
%16 = load i32, i32* @errno
%17 = tail call i8* @strerror(i32 %16)
%18 = call i32 @snprintf(i8* %4, i32 128, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.2, i64 0, i64 0), i8* %17)
br label %19
19: ; preds = %12, %15
%20 = icmp eq i32 %6, 0
br i1 %20, label %23, label %21
21: ; preds = %19
%22 = call i32 @close(i32 %6)
br label %23
23: ; preds = %8, %21, %19
%24 = load i8, i8* %4
%25 = icmp eq i8 %24, 0
br i1 %25, label %29, label %26
26: ; preds = %23
%27 = call i32 @strlen(i8* %4)
%28 = call i32 @write(i32 2, i8* %4, i32 %27)
br label %29
29: ; preds = %26, %23
%30 = load i32, i32* %1
ret i32 %30
}