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

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
size_t fread(int /*<<< orphan*/ *, int, int, int /*<<< orphan*/ *);
int getrand(uint8_t *dst, int len, void *dat) {
  FILE *fp;
  size_t bytes_read;
  if ((fp = fopen("/dev/urandom", "rb")) == NULL)
    return 0;
  bytes_read = fread(dst, 1, len, fp);
  fclose(fp);
  return bytes_read;
}


x86 assembly (clang, -O0):
<getrand>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
lea    0xeb6(%rip),%rdi
lea    0xebc(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    1170 <getrand+0x40>
movl   $0x0,-0x4(%rbp)
jmp    1199 <getrand+0x69>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%edx
mov    -0x28(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x30(%rbp),%rax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getrand>:
push   %rbp
push   %r14
push   %rbx
mov    %esi,%r14d
mov    %rdi,%rbx
lea    0xebf(%rip),%rdi
lea    0xec5(%rip),%rsi
call   1050 <fopen@plt>
test   %rax,%rax
je     1175 <getrand+0x45>
mov    %rax,%rbp
mov    %rbx,%rdi
mov    $0x1,%esi
mov    %r14d,%edx
mov    %rax,%rcx
call   1030 <fread@plt>
mov    %rax,%rbx
mov    %rbp,%rdi
call   1040 <fclose@plt>
jmp    1177 <getrand+0x47>
xor    %ebx,%ebx
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

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

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