// File: /workspace/github_cpp_makefile_star2_O3/millanek[P]evo/evo_getInformativeReadsFromSam.cpp
// Function: InfoReadsMain(int, char**)
int InfoReadsMain(int argc, char** argv) {
    parseInfoReadsOptions(argc, argv);
    string line; // for reading the input files
    
    std::istream* hetFile = createReader(opt::hetFile.c_str());
    std::istream* samtoolsFile = &std::cin;
    
    std::unordered_set<int> phasedHetsPos;
    std::map<int,PhaseInfo*> positionToPhase;
    std::map<string,std::vector<string> > readNameToSamRecords;
    
    
    if (opt::hapcutFormat) {
        // Parse the Hapcut blocks file
        while (getline(*hetFile, line)) {
            if (line[0] == '*') {
            
            } else if (line[0] == 'B' && line[1] == 'L') { // New block - should in the future separate the hets by blocks
                
            } else {
                std::vector<string> phasedSNPdetails = split(line, '\t');
                int snpPos = atoi(phasedSNPdetails[4].c_str());
                int H1phase = atoi(phasedSNPdetails[1].c_str());
                int H2phase = atoi(phasedSNPdetails[2].c_str());
                //std::cout << "line: " << line << std::endl;
                // std::cout << "phasedSNPdetails[5]: " << phasedSNPdetails[5] << std::endl;
                assert(phasedSNPdetails[5].length() == 1); assert(phasedSNPdetails[6].length() == 1);
                char refBase = phasedSNPdetails[5][0];
                char altBase = phasedSNPdetails[6][0];
                double phaseQual = stringToDouble(phasedSNPdetails[10].c_str());
                int snpCoverage = atoi(phasedSNPdetails[11].c_str());
                std::vector<char> phasedVars;
                if (H1phase == 0 && H2phase == 1) {
                    phasedVars.push_back(refBase); phasedVars.push_back(altBase);
                } else if (H1phase == 1 && H2phase == 0) {
                    phasedVars.push_back(altBase); phasedVars.push_back(refBase);
                } else{
                    continue;
                }
                PhaseInfo* thisPhase = new PhaseInfo(snpPos,phaseQual,snpCoverage, phasedVars,1);
                positionToPhase[snpPos] = thisPhase;
            }
        }
    } else {
        while (getline(*hetFile, line)) {
            std::vector<string> phasedSNPdetails = split(line, '\t');
            int snpPos = atoi(phasedSNPdetails[1].c_str());
            phasedHetsPos.insert(snpPos);
        }
    }
    
    std::cerr << "Finished reading het sites. There are " << positionToPhase.size() << " hets." << std::endl;
    std::cerr << "Processing reads:" << std::endl;
    
    int readsProcessed = 0;
    // Now parse the pairtools file to find read pairs that can be informative about the phasing and recombination
    while (getline(*samtoolsFile,line)) {
        readsProcessed++;
        if (readsProcessed % 100000 == 0) {
            std::cerr << "Processed " << readsProcessed << " reads" << std::endl;
        }
        // std::cerr << line << std::endl;
        std::vector<string> samRecVec = split(line, '\t'); //assert(pairVec.size() == 8);
        int flag = atoi(samRecVec[1].c_str());
        if (flag > 2000) continue;
        
        int MQ = atoi(samRecVec[4].c_str());
        if (MQ < opt::minMQ) continue;
        
        
        RecombRead* thisRead = new RecombRead(samRecVec);
        
        thisRead->findHetsInRead(positionToPhase);
        
        if (thisRead->hetSites.size() > 0) {
            readNameToSamRecords[thisRead->readName].push_back(line);
        }
        delete thisRead;
    }
    
    for (std::map<string,std::vector<string>>::iterator it = readNameToSamRecords.begin(); it != readNameToSamRecords.end(); it++) {
        if (it->second.size() > 1) {
            std::cout << it->second[0] << std::endl;
            std::cout << it->second[1] << std::endl;
        }
    }
    
    return 0;
    
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x138, %rsp            # imm = 0x138
callq 0xc4542
leaq 0x38(%rsp), %r15
movq %r15, -0x10(%r15)
movq $0x0, -0x8(%r15)
movb $0x0, (%r15)
movq 0x40636(%rip), %rsi     # 0x104380
leaq 0x88(%rsp), %rdi
leaq 0xc0(%rsp), %rdx
callq 0x287de
leaq 0x88(%rsp), %r14
movq %r14, %rdi
movl $0x8, %esi
callq 0x500af
movq %rax, %rbx
leaq 0x98(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0xc3d98
movq 0x98(%rsp), %rsi
incq %rsi
callq 0x8480
leaq 0xb8(%rsp), %rax
movq %rax, -0x30(%rax)
movq $0x1, -0x28(%rax)
xorps %xmm0, %xmm0
movups %xmm0, -0x20(%rax)
movl $0x3f800000, -0x10(%rax) # imm = 0x3F800000
movups %xmm0, -0x8(%rax)
leaq 0xc8(%rsp), %rax
xorl %ecx, %ecx
movl %ecx, (%rax)
movq %rcx, 0x8(%rax)
movq %rax, 0x10(%rax)
movq %rax, 0x18(%rax)
movq %rcx, 0x20(%rax)
leaq 0xf8(%rsp), %rax
movl %ecx, (%rax)
movq %rcx, 0x8(%rax)
movq %rax, 0x10(%rax)
movq %rax, 0x18(%rax)
movq %rcx, 0x20(%rax)
cmpb $0x0, 0x405c5(%rip)     # 0x1043c0
je 0xc401a
leaq 0x28(%rsp), %r14
leaq 0x48(%rsp), %r12
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x8450
movsbl %al, %edx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x8870
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0xc409d
movq 0x28(%rsp), %rax
movzbl (%rax), %ecx
cmpl $0x2a, %ecx
je 0xc3e0b
cmpl $0x42, %ecx
jne 0xc3e57
cmpb $0x4c, 0x1(%rax)
je 0xc3e0b
movq %r12, %r13
movq %r12, %rdi
movq %r14, %rsi
movl $0x9, %edx
callq 0x4d650
movq 0x48(%rsp), %rbp
movq 0x80(%rbp), %rdi
callq 0x86e0
movl %eax, 0xc(%rsp)
movq 0x20(%rbp), %rdi
callq 0x86e0
movl %eax, %r15d
movq 0x40(%rbp), %rdi
callq 0x86e0
cmpq $0x1, 0xa8(%rbp)
jne 0xc440c
cmpq $0x1, 0xc8(%rbp)
jne 0xc4415
movl %eax, %r12d
movq 0xa0(%rbp), %rax
movb (%rax), %al
movb %al, 0xb(%rsp)
movq 0xc0(%rbp), %rax
movb (%rax), %al
movb %al, 0xa(%rsp)
movq 0x140(%rbp), %rsi
leaq 0x68(%rsp), %rbp
movq %rbp, %rdi
leaq 0x10(%rsp), %rdx
callq 0x287de
movq %rbp, %rdi
callq 0x4d578
movsd %xmm0, 0x60(%rsp)
movq 0x68(%rsp), %rdi
leaq 0x78(%rsp), %rax
cmpq %rax, %rdi
je 0xc3f10
movq 0x78(%rsp), %rsi
incq %rsi
callq 0x8480
movq 0x48(%rsp), %rax
movq 0x160(%rax), %rdi
callq 0x86e0
movl %eax, %ebp
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rsp)
movq $0x0, 0x20(%rsp)
movl %r12d, %eax
xorl $0x1, %eax
orl %r15d, %eax
jne 0xc3f65
leaq 0x10(%rsp), %rdi
xorl %esi, %esi
leaq 0xb(%rsp), %rdx
callq 0x6ea22
movq %r13, %r12
movq 0x18(%rsp), %rsi
cmpq 0x20(%rsp), %rsi
je 0xc3f9f
movb 0xa(%rsp), %al
jmp 0xc3f96
xorl $0x1, %r15d
orl %r12d, %r15d
movq %r13, %r12
jne 0xc400d
leaq 0x10(%rsp), %rdi
xorl %esi, %esi
leaq 0xa(%rsp), %rdx
callq 0x6ea22
movq 0x18(%rsp), %rsi
cmpq 0x20(%rsp), %rsi
je 0xc3fa6
movb 0xb(%rsp), %al
movb %al, (%rsi)
incq 0x18(%rsp)
jmp 0xc3fb5
leaq 0xa(%rsp), %rdx
jmp 0xc3fab
leaq 0xb(%rsp), %rdx
leaq 0x10(%rsp), %rdi
callq 0x6ea22
movl $0x30, %edi
callq 0x8470
movq %rax, %r15
movl 0xc(%rsp), %esi
movq %rax, %rdi
movsd 0x60(%rsp), %xmm0
movl %ebp, %edx
leaq 0x10(%rsp), %rcx
movl $0x1, %r8d
callq 0xc237e
leaq 0xc0(%rsp), %rdi
leaq 0xc(%rsp), %rsi
callq 0xc23e0
movq %r15, (%rax)
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0xc400d
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x8480
movq %r12, %rdi
callq 0x2e784
jmp 0xc3e0b
leaq 0x28(%rsp), %r15
leaq 0x48(%rsp), %r12
leaq 0xc(%rsp), %r13
leaq 0x10(%rsp), %rbp
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x8450
movsbl %al, %edx
movq %rbx, %rdi
movq %r15, %rsi
callq 0x8870
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0xc409d
movq %r12, %rdi
movq %r15, %rsi
movl $0x9, %edx
callq 0x4d650
movq 0x48(%rsp), %rax
movq 0x20(%rax), %rdi
callq 0x86e0
movl %eax, 0xc(%rsp)
movq %r14, 0x10(%rsp)
movq %r14, %rdi
movq %r13, %rsi
movq %rbp, %rdx
callq 0xbfb88
movq %r12, %rdi
callq 0x2e784
jmp 0xc402e
movq 0x3df44(%rip), %rdi     # 0x101fe8
leaq 0x1cea2(%rip), %rsi     # 0xe0f4d
movl $0x26, %edx
callq 0x84e0
movq 0xe8(%rsp), %rsi
movq 0x3df24(%rip), %rdi     # 0x101fe8
callq 0x82f0
movq %rax, %rbx
leaq 0x1cea1(%rip), %rsi     # 0xe0f74
movl $0x6, %edx
movq %rax, %rdi
callq 0x84e0
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x8450
movsbl %al, %esi
movq %rbx, %rdi
callq 0x8040
movq %rax, %rdi
callq 0x8300
movq 0x3deda(%rip), %rbx     # 0x101fe8
leaq 0x1ce66(%rip), %rsi     # 0xe0f7b
movl $0x11, %edx
movq %rbx, %rdi
callq 0x84e0
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x8450
movsbl %al, %esi
movq 0x3dea8(%rip), %rdi     # 0x101fe8
callq 0x8040
movq %rax, %rdi
callq 0x8300
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
movl $0x1, %ebx
movq 0x3de72(%rip), %r14     # 0x101fd0
leaq 0x48(%rsp), %r13
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x8450
movsbl %al, %edx
movq %r14, %rdi
leaq 0x28(%rsp), %rsi
callq 0x8870
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0xc42f9
movl %ebx, %eax
shrl $0x5, %eax
imulq $0xa7c5ac5, %rax, %rax  # imm = 0xA7C5AC5
shrq $0x27, %rax
imull $0x186a0, %eax, %eax    # imm = 0x186A0
addl %ebp, %eax
jne 0xc4218
movl $0xa, %edx
movq 0x3de29(%rip), %rdi     # 0x101fe8
leaq 0xdda0(%rip), %rsi      # 0xd1f66
callq 0x84e0
movq 0x3de16(%rip), %rdi     # 0x101fe8
movl %ebx, %esi
callq 0x8810
movq %rax, %r12
movl $0x6, %edx
movq %rax, %rdi
leaq 0x1cda2(%rip), %rsi     # 0xe0f8d
callq 0x84e0
movq (%r12), %rax
movq -0x18(%rax), %rdi
addq %r12, %rdi
movl $0xa, %esi
callq 0x8450
movsbl %al, %esi
movq %r12, %rdi
callq 0x8040
movq %rax, %rdi
callq 0x8300
movq %r13, %rdi
leaq 0x28(%rsp), %rsi
movl $0x9, %edx
callq 0x4d650
movq 0x48(%rsp), %r12
movq 0x20(%r12), %rdi
callq 0x86e0
cmpl $0x7d0, %eax            # imm = 0x7D0
jg 0xc42e8
movq 0x80(%r12), %rdi
callq 0x86e0
cmpl 0x3e3a1(%rip), %eax     # 0x1025f8
jl 0xc42e8
movl $0x150, %edi            # imm = 0x150
callq 0x8470
movq %rax, %r12
leaq 0x120(%rsp), %r15
movq %r15, %rdi
movq %r13, %rsi
callq 0x2fc8e
movq %r12, %rdi
movq %r15, %rsi
callq 0xc2446
movq %r15, %rdi
callq 0x2e784
movq %r12, %rdi
leaq 0xc0(%rsp), %rsi
callq 0xc4a90
movq 0x110(%r12), %rax
cmpq 0x108(%r12), %rax
je 0xc42d3
movq %r12, %rsi
addq $0x28, %rsi
leaq 0xf0(%rsp), %rdi
callq 0x46fb4
movq %rax, %rdi
leaq 0x28(%rsp), %rsi
callq 0x3133c
movq %r12, %rdi
callq 0xc47cc
movl $0x150, %esi            # imm = 0x150
movq %r12, %rdi
callq 0x8480
movq %r13, %rdi
callq 0x2e784
decl %ebp
incl %ebx
jmp 0xc4163
movq 0x108(%rsp), %rbx
leaq 0xf8(%rsp), %r13
cmpq %r13, %rbx
leaq 0x38(%rsp), %r12
je 0xc43ba
movq 0x3dc9a(%rip), %r14     # 0x101fb8
movq 0x40(%rbx), %rax
movq 0x48(%rbx), %rcx
subq %rax, %rcx
cmpq $0x21, %rcx
jb 0xc43a6
movq (%rax), %rsi
movq 0x8(%rax), %rdx
movq %r14, %rdi
callq 0x84e0
movq %rax, %r15
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x8450
movsbl %al, %esi
movq %r15, %rdi
callq 0x8040
movq %rax, %rdi
callq 0x8300
movq 0x40(%rbx), %rax
movq 0x20(%rax), %rsi
movq 0x28(%rax), %rdx
movq %r14, %rdi
callq 0x84e0
movq %rax, %r15
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x8450
movsbl %al, %esi
movq %r15, %rdi
callq 0x8040
movq %rax, %rdi
callq 0x8300
movq %rbx, %rdi
callq 0x8310
movq %rax, %rbx
cmpq %r13, %rax
jne 0xc431e
leaq 0xf0(%rsp), %rdi
callq 0x3c602
leaq 0xc0(%rsp), %rdi
callq 0xc2b7e
leaq 0x88(%rsp), %rdi
callq 0xbfb18
movq 0x28(%rsp), %rdi
cmpq %r12, %rdi
je 0xc43f8
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x8480
xorl %eax, %eax
addq $0x138, %rsp            # imm = 0x138
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1c576(%rip), %rdi     # 0xe0989
jmp 0xc441c
leaq 0x1c5ce(%rip), %rdi     # 0xe09ea
leaq 0x1cae7(%rip), %rsi     # 0xe0f0a
leaq 0x1cb03(%rip), %rcx     # 0xe0f2d
movl $0x5d, %edx
callq 0x82c0
movq %rax, %rbx
leaq 0x98(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0xc445d
movq 0x98(%rsp), %rsi
incq %rsi
callq 0x8480
jmp 0xc445d
movq %rax, %rbx
movq %r15, %r12
jmp 0xc4523
movq %rax, %rbx
movl $0x30, %esi
movq %r15, %rdi
callq 0x8480
jmp 0xc44bd
movq %rax, %rbx
jmp 0xc448c
movq %rax, %rbx
leaq 0x120(%rsp), %rdi
callq 0x2e784
movl $0x150, %esi            # imm = 0x150
movq %r12, %rdi
jmp 0xc44cf
jmp 0xc44f4
movq %rax, %rbx
movq 0x68(%rsp), %rdi
leaq 0x78(%rsp), %rax
cmpq %rax, %rdi
je 0xc44dd
movq 0x78(%rsp), %rsi
incq %rsi
jmp 0xc44cf
jmp 0xc44da
jmp 0xc44ba
jmp 0xc44f4
movq %rax, %rbx
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0xc44dd
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x8480
jmp 0xc44dd
jmp 0xc44f4
jmp 0xc44da
movq %rax, %rbx
leaq 0x48(%rsp), %rdi
callq 0x2e784
jmp 0xc44f7
jmp 0xc44f4
jmp 0xc44f4
jmp 0xc44f4
movq %rax, %rbx
jmp 0xc44fc
movq %rax, %rbx
leaq 0x38(%rsp), %r12
leaq 0xf0(%rsp), %rdi
callq 0x3c602
leaq 0xc0(%rsp), %rdi
callq 0xc2b7e
leaq 0x88(%rsp), %rdi
callq 0xbfb18
movq 0x28(%rsp), %rdi
cmpq %r12, %rdi
je 0xc453a
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x8480
movq %rbx, %rdi
callq 0x8840