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

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
size_t fread(void *, int, size_t, int /*<<< orphan*/ *);
int getRandom(void *out, size_t len) {
  FILE *urandom = fopen("/dev/urandom", "r");
  if (urandom != NULL) {
    if (fread(out, 1, len, urandom) == len) {
      fclose(urandom);
      return 1;
    }
    fclose(urandom);
  }
  return 0;
}


x86 assembly (clang, -O0):
<getRandom>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
lea    0xeb9(%rip),%rdi
lea    0xebf(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     11a0 <getRandom+0x70>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
cmp    -0x18(%rbp),%rax
jne    1197 <getRandom+0x67>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x1,-0x4(%rbp)
jmp    11a7 <getRandom+0x77>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getRandom>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r15
lea    0xebc(%rip),%rdi
lea    0xec2(%rip),%rsi
call   1050 <fopen@plt>
test   %rax,%rax
je     117e <getRandom+0x4e>
mov    %rax,%rbx
mov    %r15,%rdi
mov    $0x1,%esi
mov    %r14,%rdx
mov    %rax,%rcx
call   1030 <fread@plt>
xor    %ebp,%ebp
cmp    %r14,%rax
sete   %bpl
mov    %rbx,%rdi
call   1040 <fclose@plt>
jmp    1180 <getRandom+0x50>
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [13 x i8] c"/dev/urandom\00"
@.str.1 = [2 x i8] c"r\00"
define i32 @getRandom(i8* %0, i64 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i32*
store i8* %0, i8** %4
store i64 %1, i64* %5
%7 = call i32* @fopen(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %7, i32** %6
%8 = load i32*, i32** %6
%9 = icmp ne i32* %8, null
br i1 %9, label %10, label %23
10: ; preds = %2
%11 = load i8*, i8** %4
%12 = load i64, i64* %5
%13 = load i32*, i32** %6
%14 = call i64 @fread(i8* %11, i32 1, i64 %12, i32* %13)
%15 = load i64, i64* %5
%16 = icmp eq i64 %14, %15
br i1 %16, label %17, label %20
17: ; preds = %10
%18 = load i32*, i32** %6
%19 = call i32 @fclose(i32* %18)
store i32 1, i32* %3
br label %24
20: ; preds = %10
%21 = load i32*, i32** %6
%22 = call i32 @fclose(i32* %21)
br label %23
23: ; preds = %20, %2
store i32 0, i32* %3
br label %24
24: ; preds = %23, %17
%25 = load i32, i32* %3
ret i32 %25
}

LLVM IR (-O2):
@.str = [13 x i8] c"/dev/urandom\00"
@.str.1 = [2 x i8] c"r\00"
define i32 @getRandom(i8* nocapture %0, i64 %1) {
%3 = tail call i32* @fopen(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %10, label %5
5: ; preds = %2
%6 = tail call i64 @fread(i8* %0, i32 1, i64 %1, i32* %3)
%7 = icmp eq i64 %6, %1
%8 = tail call i32 @fclose(i32* %3)
%9 = zext i1 %7 to i32
br label %10
10: ; preds = %5, %2
%11 = phi i32 [ 0, %2 ], [ %9, %5 ]
ret i32 %11
}