#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*/ close(int);
unsigned int get_devrand_fallback();
int open(char *, int /*<<< orphan*/);
int read(int, unsigned int *, int);
unsigned int get_devrand(void) {
  int fn;
  unsigned int r;
  fn = open("/dev/urandom", O_RDONLY);
  if (fn == -1)
    return get_devrand_fallback();
  if (read(fn, &r, 4) != 4)
    return get_devrand_fallback();
  close(fn);
  return r;
}


x86 assembly (clang, -O0):
<get_devrand>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    0x2e99(%rip),%rax
mov    (%rax),%esi
lea    0xea8(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    1179 <get_devrand+0x39>
mov    $0x0,%al
call   1050 <get_devrand_fallback@plt>
mov    %eax,-0x4(%rbp)
jmp    11b0 <get_devrand+0x70>
mov    -0x8(%rbp),%edi
lea    -0xc(%rbp),%rsi
mov    $0x4,%edx
call   1040 <read@plt>
cmp    $0x4,%eax
je     11a2 <get_devrand+0x62>
mov    $0x0,%al
call   1050 <get_devrand_fallback@plt>
mov    %eax,-0x4(%rbp)
jmp    11b0 <get_devrand+0x70>
mov    -0x8(%rbp),%edi
call   1030 <close@plt>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_devrand>:
push   %rbx
sub    $0x10,%rsp
mov    0x2e9c(%rip),%rax
mov    (%rax),%esi
lea    0xeab(%rip),%rdi
call   1060 <open@plt>
cmp    $0xffffffff,%eax
je     1188 <get_devrand+0x48>
mov    %eax,%ebx
lea    0xc(%rsp),%rsi
mov    %eax,%edi
mov    $0x4,%edx
call   1040 <read@plt>
cmp    $0x4,%eax
jne    1188 <get_devrand+0x48>
mov    %ebx,%edi
call   1030 <close@plt>
mov    0xc(%rsp),%eax
add    $0x10,%rsp
pop    %rbx
ret
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
jmp    1050 <get_devrand_fallback@plt>

LLVM IR (-O0):
@.str = [13 x i8] c"/dev/urandom\00"
@O_RDONLY = global i32 0
define i32 @get_devrand() {
%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 eq i32 %6, -1
br i1 %7, label %8, label %10
8: ; preds = %0
%9 = call i32 (...) @get_devrand_fallback()
store i32 %9, i32* %1
br label %20
10: ; preds = %0
%11 = load i32, i32* %2
%12 = call i32 @read(i32 %11, i32* %3, i32 4)
%13 = icmp ne i32 %12, 4
br i1 %13, label %14, label %16
14: ; preds = %10
%15 = call i32 (...) @get_devrand_fallback()
store i32 %15, i32* %1
br label %20
16: ; preds = %10
%17 = load i32, i32* %2
%18 = call i32 @close(i32 %17)
%19 = load i32, i32* %3
store i32 %19, i32* %1
br label %20
20: ; preds = %16, %14, %8
%21 = load i32, i32* %1
ret i32 %21
}

LLVM IR (-O2):
@.str = [13 x i8] c"/dev/urandom\00"
@O_RDONLY = global i32 0
define i32 @get_devrand() {
%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 eq i32 %4, -1
br i1 %5, label %6, label %8
6: ; preds = %0
%7 = tail call i32 (...) @get_devrand_fallback()
br label %16
8: ; preds = %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 (...) @get_devrand_fallback()
br label %16
13: ; preds = %8
%14 = tail call i32 @close(i32 %4)
%15 = load i32, i32* %1
br label %16
16: ; preds = %13, %11, %6
%17 = phi i32 [ %7, %6 ], [ %12, %11 ], [ %15, %13 ]
ret i32 %17
}