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

/* Variables and functions */
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fread(void *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(void *);
void *malloc(int);
int /*<<< orphan*/ mpz_import(int /*<<< orphan*/, int, int, int,
                              int /*<<< orphan*/, int /*<<< orphan*/, void *);
int mpz_sizeinbase(int /*<<< orphan*/, int);
void random_dev(mpz_t rop, int bit_len) {
  assert(bit_len > 0);
  int byte_size = bit_len / 8;
  void *buffer = malloc(byte_size);
  FILE *dev = fopen("/dev/urandom", "r");
  int read = fread(buffer, 1, byte_size, dev);
  while (read < byte_size) {
    read += fread(buffer + read, 1, byte_size - read, dev);
  }
  fclose(dev);
  mpz_import(rop, byte_size, 1, 1, 0, 0, buffer);
  free(buffer);
  assert(mpz_sizeinbase(rop, 2) <= bit_len);
}


x86 assembly (clang, -O0):
<random_dev>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
setg   %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
mov    -0x8(%rbp),%eax
mov    $0x8,%ecx
cltd
idiv   %ecx
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%edi
call   1080 <malloc@plt>
mov    %rax,-0x18(%rbp)
lea    0xe40(%rip),%rdi
lea    0xe46(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0xc(%rbp),%edx
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    121e <random_dev+0x9e>
mov    -0x18(%rbp),%rdi
movslq -0x24(%rbp),%rax
add    %rax,%rdi
mov    -0xc(%rbp),%edx
sub    -0x24(%rbp),%edx
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
add    -0x24(%rbp),%eax
mov    %eax,-0x24(%rbp)
jmp    11e8 <random_dev+0x68>
mov    -0x20(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x4(%rbp),%edi
mov    -0xc(%rbp),%esi
mov    -0x18(%rbp),%rax
mov    $0x1,%ecx
xor    %r9d,%r9d
mov    %ecx,%edx
mov    %r9d,%r8d
mov    %rax,(%rsp)
call   10a0 <mpz_import@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x4(%rbp),%edi
mov    $0x2,%esi
call   1070 <mpz_sizeinbase@plt>
cmp    -0x8(%rbp),%eax
setle  %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<random_dev>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %esi,%r15d
mov    %edi,%r14d
lea    0x7(%r15),%ebp
xor    %edi,%edi
test   %esi,%esi
setg   %dil
cmovns %esi,%ebp
call   1060 <assert@plt>
sar    $0x3,%ebp
mov    %ebp,%edi
call   1080 <malloc@plt>
mov    %rax,%r12
lea    0xe47(%rip),%rdi
lea    0xe4d(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,%r13
mov    %r12,%rdi
mov    $0x1,%esi
mov    %ebp,%edx
mov    %rax,%rcx
call   1040 <fread@plt>
cmp    %eax,%ebp
jle    11fe <random_dev+0x7e>
xchg   %ax,%ax
movslq %eax,%rbx
lea    (%r12,%rbx,1),%rdi
mov    %ebp,%edx
sub    %ebx,%edx
mov    $0x1,%esi
mov    %r13,%rcx
call   1040 <fread@plt>
add    %ebx,%eax
cmp    %eax,%ebp
jg     11e0 <random_dev+0x60>
mov    %r13,%rdi
call   1050 <fclose@plt>
mov    %r12,(%rsp)
mov    %r14d,%edi
mov    %ebp,%esi
mov    $0x1,%edx
mov    $0x1,%ecx
xor    %r8d,%r8d
xor    %r9d,%r9d
call   10a0 <mpz_import@plt>
mov    %r12,%rdi
call   1030 <free@plt>
mov    %r14d,%edi
mov    $0x2,%esi
call   1070 <mpz_sizeinbase@plt>
xor    %edi,%edi
cmp    %r15d,%eax
setle  %dil
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1060 <assert@plt>

LLVM IR (-O0):
@.str = [13 x i8] c"/dev/urandom\00"
@.str.1 = [2 x i8] c"r\00"
define void @random_dev(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32*
%8 = alloca i32
store i32 %0, i32* %3
store i32 %1, i32* %4
%9 = load i32, i32* %4
%10 = icmp sgt i32 %9, 0
%11 = zext i1 %10 to i32
%12 = call i32 @assert(i32 %11)
%13 = load i32, i32* %4
%14 = sdiv i32 %13, 8
store i32 %14, i32* %5
%15 = load i32, i32* %5
%16 = call i8* @malloc(i32 %15)
store i8* %16, i8** %6
%17 = 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* %17, i32** %7
%18 = load i8*, i8** %6
%19 = load i32, i32* %5
%20 = load i32*, i32** %7
%21 = call i32 @fread(i8* %18, i32 1, i32 %19, i32* %20)
store i32 %21, i32* %8
br label %22
22: ; preds = %26, %2
%23 = load i32, i32* %8
%24 = load i32, i32* %5
%25 = icmp slt i32 %23, %24
br i1 %25, label %26, label %38
26: ; preds = %22
%27 = load i8*, i8** %6
%28 = load i32, i32* %8
%29 = sext i32 %28 to i64
%30 = getelementptr i8, i8* %27, i64 %29
%31 = load i32, i32* %5
%32 = load i32, i32* %8
%33 = sub nsw i32 %31, %32
%34 = load i32*, i32** %7
%35 = call i32 @fread(i8* %30, i32 1, i32 %33, i32* %34)
%36 = load i32, i32* %8
%37 = add nsw i32 %36, %35
store i32 %37, i32* %8
br label %22
38: ; preds = %22
%39 = load i32*, i32** %7
%40 = call i32 @fclose(i32* %39)
%41 = load i32, i32* %3
%42 = load i32, i32* %5
%43 = load i8*, i8** %6
%44 = call i32 @mpz_import(i32 %41, i32 %42, i32 1, i32 1, i32 0, i32 0, i8* %43)
%45 = load i8*, i8** %6
%46 = call i32 @free(i8* %45)
%47 = load i32, i32* %3
%48 = call i32 @mpz_sizeinbase(i32 %47, i32 2)
%49 = load i32, i32* %4
%50 = icmp sle i32 %48, %49
%51 = zext i1 %50 to i32
%52 = call i32 @assert(i32 %51)
ret void
}

LLVM IR (-O2):
@.str = [13 x i8] c"/dev/urandom\00"
@.str.1 = [2 x i8] c"r\00"
define void @random_dev(i32 %0, i32 %1) {
%3 = icmp sgt i32 %1, 0
%4 = zext i1 %3 to i32
%5 = tail call i32 @assert(i32 %4)
%6 = sdiv i32 %1, 8
%7 = tail call i8* @malloc(i32 %6)
%8 = 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))
%9 = tail call i32 @fread(i8* %7, i32 1, i32 %6, i32* %8)
%10 = icmp sgt i32 %6, %9
br i1 %10, label %11, label %19
11: ; preds = %2, %11
%12 = phi i32 [ %17, %11 ], [ %9, %2 ]
%13 = sext i32 %12 to i64
%14 = getelementptr i8, i8* %7, i64 %13
%15 = sub nsw i32 %6, %12
%16 = tail call i32 @fread(i8* %14, i32 1, i32 %15, i32* %8)
%17 = add nsw i32 %16, %12
%18 = icmp sgt i32 %6, %17
br i1 %18, label %11, label %19
19: ; preds = %11, %2
%20 = tail call i32 @fclose(i32* %8)
%21 = tail call i32 @mpz_import(i32 %0, i32 %6, i32 1, i32 1, i32 0, i32 0, i8* %7)
%22 = tail call i32 @free(i8* %7)
%23 = tail call i32 @mpz_sizeinbase(i32 %0, i32 2)
%24 = icmp sle i32 %23, %1
%25 = zext i1 %24 to i32
%26 = tail call i32 @assert(i32 %25)
ret void
}