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

/* Variables and functions */
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int fread(void *, int /*<<< orphan*/, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ logmsg(int, char *, char const *, int /*<<< orphan*/);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
bool readRandomData(void *aData, uint32_t aLength) {
  const char *randomDevice = "/dev/urandom";
  FILE *f = fopen(randomDevice, "rb");
  if (!f) {
    logmsg(1, "Error opening %s for reading entropy: %s\n", randomDevice,
           strerror(errno));
    return false;
  }
  if (fread(aData, aLength, 1, f) != 1) {
    logmsg(1, "Short read from %s for reading entropy: %s\n", randomDevice,
           strerror(errno));
    fclose(f);
    return false;
  }
  fclose(f);
  return true;
}


x86 assembly (clang, -O0):
<readRandomData>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
lea    0xe9a(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
lea    0xe98(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11c2 <readRandomData+0x72>
mov    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    0x2e48(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x30(%rbp),%rdx
mov    %eax,%ecx
mov    $0x1,%edi
lea    0xe5f(%rip),%rsi
call   1050 <logmsg@plt>
movl   $0x0,-0x4(%rbp)
jmp    1232 <readRandomData+0xe2>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x28(%rbp),%rcx
mov    $0x1,%edx
call   1030 <fread@plt>
cmp    $0x1,%eax
je     1222 <readRandomData+0xd2>
mov    -0x20(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    0x2df1(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x38(%rbp),%rdx
mov    %eax,%ecx
mov    $0x1,%edi
lea    0xe32(%rip),%rsi
call   1050 <logmsg@plt>
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x0,-0x4(%rbp)
jmp    1232 <readRandomData+0xe2>
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readRandomData>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%r14d
mov    %rdi,%r15
lea    0xe9c(%rip),%rdi
lea    0xea2(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     119f <readRandomData+0x4f>
mov    %rax,%rbx
mov    $0x1,%ebp
mov    %r15,%rdi
mov    %r14d,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   1030 <fread@plt>
cmp    $0x1,%eax
jne    11c9 <readRandomData+0x79>
mov    %rbx,%rdi
call   1040 <fclose@plt>
jmp    11fb <readRandomData+0xab>
mov    0x2e3a(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xe5c(%rip),%rsi
lea    0xe45(%rip),%rdx
mov    $0x1,%edi
mov    %eax,%ecx
call   1050 <logmsg@plt>
jmp    11f9 <readRandomData+0xa9>
mov    0x2e10(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xe5c(%rip),%rsi
lea    0xe1b(%rip),%rdx
mov    $0x1,%edi
mov    %eax,%ecx
call   1050 <logmsg@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
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 = [3 x i8] c"rb\00"
@.str.2 = [42 x i8] c"Error opening %s for reading entropy: %s\0A\00"
@errno = global i32 0
@.str.3 = [44 x i8] c"Short read from %s for reading entropy: %s\0A\00"
define i32 @readRandomData(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32*
store i8* %0, i8** %4
store i32 %1, i32* %5
store i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8** %6
%8 = load i8*, i8** %6
%9 = call i32* @fopen(i8* %8, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %9, i32** %7
%10 = load i32*, i32** %7
%11 = icmp ne i32* %10, null
br i1 %11, label %17, label %12
12: ; preds = %2
%13 = load i8*, i8** %6
%14 = load i32, i32* @errno
%15 = call i32 @strerror(i32 %14)
%16 = call i32 @logmsg(i32 1, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.2, i64 0, i64 0), i8* %13, i32 %15)
store i32 0, i32* %3
br label %33
17: ; preds = %2
%18 = load i8*, i8** %4
%19 = load i32, i32* %5
%20 = load i32*, i32** %7
%21 = call i32 @fread(i8* %18, i32 %19, i32 1, i32* %20)
%22 = icmp ne i32 %21, 1
br i1 %22, label %23, label %30
23: ; preds = %17
%24 = load i8*, i8** %6
%25 = load i32, i32* @errno
%26 = call i32 @strerror(i32 %25)
%27 = call i32 @logmsg(i32 1, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.3, i64 0, i64 0), i8* %24, i32 %26)
%28 = load i32*, i32** %7
%29 = call i32 @fclose(i32* %28)
store i32 0, i32* %3
br label %33
30: ; preds = %17
%31 = load i32*, i32** %7
%32 = call i32 @fclose(i32* %31)
store i32 1, i32* %3
br label %33
33: ; preds = %30, %23, %12
%34 = load i32, i32* %3
ret i32 %34
}

LLVM IR (-O2):
@.str = [13 x i8] c"/dev/urandom\00"
@.str.1 = [3 x i8] c"rb\00"
@.str.2 = [42 x i8] c"Error opening %s for reading entropy: %s\0A\00"
@errno = global i32 0
@.str.3 = [44 x i8] c"Short read from %s for reading entropy: %s\0A\00"
define i32 @readRandomData(i8* nocapture %0, i32 %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 @logmsg(i32 1, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %7)
br label %19
9: ; preds = %2
%10 = tail call i32 @fread(i8* %0, i32 %1, i32 1, i32* %3)
%11 = icmp eq i32 %10, 1
br i1 %11, label %17, label %12
12: ; preds = %9
%13 = load i32, i32* @errno
%14 = tail call i32 @strerror(i32 %13)
%15 = tail call i32 @logmsg(i32 1, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %14)
%16 = tail call i32 @fclose(i32* %3)
br label %19
17: ; preds = %9
%18 = tail call i32 @fclose(i32* %3)
br label %19
19: ; preds = %17, %12, %5
%20 = phi i32 [ 0, %12 ], [ 1, %17 ], [ 0, %5 ]
ret i32 %20
}