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

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int fdevent_open_cloexec(char const *const, int /*<<< orphan*/,
                         int /*<<< orphan*/);
scalar_t__ li_getentropy(unsigned char *, size_t);
int read(int, unsigned char *, size_t);
int li_rand_device_bytes(unsigned char *buf, int num) {
  static const char *const devices[] = {"/dev/urandom", "/dev/random"};
  if (0 == li_getentropy(buf, (size_t)num))
    return 1;
  for (unsigned int u = 0; u < sizeof(devices) / sizeof(devices[0]); ++u) {
    int fd = fdevent_open_cloexec(devices[u], O_RDONLY, 0);
    if (fd >= 0) {
      ssize_t rd = 0;
      rd = read(fd, buf, (size_t)num);
      close(fd);
      if (rd == num) {
        return 1;
      }
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<li_rand_device_bytes>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
movslq -0x14(%rbp),%rsi
call   1030 <li_getentropy@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
jne    1176 <li_rand_device_bytes+0x36>
movl   $0x1,-0x4(%rbp)
jmp    1210 <li_rand_device_bytes+0xd0>
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    $0x2,%rax
jae    1209 <li_rand_device_bytes+0xc9>
mov    -0x18(%rbp),%eax
mov    %eax,%ecx
lea    0x2c6a(%rip),%rax
mov    (%rax,%rcx,8),%rdi
mov    0x2e3f(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1060 <fdevent_open_cloexec@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jl     11f6 <li_rand_device_bytes+0xb6>
movl   $0x0,-0x20(%rbp)
mov    -0x1c(%rbp),%edi
mov    -0x10(%rbp),%rsi
movslq -0x14(%rbp),%rdx
call   1050 <read@plt>
mov    %eax,-0x20(%rbp)
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
mov    -0x20(%rbp),%eax
cmp    -0x14(%rbp),%eax
jne    11f1 <li_rand_device_bytes+0xb1>
movl   $0x1,-0x4(%rbp)
jmp    1210 <li_rand_device_bytes+0xd0>
jmp    11f6 <li_rand_device_bytes+0xb6>
jmp    11fb <li_rand_device_bytes+0xbb>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    117d <li_rand_device_bytes+0x3d>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<li_rand_device_bytes>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r15
movslq %esi,%rbx
mov    %rbx,%rsi
call   1030 <li_getentropy@plt>
mov    $0x1,%r14d
test   %rax,%rax
je     11d6 <li_rand_device_bytes+0x96>
mov    0x2e7d(%rip),%r13
mov    0x0(%r13),%esi
lea    0xe8a(%rip),%rdi
xor    %edx,%edx
call   1060 <fdevent_open_cloexec@plt>
test   %eax,%eax
js     119f <li_rand_device_bytes+0x5f>
mov    %eax,%ebp
mov    %eax,%edi
mov    %r15,%rsi
mov    %rbx,%rdx
call   1050 <read@plt>
mov    %eax,%r12d
mov    %ebp,%edi
call   1040 <close@plt>
cmp    %ebx,%r12d
je     11d6 <li_rand_device_bytes+0x96>
mov    0x0(%r13),%esi
lea    0xe63(%rip),%rdi
xor    %edx,%edx
call   1060 <fdevent_open_cloexec@plt>
test   %eax,%eax
js     11d3 <li_rand_device_bytes+0x93>
mov    %eax,%ebp
mov    %eax,%edi
mov    %r15,%rsi
mov    %rbx,%rdx
call   1050 <read@plt>
mov    %eax,%r15d
mov    %ebp,%edi
call   1040 <close@plt>
cmp    %ebx,%r15d
je     11d6 <li_rand_device_bytes+0x96>
xor    %r14d,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@li_rand_device_bytes.devices = internal constant [2 x i8*] [i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i32 0, i32 0), i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i32 0, i32 0)]
@.str = [13 x i8] c"/dev/urandom\00"
@.str.1 = [12 x i8] c"/dev/random\00"
@O_RDONLY = global i32 0
define i32 @li_rand_device_bytes(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
%9 = load i8*, i8** %4
%10 = load i32, i32* %5
%11 = sext i32 %10 to i64
%12 = call i64 @li_getentropy(i8* %9, i64 %11)
%13 = icmp eq i64 0, %12
br i1 %13, label %14, label %15
14: ; preds = %2
store i32 1, i32* %3
br label %47
15: ; preds = %2
store i32 0, i32* %6
br label %16
16: ; preds = %43, %15
%17 = load i32, i32* %6
%18 = zext i32 %17 to i64
%19 = icmp ult i64 %18, 2
br i1 %19, label %20, label %46
20: ; preds = %16
%21 = load i32, i32* %6
%22 = zext i32 %21 to i64
%23 = getelementptr inbounds [2 x i8*], [2 x i8*]* @li_rand_device_bytes.devices, i64 0, i64 %22
%24 = load i8*, i8** %23
%25 = load i32, i32* @O_RDONLY
%26 = call i32 @fdevent_open_cloexec(i8* %24, i32 %25, i32 0)
store i32 %26, i32* %7
%27 = load i32, i32* %7
%28 = icmp sge i32 %27, 0
br i1 %28, label %29, label %42
29: ; preds = %20
store i32 0, i32* %8
%30 = load i32, i32* %7
%31 = load i8*, i8** %4
%32 = load i32, i32* %5
%33 = sext i32 %32 to i64
%34 = call i32 @read(i32 %30, i8* %31, i64 %33)
store i32 %34, i32* %8
%35 = load i32, i32* %7
%36 = call i32 @close(i32 %35)
%37 = load i32, i32* %8
%38 = load i32, i32* %5
%39 = icmp eq i32 %37, %38
br i1 %39, label %40, label %41
40: ; preds = %29
store i32 1, i32* %3
br label %47
41: ; preds = %29
br label %42
42: ; preds = %41, %20
br label %43
43: ; preds = %42
%44 = load i32, i32* %6
%45 = add i32 %44, 1
store i32 %45, i32* %6
br label %16
46: ; preds = %16
store i32 0, i32* %3
br label %47
47: ; preds = %46, %40, %14
%48 = load i32, i32* %3
ret i32 %48
}

LLVM IR (-O2):
@.str = [13 x i8] c"/dev/urandom\00"
@.str.1 = [12 x i8] c"/dev/random\00"
@O_RDONLY = global i32 0
define i32 @li_rand_device_bytes(i8* %0, i32 %1) {
%3 = sext i32 %1 to i64
%4 = tail call i64 @li_getentropy(i8* %0, i64 %3)
%5 = icmp eq i64 %4, 0
br i1 %5, label %23, label %6
6: ; preds = %2
%7 = load i32, i32* @O_RDONLY
%8 = tail call i32 @fdevent_open_cloexec(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %7, i32 0)
%9 = icmp sgt i32 %8, -1
br i1 %9, label %10, label %14
10: ; preds = %6
%11 = tail call i32 @read(i32 %8, i8* %0, i64 %3)
%12 = tail call i32 @close(i32 %8)
%13 = icmp eq i32 %11, %1
br i1 %13, label %23, label %14
14: ; preds = %10, %6
%15 = load i32, i32* @O_RDONLY
%16 = tail call i32 @fdevent_open_cloexec(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %15, i32 0)
%17 = icmp sgt i32 %16, -1
br i1 %17, label %18, label %22
18: ; preds = %14
%19 = tail call i32 @read(i32 %16, i8* %0, i64 %3)
%20 = tail call i32 @close(i32 %16)
%21 = icmp eq i32 %19, %1
br i1 %21, label %23, label %22
22: ; preds = %18, %14
br label %23
23: ; preds = %10, %18, %22, %2
%24 = phi i32 [ 1, %2 ], [ 1, %10 ], [ 1, %18 ], [ 0, %22 ]
ret i32 %24
}