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

/* Variables and functions */
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int getrandom(int /*<<< orphan*/ *, size_t, int);
int /*<<< orphan*/ stderr;
int secrand(void *buf, size_t len) {
  uint8_t *ptr = (uint8_t *)buf;
  int n;
  int rc;
  if (len > 256)
    return -1;
  n = getrandom(ptr, len, 1);
  if (n == -1)
    return -1;
  if (n == len)
    return 0;
  fprintf(stderr, "waiting for random... ");
  while (n < len) {
    rc = getrandom(ptr + n, len - n, 2);
    if (rc == -1) {
      fprintf(stderr, "error\n");
      return -1;
    }
    n += rc;
  }
  fprintf(stderr, "done\n");
  return 0;
}


x86 assembly (clang, -O0):
<secrand>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
cmpq   $0x100,-0x18(%rbp)
jbe    1152 <secrand+0x32>
movl   $0xffffffff,-0x4(%rbp)
jmp    1236 <secrand+0x116>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    $0x1,%edx
call   1040 <getrandom@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    117d <secrand+0x5d>
movl   $0xffffffff,-0x4(%rbp)
jmp    1236 <secrand+0x116>
movslq -0x24(%rbp),%rax
cmp    -0x18(%rbp),%rax
jne    1197 <secrand+0x77>
movl   $0x0,-0x4(%rbp)
jmp    1236 <secrand+0x116>
mov    0x2e42(%rip),%rax
mov    (%rax),%edi
lea    0xe59(%rip),%rsi
call   1030 <fprintf@plt>
movslq -0x24(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    121a <secrand+0xfa>
mov    -0x20(%rbp),%rdi
movslq -0x24(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rdi
mov    -0x18(%rbp),%rsi
movslq -0x24(%rbp),%rax
sub    %rax,%rsi
mov    $0x2,%edx
call   1040 <getrandom@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0xffffffff,-0x28(%rbp)
jne    120c <secrand+0xec>
mov    0x2dee(%rip),%rax
mov    (%rax),%edi
lea    0xe1c(%rip),%rsi
call   1030 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1236 <secrand+0x116>
mov    -0x28(%rbp),%eax
add    -0x24(%rbp),%eax
mov    %eax,-0x24(%rbp)
jmp    11ac <secrand+0x8c>
mov    0x2dbf(%rip),%rax
mov    (%rax),%edi
lea    0xdf4(%rip),%rsi
call   1030 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<secrand>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    $0xffffffff,%r14d
cmp    $0x100,%rsi
ja     11d3 <secrand+0xb3>
mov    %rsi,%rbx
mov    %rdi,%r15
mov    $0x1,%edx
call   1040 <getrandom@plt>
cmp    $0xffffffff,%eax
je     11d3 <secrand+0xb3>
mov    %eax,%ebp
movslq %eax,%r13
xor    %r14d,%r14d
cmp    %rbx,%r13
je     11d3 <secrand+0xb3>
mov    0x2e75(%rip),%rax
mov    (%rax),%edi
lea    0xe8c(%rip),%rsi
call   1030 <fprintf@plt>
xor    %r14d,%r14d
lea    0xe9b(%rip),%r12
cmp    %rbx,%r13
jae    11c2 <secrand+0xa2>
lea    0xe88(%rip),%r12
nop
lea    (%r15,%r13,4),%rdi
mov    %rbx,%rsi
sub    %r13,%rsi
mov    $0x2,%edx
call   1040 <getrandom@plt>
cmp    $0xffffffff,%eax
je     11bc <secrand+0x9c>
add    %eax,%ebp
movslq %ebp,%r13
cmp    %rbx,%r13
jb     1190 <secrand+0x70>
lea    0xe64(%rip),%r12
jmp    11c2 <secrand+0xa2>
mov    $0xffffffff,%r14d
mov    0x2e17(%rip),%rax
mov    (%rax),%edi
mov    %r12,%rsi
call   1030 <fprintf@plt>
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [23 x i8] c"waiting for random... \00"
@.str.1 = [7 x i8] c"error\0A\00"
@.str.2 = [6 x i8] c"done\0A\00"
define i32 @secrand(i8* %0, i64 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i32
store i8* %0, i8** %4
store i64 %1, i64* %5
%9 = load i8*, i8** %4
%10 = bitcast i8* %9 to i32*
store i32* %10, i32** %6
%11 = load i64, i64* %5
%12 = icmp ugt i64 %11, 256
br i1 %12, label %13, label %14
13: ; preds = %2
store i32 -1, i32* %3
br label %57
14: ; preds = %2
%15 = load i32*, i32** %6
%16 = load i64, i64* %5
%17 = call i32 @getrandom(i32* %15, i64 %16, i32 1)
store i32 %17, i32* %7
%18 = load i32, i32* %7
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %21
20: ; preds = %14
store i32 -1, i32* %3
br label %57
21: ; preds = %14
%22 = load i32, i32* %7
%23 = sext i32 %22 to i64
%24 = load i64, i64* %5
%25 = icmp eq i64 %23, %24
br i1 %25, label %26, label %27
26: ; preds = %21
store i32 0, i32* %3
br label %57
27: ; preds = %21
%28 = load i32, i32* @stderr
%29 = call i32 @fprintf(i32 %28, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
br label %30
30: ; preds = %50, %27
%31 = load i32, i32* %7
%32 = sext i32 %31 to i64
%33 = load i64, i64* %5
%34 = icmp ult i64 %32, %33
br i1 %34, label %35, label %54
35: ; preds = %30
%36 = load i32*, i32** %6
%37 = load i32, i32* %7
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds i32, i32* %36, i64 %38
%40 = load i64, i64* %5
%41 = load i32, i32* %7
%42 = sext i32 %41 to i64
%43 = sub i64 %40, %42
%44 = call i32 @getrandom(i32* %39, i64 %43, i32 2)
store i32 %44, i32* %8
%45 = load i32, i32* %8
%46 = icmp eq i32 %45, -1
br i1 %46, label %47, label %50
47: ; preds = %35
%48 = load i32, i32* @stderr
%49 = call i32 @fprintf(i32 %48, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %57
50: ; preds = %35
%51 = load i32, i32* %8
%52 = load i32, i32* %7
%53 = add nsw i32 %52, %51
store i32 %53, i32* %7
br label %30
54: ; preds = %30
%55 = load i32, i32* @stderr
%56 = call i32 @fprintf(i32 %55, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
store i32 0, i32* %3
br label %57
57: ; preds = %54, %47, %26, %20, %13
%58 = load i32, i32* %3
ret i32 %58
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [23 x i8] c"waiting for random... \00"
@.str.1 = [7 x i8] c"error\0A\00"
@.str.2 = [6 x i8] c"done\0A\00"
define i32 @secrand(i8* %0, i64 %1) {
%3 = bitcast i8* %0 to i32*
%4 = icmp ugt i64 %1, 256
br i1 %4, label %31, label %5
5: ; preds = %2
%6 = tail call i32 @getrandom(i32* %3, i64 %1, i32 1)
%7 = icmp eq i32 %6, -1
br i1 %7, label %31, label %8
8: ; preds = %5
%9 = sext i32 %6 to i64
%10 = icmp eq i64 %9, %1
br i1 %10, label %31, label %11
11: ; preds = %8
%12 = load i32, i32* @stderr
%13 = tail call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
%14 = icmp ult i64 %9, %1
br i1 %14, label %18, label %26
15: ; preds = %18
%16 = sext i32 %25 to i64
%17 = icmp ult i64 %16, %1
br i1 %17, label %18, label %26
18: ; preds = %11, %15
%19 = phi i64 [ %16, %15 ], [ %9, %11 ]
%20 = phi i32 [ %25, %15 ], [ %6, %11 ]
%21 = getelementptr inbounds i32, i32* %3, i64 %19
%22 = sub i64 %1, %19
%23 = tail call i32 @getrandom(i32* %21, i64 %22, i32 2)
%24 = icmp eq i32 %23, -1
%25 = add nsw i32 %23, %20
br i1 %24, label %26, label %15
26: ; preds = %15, %18, %11
%27 = phi i8* [ getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), %11 ], [ getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), %18 ], [ getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), %15 ]
%28 = phi i32 [ 0, %11 ], [ -1, %18 ], [ 0, %15 ]
%29 = load i32, i32* @stderr
%30 = tail call i32 @fprintf(i32 %29, i8* %27)
br label %31
31: ; preds = %26, %8, %5, %2
%32 = phi i32 [ -1, %2 ], [ -1, %5 ], [ 0, %8 ], [ %28, %26 ]
ret i32 %32
}