#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_random(mpz_t rnd, mp_bitcnt_t len) {
  FILE *dev_random, *dev_urandom;
  int byte_count, byte_read;
  char *seed;
  byte_count = (len + 7) >> 3;
  dev_random = fopen("/dev/random", "r");
  if (dev_random == NULL) {
    fprintf(stderr, "cannot open random number device!\n");
    exit(1);
  }
  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 < 16 && byte_read < byte_count) {
    byte_read += fread(seed, sizeof(char), byte_count, dev_random);
  }
  fclose(dev_random);
  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_random>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    -0x8(%rbp),%eax
add    $0x7,%eax
sar    $0x3,%eax
mov    %eax,-0x1c(%rbp)
lea    0xe5f(%rip),%rdi
lea    0xe64(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11db <gen_random+0x5b>
mov    0x2e1d(%rip),%rax
mov    (%rax),%edi
lea    0xe42(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
lea    0xe4f(%rip),%rdi
lea    0xe23(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    121c <gen_random+0x9c>
mov    0x2ddc(%rip),%rax
mov    (%rax),%edi
lea    0xe01(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
movslq -0x1c(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1070 <malloc@plt>
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x20(%rbp)
xor    %eax,%eax
cmpl   $0x10,-0x20(%rbp)
mov    %al,-0x29(%rbp)
jge    1251 <gen_random+0xd1>
mov    -0x20(%rbp),%eax
cmp    -0x1c(%rbp),%eax
setl   %al
mov    %al,-0x29(%rbp)
mov    -0x29(%rbp),%al
test   $0x1,%al
jne    1261 <gen_random+0xe1>
jmp    1288 <gen_random+0x108>
mov    -0x28(%rbp),%rdi
mov    -0x1c(%rbp),%edx
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
mov    %rax,%rcx
movslq -0x20(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x20(%rbp)
jmp    1236 <gen_random+0xb6>
mov    -0x10(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x20(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    12c4 <gen_random+0x144>
mov    -0x28(%rbp),%rdi
mov    -0x1c(%rbp),%edx
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
mov    %rax,%rcx
movslq -0x20(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x20(%rbp)
jmp    1291 <gen_random+0x111>
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x4(%rbp),%edi
mov    -0x1c(%rbp),%esi
mov    -0x28(%rbp),%rax
mov    $0x1,%ecx
xor    %r9d,%r9d
mov    %ecx,%edx
mov    %r9d,%r8d
mov    %rax,(%rsp)
call   1090 <mpz_import@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gen_random>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %esi,%ebx
mov    %edi,%ebp
lea    0xe67(%rip),%rdi
lea    0xe6c(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     1288 <gen_random+0x108>
mov    %rax,%r12
lea    0xe79(%rip),%rdi
lea    0xe4d(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     1288 <gen_random+0x108>
mov    %rax,%r15
mov    %ebp,0x14(%rsp)
lea    0x7(%rbx),%ebp
sar    $0x3,%ebp
mov    %ebp,%edi
call   1070 <malloc@plt>
mov    %rax,%r13
test   %ebx,%ebx
jle    1216 <gen_random+0x96>
xor    %ebx,%ebx
nopw   0x0(%rax,%rax,1)
mov    %ebx,%r14d
mov    %r13,%rdi
mov    $0x1,%esi
mov    %ebp,%edx
mov    %r12,%rcx
call   1040 <fread@plt>
mov    %rax,%rbx
add    %r14d,%ebx
cmp    $0xf,%ebx
jg     1218 <gen_random+0x98>
cmp    %ebp,%ebx
jl     11f0 <gen_random+0x70>
jmp    1218 <gen_random+0x98>
xor    %ebx,%ebx
mov    %r12,%rdi
call   1050 <fclose@plt>
cmp    %ebp,%ebx
jge    1248 <gen_random+0xc8>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %r13,%rdi
mov    $0x1,%esi
mov    %ebp,%edx
mov    %r15,%rcx
call   1040 <fread@plt>
add    %eax,%ebx
cmp    %ebp,%ebx
jl     1230 <gen_random+0xb0>
mov    %r15,%rdi
call   1050 <fclose@plt>
mov    %r13,(%rsp)
mov    0x14(%rsp),%edi
mov    %ebp,%esi
mov    $0x1,%edx
mov    $0x1,%ecx
xor    %r8d,%r8d
xor    %r9d,%r9d
call   1090 <mpz_import@plt>
mov    %r13,%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d51(%rip),%rax
mov    (%rax),%edi
lea    0xd76(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [12 x i8] c"/dev/random\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"
@.str.3 = [13 x i8] c"/dev/urandom\00"
define i32 @gen_random(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
store i32 %0, i32* %3
store i32 %1, i32* %4
%10 = load i32, i32* %4
%11 = add nsw i32 %10, 7
%12 = ashr i32 %11, 3
store i32 %12, i32* %7
%13 = call i32* @fopen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %13, i32** %5
%14 = load i32*, i32** %5
%15 = icmp eq i32* %14, null
br i1 %15, label %16, label %20
16: ; preds = %2
%17 = load i32, i32* @stderr
%18 = call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0))
%19 = call i32 @exit(i32 1)
unreachable
20: ; preds = %2
%21 = call i32* @fopen(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %21, i32** %6
%22 = load i32*, i32** %6
%23 = icmp eq i32* %22, null
br i1 %23, label %24, label %28
24: ; preds = %20
%25 = load i32, i32* @stderr
%26 = call i32 @fprintf(i32 %25, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0))
%27 = call i32 @exit(i32 1)
unreachable
28: ; preds = %20
%29 = load i32, i32* %7
%30 = sext i32 %29 to i64
%31 = mul i64 1, %30
%32 = trunc i64 %31 to i32
%33 = call i64 @malloc(i32 %32)
%34 = inttoptr i64 %33 to i8*
store i8* %34, i8** %9
store i32 0, i32* %8
br label %35
35: ; preds = %44, %28
%36 = load i32, i32* %8
%37 = icmp slt i32 %36, 16
br i1 %37, label %38, label %42
38: ; preds = %35
%39 = load i32, i32* %8
%40 = load i32, i32* %7
%41 = icmp slt i32 %39, %40
br label %42
42: ; preds = %38, %35
%43 = phi i1 [ false, %35 ], [ %41, %38 ]
br i1 %43, label %44, label %53
44: ; preds = %42
%45 = load i8*, i8** %9
%46 = load i32, i32* %7
%47 = load i32*, i32** %5
%48 = call i64 @fread(i8* %45, i32 1, i32 %46, i32* %47)
%49 = load i32, i32* %8
%50 = sext i32 %49 to i64
%51 = add nsw i64 %50, %48
%52 = trunc i64 %51 to i32
store i32 %52, i32* %8
br label %35
53: ; preds = %42
%54 = load i32*, i32** %5
%55 = call i32 @fclose(i32* %54)
br label %56
56: ; preds = %60, %53
%57 = load i32, i32* %8
%58 = load i32, i32* %7
%59 = icmp slt i32 %57, %58
br i1 %59, label %60, label %69
60: ; preds = %56
%61 = load i8*, i8** %9
%62 = load i32, i32* %7
%63 = load i32*, i32** %6
%64 = call i64 @fread(i8* %61, i32 1, i32 %62, i32* %63)
%65 = load i32, i32* %8
%66 = sext i32 %65 to i64
%67 = add nsw i64 %66, %64
%68 = trunc i64 %67 to i32
store i32 %68, i32* %8
br label %56
69: ; preds = %56
%70 = load i32*, i32** %6
%71 = call i32 @fclose(i32* %70)
%72 = load i32, i32* %3
%73 = load i32, i32* %7
%74 = load i8*, i8** %9
%75 = call i32 @mpz_import(i32 %72, i32 %73, i32 1, i32 1, i32 0, i32 0, i8* %74)
%76 = load i8*, i8** %9
%77 = call i32 @free(i8* %76)
ret i32 0
}

LLVM IR (-O2):
@.str = [12 x i8] c"/dev/random\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"
@.str.3 = [13 x i8] c"/dev/urandom\00"
define i32 @gen_random(i32 %0, i32 %1) {
%3 = add nsw i32 %1, 7
%4 = ashr i32 %3, 3
%5 = tail call i32* @fopen(i8* getelementptr inbounds ([12 x i8], [12 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 i32* @fopen(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %18
14: ; preds = %11
%15 = load i32, i32* @stderr
%16 = tail call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0))
%17 = tail call i32 @exit(i32 1)
unreachable
18: ; preds = %11
%19 = tail call i64 @malloc(i32 %4)
%20 = inttoptr i64 %19 to i8*
%21 = icmp sgt i32 %1, 0
br i1 %21, label %22, label %30
22: ; preds = %18, %22
%23 = phi i32 [ %26, %22 ], [ 0, %18 ]
%24 = tail call i64 @fread(i8* %20, i32 1, i32 %4, i32* %5)
%25 = trunc i64 %24 to i32
%26 = add i32 %23, %25
%27 = icmp slt i32 %26, 16
%28 = icmp slt i32 %26, %4
%29 = select i1 %27, i1 %28, i1 false
br i1 %29, label %22, label %30
30: ; preds = %22, %18
%31 = phi i32 [ 0, %18 ], [ %26, %22 ]
%32 = tail call i32 @fclose(i32* %5)
%33 = icmp slt i32 %31, %4
br i1 %33, label %34, label %40
34: ; preds = %30, %34
%35 = phi i32 [ %38, %34 ], [ %31, %30 ]
%36 = tail call i64 @fread(i8* %20, i32 1, i32 %4, i32* %12)
%37 = trunc i64 %36 to i32
%38 = add i32 %35, %37
%39 = icmp slt i32 %38, %4
br i1 %39, label %34, label %40
40: ; preds = %34, %30
%41 = tail call i32 @fclose(i32* %12)
%42 = tail call i32 @mpz_import(i32 %0, i32 %4, i32 1, i32 1, i32 0, i32 0, i8* %20)
%43 = tail call i32 @free(i8* %20)
ret i32 0
}