#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*/ errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
size_t fread(void *, size_t, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ log_fatal(char *, char *, int /*<<< orphan*/);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
int random_bytes(void *dst, size_t n) {
  FILE *f = fopen("/dev/urandom", "rb");
  if (!f) {
    log_fatal("random", "unable to read /dev/urandom: %s", strerror(errno));
  }
  size_t r = fread(dst, n, 1, f);
  fclose(f);
  if (r < 1) {
    return 0;
  }
  return 1;
}


x86 assembly (clang, -O0):
<random_bytes>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
lea    0xe99(%rip),%rdi
lea    0xe9f(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11a5 <random_bytes+0x55>
mov    0x2e57(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    %eax,%edx
lea    0xe6d(%rip),%rdi
lea    0xe70(%rip),%rsi
call   1060 <log_fatal@plt>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rcx
mov    $0x1,%edx
call   1030 <fread@plt>
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
cmpq   $0x1,-0x28(%rbp)
jae    11df <random_bytes+0x8f>
movl   $0x0,-0x4(%rbp)
jmp    11e6 <random_bytes+0x96>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<random_bytes>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r14
mov    %rdi,%r15
lea    0xe9e(%rip),%rdi
lea    0xea4(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    1199 <random_bytes+0x49>
mov    0x2e63(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xe7b(%rip),%rdi
lea    0xe7e(%rip),%rsi
mov    %eax,%edx
call   1060 <log_fatal@plt>
mov    %r15,%rdi
mov    %r14,%rsi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1030 <fread@plt>
mov    %rax,%r14
mov    %rbx,%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
test   %r14,%r14
setne  %al
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [13 x i8] c"/dev/urandom\00"
@.str.1 = [3 x i8] c"rb\00"
@.str.2 = [7 x i8] c"random\00"
@.str.3 = [32 x i8] c"unable to read /dev/urandom: %s\00"
@errno = global i32 0
define i32 @random_bytes(i8* %0, i64 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i32*
%7 = alloca i64
store i8* %0, i8** %4
store i64 %1, i64* %5
%8 = 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* %8, i32** %6
%9 = load i32*, i32** %6
%10 = icmp ne i32* %9, null
br i1 %10, label %15, label %11
11: ; preds = %2
%12 = load i32, i32* @errno
%13 = call i32 @strerror(i32 %12)
%14 = call i32 @log_fatal(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0), i32 %13)
br label %15
15: ; preds = %11, %2
%16 = load i8*, i8** %4
%17 = load i64, i64* %5
%18 = load i32*, i32** %6
%19 = call i64 @fread(i8* %16, i64 %17, i32 1, i32* %18)
store i64 %19, i64* %7
%20 = load i32*, i32** %6
%21 = call i32 @fclose(i32* %20)
%22 = load i64, i64* %7
%23 = icmp ult i64 %22, 1
br i1 %23, label %24, label %25
24: ; preds = %15
store i32 0, i32* %3
br label %26
25: ; preds = %15
store i32 1, i32* %3
br label %26
26: ; preds = %25, %24
%27 = load i32, i32* %3
ret i32 %27
}

LLVM IR (-O2):
@.str = [13 x i8] c"/dev/urandom\00"
@.str.1 = [3 x i8] c"rb\00"
@.str.2 = [7 x i8] c"random\00"
@.str.3 = [32 x i8] c"unable to read /dev/urandom: %s\00"
@errno = global i32 0
define i32 @random_bytes(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 ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %5, label %9
5: ; preds = %2
%6 = load i32, i32* @errno
%7 = tail call i32 @strerror(i32 %6)
%8 = tail call i32 @log_fatal(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0), i32 %7)
br label %9
9: ; preds = %5, %2
%10 = tail call i64 @fread(i8* %0, i64 %1, i32 1, i32* %3)
%11 = tail call i32 @fclose(i32* %3)
%12 = icmp ne i64 %10, 0
%13 = zext i1 %12 to i32
ret i32 %13
}