// File: /workspace/github_cpp_makefile_star2_O2/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rsi, %r15
movq %rdi, %r14
leaq 0x20(%rdi), %rbx
movq %rbx, %rdi
callq 0x35510
leaq 0x3e19b(%rip), %r12     # 0x767f6
movq %rax, %rdi
movq %r12, %rsi
callq 0x355b0
movl 0xb4(%r15), %esi
movq %rax, %rdi
callq 0x35400
movq %rax, %rdi
movq %r12, %rsi
callq 0x355b0
movslq 0xa8(%r15), %rcx
pushq $0x28
popq %rdi
testq %rcx, %rcx
js 0x386be
movq 0x8(%r14), %rsi
movq 0x10(%r14), %rax
subq %rsi, %rax
cqto
idivq %rdi
cmpl %eax, %ecx
jge 0x386be
imulq $0x28, %rcx, %rax
addq %rax, %rsi
movq %rbx, %rdi
callq 0x35510
leaq 0x3e13d(%rip), %rsi     # 0x767f6
movq %rax, %rdi
jmp 0x386c8
leaq 0x38ce7(%rip), %rsi     # 0x713ac
movq %rbx, %rdi
callq 0x355b0
movl 0xac(%r15), %esi
incl %esi
movq %rbx, %rdi
callq 0x35b00
leaq 0x3e111(%rip), %r12     # 0x767f6
movq %rax, %rdi
movq %r12, %rsi
callq 0x355b0
movzwl 0xb2(%r15), %esi
movq %rax, %rdi
callq 0x35a90
movq %rax, %rdi
movq %r12, %rsi
callq 0x355b0
movq 0xb8(%r15), %rbp
movq 0xc0(%r15), %r13
cmpq %r13, %rbp
je 0x38740
cmpq %r13, %rbp
je 0x38747
movl 0x4(%rbp), %esi
movq %rbx, %rdi
callq 0x35400
movsbl (%rbp), %esi
movq %rax, %rdi
callq 0x356e0
addq $0x8, %rbp
jmp 0x3871e
leaq 0x38c65(%rip), %r12     # 0x713ac
movq %rbx, %rdi
movq %r12, %rsi
callq 0x355b0
movq %r15, %rdi
callq 0x35320
testb %al, %al
je 0x3879d
movslq 0xd0(%r15), %rcx
testq %rcx, %rcx
js 0x3879d
movq 0x8(%r14), %rsi
movq 0x10(%r14), %rax
subq %rsi, %rax
cqto
pushq $0x28
popq %rdi
idivq %rdi
cmpl %eax, %ecx
jge 0x3879d
cmpl 0xa8(%r15), %ecx
jne 0x38a52
leaq 0x38c1a(%rip), %rsi     # 0x713af
movq %rbx, %rdi
jmp 0x38a6b
leaq 0x38c0e(%rip), %rsi     # 0x713b2
movq %rbx, %rdi
callq 0x355b0
cmpq $0x0, 0x30(%r15)
je 0x387cb
leaq 0x28(%r15), %rsi
movq %rbx, %rdi
callq 0x35510
leaq 0x3e030(%rip), %rsi     # 0x767f6
movq %rax, %rdi
jmp 0x387d5
leaq 0x38bda(%rip), %rsi     # 0x713ac
movq %rbx, %rdi
callq 0x355b0
cmpq $0x0, 0x70(%r15)
je 0x38801
leaq 0x68(%r15), %r14
movq %r14, %rdi
xorl %esi, %esi
callq 0x35710
cmpb $-0x1, (%rax)
je 0x38801
movq %rbx, %rdi
movq %r14, %rsi
callq 0x35510
jmp 0x38810
leaq 0x3cbc5(%rip), %rsi     # 0x753cd
movq %rbx, %rdi
callq 0x355b0
leaq 0x88(%r15), %r14
movq 0x88(%r15), %r13
movq 0x90(%r15), %rax
movq %rax, (%rsp)
xorl %ebp, %ebp
cmpq (%rsp), %rbp
jae 0x38a3b
leaq 0x8(%rsp), %rdi
movq %r14, %rsi
movq %rbp, %rdx
pushq $0x2
popq %rcx
callq 0x35600
movq %rbx, %rdi
leaq 0x3dfa4(%rip), %rsi     # 0x767f6
callq 0x355b0
movq %rax, %rdi
leaq 0x8(%rsp), %rsi
callq 0x35510
movq %rax, %rdi
leaq 0x38b03(%rip), %rsi     # 0x71371
callq 0x355b0
leaq 0x2(%rbp), %rsi
movq %r14, %rdi
callq 0x35710
movb (%rax), %cl
leaq 0x3(%rbp), %r12
movzbl %cl, %eax
leal -0x41(%rax), %edx
cmpl $0x8, %edx
jbe 0x388e4
cmpl $0x53, %eax
je 0x389fc
cmpl $0x5a, %eax
je 0x38916
cmpl $0x63, %eax
je 0x38997
cmpl $0x66, %eax
je 0x389d8
cmpl $0x69, %eax
je 0x389ba
cmpl $0x73, %eax
jne 0x38a20
movq %rbx, %rdi
leaq 0x38af0(%rip), %rsi     # 0x713bc
callq 0x355b0
movswl (%r13,%r12), %esi
movq %rax, %rdi
callq 0x35520
jmp 0x38a19
leaq 0x3873d(%rip), %rsi     # 0x71028
movslq (%rsi,%rdx,4), %rax
addq %rsi, %rax
jmpq *%rax
movq %rbx, %rdi
leaq 0x38abb(%rip), %rsi     # 0x713b9
callq 0x355b0
movsbl (%r13,%r12), %esi
movq %rax, %rdi
callq 0x356e0
jmp 0x389b4
movsbl %cl, %esi
movq %rbx, %rdi
callq 0x356e0
movq %rax, %rdi
leaq 0x38a46(%rip), %rsi     # 0x71371
callq 0x355b0
addq $0x4, %rbp
movq %rbp, %r12
movb -0x1(%r13,%r12), %al
testb %al, %al
je 0x38a20
movsbl %al, %esi
movq %rbx, %rdi
callq 0x356e0
incq %r12
jmp 0x38937
movq %rbx, %rdi
leaq 0x38a5e(%rip), %rsi     # 0x713bc
callq 0x355b0
movl (%r13,%r12), %esi
movq %rax, %rdi
callq 0x35400
jmp 0x389f6
movq %rbx, %rdi
leaq 0x38a3d(%rip), %rsi     # 0x713bc
callq 0x355b0
movsbl (%r13,%r12), %ecx
movzwl %cx, %esi
movq %rax, %rdi
callq 0x35a90
jmp 0x389b4
movq %rbx, %rdi
leaq 0x38a1b(%rip), %rsi     # 0x713bc
callq 0x355b0
movsbl (%r13,%r12), %esi
movq %rax, %rdi
callq 0x35520
addq $0x4, %rbp
jmp 0x38a1d
movq %rbx, %rdi
leaq 0x389f8(%rip), %rsi     # 0x713bc
callq 0x355b0
movl (%r13,%r12), %esi
movq %rax, %rdi
callq 0x35b00
jmp 0x389f6
movq %rbx, %rdi
leaq 0x389dd(%rip), %rsi     # 0x713bf
callq 0x355b0
movss (%r13,%r12), %xmm0
movq %rax, %rdi
callq 0x35480
addq $0x7, %rbp
jmp 0x38a1d
movq %rbx, %rdi
leaq 0x389b6(%rip), %rsi     # 0x713bc
callq 0x355b0
movzwl (%r13,%r12), %esi
movq %rax, %rdi
callq 0x35a90
addq $0x5, %rbp
movq %rbp, %r12
movb (%r13,%r12), %r15b
leaq 0x8(%rsp), %rdi
callq 0x35730
movq %r12, %rbp
testb %r15b, %r15b
jne 0x3882b
movq %rbx, %rdi
callq 0x35300
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
imulq $0x28, %rcx, %rax
addq %rax, %rsi
movq %rbx, %rdi
callq 0x35510
leaq 0x3dd8e(%rip), %rsi     # 0x767f6
movq %rax, %rdi
callq 0x355b0
movl 0xd4(%r15), %esi
incl %esi
movq %rbx, %rdi
callq 0x35b00
leaq 0x3dd6e(%rip), %r14     # 0x767f6
movq %rax, %rdi
movq %r14, %rsi
callq 0x355b0
movl 0xd8(%r15), %esi
movq %rax, %rdi
callq 0x35b00
movq %rax, %rdi
movq %r14, %rsi
jmp 0x387a7
jmp 0x38ab1
jmp 0x38ab1
movq %rax, %rbx
leaq 0x8(%rsp), %rdi
callq 0x35730
movq %rbx, %rdi
callq 0x35b50
movq %rax, %rdi
callq 0x3a198