#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*/ O_RDONLY;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int read(int, int *, int);
int /*<<< orphan*/ stderr;
int get_urandom(void) {
  static int fd = -1;
  static int buf[2048];
  static int bufpos = 0;
  static char *dev_name = "/dev/urandom";
  int bytes_wanted;
  int bytes_read;
  int rv;
  if (fd == -1) {
    fd = open(dev_name, O_RDONLY);
    if (fd < 0) {
      perror("open");
      fprintf(stderr, "Couldn't open %s.\n", dev_name);
      exit(1);
    }
  }
  if (bufpos == 0) {
    bytes_wanted = 2048 * sizeof(int);
    bytes_read = read(fd, buf, 2048 * sizeof(int));
    if (bytes_read != bytes_wanted) {
      perror("read");
      fprintf(stderr, "Couldn't read %s.\n", dev_name);
      fprintf(stderr, "Expected %d byte(s); got %d.\n", bytes_wanted,
              bytes_read);
      exit(1);
    }
  }
  rv = buf[bufpos];
  bufpos++;
  if (bufpos >= 2048)
    bufpos = 0;
  return rv;
}


x86 assembly (clang, -O0):
<get_urandom>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
cmpl   $0xffffffff,0x2ee9(%rip)
jne    11c6 <get_urandom+0x76>
mov    0x2ee4(%rip),%rdi
mov    0x2e75(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    %eax,0x2ec8(%rip)
cmpl   $0x0,0x2ec1(%rip)
jge    11c1 <get_urandom+0x71>
lea    0xe79(%rip),%rdi
call   1060 <perror@plt>
mov    0x2e38(%rip),%rax
mov    (%rax),%edi
mov    0x2ea7(%rip),%rdx
lea    0xe62(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
jmp    11c6 <get_urandom+0x76>
cmpl   $0x0,0x4ea3(%rip)
jne    1256 <get_urandom+0x106>
movl   $0x2000,-0x4(%rbp)
mov    0x2e68(%rip),%edi
lea    0x2e89(%rip),%rsi
mov    $0x2000,%edx
call   1030 <read@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%eax
cmp    -0x4(%rbp),%eax
je     1251 <get_urandom+0x101>
lea    0xe1e(%rip),%rdi
call   1060 <perror@plt>
mov    0x2dc5(%rip),%rax
mov    (%rax),%edi
mov    0x2e34(%rip),%rdx
lea    0xe07(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    0x2da7(%rip),%rax
mov    (%rax),%edi
mov    -0x4(%rbp),%edx
mov    -0x8(%rbp),%ecx
lea    0xdfd(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
jmp    1256 <get_urandom+0x106>
movslq 0x4e13(%rip),%rcx
lea    0x2e0c(%rip),%rax
mov    (%rax,%rcx,4),%eax
mov    %eax,-0xc(%rbp)
mov    0x4e00(%rip),%eax
add    $0x1,%eax
mov    %eax,0x4df7(%rip)
cmpl   $0x800,0x4ded(%rip)
jl     1293 <get_urandom+0x143>
movl   $0x0,0x4ddd(%rip)
mov    -0xc(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_urandom>:
push   %r14
push   %rbx
push   %rax
mov    0x2eee(%rip),%eax
cmp    $0xffffffff,%eax
jne    117e <get_urandom+0x2e>
mov    0x2e82(%rip),%rax
mov    (%rax),%esi
lea    0xe91(%rip),%rdi
call   1050 <open@plt>
mov    %eax,0x2ece(%rip)
test   %eax,%eax
js     11d1 <get_urandom+0x81>
mov    0x4edc(%rip),%ecx
test   %ecx,%ecx
jne    11a8 <get_urandom+0x58>
lea    0x2ed1(%rip),%rsi
mov    %eax,%edi
mov    $0x2000,%edx
call   1030 <read@plt>
cmp    $0x2000,%eax
jne    1205 <get_urandom+0xb5>
mov    0x4eb8(%rip),%ecx
movslq %ecx,%rcx
lea    0x2eae(%rip),%rax
mov    (%rax,%rcx,4),%eax
lea    0x1(%rcx),%edx
xor    %esi,%esi
cmp    $0x7ff,%ecx
cmovl  %edx,%esi
mov    %esi,0x4e97(%rip)
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe35(%rip),%rdi
call   1060 <perror@plt>
mov    0x2df4(%rip),%rax
mov    (%rax),%edi
lea    0xe25(%rip),%rsi
lea    0xe0c(%rip),%rdx
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    %eax,%ebx
lea    0xe17(%rip),%rdi
call   1060 <perror@plt>
mov    0x2dbe(%rip),%r14
mov    (%r14),%edi
lea    0xe06(%rip),%rsi
lea    0xdd5(%rip),%rdx
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    (%r14),%edi
lea    0xe01(%rip),%rsi
mov    $0x2000,%edx
mov    %ebx,%ecx
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@get_urandom.fd = internal global i32 -1
@get_urandom.buf = internal global [2048 x i32] zeroinitializer
@get_urandom.bufpos = internal global i32 0
@get_urandom.dev_name = internal global i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i32 0, i32 0)
@.str = [13 x i8] c"/dev/urandom\00"
@O_RDONLY = global i32 0
@.str.1 = [5 x i8] c"open\00"
@stderr = global i32 0
@.str.2 = [19 x i8] c"Couldn't open %s.\0A\00"
@.str.3 = [5 x i8] c"read\00"
@.str.4 = [19 x i8] c"Couldn't read %s.\0A\00"
@.str.5 = [30 x i8] c"Expected %d byte(s); got %d.\0A\00"
define i32 @get_urandom() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = load i32, i32* @get_urandom.fd
%5 = icmp eq i32 %4, -1
br i1 %5, label %6, label %19
6: ; preds = %0
%7 = load i8*, i8** @get_urandom.dev_name
%8 = load i32, i32* @O_RDONLY
%9 = call i32 @open(i8* %7, i32 %8)
store i32 %9, i32* @get_urandom.fd
%10 = load i32, i32* @get_urandom.fd
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %18
12: ; preds = %6
%13 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%14 = load i32, i32* @stderr
%15 = load i8*, i8** @get_urandom.dev_name
%16 = call i32 (i32, i8*, ...) @fprintf(i32 %14, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i8* %15)
%17 = call i32 @exit(i32 1)
unreachable
18: ; preds = %6
br label %19
19: ; preds = %18, %0
%20 = load i32, i32* @get_urandom.bufpos
%21 = icmp eq i32 %20, 0
br i1 %21, label %22, label %39
22: ; preds = %19
store i32 8192, i32* %1
%23 = load i32, i32* @get_urandom.fd
%24 = call i32 @read(i32 %23, i32* getelementptr inbounds ([2048 x i32], [2048 x i32]* @get_urandom.buf, i64 0, i64 0), i32 8192)
store i32 %24, i32* %2
%25 = load i32, i32* %2
%26 = load i32, i32* %1
%27 = icmp ne i32 %25, %26
br i1 %27, label %28, label %38
28: ; preds = %22
%29 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%30 = load i32, i32* @stderr
%31 = load i8*, i8** @get_urandom.dev_name
%32 = call i32 (i32, i8*, ...) @fprintf(i32 %30, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0), i8* %31)
%33 = load i32, i32* @stderr
%34 = load i32, i32* %1
%35 = load i32, i32* %2
%36 = call i32 (i32, i8*, ...) @fprintf(i32 %33, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.5, i64 0, i64 0), i32 %34, i32 %35)
%37 = call i32 @exit(i32 1)
unreachable
38: ; preds = %22
br label %39
39: ; preds = %38, %19
%40 = load i32, i32* @get_urandom.bufpos
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds [2048 x i32], [2048 x i32]* @get_urandom.buf, i64 0, i64 %41
%43 = load i32, i32* %42
store i32 %43, i32* %3
%44 = load i32, i32* @get_urandom.bufpos
%45 = add nsw i32 %44, 1
store i32 %45, i32* @get_urandom.bufpos
%46 = load i32, i32* @get_urandom.bufpos
%47 = icmp sge i32 %46, 2048
br i1 %47, label %48, label %49
48: ; preds = %39
store i32 0, i32* @get_urandom.bufpos
br label %49
49: ; preds = %48, %39
%50 = load i32, i32* %3
ret i32 %50
}

LLVM IR (-O2):
@get_urandom.fd = internal unnamed_addr global i32 -1
@get_urandom.buf = internal global [2048 x i32] zeroinitializer
@get_urandom.bufpos = internal unnamed_addr global i32 0
@.str = [13 x i8] c"/dev/urandom\00"
@O_RDONLY = global i32 0
@.str.1 = [5 x i8] c"open\00"
@stderr = global i32 0
@.str.2 = [19 x i8] c"Couldn't open %s.\0A\00"
@.str.3 = [5 x i8] c"read\00"
@.str.4 = [19 x i8] c"Couldn't read %s.\0A\00"
@.str.5 = [30 x i8] c"Expected %d byte(s); got %d.\0A\00"
define i32 @get_urandom() {
%1 = load i32, i32* @get_urandom.fd
%2 = icmp eq i32 %1, -1
br i1 %2, label %3, label %12
3: ; preds = %0
%4 = load i32, i32* @O_RDONLY
%5 = tail call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %4)
store i32 %5, i32* @get_urandom.fd
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %12
7: ; preds = %3
%8 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%9 = load i32, i32* @stderr
%10 = tail call i32 (i32, i8*, ...) @fprintf(i32 %9, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%11 = tail call i32 @exit(i32 1)
unreachable
12: ; preds = %3, %0
%13 = phi i32 [ %5, %3 ], [ %1, %0 ]
%14 = load i32, i32* @get_urandom.bufpos
%15 = icmp eq i32 %14, 0
br i1 %15, label %16, label %28
16: ; preds = %12
%17 = tail call i32 @read(i32 %13, i32* getelementptr inbounds ([2048 x i32], [2048 x i32]* @get_urandom.buf, i64 0, i64 0), i32 8192)
%18 = icmp eq i32 %17, 8192
br i1 %18, label %19, label %21
19: ; preds = %16
%20 = load i32, i32* @get_urandom.bufpos
br label %28
21: ; preds = %16
%22 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%23 = load i32, i32* @stderr
%24 = tail call i32 (i32, i8*, ...) @fprintf(i32 %23, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%25 = load i32, i32* @stderr
%26 = tail call i32 (i32, i8*, ...) @fprintf(i32 %25, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.5, i64 0, i64 0), i32 8192, i32 %17)
%27 = tail call i32 @exit(i32 1)
unreachable
28: ; preds = %19, %12
%29 = phi i32 [ %20, %19 ], [ %14, %12 ]
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds [2048 x i32], [2048 x i32]* @get_urandom.buf, i64 0, i64 %30
%32 = load i32, i32* %31
%33 = add nsw i32 %29, 1
%34 = icmp sgt i32 %29, 2046
%35 = select i1 %34, i32 0, i32 %33
store i32 %35, i32* @get_urandom.bufpos
ret i32 %32
}