#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, long 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    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0xffffffff,0x2ef1(%rip)
jne    1193 <randombytes+0x63>
jmp    1152 <randombytes+0x22>
mov    0x2e8f(%rip),%rax
mov    (%rax),%esi
lea    0xe9e(%rip),%rdi
call   1040 <open@plt>
mov    %eax,0x2ecb(%rip)
cmpl   $0xffffffff,0x2ec4(%rip)
je     117f <randombytes+0x4f>
jmp    118e <randombytes+0x5e>
mov    $0x1,%edi
call   1050 <sleep@plt>
jmp    1152 <randombytes+0x22>
jmp    1193 <randombytes+0x63>
jmp    1198 <randombytes+0x68>
cmpq   $0x0,-0x10(%rbp)
jle    1217 <randombytes+0xe7>
cmpq   $0x100000,-0x10(%rbp)
jge    11bd <randombytes+0x8d>
mov    -0x10(%rbp),%rax
mov    %eax,-0x14(%rbp)
jmp    11c4 <randombytes+0x94>
movl   $0x100000,-0x14(%rbp)
mov    0x2e6e(%rip),%edi
mov    -0x8(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <read@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x1,-0x14(%rbp)
jge    11f2 <randombytes+0xc2>
mov    $0x1,%edi
call   1050 <sleep@plt>
jmp    1198 <randombytes+0x68>
mov    -0x14(%rbp),%ecx
mov    -0x8(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x8(%rbp)
movslq -0x14(%rbp),%rcx
mov    -0x10(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x10(%rbp)
jmp    1198 <randombytes+0x68>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<randombytes>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
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   %r14,%r14
jg     11aa <randombytes+0x7a>
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
cltq
add    %rax,%rbx
sub    %rax,%r14
jle    1195 <randombytes+0x65>
cmp    $0x100000,%r14
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, i64 %1) {
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i32
store i8* %0, i8** %3
store i64 %1, i64* %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 = %39, %37, %18
%20 = load i64, i64* %4
%21 = icmp sgt i64 %20, 0
br i1 %21, label %22, label %48
22: ; preds = %19
%23 = load i64, i64* %4
%24 = icmp slt i64 %23, 1048576
br i1 %24, label %25, label %28
25: ; preds = %22
%26 = load i64, i64* %4
%27 = trunc i64 %26 to i32
store i32 %27, i32* %5
br label %29
28: ; preds = %22
store i32 1048576, i32* %5
br label %29
29: ; preds = %28, %25
%30 = load i32, i32* @randombytes.fd
%31 = load i8*, i8** %3
%32 = load i32, i32* %5
%33 = call i64 @read(i32 %30, i8* %31, i32 %32)
%34 = trunc i64 %33 to i32
store i32 %34, i32* %5
%35 = load i32, i32* %5
%36 = icmp slt i32 %35, 1
br i1 %36, label %37, label %39
37: ; preds = %29
%38 = call i32 @sleep(i32 1)
br label %19
39: ; preds = %29
%40 = load i32, i32* %5
%41 = load i8*, i8** %3
%42 = sext i32 %40 to i64
%43 = getelementptr inbounds i8, i8* %41, i64 %42
store i8* %43, i8** %3
%44 = load i32, i32* %5
%45 = sext i32 %44 to i64
%46 = load i64, i64* %4
%47 = sub nsw i64 %46, %45
store i64 %47, i64* %4
br label %19
48: ; 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, i64 %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 i64 %1, 0
br i1 %15, label %16, label %39
16: ; preds = %14, %32
%17 = phi i64 [ %37, %32 ], [ %1, %14 ]
%18 = phi i8* [ %36, %32 ], [ %0, %14 ]
%19 = icmp ult i64 %17, 1048576
%20 = select i1 %19, i64 %17, i64 1048576
%21 = trunc i64 %20 to i32
%22 = load i32, i32* @randombytes.fd
%23 = tail call i64 @read(i32 %22, i8* %18, i32 %21)
%24 = trunc i64 %23 to i32
%25 = icmp slt i32 %24, 1
br i1 %25, label %26, label %32
26: ; preds = %16, %26
%27 = tail call i32 @sleep(i32 1)
%28 = load i32, i32* @randombytes.fd
%29 = tail call i64 @read(i32 %28, i8* %18, i32 %21)
%30 = trunc i64 %29 to i32
%31 = icmp slt i32 %30, 1
br i1 %31, label %26, label %32
32: ; preds = %26, %16
%33 = phi i64 [ %23, %16 ], [ %29, %26 ]
%34 = shl i64 %33, 32
%35 = ashr exact i64 %34, 32
%36 = getelementptr inbounds i8, i8* %18, i64 %35
%37 = sub nsw i64 %17, %35
%38 = icmp sgt i64 %37, 0
br i1 %38, label %16, label %39
39: ; preds = %32, %14
ret void
}