#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*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
size_t fread(char *, int, int, int /*<<< orphan*/ *);
scalar_t__ malloc(int);
scalar_t__ rand();
int /*<<< orphan*/ srand(int /*<<< orphan*/);
int /*<<< orphan*/ time(int /*<<< orphan*/ *);
char *sidh_get_random_str(int num_bytes) {
  char *rand_value = (char *)malloc(num_bytes * sizeof(char));
  FILE *random_dev;
  random_dev = fopen("/dev/urandom", "r");
  if (random_dev) {
    size_t size = fread(rand_value, 1, num_bytes * sizeof(char), random_dev);
    fclose(random_dev);
    if (size != 0) {
      return rand_value;
    }
  }
  srand(time(NULL));
  for (int i = 0; i < num_bytes; i++)
    rand_value[i] = (char)rand();
  return rand_value;
}


x86 assembly (clang, -O0):
<sidh_get_random_str>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0xc(%rbp)
movslq -0xc(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1070 <malloc@plt>
mov    %rax,-0x18(%rbp)
lea    0xe6b(%rip),%rdi
lea    0xe71(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     11f6 <sidh_get_random_str+0x86>
mov    -0x18(%rbp),%rdi
movslq -0xc(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edx
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
cmpq   $0x0,-0x28(%rbp)
je     11f1 <sidh_get_random_str+0x81>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1243 <sidh_get_random_str+0xd3>
jmp    11f6 <sidh_get_random_str+0x86>
xor    %eax,%eax
mov    %eax,%edi
call   1060 <time@plt>
mov    %eax,%edi
call   1050 <srand@plt>
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    123b <sidh_get_random_str+0xcb>
mov    $0x0,%al
call   1090 <rand@plt>
mov    %al,%dl
mov    -0x18(%rbp),%rax
movslq -0x2c(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    120d <sidh_get_random_str+0x9d>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sidh_get_random_str>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %edi,%r14d
call   1070 <malloc@plt>
mov    %rax,%r12
lea    0xe76(%rip),%rdi
lea    0xe7c(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     11c1 <sidh_get_random_str+0x51>
mov    %rax,%rbp
mov    %r12,%rdi
mov    $0x1,%esi
mov    %r14d,%edx
mov    %rax,%rcx
call   1030 <fread@plt>
mov    %rax,%r15
mov    %rbp,%rdi
call   1040 <fclose@plt>
test   %r15,%r15
jne    11f4 <sidh_get_random_str+0x84>
xor    %edi,%edi
call   1060 <time@plt>
mov    %eax,%edi
call   1050 <srand@plt>
test   %r14d,%r14d
jle    11f4 <sidh_get_random_str+0x84>
mov    %r14d,%ebp
xor    %ebx,%ebx
nopl   0x0(%rax)
xor    %eax,%eax
call   1090 <rand@plt>
mov    %al,(%r12,%rbx,1)
add    $0x1,%rbx
cmp    %rbx,%rbp
jne    11e0 <sidh_get_random_str+0x70>
mov    %r12,%rax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [13 x i8] c"/dev/urandom\00"
@.str.1 = [2 x i8] c"r\00"
define i8* @sidh_get_random_str(i32 %0) {
%2 = tail call i64 @malloc(i32 %0)
%3 = inttoptr 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 ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %10, label %6
6: ; preds = %1
%7 = tail call i64 @fread(i8* %3, i32 1, i32 %0, i32* %4)
%8 = tail call i32 @fclose(i32* %4)
%9 = icmp eq i64 %7, 0
br i1 %9, label %10, label %23
10: ; preds = %6, %1
%11 = tail call i32 @time(i32* null)
%12 = tail call i32 @srand(i32 %11)
%13 = icmp sgt i32 %0, 0
br i1 %13, label %14, label %23
14: ; preds = %10
%15 = zext i32 %0 to i64
br label %16
16: ; preds = %14, %16
%17 = phi i64 [ 0, %14 ], [ %21, %16 ]
%18 = tail call i64 (...) @rand()
%19 = trunc i64 %18 to i8
%20 = getelementptr inbounds i8, i8* %3, i64 %17
store i8 %19, i8* %20
%21 = add nuw nsw i64 %17, 1
%22 = icmp eq i64 %21, %15
br i1 %22, label %23, label %16
23: ; preds = %16, %10, %6
ret i8* %3
}