#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*/ uint32_t;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int close(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ pfatal(char *);
int read(int, int /*<<< orphan*/ *, int);
uint32_t get_rand_uint32() {
  uint32_t val;
  int fd, ret;
  fd = open("/dev/urandom", O_RDONLY);
  if (fd < 0)
    pfatal("cannot open /dev/urandom");
  ret = read(fd, &val, sizeof(uint32_t));
  if (ret != sizeof(uint32_t)) {
    pfatal("error while reading from /dev/urandom");
  }
  ret = close(fd);
  if (ret < 0)
    pfatal("error while closing /dev/urandom");
  return val;
}


x86 assembly (clang, -O0):
<get_rand_uint32>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    0x2e99(%rip),%rax
mov    (%rax),%esi
lea    0xefb(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    1176 <get_rand_uint32+0x36>
lea    0xe8f(%rip),%rdi
call   1030 <pfatal@plt>
mov    -0x8(%rbp),%edi
lea    -0x4(%rbp),%rsi
mov    $0x4,%edx
call   1050 <read@plt>
mov    %eax,-0xc(%rbp)
movslq -0xc(%rbp),%rax
cmp    $0x4,%rax
je     11a4 <get_rand_uint32+0x64>
lea    0xe7a(%rip),%rdi
call   1030 <pfatal@plt>
mov    -0x8(%rbp),%edi
call   1040 <close@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    11c5 <get_rand_uint32+0x85>
lea    0xe7f(%rip),%rdi
call   1030 <pfatal@plt>
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_rand_uint32>:
push   %rbx
sub    $0x10,%rsp
mov    0x2e9c(%rip),%rax
mov    (%rax),%esi
lea    0xefe(%rip),%rdi
call   1060 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    116c <get_rand_uint32+0x2c>
lea    0xe99(%rip),%rdi
call   1030 <pfatal@plt>
lea    0xc(%rsp),%rsi
mov    %ebx,%edi
mov    $0x4,%edx
call   1050 <read@plt>
cmp    $0x4,%eax
je     118e <get_rand_uint32+0x4e>
lea    0xe90(%rip),%rdi
call   1030 <pfatal@plt>
mov    %ebx,%edi
call   1040 <close@plt>
test   %eax,%eax
jns    11a5 <get_rand_uint32+0x65>
lea    0xe9f(%rip),%rdi
call   1030 <pfatal@plt>
mov    0xc(%rsp),%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [13 x i8] c"/dev/urandom\00"
@O_RDONLY = global i32 0
@.str.1 = [25 x i8] c"cannot open /dev/urandom\00"
@.str.2 = [38 x i8] c"error while reading from /dev/urandom\00"
@.str.3 = [33 x i8] c"error while closing /dev/urandom\00"
define i32 @get_rand_uint32() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = load i32, i32* @O_RDONLY
%5 = call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %4)
store i32 %5, i32* %2
%6 = load i32, i32* %2
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %0
%9 = call i32 @pfatal(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
br label %10
10: ; preds = %8, %0
%11 = load i32, i32* %2
%12 = call i32 @read(i32 %11, i32* %1, i32 4)
store i32 %12, i32* %3
%13 = load i32, i32* %3
%14 = sext i32 %13 to i64
%15 = icmp ne i64 %14, 4
br i1 %15, label %16, label %18
16: ; preds = %10
%17 = call i32 @pfatal(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.2, i64 0, i64 0))
br label %18
18: ; preds = %16, %10
%19 = load i32, i32* %2
%20 = call i32 @close(i32 %19)
store i32 %20, i32* %3
%21 = load i32, i32* %3
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %18
%24 = call i32 @pfatal(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0))
br label %25
25: ; preds = %23, %18
%26 = load i32, i32* %1
ret i32 %26
}

LLVM IR (-O2):
@.str = [13 x i8] c"/dev/urandom\00"
@O_RDONLY = global i32 0
@.str.1 = [25 x i8] c"cannot open /dev/urandom\00"
@.str.2 = [38 x i8] c"error while reading from /dev/urandom\00"
@.str.3 = [33 x i8] c"error while closing /dev/urandom\00"
define i32 @get_rand_uint32() {
%1 = alloca i32
%2 = bitcast i32* %1 to i8*
%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 slt i32 %4, 0
br i1 %5, label %6, label %8
6: ; preds = %0
%7 = tail call i32 @pfatal(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
br label %8
8: ; preds = %6, %0
%9 = call i32 @read(i32 %4, i32* %1, i32 4)
%10 = icmp eq i32 %9, 4
br i1 %10, label %13, label %11
11: ; preds = %8
%12 = tail call i32 @pfatal(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.2, i64 0, i64 0))
br label %13
13: ; preds = %11, %8
%14 = tail call i32 @close(i32 %4)
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %13
%17 = tail call i32 @pfatal(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0))
br label %18
18: ; preds = %16, %13
%19 = load i32, i32* %1
ret i32 %19
}