// File: /workspace/github_cpp_makefile_star2_O3/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, %ebp
movq %r8, %r12
movq %rcx, %r13
movq %rdx, %r14
movq %rsi, 0x88(%rsp)
movq %rdi, 0x80(%rsp)
movq (%rcx), %rax
movq 0x8(%rcx), %rcx
movq %rcx, %r15
subq %rax, %r15
sete %sil
shrq $0x3, %r15
movq %rax, %rdx
addq $0x8, %rdx
cmpq %rcx, %rdx
sete %dil
orb %sil, %dil
movq %rax, %rsi
jne 0x1aa3a
movsd (%rax), %xmm0
movq %rax, %rsi
movsd (%rdx), %xmm1
ucomisd %xmm0, %xmm1
cmovaq %rdx, %rsi
maxsd %xmm0, %xmm1
addq $0x8, %rdx
movapd %xmm1, %xmm0
cmpq %rcx, %rdx
jne 0x1aa1d
testl %r15d, %r15d
jle 0x1aa64
movsd (%rsi), %xmm0
movl %r15d, %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
xorl %edx, %edx
movsd (%rax,%rdx,8), %xmm1
subsd %xmm0, %xmm1
movsd %xmm1, (%rax,%rdx,8)
incq %rdx
cmpq %rdx, %rcx
jne 0x1aa4e
movslq %r15d, %rbx
leaq 0xa8(%rsp), %rdi
leaq 0x90(%rsp), %rdx
movq %rbx, %rsi
callq 0x588e
leaq 0x90(%rsp), %rdi
leaq 0x48(%rsp), %rdx
movq %rbx, %rsi
callq 0x588e
movslq %ebp, %rsi
leaq 0xc0(%rsp), %rdi
leaq 0xf(%rsp), %rdx
callq 0x588e
leaq 0x48(%rsp), %rdi
leaq 0xc0(%rsp), %rdx
leaq 0xe(%rsp), %rcx
movq %rbx, %rsi
callq 0x591c
movq %r14, 0x78(%rsp)
movq 0xc0(%rsp), %rdi
testq %rdi, %rdi
je 0x1aada
callq 0x3310
movl %ebp, 0x10(%rsp)
testl %ebp, %ebp
jle 0x1abd3
xorps %xmm0, %xmm0
cvtsi2sd %r15d, %xmm0
movsd 0x1b76a(%rip), %xmm1    # 0x36260
divsd %xmm0, %xmm1
movapd %xmm1, 0x60(%rsp)
movl 0x10(%rsp), %eax
movq %rax, 0x20(%rsp)
movl %r15d, %r14d
andl $0x7fffffff, %r14d      # imm = 0x7FFFFFFF
xorl %ebp, %ebp
testl %r15d, %r15d
jle 0x1abc5
xorps %xmm4, %xmm4
xorl %ebx, %ebx
movsd %xmm4, 0x30(%rsp)
movq (%r13), %rax
movsd (%rax,%rbx,8), %xmm0
movq (%r12), %rax
movsd (%rax,%rbx,8), %xmm1
callq 0x1d34e
movq 0xa8(%rsp), %rax
movsd %xmm0, (%rax,%rbx,8)
callq 0x35a0
movq 0x90(%rsp), %rax
movsd %xmm0, (%rax,%rbx,8)
movsd 0x30(%rsp), %xmm4
addsd %xmm0, %xmm4
incq %rbx
cmpq %rbx, %r14
jne 0x1ab23
testl %r15d, %r15d
jle 0x1abc5
movq 0x90(%rsp), %rax
movq 0x48(%rsp), %rcx
movapd %xmm4, %xmm0
cmpeqsd 0x1b518(%rip), %xmm0    # 0x360a8
movapd %xmm0, %xmm1
andpd 0x60(%rsp), %xmm1
xorl %edx, %edx
movsd (%rax,%rdx,8), %xmm2
divsd %xmm4, %xmm2
movq (%rcx), %rsi
movapd %xmm0, %xmm3
andnpd %xmm2, %xmm3
orpd %xmm1, %xmm3
movlpd %xmm3, (%rsi,%rbp,8)
incq %rdx
addq $0x18, %rcx
cmpq %rdx, %r14
jne 0x1ab9c
incq %rbp
cmpq 0x20(%rsp), %rbp
jne 0x1ab15
testl %r15d, %r15d
movl 0x10(%rsp), %ebp
jle 0x1ac32
andl $0x7fffffff, %r15d      # imm = 0x7FFFFFFF
movl $0x8, %r14d
movq 0x48(%rsp), %rax
movq -0x8(%rax,%r14), %rbx
movq (%rax,%r14), %r12
cmpq %r12, %rbx
je 0x1ac29
movq %r12, %rax
subq %rbx, %rax
sarq $0x3, %rax
bsrq %rax, %rdx
xorl $0x3f, %edx
addl %edx, %edx
xorq $0x7e, %rdx
movq %rbx, %rdi
movq %r12, %rsi
callq 0x1cbcd
movq %rbx, %rdi
movq %r12, %rsi
callq 0x1ccc6
addq $0x18, %r14
decq %r15
jne 0x1abe9
movq 0xb0(%rsp), %rbx
subq 0xa8(%rsp), %rbx
shrq $0x3, %rbx
testl %ebx, %ebx
jle 0x1add6
movq 0x48(%rsp), %r14
xorps %xmm1, %xmm1
cvtsi2sd %ebp, %xmm1
movq 0x80(%rsp), %rax
movq (%rax), %r15
movq 0x88(%rsp), %rax
movq (%rax), %r12
movq 0x78(%rsp), %rax
movq (%rax), %r13
andl $0x7fffffff, %ebx       # imm = 0x7FFFFFFF
unpcklpd %xmm1, %xmm1            # xmm1 = xmm1[0,0]
mulpd 0x1bf76(%rip), %xmm1    # 0x36c00
movapd %xmm1, 0x20(%rsp)
xorpd %xmm0, %xmm0
movapd %xmm1, %xmm2
cmpltpd %xmm0, %xmm2
movapd %xmm2, 0x60(%rsp)
movapd 0x1bf65(%rip), %xmm0    # 0x36c10
addpd %xmm1, %xmm0
movapd %xmm0, 0x10(%rsp)
callq 0x3500
movapd %xmm0, 0x30(%rsp)
movaps 0x10(%rsp), %xmm0
movhlps %xmm0, %xmm0            # xmm0 = xmm0[1,1]
callq 0x3500
movaps 0x30(%rsp), %xmm1
movlhps %xmm0, %xmm1            # xmm1 = xmm1[0],xmm0[0]
movaps %xmm1, 0x30(%rsp)
movapd 0x20(%rsp), %xmm0
addpd 0x1bf38(%rip), %xmm0    # 0x36c20
movapd %xmm0, 0x20(%rsp)
callq 0x3070
movapd %xmm0, 0x10(%rsp)
movapd 0x20(%rsp), %xmm0
unpckhpd %xmm0, %xmm0            # xmm0 = xmm0[1,1]
callq 0x3070
movapd 0x10(%rsp), %xmm1
unpcklpd %xmm0, %xmm1            # xmm1 = xmm1[0],xmm0[0]
movapd 0x60(%rsp), %xmm0
andpd %xmm0, %xmm1
andnpd 0x30(%rsp), %xmm0
orpd %xmm1, %xmm0
cvttpd2dq %xmm0, %xmm0
movd %xmm0, %eax
pshufd $0x55, %xmm0, %xmm0     # xmm0 = xmm0[1,1,1,1]
movd %xmm0, %ecx
cltq
movslq %ecx, %rcx
xorl %edx, %edx
movq 0x1be3a(%rip), %xmm0    # 0x36b80
movapd 0x1be42(%rip), %xmm1    # 0x36b90
leaq (%rdx,%rdx,2), %rdi
movq (%r14,%rdi,8), %rsi
movq 0x8(%r14,%rdi,8), %rdi
subq %rsi, %rdi
movq %rdi, %r8
shrq $0x3, %r8
testl %r8d, %r8d
jle 0x1ad88
andl $0x7fffffff, %r8d       # imm = 0x7FFFFFFF
xorpd %xmm2, %xmm2
xorl %r9d, %r9d
addsd (%rsi,%r9,8), %xmm2
incq %r9
cmpq %r9, %r8
jne 0x1ad78
jmp 0x1ad8c
xorpd %xmm2, %xmm2
sarq $0x3, %rdi
movq %rdi, %xmm3
punpckldq %xmm0, %xmm3    # xmm3 = xmm3[0],xmm0[0],xmm3[1],xmm0[1]
subpd %xmm1, %xmm3
movapd %xmm3, %xmm4
unpckhpd %xmm3, %xmm4            # xmm4 = xmm4[1],xmm3[1]
addsd %xmm3, %xmm4
divsd %xmm4, %xmm2
movsd %xmm2, (%r15,%rdx,8)
movsd (%rsi,%rax,8), %xmm2
movsd %xmm2, (%r12,%rdx,8)
movsd (%rsi,%rcx,8), %xmm2
movsd %xmm2, (%r13,%rdx,8)
incq %rdx
cmpq %rbx, %rdx
jne 0x1ad4e
leaq 0x48(%rsp), %rdi
callq 0x5ae8
movq 0x90(%rsp), %rdi
testq %rdi, %rdi
je 0x1adf2
callq 0x3310
movq 0xa8(%rsp), %rdi
testq %rdi, %rdi
je 0x1ae04
callq 0x3310
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movq 0xc0(%rsp), %rdi
testq %rdi, %rdi
je 0x1ae46
callq 0x3310
jmp 0x1ae46
movq %rax, %rbx
jmp 0x1ae46
movq %rax, %rbx
jmp 0x1ae58
jmp 0x1ae39
movq %rax, %rbx
leaq 0x48(%rsp), %rdi
callq 0x5ae8
movq 0x90(%rsp), %rdi
testq %rdi, %rdi
je 0x1ae58
callq 0x3310
movq 0xa8(%rsp), %rdi
testq %rdi, %rdi
je 0x1ae6a
callq 0x3310
movq %rbx, %rdi
callq 0x3580