// File: /workspace/github_cpp_makefile_star2_O1/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 $0x38, %rsp
movq %rsi, %rbp
movq %rdi, %r15
leaq 0x20(%rdi), %rbx
movq (%rsi), %rsi
movq 0x8(%rbp), %rdx
movq %rbx, %rdi
callq 0x276c0
movq %rax, %r12
leaq 0x4b8c1(%rip), %r13     # 0x7781a
movl $0x1, %edx
movq %rax, %rdi
movq %r13, %rsi
callq 0x276c0
movl 0xb4(%rbp), %esi
movq %r12, %rdi
callq 0x27400
movl $0x1, %edx
movq %rax, %rdi
movq %r13, %rsi
callq 0x276c0
movslq 0xa8(%rbp), %rax
testq %rax, %rax
js 0x2bfd2
movq 0x8(%r15), %rcx
movq 0x10(%r15), %rdx
subq %rcx, %rdx
shrq $0x3, %rdx
imull $0xcccccccd, %edx, %edx # imm = 0xCCCCCCCD
cmpl %edx, %eax
jge 0x2bfd2
leaq (%rax,%rax,4), %rax
movq (%rcx,%rax,8), %rsi
movq 0x8(%rcx,%rax,8), %rdx
movq %rbx, %rdi
callq 0x276c0
leaq 0x4b852(%rip), %rsi     # 0x7781a
movl $0x1, %edx
movq %rax, %rdi
jmp 0x2bfe1
leaq 0x463c9(%rip), %rsi     # 0x723a2
movl $0x2, %edx
movq %rbx, %rdi
callq 0x276c0
movl 0xac(%rbp), %esi
incl %esi
movq %rbx, %rdi
callq 0x27ac0
movq %rax, %r12
leaq 0x4b81a(%rip), %r13     # 0x7781a
movl $0x1, %edx
movq %rax, %rdi
movq %r13, %rsi
callq 0x276c0
movzwl 0xb2(%rbp), %esi
movq %r12, %rdi
callq 0x27400
movl $0x1, %edx
movq %rax, %rdi
movq %r13, %rsi
callq 0x276c0
movq 0xb8(%rbp), %r14
movq 0xc0(%rbp), %r13
cmpq %r13, %r14
je 0x2c081
leaq 0x8(%rsp), %r12
movl 0x4(%r14), %esi
movq %rbx, %rdi
callq 0x27400
movb (%r14), %cl
movb %cl, 0x8(%rsp)
movl $0x1, %edx
movq %rax, %rdi
movq %r12, %rsi
callq 0x276c0
addq $0x8, %r14
cmpq %r13, %r14
jne 0x2c047
leaq 0x4b7a0(%rip), %rsi     # 0x7781a
movl $0x1, %edx
jmp 0x2c08d
leaq 0x4631a(%rip), %rsi     # 0x723a2
movl $0x2, %edx
movq %rbx, %rdi
callq 0x276c0
movq %rbp, %rdi
callq 0x27330
testb %al, %al
je 0x2c0e6
movslq 0xd0(%rbp), %rax
testq %rax, %rax
js 0x2c0e6
movq 0x8(%r15), %rcx
movq 0x10(%r15), %rdx
subq %rcx, %rdx
shrq $0x3, %rdx
imull $0xcccccccd, %edx, %edx # imm = 0xCCCCCCCD
cmpl %edx, %eax
jge 0x2c0e6
cmpl 0xa8(%rbp), %eax
jne 0x2c44e
leaq 0x462cc(%rip), %rsi     # 0x723a5
movl $0x2, %edx
movq %rbx, %rdi
jmp 0x2c472
leaq 0x462bb(%rip), %rsi     # 0x723a8
movl $0x6, %edx
movq %rbx, %rdi
callq 0x276c0
movq 0x30(%rbp), %rdx
testq %rdx, %rdx
je 0x2c120
movq 0x28(%rbp), %rsi
movq %rbx, %rdi
callq 0x276c0
leaq 0x4b704(%rip), %rsi     # 0x7781a
movl $0x1, %edx
movq %rax, %rdi
jmp 0x2c12f
leaq 0x4627b(%rip), %rsi     # 0x723a2
movl $0x2, %edx
movq %rbx, %rdi
callq 0x276c0
movq 0x70(%rbp), %rdx
testq %rdx, %rdx
je 0x2c146
movq 0x68(%rbp), %rsi
cmpb $-0x1, (%rsi)
jne 0x2c152
leaq 0x4a2c0(%rip), %rsi     # 0x7640d
movl $0x1, %edx
movq %rbx, %rdi
callq 0x276c0
leaq 0x88(%rbp), %r15
movq %rbp, %rax
movq 0x88(%rbp), %rbp
movq %rax, 0x28(%rsp)
movq 0x90(%rax), %rax
movq %rax, 0x30(%rsp)
xorl %r14d, %r14d
leaq 0x7(%rsp), %r12
cmpq 0x30(%rsp), %r14
jae 0x2c418
movl $0x2, %ecx
leaq 0x8(%rsp), %rdi
movq %r15, %rsi
movq %r14, %rdx
callq 0x27600
movl $0x1, %edx
movq %rbx, %rdi
leaq 0x4b667(%rip), %rsi     # 0x7781a
callq 0x276c0
movq 0x8(%rsp), %rsi
movq 0x10(%rsp), %rdx
movq %rbx, %rdi
callq 0x276c0
movl $0x1, %edx
movq %rax, %rdi
leaq 0x4618e(%rip), %rsi     # 0x72367
callq 0x276c0
leaq 0x2(%r14), %rsi
movq 0x28(%rsp), %rax
movq 0x90(%rax), %rdx
cmpq %rsi, %rdx
jbe 0x2c4bf
movq (%r15), %rax
movb 0x2(%rax,%r14), %cl
leaq 0x3(%r14), %r13
movzbl %cl, %eax
cmpl $0x59, %eax
jg 0x2c259
leal -0x41(%rax), %edx
cmpl $0x8, %edx
ja 0x2c32b
leaq 0x45e0a(%rip), %rsi     # 0x72028
movslq (%rsi,%rdx,4), %rax
addq %rsi, %rax
jmpq *%rax
movl $0x2, %edx
movq %rbx, %rdi
leaq 0x46179(%rip), %rsi     # 0x723af
callq 0x276c0
movb (%rbp,%r13), %al
movb %al, 0x7(%rsp)
movl $0x1, %edx
movq %rbx, %rdi
movq %r12, %rsi
callq 0x276c0
jmp 0x2c380
cmpl $0x65, %eax
jle 0x2c2a0
cmpl $0x66, %eax
je 0x2c3a9
cmpl $0x69, %eax
je 0x2c3d1
cmpl $0x73, %eax
jne 0x2c323
movl $0x2, %edx
movq %rbx, %rdi
leaq 0x4612a(%rip), %rsi     # 0x723b2
callq 0x276c0
movswl (%rbp,%r13), %esi
movq %rbx, %rdi
callq 0x27510
jmp 0x2c352
cmpl $0x5a, %eax
je 0x2c2d1
cmpl $0x63, %eax
jne 0x2c323
movl $0x2, %edx
movq %rbx, %rdi
leaq 0x460f9(%rip), %rsi     # 0x723b2
callq 0x276c0
movsbl (%rbp,%r13), %esi
movq %rbx, %rdi
callq 0x27510
jmp 0x2c380
movb %cl, 0x7(%rsp)
movl $0x1, %edx
movq %rbx, %rdi
movq %r12, %rsi
callq 0x276c0
movl $0x1, %edx
movq %rax, %rdi
leaq 0x46073(%rip), %rsi     # 0x72367
callq 0x276c0
movb (%rbp,%r13), %al
testb %al, %al
je 0x2c320
movb %al, 0x7(%rsp)
movl $0x1, %edx
movq %rbx, %rdi
movq %r12, %rsi
callq 0x276c0
movb 0x1(%rbp,%r13), %al
incq %r13
jmp 0x2c2fe
incq %r13
movq %r13, %r14
jmp 0x2c3f6
cmpl $0x53, %eax
jne 0x2c323
movl $0x2, %edx
movq %rbx, %rdi
leaq 0x46073(%rip), %rsi     # 0x723b2
callq 0x276c0
movzwl (%rbp,%r13), %esi
movq %rbx, %rdi
callq 0x27400
addq $0x5, %r14
jmp 0x2c3f6
movl $0x2, %edx
movq %rbx, %rdi
leaq 0x46048(%rip), %rsi     # 0x723b2
callq 0x276c0
movsbq (%rbp,%r13), %rax
movzwl %ax, %esi
movq %rbx, %rdi
callq 0x27400
addq $0x4, %r14
jmp 0x2c3f6
movl $0x2, %edx
movq %rbx, %rdi
leaq 0x4601d(%rip), %rsi     # 0x723b2
callq 0x276c0
movl (%rbp,%r13), %esi
movq %rbx, %rdi
callq 0x27400
jmp 0x2c3f2
movl $0x2, %edx
movq %rbx, %rdi
leaq 0x45ffd(%rip), %rsi     # 0x723b5
callq 0x276c0
xorps %xmm0, %xmm0
cvtss2sd (%rbp,%r13), %xmm0
movq %rbx, %rdi
callq 0x279e0
jmp 0x2c3f2
movl $0x2, %edx
movq %rbx, %rdi
leaq 0x45fd2(%rip), %rsi     # 0x723b2
callq 0x276c0
movl (%rbp,%r13), %esi
movq %rbx, %rdi
callq 0x27ac0
addq $0x7, %r14
movb (%rbp,%r14), %r13b
movq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rax
cmpq %rax, %rdi
je 0x2c40f
callq 0x27560
testb %r13b, %r13b
jne 0x2c184
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x27590
movsbl %al, %esi
movq %rbx, %rdi
callq 0x27040
movq %rax, %rdi
callq 0x27410
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq (%rax,%rax,4), %rax
movq (%rcx,%rax,8), %rsi
movq 0x8(%rcx,%rax,8), %rdx
movq %rbx, %rdi
callq 0x276c0
leaq 0x4b3b0(%rip), %rsi     # 0x7781a
movl $0x1, %edx
movq %rax, %rdi
callq 0x276c0
movl 0xd4(%rbp), %esi
incl %esi
movq %rbx, %rdi
callq 0x27ac0
movq %rax, %r15
leaq 0x4b389(%rip), %r12     # 0x7781a
movl $0x1, %edx
movq %rax, %rdi
movq %r12, %rsi
callq 0x276c0
movl 0xd8(%rbp), %esi
movq %r15, %rdi
callq 0x27ac0
movl $0x1, %edx
movq %rax, %rdi
movq %r12, %rsi
jmp 0x2c0f5
leaq 0x46242(%rip), %rdi     # 0x72708
xorl %eax, %eax
callq 0x278c0
jmp 0x2c4d3
jmp 0x2c4d3
jmp 0x2c4d3
movq %rax, %rbx
movq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rax
cmpq %rax, %rdi
je 0x2c4ea
callq 0x27560
movq %rbx, %rdi
callq 0x27b20