// File: /workspace/github_cpp_makefile_star2_O2/bobverity[P]MavericK/exhaustive.cpp
// Function: exhaustive_admix_fixedAlpha(globals&, int, double)
double exhaustive_admix_fixedAlpha(globals &globals, int Kindex, double alpha) {
    int K = globals.Kmin+Kindex;
    
    // initialise group
    int geneCopies = sum(globals.ploidy_vec)*globals.loci;
    vector<int> group(geneCopies,1);
    vector<int> newGroup = group;
    
    // initialise alleleCounts array
    vector< vector< vector<int> > > alleleCounts(K);
    vector< vector<int> > alleleCountsTotals(K);
    for (int k=0; k<K; k++) {
        alleleCounts[k] = vector< vector<int> >(globals.loci);
        alleleCountsTotals[k] = vector<int>(globals.loci);
        for (int l=0; l<globals.loci; l++) {
            alleleCounts[k][l] = vector<int>(globals.J[l]);
        }
    }
    
    // initialise admixCounts array
    vector< vector<int> > admixCounts(globals.n, vector<int>(K));
    vector<int> admixCountsTotals(globals.n);
    
    // populate alleleCounts and admixCounts arrays and calculate starting likelihood
    double logLike=0;
    int linearIndex=-1;
    for (int ind=0; ind<globals.n; ind++) {
        for (int l=0; l<globals.loci; l++) {
            for (int p=0; p<globals.ploidy_vec[ind]; p++) {
                linearIndex++;
                if (globals.data[ind][l][p]!=0) {
                    logLike += log(alleleCounts[group[linearIndex]-1][l][globals.data[ind][l][p]-1] + globals.lambda) - log(alleleCountsTotals[group[linearIndex]-1][l] + globals.J[l]*globals.lambda);
                    alleleCounts[group[linearIndex]-1][l][globals.data[ind][l][p]-1]++;
                    alleleCountsTotals[group[linearIndex]-1][l]++;
                    
                    logLike += log(admixCounts[ind][group[linearIndex]-1] + alpha) - log(admixCountsTotals[ind] + K*alpha);
                    admixCounts[ind][group[linearIndex]-1]++;
                    admixCountsTotals[ind]++;
                }
            }
        }
    }
    double combined_logLike = logLike + log(double(K));
    
    // loop through all possible groupings
    int uniques = 0;  // number of unique elements in group. Forms part of combinatorial constant needed to calculate correct likelihood when only searching over unique partitions (see Notes.c for details)
    double logLike_total = combined_logLike;
    while (group[0]!=0) {
        
        increment_restrictedGrowth(newGroup,K);
        if (newGroup[0]==0)
            break;
        
        // loop through all gene copies
        linearIndex=-1;
        for (int ind=0; ind<globals.n; ind++) {
            for (int l=0; l<globals.loci; l++) {
                for (int p=0; p<globals.ploidy_vec[ind]; p++) {
                    linearIndex++;
                    
                    // if group allocation for this gene copy has changed
                    if (newGroup[linearIndex]!=group[linearIndex]) {
                        
                        // subtract old group from allele counts, admix counts and likelihood
                        if (globals.data[ind][l][p]!=0) {
                            alleleCountsTotals[group[linearIndex]-1][l]--;
                            alleleCounts[group[linearIndex]-1][l][globals.data[ind][l][p]-1]--;
                            logLike -= log(alleleCounts[group[linearIndex]-1][l][globals.data[ind][l][p]-1] + globals.lambda) - log(alleleCountsTotals[group[linearIndex]-1][l] + globals.J[l]*globals.lambda);
                            
                            admixCountsTotals[ind]--;
                            admixCounts[ind][group[linearIndex]-1]--;
                            logLike -= log(admixCounts[ind][group[linearIndex]-1] + alpha) - log(admixCountsTotals[ind] + K*alpha);
                        }
                        
                        // add new group to allele counts, admix counts and likelihood
                        if (globals.data[ind][l][p]!=0) {
                            logLike += log(alleleCounts[newGroup[linearIndex]-1][l][globals.data[ind][l][p]-1] + globals.lambda) - log(alleleCountsTotals[newGroup[linearIndex]-1][l] + globals.J[l]*globals.lambda);
                            alleleCounts[newGroup[linearIndex]-1][l][globals.data[ind][l][p]-1]++;
                            alleleCountsTotals[newGroup[linearIndex]-1][l]++;
                            
                            logLike += log(admixCounts[ind][newGroup[linearIndex]-1] + alpha) - log(admixCountsTotals[ind] + K*alpha);
                            admixCounts[ind][newGroup[linearIndex]-1]++;
                            admixCountsTotals[ind]++;
                        }
                        
                    }
                    
                } // p
            } // l
        } // ind
        
        // update group
        group = newGroup;
        
        // some combinatorics to account for searching unique partitions only (see Notes.c for details)
        uniques = *max_element(begin(group),end(group));
        combined_logLike = logLike + my_lgamma(double(K)+1)-my_lgamma(double(K)-double(uniques)+1);
        
        // add to running sum
        logLike_total = logSum(logLike_total, combined_logLike);
    }
    
    return(logLike_total);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x108, %rsp            # imm = 0x108
movsd %xmm0, 0x40(%rsp)
movl %esi, %r15d
movq %rdi, %r14
addl 0x2bc8(%rdi), %r15d
addq $0x2cd0, %rdi           # imm = 0x2CD0
movq %rdi, 0xf8(%rsp)
callq 0x50fb
movslq 0x2d1c(%r14), %rcx
movslq %eax, %rsi
imulq %rcx, %rsi
leaq 0x98(%rsp), %rdx
movl $0x1, (%rdx)
leaq 0x60(%rsp), %r12
leaq 0xc8(%rsp), %rcx
movq %r12, %rdi
callq 0x5f08
leaq 0x98(%rsp), %rdi
movq %r12, %rsi
callq 0x5fce
movslq %r15d, %rbx
leaq 0xc8(%rsp), %rdi
leaq 0xb0(%rsp), %rdx
movq %rbx, %rsi
callq 0x9820
leaq 0xb0(%rsp), %rdi
leaq 0xe0(%rsp), %rdx
movq %rbx, 0x10(%rsp)
movq %rbx, %rsi
callq 0x9894
xorl %ebx, %ebx
testl %r15d, %r15d
movl $0x0, %eax
cmovgl %r15d, %eax
movq %rax, (%rsp)
leaq 0xe0(%rsp), %r13
leaq 0x48(%rsp), %rbp
movl %r15d, 0x2c(%rsp)
movq %r14, 0x20(%rsp)
cmpq (%rsp), %rbx
je 0xdc06
movslq 0x2d1c(%r14), %rsi
movq %r13, %rdi
movq %rbp, %rdx
callq 0x9894
imulq $0x18, %rbx, %r15
movq 0xc8(%rsp), %rdi
addq %r15, %rdi
movq %r13, %rsi
callq 0xa27c
movq %r13, %rdi
callq 0x98e6
movslq 0x2d1c(%r14), %rsi
movq %r13, %rdi
movq %rbp, %rdx
callq 0x5f7c
movq 0xb0(%rsp), %rdi
addq %r15, %rdi
movq %r13, %rsi
callq 0x6266
movq %r13, %rdi
callq 0x61d0
xorl %r12d, %r12d
xorl %r14d, %r14d
movq 0x20(%rsp), %rcx
movslq 0x2d1c(%rcx), %rax
cmpq %rax, %r14
jge 0xdbf6
movq 0x2d20(%rcx), %rax
movslq (%rax,%r14,4), %rsi
movq %r13, %rdi
movq %rbp, %rdx
callq 0x5f7c
movq 0xc8(%rsp), %rax
movq (%rax,%r15), %rdi
addq %r12, %rdi
movq %r13, %rsi
callq 0x6266
movq %r13, %rdi
callq 0x61d0
incq %r14
addq $0x18, %r12
jmp 0xdba7
incq %rbx
movl 0x2c(%rsp), %r15d
movq %rcx, %r14
jmp 0xdb39
movslq 0x2d18(%r14), %r13
leaq 0x48(%rsp), %rdi
leaq 0xf(%rsp), %rdx
movq 0x10(%rsp), %rsi
callq 0x5f7c
leaq 0xe0(%rsp), %rdi
leaq 0x48(%rsp), %rdx
leaq 0x37(%rsp), %rcx
movq %r13, %rsi
callq 0x9908
leaq 0x48(%rsp), %rdi
callq 0x61d0
movslq 0x2d18(%r14), %rsi
leaq 0x48(%rsp), %rdi
leaq 0xf(%rsp), %rdx
callq 0x5f7c
cvtsi2sd %r15d, %xmm0
pushq $-0x1
popq %rcx
xorpd %xmm1, %xmm1
movsd %xmm1, (%rsp)
xorl %edx, %edx
movsd %xmm0, 0x78(%rsp)
mulsd 0x40(%rsp), %xmm0
movsd %xmm0, 0x90(%rsp)
movq 0x20(%rsp), %r14
movq 0x20(%rsp), %rax
movslq 0x2d18(%rax), %rax
cmpq %rax, %rdx
jge 0xdeb9
imulq $0x18, %rdx, %rbp
xorl %esi, %esi
movq %rdx, 0x10(%rsp)
movq 0x20(%rsp), %rax
movslq 0x2d1c(%rax), %rax
cmpq %rax, %rsi
jge 0xdeac
movl %ecx, 0x30(%rsp)
movslq %ecx, %rax
leaq 0x4(,%rax,4), %rax
movq %rax, 0x88(%rsp)
movq %rsi, 0x38(%rsp)
imulq $0x18, %rsi, %rbx
xorl %r13d, %r13d
movq 0xf8(%rsp), %rax
movq (%rax), %rax
movslq (%rax,%rdx,4), %rax
cmpq %rax, %r13
jge 0xde98
movq 0x2d00(%r14), %rax
movq (%rax,%rbp), %rax
movq (%rax,%rbx), %rax
movslq (%rax,%r13,4), %rax
testq %rax, %rax
je 0xde90
movq 0x60(%rsp), %rcx
movq 0x88(%rsp), %r12
addq %r12, %rcx
movslq (%rcx,%r13,4), %rcx
movq 0xc8(%rsp), %rdx
imulq $0x18, %rcx, %rcx
movq -0x18(%rdx,%rcx), %rcx
movq (%rcx,%rbx), %rcx
cvtsi2sdl -0x4(%rcx,%rax,4), %xmm0
addsd 0x2c50(%r14), %xmm0
callq 0x32d0
movq 0x60(%rsp), %rax
addq %r12, %rax
movslq (%rax,%r13,4), %rax
movq 0xb0(%rsp), %rcx
imulq $0x18, %rax, %rax
movq -0x18(%rcx,%rax), %rax
movq 0x38(%rsp), %r15
cvtsi2sdl (%rax,%r15,4), %xmm2
movq 0x2d20(%r14), %rax
cvtsi2sdl (%rax,%r15,4), %xmm1
movsd %xmm0, 0x18(%rsp)
mulsd 0x2c50(%r14), %xmm1
addsd %xmm2, %xmm1
movapd %xmm1, %xmm0
callq 0x32d0
movq 0x60(%rsp), %rax
addq %r12, %rax
movslq (%rax,%r13,4), %rcx
movq 0xc8(%rsp), %rdx
imulq $0x18, %rcx, %rcx
movq -0x18(%rdx,%rcx), %rcx
movq 0x2d00(%r14), %rdx
movq (%rdx,%rbp), %rdx
movq (%rdx,%rbx), %rdx
movslq (%rdx,%r13,4), %rdx
movq (%rcx,%rbx), %rcx
incl -0x4(%rcx,%rdx,4)
movslq (%rax,%r13,4), %rcx
movq 0xb0(%rsp), %rdx
imulq $0x18, %rcx, %rcx
movq -0x18(%rdx,%rcx), %rcx
incl (%rcx,%r15,4)
movsd 0x18(%rsp), %xmm1
subsd %xmm0, %xmm1
movsd (%rsp), %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, (%rsp)
movq 0xe0(%rsp), %rcx
movslq (%rax,%r13,4), %rax
movq (%rcx,%rbp), %rcx
cvtsi2sdl -0x4(%rcx,%rax,4), %xmm0
addsd 0x40(%rsp), %xmm0
callq 0x32d0
movsd %xmm0, 0x18(%rsp)
movq 0x48(%rsp), %rax
movq 0x10(%rsp), %rcx
cvtsi2sdl (%rax,%rcx,4), %xmm0
addsd 0x90(%rsp), %xmm0
callq 0x32d0
movq 0x10(%rsp), %rdx
movsd 0x18(%rsp), %xmm1
subsd %xmm0, %xmm1
movsd (%rsp), %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, (%rsp)
movq 0xe0(%rsp), %rax
movq 0x60(%rsp), %rcx
addq %r12, %rcx
movslq (%rcx,%r13,4), %rcx
movq (%rax,%rbp), %rax
incl -0x4(%rax,%rcx,4)
movq 0x48(%rsp), %rax
incl (%rax,%rdx,4)
incq %r13
jmp 0xdce0
movl 0x30(%rsp), %ecx
addl %r13d, %ecx
movq 0x38(%rsp), %rsi
incq %rsi
jmp 0xdca8
incq %rdx
movl 0x2c(%rsp), %r15d
jmp 0xdc88
movsd 0x78(%rsp), %xmm0
callq 0x32d0
addsd (%rsp), %xmm0
movsd %xmm0, 0x80(%rsp)
movsd 0x19336(%rip), %xmm0    # 0x27210
addsd 0x78(%rsp), %xmm0
movsd %xmm0, 0x100(%rsp)
leaq 0x98(%rsp), %rbx
movq 0x20(%rsp), %r14
movq 0x20(%rsp), %r12
movq 0x60(%rsp), %rax
cmpl $0x0, (%rax)
je 0xe373
movq %rbx, %rdi
movl %r15d, %esi
callq 0x1610c
movq 0x98(%rsp), %rax
cmpl $0x0, (%rax)
je 0xe373
xorl %edi, %edi
pushq $-0x1
popq %rcx
movslq 0x2d18(%r14), %rax
cmpq %rax, %rdi
jge 0xe2ed
imulq $0x18, %rdi, %rbp
xorl %edx, %edx
movq %rdi, 0x38(%rsp)
movq %rbp, 0x88(%rsp)
movslq 0x2d1c(%r14), %rax
cmpq %rax, %rdx
jge 0xe2e0
movl %ecx, 0x30(%rsp)
movslq %ecx, %rax
leaq 0x4(,%rax,4), %r15
movq %rdx, 0x18(%rsp)
imulq $0x18, %rdx, %r14
xorl %r13d, %r13d
movq 0xf8(%rsp), %rax
movq (%rax), %rax
movslq (%rax,%rdi,4), %rax
cmpq %rax, %r13
jge 0xe2c7
movq 0x98(%rsp), %rcx
addq %r15, %rcx
movq 0x60(%rsp), %rax
addq %r15, %rax
movslq (%rax,%r13,4), %rdx
cmpl %edx, (%rcx,%r13,4)
je 0xe2bf
movq 0x2d00(%r12), %rcx
movq (%rcx,%rbp), %rcx
movq (%rcx,%r14), %rcx
cmpl $0x0, (%rcx,%r13,4)
je 0xe2bf
movq 0xb0(%rsp), %rsi
imulq $0x18, %rdx, %rdx
movq -0x18(%rsi,%rdx), %rdx
movq %r14, %rbp
movq %rdi, %r14
movq 0x18(%rsp), %rbx
decl (%rdx,%rbx,4)
movslq (%rax,%r13,4), %rdx
movq 0xc8(%rsp), %rsi
imulq $0x18, %rdx, %rdx
movq -0x18(%rsi,%rdx), %rdx
movslq (%rcx,%r13,4), %rdi
movq (%rdx,%rbp), %rdx
decl -0x4(%rdx,%rdi,4)
movslq (%rax,%r13,4), %rax
imulq $0x18, %rax, %rax
movq -0x18(%rsi,%rax), %rax
movslq (%rcx,%r13,4), %rcx
movq (%rax,%rbp), %rax
cvtsi2sdl -0x4(%rax,%rcx,4), %xmm0
addsd 0x2c50(%r12), %xmm0
callq 0x32d0
movq 0x60(%rsp), %rax
addq %r15, %rax
movslq (%rax,%r13,4), %rax
movq 0xb0(%rsp), %rcx
imulq $0x18, %rax, %rax
movq -0x18(%rcx,%rax), %rax
cvtsi2sdl (%rax,%rbx,4), %xmm2
movq 0x2d20(%r12), %rax
cvtsi2sdl (%rax,%rbx,4), %xmm1
movsd %xmm0, 0x10(%rsp)
mulsd 0x2c50(%r12), %xmm1
addsd %xmm2, %xmm1
movapd %xmm1, %xmm0
callq 0x32d0
movsd 0x10(%rsp), %xmm1
subsd %xmm0, %xmm1
movsd (%rsp), %xmm0
subsd %xmm1, %xmm0
movsd %xmm0, (%rsp)
movq 0x48(%rsp), %rax
decl (%rax,%r14,4)
movq 0xe0(%rsp), %rax
movq 0x60(%rsp), %rcx
addq %r15, %rcx
movslq (%rcx,%r13,4), %rdx
movq 0x88(%rsp), %rbx
movq (%rax,%rbx), %rax
decl -0x4(%rax,%rdx,4)
movslq (%rcx,%r13,4), %rcx
cvtsi2sdl -0x4(%rax,%rcx,4), %xmm0
addsd 0x40(%rsp), %xmm0
callq 0x32d0
movsd %xmm0, 0x10(%rsp)
movq 0x48(%rsp), %rax
cvtsi2sdl (%rax,%r14,4), %xmm0
movq %rbp, %r14
addsd 0x90(%rsp), %xmm0
callq 0x32d0
movsd 0x10(%rsp), %xmm1
subsd %xmm0, %xmm1
movsd (%rsp), %xmm0
subsd %xmm1, %xmm0
movsd %xmm0, (%rsp)
movq 0x2d00(%r12), %rax
movq (%rax,%rbx), %rax
movq (%rax,%rbp), %rax
movslq (%rax,%r13,4), %rax
testq %rax, %rax
je 0xe2b7
movq 0x98(%rsp), %rcx
addq %r15, %rcx
movslq (%rcx,%r13,4), %rcx
movq 0xc8(%rsp), %rdx
imulq $0x18, %rcx, %rcx
movq -0x18(%rdx,%rcx), %rcx
movq (%rcx,%r14), %rcx
cvtsi2sdl -0x4(%rcx,%rax,4), %xmm0
addsd 0x2c50(%r12), %xmm0
callq 0x32d0
movq 0x98(%rsp), %rax
addq %r15, %rax
movslq (%rax,%r13,4), %rax
movq 0xb0(%rsp), %rcx
imulq $0x18, %rax, %rax
movq -0x18(%rcx,%rax), %rax
movq 0x18(%rsp), %rbp
cvtsi2sdl (%rax,%rbp,4), %xmm2
movq 0x2d20(%r12), %rax
cvtsi2sdl (%rax,%rbp,4), %xmm1
movsd %xmm0, 0x10(%rsp)
mulsd 0x2c50(%r12), %xmm1
addsd %xmm2, %xmm1
movapd %xmm1, %xmm0
callq 0x32d0
movq 0x98(%rsp), %rax
addq %r15, %rax
movslq (%rax,%r13,4), %rcx
movq 0xc8(%rsp), %rdx
imulq $0x18, %rcx, %rcx
movq -0x18(%rdx,%rcx), %rcx
movq 0x2d00(%r12), %rdx
movq (%rdx,%rbx), %rdx
movq (%rdx,%r14), %rdx
movslq (%rdx,%r13,4), %rdx
movq (%rcx,%r14), %rcx
incl -0x4(%rcx,%rdx,4)
movslq (%rax,%r13,4), %rcx
movq 0xb0(%rsp), %rdx
imulq $0x18, %rcx, %rcx
movq -0x18(%rdx,%rcx), %rcx
incl (%rcx,%rbp,4)
movsd 0x10(%rsp), %xmm1
subsd %xmm0, %xmm1
movsd (%rsp), %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, (%rsp)
movq 0xe0(%rsp), %rcx
movslq (%rax,%r13,4), %rax
movq (%rcx,%rbx), %rcx
cvtsi2sdl -0x4(%rcx,%rax,4), %xmm0
addsd 0x40(%rsp), %xmm0
callq 0x32d0
movsd %xmm0, 0x10(%rsp)
movq 0x48(%rsp), %rax
movq %rbx, %rbp
movq 0x38(%rsp), %rbx
cvtsi2sdl (%rax,%rbx,4), %xmm0
addsd 0x90(%rsp), %xmm0
callq 0x32d0
movsd 0x10(%rsp), %xmm1
subsd %xmm0, %xmm1
movsd (%rsp), %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, (%rsp)
movq 0xe0(%rsp), %rax
movq 0x98(%rsp), %rcx
addq %r15, %rcx
movslq (%rcx,%r13,4), %rcx
movq (%rax,%rbp), %rax
incl -0x4(%rax,%rcx,4)
movq 0x48(%rsp), %rax
incl (%rax,%rbx,4)
movq %rbx, %rdi
jmp 0xe2bf
movq %rbx, %rbp
movq 0x38(%rsp), %rdi
incq %r13
jmp 0xdf78
movl 0x30(%rsp), %ecx
addl %r13d, %ecx
movq 0x18(%rsp), %rdx
incq %rdx
movq 0x20(%rsp), %r14
jmp 0xdf4d
incq %rdi
movl 0x2c(%rsp), %r15d
jmp 0xdf2a
leaq 0x60(%rsp), %rdi
leaq 0x98(%rsp), %rbx
movq %rbx, %rsi
callq 0x969e
movq 0x60(%rsp), %rdi
movq 0x68(%rsp), %rsi
callq 0xe459
cvtsi2sdl (%rax), %xmm0
movsd %xmm0, 0x18(%rsp)
movsd 0x100(%rsp), %xmm0
callq 0x32a0
addsd (%rsp), %xmm0
movsd %xmm0, 0x10(%rsp)
movsd 0x78(%rsp), %xmm0
subsd 0x18(%rsp), %xmm0
addsd 0x18ec8(%rip), %xmm0    # 0x27210
callq 0x32a0
movsd 0x10(%rsp), %xmm1
subsd %xmm0, %xmm1
movsd 0x80(%rsp), %xmm0
callq 0x14ce5
movsd %xmm0, 0x80(%rsp)
jmp 0xdefb
leaq 0x48(%rsp), %rdi
callq 0x61d0
leaq 0xe0(%rsp), %rdi
callq 0x98e6
leaq 0xb0(%rsp), %rdi
callq 0x98e6
leaq 0xc8(%rsp), %rdi
callq 0x9872
leaq 0x98(%rsp), %rdi
callq 0x61d0
leaq 0x60(%rsp), %rdi
callq 0x61d0
movsd 0x80(%rsp), %xmm0
addq $0x108, %rsp            # imm = 0x108
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
jmp 0xe40e
movq %rax, %rbx
leaq 0x48(%rsp), %rdi
callq 0x61d0
jmp 0xe420
jmp 0xe41d
movq %rax, %rbx
jmp 0xe42d
movq %rax, %rbx
jmp 0xe43a
movq %rax, %rbx
jmp 0xe447
jmp 0xe401
jmp 0xe41d
jmp 0xe41d
movq %rax, %rbx
leaq 0x48(%rsp), %rdi
callq 0x61d0
leaq 0xe0(%rsp), %rdi
callq 0x98e6
jmp 0xe420
movq %rax, %rbx
leaq 0xb0(%rsp), %rdi
callq 0x98e6
leaq 0xc8(%rsp), %rdi
callq 0x9872
leaq 0x98(%rsp), %rdi
callq 0x61d0
leaq 0x60(%rsp), %rdi
callq 0x61d0
movq %rbx, %rdi
callq 0x35a0