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

/* Variables and functions */
int /*<<< orphan*/ LOG_ERR;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int open(char *, int /*<<< orphan*/);
int read(int, int /*<<< orphan*/ *, size_t);
int /*<<< orphan*/ syslog(int /*<<< orphan*/, char *);
int platGenerateCryptoRand(uint8_t *buf, int *len) {
  int fd;
  int rc = 0;
  ssize_t s;
  if ((fd = open("/dev/urandom", O_RDONLY)) == -1) {
    syslog(LOG_ERR, "Failed to open prng driver");
    return 0;
  }
  s = read(fd, buf, (size_t)*len);
  if (s > 0) {
    *len = s;
    rc = 1;
  } else {
    *len = 0;
    rc = 0;
  }
  (void)close(fd);
  return rc;
}


x86 assembly (clang, -O0):
<platGenerateCryptoRand>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0x0,-0x20(%rbp)
mov    0x2e8a(%rip),%rax
mov    (%rax),%esi
lea    0xe99(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0xffffffff,%eax
jne    1199 <platGenerateCryptoRand+0x59>
mov    0x2e51(%rip),%rax
mov    (%rax),%edi
lea    0xe85(%rip),%rsi
call   1050 <syslog@plt>
movl   $0x0,-0x4(%rbp)
jmp    11ed <platGenerateCryptoRand+0xad>
mov    -0x1c(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rax
movslq (%rax),%rdx
call   1040 <read@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jle    11ce <platGenerateCryptoRand+0x8e>
mov    -0x24(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x1,-0x20(%rbp)
jmp    11df <platGenerateCryptoRand+0x9f>
mov    -0x18(%rbp),%rax
movl   $0x0,(%rax)
movl   $0x0,-0x20(%rbp)
mov    -0x1c(%rbp),%edi
call   1030 <close@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<platGenerateCryptoRand>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r15
mov    %rdi,%r14
mov    0x2e94(%rip),%rax
mov    (%rax),%esi
lea    0xea3(%rip),%rdi
call   1060 <open@plt>
cmp    $0xffffffff,%eax
je     118e <platGenerateCryptoRand+0x4e>
mov    %eax,%ebp
movslq (%r15),%rdx
mov    %eax,%edi
mov    %r14,%rsi
call   1040 <read@plt>
xor    %ecx,%ecx
xor    %ebx,%ebx
test   %eax,%eax
setg   %bl
cmovg  %eax,%ecx
mov    %ecx,(%r15)
mov    %ebp,%edi
call   1030 <close@plt>
jmp    11a5 <platGenerateCryptoRand+0x65>
mov    0x2e3b(%rip),%rax
mov    (%rax),%edi
lea    0xe6f(%rip),%rsi
call   1050 <syslog@plt>
xor    %ebx,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [13 x i8] c"/dev/urandom\00"
@O_RDONLY = global i32 0
@LOG_ERR = global i32 0
@.str.1 = [27 x i8] c"Failed to open prng driver\00"
define i32 @platGenerateCryptoRand(i32* %0, i32* %1) {
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i32* %0, i32** %4
store i32* %1, i32** %5
store i32 0, i32* %7
%9 = load i32, i32* @O_RDONLY
%10 = call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %9)
store i32 %10, i32* %6
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %15
12: ; preds = %2
%13 = load i32, i32* @LOG_ERR
%14 = call i32 @syslog(i32 %13, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %3
br label %33
15: ; preds = %2
%16 = load i32, i32* %6
%17 = load i32*, i32** %4
%18 = load i32*, i32** %5
%19 = load i32, i32* %18
%20 = sext i32 %19 to i64
%21 = call i32 @read(i32 %16, i32* %17, i64 %20)
store i32 %21, i32* %8
%22 = load i32, i32* %8
%23 = icmp sgt i32 %22, 0
br i1 %23, label %24, label %27
24: ; preds = %15
%25 = load i32, i32* %8
%26 = load i32*, i32** %5
store i32 %25, i32* %26
store i32 1, i32* %7
br label %29
27: ; preds = %15
%28 = load i32*, i32** %5
store i32 0, i32* %28
store i32 0, i32* %7
br label %29
29: ; preds = %27, %24
%30 = load i32, i32* %6
%31 = call i32 @close(i32 %30)
%32 = load i32, i32* %7
store i32 %32, i32* %3
br label %33
33: ; preds = %29, %12
%34 = load i32, i32* %3
ret i32 %34
}

LLVM IR (-O2):
@.str = [13 x i8] c"/dev/urandom\00"
@O_RDONLY = global i32 0
@LOG_ERR = global i32 0
@.str.1 = [27 x i8] c"Failed to open prng driver\00"
define i32 @platGenerateCryptoRand(i32* nocapture %0, i32* nocapture %1) {
%3 = load i32, i32* @O_RDONLY
%4 = tail call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = icmp eq i32 %4, -1
br i1 %5, label %6, label %9
6: ; preds = %2
%7 = load i32, i32* @LOG_ERR
%8 = tail call i32 @syslog(i32 %7, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0))
br label %17
9: ; preds = %2
%10 = load i32, i32* %1
%11 = sext i32 %10 to i64
%12 = tail call i32 @read(i32 %4, i32* %0, i64 %11)
%13 = icmp sgt i32 %12, 0
%14 = select i1 %13, i32 %12, i32 0
%15 = zext i1 %13 to i32
store i32 %14, i32* %1
%16 = tail call i32 @close(i32 %4)
br label %17
17: ; preds = %9, %6
%18 = phi i32 [ 0, %6 ], [ %15, %9 ]
ret i32 %18
}