#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 */
typedef int /*<<< orphan*/ randVal;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ read(int, unsigned int *, int);
unsigned int getRand(void) {
  unsigned int randVal;
  int urandom_fd;
  urandom_fd = open("/dev/random", O_RDONLY);
  if (-1 == urandom_fd) {
    int errsv = urandom_fd;
    printf("Error opening [/dev/urandom]: %i\n", errsv);
    exit(1);
  }
  read(urandom_fd, &randVal, sizeof(randVal));
  close(urandom_fd);
  return randVal;
}


x86 assembly (clang, -O0):
<getRand>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    0x2e89(%rip),%rax
mov    (%rax),%esi
lea    0xe98(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x8(%rbp)
mov    $0xffffffff,%eax
cmp    -0x8(%rbp),%eax
jne    119d <getRand+0x4d>
mov    -0x8(%rbp),%eax
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%esi
lea    0xe7e(%rip),%rdi
call   1030 <printf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x8(%rbp),%edi
lea    -0x4(%rbp),%rsi
mov    $0x4,%edx
call   1050 <read@plt>
mov    -0x8(%rbp),%edi
call   1040 <close@plt>
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getRand>:
push   %rbx
sub    $0x10,%rsp
mov    0x2e8c(%rip),%rax
mov    (%rax),%esi
lea    0xe9b(%rip),%rdi
call   1060 <open@plt>
cmp    $0xffffffff,%eax
je     1193 <getRand+0x43>
mov    %eax,%ebx
lea    0xc(%rsp),%rsi
mov    %eax,%edi
mov    $0x4,%edx
call   1050 <read@plt>
mov    %ebx,%edi
call   1040 <close@plt>
mov    0xc(%rsp),%eax
add    $0x10,%rsp
pop    %rbx
ret
lea    0xe72(%rip),%rdi
mov    $0xffffffff,%esi
call   1030 <printf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [12 x i8] c"/dev/random\00"
@O_RDONLY = global i32 0
@.str.1 = [34 x i8] c"Error opening [/dev/urandom]: %i\0A\00"
define i32 @getRand() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = load i32, i32* @O_RDONLY
%5 = call i32 @open(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %4)
store i32 %5, i32* %2
%6 = load i32, i32* %2
%7 = icmp eq i32 -1, %6
br i1 %7, label %8, label %13
8: ; preds = %0
%9 = load i32, i32* %2
store i32 %9, i32* %3
%10 = load i32, i32* %3
%11 = call i32 @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0), i32 %10)
%12 = call i32 @exit(i32 1)
unreachable
13: ; preds = %0
%14 = load i32, i32* %2
%15 = call i32 @read(i32 %14, i32* %1, i32 4)
%16 = load i32, i32* %2
%17 = call i32 @close(i32 %16)
%18 = load i32, i32* %1
ret i32 %18
}

LLVM IR (-O2):
@.str = [12 x i8] c"/dev/random\00"
@O_RDONLY = global i32 0
@.str.1 = [34 x i8] c"Error opening [/dev/urandom]: %i\0A\00"
define i32 @getRand() {
%1 = alloca i32
%2 = bitcast i32* %1 to i8*
%3 = load i32, i32* @O_RDONLY
%4 = tail call i32 @open(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = icmp eq i32 %4, -1
br i1 %5, label %6, label %9
6: ; preds = %0
%7 = tail call i32 @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0), i32 -1)
%8 = tail call i32 @exit(i32 1)
unreachable
9: ; preds = %0
%10 = call i32 @read(i32 %4, i32* %1, i32 4)
%11 = tail call i32 @close(i32 %4)
%12 = load i32, i32* %1
ret i32 %12
}