// File: /workspace/github_cpp_makefile_star2_O2/bobverity[P]MavericK/misc.cpp
// Function: normalise_log_sim(std::vector<double, std::allocator<double>>&, std::vector<double, std::allocator<double>>&, std::vector<double, std::allocator<double>>&, std::vector<double, std::allocator<double>>, std::vector<double, std::allocator<double>>, int)
void normalise_log_sim(vector<double> &normMean, vector<double> &LL, vector<double> &UL, vector<double> x_mean, vector<double> x_sd, int draws) {
    
    // subtract maximum value from x_mean (this has no effect on final outcome but prevents under/overflow)
    int n = int(x_mean.size());
    double maxVal = *max_element(begin(x_mean),end(x_mean));
    for (int j=0; j<n; j++) {
        x_mean[j] -= maxVal;
    }
    
    // draw random values of Z (exponentiated and normalised draws)
    vector<double> X(n);
    vector<double> Y(n);
    double Ysum;
    vector< vector<double> > Z(n,vector<double>(draws));
    for (int i=0; i<draws; i++) {
        Ysum = 0;
        for (int j=0; j<n; j++) {
            X[j] = rnorm1(x_mean[j], x_sd[j]);
            Y[j] = exp(X[j]);
            Ysum += Y[j];
        }
        for (int j=0; j<n; j++) {
            Z[j][i] = Y[j]/Ysum;
            if (Ysum==0)
                Z[j][i] = 1/double(n);
        }
    }
    
    // sort values of Z
    for (int j=0; j<n; j++) {
        sort(Z[j].begin(),Z[j].end());
    }
    
    // store mean and 95% CI values
    for (int j=0; j<int(X.size()); j++) {
        //median[j] = Z[j][round(draws*0.5)];
        normMean[j] = mean(Z[j]);
        LL[j] = Z[j][my_round(draws*0.025)];
        UL[j] = Z[j][my_round(draws*0.975)];
    }
    
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xd8, %rsp
movl %r9d, %r14d
movq %r8, %r12
movq %rcx, %r13
movq %rdx, 0x80(%rsp)
movq %rsi, 0x78(%rsp)
movq %rdi, 0x88(%rsp)
movq (%rcx), %rdi
movq 0x8(%rcx), %rsi
movq %rsi, %rbx
subq %rdi, %rbx
shrq $0x3, %rbx
callq 0xc694
movsd (%rax), %xmm0
xorl %eax, %eax
testl %ebx, %ebx
movl $0x0, %ebp
cmovgl %ebx, %ebp
cmpq %rax, %rbp
je 0x14f2d
movq (%r13), %rcx
movsd (%rcx,%rax,8), %xmm1
subsd %xmm0, %xmm1
movsd %xmm1, (%rcx,%rax,8)
incq %rax
jmp 0x14f11
movslq %ebx, %r15
leaq 0xa8(%rsp), %rdi
leaq 0x90(%rsp), %rdx
movq %r15, %rsi
callq 0x4f6c
leaq 0x90(%rsp), %rdi
leaq 0x38(%rsp), %rdx
movq %r15, %rsi
callq 0x4f6c
movslq %r14d, %rsi
leaq 0xc0(%rsp), %rdi
leaq 0xf(%rsp), %rdx
callq 0x4f6c
leaq 0x38(%rsp), %rdi
leaq 0xc0(%rsp), %rdx
leaq 0xe(%rsp), %rcx
movq %r15, %rsi
callq 0x4fbe
leaq 0xc0(%rsp), %rdi
callq 0x51a2
cvtsi2sd %ebx, %xmm0
movsd 0x1226b(%rip), %xmm1    # 0x27210
divsd %xmm0, %xmm1
movapd %xmm1, 0x10(%rsp)
xorl %ebx, %ebx
testl %r14d, %r14d
movl %r14d, %eax
movl $0x0, %r14d
movl %eax, 0x60(%rsp)
cmovgl %eax, %r14d
cmpq %r14, %rbx
je 0x15082
xorpd %xmm4, %xmm4
xorl %r15d, %r15d
cmpq %r15, %rbp
je 0x15029
movsd %xmm4, 0x20(%rsp)
movq (%r13), %rax
movsd (%rax,%r15,8), %xmm0
movq (%r12), %rax
movsd (%rax,%r15,8), %xmm1
callq 0x17188
movq 0xa8(%rsp), %rax
movsd %xmm0, (%rax,%r15,8)
callq 0x35c0
movq 0x90(%rsp), %rax
movsd %xmm0, (%rax,%r15,8)
movsd 0x20(%rsp), %xmm4
addsd %xmm0, %xmm4
incq %r15
jmp 0x14fd5
movq 0x90(%rsp), %rax
movq 0x38(%rsp), %rcx
movapd %xmm4, %xmm0
cmpeqsd 0x12035(%rip), %xmm0    # 0x27078
movapd %xmm0, %xmm1
andpd 0x10(%rsp), %xmm1
xorl %edx, %edx
cmpq %rdx, %rbp
je 0x1507a
movsd (%rax,%rdx,8), %xmm2
divsd %xmm4, %xmm2
movq (%rcx), %rsi
movapd %xmm0, %xmm3
andnpd %xmm2, %xmm3
orpd %xmm1, %xmm3
movlpd %xmm3, (%rsi,%rbx,8)
incq %rdx
addq $0x18, %rcx
jmp 0x1504f
incq %rbx
jmp 0x14fc5
imulq $0x18, %rbp, %rbx
xorl %r14d, %r14d
movq 0x80(%rsp), %r15
movq 0x78(%rsp), %rbp
cmpq %r14, %rbx
je 0x150b4
movq 0x38(%rsp), %rax
movq (%rax,%r14), %rdi
movq 0x8(%rax,%r14), %rsi
callq 0x16a01
addq $0x18, %r14
jmp 0x15096
cvtsi2sdl 0x60(%rsp), %xmm2
unpcklpd %xmm2, %xmm2            # xmm2 = xmm2[0,0]
mulpd 0x12aca(%rip), %xmm2    # 0x27b90
movapd %xmm2, 0x10(%rsp)
xorpd %xmm0, %xmm0
movapd %xmm2, %xmm1
cmpltpd %xmm0, %xmm1
movapd %xmm1, 0x60(%rsp)
movapd 0x12ab9(%rip), %xmm0    # 0x27ba0
addpd %xmm2, %xmm0
movapd %xmm0, 0x50(%rsp)
callq 0x3530
movapd %xmm0, 0x20(%rsp)
movaps 0x50(%rsp), %xmm0
movhlps %xmm0, %xmm0            # xmm0 = xmm0[1,1]
callq 0x3530
movaps 0x20(%rsp), %xmm1
movlhps %xmm0, %xmm1            # xmm1 = xmm1[0],xmm0[0]
movaps %xmm1, 0x20(%rsp)
movapd 0x10(%rsp), %xmm0
addpd 0x12a8c(%rip), %xmm0    # 0x27bb0
movapd %xmm0, 0x10(%rsp)
callq 0x3070
movapd %xmm0, 0x50(%rsp)
movapd 0x10(%rsp), %xmm0
unpckhpd %xmm0, %xmm0            # xmm0 = xmm0[1,1]
callq 0x3070
movapd 0x50(%rsp), %xmm1
unpcklpd %xmm0, %xmm1            # xmm1 = xmm1[0],xmm0[0]
movapd 0x60(%rsp), %xmm0
andpd %xmm0, %xmm1
andnpd 0x20(%rsp), %xmm0
orpd %xmm1, %xmm0
cvttpd2dq %xmm0, %xmm0
movd %xmm0, %eax
pshufd $0x55, %xmm0, %xmm0     # xmm0 = xmm0[1,1,1,1]
movd %xmm0, %ecx
movslq %eax, %rbx
movslq %ecx, %r14
xorl %r12d, %r12d
xorl %r13d, %r13d
movq 0xb0(%rsp), %rax
subq 0xa8(%rsp), %rax
shrq $0x3, %rax
cltq
cmpq %rax, %r13
jge 0x151e8
movq 0x38(%rsp), %rdi
addq %r12, %rdi
callq 0x14ada
movq 0x88(%rsp), %rax
movq (%rax), %rax
movq %xmm0, (%rax,%r13,8)
movq 0x38(%rsp), %rax
movq (%rax,%r12), %rax
movsd (%rax,%rbx,8), %xmm0
movq (%rbp), %rcx
movsd %xmm0, (%rcx,%r13,8)
movq (%rax,%r14,8), %xmm0
movq (%r15), %rax
movq %xmm0, (%rax,%r13,8)
incq %r13
addq $0x18, %r12
jmp 0x1517f
leaq 0x38(%rsp), %rdi
callq 0x50da
leaq 0x90(%rsp), %rdi
callq 0x51a2
leaq 0xa8(%rsp), %rdi
callq 0x51a2
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq 0xc0(%rsp), %rdi
callq 0x51a2
jmp 0x1524b
movq %rax, %rbx
jmp 0x1524b
movq %rax, %rbx
jmp 0x15258
jmp 0x1523e
jmp 0x1523e
movq %rax, %rbx
leaq 0x38(%rsp), %rdi
callq 0x50da
leaq 0x90(%rsp), %rdi
callq 0x51a2
leaq 0xa8(%rsp), %rdi
callq 0x51a2
movq %rbx, %rdi
callq 0x35a0