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

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
long abs(int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ random();
int read(int, int /*<<< orphan*/ *, int);
long int olsr_random(void) {
  int32_t value;
  int randomFile;
  randomFile = open("/dev/urandom", O_RDONLY);
  if (randomFile == -1) {
    randomFile = open("/dev/random", O_RDONLY);
  }
  if ((randomFile == -1) ||
      (read(randomFile, &value, sizeof(value)) != sizeof(value))) {
    value = random();
  }
  if (randomFile != -1) {
    close(randomFile);
  }
  return abs(value);
}


x86 assembly (clang, -O0):
<olsr_random>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    0x2e89(%rip),%rax
mov    (%rax),%esi
lea    0xe98(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    1192 <olsr_random+0x42>
mov    0x2e67(%rip),%rax
mov    (%rax),%esi
lea    0xe83(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
je     11b9 <olsr_random+0x69>
mov    -0x8(%rbp),%edi
lea    -0x4(%rbp),%rsi
mov    $0x4,%edx
call   1050 <read@plt>
cltq
cmp    $0x4,%rax
je     11c3 <olsr_random+0x73>
mov    $0x0,%al
call   1060 <random@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
je     11d5 <olsr_random+0x85>
mov    -0x8(%rbp),%edi
call   1040 <close@plt>
mov    -0x4(%rbp),%edi
call   1030 <abs@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<olsr_random>:
push   %rbp
push   %rbx
push   %rax
mov    0x2e8e(%rip),%rbp
mov    0x0(%rbp),%esi
lea    0xe9c(%rip),%rdi
call   1070 <open@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
jne    1186 <olsr_random+0x36>
mov    0x0(%rbp),%esi
lea    0xe93(%rip),%rdi
call   1070 <open@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
je     11bf <olsr_random+0x6f>
lea    0x4(%rsp),%rsi
mov    %ebx,%edi
mov    $0x4,%edx
call   1050 <read@plt>
cmp    $0x4,%eax
jne    11a2 <olsr_random+0x52>
mov    0x4(%rsp),%ebp
jmp    11ab <olsr_random+0x5b>
xor    %eax,%eax
call   1060 <random@plt>
mov    %eax,%ebp
mov    %ebx,%edi
call   1040 <close@plt>
mov    %ebp,%edi
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1030 <abs@plt>
xor    %eax,%eax
call   1060 <random@plt>
mov    %eax,%ebp
jmp    11b2 <olsr_random+0x62>

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

LLVM IR (-O2):
@.str = [13 x i8] c"/dev/urandom\00"
@O_RDONLY = global i32 0
@.str.1 = [12 x i8] c"/dev/random\00"
define i64 @olsr_random() {
%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 %12
6: ; preds = %0
%7 = load i32, i32* @O_RDONLY
%8 = tail call i32 @open(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %7)
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %12
10: ; preds = %6
%11 = tail call i32 (...) @random()
br label %23
12: ; preds = %0, %6
%13 = phi i32 [ %8, %6 ], [ %4, %0 ]
%14 = call i32 @read(i32 %13, i32* %1, i32 4)
%15 = icmp eq i32 %14, 4
br i1 %15, label %16, label %18
16: ; preds = %12
%17 = load i32, i32* %1
br label %20
18: ; preds = %12
%19 = tail call i32 (...) @random()
br label %20
20: ; preds = %16, %18
%21 = phi i32 [ %17, %16 ], [ %19, %18 ]
%22 = tail call i32 @close(i32 %13)
br label %23
23: ; preds = %10, %20
%24 = phi i32 [ %11, %10 ], [ %21, %20 ]
%25 = tail call i64 @abs(i32 %24)
ret i64 %25
}