#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*/ assert(int);
int /*<<< orphan*/ close(int);
int open(char *, int /*<<< orphan*/);
int read(int, char *, int);
void ws_genrandom(char *dest, int length) {
  int fd_random = open("/dev/urandom", O_RDONLY);
  assert(fd_random != -1);
  assert(length == read(fd_random, dest, length));
  close(fd_random);
}


x86 assembly (clang, -O0):
<ws_genrandom>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    0x2e92(%rip),%rax
mov    (%rax),%esi
lea    0xea1(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0xffffffff,-0x10(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
mov    -0xc(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%edi
mov    -0x8(%rbp),%rsi
mov    -0xc(%rbp),%edx
call   1040 <read@plt>
mov    %eax,%ecx
mov    -0x14(%rbp),%eax
cmp    %ecx,%eax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
mov    -0x10(%rbp),%edi
call   1030 <close@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ws_genrandom>:
push   %rbp
push   %r14
push   %rbx
mov    %esi,%ebx
mov    %rdi,%r14
mov    0x2e98(%rip),%rax
mov    (%rax),%esi
lea    0xea7(%rip),%rdi
call   1060 <open@plt>
mov    %eax,%ebp
xor    %edi,%edi
cmp    $0xffffffff,%eax
setne  %dil
call   1050 <assert@plt>
mov    %ebp,%edi
mov    %r14,%rsi
mov    %ebx,%edx
call   1040 <read@plt>
xor    %edi,%edi
cmp    %ebx,%eax
sete   %dil
call   1050 <assert@plt>
mov    %ebp,%edi
pop    %rbx
pop    %r14
pop    %rbp
jmp    1030 <close@plt>

LLVM IR (-O0):
@.str = [13 x i8] c"/dev/urandom\00"
@O_RDONLY = global i32 0
define void @ws_genrandom(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
store i8* %0, i8** %3
store i32 %1, i32* %4
%6 = load i32, i32* @O_RDONLY
%7 = call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %6)
store i32 %7, i32* %5
%8 = load i32, i32* %5
%9 = icmp ne i32 %8, -1
%10 = zext i1 %9 to i32
%11 = call i32 @assert(i32 %10)
%12 = load i32, i32* %4
%13 = load i32, i32* %5
%14 = load i8*, i8** %3
%15 = load i32, i32* %4
%16 = call i32 @read(i32 %13, i8* %14, i32 %15)
%17 = icmp eq i32 %12, %16
%18 = zext i1 %17 to i32
%19 = call i32 @assert(i32 %18)
%20 = load i32, i32* %5
%21 = call i32 @close(i32 %20)
ret void
}

LLVM IR (-O2):
@.str = [13 x i8] c"/dev/urandom\00"
@O_RDONLY = global i32 0
define void @ws_genrandom(i8* nocapture %0, i32 %1) {
%3 = load i32, i32* @O_RDONLY
%4 = tail call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = icmp ne i32 %4, -1
%6 = zext i1 %5 to i32
%7 = tail call i32 @assert(i32 %6)
%8 = tail call i32 @read(i32 %4, i8* %0, i32 %1)
%9 = icmp eq i32 %8, %1
%10 = zext i1 %9 to i32
%11 = tail call i32 @assert(i32 %10)
%12 = tail call i32 @close(i32 %4)
ret void
}