#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 __u64;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ read(int, char *, int);
int time_get_ns();
void test_urandom_read(int cpu) {
  __u64 start_time;
  char buf[4];
  int i, fd;
  fd = open("/dev/urandom", O_RDONLY);
  if (fd < 0) {
    printf("couldn't open /dev/urandom\n");
    exit(1);
  }
  start_time = time_get_ns();
  for (i = 0; i < 1000000; i++) {
    read(fd, buf, sizeof(buf));
  }
  printf("urandom_read:%d: %lld events per sec\n", cpu,
         1000000 * 1000000000ll / (time_get_ns() - start_time));
  close(fd);
}


x86 assembly (clang, -O0):
<test_urandom_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    0x2e76(%rip),%rax
mov    (%rax),%esi
lea    0xe85(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11a5 <test_urandom_read+0x45>
lea    0xe79(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    $0x0,%al
call   1060 <time_get_ns@plt>
mov    %eax,-0x8(%rbp)
movl   $0x0,-0x10(%rbp)
cmpl   $0xf4240,-0x10(%rbp)
jge    11e2 <test_urandom_read+0x82>
mov    -0x14(%rbp),%edi
lea    -0xc(%rbp),%rsi
mov    $0x4,%edx
call   1050 <read@plt>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    11b6 <test_urandom_read+0x56>
mov    -0x4(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    $0x0,%al
call   1060 <time_get_ns@plt>
mov    -0x18(%rbp),%esi
sub    -0x8(%rbp),%eax
movslq %eax,%rcx
movabs $0x38d7ea4c68000,%rax
cqto
idiv   %rcx
mov    %rax,%rdx
lea    0xe18(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_urandom_read>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %edi,%r14d
mov    0x2e62(%rip),%rax
mov    (%rax),%esi
lea    0xea5(%rip),%rdi
call   1080 <open@plt>
test   %eax,%eax
js     1207 <test_urandom_read+0x97>
mov    %eax,%ebp
xor    %eax,%eax
call   1070 <time_get_ns@plt>
mov    %eax,%r15d
mov    $0xf4240,%ebx
lea    0xc(%rsp),%r12
xchg   %ax,%ax
mov    %ebp,%edi
mov    %r12,%rsi
mov    $0x4,%edx
call   1060 <read@plt>
add    $0xffffffff,%ebx
jne    11b0 <test_urandom_read+0x40>
xor    %eax,%eax
call   1070 <time_get_ns@plt>
sub    %r15d,%eax
movslq %eax,%rcx
movabs $0x38d7ea4c68000,%rax
xor    %edx,%edx
idiv   %rcx
lea    0xe19(%rip),%rdi
mov    %r14d,%esi
mov    %rax,%rdx
xor    %eax,%eax
call   1040 <printf@plt>
mov    %ebp,%edi
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <close@plt>
lea    0xe18(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [13 x i8] c"/dev/urandom\00"
@O_RDONLY = global i32 0
@.str.1 = [28 x i8] c"couldn't open /dev/urandom\0A\00"
@.str.2 = [38 x i8] c"urandom_read:%d: %lld events per sec\0A\00"
define void @test_urandom_read(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca [4 x i8]
%5 = alloca i32
%6 = alloca i32
store i32 %0, i32* %2
%7 = load i32, i32* @O_RDONLY
%8 = call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %7)
store i32 %8, i32* %6
%9 = load i32, i32* %6
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %1
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
%13 = call i32 @exit(i32 1)
unreachable
14: ; preds = %1
%15 = call i32 (...) @time_get_ns()
store i32 %15, i32* %3
store i32 0, i32* %5
br label %16
16: ; preds = %23, %14
%17 = load i32, i32* %5
%18 = icmp slt i32 %17, 1000000
br i1 %18, label %19, label %26
19: ; preds = %16
%20 = load i32, i32* %6
%21 = getelementptr inbounds [4 x i8], [4 x i8]* %4, i64 0, i64 0
%22 = call i32 @read(i32 %20, i8* %21, i32 4)
br label %23
23: ; preds = %19
%24 = load i32, i32* %5
%25 = add nsw i32 %24, 1
store i32 %25, i32* %5
br label %16
26: ; preds = %16
%27 = load i32, i32* %2
%28 = call i32 (...) @time_get_ns()
%29 = load i32, i32* %3
%30 = sub nsw i32 %28, %29
%31 = sext i32 %30 to i64
%32 = sdiv i64 1000000000000000, %31
%33 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.2, i64 0, i64 0), i32 %27, i64 %32)
%34 = load i32, i32* %6
%35 = call i32 @close(i32 %34)
ret void
}

LLVM IR (-O2):
@.str = [13 x i8] c"/dev/urandom\00"
@O_RDONLY = global i32 0
@.str.2 = [38 x i8] c"urandom_read:%d: %lld events per sec\0A\00"
@str = [27 x i8] c"couldn't open /dev/urandom\00"
define void @test_urandom_read(i32 %0) {
%2 = alloca [4 x i8]
%3 = getelementptr inbounds [4 x i8], [4 x i8]* %2, i64 0, i64 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)
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %10
7: ; preds = %1
%8 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str, i64 0, i64 0))
%9 = tail call i32 @exit(i32 1)
unreachable
10: ; preds = %1
%11 = tail call i32 (...) @time_get_ns()
br label %12
12: ; preds = %10, %12
%13 = phi i32 [ 0, %10 ], [ %15, %12 ]
%14 = call i32 @read(i32 %5, i8* %3, i32 4)
%15 = add nuw nsw i32 %13, 1
%16 = icmp eq i32 %15, 1000000
br i1 %16, label %17, label %12
17: ; preds = %12
%18 = tail call i32 (...) @time_get_ns()
%19 = sub nsw i32 %18, %11
%20 = sext i32 %19 to i64
%21 = sdiv i64 1000000000000000, %20
%22 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @.str.2, i64 0, i64 0), i32 %0, i64 %21)
%23 = tail call i32 @close(i32 %5)
ret void
}