#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*/ close(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
long long get_ustime_sec();
int open(char *, int /*<<< orphan*/);
int read(int, unsigned char *, int);
int /*<<< orphan*/ stderr;
long long bench_urandom2(int times) {
  int fd, i, rc, len;
  unsigned char key[1024];
  len = 0;
  long long start = get_ustime_sec();
  for (i = 0; i < times; i++) {
    fd = open("/dev/urandom", O_RDONLY);
    if (fd < 0) {
      fprintf(stderr, "Cannot open /dev/urandom!\n");
      return 0;
    }
    while (len < 1024) {
      rc = read(fd, key + len, 1024 - len);
      if (rc < 0) {
        fprintf(stderr, "Cannot open /dev/urandom!\n");
        break;
      }
      len += rc;
    }
    close(fd);
    len = 0;
  }
  return (get_ustime_sec() - start);
}


x86 assembly (clang, -O0):
<bench_urandom2>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
mov    %edi,-0xc(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    $0x0,%al
call   1070 <get_ustime_sec@plt>
mov    %rax,-0x428(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    124c <bench_urandom2+0xfc>
mov    0x2e5b(%rip),%rax
mov    (%rax),%esi
lea    0xe6a(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    11ca <bench_urandom2+0x7a>
mov    0x2e29(%rip),%rax
mov    (%rax),%edi
lea    0xe55(%rip),%rsi
call   1050 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    125e <bench_urandom2+0x10e>
jmp    11cf <bench_urandom2+0x7f>
cmpl   $0x400,-0x1c(%rbp)
jge    122f <bench_urandom2+0xdf>
mov    -0x10(%rbp),%edi
lea    -0x420(%rbp),%rsi
movslq -0x1c(%rbp),%rax
add    %rax,%rsi
mov    $0x400,%edx
sub    -0x1c(%rbp),%edx
call   1040 <read@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    1221 <bench_urandom2+0xd1>
mov    0x2dca(%rip),%rax
mov    (%rax),%edi
lea    0xdf6(%rip),%rsi
call   1050 <fprintf@plt>
jmp    122f <bench_urandom2+0xdf>
mov    -0x18(%rbp),%eax
add    -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
jmp    11cf <bench_urandom2+0x7f>
mov    -0x10(%rbp),%edi
call   1030 <close@plt>
movl   $0x0,-0x1c(%rbp)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    117a <bench_urandom2+0x2a>
mov    $0x0,%al
call   1070 <get_ustime_sec@plt>
sub    -0x428(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bench_urandom2>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x418,%rsp
mov    %edi,%r13d
xor    %ebp,%ebp
xor    %eax,%eax
call   1070 <get_ustime_sec@plt>
mov    %rax,0x8(%rsp)
test   %r13d,%r13d
jle    11fb <bench_urandom2+0xab>
mov    0x2e66(%rip),%r14
lea    0xe77(%rip),%r12
jmp    11b4 <bench_urandom2+0x64>
nopl   0x0(%rax,%rax,1)
mov    0x2e41(%rip),%rax
mov    (%rax),%edi
lea    0xe6d(%rip),%rsi
call   1050 <fprintf@plt>
mov    %ebx,%edi
call   1030 <close@plt>
add    $0x1,%ebp
cmp    %r13d,%ebp
je     11fb <bench_urandom2+0xab>
mov    (%r14),%esi
mov    %r12,%rdi
call   1060 <open@plt>
test   %eax,%eax
js     1209 <bench_urandom2+0xb9>
mov    %eax,%ebx
xor    %r15d,%r15d
nopl   0x0(%rax,%rax,1)
movslq %r15d,%rax
lea    (%rsp,%rax,1),%rsi
add    $0x10,%rsi
mov    $0x400,%edx
sub    %eax,%edx
mov    %ebx,%edi
call   1040 <read@plt>
test   %eax,%eax
js     1190 <bench_urandom2+0x40>
add    %eax,%r15d
cmp    $0x3ff,%r15d
jle    11d0 <bench_urandom2+0x80>
jmp    11a5 <bench_urandom2+0x55>
xor    %eax,%eax
call   1070 <get_ustime_sec@plt>
sub    0x8(%rsp),%rax
jmp    1220 <bench_urandom2+0xd0>
mov    0x2dc8(%rip),%rax
mov    (%rax),%edi
lea    0xdf4(%rip),%rsi
call   1050 <fprintf@plt>
xor    %eax,%eax
add    $0x418,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [13 x i8] c"/dev/urandom\00"
@O_RDONLY = global i32 0
@stderr = global i32 0
@.str.1 = [27 x i8] c"Cannot open /dev/urandom!\0A\00"
define i64 @bench_urandom2(i32 %0) {
%2 = alloca i64
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca [1024 x i8]
%9 = alloca i64
store i32 %0, i32* %3
store i32 0, i32* %7
%10 = call i64 (...) @get_ustime_sec()
store i64 %10, i64* %9
store i32 0, i32* %5
br label %11
11: ; preds = %48, %1
%12 = load i32, i32* %5
%13 = load i32, i32* %3
%14 = icmp slt i32 %12, %13
br i1 %14, label %15, label %51
15: ; preds = %11
%16 = load i32, i32* @O_RDONLY
%17 = call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %16)
store i32 %17, i32* %4
%18 = load i32, i32* %4
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %23
20: ; preds = %15
%21 = load i32, i32* @stderr
%22 = call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0))
store i64 0, i64* %2
br label %55
23: ; preds = %15
br label %24
24: ; preds = %41, %23
%25 = load i32, i32* %7
%26 = icmp slt i32 %25, 1024
br i1 %26, label %27, label %45
27: ; preds = %24
%28 = load i32, i32* %4
%29 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%30 = load i32, i32* %7
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds i8, i8* %29, i64 %31
%33 = load i32, i32* %7
%34 = sub nsw i32 1024, %33
%35 = call i32 @read(i32 %28, i8* %32, i32 %34)
store i32 %35, i32* %6
%36 = load i32, i32* %6
%37 = icmp slt i32 %36, 0
br i1 %37, label %38, label %41
38: ; preds = %27
%39 = load i32, i32* @stderr
%40 = call i32 @fprintf(i32 %39, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0))
br label %45
41: ; preds = %27
%42 = load i32, i32* %6
%43 = load i32, i32* %7
%44 = add nsw i32 %43, %42
store i32 %44, i32* %7
br label %24
45: ; preds = %38, %24
%46 = load i32, i32* %4
%47 = call i32 @close(i32 %46)
store i32 0, i32* %7
br label %48
48: ; preds = %45
%49 = load i32, i32* %5
%50 = add nsw i32 %49, 1
store i32 %50, i32* %5
br label %11
51: ; preds = %11
%52 = call i64 (...) @get_ustime_sec()
%53 = load i64, i64* %9
%54 = sub nsw i64 %52, %53
store i64 %54, i64* %2
br label %55
55: ; preds = %51, %20
%56 = load i64, i64* %2
ret i64 %56
}

LLVM IR (-O2):
@.str = [13 x i8] c"/dev/urandom\00"
@O_RDONLY = global i32 0
@stderr = global i32 0
@.str.1 = [27 x i8] c"Cannot open /dev/urandom!\0A\00"
define i64 @bench_urandom2(i32 %0) {
%2 = alloca [1024 x i8]
%3 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%4 = tail call i64 (...) @get_ustime_sec()
%5 = icmp sgt i32 %0, 0
br i1 %5, label %6, label %31
6: ; preds = %1, %27
%7 = phi i32 [ %29, %27 ], [ 0, %1 ]
%8 = load i32, i32* @O_RDONLY
%9 = tail call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %8)
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %16
11: ; preds = %6
%12 = load i32, i32* @stderr
%13 = tail call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0))
br label %34
14: ; preds = %16
%15 = icmp slt i32 %23, 1024
br i1 %15, label %16, label %27
16: ; preds = %6, %14
%17 = phi i32 [ %23, %14 ], [ 0, %6 ]
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 %18
%20 = sub nsw i32 1024, %17
%21 = call i32 @read(i32 %9, i8* %19, i32 %20)
%22 = icmp slt i32 %21, 0
%23 = add nsw i32 %21, %17
br i1 %22, label %24, label %14
24: ; preds = %16
%25 = load i32, i32* @stderr
%26 = tail call i32 @fprintf(i32 %25, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0))
br label %27
27: ; preds = %14, %24
%28 = tail call i32 @close(i32 %9)
%29 = add nuw nsw i32 %7, 1
%30 = icmp eq i32 %29, %0
br i1 %30, label %31, label %6
31: ; preds = %27, %1
%32 = tail call i64 (...) @get_ustime_sec()
%33 = sub nsw i64 %32, %4
br label %34
34: ; preds = %31, %11
%35 = phi i64 [ 0, %11 ], [ %33, %31 ]
ret i64 %35
}