#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 */
long random();
int /*<<< orphan*/ srandom(unsigned int);
intptr_t time(int /*<<< orphan*/ *);
void zrand_libc_random(void *out, size_t n, void *statep) {
  static char inited = 0;
  long int ri;
  unsigned char *buf = out;
  if (!inited) {
    inited = 1;
    srandom((unsigned)((intptr_t)out | time(NULL)));
  }
  while (n--) {
    ri = random();
    buf[n] = (unsigned char)((ri >> 0) & 255);
    if (!n--)
      break;
    buf[n] = (unsigned char)((ri >> 8) & 255);
    if (!n--)
      break;
    buf[n] = (unsigned char)((ri >> 16) & 255);
  }
  (void)statep;
}


x86 assembly (clang, -O0):
<zrand_libc_random>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x28(%rbp)
cmpb   $0x0,0x2ee6(%rip)
jne    1182 <zrand_libc_random+0x52>
movb   $0x1,0x2ed9(%rip)
mov    -0x8(%rbp),%rax
mov    %rax,-0x30(%rbp)
xor    %eax,%eax
mov    %eax,%edi
call   1040 <time@plt>
mov    %rax,%rcx
mov    -0x30(%rbp),%rax
or     %rcx,%rax
mov    %eax,%edi
call   1030 <srandom@plt>
jmp    1187 <zrand_libc_random+0x57>
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0xffffffffffffffff,%rcx
mov    %rcx,-0x10(%rbp)
cmp    $0x0,%rax
je     123d <zrand_libc_random+0x10d>
mov    $0x0,%al
call   1050 <random@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
sar    $0x0,%rax
and    $0xff,%rax
mov    %al,%dl
mov    -0x28(%rbp),%rax
mov    -0x10(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0xffffffffffffffff,%rcx
mov    %rcx,-0x10(%rbp)
cmp    $0x0,%rax
jne    11e4 <zrand_libc_random+0xb4>
jmp    123d <zrand_libc_random+0x10d>
mov    -0x20(%rbp),%rax
sar    $0x8,%rax
and    $0xff,%rax
mov    %al,%dl
mov    -0x28(%rbp),%rax
mov    -0x10(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0xffffffffffffffff,%rcx
mov    %rcx,-0x10(%rbp)
cmp    $0x0,%rax
jne    121d <zrand_libc_random+0xed>
jmp    123d <zrand_libc_random+0x10d>
mov    -0x20(%rbp),%rax
sar    $0x10,%rax
and    $0xff,%rax
mov    %al,%dl
mov    -0x28(%rbp),%rax
mov    -0x10(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
jmp    1187 <zrand_libc_random+0x57>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<zrand_libc_random>:
push   %rbp
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %rdi,%rbp
cmpb   $0x0,0x2ef9(%rip)
jne    1187 <zrand_libc_random+0x57>
movb   $0x1,0x2ef0(%rip)
xor    %edi,%edi
call   1040 <time@plt>
mov    %ebp,%edi
or     %eax,%edi
call   1030 <srandom@plt>
jmp    1187 <zrand_libc_random+0x57>
nopl   0x0(%rax,%rax,1)
xor    %eax,%eax
call   1050 <random@plt>
mov    %al,-0x1(%rbp,%rbx,1)
cmp    $0x1,%rbx
je     118c <zrand_libc_random+0x5c>
mov    %ah,-0x2(%rbp,%rbx,1)
cmp    $0x2,%rbx
je     118c <zrand_libc_random+0x5c>
shr    $0x10,%rax
mov    %al,-0x3(%rbp,%rbx,1)
add    $0xfffffffffffffffd,%rbx
test   %rbx,%rbx
jne    1160 <zrand_libc_random+0x30>
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@zrand_libc_random.inited = internal global i8 0
define void @zrand_libc_random(i8* %0, i64 %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i8*
store i8* %0, i8** %4
store i64 %1, i64* %5
store i8* %2, i8** %6
%9 = load i8*, i8** %4
store i8* %9, i8** %8
%10 = load i8, i8* @zrand_libc_random.inited
%11 = icmp ne i8 %10, 0
br i1 %11, label %19, label %12
12: ; preds = %3
store i8 1, i8* @zrand_libc_random.inited
%13 = load i8*, i8** %4
%14 = ptrtoint i8* %13 to i64
%15 = call i64 @time(i32* null)
%16 = or i64 %14, %15
%17 = trunc i64 %16 to i32
%18 = call i32 @srandom(i32 %17)
br label %19
19: ; preds = %12, %3
br label %20
20: ; preds = %49, %19
%21 = load i64, i64* %5
%22 = add i64 %21, -1
store i64 %22, i64* %5
%23 = icmp ne i64 %21, 0
br i1 %23, label %24, label %57
24: ; preds = %20
%25 = call i64 (...) @random()
store i64 %25, i64* %7
%26 = load i64, i64* %7
%27 = ashr i64 %26, 0
%28 = and i64 %27, 255
%29 = trunc i64 %28 to i8
%30 = load i8*, i8** %8
%31 = load i64, i64* %5
%32 = getelementptr inbounds i8, i8* %30, i64 %31
store i8 %29, i8* %32
%33 = load i64, i64* %5
%34 = add i64 %33, -1
store i64 %34, i64* %5
%35 = icmp ne i64 %33, 0
br i1 %35, label %37, label %36
36: ; preds = %24
br label %57
37: ; preds = %24
%38 = load i64, i64* %7
%39 = ashr i64 %38, 8
%40 = and i64 %39, 255
%41 = trunc i64 %40 to i8
%42 = load i8*, i8** %8
%43 = load i64, i64* %5
%44 = getelementptr inbounds i8, i8* %42, i64 %43
store i8 %41, i8* %44
%45 = load i64, i64* %5
%46 = add i64 %45, -1
store i64 %46, i64* %5
%47 = icmp ne i64 %45, 0
br i1 %47, label %49, label %48
48: ; preds = %37
br label %57
49: ; preds = %37
%50 = load i64, i64* %7
%51 = ashr i64 %50, 16
%52 = and i64 %51, 255
%53 = trunc i64 %52 to i8
%54 = load i8*, i8** %8
%55 = load i64, i64* %5
%56 = getelementptr inbounds i8, i8* %54, i64 %55
store i8 %53, i8* %56
br label %20
57: ; preds = %48, %36, %20
%58 = load i8*, i8** %6
ret void
}

LLVM IR (-O2):
@zrand_libc_random.inited = internal unnamed_addr global i1 false
define void @zrand_libc_random(i8* %0, i64 %1, i8* nocapture readnone %2) {
%4 = load i1, i1* @zrand_libc_random.inited
br i1 %4, label %11, label %5
5: ; preds = %3
store i1 true, i1* @zrand_libc_random.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 @srandom(i32 %9)
br label %11
11: ; preds = %5, %3
%12 = icmp eq i64 %1, 0
br i1 %12, label %32, label %13
13: ; preds = %11, %27
%14 = phi i64 [ %25, %27 ], [ %1, %11 ]
%15 = add i64 %14, -1
%16 = tail call i64 (...) @random()
%17 = trunc i64 %16 to i8
%18 = getelementptr inbounds i8, i8* %0, i64 %15
store i8 %17, i8* %18
%19 = add i64 %14, -2
%20 = icmp eq i64 %15, 0
br i1 %20, label %32, label %21
21: ; preds = %13
%22 = lshr i64 %16, 8
%23 = trunc i64 %22 to i8
%24 = getelementptr inbounds i8, i8* %0, i64 %19
store i8 %23, i8* %24
%25 = add i64 %14, -3
%26 = icmp eq i64 %19, 0
br i1 %26, label %32, label %27
27: ; preds = %21
%28 = lshr i64 %16, 16
%29 = trunc i64 %28 to i8
%30 = getelementptr inbounds i8, i8* %0, i64 %25
store i8 %29, i8* %30
%31 = icmp eq i64 %25, 0
br i1 %31, label %32, label %13
32: ; preds = %27, %13, %21, %11
ret void
}