#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 fluff_random_urandom(void *buf, size_t bufsize) {
  FILE *urandom;
  size_t red;
  if (!(urandom = fopen("/dev/urandom", "r"))) {
    return -1;
  }
  red = fread(buf, 1, bufsize, urandom);
  fclose(urandom);
  return (red == bufsize) ? 0 : 1;
}


x86 assembly (clang, -O0):
<fluff_random_urandom>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%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)
cmp    $0x0,%rax
jne    116d <fluff_random_urandom+0x3d>
movl   $0xffffffff,-0x4(%rbp)
jmp    11a8 <fluff_random_urandom+0x78>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x28(%rbp),%rdx
mov    -0x18(%rbp),%rsi
mov    $0x1,%eax
xor    %ecx,%ecx
cmp    %rsi,%rdx
cmove  %ecx,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fluff_random_urandom>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r15
mov    %rdi,%r14
lea    0xebe(%rip),%rdi
lea    0xec4(%rip),%rsi
call   1050 <fopen@plt>
test   %rax,%rax
je     117e <fluff_random_urandom+0x4e>
mov    %rax,%rbx
mov    %r14,%rdi
mov    $0x1,%esi
mov    %r15,%rdx
mov    %rax,%rcx
call   1030 <fread@plt>
mov    %rax,%r14
mov    %rbx,%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
cmp    %r15,%r14
setne  %al
jmp    1183 <fluff_random_urandom+0x53>
mov    $0xffffffff,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
@.str = [13 x i8] c"/dev/urandom\00"
@.str.1 = [2 x i8] c"r\00"
define i32 @fluff_random_urandom(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 = tail call i32 @fclose(i32* %3)
%8 = icmp ne i64 %6, %1
%9 = zext i1 %8 to i32
br label %10
10: ; preds = %2, %5
%11 = phi i32 [ %9, %5 ], [ -1, %2 ]
ret i32 %11
}