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

/* Variables and functions */
scalar_t__ RAND_MAX;
scalar_t__ rand();
int /*<<< orphan*/ srand(unsigned int);
intptr_t time(int /*<<< orphan*/ *);
void zrand_libc_rand(void *out, size_t n, void *statep) {
  static char inited = 0;
  unsigned int ri;
  double rd;
  unsigned char *buf = out;
  if (!inited) {
    inited = 1;
    srand((unsigned)((intptr_t)out | time(NULL)));
  }
  while (n--) {
    ri = (unsigned)rand();
    rd = (double)ri / ((double)RAND_MAX + 1);
    rd *= 256;
    buf[n] = (unsigned char)rd;
  }
  (void)statep;
}


x86 assembly (clang, -O0):
<zrand_libc_rand>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x30(%rbp)
cmpb   $0x0,0x2eed(%rip)
jne    1182 <zrand_libc_rand+0x52>
movb   $0x1,0x2ee0(%rip)
mov    -0x8(%rbp),%rax
mov    %rax,-0x38(%rbp)
xor    %eax,%eax
mov    %eax,%edi
call   1040 <time@plt>
mov    %rax,%rcx
mov    -0x38(%rbp),%rax
or     %rcx,%rax
mov    %eax,%edi
call   1030 <srand@plt>
jmp    1187 <zrand_libc_rand+0x57>
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0xffffffffffffffff,%rcx
mov    %rcx,-0x10(%rbp)
cmp    $0x0,%rax
je     1200 <zrand_libc_rand+0xd0>
xor    %eax,%eax
call   1050 <rand@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cvtsi2sd %rax,%xmm0
mov    0x2e2f(%rip),%rax
cvtsi2sdq (%rax),%xmm1
movsd  0xe3a(%rip),%xmm2
addsd  %xmm2,%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x28(%rbp)
movsd  -0x28(%rbp),%xmm0
movsd  0xe28(%rip),%xmm1
mulsd  %xmm1,%xmm0
movsd  %xmm0,-0x28(%rbp)
cvttsd2si -0x28(%rbp),%eax
mov    %al,%dl
mov    -0x30(%rbp),%rax
mov    -0x10(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
jmp    1187 <zrand_libc_rand+0x57>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<zrand_libc_rand>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%rbx
mov    %rdi,%r14
cmpb   $0x0,0x2efe(%rip)
jne    115c <zrand_libc_rand+0x2c>
movb   $0x1,0x2ef5(%rip)
xor    %edi,%edi
call   1040 <time@plt>
mov    %r14d,%edi
or     %eax,%edi
call   1030 <srand@plt>
test   %rbx,%rbx
je     11ac <zrand_libc_rand+0x7c>
mov    0x2e80(%rip),%r15
nopl   0x0(%rax,%rax,1)
xor    %eax,%eax
call   1050 <rand@plt>
mov    %eax,%eax
xorps  %xmm0,%xmm0
cvtsi2sdq (%r15),%xmm0
xorps  %xmm1,%xmm1
cvtsi2sd %rax,%xmm1
addsd  0xe6f(%rip),%xmm0
divsd  %xmm0,%xmm1
mulsd  0xe6b(%rip),%xmm1
cvttsd2si %xmm1,%eax
mov    %al,-0x1(%r14,%rbx,1)
add    $0xffffffffffffffff,%rbx
jne    1170 <zrand_libc_rand+0x40>
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@zrand_libc_rand.inited = internal global i8 0
@RAND_MAX = global i64 0
define void @zrand_libc_rand(i8* %0, i64 %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i8*
%7 = alloca i32
%8 = alloca double
%9 = alloca i8*
store i8* %0, i8** %4
store i64 %1, i64* %5
store i8* %2, i8** %6
%10 = load i8*, i8** %4
store i8* %10, i8** %9
%11 = load i8, i8* @zrand_libc_rand.inited
%12 = icmp ne i8 %11, 0
br i1 %12, label %20, label %13
13: ; preds = %3
store i8 1, i8* @zrand_libc_rand.inited
%14 = load i8*, i8** %4
%15 = ptrtoint i8* %14 to i64
%16 = call i64 @time(i32* null)
%17 = or i64 %15, %16
%18 = trunc i64 %17 to i32
%19 = call i32 @srand(i32 %18)
br label %20
20: ; preds = %13, %3
br label %21
21: ; preds = %25, %20
%22 = load i64, i64* %5
%23 = add i64 %22, -1
store i64 %23, i64* %5
%24 = icmp ne i64 %22, 0
br i1 %24, label %25, label %41
25: ; preds = %21
%26 = call i64 (...) @rand()
%27 = trunc i64 %26 to i32
store i32 %27, i32* %7
%28 = load i32, i32* %7
%29 = uitofp i32 %28 to double
%30 = load i64, i64* @RAND_MAX
%31 = sitofp i64 %30 to double
%32 = fadd double %31, 1.000000e+00
%33 = fdiv double %29, %32
store double %33, double* %8
%34 = load double, double* %8
%35 = fmul double %34, 2.560000e+02
store double %35, double* %8
%36 = load double, double* %8
%37 = fptoui double %36 to i8
%38 = load i8*, i8** %9
%39 = load i64, i64* %5
%40 = getelementptr inbounds i8, i8* %38, i64 %39
store i8 %37, i8* %40
br label %21
41: ; preds = %21
%42 = load i8*, i8** %6
ret void
}

LLVM IR (-O2):
@zrand_libc_rand.inited = internal unnamed_addr global i1 false
@RAND_MAX = global i64 0
define void @zrand_libc_rand(i8* %0, i64 %1, i8* nocapture readnone %2) {
%4 = load i1, i1* @zrand_libc_rand.inited
br i1 %4, label %11, label %5
5: ; preds = %3
store i1 true, i1* @zrand_libc_rand.inited
%6 = ptrtoint i8* %0 to i64
%7 = tail call i64 @time(i32* null)
%8 = or i64 %7, %6
%9 = trunc i64 %8 to i32
%10 = tail call i32 @srand(i32 %9)
br label %11
11: ; preds = %5, %3
%12 = icmp eq i64 %1, 0
br i1 %12, label %27, label %13
13: ; preds = %11, %13
%14 = phi i64 [ %15, %13 ], [ %1, %11 ]
%15 = add i64 %14, -1
%16 = tail call i64 (...) @rand()
%17 = trunc i64 %16 to i32
%18 = uitofp i32 %17 to double
%19 = load i64, i64* @RAND_MAX
%20 = sitofp i64 %19 to double
%21 = fadd double %20, 1.000000e+00
%22 = fdiv double %18, %21
%23 = fmul double %22, 2.560000e+02
%24 = fptoui double %23 to i8
%25 = getelementptr inbounds i8, i8* %0, i64 %15
store i8 %24, i8* %25
%26 = icmp eq i64 %15, 0
br i1 %26, label %27, label %13
27: ; preds = %13, %11
ret void
}