// File: 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:
subq $0x1d8, %rsp            # imm = 0x1D8
movq %rcx, 0xb0(%rsp)
movq %rdi, %rax
movq 0xb0(%rsp), %rdi
movq %rdi, 0xc0(%rsp)
movq %r8, 0xb8(%rsp)
movq %rax, 0x1d0(%rsp)
movq %rsi, 0x1c8(%rsp)
movq %rdx, 0x1c0(%rsp)
movq %rdi, 0x1b8(%rsp)
movq %r8, 0x1b0(%rsp)
movl %r9d, 0x1ac(%rsp)
callq 0x9b80
movq 0xc0(%rsp), %rdi
movl %eax, 0x1a8(%rsp)
callq 0xc880
movq 0xc0(%rsp), %rdi
movq %rax, 0x190(%rsp)
callq 0xc8b0
movq %rax, 0x188(%rsp)
movq 0x190(%rsp), %rdi
movq 0x188(%rsp), %rsi
callq 0x21080
movq %rax, 0x198(%rsp)
leaq 0x198(%rsp), %rdi
callq 0xc8e0
movsd (%rax), %xmm0
movsd %xmm0, 0x1a0(%rsp)
movl $0x0, 0x184(%rsp)
movl 0x184(%rsp), %eax
cmpl 0x1a8(%rsp), %eax
jge 0x33fc1
movq 0xc0(%rsp), %rdi
movsd 0x1a0(%rsp), %xmm0
movsd %xmm0, 0xa8(%rsp)
movslq 0x184(%rsp), %rsi
callq 0x8c20
movsd 0xa8(%rsp), %xmm1
movsd (%rax), %xmm0
subsd %xmm1, %xmm0
movsd %xmm0, (%rax)
movl 0x184(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x184(%rsp)
jmp 0x33f62
movslq 0x1a8(%rsp), %rax
movq %rax, 0x98(%rsp)
leaq 0x167(%rsp), %rdi
movq %rdi, 0xa0(%rsp)
callq 0x8780
movq 0x98(%rsp), %rsi
movq 0xa0(%rsp), %rdx
leaq 0x168(%rsp), %rdi
callq 0x8790
jmp 0x34005
leaq 0x167(%rsp), %rdi
callq 0x8910
movslq 0x1a8(%rsp), %rax
movq %rax, 0x88(%rsp)
leaq 0x137(%rsp), %rdi
movq %rdi, 0x90(%rsp)
callq 0x8780
movq 0x88(%rsp), %rsi
movq 0x90(%rsp), %rdx
leaq 0x138(%rsp), %rdi
callq 0x8790
jmp 0x34056
leaq 0x137(%rsp), %rdi
callq 0x8910
movslq 0x1a8(%rsp), %rax
movq %rax, 0x70(%rsp)
movslq 0x1ac(%rsp), %rax
movq %rax, 0x78(%rsp)
leaq 0xf7(%rsp), %rdi
movq %rdi, 0x80(%rsp)
callq 0x8780
movq 0x78(%rsp), %rsi
movq 0x80(%rsp), %rdx
leaq 0xf8(%rsp), %rdi
callq 0x8790
jmp 0x340ae
leaq 0xf6(%rsp), %rdi
movq %rdi, 0x68(%rsp)
callq 0x8810
movq 0x70(%rsp), %rsi
movq 0x68(%rsp), %rcx
leaq 0x110(%rsp), %rdi
leaq 0xf8(%rsp), %rdx
callq 0x8820
jmp 0x340e1
leaq 0xf6(%rsp), %rdi
callq 0x88a0
leaq 0xf8(%rsp), %rdi
callq 0x88b0
leaq 0xf7(%rsp), %rdi
callq 0x8910
movl $0x0, 0xf0(%rsp)
movl 0xf0(%rsp), %eax
cmpl 0x1ac(%rsp), %eax
jge 0x3440c
xorps %xmm0, %xmm0
movsd %xmm0, 0x128(%rsp)
movl $0x0, 0xec(%rsp)
movl 0xec(%rsp), %eax
cmpl 0x1a8(%rsp), %eax
jge 0x34308
movq 0xc0(%rsp), %rdi
movslq 0xec(%rsp), %rsi
callq 0x8c20
movq 0xb8(%rsp), %rdi
movsd (%rax), %xmm0
movsd %xmm0, 0x58(%rsp)
movslq 0xec(%rsp), %rsi
callq 0x8c20
movsd 0x58(%rsp), %xmm0
movsd (%rax), %xmm1
callq 0x39310
movsd %xmm0, 0x60(%rsp)
jmp 0x3419d
movslq 0xec(%rsp), %rsi
leaq 0x168(%rsp), %rdi
callq 0x8c20
movsd 0x60(%rsp), %xmm0
movsd %xmm0, (%rax)
movslq 0xec(%rsp), %rsi
leaq 0x168(%rsp), %rdi
callq 0x8c20
movsd (%rax), %xmm0
callq 0x4760
movsd %xmm0, 0x50(%rsp)
movslq 0xec(%rsp), %rsi
leaq 0x138(%rsp), %rdi
callq 0x8c20
movsd 0x50(%rsp), %xmm0
movsd %xmm0, (%rax)
movslq 0xec(%rsp), %rsi
leaq 0x138(%rsp), %rdi
callq 0x8c20
movsd (%rax), %xmm0
addsd 0x128(%rsp), %xmm0
movsd %xmm0, 0x128(%rsp)
movl 0xec(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xec(%rsp)
jmp 0x3413e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x158(%rsp)
movl %eax, 0x154(%rsp)
leaq 0x167(%rsp), %rdi
callq 0x8910
jmp 0x3464b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x158(%rsp)
movl %eax, 0x154(%rsp)
leaq 0x137(%rsp), %rdi
callq 0x8910
jmp 0x3463e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x158(%rsp)
movl %eax, 0x154(%rsp)
jmp 0x342d0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x158(%rsp)
movl %eax, 0x154(%rsp)
leaq 0xf6(%rsp), %rdi
callq 0x88a0
leaq 0xf8(%rsp), %rdi
callq 0x88b0
leaq 0xf7(%rsp), %rdi
callq 0x8910
jmp 0x34631
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x158(%rsp)
movl %eax, 0x154(%rsp)
leaq 0x110(%rsp), %rdi
callq 0x8b40
jmp 0x34631
movl $0x0, 0xe8(%rsp)
movl 0xe8(%rsp), %eax
cmpl 0x1a8(%rsp), %eax
jge 0x343f4
movslq 0xe8(%rsp), %rsi
leaq 0x138(%rsp), %rdi
callq 0x8c20
movsd (%rax), %xmm0
divsd 0x128(%rsp), %xmm0
movsd %xmm0, 0x48(%rsp)
movslq 0xe8(%rsp), %rsi
leaq 0x110(%rsp), %rdi
callq 0x8bc0
movq %rax, %rdi
movslq 0xf0(%rsp), %rsi
callq 0x8c20
movsd 0x48(%rsp), %xmm0
movsd %xmm0, (%rax)
movsd 0x128(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x343dc
jp 0x343dc
cvtsi2sdl 0x1a8(%rsp), %xmm1
movsd 0x27e5d(%rip), %xmm0    # 0x5c200
divsd %xmm1, %xmm0
movsd %xmm0, 0x40(%rsp)
movslq 0xe8(%rsp), %rsi
leaq 0x110(%rsp), %rdi
callq 0x8bc0
movq %rax, %rdi
movslq 0xf0(%rsp), %rsi
callq 0x8c20
movsd 0x40(%rsp), %xmm0
movsd %xmm0, (%rax)
jmp 0x343de
movl 0xe8(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xe8(%rsp)
jmp 0x34313
jmp 0x343f6
movl 0xf0(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xf0(%rsp)
jmp 0x34113
movl $0x0, 0xe4(%rsp)
movl 0xe4(%rsp), %eax
cmpl 0x1a8(%rsp), %eax
jge 0x344a2
movslq 0xe4(%rsp), %rsi
leaq 0x110(%rsp), %rdi
movq %rdi, 0x38(%rsp)
callq 0x8bc0
movq %rax, %rdi
callq 0xde70
movq 0x38(%rsp), %rdi
movq %rax, 0xd8(%rsp)
movslq 0xe4(%rsp), %rsi
callq 0x8bc0
movq %rax, %rdi
callq 0xdec0
movq %rax, 0xd0(%rsp)
movq 0xd8(%rsp), %rdi
movq 0xd0(%rsp), %rsi
callq 0x36e60
jmp 0x3448a
jmp 0x3448c
movl 0xe4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xe4(%rsp)
jmp 0x34417
movl $0x0, 0xcc(%rsp)
movl 0xcc(%rsp), %eax
movl %eax, 0x34(%rsp)
leaq 0x168(%rsp), %rdi
callq 0x9b80
movq %rax, %rcx
movl 0x34(%rsp), %eax
cmpl %ecx, %eax
jge 0x34602
movslq 0xcc(%rsp), %rsi
leaq 0x110(%rsp), %rdi
callq 0x8bc0
movq %rax, %rdi
callq 0x33380
movsd %xmm0, 0x28(%rsp)
jmp 0x344f9
movq 0x1d0(%rsp), %rdi
movslq 0xcc(%rsp), %rsi
callq 0x8c20
movsd 0x28(%rsp), %xmm0
movsd %xmm0, (%rax)
movslq 0xcc(%rsp), %rsi
leaq 0x110(%rsp), %rdi
callq 0x8bc0
movq %rax, 0x8(%rsp)
cvtsi2sdl 0x1ac(%rsp), %xmm0
movsd 0x285dd(%rip), %xmm1    # 0x5cb20
mulsd %xmm1, %xmm0
callq 0x33950
movq 0x8(%rsp), %rdi
movslq %eax, %rsi
callq 0x8c20
movsd (%rax), %xmm0
movsd %xmm0, 0x10(%rsp)
movq 0x1c8(%rsp), %rdi
movslq 0xcc(%rsp), %rsi
callq 0x8c20
movsd 0x10(%rsp), %xmm0
movsd %xmm0, (%rax)
movslq 0xcc(%rsp), %rsi
leaq 0x110(%rsp), %rdi
callq 0x8bc0
movq %rax, 0x18(%rsp)
cvtsi2sdl 0x1ac(%rsp), %xmm0
movsd 0x2856b(%rip), %xmm1    # 0x5cb18
mulsd %xmm1, %xmm0
callq 0x33950
movq 0x18(%rsp), %rdi
movslq %eax, %rsi
callq 0x8c20
movsd (%rax), %xmm0
movsd %xmm0, 0x20(%rsp)
movq 0x1c0(%rsp), %rdi
movslq 0xcc(%rsp), %rsi
callq 0x8c20
movsd 0x20(%rsp), %xmm0
movsd %xmm0, (%rax)
movl 0xcc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xcc(%rsp)
jmp 0x344ad
leaq 0x110(%rsp), %rdi
callq 0x8b40
leaq 0x138(%rsp), %rdi
callq 0x88b0
leaq 0x168(%rsp), %rdi
callq 0x88b0
addq $0x1d8, %rsp            # imm = 0x1D8
retq
leaq 0x138(%rsp), %rdi
callq 0x88b0
leaq 0x168(%rsp), %rdi
callq 0x88b0
movq 0x158(%rsp), %rdi
callq 0x4730
nopl (%rax,%rax)