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

/* Variables and functions */
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fread(unsigned long *, int, int, int /*<<< orphan*/ *);
unsigned long bigrand(unsigned long max) {
  FILE *rdp;
  unsigned long num;
  rdp = fopen("/dev/urandom", "rb");
  assert(rdp);
  assert(fread(&num, sizeof(num), 1, rdp) == 1);
  fclose(rdp);
  return num % max;
}


x86 assembly (clang, -O0):
<bigrand>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
lea    0xead(%rip),%rdi
lea    0xeb3(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %eax,%edi
call   1050 <assert@plt>
mov    -0x10(%rbp),%rcx
lea    -0x18(%rbp),%rdi
mov    $0x8,%esi
mov    $0x1,%edx
call   1030 <fread@plt>
cmp    $0x1,%eax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
mov    -0x10(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x18(%rbp),%rax
xor    %ecx,%ecx
mov    %ecx,%edx
divq   -0x8(%rbp)
mov    %rdx,%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bigrand>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
lea    0xeb2(%rip),%rdi
lea    0xeb8(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,%rbx
mov    %ebx,%edi
call   1050 <assert@plt>
mov    %rsp,%rdi
mov    $0x8,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1030 <fread@plt>
xor    %edi,%edi
cmp    $0x1,%eax
sete   %dil
call   1050 <assert@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    (%rsp),%rax
mov    %rax,%rcx
or     %r14,%rcx
shr    $0x20,%rcx
je     11a9 <bigrand+0x69>
xor    %edx,%edx
div    %r14
mov    %rdx,%rax
jmp    11b0 <bigrand+0x70>
xor    %edx,%edx
div    %r14d
mov    %edx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [13 x i8] c"/dev/urandom\00"
@.str.1 = [3 x i8] c"rb\00"
define i64 @bigrand(i64 %0) {
%2 = alloca i64
%3 = alloca i32*
%4 = alloca i64
store i64 %0, i64* %2
%5 = call i32* @fopen(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %5, i32** %3
%6 = load i32*, i32** %3
%7 = ptrtoint i32* %6 to i32
%8 = call i32 @assert(i32 %7)
%9 = load i32*, i32** %3
%10 = call i32 @fread(i64* %4, i32 8, i32 1, i32* %9)
%11 = icmp eq i32 %10, 1
%12 = zext i1 %11 to i32
%13 = call i32 @assert(i32 %12)
%14 = load i32*, i32** %3
%15 = call i32 @fclose(i32* %14)
%16 = load i64, i64* %4
%17 = load i64, i64* %2
%18 = urem i64 %16, %17
ret i64 %18
}

LLVM IR (-O2):
@.str = [13 x i8] c"/dev/urandom\00"
@.str.1 = [3 x i8] c"rb\00"
define i64 @bigrand(i64 %0) {
%2 = alloca i64
%3 = bitcast i64* %2 to i8*
%4 = tail call i32* @fopen(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%5 = ptrtoint i32* %4 to i64
%6 = trunc i64 %5 to i32
%7 = tail call i32 @assert(i32 %6)
%8 = call i32 @fread(i64* %2, i32 8, i32 1, i32* %4)
%9 = icmp eq i32 %8, 1
%10 = zext i1 %9 to i32
%11 = tail call i32 @assert(i32 %10)
%12 = tail call i32 @fclose(i32* %4)
%13 = load i64, i64* %2
%14 = urem i64 %13, %0
ret i64 %14
}