#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*/ buf;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int fread(size_t *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
size_t csprng(size_t bits) {
  static FILE *fp;
  static size_t bits_left, buf;
  size_t ret, shift;
  if (!fp && !(fp = fopen("/dev/urandom", "rb"))) {
    fprintf(stderr, "Could not open "
                    "/dev/urandom"
                    "\n");
    exit(5);
  }
  if (bits > 8 * sizeof(buf))
    bits = 8 * sizeof(buf);
  ret = 0;
  if (bits_left < bits) {
    ret = buf;
    bits -= bits_left;
    ret <<= bits;
    if (fread(&buf, sizeof(buf), 1, fp) != 1) {
      fprintf(stderr, "I/O error while reading from "
                      "/dev/urandom"
                      "\n");
      exit(5);
    }
    bits_left = 8 * sizeof(buf);
  }
  shift = 8 * sizeof(buf) - bits;
  ret |= buf << shift >> shift;
  buf >>= bits;
  bits_left -= bits;
  return ret;
}


x86 assembly (clang, -O0):
<csprng>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
cmpq   $0x0,0x2ef4(%rip)
jne    119d <csprng+0x5d>
lea    0xe9f(%rip),%rdi
lea    0xea5(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,0x2ed4(%rip)
cmp    $0x0,%rax
jne    119d <csprng+0x5d>
mov    0x2e5b(%rip),%rax
mov    (%rax),%edi
lea    0xe82(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x5,%edi
call   1060 <exit@plt>
cmpq   $0x40,-0x8(%rbp)
jbe    11b0 <csprng+0x70>
movq   $0x40,-0x8(%rbp)
movq   $0x0,-0x10(%rbp)
mov    0x2e91(%rip),%rax
cmp    -0x8(%rbp),%rax
jae    1245 <csprng+0x105>
mov    0x2e88(%rip),%rax
mov    %rax,-0x10(%rbp)
mov    0x2e75(%rip),%rcx
mov    -0x8(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rcx
mov    -0x10(%rbp),%rax
shl    %cl,%rax
mov    %rax,-0x10(%rbp)
mov    0x2e4c(%rip),%rcx
lea    0x2e55(%rip),%rdi
mov    $0x8,%esi
mov    $0x1,%edx
call   1030 <fread@plt>
cmp    $0x1,%eax
je     123a <csprng+0xfa>
mov    0x2dbe(%rip),%rax
mov    (%rax),%edi
lea    0xe02(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x5,%edi
call   1060 <exit@plt>
movq   $0x40,0x2e0b(%rip)
mov    $0x40,%eax
sub    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    0x2dff(%rip),%rax
mov    -0x18(%rbp),%rcx
shl    %cl,%rax
mov    -0x18(%rbp),%rcx
shr    %cl,%rax
or     -0x10(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    -0x8(%rbp),%rcx
mov    0x2dde(%rip),%rax
shr    %cl,%rax
mov    %rax,0x2dd4(%rip)
mov    -0x8(%rbp),%rcx
mov    0x2dc1(%rip),%rax
sub    %rcx,%rax
mov    %rax,0x2db7(%rip)
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<csprng>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
mov    0x2efa(%rip),%rcx
test   %rcx,%rcx
jne    1179 <csprng+0x39>
lea    0xea6(%rip),%rdi
lea    0xeac(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,%rcx
mov    %rax,0x2ed8(%rip)
test   %rax,%rax
je     1210 <csprng+0xd0>
cmp    $0x40,%rbx
mov    $0x40,%edx
cmovb  %rbx,%rdx
mov    0x2ec3(%rip),%rsi
mov    %rdx,%rbx
sub    %rsi,%rbx
jbe    11c6 <csprng+0x86>
mov    0x2ebc(%rip),%r14
lea    0x2eb5(%rip),%rdi
mov    $0x8,%esi
mov    $0x1,%edx
call   1030 <fread@plt>
cmp    $0x1,%eax
jne    11fe <csprng+0xbe>
mov    %ebx,%ecx
shl    %cl,%r14
mov    $0x40,%esi
mov    %rbx,%rdx
jmp    11c9 <csprng+0x89>
xor    %r14d,%r14d
mov    0x2e88(%rip),%rdi
mov    %edx,%ecx
neg    %cl
mov    %rdi,%rax
shl    %cl,%rax
shr    %cl,%rax
mov    %edx,%ecx
shr    %cl,%rdi
or     %r14,%rax
mov    %rdi,0x2e6c(%rip)
sub    %rdx,%rsi
mov    %rsi,0x2e5a(%rip)
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
mov    0x2ddb(%rip),%rax
mov    (%rax),%edi
lea    0xe1f(%rip),%rsi
jmp    1220 <csprng+0xe0>
mov    0x2dc9(%rip),%rax
mov    (%rax),%edi
lea    0xdf0(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x5,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@csprng.fp = internal global i32* null
@csprng.bits_left = internal global i64 0
@csprng.buf = internal global i64 0
@.str = [13 x i8] c"/dev/urandom\00"
@.str.1 = [3 x i8] c"rb\00"
@stderr = global i32 0
@.str.2 = [29 x i8] c"Could not open /dev/urandom\0A\00"
@.str.3 = [43 x i8] c"I/O error while reading from /dev/urandom\0A\00"
define i64 @csprng(i64 %0) {
%2 = alloca i64
%3 = alloca i64
%4 = alloca i64
store i64 %0, i64* %2
%5 = load i32*, i32** @csprng.fp
%6 = icmp ne i32* %5, null
br i1 %6, label %14, label %7
7: ; preds = %1
%8 = call i32* @fopen(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %8, i32** @csprng.fp
%9 = icmp ne i32* %8, null
br i1 %9, label %14, label %10
10: ; preds = %7
%11 = load i32, i32* @stderr
%12 = call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0))
%13 = call i32 @exit(i32 5)
unreachable
14: ; preds = %7, %1
%15 = load i64, i64* %2
%16 = icmp ugt i64 %15, 64
br i1 %16, label %17, label %18
17: ; preds = %14
store i64 64, i64* %2
br label %18
18: ; preds = %17, %14
store i64 0, i64* %3
%19 = load i64, i64* @csprng.bits_left
%20 = load i64, i64* %2
%21 = icmp ult i64 %19, %20
br i1 %21, label %22, label %38
22: ; preds = %18
%23 = load i64, i64* @csprng.buf
store i64 %23, i64* %3
%24 = load i64, i64* @csprng.bits_left
%25 = load i64, i64* %2
%26 = sub i64 %25, %24
store i64 %26, i64* %2
%27 = load i64, i64* %2
%28 = load i64, i64* %3
%29 = shl i64 %28, %27
store i64 %29, i64* %3
%30 = load i32*, i32** @csprng.fp
%31 = call i32 @fread(i64* @csprng.buf, i32 8, i32 1, i32* %30)
%32 = icmp ne i32 %31, 1
br i1 %32, label %33, label %37
33: ; preds = %22
%34 = load i32, i32* @stderr
%35 = call i32 @fprintf(i32 %34, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.3, i64 0, i64 0))
%36 = call i32 @exit(i32 5)
unreachable
37: ; preds = %22
store i64 64, i64* @csprng.bits_left
br label %38
38: ; preds = %37, %18
%39 = load i64, i64* %2
%40 = sub i64 64, %39
store i64 %40, i64* %4
%41 = load i64, i64* @csprng.buf
%42 = load i64, i64* %4
%43 = shl i64 %41, %42
%44 = load i64, i64* %4
%45 = lshr i64 %43, %44
%46 = load i64, i64* %3
%47 = or i64 %46, %45
store i64 %47, i64* %3
%48 = load i64, i64* %2
%49 = load i64, i64* @csprng.buf
%50 = lshr i64 %49, %48
store i64 %50, i64* @csprng.buf
%51 = load i64, i64* %2
%52 = load i64, i64* @csprng.bits_left
%53 = sub i64 %52, %51
store i64 %53, i64* @csprng.bits_left
%54 = load i64, i64* %3
ret i64 %54
}

LLVM IR (-O2):
@csprng.fp = internal unnamed_addr global i32* null
@csprng.bits_left = internal unnamed_addr global i64 0
@csprng.buf = internal global i64 0
@.str = [13 x i8] c"/dev/urandom\00"
@.str.1 = [3 x i8] c"rb\00"
@stderr = global i32 0
@.str.2 = [29 x i8] c"Could not open /dev/urandom\0A\00"
@.str.3 = [43 x i8] c"I/O error while reading from /dev/urandom\0A\00"
define i64 @csprng(i64 %0) {
%2 = load i32*, i32** @csprng.fp
%3 = icmp eq i32* %2, null
br i1 %3, label %4, label %11
4: ; preds = %1
%5 = tail call i32* @fopen(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %5, i32** @csprng.fp
%6 = icmp eq i32* %5, null
br i1 %6, label %7, label %11
7: ; preds = %4
%8 = load i32, i32* @stderr
%9 = tail call i32 @fprintf(i32 %8, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0))
%10 = tail call i32 @exit(i32 5)
unreachable
11: ; preds = %4, %1
%12 = phi i32* [ %5, %4 ], [ %2, %1 ]
%13 = icmp ult i64 %0, 64
%14 = select i1 %13, i64 %0, i64 64
%15 = load i64, i64* @csprng.bits_left
%16 = icmp ugt i64 %14, %15
br i1 %16, label %17, label %28
17: ; preds = %11
%18 = load i64, i64* @csprng.buf
%19 = sub i64 %14, %15
%20 = tail call i32 @fread(i64* @csprng.buf, i32 8, i32 1, i32* %12)
%21 = icmp eq i32 %20, 1
br i1 %21, label %26, label %22
22: ; preds = %17
%23 = load i32, i32* @stderr
%24 = tail call i32 @fprintf(i32 %23, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.3, i64 0, i64 0))
%25 = tail call i32 @exit(i32 5)
unreachable
26: ; preds = %17
%27 = shl i64 %18, %19
br label %28
28: ; preds = %26, %11
%29 = phi i64 [ 64, %26 ], [ %15, %11 ]
%30 = phi i64 [ %27, %26 ], [ 0, %11 ]
%31 = phi i64 [ %19, %26 ], [ %14, %11 ]
%32 = sub i64 64, %31
%33 = load i64, i64* @csprng.buf
%34 = lshr i64 -1, %32
%35 = and i64 %34, %33
%36 = or i64 %35, %30
%37 = lshr i64 %33, %31
store i64 %37, i64* @csprng.buf
%38 = sub i64 %29, %31
store i64 %38, i64* @csprng.bits_left
ret i64 %36
}