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

/* Variables and functions */
int RAND_MAX;
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fread(int *, int, int, int /*<<< orphan*/ *);
int rand();
int my_rand() {
  enum {
    USE_RAND,
    USE_DEV_RANDOM,
    RAND_UNDEFINED,
  } rand_method = RAND_UNDEFINED;
  static FILE *file;
  int number;
  if (rand_method == RAND_UNDEFINED) {
    file = fopen("/dev/urandom", "r");
    if (file == NULL)
      rand_method = USE_RAND;
    else
      rand_method = USE_DEV_RANDOM;
  }
  switch (rand_method) {
  case USE_DEV_RANDOM:
    fread(&number, sizeof(int), 1, file);
    number = number < 0 ? -number : number;
    number = number % RAND_MAX;
    break;
  default:
    number = rand();
  }
  return number;
}


x86 assembly (clang, -O0):
<my_rand>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x2,-0x4(%rbp)
cmpl   $0x2,-0x4(%rbp)
jne    1189 <my_rand+0x59>
lea    0xeb0(%rip),%rdi
lea    0xeb6(%rip),%rsi
call   1040 <fopen@plt>
mov    %rax,0x2edd(%rip)
cmpq   $0x0,0x2ed5(%rip)
jne    117d <my_rand+0x4d>
movl   $0x0,-0x4(%rbp)
jmp    1184 <my_rand+0x54>
movl   $0x1,-0x4(%rbp)
jmp    1189 <my_rand+0x59>
mov    -0x4(%rbp),%eax
sub    $0x1,%eax
jne    11ec <my_rand+0xbc>
jmp    119a <my_rand+0x6a>
mov    0x2e9f(%rip),%rcx
lea    -0x8(%rbp),%rdi
mov    $0x4,%esi
mov    $0x1,%edx
call   1030 <fread@plt>
cmpl   $0x0,-0x8(%rbp)
jge    11cb <my_rand+0x9b>
xor    %eax,%eax
sub    -0x8(%rbp),%eax
mov    %eax,-0xc(%rbp)
jmp    11d1 <my_rand+0xa1>
mov    -0x8(%rbp),%eax
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%eax
mov    0x2e07(%rip),%rcx
cltd
idivl  (%rcx)
mov    %edx,-0x8(%rbp)
jmp    11f6 <my_rand+0xc6>
mov    $0x0,%al
call   1050 <rand@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<my_rand>:
push   %rax
lea    0xec8(%rip),%rdi
lea    0xece(%rip),%rsi
call   1040 <fopen@plt>
mov    %rax,0x2ef5(%rip)
test   %rax,%rax
je     1180 <my_rand+0x50>
lea    0x4(%rsp),%rdi
mov    $0x4,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   1030 <fread@plt>
mov    0x4(%rsp),%ecx
mov    %ecx,%eax
neg    %eax
cmovs  %ecx,%eax
mov    0x2e6f(%rip),%rcx
cltd
idivl  (%rcx)
mov    %edx,%eax
pop    %rcx
ret
xor    %eax,%eax
pop    %rcx
jmp    1050 <rand@plt>

LLVM IR (-O0):
@my_rand.file = internal global i32* null
@.str = [13 x i8] c"/dev/urandom\00"
@.str.1 = [2 x i8] c"r\00"
@RAND_MAX = global i32 0
define i32 @my_rand() {
%1 = alloca i32
%2 = alloca i32
store i32 2, i32* %1
%3 = load i32, i32* %1
%4 = icmp eq i32 %3, 2
br i1 %4, label %5, label %12
5: ; preds = %0
%6 = call i32* @fopen(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %6, i32** @my_rand.file
%7 = load i32*, i32** @my_rand.file
%8 = icmp eq i32* %7, null
br i1 %8, label %9, label %10
9: ; preds = %5
store i32 0, i32* %1
br label %11
10: ; preds = %5
store i32 1, i32* %1
br label %11
11: ; preds = %10, %9
br label %12
12: ; preds = %11, %0
%13 = load i32, i32* %1
switch i32 %13, label %29 [
i32 1, label %14
]
14: ; preds = %12
%15 = load i32*, i32** @my_rand.file
%16 = call i32 @fread(i32* %2, i32 4, i32 1, i32* %15)
%17 = load i32, i32* %2
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %14
%20 = load i32, i32* %2
%21 = sub nsw i32 0, %20
br label %24
22: ; preds = %14
%23 = load i32, i32* %2
br label %24
24: ; preds = %22, %19
%25 = phi i32 [ %21, %19 ], [ %23, %22 ]
store i32 %25, i32* %2
%26 = load i32, i32* %2
%27 = load i32, i32* @RAND_MAX
%28 = srem i32 %26, %27
store i32 %28, i32* %2
br label %31
29: ; preds = %12
%30 = call i32 (...) @rand()
store i32 %30, i32* %2
br label %31
31: ; preds = %29, %24
%32 = load i32, i32* %2
ret i32 %32
}

LLVM IR (-O2):
@my_rand.file = internal unnamed_addr global i32* null
@.str = [13 x i8] c"/dev/urandom\00"
@.str.1 = [2 x i8] c"r\00"
@RAND_MAX = global i32 0
define i32 @my_rand() {
%1 = alloca i32
%2 = bitcast i32* %1 to i8*
%3 = tail call i32* @fopen(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %3, i32** @my_rand.file
%4 = icmp eq i32* %3, null
br i1 %4, label %11, label %5
5: ; preds = %0
%6 = call i32 @fread(i32* %1, i32 4, i32 1, i32* %3)
%7 = load i32, i32* %1
%8 = tail call i32 @llvm.abs.i32(i32 %7, i1 true)
%9 = load i32, i32* @RAND_MAX
%10 = srem i32 %8, %9
br label %13
11: ; preds = %0
%12 = tail call i32 (...) @rand()
br label %13
13: ; preds = %11, %5
%14 = phi i32 [ %12, %11 ], [ %10, %5 ]
ret i32 %14
}