#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 /*<<< orphan*/ assert(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ free(double *);
double log(double);
double log1p(double);
double log_sum(double, double);
double *malloc(int);
double pow(double, double);
int /*<<< orphan*/ stderr;
double *naive_calc_prob_dist(const int *quals, int N, int K) {
  double *probvec = NULL;
  double *probvec_prev = NULL;
  double *probvec_swp = NULL;
  int n;
  if (NULL == (probvec = malloc((N + 1) * sizeof(double)))) {
    fprintf(stderr, "FATAL: couldn't allocate memory at %d\n", 133);
    return NULL;
  }
  if (NULL == (probvec_prev = malloc((N + 1) * sizeof(double)))) {
    fprintf(stderr, "FATAL: couldn't allocate memory at %d\n", 138);
    return NULL;
  }
  probvec_prev[0] = 0.0;
  for (n = 1; n < N + 1; n++) {
    int k;
    double pn = (pow(10.0, -1.0 * (quals[n - 1]) / 10.0));
    double log_pn = log(pn);
    double log_1_pn = log1p(-pn);
    assert(((quals[n - 1]) > 1 && (quals[n - 1]) < 100));
    k = 0;
    probvec[k] = probvec_prev[k] + log_1_pn;
    for (k = 1; k < n; k++) {
      probvec[k] =
          log_sum(probvec_prev[k] + log_1_pn, probvec_prev[k - 1] + log_pn);
    }
    k = n;
    probvec[k] = probvec_prev[k - 1] + log_pn;
    if (n < N) {
      probvec_swp = probvec;
      probvec = probvec_prev;
      probvec_prev = probvec_swp;
    }
  }
  free(probvec_prev);
  return probvec;
}


x86 assembly (clang, -O0):
<naive_calc_prob_dist>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
cltq
shl    $0x3,%rax
mov    %eax,%edi
call   1070 <malloc@plt>
mov    %rax,%rcx
mov    %rcx,-0x20(%rbp)
xor    %eax,%eax
cmp    %rcx,%rax
jne    11f6 <naive_calc_prob_dist+0x76>
mov    0x2e0a(%rip),%rax
mov    (%rax),%edi
lea    0xe31(%rip),%rsi
mov    $0x85,%edx
call   1050 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    140a <naive_calc_prob_dist+0x28a>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
cltq
shl    $0x3,%rax
mov    %eax,%edi
call   1070 <malloc@plt>
mov    %rax,%rcx
mov    %rcx,-0x28(%rbp)
xor    %eax,%eax
cmp    %rcx,%rax
jne    1242 <naive_calc_prob_dist+0xc2>
mov    0x2dbe(%rip),%rax
mov    (%rax),%edi
lea    0xde5(%rip),%rsi
mov    $0x8a,%edx
call   1050 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    140a <naive_calc_prob_dist+0x28a>
mov    -0x28(%rbp),%rax
xorps  %xmm0,%xmm0
movsd  %xmm0,(%rax)
movl   $0x1,-0x34(%rbp)
mov    -0x34(%rbp),%eax
mov    -0x14(%rbp),%ecx
add    $0x1,%ecx
cmp    %ecx,%eax
jge    13f9 <naive_calc_prob_dist+0x279>
mov    -0x10(%rbp),%rax
mov    -0x34(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
cvtsi2sdl (%rax,%rcx,4),%xmm0
movsd  0xd89(%rip),%xmm1
mulsd  %xmm0,%xmm1
movsd  0xd75(%rip),%xmm0
divsd  %xmm0,%xmm1
movsd  0xd69(%rip),%xmm0
call   1090 <pow@plt>
movsd  %xmm0,-0x40(%rbp)
movsd  -0x40(%rbp),%xmm0
call   1080 <log@plt>
movsd  %xmm0,-0x48(%rbp)
movsd  -0x40(%rbp),%xmm0
movq   %xmm0,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm0
call   10a0 <log1p@plt>
movsd  %xmm0,-0x50(%rbp)
mov    -0x10(%rbp),%rcx
mov    -0x34(%rbp),%eax
sub    $0x1,%eax
movslq %eax,%rdx
xor    %eax,%eax
cmpl   $0x1,(%rcx,%rdx,4)
mov    %al,-0x51(%rbp)
jle    1309 <naive_calc_prob_dist+0x189>
mov    -0x10(%rbp),%rax
mov    -0x34(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
cmpl   $0x64,(%rax,%rcx,4)
setl   %al
mov    %al,-0x51(%rbp)
mov    -0x51(%rbp),%al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
movl   $0x0,-0x38(%rbp)
mov    -0x28(%rbp),%rax
movslq -0x38(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
addsd  -0x50(%rbp),%xmm0
mov    -0x20(%rbp),%rax
movslq -0x38(%rbp),%rcx
movsd  %xmm0,(%rax,%rcx,8)
movl   $0x1,-0x38(%rbp)
mov    -0x38(%rbp),%eax
cmp    -0x34(%rbp),%eax
jge    1398 <naive_calc_prob_dist+0x218>
mov    -0x28(%rbp),%rax
movslq -0x38(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
addsd  -0x50(%rbp),%xmm0
mov    -0x28(%rbp),%rax
mov    -0x38(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movsd  (%rax,%rcx,8),%xmm1
addsd  -0x48(%rbp),%xmm1
call   1060 <log_sum@plt>
mov    -0x20(%rbp),%rax
movslq -0x38(%rbp),%rcx
movsd  %xmm0,(%rax,%rcx,8)
mov    -0x38(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x38(%rbp)
jmp    1343 <naive_calc_prob_dist+0x1c3>
mov    -0x34(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    -0x28(%rbp),%rax
mov    -0x38(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movsd  (%rax,%rcx,8),%xmm0
addsd  -0x48(%rbp),%xmm0
mov    -0x20(%rbp),%rax
movslq -0x38(%rbp),%rcx
movsd  %xmm0,(%rax,%rcx,8)
mov    -0x34(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    13e6 <naive_calc_prob_dist+0x266>
mov    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x28(%rbp)
jmp    13eb <naive_calc_prob_dist+0x26b>
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    1254 <naive_calc_prob_dist+0xd4>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<naive_calc_prob_dist>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %esi,%r15d
mov    %rdi,0x30(%rsp)
lea    0x8(,%r15,8),%ebx
mov    %ebx,%edi
call   1070 <malloc@plt>
test   %rax,%rax
je     1318 <naive_calc_prob_dist+0x198>
mov    %rax,%r12
mov    %ebx,%edi
call   1070 <malloc@plt>
test   %rax,%rax
je     132f <naive_calc_prob_dist+0x1af>
mov    %rax,%rbx
movq   $0x0,(%rax)
test   %r15d,%r15d
jle    134e <naive_calc_prob_dist+0x1ce>
mov    %r15d,%eax
mov    %rax,0x28(%rsp)
add    $0x1,%r15d
mov    $0x1,%ebp
mov    $0xffffffffffffffff,%r13
jmp    1226 <naive_calc_prob_dist+0xa6>
xchg   %ax,%ax
movsd  -0x8(%rbx,%rbp,8),%xmm0
addsd  0x8(%rsp),%xmm0
movsd  %xmm0,(%r12,%rbp,8)
cmp    0x28(%rsp),%rbp
mov    %rbx,%rdi
cmovb  %r12,%rdi
cmovb  %rbx,%r12
add    $0x1,%rbp
add    $0x1,%r13
mov    %rdi,%rbx
cmp    %r15,%rbp
je     1351 <naive_calc_prob_dist+0x1d1>
mov    0x30(%rsp),%r14
xorps  %xmm1,%xmm1
cvtsi2sdl -0x4(%r14,%rbp,4),%xmm1
divsd  0xdc3(%rip),%xmm1
movsd  0xdc3(%rip),%xmm0
call   1090 <pow@plt>
movaps %xmm0,0x10(%rsp)
call   1080 <log@plt>
movsd  %xmm0,0x8(%rsp)
movaps 0x10(%rsp),%xmm0
xorps  0xdaa(%rip),%xmm0
call   10a0 <log1p@plt>
movsd  %xmm0,0x10(%rsp)
mov    -0x4(%r14,%rbp,4),%eax
add    $0xfffffffe,%eax
xor    %edi,%edi
cmp    $0x62,%eax
setb   %dil
call   1040 <assert@plt>
movsd  0x10(%rsp),%xmm1
movsd  (%rbx),%xmm2
movapd %xmm1,%xmm0
addsd  %xmm2,%xmm0
movsd  %xmm0,(%r12)
cmp    $0x2,%rbp
jb     11f0 <naive_calc_prob_dist+0x70>
movsd  0x8(%rbx),%xmm0
addsd  %xmm1,%xmm0
movsd  0x8(%rsp),%xmm1
addsd  %xmm2,%xmm1
call   1060 <log_sum@plt>
movsd  0x10(%rsp),%xmm1
movsd  %xmm0,0x8(%r12)
cmp    $0x2,%rbp
je     11f0 <naive_calc_prob_dist+0x70>
xor    %r14d,%r14d
nopl   0x0(%rax,%rax,1)
movsd  0x10(%rbx,%r14,8),%xmm0
addsd  %xmm1,%xmm0
movsd  0x8(%rbx,%r14,8),%xmm1
addsd  0x8(%rsp),%xmm1
call   1060 <log_sum@plt>
movsd  0x10(%rsp),%xmm1
movsd  %xmm0,0x10(%r12,%r14,8)
add    $0x1,%r14
cmp    %r14,%r13
jne    12e0 <naive_calc_prob_dist+0x160>
jmp    11f0 <naive_calc_prob_dist+0x70>
mov    0x2cc1(%rip),%rax
mov    (%rax),%edi
lea    0xcf8(%rip),%rsi
mov    $0x85,%edx
jmp    1344 <naive_calc_prob_dist+0x1c4>
mov    0x2caa(%rip),%rax
mov    (%rax),%edi
lea    0xce1(%rip),%rsi
mov    $0x8a,%edx
call   1050 <fprintf@plt>
xor    %r12d,%r12d
jmp    1356 <naive_calc_prob_dist+0x1d6>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %r12,%rax
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [39 x i8] c"FATAL: couldn't allocate memory at %d\0A\00"
define double* @naive_calc_prob_dist(i32* %0, i32 %1, i32 %2) {
%4 = alloca double*
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i32
%8 = alloca double*
%9 = alloca double*
%10 = alloca double*
%11 = alloca i32
%12 = alloca i32
%13 = alloca double
%14 = alloca double
%15 = alloca double
store i32* %0, i32** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
store double* null, double** %8
store double* null, double** %9
store double* null, double** %10
%16 = load i32, i32* %6
%17 = add nsw i32 %16, 1
%18 = sext i32 %17 to i64
%19 = mul i64 %18, 8
%20 = trunc i64 %19 to i32
%21 = call double* @malloc(i32 %20)
store double* %21, double** %8
%22 = icmp eq double* null, %21
br i1 %22, label %23, label %26
23: ; preds = %3
%24 = load i32, i32* @stderr
%25 = call i32 @fprintf(i32 %24, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0), i32 133)
store double* null, double** %4
br label %148
26: ; preds = %3
%27 = load i32, i32* %6
%28 = add nsw i32 %27, 1
%29 = sext i32 %28 to i64
%30 = mul i64 %29, 8
%31 = trunc i64 %30 to i32
%32 = call double* @malloc(i32 %31)
store double* %32, double** %9
%33 = icmp eq double* null, %32
br i1 %33, label %34, label %37
34: ; preds = %26
%35 = load i32, i32* @stderr
%36 = call i32 @fprintf(i32 %35, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0), i32 138)
store double* null, double** %4
br label %148
37: ; preds = %26
%38 = load double*, double** %9
%39 = getelementptr inbounds double, double* %38, i64 0
store double 0.000000e+00, double* %39
store i32 1, i32* %11
br label %40
40: ; preds = %141, %37
%41 = load i32, i32* %11
%42 = load i32, i32* %6
%43 = add nsw i32 %42, 1
%44 = icmp slt i32 %41, %43
br i1 %44, label %45, label %144
45: ; preds = %40
%46 = load i32*, i32** %5
%47 = load i32, i32* %11
%48 = sub nsw i32 %47, 1
%49 = sext i32 %48 to i64
%50 = getelementptr inbounds i32, i32* %46, i64 %49
%51 = load i32, i32* %50
%52 = sitofp i32 %51 to double
%53 = fmul double -1.000000e+00, %52
%54 = fdiv double %53, 1.000000e+01
%55 = call double @pow(double 1.000000e+01, double %54)
store double %55, double* %13
%56 = load double, double* %13
%57 = call double @log(double %56)
store double %57, double* %14
%58 = load double, double* %13
%59 = fneg double %58
%60 = call double @log1p(double %59)
store double %60, double* %15
%61 = load i32*, i32** %5
%62 = load i32, i32* %11
%63 = sub nsw i32 %62, 1
%64 = sext i32 %63 to i64
%65 = getelementptr inbounds i32, i32* %61, i64 %64
%66 = load i32, i32* %65
%67 = icmp sgt i32 %66, 1
br i1 %67, label %68, label %76
68: ; preds = %45
%69 = load i32*, i32** %5
%70 = load i32, i32* %11
%71 = sub nsw i32 %70, 1
%72 = sext i32 %71 to i64
%73 = getelementptr inbounds i32, i32* %69, i64 %72
%74 = load i32, i32* %73
%75 = icmp slt i32 %74, 100
br label %76
76: ; preds = %68, %45
%77 = phi i1 [ false, %45 ], [ %75, %68 ]
%78 = zext i1 %77 to i32
%79 = call i32 @assert(i32 %78)
store i32 0, i32* %12
%80 = load double*, double** %9
%81 = load i32, i32* %12
%82 = sext i32 %81 to i64
%83 = getelementptr inbounds double, double* %80, i64 %82
%84 = load double, double* %83
%85 = load double, double* %15
%86 = fadd double %84, %85
%87 = load double*, double** %8
%88 = load i32, i32* %12
%89 = sext i32 %88 to i64
%90 = getelementptr inbounds double, double* %87, i64 %89
store double %86, double* %90
store i32 1, i32* %12
br label %91
91: ; preds = %116, %76
%92 = load i32, i32* %12
%93 = load i32, i32* %11
%94 = icmp slt i32 %92, %93
br i1 %94, label %95, label %119
95: ; preds = %91
%96 = load double*, double** %9
%97 = load i32, i32* %12
%98 = sext i32 %97 to i64
%99 = getelementptr inbounds double, double* %96, i64 %98
%100 = load double, double* %99
%101 = load double, double* %15
%102 = fadd double %100, %101
%103 = load double*, double** %9
%104 = load i32, i32* %12
%105 = sub nsw i32 %104, 1
%106 = sext i32 %105 to i64
%107 = getelementptr inbounds double, double* %103, i64 %106
%108 = load double, double* %107
%109 = load double, double* %14
%110 = fadd double %108, %109
%111 = call double @log_sum(double %102, double %110)
%112 = load double*, double** %8
%113 = load i32, i32* %12
%114 = sext i32 %113 to i64
%115 = getelementptr inbounds double, double* %112, i64 %114
store double %111, double* %115
br label %116
116: ; preds = %95
%117 = load i32, i32* %12
%118 = add nsw i32 %117, 1
store i32 %118, i32* %12
br label %91
119: ; preds = %91
%120 = load i32, i32* %11
store i32 %120, i32* %12
%121 = load double*, double** %9
%122 = load i32, i32* %12
%123 = sub nsw i32 %122, 1
%124 = sext i32 %123 to i64
%125 = getelementptr inbounds double, double* %121, i64 %124
%126 = load double, double* %125
%127 = load double, double* %14
%128 = fadd double %126, %127
%129 = load double*, double** %8
%130 = load i32, i32* %12
%131 = sext i32 %130 to i64
%132 = getelementptr inbounds double, double* %129, i64 %131
store double %128, double* %132
%133 = load i32, i32* %11
%134 = load i32, i32* %6
%135 = icmp slt i32 %133, %134
br i1 %135, label %136, label %140
136: ; preds = %119
%137 = load double*, double** %8
store double* %137, double** %10
%138 = load double*, double** %9
store double* %138, double** %8
%139 = load double*, double** %10
store double* %139, double** %9
br label %140
140: ; preds = %136, %119
br label %141
141: ; preds = %140
%142 = load i32, i32* %11
%143 = add nsw i32 %142, 1
store i32 %143, i32* %11
br label %40
144: ; preds = %40
%145 = load double*, double** %9
%146 = call i32 @free(double* %145)
%147 = load double*, double** %8
store double* %147, double** %4
br label %148
148: ; preds = %144, %34, %23
%149 = load double*, double** %4
ret double* %149
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [39 x i8] c"FATAL: couldn't allocate memory at %d\0A\00"
define noalias double* @naive_calc_prob_dist(i32* nocapture %0, i32 %1, i32 %2) {
%4 = shl i32 %1, 3
%5 = add i32 %4, 8
%6 = tail call double* @malloc(i32 %5)
%7 = icmp eq double* %6, null
br i1 %7, label %8, label %11
8: ; preds = %3
%9 = load i32, i32* @stderr
%10 = tail call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0), i32 133)
br label %78
11: ; preds = %3
%12 = tail call double* @malloc(i32 %5)
%13 = icmp eq double* %12, null
br i1 %13, label %14, label %17
14: ; preds = %11
%15 = load i32, i32* @stderr
%16 = tail call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0), i32 138)
br label %78
17: ; preds = %11
store double 0.000000e+00, double* %12
%18 = icmp slt i32 %1, 1
br i1 %18, label %74, label %19
19: ; preds = %17
%20 = zext i32 %1 to i64
%21 = add nuw i32 %1, 1
%22 = zext i32 %21 to i64
br label %23
23: ; preds = %19, %64
%24 = phi i64 [ 1, %19 ], [ %72, %64 ]
%25 = phi double* [ %6, %19 ], [ %71, %64 ]
%26 = phi double* [ %12, %19 ], [ %70, %64 ]
%27 = add nsw i64 %24, -1
%28 = getelementptr inbounds i32, i32* %0, i64 %27
%29 = load i32, i32* %28
%30 = sitofp i32 %29 to double
%31 = fdiv double %30, -1.000000e+01
%32 = tail call double @pow(double 1.000000e+01, double %31)
%33 = tail call double @log(double %32)
%34 = fneg double %32
%35 = tail call double @log1p(double %34)
%36 = load i32, i32* %28
%37 = add i32 %36, -2
%38 = icmp ult i32 %37, 98
%39 = zext i1 %38 to i32
%40 = tail call i32 @assert(i32 %39)
%41 = load double, double* %26
%42 = fadd double %35, %41
store double %42, double* %25
%43 = icmp ugt i64 %24, 1
br i1 %43, label %44, label %64
44: ; preds = %23
%45 = getelementptr inbounds double, double* %26, i64 1
%46 = load double, double* %45
%47 = fadd double %35, %46
%48 = fadd double %33, %41
%49 = tail call double @log_sum(double %47, double %48)
%50 = getelementptr inbounds double, double* %25, i64 1
store double %49, double* %50
%51 = icmp eq i64 %24, 2
br i1 %51, label %64, label %52
52: ; preds = %44, %52
%53 = phi i64 [ %62, %52 ], [ 2, %44 ]
%54 = phi double* [ %56, %52 ], [ %45, %44 ]
%55 = load double, double* %54
%56 = getelementptr inbounds double, double* %26, i64 %53
%57 = load double, double* %56
%58 = fadd double %35, %57
%59 = fadd double %33, %55
%60 = tail call double @log_sum(double %58, double %59)
%61 = getelementptr inbounds double, double* %25, i64 %53
store double %60, double* %61
%62 = add nuw nsw i64 %53, 1
%63 = icmp eq i64 %62, %24
br i1 %63, label %64, label %52
64: ; preds = %52, %44, %23
%65 = getelementptr inbounds double, double* %26, i64 %27
%66 = load double, double* %65
%67 = fadd double %33, %66
%68 = getelementptr inbounds double, double* %25, i64 %24
store double %67, double* %68
%69 = icmp ult i64 %24, %20
%70 = select i1 %69, double* %25, double* %26
%71 = select i1 %69, double* %26, double* %25
%72 = add nuw nsw i64 %24, 1
%73 = icmp eq i64 %72, %22
br i1 %73, label %74, label %23
74: ; preds = %64, %17
%75 = phi double* [ %12, %17 ], [ %70, %64 ]
%76 = phi double* [ %6, %17 ], [ %71, %64 ]
%77 = tail call i32 @free(double* %75)
br label %78
78: ; preds = %74, %14, %8
%79 = phi double* [ null, %8 ], [ null, %14 ], [ %76, %74 ]
ret double* %79
}