#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 */

/* Variables and functions */
int /*<<< orphan*/ SYS_getrandom;
int /*<<< orphan*/ sleep(int);
int syscall(int /*<<< orphan*/, unsigned char *, int, int /*<<< orphan*/);
void pqkernelrandombytes_impl(unsigned char *x, unsigned long long xlen) {
  int i;
  while (xlen > 0) {
    if (xlen < 1048576)
      i = xlen;
    else
      i = 1048576;
    i = syscall(SYS_getrandom, x, i, 0);
    if (i < 1) {
      sleep(1);
      continue;
    }
    x += i;
    xlen -= i;
  }
}


x86 assembly (clang, -O0):
<pqkernelrandombytes_impl>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jbe    11b4 <pqkernelrandombytes_impl+0x94>
cmpq   $0x100000,-0x10(%rbp)
jae    1155 <pqkernelrandombytes_impl+0x35>
mov    -0x10(%rbp),%rax
mov    %eax,-0x14(%rbp)
jmp    115c <pqkernelrandombytes_impl+0x3c>
movl   $0x100000,-0x14(%rbp)
mov    0x2e85(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rsi
mov    -0x14(%rbp),%edx
xor    %ecx,%ecx
call   1030 <syscall@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x1,-0x14(%rbp)
jge    118f <pqkernelrandombytes_impl+0x6f>
mov    $0x1,%edi
call   1040 <sleep@plt>
jmp    1130 <pqkernelrandombytes_impl+0x10>
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    1130 <pqkernelrandombytes_impl+0x10>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pqkernelrandombytes_impl>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
test   %rsi,%rsi
je     117f <pqkernelrandombytes_impl+0x5f>
mov    %rsi,%r14
mov    %rdi,%rbx
mov    0x2eaf(%rip),%r15
jmp    114a <pqkernelrandombytes_impl+0x2a>
nopl   0x0(%rax,%rax,1)
mov    %eax,%eax
add    %rax,%rbx
sub    %rax,%r14
je     117f <pqkernelrandombytes_impl+0x5f>
cmp    $0x100000,%r14
mov    $0x100000,%ebp
cmovb  %r14d,%ebp
nopw   0x0(%rax,%rax,1)
mov    (%r15),%edi
mov    %rbx,%rsi
mov    %ebp,%edx
xor    %ecx,%ecx
call   1030 <syscall@plt>
test   %eax,%eax
jg     1140 <pqkernelrandombytes_impl+0x20>
mov    $0x1,%edi
call   1040 <sleep@plt>
jmp    1160 <pqkernelrandombytes_impl+0x40>
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@SYS_getrandom = global i32 0
define void @pqkernelrandombytes_impl(i8* %0, i64 %1) {
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i32
store i8* %0, i8** %3
store i64 %1, i64* %4
br label %6
6: ; preds = %25, %23, %2
%7 = load i64, i64* %4
%8 = icmp ugt i64 %7, 0
br i1 %8, label %9, label %34
9: ; preds = %6
%10 = load i64, i64* %4
%11 = icmp ult i64 %10, 1048576
br i1 %11, label %12, label %15
12: ; preds = %9
%13 = load i64, i64* %4
%14 = trunc i64 %13 to i32
store i32 %14, i32* %5
br label %16
15: ; preds = %9
store i32 1048576, i32* %5
br label %16
16: ; preds = %15, %12
%17 = load i32, i32* @SYS_getrandom
%18 = load i8*, i8** %3
%19 = load i32, i32* %5
%20 = call i32 @syscall(i32 %17, i8* %18, i32 %19, i32 0)
store i32 %20, i32* %5
%21 = load i32, i32* %5
%22 = icmp slt i32 %21, 1
br i1 %22, label %23, label %25
23: ; preds = %16
%24 = call i32 @sleep(i32 1)
br label %6
25: ; preds = %16
%26 = load i32, i32* %5
%27 = load i8*, i8** %3
%28 = sext i32 %26 to i64
%29 = getelementptr inbounds i8, i8* %27, i64 %28
store i8* %29, i8** %3
%30 = load i32, i32* %5
%31 = sext i32 %30 to i64
%32 = load i64, i64* %4
%33 = sub i64 %32, %31
store i64 %33, i64* %4
br label %6
34: ; preds = %6
ret void
}

LLVM IR (-O2):
@SYS_getrandom = global i32 0
define void @pqkernelrandombytes_impl(i8* %0, i64 %1) {
%3 = icmp eq i64 %1, 0
br i1 %3, label %24, label %4
4: ; preds = %2, %18
%5 = phi i64 [ %22, %18 ], [ %1, %2 ]
%6 = phi i8* [ %21, %18 ], [ %0, %2 ]
%7 = icmp ult i64 %5, 1048576
%8 = select i1 %7, i64 %5, i64 1048576
%9 = trunc i64 %8 to i32
%10 = load i32, i32* @SYS_getrandom
%11 = tail call i32 @syscall(i32 %10, i8* %6, i32 %9, i32 0)
%12 = icmp slt i32 %11, 1
br i1 %12, label %13, label %18
13: ; preds = %4, %13
%14 = tail call i32 @sleep(i32 1)
%15 = load i32, i32* @SYS_getrandom
%16 = tail call i32 @syscall(i32 %15, i8* %6, i32 %9, i32 0)
%17 = icmp slt i32 %16, 1
br i1 %17, label %13, label %18
18: ; preds = %13, %4
%19 = phi i32 [ %11, %4 ], [ %16, %13 ]
%20 = zext i32 %19 to i64
%21 = getelementptr inbounds i8, i8* %6, i64 %20
%22 = sub i64 %5, %20
%23 = icmp eq i64 %22, 0
br i1 %23, label %24, label %4
24: ; preds = %18, %2
ret void
}