// File: zeeev[P]DASVC/bamtools/src/toolkit/bamtools_convert.cpp
// Function: BamTools::ConvertTool::ConvertToolPrivate::PrintSam(BamTools::BamAlignment const&)
void ConvertTool::ConvertToolPrivate::PrintSam(const BamAlignment& a) {
  
    // tab-delimited
    // <QNAME> <FLAG> <RNAME> <POS> <MAPQ> <CIGAR> <MRNM> <MPOS> <ISIZE> <SEQ> <QUAL> [ <TAG>:<VTYPE>:<VALUE> [...] ]
  
    // write name & alignment flag
   m_out << a.Name << "\t" << a.AlignmentFlag << "\t";

    // write reference name
    if ( (a.RefID >= 0) && (a.RefID < (int)m_references.size()) ) 
        m_out << m_references[a.RefID].RefName << "\t";
    else 
        m_out << "*\t";
    
    // write position & map quality
    m_out << a.Position+1 << "\t" << a.MapQuality << "\t";
    
    // write CIGAR
    const vector<CigarOp>& cigarData = a.CigarData;
    if ( cigarData.empty() ) m_out << "*\t";
    else {
        vector<CigarOp>::const_iterator cigarIter = cigarData.begin();
        vector<CigarOp>::const_iterator cigarEnd  = cigarData.end();
        for ( ; cigarIter != cigarEnd; ++cigarIter ) {
            const CigarOp& op = (*cigarIter);
            m_out << op.Length << op.Type;
        }
        m_out << "\t";
    }
    
    // write mate reference name, mate position, & insert size
    if ( a.IsPaired() && (a.MateRefID >= 0) && (a.MateRefID < (int)m_references.size()) ) {
        if ( a.MateRefID == a.RefID )
            m_out << "=\t";
        else
           m_out << m_references[a.MateRefID].RefName << "\t";
        m_out << a.MatePosition+1 << "\t" << a.InsertSize << "\t";
    } 
    else
        m_out << "*\t0\t0\t";
    
    // write sequence
    if ( a.QueryBases.empty() )
        m_out << "*\t";
    else
        m_out << a.QueryBases << "\t";
    
    // write qualities
    if ( a.Qualities.empty() || (a.Qualities.at(0) == (char)0xFF) )
        m_out << "*";
    else
        m_out << a.Qualities;
    
    // write tag data
    const char* tagData = a.TagData.c_str();
    const size_t tagDataLength = a.TagData.length();
    
    size_t index = 0;
    while ( index < tagDataLength ) {

        // write tag name   
        string tagName = a.TagData.substr(index, 2);
        m_out << "\t" << tagName << ":";
        index += 2;
        
        // get data type
        char type = a.TagData.at(index);
        ++index;
        switch ( type ) {
            case (Constants::BAM_TAG_TYPE_ASCII) :
                m_out << "A:" << tagData[index];
                ++index;
                break;

            case (Constants::BAM_TAG_TYPE_INT8) :
                // force value into integer-type (instead of char value)
                m_out << "i:" << static_cast<int16_t>(tagData[index]);
                ++index;
                break;

            case (Constants::BAM_TAG_TYPE_UINT8) :
                // force value into integer-type (instead of char value)
                m_out << "i:" << static_cast<uint16_t>(tagData[index]);
                ++index;
                break;

            case (Constants::BAM_TAG_TYPE_INT16) :
                m_out << "i:" << BamTools::UnpackSignedShort(&tagData[index]);
                index += sizeof(int16_t);
                break;

            case (Constants::BAM_TAG_TYPE_UINT16) :
                m_out << "i:" << BamTools::UnpackUnsignedShort(&tagData[index]);
                index += sizeof(uint16_t);
                break;

            case (Constants::BAM_TAG_TYPE_INT32) :
                m_out << "i:" << BamTools::UnpackSignedInt(&tagData[index]);
                index += sizeof(int32_t);
                break;

            case (Constants::BAM_TAG_TYPE_UINT32) :
                m_out << "i:" << BamTools::UnpackUnsignedInt(&tagData[index]);
                index += sizeof(uint32_t);
                break;

            case (Constants::BAM_TAG_TYPE_FLOAT) :
                m_out << "f:" << BamTools::UnpackFloat(&tagData[index]);
                index += sizeof(float);
                break;

            case (Constants::BAM_TAG_TYPE_HEX)    : // fall-through
            case (Constants::BAM_TAG_TYPE_STRING) :
                m_out << type << ":";
                while (tagData[index]) {
                    m_out << tagData[index];
                    ++index;
                }
                ++index;
                break;
        }

        if ( tagData[index] == '\0' )
            break;
    }

    m_out << endl;
}

// Assembly:
subq $0x138, %rsp            # imm = 0x138
movq %rdi, 0x130(%rsp)
movq %rsi, 0x128(%rsp)
movq 0x130(%rsp), %rdi
movq %rdi, 0xb0(%rsp)
addq $0x20, %rdi
movq 0x128(%rsp), %rsi
callq 0x86640
movq %rax, %rdi
leaq 0xa7540(%rip), %rsi     # 0x134c72
callq 0x86700
movq %rax, %rdi
movq 0x128(%rsp), %rax
movl 0xb4(%rax), %esi
callq 0x869c0
movq %rax, %rdi
leaq 0xa751b(%rip), %rsi     # 0x134c72
callq 0x86700
movq 0x128(%rsp), %rax
cmpl $0x0, 0xa8(%rax)
jl 0x8d7f5
movq 0xb0(%rsp), %rdi
movq 0x128(%rsp), %rax
movl 0xa8(%rax), %eax
movl %eax, 0xac(%rsp)
addq $0x8, %rdi
callq 0x91700
movq %rax, %rcx
movl 0xac(%rsp), %eax
cmpl %ecx, %eax
jge 0x8d7f5
movq 0xb0(%rsp), %rdi
movq %rdi, %rax
addq $0x20, %rax
movq %rax, 0xa0(%rsp)
addq $0x8, %rdi
movq 0x128(%rsp), %rax
movslq 0xa8(%rax), %rsi
callq 0x91720
movq 0xa0(%rsp), %rdi
movq %rax, %rsi
callq 0x86640
movq %rax, %rdi
leaq 0xa7484(%rip), %rsi     # 0x134c72
callq 0x86700
jmp 0x8d80d
movq 0xb0(%rsp), %rdi
addq $0x20, %rdi
leaq 0xa0cf4(%rip), %rsi     # 0x12e4fc
callq 0x86700
movq 0xb0(%rsp), %rdi
addq $0x20, %rdi
movq 0x128(%rsp), %rax
movl 0xac(%rax), %esi
addl $0x1, %esi
callq 0x86d40
movq %rax, %rdi
leaq 0xa7439(%rip), %rsi     # 0x134c72
callq 0x86700
movq %rax, %rdi
movq 0x128(%rsp), %rax
movzwl 0xb2(%rax), %esi
callq 0x86cb0
movq %rax, %rdi
leaq 0xa7413(%rip), %rsi     # 0x134c72
callq 0x86700
movq 0x128(%rsp), %rax
addq $0xb8, %rax
movq %rax, 0x120(%rsp)
movq 0x120(%rsp), %rdi
callq 0x91740
testb $0x1, %al
jne 0x8d88d
jmp 0x8d8aa
movq 0xb0(%rsp), %rdi
addq $0x20, %rdi
leaq 0xa0c5c(%rip), %rsi     # 0x12e4fc
callq 0x86700
jmp 0x8d95a
movq 0x120(%rsp), %rdi
callq 0x91790
movq %rax, 0x118(%rsp)
movq 0x120(%rsp), %rdi
callq 0x917c0
movq %rax, 0x110(%rsp)
leaq 0x118(%rsp), %rdi
leaq 0x110(%rsp), %rsi
callq 0x917f0
testb $0x1, %al
jne 0x8d8ef
jmp 0x8d942
leaq 0x118(%rsp), %rdi
callq 0x91830
movq 0xb0(%rsp), %rdi
movq %rax, 0x108(%rsp)
addq $0x20, %rdi
movq 0x108(%rsp), %rax
movl 0x4(%rax), %esi
callq 0x869c0
movq %rax, %rdi
movq 0x108(%rsp), %rax
movsbl (%rax), %esi
callq 0x86890
leaq 0x118(%rsp), %rdi
callq 0x91840
jmp 0x8d8d4
movq 0xb0(%rsp), %rdi
addq $0x20, %rdi
leaq 0xa731d(%rip), %rsi     # 0x134c72
callq 0x86700
movq 0x128(%rsp), %rdi
callq 0x863d0
testb $0x1, %al
jne 0x8d970
jmp 0x8da9b
movq 0x128(%rsp), %rax
cmpl $0x0, 0xd0(%rax)
jl 0x8da9b
movq 0xb0(%rsp), %rdi
movq 0x128(%rsp), %rax
movl 0xd0(%rax), %eax
movl %eax, 0x9c(%rsp)
addq $0x8, %rdi
callq 0x91700
movq %rax, %rcx
movl 0x9c(%rsp), %eax
cmpl %ecx, %eax
jge 0x8da9b
movq 0x128(%rsp), %rax
movl 0xd0(%rax), %eax
movq 0x128(%rsp), %rcx
cmpl 0xa8(%rcx), %eax
jne 0x8d9f5
movq 0xb0(%rsp), %rdi
addq $0x20, %rdi
leaq 0xa0b11(%rip), %rsi     # 0x12e4ff
callq 0x86700
jmp 0x8da43
movq 0xb0(%rsp), %rdi
movq %rdi, %rax
addq $0x20, %rax
movq %rax, 0x90(%rsp)
addq $0x8, %rdi
movq 0x128(%rsp), %rax
movslq 0xd0(%rax), %rsi
callq 0x91720
movq 0x90(%rsp), %rdi
movq %rax, %rsi
callq 0x86640
movq %rax, %rdi
leaq 0xa7234(%rip), %rsi     # 0x134c72
callq 0x86700
movq 0xb0(%rsp), %rdi
addq $0x20, %rdi
movq 0x128(%rsp), %rax
movl 0xd4(%rax), %esi
addl $0x1, %esi
callq 0x86d40
movq %rax, %rdi
leaq 0xa7203(%rip), %rsi     # 0x134c72
callq 0x86700
movq %rax, %rdi
movq 0x128(%rsp), %rax
movl 0xd8(%rax), %esi
callq 0x86d40
movq %rax, %rdi
leaq 0xa71de(%rip), %rsi     # 0x134c72
callq 0x86700
jmp 0x8dab3
movq 0xb0(%rsp), %rdi
addq $0x20, %rdi
leaq 0xa0a54(%rip), %rsi     # 0x12e502
callq 0x86700
movq 0x128(%rsp), %rdi
addq $0x28, %rdi
callq 0x86c90
testb $0x1, %al
jne 0x8daca
jmp 0x8dae4
movq 0xb0(%rsp), %rdi
addq $0x20, %rdi
leaq 0xa0a1f(%rip), %rsi     # 0x12e4fc
callq 0x86700
jmp 0x8db10
movq 0xb0(%rsp), %rdi
addq $0x20, %rdi
movq 0x128(%rsp), %rsi
addq $0x28, %rsi
callq 0x86640
movq %rax, %rdi
leaq 0xa7167(%rip), %rsi     # 0x134c72
callq 0x86700
movq 0x128(%rsp), %rdi
addq $0x68, %rdi
callq 0x86c90
testb $0x1, %al
jne 0x8db42
movq 0x128(%rsp), %rdi
addq $0x68, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x868c0
movsbl (%rax), %eax
cmpl $-0x1, %eax
jne 0x8db5c
movq 0xb0(%rsp), %rdi
addq $0x20, %rdi
leaq 0xa5a28(%rip), %rsi     # 0x13357d
callq 0x86700
jmp 0x8db79
movq 0xb0(%rsp), %rdi
addq $0x20, %rdi
movq 0x128(%rsp), %rsi
addq $0x68, %rsi
callq 0x86640
movq 0x128(%rsp), %rdi
addq $0x88, %rdi
callq 0x86320
movq %rax, 0x100(%rsp)
movq 0x128(%rsp), %rdi
addq $0x88, %rdi
callq 0x86ac0
movq %rax, 0xf8(%rsp)
movq $0x0, 0xf0(%rsp)
movq 0xf0(%rsp), %rax
cmpq 0xf8(%rsp), %rax
jae 0x8e106
movq 0x128(%rsp), %rsi
addq $0x88, %rsi
movq 0xf0(%rsp), %rdx
leaq 0xd0(%rsp), %rdi
movl $0x2, %ecx
callq 0x86750
movq 0xb0(%rsp), %rdi
addq $0x20, %rdi
leaq 0xa7063(%rip), %rsi     # 0x134c72
callq 0x86700
movq %rax, 0x88(%rsp)
jmp 0x8dc1e
movq 0x88(%rsp), %rdi
leaq 0xd0(%rsp), %rsi
callq 0x86640
movq %rax, 0x80(%rsp)
jmp 0x8dc3d
movq 0x80(%rsp), %rdi
leaq 0xa0875(%rip), %rsi     # 0x12e4c1
callq 0x86700
jmp 0x8dc53
movq 0xf0(%rsp), %rax
addq $0x2, %rax
movq %rax, 0xf0(%rsp)
movq 0x128(%rsp), %rdi
addq $0x88, %rdi
movq 0xf0(%rsp), %rsi
callq 0x868c0
movq %rax, 0x78(%rsp)
jmp 0x8dc8a
movq 0x78(%rsp), %rax
movb (%rax), %al
movb %al, 0xc3(%rsp)
movq 0xf0(%rsp), %rax
incq %rax
movq %rax, 0xf0(%rsp)
movsbl 0xc3(%rsp), %eax
addl $-0x41, %eax
movl %eax, %ecx
movq %rcx, 0x70(%rsp)
subl $0x32, %eax
ja 0x8e0b2
movq 0x70(%rsp), %rax
leaq 0xa03fe(%rip), %rcx     # 0x12e0d0
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xc8(%rsp)
movl %eax, 0xc4(%rsp)
leaq 0xd0(%rsp), %rdi
callq 0x86f68
jmp 0x8e104
movq 0xb0(%rsp), %rdi
addq $0x20, %rdi
leaq 0xa07f5(%rip), %rsi     # 0x12e509
callq 0x86700
movq %rax, 0x68(%rsp)
jmp 0x8dd20
movq 0x68(%rsp), %rdi
movq 0x100(%rsp), %rax
movq 0xf0(%rsp), %rcx
movsbl (%rax,%rcx), %esi
callq 0x86890
jmp 0x8dd40
movq 0xf0(%rsp), %rax
addq $0x1, %rax
movq %rax, 0xf0(%rsp)
jmp 0x8e0b2
movq 0xb0(%rsp), %rdi
addq $0x20, %rdi
leaq 0xa07a0(%rip), %rsi     # 0x12e50c
callq 0x86700
movq %rax, 0x60(%rsp)
jmp 0x8dd78
movq 0x60(%rsp), %rdi
movq 0x100(%rsp), %rax
movq 0xf0(%rsp), %rcx
movsbl (%rax,%rcx), %esi
callq 0x86650
jmp 0x8dd98
movq 0xf0(%rsp), %rax
addq $0x1, %rax
movq %rax, 0xf0(%rsp)
jmp 0x8e0b2
movq 0xb0(%rsp), %rdi
addq $0x20, %rdi
leaq 0xa0748(%rip), %rsi     # 0x12e50c
callq 0x86700
movq %rax, 0x58(%rsp)
jmp 0x8ddd0
movq 0x58(%rsp), %rdi
movq 0x100(%rsp), %rax
movq 0xf0(%rsp), %rcx
movsbl (%rax,%rcx), %eax
movzwl %ax, %esi
callq 0x86cb0
jmp 0x8ddf3
movq 0xf0(%rsp), %rax
addq $0x1, %rax
movq %rax, 0xf0(%rsp)
jmp 0x8e0b2
movq 0xb0(%rsp), %rdi
addq $0x20, %rdi
leaq 0xa06ed(%rip), %rsi     # 0x12e50c
callq 0x86700
movq %rax, 0x50(%rsp)
jmp 0x8de2b
movq 0x100(%rsp), %rdi
movq 0xf0(%rsp), %rax
addq %rax, %rdi
callq 0x918d0
movw %ax, 0x4e(%rsp)
jmp 0x8de4a
movq 0x50(%rsp), %rdi
movw 0x4e(%rsp), %ax
movswl %ax, %esi
callq 0x86650
jmp 0x8de5e
movq 0xf0(%rsp), %rax
addq $0x2, %rax
movq %rax, 0xf0(%rsp)
jmp 0x8e0b2
movq 0xb0(%rsp), %rdi
addq $0x20, %rdi
leaq 0xa0682(%rip), %rsi     # 0x12e50c
callq 0x86700
movq %rax, 0x40(%rsp)
jmp 0x8de96
movq 0x100(%rsp), %rdi
movq 0xf0(%rsp), %rax
addq %rax, %rdi
callq 0x91900
movw %ax, 0x3e(%rsp)
jmp 0x8deb5
movq 0x40(%rsp), %rdi
movw 0x3e(%rsp), %ax
movzwl %ax, %esi
callq 0x86cb0
jmp 0x8dec9
movq 0xf0(%rsp), %rax
addq $0x2, %rax
movq %rax, 0xf0(%rsp)
jmp 0x8e0b2
movq 0xb0(%rsp), %rdi
addq $0x20, %rdi
leaq 0xa0617(%rip), %rsi     # 0x12e50c
callq 0x86700
movq %rax, 0x30(%rsp)
jmp 0x8df01
movq 0x100(%rsp), %rdi
movq 0xf0(%rsp), %rax
addq %rax, %rdi
callq 0x91930
movl %eax, 0x2c(%rsp)
jmp 0x8df1f
movl 0x2c(%rsp), %esi
movq 0x30(%rsp), %rdi
callq 0x86d40
jmp 0x8df2f
movq 0xf0(%rsp), %rax
addq $0x4, %rax
movq %rax, 0xf0(%rsp)
jmp 0x8e0b2
movq 0xb0(%rsp), %rdi
addq $0x20, %rdi
leaq 0xa05b1(%rip), %rsi     # 0x12e50c
callq 0x86700
movq %rax, 0x20(%rsp)
jmp 0x8df67
movq 0x100(%rsp), %rdi
movq 0xf0(%rsp), %rax
addq %rax, %rdi
callq 0x91980
movl %eax, 0x1c(%rsp)
jmp 0x8df85
movl 0x1c(%rsp), %esi
movq 0x20(%rsp), %rdi
callq 0x869c0
jmp 0x8df95
movq 0xf0(%rsp), %rax
addq $0x4, %rax
movq %rax, 0xf0(%rsp)
jmp 0x8e0b2
movq 0xb0(%rsp), %rdi
addq $0x20, %rdi
leaq 0xa054e(%rip), %rsi     # 0x12e50f
callq 0x86700
movq %rax, 0x10(%rsp)
jmp 0x8dfcd
movq 0x100(%rsp), %rdi
movq 0xf0(%rsp), %rax
addq %rax, %rdi
callq 0x919d0
movss %xmm0, 0xc(%rsp)
jmp 0x8dfed
movss 0xc(%rsp), %xmm0
movq 0x10(%rsp), %rdi
callq 0x86570
jmp 0x8dfff
movq 0xf0(%rsp), %rax
addq $0x4, %rax
movq %rax, 0xf0(%rsp)
jmp 0x8e0b2
movq 0xb0(%rsp), %rdi
addq $0x20, %rdi
movsbl 0xc3(%rsp), %esi
callq 0x86890
movq %rax, (%rsp)
jmp 0x8e037
movq (%rsp), %rdi
leaq 0xa047f(%rip), %rsi     # 0x12e4c1
callq 0x86700
jmp 0x8e049
jmp 0x8e04b
movq 0x100(%rsp), %rax
movq 0xf0(%rsp), %rcx
cmpb $0x0, (%rax,%rcx)
je 0x8e09e
movq 0xb0(%rsp), %rdi
addq $0x20, %rdi
movq 0x100(%rsp), %rax
movq 0xf0(%rsp), %rcx
movsbl (%rax,%rcx), %esi
callq 0x86890
jmp 0x8e088
movq 0xf0(%rsp), %rax
addq $0x1, %rax
movq %rax, 0xf0(%rsp)
jmp 0x8e04b
movq 0xf0(%rsp), %rax
addq $0x1, %rax
movq %rax, 0xf0(%rsp)
movq 0x100(%rsp), %rax
movq 0xf0(%rsp), %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x0, %eax
jne 0x8e0d8
movl $0x6, 0xbc(%rsp)
jmp 0x8e0e3
movl $0x0, 0xbc(%rsp)
leaq 0xd0(%rsp), %rdi
callq 0x86f68
movl 0xbc(%rsp), %eax
testl %eax, %eax
je 0x8e0ff
jmp 0x8e0fd
jmp 0x8e106
jmp 0x8dbbd
jmp 0x8e126
movq 0xb0(%rsp), %rdi
addq $0x20, %rdi
movq 0xd6e57(%rip), %rsi     # 0x164f70
callq 0x86770
addq $0x138, %rsp            # imm = 0x138
retq
movq 0xc8(%rsp), %rdi
callq 0x86d80
movq %rax, %rdi
callq 0x91290
nopl (%rax,%rax)