// File: zeeev[P]DASVC/bamtools/src/toolkit/bamtools_resolve.cpp
// Function: BamTools::ResolveTool::ResolveToolPrivate::ResolvePairs()
bool ResolveTool::ResolveToolPrivate::ResolvePairs(void) {

    // open file containing read names of candidate proper pairs
    ResolveTool::ReadNamesFileReader readNamesReader;
    if ( !readNamesReader.Open(m_settings->ReadNamesFilename) ) {
        cerr << "bamtools resolve ERROR: could not open (temp) inputput read names file: "
             << m_settings->ReadNamesFilename << endl;
        return false;
    }

    // parse read names (matching with corresponding read groups)
    if ( !readNamesReader.Read(m_readGroups) ) {
        cerr << "bamtools resolve ERROR: could not read candidate read names from file: "
             << m_settings->ReadNamesFilename << endl;
        readNamesReader.Close();
        return false;
    }

    // close read name file reader & delete temp file
    readNamesReader.Close();
    if ( remove(m_settings->ReadNamesFilename.c_str()) != 0 ) {
        cerr << "bamtools resolve WARNING: could not delete temp file: "
             << m_settings->ReadNamesFilename << endl;
    }

    // open our BAM reader
    BamReader reader;
    if ( !reader.Open(m_settings->InputBamFilename) ) {
        cerr << "bamtools resolve ERROR: could not open input BAM file: "
             << m_settings->InputBamFilename << endl;
        return false;
    }

    // retrieve header & reference dictionary info
    const SamHeader& header = reader.GetHeader();
    const RefVector& references = reader.GetReferenceData();

    // determine compression mode for BamWriter
    bool writeUncompressed = ( m_settings->OutputBamFilename == Options::StandardOut() &&
                               !m_settings->IsForceCompression );
    BamWriter::CompressionMode compressionMode = BamWriter::Compressed;
    if ( writeUncompressed ) compressionMode = BamWriter::Uncompressed;

    // open BamWriter
    BamWriter writer;
    writer.SetCompressionMode(compressionMode);
    if ( !writer.Open(m_settings->OutputBamFilename, header, references) ) {
        cerr << "bamtools resolve ERROR: could not open "
             << m_settings->OutputBamFilename << " for writing." << endl;
        reader.Close();
        return false;
    }

    // plow through alignments, setting/clearing 'proper pair' flag
    // and writing to new output BAM file
    BamAlignment al;
    while ( reader.GetNextAlignment(al) ) {
        ResolveAlignment(al);
        writer.SaveAlignment(al);
    }

    // clean up & return success
    reader.Close();
    writer.Close();
    return true;
}

// Assembly:
subq $0x5b8, %rsp            # imm = 0x5B8
movq %rdi, 0x5a8(%rsp)
movq 0x5a8(%rsp), %rax
movq %rax, 0xa0(%rsp)
leaq 0x3a0(%rsp), %rdi
movq %rdi, 0xa8(%rsp)
callq 0xd00d0
movq 0xa0(%rsp), %rax
movq 0xa8(%rsp), %rdi
movq (%rax), %rsi
addq $0x70, %rsi
callq 0xc8280
movb %al, 0xb7(%rsp)
jmp 0xcbdf9
movb 0xb7(%rsp), %al
testb $0x1, %al
jne 0xcbe94
jmp 0xcbe0a
movq 0x991cf(%rip), %rdi     # 0x164fe0
leaq 0x653fe(%rip), %rsi     # 0x131216
callq 0x86700
movq %rax, 0x98(%rsp)
jmp 0xcbe27
movq 0x98(%rsp), %rdi
movq 0xa0(%rsp), %rax
movq (%rax), %rsi
addq $0x70, %rsi
callq 0x86640
movq %rax, 0x90(%rsp)
jmp 0xcbe4d
movq 0x90(%rsp), %rdi
movq 0x99114(%rip), %rsi     # 0x164f70
callq 0x86770
jmp 0xcbe63
movb $0x0, 0x5b7(%rsp)
movl $0x1, 0x390(%rsp)
jmp 0xcc3a8
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x398(%rsp)
movl %eax, 0x394(%rsp)
jmp 0xcc3c6
movq 0xa0(%rsp), %rsi
addq $0x8, %rsi
leaq 0x3a0(%rsp), %rdi
callq 0xc82d0
movb %al, 0x8f(%rsp)
jmp 0xcbeb6
movb 0x8f(%rsp), %al
testb $0x1, %al
jne 0xcbf3d
jmp 0xcbec3
movq 0x99116(%rip), %rdi     # 0x164fe0
leaq 0x6538e(%rip), %rsi     # 0x13125f
callq 0x86700
movq %rax, 0x80(%rsp)
jmp 0xcbee0
movq 0x80(%rsp), %rdi
movq 0xa0(%rsp), %rax
movq (%rax), %rsi
addq $0x70, %rsi
callq 0x86640
movq %rax, 0x78(%rsp)
jmp 0xcbf03
movq 0x78(%rsp), %rdi
movq 0x99061(%rip), %rsi     # 0x164f70
callq 0x86770
jmp 0xcbf16
leaq 0x3a0(%rsp), %rdi
callq 0xc8250
jmp 0xcbf25
movb $0x0, 0x5b7(%rsp)
movl $0x1, 0x390(%rsp)
jmp 0xcc3a8
leaq 0x3a0(%rsp), %rdi
callq 0xc8250
jmp 0xcbf4c
movq 0xa0(%rsp), %rax
movq (%rax), %rdi
addq $0x70, %rdi
callq 0x86320
movq %rax, 0x70(%rsp)
jmp 0xcbf67
movq 0x70(%rsp), %rdi
callq 0x86de0
cmpl $0x0, %eax
je 0xcbfc5
movq 0x99063(%rip), %rdi     # 0x164fe0
leaq 0x65323(%rip), %rsi     # 0x1312a7
callq 0x86700
movq %rax, 0x68(%rsp)
jmp 0xcbf90
movq 0x68(%rsp), %rdi
movq 0xa0(%rsp), %rax
movq (%rax), %rsi
addq $0x70, %rsi
callq 0x86640
movq %rax, 0x60(%rsp)
jmp 0xcbfb0
movq 0x60(%rsp), %rdi
movq 0x98fb4(%rip), %rsi     # 0x164f70
callq 0x86770
jmp 0xcbfc3
jmp 0xcbfc5
leaq 0x388(%rsp), %rdi
callq 0x864d0
jmp 0xcbfd4
movq 0xa0(%rsp), %rax
movq (%rax), %rsi
addq $0x10, %rsi
leaq 0x388(%rsp), %rdi
callq 0x86980
movb %al, 0x5f(%rsp)
jmp 0xcbff6
movb 0x5f(%rsp), %al
testb $0x1, %al
jne 0xcc082
jmp 0xcc004
movq 0x98fd5(%rip), %rdi     # 0x164fe0
leaq 0x65078(%rip), %rsi     # 0x13108a
callq 0x86700
movq %rax, 0x50(%rsp)
jmp 0xcc01e
movq 0x50(%rsp), %rdi
movq 0xa0(%rsp), %rax
movq (%rax), %rsi
addq $0x10, %rsi
callq 0x86640
movq %rax, 0x48(%rsp)
jmp 0xcc03e
movq 0x48(%rsp), %rdi
movq 0x98f26(%rip), %rsi     # 0x164f70
callq 0x86770
jmp 0xcc051
movb $0x0, 0x5b7(%rsp)
movl $0x1, 0x390(%rsp)
jmp 0xcc386
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x398(%rsp)
movl %eax, 0x394(%rsp)
jmp 0xcc397
leaq 0x228(%rsp), %rdi
leaq 0x388(%rsp), %rsi
callq 0x869b0
jmp 0xcc099
leaq 0x228(%rsp), %rax
movq %rax, 0x380(%rsp)
leaq 0x388(%rsp), %rdi
callq 0x860a0
movq %rax, 0x40(%rsp)
jmp 0xcc0bd
movq 0xa0(%rsp), %rax
movq 0x40(%rsp), %rcx
movq %rcx, 0x220(%rsp)
movq (%rax), %rax
addq $0x30, %rax
movq %rax, 0x30(%rsp)
callq 0x10f290
movq %rax, 0x38(%rsp)
jmp 0xcc0ea
movq 0x38(%rsp), %rsi
movq 0x30(%rsp), %rdi
callq 0x91630
movb %al, 0x2f(%rsp)
jmp 0xcc0ff
movb 0x2f(%rsp), %cl
xorl %eax, %eax
testb $0x1, %cl
movb %al, 0x2e(%rsp)
jne 0xcc110
jmp 0xcc124
movq 0xa0(%rsp), %rax
movq (%rax), %rax
movb 0x6(%rax), %al
xorb $-0x1, %al
movb %al, 0x2e(%rsp)
movb 0x2e(%rsp), %al
andb $0x1, %al
movb %al, 0x21f(%rsp)
movl $0x0, 0x218(%rsp)
testb $0x1, 0x21f(%rsp)
je 0xcc16c
movl $0x1, 0x218(%rsp)
jmp 0xcc16c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x398(%rsp)
movl %eax, 0x394(%rsp)
jmp 0xcc375
leaq 0x210(%rsp), %rdi
callq 0x863f0
jmp 0xcc17b
leaq 0x210(%rsp), %rdi
leaq 0x218(%rsp), %rsi
callq 0x86280
jmp 0xcc192
movq 0xa0(%rsp), %rax
movq (%rax), %rsi
addq $0x30, %rsi
movq 0x380(%rsp), %rdx
movq 0x220(%rsp), %rcx
leaq 0x210(%rsp), %rdi
callq 0x86230
movb %al, 0x2d(%rsp)
jmp 0xcc1c4
movb 0x2d(%rsp), %al
testb $0x1, %al
jne 0xcc277
jmp 0xcc1d2
movq 0x98e07(%rip), %rdi     # 0x164fe0
leaq 0x650fe(%rip), %rsi     # 0x1312de
callq 0x86700
movq %rax, 0x20(%rsp)
jmp 0xcc1ec
movq 0x20(%rsp), %rdi
movq 0xa0(%rsp), %rax
movq (%rax), %rsi
addq $0x30, %rsi
callq 0x86640
movq %rax, 0x18(%rsp)
jmp 0xcc20c
movq 0x18(%rsp), %rdi
leaq 0x6342d(%rip), %rsi     # 0x12f645
callq 0x86700
movq %rax, 0x10(%rsp)
jmp 0xcc224
movq 0x10(%rsp), %rdi
movq 0x98d40(%rip), %rsi     # 0x164f70
callq 0x86770
jmp 0xcc237
leaq 0x388(%rsp), %rdi
callq 0x860c0
jmp 0xcc246
movb $0x0, 0x5b7(%rsp)
movl $0x1, 0x390(%rsp)
jmp 0xcc344
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x398(%rsp)
movl %eax, 0x394(%rsp)
jmp 0xcc362
leaq 0xb8(%rsp), %rdi
callq 0x86260
jmp 0xcc286
jmp 0xcc288
leaq 0x388(%rsp), %rdi
leaq 0xb8(%rsp), %rsi
callq 0x86bb0
movb %al, 0xf(%rsp)
jmp 0xcc2a3
movb 0xf(%rsp), %al
testb $0x1, %al
jne 0xcc2ad
jmp 0xcc300
movq 0xa0(%rsp), %rdi
leaq 0xb8(%rsp), %rsi
callq 0xcba20
jmp 0xcc2c4
leaq 0x210(%rsp), %rdi
leaq 0xb8(%rsp), %rsi
callq 0x86e80
jmp 0xcc2db
jmp 0xcc288
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x398(%rsp)
movl %eax, 0x394(%rsp)
leaq 0xb8(%rsp), %rdi
callq 0x86b30
jmp 0xcc342
leaq 0x388(%rsp), %rdi
callq 0x860c0
jmp 0xcc30f
leaq 0x210(%rsp), %rdi
callq 0x86410
jmp 0xcc31e
movb $0x1, 0x5b7(%rsp)
movl $0x1, 0x390(%rsp)
leaq 0xb8(%rsp), %rdi
callq 0x86b30
jmp 0xcc340
jmp 0xcc344
jmp 0xcc362
leaq 0x210(%rsp), %rdi
callq 0x86510
jmp 0xcc353
leaq 0x228(%rsp), %rdi
callq 0x866e0
jmp 0xcc373
leaq 0x210(%rsp), %rdi
callq 0x86510
jmp 0xcc371
jmp 0xcc375
jmp 0xcc386
leaq 0x228(%rsp), %rdi
callq 0x866e0
jmp 0xcc384
jmp 0xcc397
leaq 0x388(%rsp), %rdi
callq 0x86790
jmp 0xcc395
jmp 0xcc3a8
leaq 0x388(%rsp), %rdi
callq 0x86790
jmp 0xcc3a6
jmp 0xcc3c6
leaq 0x3a0(%rsp), %rdi
callq 0xd00e0
movb 0x5b7(%rsp), %al
andb $0x1, %al
addq $0x5b8, %rsp            # imm = 0x5B8
retq
leaq 0x3a0(%rsp), %rdi
callq 0xd00e0
jmp 0xcc3d5
jmp 0xcc3d7
movq 0x398(%rsp), %rdi
callq 0x86d80
movq %rax, %rdi
callq 0x91290
nopl (%rax)