#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];
  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)
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    11a2 <generate+0x82>
jp     11a2 <generate+0x82>
movsd  0xe63(%rip),%xmm0
movsd  %xmm0,-0x18(%rbp)
mov    0x2e37(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
shr    $0xa,%rax
mov    %eax,%edx
movsd  0xe4b(%rip),%xmm0
mulsd  -0x18(%rbp),%xmm0
lea    0xe47(%rip),%rsi
call   1030 <fprintf@plt>
cmpl   $0x0,-0x1024(%rbp)
je     1281 <generate+0x161>
cvtsi2sdl -0x1024(%rbp),%xmm0
ff
movsd  -0x18(%rbp),%xmm1
mulsd  %xmm1,%xmm0
cvttsd2si %xmm0,%rax
mov    %eax,-0x1048(%rbp)
cmpl   $0x0,-0x1048(%rbp)
jne    120e <generate+0xee>
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    1259 <generate+0x139>
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    1220 <generate+0x100>
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    11ce <generate+0xae>
jmp    1286 <generate+0x166>
mov    -0x1038(%rbp),%rax
cmp    -0x1040(%rbp),%rax
jae    12da <generate+0x1ba>
lea    -0x1044(%rbp),%rdi
call   1040 <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    1286 <generate+0x166>
add    $0x1050,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<generate>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1028,%rsp
mov    %rdi,%r12
movl   $0x1,0x4(%rsp)
xorpd  %xmm2,%xmm2
cmpeqsd %xmm0,%xmm2
movsd  0xea3(%rip),%xmm1
andpd  %xmm2,%xmm1
andnpd %xmm0,%xmm2
orpd   %xmm1,%xmm2
mov    0x2e70(%rip),%rax
mov    (%rax),%edi
mov    %rsi,0x8(%rsp)
mov    %rsi,%rdx
shr    $0xa,%rdx
movapd %xmm2,0x10(%rsp)
movapd %xmm2,%xmm0
mulsd  0xe78(%rip),%xmm0
lea    0xe79(%rip),%rsi
call   1040 <fprintf@plt>
xor    %r13d,%r13d
mov    $0x1000,%ebx
mov    $0x1,%r15d
xor    %eax,%eax
jmp    11d0 <generate+0xa0>
xchg   %ax,%ax
mov    %eax,%eax
lea    (%rsp,%rax,1),%rdi
add    $0x20,%rdi
mov    %ebp,%edx
movzbl %r13b,%esi
call   1030 <memset@plt>
add    $0x1,%r13b
mov    %r14d,%eax
sub    %ebp,%ebx
je     11f6 <generate+0xc6>
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     11b0 <generate+0x80>
mov    %eax,%r14d
jmp    11c5 <generate+0x95>
mov    0x8(%rsp),%rbx
test   %rbx,%rbx
jle    122f <generate+0xff>
add    %r12,%rbx
lea    0x4(%rsp),%r15
nopl   0x0(%rax,%rax,1)
mov    %r15,%rdi
call   1050 <GEN_rand@plt>
and    $0xfff,%eax
movzbl 0x20(%rsp,%rax,1),%eax
mov    %al,(%r12)
add    $0x1,%r12
cmp    %rbx,%r12
jb     1210 <generate+0xe0>
add    $0x1028,%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
store i32 4096, i32* %8
store i32 0, i32* %9
store i32 0, i32* %10
%17 = load i8*, i8** %4
store i8* %17, i8** %11
%18 = load i8*, i8** %11
%19 = load i64, i64* %5
%20 = getelementptr inbounds i8, i8* %18, i64 %19
store i8* %20, i8** %12
store i32 1, i32* %13
%21 = load double, double* %6
%22 = fcmp oeq double %21, 0.000000e+00
br i1 %22, label %23, label %24
23: ; preds = %3
store double 5.000000e-03, double* %6
br label %24
24: ; preds = %23, %3
%25 = load i32, i32* @stderr
%26 = load i64, i64* %5
%27 = lshr i64 %26, 10
%28 = trunc i64 %27 to i32
%29 = load double, double* %6
%30 = fmul double %29, 1.000000e+02
%31 = call i32 @fprintf(i32 %25, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i32 %28, double %30)
br label %32
32: ; preds = %59, %24
%33 = load i32, i32* %8
%34 = icmp ne i32 %33, 0
br i1 %34, label %35, label %65
35: ; preds = %32
%36 = load i32, i32* %8
%37 = sitofp i32 %36 to double
%38 = load double, double* %6
%39 = fmul double %37, %38
%40 = fptoui double %39 to i32
store i32 %40, i32* %14
%41 = load i32, i32* %14
%42 = icmp ne i32 %41, 0
br i1 %42, label %44, label %43
43: ; preds = %35
store i32 1, i32* %14
br label %44
44: ; preds = %43, %35
%45 = load i32, i32* %9
%46 = load i32, i32* %14
%47 = add i32 %45, %46
store i32 %47, i32* %15
br label %48
48: ; preds = %52, %44
%49 = load i32, i32* %9
%50 = load i32, i32* %15
%51 = icmp ult i32 %49, %50
br i1 %51, label %52, label %59
52: ; preds = %48
%53 = load i32, i32* %10
%54 = trunc i32 %53 to i8
%55 = load i32, i32* %9
%56 = add i32 %55, 1
store i32 %56, i32* %9
%57 = zext i32 %55 to i64
%58 = getelementptr inbounds [4096 x i8], [4096 x i8]* %7, i64 0, i64 %57
store i8 %54, i8* %58
br label %48
59: ; preds = %48
%60 = load i32, i32* %10
%61 = add i32 %60, 1
store i32 %61, i32* %10
%62 = load i32, i32* %14
%63 = load i32, i32* %8
%64 = sub i32 %63, %62
store i32 %64, i32* %8
br label %32
65: ; preds = %32
br label %66
66: ; preds = %70, %65
%67 = load i8*, i8** %11
%68 = load i8*, i8** %12
%69 = icmp ult i8* %67, %68
br i1 %69, label %70, label %79
70: ; preds = %66
%71 = call i32 @GEN_rand(i32* %13)
%72 = and i32 %71, 4095
store i32 %72, i32* %16
%73 = load i32, i32* %16
%74 = zext i32 %73 to i64
%75 = getelementptr inbounds [4096 x i8], [4096 x i8]* %7, i64 0, i64 %74
%76 = load i8, i8* %75
%77 = load i8*, i8** %11
%78 = getelementptr inbounds i8, i8* %77, i32 1
store i8* %78, i8** %11
store i8 %76, i8* %77
br label %66
79: ; preds = %66
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
}