#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*/ *);
scalar_t__ feof(int /*<<< orphan*/ *);
scalar_t__ ferror(int /*<<< orphan*/ *);
int fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ sprintf(char *, char *, int);
size_t strlen(char *);
int read_random(char *buffer, size_t size) {
  char *p = buffer;
  int data_read = 0;
  if (!buffer || size < 2)
    return -1;
  FILE *randfd = fopen("/dev/urandom", "r");
  if (!randfd) {
    randfd = fopen("/dev/random", "r");
    if (!randfd)
      return -1;
  }
  while (strlen(buffer) < size) {
    int c = fgetc(randfd);
    if (ferror(randfd) != 0 || feof(randfd) != 0) {
      break;
    }
    if (c < 1)
      continue;
    sprintf(p, "%02x", c);
    p += 2;
  }
ret:
  fclose(randfd);
  return data_read;
}


x86 assembly (clang, -O0):
<read_random>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11a5 <read_random+0x35>
cmpq   $0x2,-0x18(%rbp)
jae    11b1 <read_random+0x41>
movl   $0xffffffff,-0x4(%rbp)
jmp    129c <read_random+0x12c>
lea    0xe48(%rip),%rdi
lea    0xe4e(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1206 <read_random+0x96>
lea    0xe35(%rip),%rdi
lea    0xe2c(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1201 <read_random+0x91>
movl   $0xffffffff,-0x4(%rbp)
jmp    129c <read_random+0x12c>
jmp    1206 <read_random+0x96>
jmp    120b <read_random+0x9b>
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
cmp    -0x18(%rbp),%rax
jae    1288 <read_random+0x118>
mov    -0x30(%rbp),%rdi
call   1060 <fgetc@plt>
mov    %eax,-0x34(%rbp)
mov    -0x30(%rbp),%rdi
call   1030 <ferror@plt>
cmp    $0x0,%rax
jne    1250 <read_random+0xe0>
mov    -0x30(%rbp),%rdi
call   1070 <feof@plt>
cmp    $0x0,%rax
je     1255 <read_random+0xe5>
jmp    1288 <read_random+0x118>
cmpl   $0x1,-0x34(%rbp)
jge    1264 <read_random+0xf4>
jmp    120b <read_random+0x9b>
mov    -0x20(%rbp),%rdi
mov    -0x34(%rbp),%edx
lea    0xda9(%rip),%rsi
call   1090 <sprintf@plt>
mov    -0x20(%rbp),%rax
add    $0x2,%rax
mov    %rax,-0x20(%rbp)
jmp    120b <read_random+0x9b>
jmp    128d <read_random+0x11d>
mov    -0x30(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_random>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    $0xffffffff,%ebp
test   %rdi,%rdi
je     1242 <read_random+0xd2>
mov    %rsi,%r14
cmp    $0x2,%rsi
jb     1242 <read_random+0xd2>
mov    %rdi,%r15
lea    0xe60(%rip),%rdi
lea    0xe66(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    11cf <read_random+0x5f>
lea    0xe54(%rip),%rdi
lea    0xe4b(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,%rbx
test   %rax,%rax
je     1242 <read_random+0xd2>
mov    %r15,%rdi
call   1050 <strlen@plt>
cmp    %r14,%rax
jae    1238 <read_random+0xc8>
lea    0xe38(%rip),%r12
mov    %r15,%r13
jmp    11fd <read_random+0x8d>
nopl   0x0(%rax,%rax,1)
mov    %r15,%rdi
call   1050 <strlen@plt>
cmp    %r14,%rax
jae    1238 <read_random+0xc8>
mov    %rbx,%rdi
call   1060 <fgetc@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1030 <ferror@plt>
test   %rax,%rax
jne    1238 <read_random+0xc8>
mov    %rbx,%rdi
call   1070 <feof@plt>
test   %rax,%rax
jne    1238 <read_random+0xc8>
test   %ebp,%ebp
jle    11f0 <read_random+0x80>
mov    %r13,%rdi
mov    %r12,%rsi
mov    %ebp,%edx
call   1090 <sprintf@plt>
add    $0x2,%r13
jmp    11f0 <read_random+0x80>
mov    %rbx,%rdi
call   1040 <fclose@plt>
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
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"
@.str.2 = [12 x i8] c"/dev/random\00"
@.str.3 = [5 x i8] c"%02x\00"
define i32 @read_random(i8* %0, i64 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32*
%9 = alloca i32
store i8* %0, i8** %4
store i64 %1, i64* %5
%10 = load i8*, i8** %4
store i8* %10, i8** %6
store i32 0, i32* %7
%11 = load i8*, i8** %4
%12 = icmp ne i8* %11, null
br i1 %12, label %13, label %16
13: ; preds = %2
%14 = load i64, i64* %5
%15 = icmp ult i64 %14, 2
br i1 %15, label %16, label %17
16: ; preds = %13, %2
store i32 -1, i32* %3
br label %59
17: ; preds = %13
%18 = 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* %18, i32** %8
%19 = load i32*, i32** %8
%20 = icmp ne i32* %19, null
br i1 %20, label %27, label %21
21: ; preds = %17
%22 = call i32* @fopen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %22, i32** %8
%23 = load i32*, i32** %8
%24 = icmp ne i32* %23, null
br i1 %24, label %26, label %25
25: ; preds = %21
store i32 -1, i32* %3
br label %59
26: ; preds = %21
br label %27
27: ; preds = %26, %17
br label %28
28: ; preds = %48, %47, %27
%29 = load i8*, i8** %4
%30 = call i64 @strlen(i8* %29)
%31 = load i64, i64* %5
%32 = icmp ult i64 %30, %31
br i1 %32, label %33, label %54
33: ; preds = %28
%34 = load i32*, i32** %8
%35 = call i32 @fgetc(i32* %34)
store i32 %35, i32* %9
%36 = load i32*, i32** %8
%37 = call i64 @ferror(i32* %36)
%38 = icmp ne i64 %37, 0
br i1 %38, label %43, label %39
39: ; preds = %33
%40 = load i32*, i32** %8
%41 = call i64 @feof(i32* %40)
%42 = icmp ne i64 %41, 0
br i1 %42, label %43, label %44
43: ; preds = %39, %33
br label %54
44: ; preds = %39
%45 = load i32, i32* %9
%46 = icmp slt i32 %45, 1
br i1 %46, label %47, label %48
47: ; preds = %44
br label %28
48: ; preds = %44
%49 = load i8*, i8** %6
%50 = load i32, i32* %9
%51 = call i32 @sprintf(i8* %49, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i32 %50)
%52 = load i8*, i8** %6
%53 = getelementptr inbounds i8, i8* %52, i64 2
store i8* %53, i8** %6
br label %28
54: ; preds = %43, %28
br label %55
55: ; preds = %54
%56 = load i32*, i32** %8
%57 = call i32 @fclose(i32* %56)
%58 = load i32, i32* %7
store i32 %58, i32* %3
br label %59
59: ; preds = %55, %25, %16
%60 = load i32, i32* %3
ret i32 %60
}

LLVM IR (-O2):
@.str = [13 x i8] c"/dev/urandom\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [12 x i8] c"/dev/random\00"
@.str.3 = [5 x i8] c"%02x\00"
define i32 @read_random(i8* %0, i64 %1) {
%3 = icmp eq i8* %0, null
%4 = icmp ult i64 %1, 2
%5 = or i1 %3, %4
br i1 %5, label %35, label %6
6: ; preds = %2
%7 = 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))
%8 = icmp eq i32* %7, null
br i1 %8, label %9, label %12
9: ; preds = %6
%10 = tail call i32* @fopen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%11 = icmp eq i32* %10, null
br i1 %11, label %35, label %12
12: ; preds = %9, %6
%13 = phi i32* [ %7, %6 ], [ %10, %9 ]
%14 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%15 = icmp ult i64 %14, %1
br i1 %15, label %16, label %33
16: ; preds = %12, %29
%17 = phi i8* [ %30, %29 ], [ %0, %12 ]
%18 = tail call i32 @fgetc(i32* %13)
%19 = tail call i64 @ferror(i32* %13)
%20 = icmp eq i64 %19, 0
br i1 %20, label %21, label %33
21: ; preds = %16
%22 = tail call i64 @feof(i32* %13)
%23 = icmp eq i64 %22, 0
br i1 %23, label %24, label %33
24: ; preds = %21
%25 = icmp slt i32 %18, 1
br i1 %25, label %29, label %26
26: ; preds = %24
%27 = tail call i32 @sprintf(i8* dereferenceable(1) %17, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i32 %18)
%28 = getelementptr inbounds i8, i8* %17, i64 2
br label %29
29: ; preds = %24, %26
%30 = phi i8* [ %28, %26 ], [ %17, %24 ]
%31 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%32 = icmp ult i64 %31, %1
br i1 %32, label %16, label %33
33: ; preds = %29, %21, %16, %12
%34 = tail call i32 @fclose(i32* %13)
br label %35
35: ; preds = %33, %9, %2
%36 = phi i32 [ -1, %2 ], [ 0, %33 ], [ -1, %9 ]
ret i32 %36
}