#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;

/* Variables and functions */
int O_RDONLY;
int /*<<< orphan*/ close(int);
int open(char *, int);
scalar_t__ read(int, char *, size_t);
int default_CSPRNG(uint8_t *dest, unsigned int size) {
  if (dest == (uint8_t *)0 || (size <= 0))
    return 0;
  int fd = open("/dev/urandom", O_RDONLY | 0);
  if (fd == -1) {
    fd = open("/dev/random", O_RDONLY | 0);
    if (fd == -1) {
      return 0;
    }
  }
  char *ptr = (char *)dest;
  size_t left = (size_t)size;
  while (left > 0) {
    ssize_t bytes_read = read(fd, ptr, left);
    if (bytes_read <= 0) {
      close(fd);
      return 0;
    }
    left -= bytes_read;
    ptr += bytes_read;
  }
  close(fd);
  return 1;
}


x86 assembly (clang, -O0):
<default_CSPRNG>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1154 <default_CSPRNG+0x24>
cmpl   $0x0,-0x14(%rbp)
ja     1160 <default_CSPRNG+0x30>
movl   $0x0,-0x4(%rbp)
jmp    1237 <default_CSPRNG+0x107>
mov    0x2e81(%rip),%rax
mov    (%rax),%esi
or     $0x0,%esi
lea    0xe8d(%rip),%rdi
call   1050 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    11bb <default_CSPRNG+0x8b>
mov    0x2e5c(%rip),%rax
mov    (%rax),%esi
or     $0x0,%esi
lea    0xe75(%rip),%rdi
call   1050 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    11b6 <default_CSPRNG+0x86>
movl   $0x0,-0x4(%rbp)
jmp    1237 <default_CSPRNG+0x107>
jmp    11bb <default_CSPRNG+0x8b>
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x14(%rbp),%eax
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jbe    1228 <default_CSPRNG+0xf8>
mov    -0x18(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1040 <read@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jg     1208 <default_CSPRNG+0xd8>
mov    -0x18(%rbp),%edi
call   1030 <close@plt>
movl   $0x0,-0x4(%rbp)
jmp    1237 <default_CSPRNG+0x107>
mov    -0x30(%rbp),%rcx
mov    -0x28(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x28(%rbp)
mov    -0x30(%rbp),%rax
add    -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
jmp    11ca <default_CSPRNG+0x9a>
mov    -0x18(%rbp),%edi
call   1030 <close@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<default_CSPRNG>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
xor    %r14d,%r14d
test   %rdi,%rdi
je     11be <default_CSPRNG+0x8e>
mov    %esi,%r15d
test   %esi,%esi
je     11be <default_CSPRNG+0x8e>
mov    %rdi,%rbx
mov    0x2e97(%rip),%rbp
mov    0x0(%rbp),%esi
lea    0xea5(%rip),%rdi
call   1050 <open@plt>
mov    %eax,%r12d
cmp    $0xffffffff,%eax
jne    117f <default_CSPRNG+0x4f>
mov    0x0(%rbp),%esi
lea    0xe9b(%rip),%rdi
call   1050 <open@plt>
mov    %eax,%r12d
cmp    $0xffffffff,%eax
je     11be <default_CSPRNG+0x8e>
mov    %r15d,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %r12d,%edi
mov    %rbx,%rsi
mov    %rbp,%rdx
call   1040 <read@plt>
test   %rax,%rax
jle    11b3 <default_CSPRNG+0x83>
add    %rax,%rbx
sub    %rax,%rbp
jne    1190 <default_CSPRNG+0x60>
mov    $0x1,%r14d
jmp    11b6 <default_CSPRNG+0x86>
xor    %r14d,%r14d
mov    %r12d,%edi
call   1030 <close@plt>
mov    %r14d,%eax
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 = [12 x i8] c"/dev/random\00"
define i32 @default_CSPRNG(i32* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i64
store i32* %0, i32** %4
store i32 %1, i32* %5
%10 = load i32*, i32** %4
%11 = icmp eq i32* %10, null
br i1 %11, label %15, label %12
12: ; preds = %2
%13 = load i32, i32* %5
%14 = icmp ule i32 %13, 0
br i1 %14, label %15, label %16
15: ; preds = %12, %2
store i32 0, i32* %3
br label %58
16: ; preds = %12
%17 = load i32, i32* @O_RDONLY
%18 = or i32 %17, 0
%19 = call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %18)
store i32 %19, i32* %6
%20 = load i32, i32* %6
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %30
22: ; preds = %16
%23 = load i32, i32* @O_RDONLY
%24 = or i32 %23, 0
%25 = call i32 @open(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %24)
store i32 %25, i32* %6
%26 = load i32, i32* %6
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %29
28: ; preds = %22
store i32 0, i32* %3
br label %58
29: ; preds = %22
br label %30
30: ; preds = %29, %16
%31 = load i32*, i32** %4
%32 = bitcast i32* %31 to i8*
store i8* %32, i8** %7
%33 = load i32, i32* %5
%34 = zext i32 %33 to i64
store i64 %34, i64* %8
br label %35
35: ; preds = %48, %30
%36 = load i64, i64* %8
%37 = icmp ugt i64 %36, 0
br i1 %37, label %38, label %55
38: ; preds = %35
%39 = load i32, i32* %6
%40 = load i8*, i8** %7
%41 = load i64, i64* %8
%42 = call i64 @read(i32 %39, i8* %40, i64 %41)
store i64 %42, i64* %9
%43 = load i64, i64* %9
%44 = icmp sle i64 %43, 0
br i1 %44, label %45, label %48
45: ; preds = %38
%46 = load i32, i32* %6
%47 = call i32 @close(i32 %46)
store i32 0, i32* %3
br label %58
48: ; preds = %38
%49 = load i64, i64* %9
%50 = load i64, i64* %8
%51 = sub i64 %50, %49
store i64 %51, i64* %8
%52 = load i64, i64* %9
%53 = load i8*, i8** %7
%54 = getelementptr inbounds i8, i8* %53, i64 %52
store i8* %54, i8** %7
br label %35
55: ; preds = %35
%56 = load i32, i32* %6
%57 = call i32 @close(i32 %56)
store i32 1, i32* %3
br label %58
58: ; preds = %55, %45, %28, %15
%59 = load i32, i32* %3
ret i32 %59
}

LLVM IR (-O2):
@.str = [13 x i8] c"/dev/urandom\00"
@O_RDONLY = global i32 0
@.str.1 = [12 x i8] c"/dev/random\00"
define i32 @default_CSPRNG(i32* %0, i32 %1) {
%3 = icmp eq i32* %0, null
%4 = icmp eq i32 %1, 0
%5 = or i1 %3, %4
br i1 %5, label %30, label %6
6: ; preds = %2
%7 = load i32, i32* @O_RDONLY
%8 = tail call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %7)
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %14
10: ; preds = %6
%11 = load i32, i32* @O_RDONLY
%12 = tail call i32 @open(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %11)
%13 = icmp eq i32 %12, -1
br i1 %13, label %30, label %14
14: ; preds = %6, %10
%15 = phi i32 [ %12, %10 ], [ %8, %6 ]
%16 = zext i32 %1 to i64
%17 = bitcast i32* %0 to i8*
br label %18
18: ; preds = %14, %23
%19 = phi i64 [ %24, %23 ], [ %16, %14 ]
%20 = phi i8* [ %25, %23 ], [ %17, %14 ]
%21 = tail call i64 @read(i32 %15, i8* %20, i64 %19)
%22 = icmp slt i64 %21, 1
br i1 %22, label %27, label %23
23: ; preds = %18
%24 = sub i64 %19, %21
%25 = getelementptr inbounds i8, i8* %20, i64 %21
%26 = icmp eq i64 %24, 0
br i1 %26, label %27, label %18
27: ; preds = %23, %18
%28 = phi i32 [ 0, %18 ], [ 1, %23 ]
%29 = tail call i32 @close(i32 %15)
br label %30
30: ; preds = %27, %10, %2
%31 = phi i32 [ 0, %2 ], [ 0, %10 ], [ %28, %27 ]
ret i32 %31
}