#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 */
struct timeval {
  int tv_sec;
  scalar_t__ tv_usec;
};
typedef int /*<<< orphan*/ fd_set;

/* Variables and functions */
scalar_t__ EINTR;
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dropbear_exit(char *);
int /*<<< orphan*/ dropbear_log(char *);
scalar_t__ errno;
int open(char *, int /*<<< orphan*/);
int read(int, unsigned char *, unsigned int);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
void readrand(unsigned char *buf, unsigned int buflen) {
  static int already_blocked = 0;
  int readfd;
  unsigned int readpos;
  int readlen;
  readfd = open("/dev/urandom", O_RDONLY);
  if (readfd < 0)
    dropbear_exit("Couldn't open random device");
  readpos = 0;
  do {
    if (!already_blocked) {
      int ret;
      struct timeval timeout;
      fd_set read_fds;
      timeout.tv_sec = 2;
      timeout.tv_usec = 0;
      FD_ZERO(&read_fds);
      FD_SET(readfd, &read_fds);
      ret = select(readfd + 1, &read_fds, NULL, NULL, &timeout);
      if (ret == 0) {
        dropbear_log("Warning: Reading the random source seems to have "
                     "blocked.\nIf you experience problems, you probably need "
                     "to find a better entropy source.");
        already_blocked = 1;
      }
    }
    readlen = read(readfd, &buf[readpos], buflen - readpos);
    if (readlen <= 0) {
      if (readlen < 0 && errno == EINTR) {
        continue;
      }
      dropbear_exit("Error reading random source");
    }
    readpos += readlen;
  } while (readpos < buflen);
  close(readfd);
}


x86 assembly (clang, -O0):
<readrand>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    0x2e52(%rip),%rax
mov    (%rax),%esi
lea    0xe61(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    11bd <readrand+0x3d>
lea    0xe55(%rip),%rdi
call   1080 <dropbear_exit@plt>
movl   $0x0,-0x14(%rbp)
cmpl   $0x0,0x2e9d(%rip)
jne    1237 <readrand+0xb7>
movl   $0x2,-0x30(%rbp)
movq   $0x0,-0x28(%rbp)
lea    -0x34(%rbp),%rdi
call   1050 <FD_ZERO@plt>
mov    -0x10(%rbp),%edi
lea    -0x34(%rbp),%rsi
call   10a0 <FD_SET@plt>
mov    -0x10(%rbp),%edi
add    $0x1,%edi
lea    -0x34(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x30(%rbp),%r8
mov    %rcx,%rdx
call   1060 <select@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jne    1232 <readrand+0xb2>
lea    0xe06(%rip),%rdi
call   1090 <dropbear_log@plt>
movl   $0x1,0x2e36(%rip)
jmp    1237 <readrand+0xb7>
mov    -0x10(%rbp),%edi
mov    -0x8(%rbp),%rsi
mov    -0x14(%rbp),%eax
add    %rax,%rsi
mov    -0xc(%rbp),%edx
sub    -0x14(%rbp),%edx
call   1040 <read@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jg     1291 <readrand+0x111>
cmpl   $0x0,-0x18(%rbp)
jge    1285 <readrand+0x105>
mov    0x2d6b(%rip),%rax
mov    (%rax),%rax
mov    0x2d59(%rip),%rcx
cmp    (%rcx),%rax
jne    1285 <readrand+0x105>
jmp    129a <readrand+0x11a>
lea    0xe26(%rip),%rdi
call   1080 <dropbear_exit@plt>
mov    -0x18(%rbp),%eax
add    -0x14(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0xc(%rbp),%eax
jb     11c4 <readrand+0x44>
mov    -0x10(%rbp),%edi
call   1030 <close@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readrand>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %esi,%ebx
mov    %rdi,%r14
mov    0x2e4e(%rip),%rax
mov    (%rax),%esi
lea    0xe5d(%rip),%rdi
call   1070 <open@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    11ba <readrand+0x3a>
lea    0xe58(%rip),%rdi
call   1080 <dropbear_exit@plt>
mov    %ebp,%eax
add    $0x1,%eax
mov    %eax,(%rsp)
xor    %r15d,%r15d
lea    0x4(%rsp),%r13
nopw   0x0(%rax,%rax,1)
cmpb   $0x0,0x2e91(%rip)
jne    1230 <readrand+0xb0>
movl   $0x2,0x8(%rsp)
movq   $0x0,0x10(%rsp)
mov    %r13,%rdi
call   1050 <FD_ZERO@plt>
mov    %ebp,%edi
mov    %r13,%rsi
call   10a0 <FD_SET@plt>
mov    (%rsp),%edi
mov    %r13,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
lea    0x8(%rsp),%r8
call   1060 <select@plt>
test   %eax,%eax
jne    1230 <readrand+0xb0>
lea    0xe0e(%rip),%rdi
call   1090 <dropbear_log@plt>
movb   $0x1,0x2e41(%rip)
nopw   0x0(%rax,%rax,1)
mov    %r15d,%esi
add    %r14,%rsi
mov    %ebx,%edx
sub    %r15d,%edx
mov    %ebp,%edi
call   1040 <read@plt>
mov    %eax,%r12d
test   %eax,%eax
jg     126d <readrand+0xed>
jns    1261 <readrand+0xe1>
mov    0x2d86(%rip),%rax
mov    (%rax),%rax
mov    0x2d74(%rip),%rcx
cmp    (%rcx),%rax
je     1270 <readrand+0xf0>
lea    0xe4a(%rip),%rdi
call   1080 <dropbear_exit@plt>
add    %r12d,%r15d
cmp    %ebx,%r15d
jb     11d0 <readrand+0x50>
mov    %ebp,%edi
call   1030 <close@plt>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i64 }
@readrand.already_blocked = internal global i32 0
@.str = [13 x i8] c"/dev/urandom\00"
@O_RDONLY = global i32 0
@.str.1 = [28 x i8] c"Couldn't open random device\00"
@.str.2 = [137 x i8] c"Warning: Reading the random source seems to have blocked.\0AIf you experience problems, you probably need to find a better entropy source.\00"
@errno = global i64 0
@EINTR = global i64 0
@.str.3 = [28 x i8] c"Error reading random source\00"
define void @readrand(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca %struct.timeval
%10 = alloca i32
store i8* %0, i8** %3
store i32 %1, i32* %4
%11 = load i32, i32* @O_RDONLY
%12 = call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %11)
store i32 %12, i32* %5
%13 = load i32, i32* %5
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %2
%16 = call i32 @dropbear_exit(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
br label %17
17: ; preds = %15, %2
store i32 0, i32* %6
br label %18
18: ; preds = %61, %17
%19 = load i32, i32* @readrand.already_blocked
%20 = icmp ne i32 %19, 0
br i1 %20, label %35, label %21
21: ; preds = %18
%22 = getelementptr inbounds %struct.timeval, %struct.timeval* %9, i32 0, i32 0
store i32 2, i32* %22
%23 = getelementptr inbounds %struct.timeval, %struct.timeval* %9, i32 0, i32 1
store i64 0, i64* %23
%24 = call i32 @FD_ZERO(i32* %10)
%25 = load i32, i32* %5
%26 = call i32 @FD_SET(i32 %25, i32* %10)
%27 = load i32, i32* %5
%28 = add nsw i32 %27, 1
%29 = call i32 @select(i32 %28, i32* %10, i32* null, i32* null, %struct.timeval* %9)
store i32 %29, i32* %8
%30 = load i32, i32* %8
%31 = icmp eq i32 %30, 0
br i1 %31, label %32, label %34
32: ; preds = %21
%33 = call i32 @dropbear_log(i8* getelementptr inbounds ([137 x i8], [137 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* @readrand.already_blocked
br label %34
34: ; preds = %32, %21
br label %35
35: ; preds = %34, %18
%36 = load i32, i32* %5
%37 = load i8*, i8** %3
%38 = load i32, i32* %6
%39 = zext i32 %38 to i64
%40 = getelementptr inbounds i8, i8* %37, i64 %39
%41 = load i32, i32* %4
%42 = load i32, i32* %6
%43 = sub i32 %41, %42
%44 = call i32 @read(i32 %36, i8* %40, i32 %43)
store i32 %44, i32* %7
%45 = load i32, i32* %7
%46 = icmp sle i32 %45, 0
br i1 %46, label %47, label %57
47: ; preds = %35
%48 = load i32, i32* %7
%49 = icmp slt i32 %48, 0
br i1 %49, label %50, label %55
50: ; preds = %47
%51 = load i64, i64* @errno
%52 = load i64, i64* @EINTR
%53 = icmp eq i64 %51, %52
br i1 %53, label %54, label %55
54: ; preds = %50
br label %61
55: ; preds = %50, %47
%56 = call i32 @dropbear_exit(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0))
br label %57
57: ; preds = %55, %35
%58 = load i32, i32* %7
%59 = load i32, i32* %6
%60 = add i32 %59, %58
store i32 %60, i32* %6
br label %61
61: ; preds = %57, %54
%62 = load i32, i32* %6
%63 = load i32, i32* %4
%64 = icmp ult i32 %62, %63
br i1 %64, label %18, label %65
65: ; preds = %61
%66 = load i32, i32* %5
%67 = call i32 @close(i32 %66)
ret void
}

LLVM IR (-O2):
%struct.timeval = type { i32, i64 }
@readrand.already_blocked = internal unnamed_addr global i1 false
@.str = [13 x i8] c"/dev/urandom\00"
@O_RDONLY = global i32 0
@.str.1 = [28 x i8] c"Couldn't open random device\00"
@.str.2 = [137 x i8] c"Warning: Reading the random source seems to have blocked.\0AIf you experience problems, you probably need to find a better entropy source.\00"
@errno = global i64 0
@EINTR = global i64 0
@.str.3 = [28 x i8] c"Error reading random source\00"
define void @readrand(i8* nocapture %0, i32 %1) {
%3 = alloca %struct.timeval
%4 = alloca i32
%5 = load i32, i32* @O_RDONLY
%6 = tail call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %5)
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %2
%9 = tail call i32 @dropbear_exit(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
br label %10
10: ; preds = %8, %2
%11 = bitcast %struct.timeval* %3 to i8*
%12 = bitcast i32* %4 to i8*
%13 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 0
%14 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 1
%15 = add nsw i32 %6, 1
br label %16
16: ; preds = %43, %10
%17 = phi i32 [ 0, %10 ], [ %44, %43 ]
%18 = load i1, i1* @readrand.already_blocked
br i1 %18, label %27, label %19
19: ; preds = %16
store i32 2, i32* %13
store i64 0, i64* %14
%20 = call i32 @FD_ZERO(i32* %4)
%21 = call i32 @FD_SET(i32 %6, i32* %4)
%22 = call i32 @select(i32 %15, i32* %4, i32* null, i32* null, %struct.timeval* %3)
%23 = icmp eq i32 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %19
%25 = call i32 @dropbear_log(i8* getelementptr inbounds ([137 x i8], [137 x i8]* @.str.2, i64 0, i64 0))
store i1 true, i1* @readrand.already_blocked
br label %26
26: ; preds = %24, %19
br label %27
27: ; preds = %26, %16
%28 = zext i32 %17 to i64
%29 = getelementptr inbounds i8, i8* %0, i64 %28
%30 = sub i32 %1, %17
%31 = call i32 @read(i32 %6, i8* %29, i32 %30)
%32 = icmp slt i32 %31, 1
br i1 %32, label %33, label %41
33: ; preds = %27
%34 = icmp slt i32 %31, 0
br i1 %34, label %35, label %39
35: ; preds = %33
%36 = load i64, i64* @errno
%37 = load i64, i64* @EINTR
%38 = icmp eq i64 %36, %37
br i1 %38, label %43, label %39
39: ; preds = %35, %33
%40 = call i32 @dropbear_exit(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0))
br label %41
41: ; preds = %39, %27
%42 = add i32 %31, %17
br label %43
43: ; preds = %35, %41
%44 = phi i32 [ %17, %35 ], [ %42, %41 ]
%45 = icmp ult i32 %44, %1
br i1 %45, label %16, label %46
46: ; preds = %43
%47 = call i32 @close(i32 %6)
ret void
}