#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 */
scalar_t__ ENOSYS;
int /*<<< orphan*/ GRND_NONBLOCK;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ abort();
int /*<<< orphan*/ close(int);
scalar_t__ errno;
int getrandom(void *, size_t, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int read(int, void *, size_t);
void arc4random_buf(void *Buf, size_t N) {
  int r;
  if ((size_t)(r = getrandom(Buf, N, GRND_NONBLOCK)) != N) {
    if (r == -1 && errno == ENOSYS) {
      int fd = open("/dev/urandom", O_RDONLY);
      if (fd == -1)
        abort();
      r = read(fd, Buf, N);
      close(fd);
    }
    if ((size_t)r != N)
      abort();
  }
}


x86 assembly (clang, -O0):
<arc4random_buf>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    0x2e69(%rip),%rax
mov    (%rax),%edx
call   1070 <getrandom@plt>
mov    %eax,-0x14(%rbp)
cltq
cmp    -0x10(%rbp),%rax
je     1207 <arc4random_buf+0xb7>
cmpl   $0xffffffff,-0x14(%rbp)
jne    11ed <arc4random_buf+0x9d>
mov    0x2e32(%rip),%rax
mov    (%rax),%rax
mov    0x2e40(%rip),%rcx
cmp    (%rcx),%rax
jne    11ed <arc4random_buf+0x9d>
mov    0x2e38(%rip),%rax
mov    (%rax),%esi
lea    0xe47(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    11d2 <arc4random_buf+0x82>
mov    $0x0,%al
call   1030 <abort@plt>
mov    -0x18(%rbp),%edi
mov    -0x8(%rbp),%rsi
mov    -0x10(%rbp),%rdx
call   1050 <read@plt>
mov    %eax,-0x14(%rbp)
mov    -0x18(%rbp),%edi
call   1040 <close@plt>
movslq -0x14(%rbp),%rax
cmp    -0x10(%rbp),%rax
je     1202 <arc4random_buf+0xb2>
mov    $0x0,%al
call   1030 <abort@plt>
jmp    1207 <arc4random_buf+0xb7>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<arc4random_buf>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %rdi,%r14
mov    0x2e74(%rip),%rax
mov    (%rax),%edx
call   1070 <getrandom@plt>
mov    %eax,%ebp
cltq
cmp    %rbx,%rax
je     11d0 <arc4random_buf+0x80>
cmp    $0xffffffff,%ebp
jne    11c8 <arc4random_buf+0x78>
mov    0x2e48(%rip),%rax
mov    (%rax),%rax
mov    0x2e56(%rip),%rcx
mov    $0xffffffff,%ebp
cmp    (%rcx),%rax
jne    11c8 <arc4random_buf+0x78>
mov    0x2e4d(%rip),%rax
mov    (%rax),%esi
lea    0xe5c(%rip),%rdi
call   1060 <open@plt>
cmp    $0xffffffff,%eax
je     11db <arc4random_buf+0x8b>
mov    %eax,%r15d
mov    %eax,%edi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1050 <read@plt>
mov    %eax,%ebp
mov    %r15d,%edi
call   1040 <close@plt>
movslq %ebp,%rax
cmp    %rbx,%rax
jne    11db <arc4random_buf+0x8b>
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %eax,%eax
call   1030 <abort@plt>

LLVM IR (-O0):
@GRND_NONBLOCK = global i32 0
@errno = global i64 0
@ENOSYS = global i64 0
@.str = [13 x i8] c"/dev/urandom\00"
@O_RDONLY = global i32 0
define void @arc4random_buf(i8* %0, i64 %1) {
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i32
%6 = alloca i32
store i8* %0, i8** %3
store i64 %1, i64* %4
%7 = load i8*, i8** %3
%8 = load i64, i64* %4
%9 = load i32, i32* @GRND_NONBLOCK
%10 = call i32 @getrandom(i8* %7, i64 %8, i32 %9)
store i32 %10, i32* %5
%11 = sext i32 %10 to i64
%12 = load i64, i64* %4
%13 = icmp ne i64 %11, %12
br i1 %13, label %14, label %43
14: ; preds = %2
%15 = load i32, i32* %5
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %35
17: ; preds = %14
%18 = load i64, i64* @errno
%19 = load i64, i64* @ENOSYS
%20 = icmp eq i64 %18, %19
br i1 %20, label %21, label %35
21: ; preds = %17
%22 = load i32, i32* @O_RDONLY
%23 = call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %22)
store i32 %23, i32* %6
%24 = load i32, i32* %6
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %28
26: ; preds = %21
%27 = call i32 (...) @abort()
unreachable
28: ; preds = %21
%29 = load i32, i32* %6
%30 = load i8*, i8** %3
%31 = load i64, i64* %4
%32 = call i32 @read(i32 %29, i8* %30, i64 %31)
store i32 %32, i32* %5
%33 = load i32, i32* %6
%34 = call i32 @close(i32 %33)
br label %35
35: ; preds = %28, %17, %14
%36 = load i32, i32* %5
%37 = sext i32 %36 to i64
%38 = load i64, i64* %4
%39 = icmp ne i64 %37, %38
br i1 %39, label %40, label %42
40: ; preds = %35
%41 = call i32 (...) @abort()
unreachable
42: ; preds = %35
br label %43
43: ; preds = %42, %2
ret void
}

LLVM IR (-O2):
@GRND_NONBLOCK = global i32 0
@errno = global i64 0
@ENOSYS = global i64 0
@.str = [13 x i8] c"/dev/urandom\00"
@O_RDONLY = global i32 0
define void @arc4random_buf(i8* %0, i64 %1) {
%3 = load i32, i32* @GRND_NONBLOCK
%4 = tail call i32 @getrandom(i8* %0, i64 %1, i32 %3)
%5 = sext i32 %4 to i64
%6 = icmp eq i64 %5, %1
br i1 %6, label %28, label %7
7: ; preds = %2
%8 = icmp eq i32 %4, -1
br i1 %8, label %9, label %22
9: ; preds = %7
%10 = load i64, i64* @errno
%11 = load i64, i64* @ENOSYS
%12 = icmp eq i64 %10, %11
br i1 %12, label %13, label %22
13: ; preds = %9
%14 = load i32, i32* @O_RDONLY
%15 = tail call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %14)
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %19
17: ; preds = %13
%18 = tail call i32 (...) @abort()
unreachable
19: ; preds = %13
%20 = tail call i32 @read(i32 %15, i8* %0, i64 %1)
%21 = tail call i32 @close(i32 %15)
br label %22
22: ; preds = %19, %9, %7
%23 = phi i32 [ %20, %19 ], [ -1, %9 ], [ %4, %7 ]
%24 = sext i32 %23 to i64
%25 = icmp eq i64 %24, %1
br i1 %25, label %28, label %26
26: ; preds = %22
%27 = tail call i32 (...) @abort()
unreachable
28: ; preds = %22, %2
ret void
}