// File: /workspace/github_cpp_makefile_star2_O1/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 0x1ad3c
movq %rax, %rdi
movsd (%rdx), %xmm0
ucomisd (%rdi), %xmm0
movq %rdx, %rsi
ja 0x1ad30
movq %rdi, %rsi
addq $0x8, %rdx
movq %rsi, %rdi
cmpq %rcx, %rdx
jne 0x1ad20
testl %r15d, %r15d
jle 0x1ad66
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 0x1ad50
movslq %r15d, %rbx
leaq 0xa8(%rsp), %rdi
leaq 0x90(%rsp), %rdx
movq %rbx, %rsi
callq 0x5916
leaq 0x90(%rsp), %rdi
leaq 0x48(%rsp), %rdx
movq %rbx, %rsi
callq 0x5916
movslq %ebp, %rsi
leaq 0xc0(%rsp), %rdi
leaq 0xf(%rsp), %rdx
callq 0x5916
leaq 0x48(%rsp), %rdi
leaq 0xc0(%rsp), %rdx
leaq 0xe(%rsp), %rcx
movq %rbx, %rsi
callq 0x59a0
movq %r14, 0x78(%rsp)
movq 0xc0(%rsp), %rdi
testq %rdi, %rdi
je 0x1addc
callq 0x3310
movl %ebp, 0x10(%rsp)
testl %ebp, %ebp
jle 0x1aed2
xorps %xmm0, %xmm0
cvtsi2sd %r15d, %xmm0
movsd 0x1c468(%rip), %xmm1    # 0x37260
divsd %xmm0, %xmm1
movapd %xmm1, 0x60(%rsp)
movl 0x10(%rsp), %eax
movq %rax, 0x30(%rsp)
movl %r15d, %r14d
andl $0x7fffffff, %r14d      # imm = 0x7FFFFFFF
xorl %ebp, %ebp
xorpd %xmm4, %xmm4
testl %r15d, %r15d
jle 0x1ae70
xorl %ebx, %ebx
movsd %xmm4, 0x20(%rsp)
movq (%r13), %rax
movsd (%rax,%rbx,8), %xmm0
movq (%r12), %rax
movsd (%rax,%rbx,8), %xmm1
callq 0x1d6a2
movq 0xa8(%rsp), %rax
movsd %xmm0, (%rax,%rbx,8)
callq 0x35a0
movq 0x90(%rsp), %rax
movsd %xmm0, (%rax,%rbx,8)
movsd 0x20(%rsp), %xmm4
addsd %xmm0, %xmm4
incq %rbx
cmpq %rbx, %r14
jne 0x1ae22
testl %r15d, %r15d
jle 0x1aec4
movq 0x90(%rsp), %rax
movq 0x48(%rsp), %rcx
movapd %xmm4, %xmm0
cmpeqsd 0x1c219(%rip), %xmm0    # 0x370a8
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 0x1ae9b
incq %rbp
cmpq 0x30(%rsp), %rbp
jne 0x1ae17
testl %r15d, %r15d
movl 0x10(%rsp), %ebp
jle 0x1af31
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 0x1af28
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 0x1ceff
movq %rbx, %rdi
movq %r12, %rsi
callq 0x1cff8
addq $0x18, %r14
decq %r15
jne 0x1aee8
movq 0xb0(%rsp), %rbx
subq 0xa8(%rsp), %rbx
shrq $0x3, %rbx
testl %ebx, %ebx
jle 0x1b0cc
movq 0x48(%rsp), %r14
movq 0x80(%rsp), %rax
movq (%rax), %r15
xorps %xmm1, %xmm1
cvtsi2sd %ebp, %xmm1
movq 0x88(%rsp), %rax
movq (%rax), %r12
unpcklpd %xmm1, %xmm1            # xmm1 = xmm1[0,0]
mulpd 0x1cc85(%rip), %xmm1    # 0x37c00
movapd 0x1cc8d(%rip), %xmm0    # 0x37c10
addpd %xmm1, %xmm0
movapd %xmm0, 0x10(%rsp)
movapd 0x1cc8b(%rip), %xmm2    # 0x37c20
addpd %xmm1, %xmm2
movapd %xmm2, 0x30(%rsp)
xorpd %xmm2, %xmm2
cmpltpd %xmm2, %xmm1
movapd %xmm1, 0x60(%rsp)
callq 0x3500
movapd %xmm0, 0x20(%rsp)
movaps 0x10(%rsp), %xmm0
movhlps %xmm0, %xmm0            # xmm0 = xmm0[1,1]
callq 0x3500
movaps 0x20(%rsp), %xmm1
movlhps %xmm0, %xmm1            # xmm1 = xmm1[0],xmm0[0]
movaps %xmm1, 0x20(%rsp)
movaps 0x30(%rsp), %xmm0
callq 0x3070
movaps %xmm0, 0x10(%rsp)
movapd 0x30(%rsp), %xmm0
unpckhpd %xmm0, %xmm0            # xmm0 = xmm0[1,1]
callq 0x3070
movapd 0x10(%rsp), %xmm1
unpcklpd %xmm0, %xmm1            # xmm1 = xmm1[0],xmm0[0]
movq 0x78(%rsp), %rax
movq (%rax), %rax
andl $0x7fffffff, %ebx       # imm = 0x7FFFFFFF
movapd 0x60(%rsp), %xmm0
andpd %xmm0, %xmm1
andnpd 0x20(%rsp), %xmm0
orpd %xmm1, %xmm0
cvttpd2dq %xmm0, %xmm0
movd %xmm0, %ecx
movslq %ecx, %rcx
pshufd $0x55, %xmm0, %xmm0     # xmm0 = xmm0[1,1,1,1]
movd %xmm0, %edx
movslq %edx, %rdx
xorl %esi, %esi
movq 0x1cb42(%rip), %xmm0    # 0x37b80
movapd 0x1cb4a(%rip), %xmm1    # 0x37b90
leaq (%rsi,%rsi,2), %r8
movq (%r14,%r8,8), %rdi
movq 0x8(%r14,%r8,8), %r8
subq %rdi, %r8
movq %r8, %r9
shrq $0x3, %r9
testl %r9d, %r9d
jle 0x1b080
andl $0x7fffffff, %r9d       # imm = 0x7FFFFFFF
xorpd %xmm2, %xmm2
xorl %r10d, %r10d
addsd (%rdi,%r10,8), %xmm2
incq %r10
cmpq %r10, %r9
jne 0x1b070
jmp 0x1b084
xorpd %xmm2, %xmm2
sarq $0x3, %r8
movq %r8, %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,%rsi,8)
movsd (%rdi,%rcx,8), %xmm2
movsd %xmm2, (%r12,%rsi,8)
movsd (%rdi,%rdx,8), %xmm2
movsd %xmm2, (%rax,%rsi,8)
incq %rsi
cmpq %rbx, %rsi
jne 0x1b046
leaq 0x48(%rsp), %rdi
callq 0x5b7a
movq 0x90(%rsp), %rdi
testq %rdi, %rdi
je 0x1b0e8
callq 0x3310
movq 0xa8(%rsp), %rdi
testq %rdi, %rdi
je 0x1b0fa
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 0x1b13c
callq 0x3310
jmp 0x1b13c
movq %rax, %rbx
jmp 0x1b13c
movq %rax, %rbx
jmp 0x1b14e
jmp 0x1b12f
movq %rax, %rbx
leaq 0x48(%rsp), %rdi
callq 0x5b7a
movq 0x90(%rsp), %rdi
testq %rdi, %rdi
je 0x1b14e
callq 0x3310
movq 0xa8(%rsp), %rdi
testq %rdi, %rdi
je 0x1b160
callq 0x3310
movq %rbx, %rdi
callq 0x3580