#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 int32_t;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int open(char *, int /*<<< orphan*/);
scalar_t__ read(int, unsigned char *, int);
int /*<<< orphan*/ sleep(int);
void randombytes(unsigned char *x, int xlen) {
  static int fd = -1;
  int32_t i;
  if (fd == -1) {
    for (;;) {
      fd = open("/dev/urandom", O_RDONLY);
      if (fd != -1)
        break;
      sleep(1);
    }
  }
  while (xlen > 0) {
    if (xlen < 1048576)
      i = (int32_t)xlen;
    else
      i = 1048576;
    i = (int32_t)read(fd, x, i);
    if (i < 1) {
      sleep(1);
      continue;
    }
    x += i;
    xlen -= i;
  }
}


x86 assembly (clang, -O0):
<randombytes>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
cmpl   $0xffffffff,0x2ef2(%rip)
jne    1192 <randombytes+0x62>
jmp    1151 <randombytes+0x21>
mov    0x2e90(%rip),%rax
mov    (%rax),%esi
lea    0xe9f(%rip),%rdi
call   1040 <open@plt>
mov    %eax,0x2ecc(%rip)
cmpl   $0xffffffff,0x2ec5(%rip)
je     117e <randombytes+0x4e>
jmp    118d <randombytes+0x5d>
mov    $0x1,%edi
call   1050 <sleep@plt>
jmp    1151 <randombytes+0x21>
jmp    1192 <randombytes+0x62>
jmp    1197 <randombytes+0x67>
cmpl   $0x0,-0xc(%rbp)
jle    120f <randombytes+0xdf>
cmpl   $0x100000,-0xc(%rbp)
jge    11b9 <randombytes+0x89>
mov    -0xc(%rbp),%eax
mov    %eax,-0x10(%rbp)
jmp    11c0 <randombytes+0x90>
movl   $0x100000,-0x10(%rbp)
mov    0x2e72(%rip),%edi
mov    -0x8(%rbp),%rsi
mov    -0x10(%rbp),%edx
call   1030 <read@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x1,-0x10(%rbp)
jge    11ee <randombytes+0xbe>
mov    $0x1,%edi
call   1050 <sleep@plt>
jmp    1197 <randombytes+0x67>
mov    -0x10(%rbp),%ecx
mov    -0x8(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x8(%rbp)
mov    -0x10(%rbp),%ecx
mov    -0xc(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0xc(%rbp)
jmp    1197 <randombytes+0x67>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<randombytes>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%r14d
mov    %rdi,%rbx
cmpl   $0xffffffff,0x2ef4(%rip)
jne    1190 <randombytes+0x60>
mov    0x2e9b(%rip),%r15
mov    (%r15),%esi
lea    0xea9(%rip),%rdi
call   1040 <open@plt>
mov    %eax,0x2ed6(%rip)
cmp    $0xffffffff,%eax
jne    1190 <randombytes+0x60>
lea    0xe92(%rip),%rbp
xchg   %ax,%ax
mov    $0x1,%edi
call   1050 <sleep@plt>
mov    (%r15),%esi
mov    %rbp,%rdi
call   1040 <open@plt>
mov    %eax,0x2ead(%rip)
cmp    $0xffffffff,%eax
je     1170 <randombytes+0x40>
test   %r14d,%r14d
jg     11aa <randombytes+0x7a>
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
cltq
add    %rax,%rbx
sub    %eax,%r14d
jle    1195 <randombytes+0x65>
cmp    $0x100000,%r14d
mov    $0x100000,%ebp
cmovb  %r14d,%ebp
nopw   0x0(%rax,%rax,1)
mov    0x2e72(%rip),%edi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1030 <read@plt>
test   %eax,%eax
jg     11a0 <randombytes+0x70>
mov    $0x1,%edi
call   1050 <sleep@plt>
jmp    11c0 <randombytes+0x90>

LLVM IR (-O0):
@randombytes.fd = internal global i32 -1
@.str = [13 x i8] c"/dev/urandom\00"
@O_RDONLY = global i32 0
define void @randombytes(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
store i8* %0, i8** %3
store i32 %1, i32* %4
%6 = load i32, i32* @randombytes.fd
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %18
8: ; preds = %2
br label %9
9: ; preds = %15, %8
%10 = load i32, i32* @O_RDONLY
%11 = call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %10)
store i32 %11, i32* @randombytes.fd
%12 = load i32, i32* @randombytes.fd
%13 = icmp ne i32 %12, -1
br i1 %13, label %14, label %15
14: ; preds = %9
br label %17
15: ; preds = %9
%16 = call i32 @sleep(i32 1)
br label %9
17: ; preds = %14
br label %18
18: ; preds = %17, %2
br label %19
19: ; preds = %38, %36, %18
%20 = load i32, i32* %4
%21 = icmp sgt i32 %20, 0
br i1 %21, label %22, label %46
22: ; preds = %19
%23 = load i32, i32* %4
%24 = icmp slt i32 %23, 1048576
br i1 %24, label %25, label %27
25: ; preds = %22
%26 = load i32, i32* %4
store i32 %26, i32* %5
br label %28
27: ; preds = %22
store i32 1048576, i32* %5
br label %28
28: ; preds = %27, %25
%29 = load i32, i32* @randombytes.fd
%30 = load i8*, i8** %3
%31 = load i32, i32* %5
%32 = call i64 @read(i32 %29, i8* %30, i32 %31)
%33 = trunc i64 %32 to i32
store i32 %33, i32* %5
%34 = load i32, i32* %5
%35 = icmp slt i32 %34, 1
br i1 %35, label %36, label %38
36: ; preds = %28
%37 = call i32 @sleep(i32 1)
br label %19
38: ; preds = %28
%39 = load i32, i32* %5
%40 = load i8*, i8** %3
%41 = sext i32 %39 to i64
%42 = getelementptr inbounds i8, i8* %40, i64 %41
store i8* %42, i8** %3
%43 = load i32, i32* %5
%44 = load i32, i32* %4
%45 = sub nsw i32 %44, %43
store i32 %45, i32* %4
br label %19
46: ; preds = %19
ret void
}

LLVM IR (-O2):
@randombytes.fd = internal unnamed_addr global i32 -1
@.str = [13 x i8] c"/dev/urandom\00"
@O_RDONLY = global i32 0
define void @randombytes(i8* nocapture %0, i32 %1) {
%3 = load i32, i32* @randombytes.fd
%4 = icmp eq i32 %3, -1
br i1 %4, label %5, label %14
5: ; preds = %2
%6 = load i32, i32* @O_RDONLY
%7 = tail call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %6)
store i32 %7, i32* @randombytes.fd
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %14
9: ; preds = %5, %9
%10 = tail call i32 @sleep(i32 1)
%11 = load i32, i32* @O_RDONLY
%12 = tail call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %11)
store i32 %12, i32* @randombytes.fd
%13 = icmp eq i32 %12, -1
br i1 %13, label %9, label %14
14: ; preds = %9, %5, %2
%15 = icmp sgt i32 %1, 0
br i1 %15, label %16, label %39
16: ; preds = %14, %31
%17 = phi i32 [ %37, %31 ], [ %1, %14 ]
%18 = phi i8* [ %36, %31 ], [ %0, %14 ]
%19 = icmp ult i32 %17, 1048576
%20 = select i1 %19, i32 %17, i32 1048576
%21 = load i32, i32* @randombytes.fd
%22 = tail call i64 @read(i32 %21, i8* %18, i32 %20)
%23 = trunc i64 %22 to i32
%24 = icmp slt i32 %23, 1
br i1 %24, label %25, label %31
25: ; preds = %16, %25
%26 = tail call i32 @sleep(i32 1)
%27 = load i32, i32* @randombytes.fd
%28 = tail call i64 @read(i32 %27, i8* %18, i32 %20)
%29 = trunc i64 %28 to i32
%30 = icmp slt i32 %29, 1
br i1 %30, label %25, label %31
31: ; preds = %25, %16
%32 = phi i64 [ %22, %16 ], [ %28, %25 ]
%33 = trunc i64 %32 to i32
%34 = shl i64 %32, 32
%35 = ashr exact i64 %34, 32
%36 = getelementptr inbounds i8, i8* %18, i64 %35
%37 = sub nsw i32 %17, %33
%38 = icmp sgt i32 %37, 0
br i1 %38, label %16, label %39
39: ; preds = %31, %14
ret void
}