#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 O_NONBLOCK;
int /*<<< orphan*/ close(int);
int open(char const *, int);
int read(int, void *, size_t);
int getrandom(void *buf, size_t buflen, unsigned int flags) {
  const char *devname = (flags & 2) ? "/dev/random" : "/dev/urandom";
  int devflag = 0;
  int dev;
  int rval;
  if (flags & 1)
    devflag |= O_NONBLOCK;
  dev = open(devname, devflag);
  if (dev == -1)
    return -1;
  rval = read(dev, buf, buflen);
  close(dev);
  return rval;
}


x86 assembly (clang, -O0):
<getrandom>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x1c(%rbp),%edx
and    $0x2,%edx
lea    0xebc(%rip),%rax
lea    0xea9(%rip),%rcx
cmp    $0x0,%edx
cmovne %rcx,%rax
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x1c(%rbp),%eax
and    $0x1,%eax
cmp    $0x0,%eax
je     1187 <getrandom+0x57>
mov    0x2e69(%rip),%rax
mov    (%rax),%eax
or     -0x2c(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x2c(%rbp),%esi
call   1050 <open@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0xffffffff,-0x30(%rbp)
jne    11ac <getrandom+0x7c>
movl   $0xffffffff,-0x4(%rbp)
jmp    11cd <getrandom+0x9d>
mov    -0x30(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1040 <read@plt>
mov    %eax,-0x34(%rbp)
mov    -0x30(%rbp),%edi
call   1030 <close@plt>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getrandom>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%r14
mov    %rdi,%rbx
test   $0x2,%dl
lea    0xec8(%rip),%rax
lea    0xeb5(%rip),%rdi
cmove  %rax,%rdi
and    $0x1,%edx
neg    %edx
mov    0x2e8d(%rip),%rax
and    (%rax),%edx
mov    %edx,%esi
call   1050 <open@plt>
cmp    $0xffffffff,%eax
je     1185 <getrandom+0x55>
mov    %eax,%ebp
mov    %eax,%edi
mov    %rbx,%rsi
mov    %r14,%rdx
call   1040 <read@plt>
mov    %eax,%ebx
mov    %ebp,%edi
call   1030 <close@plt>
mov    %ebx,%eax
jmp    118a <getrandom+0x5a>
mov    $0xffffffff,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [12 x i8] c"/dev/random\00"
@.str.1 = [13 x i8] c"/dev/urandom\00"
@O_NONBLOCK = global i32 0
define i32 @getrandom(i8* nocapture %0, i64 %1, i32 %2) {
%4 = and i32 %2, 2
%5 = icmp eq i32 %4, 0
%6 = select i1 %5, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0)
%7 = and i32 %2, 1
%8 = icmp eq i32 %7, 0
%9 = load i32, i32* @O_NONBLOCK
%10 = select i1 %8, i32 0, i32 %9
%11 = tail call i32 @open(i8* %6, i32 %10)
%12 = icmp eq i32 %11, -1
br i1 %12, label %16, label %13
13: ; preds = %3
%14 = tail call i32 @read(i32 %11, i8* %0, i64 %1)
%15 = tail call i32 @close(i32 %11)
br label %16
16: ; preds = %3, %13
%17 = phi i32 [ %14, %13 ], [ -1, %3 ]
ret i32 %17
}