#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 getrandom(unsigned char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ sleep(int);
void pqkernelrandombytes_impl(unsigned char *x, unsigned long long xlen) {
  int i;
  while (xlen > 0) {
    if (xlen < 1048576)
      i = xlen;
    else
      i = 1048576;
    i = 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    11ab <pqkernelrandombytes_impl+0x8b>
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    -0x8(%rbp),%rdi
mov    -0x14(%rbp),%esi
xor    %edx,%edx
call   1030 <getrandom@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x1,-0x14(%rbp)
jge    1186 <pqkernelrandombytes_impl+0x66>
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   %r14
push   %rbx
test   %rsi,%rsi
je     117c <pqkernelrandombytes_impl+0x5c>
mov    %rsi,%r14
mov    %rdi,%rbx
jmp    114a <pqkernelrandombytes_impl+0x2a>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %eax,%eax
add    %rax,%rbx
sub    %rax,%r14
je     117c <pqkernelrandombytes_impl+0x5c>
cmp    $0x100000,%r14
mov    $0x100000,%ebp
cmovb  %r14d,%ebp
nopw   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    %ebp,%esi
xor    %edx,%edx
call   1030 <getrandom@plt>
test   %eax,%eax
jg     1140 <pqkernelrandombytes_impl+0x20>
mov    $0x1,%edi
call   1040 <sleep@plt>
jmp    1160 <pqkernelrandombytes_impl+0x40>
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
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 = %24, %22, %2
%7 = load i64, i64* %4
%8 = icmp ugt i64 %7, 0
br i1 %8, label %9, label %33
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 i8*, i8** %3
%18 = load i32, i32* %5
%19 = call i32 @getrandom(i8* %17, i32 %18, i32 0)
store i32 %19, i32* %5
%20 = load i32, i32* %5
%21 = icmp slt i32 %20, 1
br i1 %21, label %22, label %24
22: ; preds = %16
%23 = call i32 @sleep(i32 1)
br label %6
24: ; preds = %16
%25 = load i32, i32* %5
%26 = load i8*, i8** %3
%27 = sext i32 %25 to i64
%28 = getelementptr inbounds i8, i8* %26, i64 %27
store i8* %28, i8** %3
%29 = load i32, i32* %5
%30 = sext i32 %29 to i64
%31 = load i64, i64* %4
%32 = sub i64 %31, %30
store i64 %32, i64* %4
br label %6
33: ; preds = %6
ret void
}

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