#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 scalar_t__ uint32_t;
typedef scalar_t__ ssize_t;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ die(char *, int /*<<< orphan*/);
int /*<<< orphan*/ errno;
int open(char *, int /*<<< orphan*/);
scalar_t__ read(int, scalar_t__ *, int);
uint32_t secure_random() {
  int fd = open("/dev/urandom", O_RDONLY);
  uint32_t res = 0;
  size_t pos = 0;
  while (pos < sizeof(uint32_t)) {
    ssize_t result = read(fd, &res + pos, (sizeof res) - pos);
    if (result < 0) {
      die("Unable to dead from /dev/urandom", errno);
    }
    pos += result;
  }
  close(fd);
  return res;
}


x86 assembly (clang, -O0):
<secure_random>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    0x2e99(%rip),%rax
mov    (%rax),%esi
lea    0xebc(%rip),%rdi
call   1050 <open@plt>
mov    %eax,-0x4(%rbp)
movq   $0x0,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
cmpq   $0x8,-0x18(%rbp)
jae    11d2 <secure_random+0x92>
mov    -0x4(%rbp),%edi
mov    -0x18(%rbp),%rax
lea    -0x10(%rbp),%rsi
shl    $0x3,%rax
add    %rax,%rsi
mov    $0x8,%eax
sub    -0x18(%rbp),%rax
mov    %eax,%edx
call   1040 <read@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jge    11c1 <secure_random+0x81>
mov    0x2e25(%rip),%rax
mov    (%rax),%esi
lea    0xe44(%rip),%rdi
call   1060 <die@plt>
mov    -0x20(%rbp),%rax
add    -0x18(%rbp),%rax
mov    %rax,-0x18(%rbp)
jmp    1170 <secure_random+0x30>
mov    -0x4(%rbp),%edi
call   1030 <close@plt>
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<secure_random>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    0x2e95(%rip),%rax
mov    (%rax),%esi
lea    0xeb8(%rip),%rdi
call   1050 <open@plt>
mov    %eax,%r15d
movq   $0x0,0x8(%rsp)
xor    %ebp,%ebp
mov    0x2e62(%rip),%r12
lea    0xe83(%rip),%r14
jmp    1189 <secure_random+0x49>
nop
add    %rbx,%rbp
cmp    $0x8,%rbp
jae    11b6 <secure_random+0x76>
lea    (%rsp,%rbp,8),%rsi
add    $0x8,%rsi
mov    $0x8,%edx
sub    %ebp,%edx
mov    %r15d,%edi
call   1040 <read@plt>
mov    %rax,%rbx
test   %rax,%rax
jns    1180 <secure_random+0x40>
mov    (%r12),%esi
mov    %r14,%rdi
call   1060 <die@plt>
jmp    1180 <secure_random+0x40>
mov    %r15d,%edi
call   1030 <close@plt>
mov    0x8(%rsp),%rax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [13 x i8] c"/dev/urandom\00"
@O_RDONLY = global i32 0
@.str.1 = [33 x i8] c"Unable to dead from /dev/urandom\00"
@errno = global i32 0
define i64 @secure_random() {
%1 = alloca i32
%2 = alloca i64
%3 = alloca i64
%4 = alloca i64
%5 = load i32, i32* @O_RDONLY
%6 = call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %5)
store i32 %6, i32* %1
store i64 0, i64* %2
store i64 0, i64* %3
br label %7
7: ; preds = %23, %0
%8 = load i64, i64* %3
%9 = icmp ult i64 %8, 8
br i1 %9, label %10, label %27
10: ; preds = %7
%11 = load i32, i32* %1
%12 = load i64, i64* %3
%13 = getelementptr inbounds i64, i64* %2, i64 %12
%14 = load i64, i64* %3
%15 = sub i64 8, %14
%16 = trunc i64 %15 to i32
%17 = call i64 @read(i32 %11, i64* %13, i32 %16)
store i64 %17, i64* %4
%18 = load i64, i64* %4
%19 = icmp slt i64 %18, 0
br i1 %19, label %20, label %23
20: ; preds = %10
%21 = load i32, i32* @errno
%22 = call i32 @die(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i32 %21)
br label %23
23: ; preds = %20, %10
%24 = load i64, i64* %4
%25 = load i64, i64* %3
%26 = add i64 %25, %24
store i64 %26, i64* %3
br label %7
27: ; preds = %7
%28 = load i32, i32* %1
%29 = call i32 @close(i32 %28)
%30 = load i64, i64* %2
ret i64 %30
}

LLVM IR (-O2):
@.str = [13 x i8] c"/dev/urandom\00"
@O_RDONLY = global i32 0
@.str.1 = [33 x i8] c"Unable to dead from /dev/urandom\00"
@errno = global i32 0
define i64 @secure_random() {
%1 = alloca i64
%2 = load i32, i32* @O_RDONLY
%3 = tail call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %2)
%4 = bitcast i64* %1 to i8*
store i64 0, i64* %1
br label %5
5: ; preds = %0, %15
%6 = phi i64 [ 0, %0 ], [ %16, %15 ]
%7 = getelementptr inbounds i64, i64* %1, i64 %6
%8 = trunc i64 %6 to i32
%9 = sub nuw nsw i32 8, %8
%10 = call i64 @read(i32 %3, i64* %7, i32 %9)
%11 = icmp slt i64 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %5
%13 = load i32, i32* @errno
%14 = tail call i32 @die(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i32 %13)
br label %15
15: ; preds = %12, %5
%16 = add i64 %10, %6
%17 = icmp ult i64 %16, 8
br i1 %17, label %5, label %18
18: ; preds = %15
%19 = tail call i32 @close(i32 %3)
%20 = load i64, i64* %1
ret i64 %20
}