#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 rand();
int generate_distribution(double *prob, int count) {
  if (prob == NULL || count < 0)
    return -1;
  double sum = 0.0;
  int id = 0;
  for (; id < count; id++) {
    double item = rand() % 1024;
    sum += item;
    prob[id] = item;
  }
  for (id = 0; id < count; id++) {
    prob[id] /= sum;
  }
  return 0;
}


x86 assembly (clang, -O0):
<generate_distribution>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1134 <generate_distribution+0x24>
cmpl   $0x0,-0x14(%rbp)
jge    1140 <generate_distribution+0x30>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e5 <generate_distribution+0xd5>
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    11a2 <generate_distribution+0x92>
mov    $0x0,%al
call   1030 <rand@plt>
mov    $0x400,%ecx
cltd
idiv   %ecx
cvtsi2sd %edx,%xmm0
movsd  %xmm0,-0x30(%rbp)
movsd  -0x30(%rbp),%xmm0
addsd  -0x20(%rbp),%xmm0
movsd  %xmm0,-0x20(%rbp)
movsd  -0x30(%rbp),%xmm0
mov    -0x10(%rbp),%rax
movslq -0x24(%rbp),%rcx
movsd  %xmm0,(%rax,%rcx,8)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    114f <generate_distribution+0x3f>
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    11de <generate_distribution+0xce>
movsd  -0x20(%rbp),%xmm1
mov    -0x10(%rbp),%rax
movslq -0x24(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
divsd  %xmm1,%xmm0
movsd  %xmm0,(%rax,%rcx,8)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    11a9 <generate_distribution+0x99>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<generate_distribution>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    $0xffffffff,%eax
test   %rdi,%rdi
je     1249 <generate_distribution+0x139>
mov    %esi,%r14d
test   %esi,%esi
js     1249 <generate_distribution+0x139>
je     1247 <generate_distribution+0x137>
mov    %rdi,%rbx
mov    %r14d,%r15d
xorpd  %xmm3,%xmm3
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
nop
movapd %xmm3,(%rsp)
xor    %eax,%eax
call   1030 <rand@plt>
movapd (%rsp),%xmm3
lea    0x3ff(%rax),%ecx
test   %eax,%eax
cmovns %eax,%ecx
and    $0xfffffc00,%ecx
sub    %ecx,%eax
xorps  %xmm0,%xmm0
cvtsi2sd %eax,%xmm0
addsd  %xmm0,%xmm3
movsd  %xmm0,(%rbx,%rbp,8)
add    $0x1,%rbp
cmp    %rbp,%r15
jne    1150 <generate_distribution+0x40>
test   %r14d,%r14d
je     1247 <generate_distribution+0x137>
cmp    $0x1,%r14d
jne    11a3 <generate_distribution+0x93>
xor    %ecx,%ecx
jmp    1230 <generate_distribution+0x120>
mov    %r15d,%ecx
and    $0xfffffffe,%ecx
movapd %xmm3,%xmm0
unpcklpd %xmm3,%xmm0
lea    -0x2(%rcx),%rdx
mov    %rdx,%rax
shr    %rax
add    $0x1,%rax
test   %rdx,%rdx
je     1254 <generate_distribution+0x144>
mov    %rax,%rsi
and    $0xfffffffffffffffe,%rsi
xor    %edx,%edx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
movupd (%rbx,%rdx,8),%xmm1
movupd 0x10(%rbx,%rdx,8),%xmm2
divpd  %xmm0,%xmm1
movupd %xmm1,(%rbx,%rdx,8)
divpd  %xmm0,%xmm2
movupd %xmm2,0x10(%rbx,%rdx,8)
add    $0x4,%rdx
add    $0xfffffffffffffffe,%rsi
jne    11e0 <generate_distribution+0xd0>
test   $0x1,%al
je     121a <generate_distribution+0x10a>
movupd (%rbx,%rdx,8),%xmm1
divpd  %xmm0,%xmm1
movupd %xmm1,(%rbx,%rdx,8)
xor    %eax,%eax
cmp    %r15,%rcx
je     1249 <generate_distribution+0x139>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
movsd  (%rbx,%rcx,8),%xmm0
divsd  %xmm3,%xmm0
movsd  %xmm0,(%rbx,%rcx,8)
add    $0x1,%rcx
cmp    %rcx,%r15
jne    1230 <generate_distribution+0x120>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %edx,%edx
test   $0x1,%al
jne    120c <generate_distribution+0xfc>
jmp    121a <generate_distribution+0x10a>

LLVM IR (-O0):
define i32 @generate_distribution(double* %0, i32 %1) {
%3 = alloca i32
%4 = alloca double*
%5 = alloca i32
%6 = alloca double
%7 = alloca i32
%8 = alloca double
store double* %0, double** %4
store i32 %1, i32* %5
%9 = load double*, double** %4
%10 = icmp eq double* %9, null
br i1 %10, label %14, label %11
11: ; preds = %2
%12 = load i32, i32* %5
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %15
14: ; preds = %11, %2
store i32 -1, i32* %3
br label %52
15: ; preds = %11
store double 0.000000e+00, double* %6
store i32 0, i32* %7
br label %16
16: ; preds = %32, %15
%17 = load i32, i32* %7
%18 = load i32, i32* %5
%19 = icmp slt i32 %17, %18
br i1 %19, label %20, label %35
20: ; preds = %16
%21 = call i32 (...) @rand()
%22 = srem i32 %21, 1024
%23 = sitofp i32 %22 to double
store double %23, double* %8
%24 = load double, double* %8
%25 = load double, double* %6
%26 = fadd double %25, %24
store double %26, double* %6
%27 = load double, double* %8
%28 = load double*, double** %4
%29 = load i32, i32* %7
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds double, double* %28, i64 %30
store double %27, double* %31
br label %32
32: ; preds = %20
%33 = load i32, i32* %7
%34 = add nsw i32 %33, 1
store i32 %34, i32* %7
br label %16
35: ; preds = %16
store i32 0, i32* %7
br label %36
36: ; preds = %48, %35
%37 = load i32, i32* %7
%38 = load i32, i32* %5
%39 = icmp slt i32 %37, %38
br i1 %39, label %40, label %51
40: ; preds = %36
%41 = load double, double* %6
%42 = load double*, double** %4
%43 = load i32, i32* %7
%44 = sext i32 %43 to i64
%45 = getelementptr inbounds double, double* %42, i64 %44
%46 = load double, double* %45
%47 = fdiv double %46, %41
store double %47, double* %45
br label %48
48: ; preds = %40
%49 = load i32, i32* %7
%50 = add nsw i32 %49, 1
store i32 %50, i32* %7
br label %36
51: ; preds = %36
store i32 0, i32* %3
br label %52
52: ; preds = %51, %14
%53 = load i32, i32* %3
ret i32 %53
}

LLVM IR (-O2):
define i32 @generate_distribution(double* %0, i32 %1) {
%3 = icmp eq double* %0, null
%4 = icmp slt i32 %1, 0
%5 = or i1 %3, %4
br i1 %5, label %72, label %6
6: ; preds = %2
%7 = icmp eq i32 %1, 0
br i1 %7, label %72, label %8
8: ; preds = %6
%9 = zext i32 %1 to i64
br label %55
10: ; preds = %55
br i1 %7, label %72, label %11
11: ; preds = %10
%12 = zext i32 %1 to i64
%13 = icmp eq i32 %1, 1
br i1 %13, label %53, label %14
14: ; preds = %11
%15 = and i64 %9, 4294967294
%16 = insertelement <2 x double> poison, double %61, i64 0
%17 = shufflevector <2 x double> %16, <2 x double> poison, <2 x i32> zeroinitializer
%18 = add nsw i64 %15, -2
%19 = lshr exact i64 %18, 1
%20 = add nuw i64 %19, 1
%21 = and i64 %20, 1
%22 = icmp eq i64 %18, 0
br i1 %22, label %42, label %23
23: ; preds = %14
%24 = and i64 %20, -2
br label %25
25: ; preds = %25, %23
%26 = phi i64 [ 0, %23 ], [ %39, %25 ]
%27 = phi i64 [ 0, %23 ], [ %40, %25 ]
%28 = getelementptr inbounds double, double* %0, i64 %26
%29 = bitcast double* %28 to <2 x double>*
%30 = load <2 x double>, <2 x double>* %29
%31 = fdiv <2 x double> %30, %17
%32 = bitcast double* %28 to <2 x double>*
store <2 x double> %31, <2 x double>* %32
%33 = or i64 %26, 2
%34 = getelementptr inbounds double, double* %0, i64 %33
%35 = bitcast double* %34 to <2 x double>*
%36 = load <2 x double>, <2 x double>* %35
%37 = fdiv <2 x double> %36, %17
%38 = bitcast double* %34 to <2 x double>*
store <2 x double> %37, <2 x double>* %38
%39 = add nuw i64 %26, 4
%40 = add nuw i64 %27, 2
%41 = icmp eq i64 %40, %24
br i1 %41, label %42, label %25
42: ; preds = %25, %14
%43 = phi i64 [ 0, %14 ], [ %39, %25 ]
%44 = icmp eq i64 %21, 0
br i1 %44, label %51, label %45
45: ; preds = %42
%46 = getelementptr inbounds double, double* %0, i64 %43
%47 = bitcast double* %46 to <2 x double>*
%48 = load <2 x double>, <2 x double>* %47
%49 = fdiv <2 x double> %48, %17
%50 = bitcast double* %46 to <2 x double>*
store <2 x double> %49, <2 x double>* %50
br label %51
51: ; preds = %42, %45
%52 = icmp eq i64 %15, %9
br i1 %52, label %72, label %53
53: ; preds = %11, %51
%54 = phi i64 [ 0, %11 ], [ %15, %51 ]
br label %65
55: ; preds = %8, %55
%56 = phi i64 [ 0, %8 ], [ %63, %55 ]
%57 = phi double [ 0.000000e+00, %8 ], [ %61, %55 ]
%58 = tail call i32 (...) @rand()
%59 = srem i32 %58, 1024
%60 = sitofp i32 %59 to double
%61 = fadd double %57, %60
%62 = getelementptr inbounds double, double* %0, i64 %56
store double %60, double* %62
%63 = add nuw nsw i64 %56, 1
%64 = icmp eq i64 %63, %9
br i1 %64, label %10, label %55
65: ; preds = %53, %65
%66 = phi i64 [ %70, %65 ], [ %54, %53 ]
%67 = getelementptr inbounds double, double* %0, i64 %66
%68 = load double, double* %67
%69 = fdiv double %68, %61
store double %69, double* %67
%70 = add nuw nsw i64 %66, 1
%71 = icmp eq i64 %70, %12
br i1 %71, label %72, label %65
72: ; preds = %65, %51, %6, %10, %2
%73 = phi i32 [ -1, %2 ], [ 0, %10 ], [ 0, %6 ], [ 0, %51 ], [ 0, %65 ]
ret i32 %73
}