#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 */
int GEN_rand(unsigned int *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, unsigned int, double);
int /*<<< orphan*/ stderr;
void generate(void *buffer, size_t buffSize, double p) {
  char table[4096] = {0};
  int remaining = 4096;
  unsigned pos = 0;
  unsigned s = 0;
  char *op = (char *)buffer;
  char *oend = op + buffSize;
  unsigned seed = 1;
  if (p == 0.0)
    p = 0.005;
  fprintf(stderr, "Generating %u KB with P=%.2f%%\n",
          (unsigned)(buffSize >> 10), p * 100);
  while (remaining) {
    unsigned n = (unsigned)(remaining * p);
    unsigned end;
    if (!n)
      n = 1;
    end = pos + n;
    while (pos < end)
      table[pos++] = (char)s;
    s++;
    remaining -= n;
  }
  while (op < oend) {
    const unsigned r = GEN_rand(&seed) & (4096 - 1);
    *op++ = table[r];
  }
}


x86 assembly (clang, -O0):
<generate>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1050,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movsd  %xmm0,-0x18(%rbp)
lea    -0x1020(%rbp),%rdi
xor    %esi,%esi
mov    $0x1000,%edx
call   1030 <memset@plt>
movl   $0x1000,-0x1024(%rbp)
movl   $0x0,-0x1028(%rbp)
movl   $0x0,-0x102c(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x1038(%rbp)
mov    -0x1038(%rbp),%rax
add    -0x10(%rbp),%rax
mov    %rax,-0x1040(%rbp)
movl   $0x1,-0x1044(%rbp)
movsd  -0x18(%rbp),%xmm0
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jne    11c5 <generate+0x95>
jp     11c5 <generate+0x95>
movsd  0xe40(%rip),%xmm0
movsd  %xmm0,-0x18(%rbp)
mov    0x2e14(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
shr    $0xa,%rax
mov    %eax,%edx
movsd  0xe28(%rip),%xmm0
mulsd  -0x18(%rbp),%xmm0
lea    0xe24(%rip),%rsi
call   1040 <fprintf@plt>
cmpl   $0x0,-0x1024(%rbp)
je     12a4 <generate+0x174>
cvtsi2sdl -0x1024(%rbp),%xmm0
ff
movsd  -0x18(%rbp),%xmm1
mulsd  %xmm1,%xmm0
cvttsd2si %xmm0,%rax
mov    %eax,-0x1048(%rbp)
cmpl   $0x0,-0x1048(%rbp)
jne    1231 <generate+0x101>
movl   $0x1,-0x1048(%rbp)
mov    -0x1028(%rbp),%eax
add    -0x1048(%rbp),%eax
mov    %eax,-0x104c(%rbp)
mov    -0x1028(%rbp),%eax
cmp    -0x104c(%rbp),%eax
jae    127c <generate+0x14c>
mov    -0x102c(%rbp),%eax
mov    %al,%cl
mov    -0x1028(%rbp),%eax
mov    %eax,%edx
add    $0x1,%edx
mov    %edx,-0x1028(%rbp)
mov    %eax,%eax
mov    %cl,-0x1020(%rbp,%rax,1)
jmp    1243 <generate+0x113>
mov    -0x102c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x102c(%rbp)
mov    -0x1048(%rbp),%ecx
mov    -0x1024(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x1024(%rbp)
jmp    11f1 <generate+0xc1>
jmp    12a9 <generate+0x179>
mov    -0x1038(%rbp),%rax
cmp    -0x1040(%rbp),%rax
jae    12fd <generate+0x1cd>
lea    -0x1044(%rbp),%rdi
call   1050 <GEN_rand@plt>
and    $0xfff,%eax
mov    %eax,-0x1050(%rbp)
mov    -0x1050(%rbp),%eax
mov    -0x1020(%rbp,%rax,1),%cl
mov    -0x1038(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x1038(%rbp)
mov    %cl,(%rax)
jmp    12a9 <generate+0x179>
add    $0x1050,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<generate>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1038,%rsp
movaps %xmm0,0x10(%rsp)
mov    %rsi,%rbx
mov    %rdi,%r12
lea    0x30(%rsp),%rdi
xor    %r13d,%r13d
mov    $0x1000,%edx
xor    %esi,%esi
call   1030 <memset@plt>
movl   $0x1,0xc(%rsp)
xorpd  %xmm1,%xmm1
movapd 0x10(%rsp),%xmm2
cmpeqsd %xmm2,%xmm1
movsd  0xe81(%rip),%xmm0
andpd  %xmm1,%xmm0
andnpd %xmm2,%xmm1
orpd   %xmm0,%xmm1
mov    0x2e4e(%rip),%rax
mov    (%rax),%edi
mov    %rbx,0x28(%rsp)
shr    $0xa,%rbx
movapd %xmm1,0x10(%rsp)
movapd %xmm1,%xmm0
mulsd  0xe59(%rip),%xmm0
lea    0xe5a(%rip),%rsi
mov    %ebx,%edx
call   1040 <fprintf@plt>
mov    $0x1000,%ebx
mov    $0x1,%r15d
xor    %eax,%eax
jmp    11f0 <generate+0xc0>
nopl   0x0(%rax)
mov    %eax,%eax
lea    (%rsp,%rax,1),%rdi
add    $0x30,%rdi
mov    %ebp,%edx
movzbl %r13b,%esi
call   1030 <memset@plt>
add    $0x1,%r13b
mov    %r14d,%eax
sub    %ebp,%ebx
je     1216 <generate+0xe6>
xorps  %xmm0,%xmm0
cvtsi2sd %ebx,%xmm0
mulsd  0x10(%rsp),%xmm0
cvttsd2si %xmm0,%rbp
test   %ebp,%ebp
cmove  %r15d,%ebp
lea    (%rax,%rbp,1),%r14d
cmp    %r14d,%eax
jb     11d0 <generate+0xa0>
mov    %eax,%r14d
jmp    11e5 <generate+0xb5>
mov    0x28(%rsp),%rbx
test   %rbx,%rbx
jle    124f <generate+0x11f>
add    %r12,%rbx
lea    0xc(%rsp),%r15
nopl   0x0(%rax,%rax,1)
mov    %r15,%rdi
call   1050 <GEN_rand@plt>
and    $0xfff,%eax
movzbl 0x30(%rsp,%rax,1),%eax
mov    %al,(%r12)
add    $0x1,%r12
cmp    %rbx,%r12
jb     1230 <generate+0x100>
add    $0x1038,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [32 x i8] c"Generating %u KB with P=%.2f%%\0A\00"
define void @generate(i8* %0, i64 %1, double %2) {
%4 = alloca i8*
%5 = alloca i64
%6 = alloca double
%7 = alloca [4096 x i8]
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i8*
%12 = alloca i8*
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
%16 = alloca i32
store i8* %0, i8** %4
store i64 %1, i64* %5
store double %2, double* %6
%17 = bitcast [4096 x i8]* %7 to i8*
store i32 4096, i32* %8
store i32 0, i32* %9
store i32 0, i32* %10
%18 = load i8*, i8** %4
store i8* %18, i8** %11
%19 = load i8*, i8** %11
%20 = load i64, i64* %5
%21 = getelementptr inbounds i8, i8* %19, i64 %20
store i8* %21, i8** %12
store i32 1, i32* %13
%22 = load double, double* %6
%23 = fcmp oeq double %22, 0.000000e+00
br i1 %23, label %24, label %25
24: ; preds = %3
store double 5.000000e-03, double* %6
br label %25
25: ; preds = %24, %3
%26 = load i32, i32* @stderr
%27 = load i64, i64* %5
%28 = lshr i64 %27, 10
%29 = trunc i64 %28 to i32
%30 = load double, double* %6
%31 = fmul double %30, 1.000000e+02
%32 = call i32 @fprintf(i32 %26, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i32 %29, double %31)
br label %33
33: ; preds = %60, %25
%34 = load i32, i32* %8
%35 = icmp ne i32 %34, 0
br i1 %35, label %36, label %66
36: ; preds = %33
%37 = load i32, i32* %8
%38 = sitofp i32 %37 to double
%39 = load double, double* %6
%40 = fmul double %38, %39
%41 = fptoui double %40 to i32
store i32 %41, i32* %14
%42 = load i32, i32* %14
%43 = icmp ne i32 %42, 0
br i1 %43, label %45, label %44
44: ; preds = %36
store i32 1, i32* %14
br label %45
45: ; preds = %44, %36
%46 = load i32, i32* %9
%47 = load i32, i32* %14
%48 = add i32 %46, %47
store i32 %48, i32* %15
br label %49
49: ; preds = %53, %45
%50 = load i32, i32* %9
%51 = load i32, i32* %15
%52 = icmp ult i32 %50, %51
br i1 %52, label %53, label %60
53: ; preds = %49
%54 = load i32, i32* %10
%55 = trunc i32 %54 to i8
%56 = load i32, i32* %9
%57 = add i32 %56, 1
store i32 %57, i32* %9
%58 = zext i32 %56 to i64
%59 = getelementptr inbounds [4096 x i8], [4096 x i8]* %7, i64 0, i64 %58
store i8 %55, i8* %59
br label %49
60: ; preds = %49
%61 = load i32, i32* %10
%62 = add i32 %61, 1
store i32 %62, i32* %10
%63 = load i32, i32* %14
%64 = load i32, i32* %8
%65 = sub i32 %64, %63
store i32 %65, i32* %8
br label %33
66: ; preds = %33
br label %67
67: ; preds = %71, %66
%68 = load i8*, i8** %11
%69 = load i8*, i8** %12
%70 = icmp ult i8* %68, %69
br i1 %70, label %71, label %80
71: ; preds = %67
%72 = call i32 @GEN_rand(i32* %13)
%73 = and i32 %72, 4095
store i32 %73, i32* %16
%74 = load i32, i32* %16
%75 = zext i32 %74 to i64
%76 = getelementptr inbounds [4096 x i8], [4096 x i8]* %7, i64 0, i64 %75
%77 = load i8, i8* %76
%78 = load i8*, i8** %11
%79 = getelementptr inbounds i8, i8* %78, i32 1
store i8* %79, i8** %11
store i8 %77, i8* %78
br label %67
80: ; preds = %67
ret void
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [32 x i8] c"Generating %u KB with P=%.2f%%\0A\00"
define void @generate(i8* writeonly %0, i64 %1, double %2) {
%4 = alloca [4096 x i8]
%5 = alloca i32
%6 = getelementptr inbounds [4096 x i8], [4096 x i8]* %4, i64 0, i64 0
%7 = bitcast i32* %5 to i8*
store i32 1, i32* %5
%8 = fcmp oeq double %2, 0.000000e+00
%9 = select i1 %8, double 5.000000e-03, double %2
%10 = load i32, i32* @stderr
%11 = lshr i64 %1, 10
%12 = trunc i64 %11 to i32
%13 = fmul double %9, 1.000000e+02
%14 = tail call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i32 %12, double %13)
br label %18
15: ; preds = %35
%16 = getelementptr inbounds i8, i8* %0, i64 %1
%17 = icmp sgt i64 %1, 0
br i1 %17, label %40, label %49
18: ; preds = %3, %35
%19 = phi i32 [ 4096, %3 ], [ %38, %35 ]
%20 = phi i32 [ 0, %3 ], [ %36, %35 ]
%21 = phi i32 [ 0, %3 ], [ %37, %35 ]
%22 = sitofp i32 %19 to double
%23 = fmul double %9, %22
%24 = fptoui double %23 to i32
%25 = icmp eq i32 %24, 0
%26 = select i1 %25, i32 1, i32 %24
%27 = add i32 %26, %20
%28 = icmp ult i32 %20, %27
br i1 %28, label %29, label %35
29: ; preds = %18
%30 = trunc i32 %21 to i8
%31 = zext i32 %20 to i64
%32 = getelementptr [4096 x i8], [4096 x i8]* %4, i64 0, i64 %31
%33 = zext i32 %26 to i64
%34 = add i32 %20, %26
br label %35
35: ; preds = %29, %18
%36 = phi i32 [ %20, %18 ], [ %34, %29 ]
%37 = add i32 %21, 1
%38 = sub i32 %19, %26
%39 = icmp eq i32 %38, 0
br i1 %39, label %15, label %18
40: ; preds = %15, %40
%41 = phi i8* [ %47, %40 ], [ %0, %15 ]
%42 = call i32 @GEN_rand(i32* %5)
%43 = and i32 %42, 4095
%44 = zext i32 %43 to i64
%45 = getelementptr inbounds [4096 x i8], [4096 x i8]* %4, i64 0, i64 %44
%46 = load i8, i8* %45
%47 = getelementptr inbounds i8, i8* %41, i64 1
store i8 %46, i8* %41
%48 = icmp ult i8* %47, %16
br i1 %48, label %40, label %49
49: ; preds = %40, %15
ret void
}