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

/* Variables and functions */
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
scalar_t__ fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
scalar_t__ malloc(int);
int /*<<< orphan*/ mpz_import(int /*<<< orphan*/, int, int, int,
                              int /*<<< orphan*/, int /*<<< orphan*/, char *);
int /*<<< orphan*/ stderr;
int gen_pseudorandom(mpz_t rnd, mp_bitcnt_t len) {
  FILE *dev_urandom;
  int byte_count, byte_read;
  char *seed;
  byte_count = (len + 7) >> 3;
  dev_urandom = fopen("/dev/urandom", "r");
  if (dev_urandom == NULL) {
    fprintf(stderr, "cannot open random number device!\n");
    exit(1);
  }
  seed = (char *)malloc(sizeof(char) * byte_count);
  byte_read = 0;
  while (byte_read < byte_count) {
    byte_read += fread(seed, sizeof(char), byte_count, dev_urandom);
  }
  fclose(dev_urandom);
  mpz_import(rnd, byte_count, 1, sizeof(seed[0]), 0, 0, seed);
  free(seed);
  return 0;
}


x86 assembly (clang, -O0):
<gen_pseudorandom>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    -0x8(%rbp),%eax
add    $0x7,%eax
sar    $0x3,%eax
mov    %eax,-0x14(%rbp)
lea    0xe5f(%rip),%rdi
lea    0xe65(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11db <gen_pseudorandom+0x5b>
mov    0x2e1d(%rip),%rax
mov    (%rax),%edi
lea    0xe43(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
movslq -0x14(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1070 <malloc@plt>
mov    %rax,-0x20(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    1228 <gen_pseudorandom+0xa8>
mov    -0x20(%rbp),%rdi
mov    -0x14(%rbp),%edx
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
mov    %rax,%rcx
movslq -0x18(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x18(%rbp)
jmp    11f5 <gen_pseudorandom+0x75>
mov    -0x10(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x4(%rbp),%edi
mov    -0x14(%rbp),%esi
mov    -0x20(%rbp),%rax
mov    $0x1,%ecx
xor    %r9d,%r9d
mov    %ecx,%edx
mov    %r9d,%r8d
mov    %rax,(%rsp)
call   1090 <mpz_import@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gen_pseudorandom>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %esi,%r12d
mov    %edi,%r14d
lea    0xe68(%rip),%rdi
lea    0xe6e(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     1227 <gen_pseudorandom+0xa7>
mov    %rax,%r13
lea    0x7(%r12),%ebp
sar    $0x3,%ebp
mov    %ebp,%edi
call   1070 <malloc@plt>
mov    %rax,%r15
test   %r12d,%r12d
jle    11e8 <gen_pseudorandom+0x68>
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r15,%rdi
mov    $0x1,%esi
mov    %ebp,%edx
mov    %r13,%rcx
call   1040 <fread@plt>
add    %eax,%ebx
cmp    %ebp,%ebx
jl     11d0 <gen_pseudorandom+0x50>
mov    %r13,%rdi
call   1050 <fclose@plt>
mov    %r15,(%rsp)
mov    %r14d,%edi
mov    %ebp,%esi
mov    $0x1,%edx
mov    $0x1,%ecx
xor    %r8d,%r8d
xor    %r9d,%r9d
call   1090 <mpz_import@plt>
mov    %r15,%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2db2(%rip),%rax
mov    (%rax),%edi
lea    0xdd8(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [13 x i8] c"/dev/urandom\00"
@.str.1 = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.2 = [35 x i8] c"cannot open random number device!\0A\00"
define i32 @gen_pseudorandom(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
store i32 %0, i32* %3
store i32 %1, i32* %4
%9 = load i32, i32* %4
%10 = add nsw i32 %9, 7
%11 = ashr i32 %10, 3
store i32 %11, i32* %6
%12 = 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* %12, i32** %5
%13 = load i32*, i32** %5
%14 = icmp eq i32* %13, null
br i1 %14, label %15, label %19
15: ; preds = %2
%16 = load i32, i32* @stderr
%17 = call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0))
%18 = call i32 @exit(i32 1)
unreachable
19: ; preds = %2
%20 = load i32, i32* %6
%21 = sext i32 %20 to i64
%22 = mul i64 1, %21
%23 = trunc i64 %22 to i32
%24 = call i64 @malloc(i32 %23)
%25 = inttoptr i64 %24 to i8*
store i8* %25, i8** %8
store i32 0, i32* %7
br label %26
26: ; preds = %30, %19
%27 = load i32, i32* %7
%28 = load i32, i32* %6
%29 = icmp slt i32 %27, %28
br i1 %29, label %30, label %39
30: ; preds = %26
%31 = load i8*, i8** %8
%32 = load i32, i32* %6
%33 = load i32*, i32** %5
%34 = call i64 @fread(i8* %31, i32 1, i32 %32, i32* %33)
%35 = load i32, i32* %7
%36 = sext i32 %35 to i64
%37 = add nsw i64 %36, %34
%38 = trunc i64 %37 to i32
store i32 %38, i32* %7
br label %26
39: ; preds = %26
%40 = load i32*, i32** %5
%41 = call i32 @fclose(i32* %40)
%42 = load i32, i32* %3
%43 = load i32, i32* %6
%44 = load i8*, i8** %8
%45 = call i32 @mpz_import(i32 %42, i32 %43, i32 1, i32 1, i32 0, i32 0, i8* %44)
%46 = load i8*, i8** %8
%47 = call i32 @free(i8* %46)
ret i32 0
}

LLVM IR (-O2):
@.str = [13 x i8] c"/dev/urandom\00"
@.str.1 = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.2 = [35 x i8] c"cannot open random number device!\0A\00"
define i32 @gen_pseudorandom(i32 %0, i32 %1) {
%3 = add nsw i32 %1, 7
%4 = ashr i32 %3, 3
%5 = 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))
%6 = icmp eq i32* %5, null
br i1 %6, label %7, label %11
7: ; preds = %2
%8 = load i32, i32* @stderr
%9 = tail call i32 @fprintf(i32 %8, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0))
%10 = tail call i32 @exit(i32 1)
unreachable
11: ; preds = %2
%12 = tail call i64 @malloc(i32 %4)
%13 = inttoptr i64 %12 to i8*
%14 = icmp sgt i32 %1, 0
br i1 %14, label %15, label %21
15: ; preds = %11, %15
%16 = phi i32 [ %19, %15 ], [ 0, %11 ]
%17 = tail call i64 @fread(i8* %13, i32 1, i32 %4, i32* %5)
%18 = trunc i64 %17 to i32
%19 = add i32 %16, %18
%20 = icmp slt i32 %19, %4
br i1 %20, label %15, label %21
21: ; preds = %15, %11
%22 = tail call i32 @fclose(i32* %5)
%23 = tail call i32 @mpz_import(i32 %0, i32 %4, i32 1, i32 1, i32 0, i32 0, i8* %13)
%24 = tail call i32 @free(i8* %13)
ret i32 0
}