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

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fread(unsigned char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ mpz_import(int /*<<< orphan*/, int const, int, int,
                              int /*<<< orphan*/, int /*<<< orphan*/,
                              unsigned char *);
int mpz_seed(mpz_t seed, int bits) {
  FILE *fd = fopen("/dev/urandom", "rb");
  if (!fd)
    return 0x1;
  const int bytes = (bits / 8) + 1;
  unsigned char random_bytes[bytes];
  if (fread(random_bytes, sizeof(random_bytes), 1, fd) != 1)
    return 0x2;
  mpz_import(seed, bytes, 1, sizeof(random_bytes[0]), 0, 0, random_bytes);
  fclose(fd);
  return 0x0;
}


x86 assembly (clang, -O0):
<mpz_seed>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
lea    0xeab(%rip),%rdi
lea    0xeb1(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    117c <mpz_seed+0x3c>
movl   $0x1,-0x4(%rbp)
jmp    122a <mpz_seed+0xea>
mov    -0xc(%rbp),%eax
mov    %eax,%ecx
add    $0x7,%ecx
test   %eax,%eax
cmovs  %ecx,%eax
sar    $0x3,%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    %rsp,%rcx
mov    %rcx,-0x28(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x40(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x30(%rbp)
mov    %eax,%esi
mov    -0x18(%rbp),%rcx
mov    $0x1,%edx
call   1030 <fread@plt>
cmp    $0x1,%eax
je     11e4 <mpz_seed+0xa4>
movl   $0x2,-0x4(%rbp)
movl   $0x1,-0x34(%rbp)
jmp    1223 <mpz_seed+0xe3>
mov    -0x40(%rbp),%rax
mov    -0x8(%rbp),%edi
mov    -0x1c(%rbp),%esi
mov    $0x1,%ecx
xor    %r9d,%r9d
sub    $0x10,%rsp
mov    %ecx,%edx
mov    %r9d,%r8d
mov    %rax,(%rsp)
call   1060 <mpz_import@plt>
add    $0x10,%rsp
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x34(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mpz_seed>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %esi,%r12d
mov    %edi,%ebx
lea    0xea6(%rip),%rdi
lea    0xeac(%rip),%rsi
call   1050 <fopen@plt>
test   %rax,%rax
je     11e8 <mpz_seed+0xa8>
mov    %rax,%r15
mov    %ebx,-0x2c(%rbp)
mov    %rsp,%r14
lea    0x7(%r12),%ebx
test   %r12d,%r12d
cmovns %r12d,%ebx
sar    $0x3,%ebx
add    $0x1,%ebx
lea    0xf(%rbx),%rax
and    $0xfffffffffffffff0,%rax
mov    %rsp,%r13
sub    %rax,%r13
mov    %r13,%rsp
mov    %r13,%rdi
mov    %ebx,%esi
mov    $0x1,%edx
mov    %r15,%rcx
call   1030 <fread@plt>
mov    $0x2,%r12d
cmp    $0x1,%eax
jne    11e3 <mpz_seed+0xa3>
sub    $0x8,%rsp
xor    %r12d,%r12d
mov    -0x2c(%rbp),%edi
mov    %ebx,%esi
mov    $0x1,%edx
mov    $0x1,%ecx
xor    %r8d,%r8d
xor    %r9d,%r9d
push   %r13
call   1060 <mpz_import@plt>
add    $0x10,%rsp
mov    %r15,%rdi
call   1040 <fclose@plt>
mov    %r14,%rsp
jmp    11ee <mpz_seed+0xae>
mov    $0x1,%r12d
mov    %r12d,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [13 x i8] c"/dev/urandom\00"
@.str.1 = [3 x i8] c"rb\00"
define i32 @mpz_seed(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
%11 = 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* %11, i32** %6
%12 = load i32*, i32** %6
%13 = icmp ne i32* %12, null
br i1 %13, label %15, label %14
14: ; preds = %2
store i32 1, i32* %3
br label %36
15: ; preds = %2
%16 = load i32, i32* %5
%17 = sdiv i32 %16, 8
%18 = add nsw i32 %17, 1
store i32 %18, i32* %7
%19 = load i32, i32* %7
%20 = zext i32 %19 to i64
%21 = call i8* @llvm.stacksave()
store i8* %21, i8** %8
%22 = alloca i8, i64 %20
store i64 %20, i64* %9
%23 = trunc i64 %20 to i32
%24 = load i32*, i32** %6
%25 = call i32 @fread(i8* %22, i32 %23, i32 1, i32* %24)
%26 = icmp ne i32 %25, 1
br i1 %26, label %27, label %28
27: ; preds = %15
store i32 2, i32* %3
store i32 1, i32* %10
br label %34
28: ; preds = %15
%29 = load i32, i32* %4
%30 = load i32, i32* %7
%31 = call i32 @mpz_import(i32 %29, i32 %30, i32 1, i32 1, i32 0, i32 0, i8* %22)
%32 = load i32*, i32** %6
%33 = call i32 @fclose(i32* %32)
store i32 0, i32* %3
store i32 1, i32* %10
br label %34
34: ; preds = %28, %27
%35 = load i8*, i8** %8
br label %36
36: ; preds = %34, %14
%37 = load i32, i32* %3
ret i32 %37
}

LLVM IR (-O2):
@.str = [13 x i8] c"/dev/urandom\00"
@.str.1 = [3 x i8] c"rb\00"
define i32 @mpz_seed(i32 %0, i32 %1) {
%3 = 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))
%4 = icmp eq i32* %3, null
br i1 %4, label %18, label %5
5: ; preds = %2
%6 = sdiv i32 %1, 8
%7 = add nsw i32 %6, 1
%8 = zext i32 %7 to i64
%9 = tail call i8* @llvm.stacksave()
%10 = alloca i8, i64 %8
%11 = call i32 @fread(i8* %10, i32 %7, i32 1, i32* %3)
%12 = icmp eq i32 %11, 1
br i1 %12, label %13, label %16
13: ; preds = %5
%14 = call i32 @mpz_import(i32 %0, i32 %7, i32 1, i32 1, i32 0, i32 0, i8* %10)
%15 = call i32 @fclose(i32* %3)
br label %16
16: ; preds = %5, %13
%17 = phi i32 [ 0, %13 ], [ 2, %5 ]
br label %18
18: ; preds = %2, %16
%19 = phi i32 [ %17, %16 ], [ 1, %2 ]
ret i32 %19
}