// File: bobverity[P]MavericK/writeOut.cpp
// Function: printQmatrix_gene(globals&, int)
void printQmatrix_gene(globals &globals, int Kindex) {
    int K = globals.Kmin+Kindex;
    
    // open outputQmatrix_gene filestream for this K
    string s = globals.outputQmatrix_gene_filePath;
    s.erase(end(s)-4,end(s));
    s = s + "_K" + to_string((long long)K) + ".csv";
    globals.outputQmatrix_gene_fileStream = safe_ofstream(s,globals.outputLog_on, globals.outputLog_fileStream);
    
    // write headers
    globals.outputQmatrix_gene_fileStream << "index,label";
    if (globals.popCol_on)
        globals.outputQmatrix_gene_fileStream << ",given_population";
    globals.outputQmatrix_gene_fileStream << ",locus,gene_copy";
    for (int k=0; k<K; k++)
        globals.outputQmatrix_gene_fileStream << ",deme" << k+1;
    globals.outputQmatrix_gene_fileStream << "\n";
    
    // write results
    int groupIndex=-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++) {
                groupIndex++;
                
                // individual index
                globals.outputQmatrix_gene_fileStream << ind+1 << ",";
                
                // individual label
                globals.outputQmatrix_gene_fileStream << globals.indLabels_vec[ind] << ",";
                
                // population data
                if (globals.popCol_on)
                    globals.outputQmatrix_gene_fileStream << globals.pop_vec[ind] << ",";
                
                // locus
                globals.outputQmatrix_gene_fileStream << l+1 << ",";
                
                // gene copy
                globals.outputQmatrix_gene_fileStream << p+1 << ",";
                
                // probabilities
                for (int k=0; k<K; k++) {
                    char * buffer = new char[256];
                    sprintf(buffer, "%.3f", my_round(globals.Qmatrix_gene[Kindex][groupIndex][k]*1000)/1000.0);
                    string s = buffer;
                    delete [] buffer;
                    globals.outputQmatrix_gene_fileStream << s;
                    if (k<(K-1))
                        globals.outputQmatrix_gene_fileStream << ",";
                }
                
                globals.outputQmatrix_gene_fileStream << "\n";
                
            }
        }
    }
    globals.outputQmatrix_gene_fileStream.flush();
}

// Assembly:
subq $0x408, %rsp            # imm = 0x408
movq %rdi, 0x400(%rsp)
movl %esi, 0x3fc(%rsp)
movq 0x400(%rsp), %rax
movl 0x2bc8(%rax), %eax
movl 0x3fc(%rsp), %ecx
addl %ecx, %eax
movl %eax, 0x3f8(%rsp)
movq 0x400(%rsp), %rsi
addq $0x3a0, %rsi            # imm = 0x3A0
leaq 0x3d8(%rsp), %rdi
movq %rdi, 0xa0(%rsp)
callq 0x4180
movq 0xa0(%rsp), %rdi
callq 0x5bc90
movq %rax, 0xa8(%rsp)
jmp 0x57d3f
movq 0xa8(%rsp), %rax
movq %rax, 0x3c0(%rsp)
leaq 0x3c0(%rsp), %rdi
movl $0x4, %esi
callq 0x5bcc0
movq %rax, 0x3c8(%rsp)
leaq 0x3d0(%rsp), %rdi
leaq 0x3c8(%rsp), %rsi
callq 0x5bd00
leaq 0x3d8(%rsp), %rdi
callq 0x5bc90
movq %rax, 0x98(%rsp)
jmp 0x57d95
movq 0x98(%rsp), %rax
movq %rax, 0x3a0(%rsp)
leaq 0x3a8(%rsp), %rdi
leaq 0x3a0(%rsp), %rsi
callq 0x5bd00
movq 0x3d0(%rsp), %rsi
movq 0x3a8(%rsp), %rdx
leaq 0x3d8(%rsp), %rdi
callq 0x40d0
movq %rax, 0x90(%rsp)
jmp 0x57de1
movq 0x90(%rsp), %rax
movq %rax, 0x398(%rsp)
leaq 0x69fe(%rip), %rdx      # 0x5e7f6
leaq 0x338(%rsp), %rdi
leaq 0x3d8(%rsp), %rsi
callq 0x2af00
jmp 0x57e0f
movslq 0x3f8(%rsp), %rsi
leaq 0x318(%rsp), %rdi
callq 0xcb30
jmp 0x57e26
leaq 0x358(%rsp), %rdi
leaq 0x338(%rsp), %rsi
leaq 0x318(%rsp), %rdx
callq 0xc9f0
jmp 0x57e45
leaq 0x4509(%rip), %rdx      # 0x5c355
leaq 0x378(%rsp), %rdi
leaq 0x358(%rsp), %rsi
callq 0x22900
jmp 0x57e63
leaq 0x3d8(%rsp), %rdi
movq %rdi, 0x88(%rsp)
leaq 0x378(%rsp), %rsi
movq %rsi, 0x80(%rsp)
callq 0x4500
movq 0x80(%rsp), %rdi
callq 0x4270
leaq 0x358(%rsp), %rdi
callq 0x4270
leaq 0x318(%rsp), %rdi
callq 0x4270
leaq 0x338(%rsp), %rdi
callq 0x4270
movq 0x88(%rsp), %rsi
leaq 0xf8(%rsp), %rdi
callq 0x4180
jmp 0x57ed3
movq 0x400(%rsp), %rax
movq %rax, %rcx
addq $0x970, %rcx            # imm = 0x970
movzbl 0x2c3c(%rax), %edx
andl $0x1, %edx
leaq 0x118(%rsp), %rdi
leaq 0xf8(%rsp), %rsi
callq 0x52e90
jmp 0x57f06
movq 0x400(%rsp), %rdi
addq $0x1170, %rdi           # imm = 0x1170
leaq 0x118(%rsp), %rsi
callq 0x44e0
jmp 0x57f24
leaq 0x118(%rsp), %rdi
callq 0x45d0
leaq 0xf8(%rsp), %rdi
callq 0x4270
movq 0x400(%rsp), %rdi
addq $0x1170, %rdi           # imm = 0x1170
leaq 0x6a5a(%rip), %rsi      # 0x5e9ae
callq 0x4430
jmp 0x57f5b
movq 0x400(%rsp), %rax
testb $0x1, 0x2ba1(%rax)
je 0x58060
movq 0x400(%rsp), %rdi
addq $0x1170, %rdi           # imm = 0x1170
leaq 0x6a34(%rip), %rsi      # 0x5e9ba
callq 0x4430
jmp 0x57f8d
jmp 0x58060
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x3b8(%rsp)
movl %eax, 0x3b4(%rsp)
jmp 0x5857e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x3b8(%rsp)
movl %eax, 0x3b4(%rsp)
jmp 0x58005
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x3b8(%rsp)
movl %eax, 0x3b4(%rsp)
jmp 0x57ff8
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x3b8(%rsp)
movl %eax, 0x3b4(%rsp)
leaq 0x358(%rsp), %rdi
callq 0x4270
leaq 0x318(%rsp), %rdi
callq 0x4270
leaq 0x338(%rsp), %rdi
callq 0x4270
jmp 0x5857e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x3b8(%rsp)
movl %eax, 0x3b4(%rsp)
jmp 0x5804e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x3b8(%rsp)
movl %eax, 0x3b4(%rsp)
leaq 0x118(%rsp), %rdi
callq 0x45d0
leaq 0xf8(%rsp), %rdi
callq 0x4270
jmp 0x5857e
movq 0x400(%rsp), %rdi
addq $0x1170, %rdi           # imm = 0x1170
leaq 0x6956(%rip), %rsi      # 0x5e9cc
callq 0x4430
jmp 0x5807d
movl $0x0, 0xf4(%rsp)
movl 0xf4(%rsp), %eax
cmpl 0x3f8(%rsp), %eax
jge 0x580e4
movq 0x400(%rsp), %rdi
addq $0x1170, %rdi           # imm = 0x1170
leaq 0x692f(%rip), %rsi      # 0x5e9dd
callq 0x4430
movq %rax, 0x78(%rsp)
jmp 0x580ba
movq 0x78(%rsp), %rdi
movl 0xf4(%rsp), %esi
incl %esi
callq 0x4700
jmp 0x580cf
jmp 0x580d1
movl 0xf4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xf4(%rsp)
jmp 0x58088
movq 0x400(%rsp), %rdi
addq $0x1170, %rdi           # imm = 0x1170
leaq 0x4798(%rip), %rsi      # 0x5c892
callq 0x4430
jmp 0x58101
movl $0xffffffff, 0xf0(%rsp) # imm = 0xFFFFFFFF
movl $0x0, 0xec(%rsp)
movl 0xec(%rsp), %eax
movq 0x400(%rsp), %rcx
cmpl 0x2d18(%rcx), %eax
jge 0x58553
movl $0x0, 0xe8(%rsp)
movl 0xe8(%rsp), %eax
movq 0x400(%rsp), %rcx
cmpl 0x2d1c(%rcx), %eax
jge 0x5853b
movl $0x0, 0xe4(%rsp)
movl 0xe4(%rsp), %eax
movl %eax, 0x74(%rsp)
movq 0x400(%rsp), %rdi
addq $0x2cd0, %rdi           # imm = 0x2CD0
movslq 0xec(%rsp), %rsi
callq 0x8ba0
movq %rax, %rcx
movl 0x74(%rsp), %eax
cmpl (%rcx), %eax
jge 0x58523
movl 0xf0(%rsp), %eax
incl %eax
movl %eax, 0xf0(%rsp)
movq 0x400(%rsp), %rdi
addq $0x1170, %rdi           # imm = 0x1170
movl 0xec(%rsp), %esi
incl %esi
callq 0x4700
movq %rax, 0x68(%rsp)
jmp 0x581cd
movq 0x68(%rsp), %rdi
leaq 0x404f(%rip), %rsi      # 0x5c228
callq 0x4430
jmp 0x581e0
movq 0x400(%rsp), %rdi
movq %rdi, %rax
addq $0x1170, %rax           # imm = 0x1170
movq %rax, 0x58(%rsp)
addq $0x2c58, %rdi           # imm = 0x2C58
movslq 0xec(%rsp), %rsi
callq 0x4fe90
movq 0x58(%rsp), %rdi
movq %rax, %rsi
callq 0x43e0
movq %rax, 0x60(%rsp)
jmp 0x5821e
movq 0x60(%rsp), %rdi
leaq 0x3ffe(%rip), %rsi      # 0x5c228
callq 0x4430
jmp 0x58231
movq 0x400(%rsp), %rax
testb $0x1, 0x2ba1(%rax)
je 0x58295
movq 0x400(%rsp), %rdi
movq %rdi, %rax
addq $0x1170, %rax           # imm = 0x1170
movq %rax, 0x48(%rsp)
addq $0x2c70, %rdi           # imm = 0x2C70
movslq 0xec(%rsp), %rsi
callq 0x4fe90
movq 0x48(%rsp), %rdi
movq %rax, %rsi
callq 0x43e0
movq %rax, 0x50(%rsp)
jmp 0x58280
movq 0x50(%rsp), %rdi
leaq 0x3f9c(%rip), %rsi      # 0x5c228
callq 0x4430
jmp 0x58293
jmp 0x58295
movq 0x400(%rsp), %rdi
addq $0x1170, %rdi           # imm = 0x1170
movl 0xe8(%rsp), %esi
incl %esi
callq 0x4700
movq %rax, 0x40(%rsp)
jmp 0x582b9
movq 0x40(%rsp), %rdi
leaq 0x3f63(%rip), %rsi      # 0x5c228
callq 0x4430
jmp 0x582cc
movq 0x400(%rsp), %rdi
addq $0x1170, %rdi           # imm = 0x1170
movl 0xe4(%rsp), %esi
incl %esi
callq 0x4700
movq %rax, 0x38(%rsp)
jmp 0x582f0
movq 0x38(%rsp), %rdi
leaq 0x3f2c(%rip), %rsi      # 0x5c228
callq 0x4430
jmp 0x58303
movl $0x0, 0xe0(%rsp)
movl 0xe0(%rsp), %eax
cmpl 0x3f8(%rsp), %eax
jge 0x584ee
movl $0x100, %edi            # imm = 0x100
callq 0x4030
movq %rax, 0x30(%rsp)
jmp 0x58333
movq 0x30(%rsp), %rax
movq %rax, 0xd8(%rsp)
movq 0xd8(%rsp), %rax
movq %rax, 0x20(%rsp)
movq 0x400(%rsp), %rdi
addq $0x2d70, %rdi           # imm = 0x2D70
movslq 0x3fc(%rsp), %rsi
callq 0x8ae0
movq %rax, %rdi
movslq 0xf0(%rsp), %rsi
callq 0x8bc0
movq %rax, %rdi
movslq 0xe0(%rsp), %rsi
callq 0x8c20
movsd (%rax), %xmm0
movsd 0x62eb(%rip), %xmm1     # 0x5e680
mulsd %xmm1, %xmm0
callq 0x33950
movl %eax, 0x2c(%rsp)
jmp 0x583a4
movq 0x20(%rsp), %rdi
movl 0x2c(%rsp), %eax
cvtsi2sd %eax, %xmm0
movsd 0x62c7(%rip), %xmm1     # 0x5e680
divsd %xmm1, %xmm0
leaq 0x661f(%rip), %rsi      # 0x5e9e3
movb $0x1, %al
callq 0x40b0
movq 0xd8(%rsp), %rax
movq %rax, 0x10(%rsp)
leaq 0xb7(%rsp), %rdi
movq %rdi, 0x18(%rsp)
callq 0x4740
movq 0x10(%rsp), %rsi
movq 0x18(%rsp), %rdx
leaq 0xb8(%rsp), %rdi
callq 0x4580
jmp 0x58403
leaq 0xb7(%rsp), %rdi
callq 0x4490
movq 0xd8(%rsp), %rax
movq %rax, 0x8(%rsp)
cmpq $0x0, %rax
je 0x5842d
movq 0x8(%rsp), %rdi
callq 0x4530
movq 0x400(%rsp), %rdi
addq $0x1170, %rdi           # imm = 0x1170
leaq 0xb8(%rsp), %rsi
callq 0x43e0
jmp 0x5844b
movl 0xe0(%rsp), %eax
movl 0x3f8(%rsp), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jge 0x584cb
movq 0x400(%rsp), %rdi
addq $0x1170, %rdi           # imm = 0x1170
leaq 0x3db2(%rip), %rsi      # 0x5c228
callq 0x4430
jmp 0x5847d
jmp 0x584cb
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x3b8(%rsp)
movl %eax, 0x3b4(%rsp)
leaq 0xb7(%rsp), %rdi
callq 0x4490
jmp 0x5857e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x3b8(%rsp)
movl %eax, 0x3b4(%rsp)
leaq 0xb8(%rsp), %rdi
callq 0x4270
jmp 0x5857e
leaq 0xb8(%rsp), %rdi
callq 0x4270
movl 0xe0(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xe0(%rsp)
jmp 0x5830e
movq 0x400(%rsp), %rdi
addq $0x1170, %rdi           # imm = 0x1170
leaq 0x438e(%rip), %rsi      # 0x5c892
callq 0x4430
jmp 0x5850b
jmp 0x5850d
movl 0xe4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xe4(%rsp)
jmp 0x58163
jmp 0x58525
movl 0xe8(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xe8(%rsp)
jmp 0x5813d
jmp 0x5853d
movl 0xec(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xec(%rsp)
jmp 0x58117
movq 0x400(%rsp), %rdi
addq $0x1170, %rdi           # imm = 0x1170
callq 0x42b0
jmp 0x58569
leaq 0x3d8(%rsp), %rdi
callq 0x4270
addq $0x408, %rsp            # imm = 0x408
retq
leaq 0x3d8(%rsp), %rdi
callq 0x4270
movq 0x3b8(%rsp), %rdi
callq 0x4730
nopl (%rax,%rax)