#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 */
double RAND_MAX;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ fread(unsigned char *, int, int, int /*<<< orphan*/ *);
scalar_t__ random();
int /*<<< orphan*/ srandom(int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int /*<<< orphan*/ time(int /*<<< orphan*/);
void get_secure_salt(unsigned char *salt, int length) {
  FILE *randfile = fopen("/dev/random", "rb");
  if (!randfile) {
    int i;
    fprintf(stderr, "error opening /dev/random: %s\n", strerror(errno));
    fprintf(stderr, "warning - using pseudorandom number generator for "
                    "salting! BAD IDEA.\n");
    srandom(time(0));
    for (i = 0; i < length; i++) {
      *(salt + i) = ((double)random() / RAND_MAX) * 0xFF;
    }
  } else {
    fread(salt, sizeof(unsigned char), length, randfile);
    fclose(randfile);
  }
}


x86 assembly (clang, -O0):
<get_secure_salt>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
lea    0xe72(%rip),%rdi
lea    0xe77(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1262 <get_secure_salt+0xe2>
mov    0x2e20(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2e0c(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x20(%rbp),%edi
mov    %rax,%rdx
lea    0xe3f(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    0x2df2(%rip),%rax
mov    (%rax),%edi
lea    0xe47(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
xor    %edi,%edi
call   1070 <time@plt>
mov    %eax,%edi
call   1030 <srandom@plt>
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    125d <get_secure_salt+0xdd>
xor    %eax,%eax
call   1080 <random@plt>
cvtsi2sd %rax,%xmm0
mov    0x2dbe(%rip),%rax
movsd  (%rax),%xmm1
divsd  %xmm1,%xmm0
movsd  0xdc6(%rip),%xmm1
mulsd  %xmm1,%xmm0
cvttsd2si %xmm0,%eax
mov    %al,%dl
mov    -0x8(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    120b <get_secure_salt+0x8b>
jmp    1280 <get_secure_salt+0x100>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%edx
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_secure_salt>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%r14d
mov    %rdi,%r15
lea    0xe74(%rip),%rdi
lea    0xe79(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     11cd <get_secure_salt+0x4d>
mov    %rax,%rbp
mov    %r15,%rdi
mov    $0x1,%esi
mov    %r14d,%edx
mov    %rax,%rcx
call   1040 <fread@plt>
mov    %rbp,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <fclose@plt>
mov    0x2e04(%rip),%rbx
mov    (%rbx),%ebp
mov    0x2df3(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xe2c(%rip),%rsi
mov    %ebp,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    (%rbx),%edi
lea    0xe36(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
xor    %edi,%edi
call   1070 <time@plt>
mov    %eax,%edi
call   1030 <srandom@plt>
test   %r14d,%r14d
jle    125d <get_secure_salt+0xdd>
mov    %r14d,%ebp
mov    0x2dc4(%rip),%r14
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
xor    %eax,%eax
call   1080 <random@plt>
xorps  %xmm0,%xmm0
cvtsi2sd %rax,%xmm0
divsd  (%r14),%xmm0
mulsd  0xdb4(%rip),%xmm0
cvttsd2si %xmm0,%eax
mov    %al,(%r15,%rbx,1)
add    $0x1,%rbx
cmp    %rbx,%rbp
jne    1230 <get_secure_salt+0xb0>
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [12 x i8] c"/dev/random\00"
@.str.1 = [3 x i8] c"rb\00"
@stderr = global i32 0
@.str.2 = [31 x i8] c"error opening /dev/random: %s\0A\00"
@errno = global i32 0
@.str.3 = [70 x i8] c"warning - using pseudorandom number generator for salting! BAD IDEA.\0A\00"
@RAND_MAX = global double 0.000000e+00
define void @get_secure_salt(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i32
store i8* %0, i8** %3
store i32 %1, i32* %4
%7 = call i32* @fopen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %7, i32** %5
%8 = load i32*, i32** %5
%9 = icmp ne i32* %8, null
br i1 %9, label %38, label %10
10: ; preds = %2
%11 = load i32, i32* @stderr
%12 = load i32, i32* @errno
%13 = call i8* @strerror(i32 %12)
%14 = call i32 (i32, i8*, ...) @fprintf(i32 %11, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i8* %13)
%15 = load i32, i32* @stderr
%16 = call i32 (i32, i8*, ...) @fprintf(i32 %15, i8* getelementptr inbounds ([70 x i8], [70 x i8]* @.str.3, i64 0, i64 0))
%17 = call i32 @time(i32 0)
%18 = call i32 @srandom(i32 %17)
store i32 0, i32* %6
br label %19
19: ; preds = %34, %10
%20 = load i32, i32* %6
%21 = load i32, i32* %4
%22 = icmp slt i32 %20, %21
br i1 %22, label %23, label %37
23: ; preds = %19
%24 = call i64 (...) @random()
%25 = sitofp i64 %24 to double
%26 = load double, double* @RAND_MAX
%27 = fdiv double %25, %26
%28 = fmul double %27, 2.550000e+02
%29 = fptoui double %28 to i8
%30 = load i8*, i8** %3
%31 = load i32, i32* %6
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds i8, i8* %30, i64 %32
store i8 %29, i8* %33
br label %34
34: ; preds = %23
%35 = load i32, i32* %6
%36 = add nsw i32 %35, 1
store i32 %36, i32* %6
br label %19
37: ; preds = %19
br label %45
38: ; preds = %2
%39 = load i8*, i8** %3
%40 = load i32, i32* %4
%41 = load i32*, i32** %5
%42 = call i32 @fread(i8* %39, i32 1, i32 %40, i32* %41)
%43 = load i32*, i32** %5
%44 = call i32 @fclose(i32* %43)
br label %45
45: ; preds = %38, %37
ret void
}

LLVM IR (-O2):
@.str = [12 x i8] c"/dev/random\00"
@.str.1 = [3 x i8] c"rb\00"
@stderr = global i32 0
@.str.2 = [31 x i8] c"error opening /dev/random: %s\0A\00"
@errno = global i32 0
@.str.3 = [70 x i8] c"warning - using pseudorandom number generator for salting! BAD IDEA.\0A\00"
@RAND_MAX = global double 0.000000e+00
define void @get_secure_salt(i8* nocapture %0, i32 %1) {
%3 = tail call i32* @fopen(i8* getelementptr inbounds ([12 x i8], [12 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 %5, label %28
5: ; preds = %2
%6 = load i32, i32* @stderr
%7 = load i32, i32* @errno
%8 = tail call i8* @strerror(i32 %7)
%9 = tail call i32 (i32, i8*, ...) @fprintf(i32 %6, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i8* %8)
%10 = load i32, i32* @stderr
%11 = tail call i32 (i32, i8*, ...) @fprintf(i32 %10, i8* getelementptr inbounds ([70 x i8], [70 x i8]* @.str.3, i64 0, i64 0))
%12 = tail call i32 @time(i32 0)
%13 = tail call i32 @srandom(i32 %12)
%14 = icmp sgt i32 %1, 0
br i1 %14, label %15, label %31
15: ; preds = %5
%16 = zext i32 %1 to i64
br label %17
17: ; preds = %15, %17
%18 = phi i64 [ 0, %15 ], [ %26, %17 ]
%19 = tail call i64 (...) @random()
%20 = sitofp i64 %19 to double
%21 = load double, double* @RAND_MAX
%22 = fdiv double %20, %21
%23 = fmul double %22, 2.550000e+02
%24 = fptoui double %23 to i8
%25 = getelementptr inbounds i8, i8* %0, i64 %18
store i8 %24, i8* %25
%26 = add nuw nsw i64 %18, 1
%27 = icmp eq i64 %26, %16
br i1 %27, label %31, label %17
28: ; preds = %2
%29 = tail call i32 @fread(i8* %0, i32 1, i32 %1, i32* %3)
%30 = tail call i32 @fclose(i32* %3)
br label %31
31: ; preds = %17, %5, %28
ret void
}