// File: /workspace/github_cpp_makefile_star2_O1/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 0xc51cf
leaq 0x38(%rsp), %r15
movq %r15, -0x10(%r15)
movq $0x0, -0x8(%r15)
movb $0x0, (%r15)
movq 0x489c6(%rip), %rsi     # 0x10d370
leaq 0x88(%rsp), %rdi
leaq 0xc0(%rsp), %rdx
callq 0x286de
leaq 0x88(%rsp), %r14
movq %r14, %rdi
movl $0x8, %esi
callq 0x53224
movq %rax, %rbx
leaq 0x98(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0xc49f8
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, 0x48955(%rip)     # 0x10d3b0
je 0xc4ca7
leaq 0x48(%rsp), %r15
leaq 0x68(%rsp), %r14
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x8450
movsbl %al, %edx
movq %rbx, %rdi
leaq 0x28(%rsp), %rsi
callq 0x8870
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0xc4d2a
movq 0x28(%rsp), %rax
movzbl (%rax), %ecx
cmpl $0x2a, %ecx
je 0xc4a6b
cmpl $0x42, %ecx
jne 0xc4ab9
cmpb $0x4c, 0x1(%rax)
je 0xc4a6b
movq %r15, %r13
movq %r15, %rdi
leaq 0x28(%rsp), %rsi
movl $0x9, %edx
callq 0x50624
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 0xc5099
cmpq $0x1, 0xc8(%rbp)
jne 0xc50a2
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
movq %r14, %rbp
movq %r14, %rdi
leaq 0x10(%rsp), %rdx
callq 0x286de
movq %rbp, %rdi
callq 0x5054c
movsd %xmm0, 0x60(%rsp)
movq 0x68(%rsp), %rdi
leaq 0x78(%rsp), %rax
cmpq %rax, %rdi
je 0xc4b72
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 0xc4bba
movq 0x18(%rsp), %rsi
cmpq 0x20(%rsp), %rsi
je 0xc4be0
movb 0xb(%rsp), %al
movb %al, (%rsi)
incq 0x18(%rsp)
jmp 0xc4bef
xorl $0x1, %r15d
orl %r12d, %r15d
jne 0xc4c80
movq 0x18(%rsp), %rsi
cmpq 0x20(%rsp), %rsi
je 0xc4c08
movb 0xa(%rsp), %al
movb %al, (%rsi)
incq 0x18(%rsp)
jmp 0xc4c17
leaq 0x10(%rsp), %rdi
leaq 0xb(%rsp), %rdx
callq 0x6ed3c
movq 0x18(%rsp), %rsi
cmpq 0x20(%rsp), %rsi
je 0xc4c01
movb 0xa(%rsp), %al
jmp 0xc4c27
leaq 0xa(%rsp), %rdx
jmp 0xc4c35
leaq 0x10(%rsp), %rdi
leaq 0xa(%rsp), %rdx
callq 0x6ed3c
movq 0x18(%rsp), %rsi
cmpq 0x20(%rsp), %rsi
je 0xc4c30
movb 0xb(%rsp), %al
movb %al, (%rsi)
incq 0x18(%rsp)
jmp 0xc4c3f
leaq 0xb(%rsp), %rdx
leaq 0x10(%rsp), %rdi
callq 0x6ed3c
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 0xc2fb4
leaq 0xc0(%rsp), %rdi
leaq 0xc(%rsp), %rsi
callq 0xc3016
movq %r15, (%rax)
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
movq %r13, %r15
je 0xc4c9a
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x8480
movq %r15, %rdi
callq 0x3123c
jmp 0xc4a6b
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 0xc4d2a
movq %r12, %rdi
movq %r15, %rsi
movl $0x9, %edx
callq 0x50624
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 0xc06d4
movq %r12, %rdi
callq 0x3123c
jmp 0xc4cbb
movq 0x462b7(%rip), %rdi     # 0x10afe8
leaq 0x2519d(%rip), %rsi     # 0xe9ed5
movl $0x26, %edx
callq 0x84e0
movq 0xe8(%rsp), %rsi
movq 0x46297(%rip), %rdi     # 0x10afe8
callq 0x82f0
movq %rax, %rbx
leaq 0x2519c(%rip), %rsi     # 0xe9efc
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 0x4624d(%rip), %rbx     # 0x10afe8
leaq 0x25161(%rip), %rsi     # 0xe9f03
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 0x4621b(%rip), %rdi     # 0x10afe8
callq 0x8040
movq %rax, %rdi
callq 0x8300
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
movl $0x1, %ebx
movq 0x461e5(%rip), %r14     # 0x10afd0
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 0xc4f86
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 0xc4ea5
movl $0xa, %edx
movq 0x4619c(%rip), %rdi     # 0x10afe8
leaq 0x135d3(%rip), %rsi     # 0xd8426
callq 0x84e0
movq 0x46189(%rip), %rdi     # 0x10afe8
movl %ebx, %esi
callq 0x8810
movq %rax, %r12
movl $0x6, %edx
movq %rax, %rdi
leaq 0x2509d(%rip), %rsi     # 0xe9f15
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 0x50624
movq 0x48(%rsp), %r12
movq 0x20(%r12), %rdi
callq 0x86e0
cmpl $0x7d0, %eax            # imm = 0x7D0
jg 0xc4f75
movq 0x80(%r12), %rdi
callq 0x86e0
cmpl 0x46714(%rip), %eax     # 0x10b5f8
jl 0xc4f75
movl $0x150, %edi            # imm = 0x150
callq 0x8470
movq %rax, %r12
leaq 0x120(%rsp), %r15
movq %r15, %rdi
movq %r13, %rsi
callq 0x3273a
movq %r12, %rdi
movq %r15, %rsi
callq 0xc307a
movq %r15, %rdi
callq 0x3123c
movq %r12, %rdi
leaq 0xc0(%rsp), %rsi
callq 0xc572a
movq 0x110(%r12), %rax
cmpq 0x108(%r12), %rax
je 0xc4f60
movq %r12, %rsi
addq $0x28, %rsi
leaq 0xf0(%rsp), %rdi
callq 0x49f44
movq %rax, %rdi
leaq 0x28(%rsp), %rsi
callq 0x33db0
movq %r12, %rdi
callq 0xc5460
movl $0x150, %esi            # imm = 0x150
movq %r12, %rdi
callq 0x8480
movq %r13, %rdi
callq 0x3123c
decl %ebp
incl %ebx
jmp 0xc4df0
movq 0x108(%rsp), %rbx
leaq 0xf8(%rsp), %r13
cmpq %r13, %rbx
leaq 0x38(%rsp), %r12
je 0xc5047
movq 0x4600d(%rip), %r14     # 0x10afb8
movq 0x40(%rbx), %rax
movq 0x48(%rbx), %rcx
subq %rax, %rcx
cmpq $0x21, %rcx
jb 0xc5033
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 0xc4fab
leaq 0xf0(%rsp), %rdi
callq 0x3f522
leaq 0xc0(%rsp), %rdi
callq 0xc37ae
leaq 0x88(%rsp), %rdi
callq 0xc0664
movq 0x28(%rsp), %rdi
cmpq %r12, %rdi
je 0xc5085
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 0x24871(%rip), %rdi     # 0xe9911
jmp 0xc50a9
leaq 0x248c9(%rip), %rdi     # 0xe9972
leaq 0x24de2(%rip), %rsi     # 0xe9e92
leaq 0x24dfe(%rip), %rcx     # 0xe9eb5
movl $0x5d, %edx
callq 0x82c0
movq %rax, %rbx
leaq 0x98(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0xc50ea
movq 0x98(%rsp), %rsi
incq %rsi
callq 0x8480
jmp 0xc50ea
movq %rax, %rbx
movq %r15, %r12
jmp 0xc51b0
jmp 0xc5147
movq %rax, %rbx
movl $0x30, %esi
movq %r15, %rdi
callq 0x8480
jmp 0xc514a
movq %rax, %rbx
jmp 0xc511b
movq %rax, %rbx
leaq 0x120(%rsp), %rdi
callq 0x3123c
movl $0x150, %esi            # imm = 0x150
movq %r12, %rdi
jmp 0xc515c
jmp 0xc5167
movq %rax, %rbx
movq 0x68(%rsp), %rdi
leaq 0x78(%rsp), %rax
cmpq %rax, %rdi
je 0xc516a
movq 0x78(%rsp), %rsi
incq %rsi
jmp 0xc515c
jmp 0xc5181
jmp 0xc5181
movq %rax, %rbx
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0xc516a
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x8480
jmp 0xc516a
jmp 0xc5181
jmp 0xc5167
movq %rax, %rbx
leaq 0x48(%rsp), %rdi
callq 0x3123c
jmp 0xc5184
jmp 0xc5181
jmp 0xc5181
jmp 0xc5181
movq %rax, %rbx
jmp 0xc5189
movq %rax, %rbx
leaq 0x38(%rsp), %r12
leaq 0xf0(%rsp), %rdi
callq 0x3f522
leaq 0xc0(%rsp), %rdi
callq 0xc37ae
leaq 0x88(%rsp), %rdi
callq 0xc0664
movq 0x28(%rsp), %rdi
cmpq %r12, %rdi
je 0xc51c7
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x8480
movq %rbx, %rdi
callq 0x8840