// File: /workspace/github_cpp_makefile_star2_O3/zeeev[P]DASVC/bamtools/src/toolkit/bamtools_convert.cpp
// Function: BamTools::ConvertTool::ConvertToolPrivate::PrintJson(BamTools::BamAlignment const&)
void ConvertTool::ConvertToolPrivate::PrintJson(const BamAlignment& a) {
  
    // write name & alignment flag
    m_out << "{\"name\":\"" << a.Name << "\",\"alignmentFlag\":\"" << a.AlignmentFlag << "\",";
    
    // write reference name
    if ( (a.RefID >= 0) && (a.RefID < (int)m_references.size()) ) 
        m_out << "\"reference\":\"" << m_references[a.RefID].RefName << "\",";
    
    // write position & map quality
    m_out << "\"position\":" << a.Position+1 << ",\"mapQuality\":" << a.MapQuality << ",";
    
    // write CIGAR
    const vector<CigarOp>& cigarData = a.CigarData;
    if ( !cigarData.empty() ) {
        m_out << "\"cigar\":[";
        vector<CigarOp>::const_iterator cigarBegin = cigarData.begin();
        vector<CigarOp>::const_iterator cigarIter  = cigarBegin;
        vector<CigarOp>::const_iterator cigarEnd   = cigarData.end();
        for ( ; cigarIter != cigarEnd; ++cigarIter ) {
            const CigarOp& op = (*cigarIter);
            if (cigarIter != cigarBegin)
                m_out << ",";
            m_out << "\"" << op.Length << op.Type << "\"";
        }
        m_out << "],";
    }
    
    // write mate reference name, mate position, & insert size
    if ( a.IsPaired() && (a.MateRefID >= 0) && (a.MateRefID < (int)m_references.size()) ) {
        m_out << "\"mate\":{"
              << "\"reference\":\"" << m_references[a.MateRefID].RefName << "\","
              << "\"position\":" << a.MatePosition+1
              << ",\"insertSize\":" << a.InsertSize << "},";
    }
    
    // write sequence
    if ( !a.QueryBases.empty() ) 
        m_out << "\"queryBases\":\"" << a.QueryBases << "\",";
    
    // write qualities
    if ( !a.Qualities.empty() && a.Qualities.at(0) != (char)0xFF ) {
        string::const_iterator s = a.Qualities.begin();
        m_out << "\"qualities\":[" << static_cast<short>(*s) - 33;
        ++s;
        for ( ; s != a.Qualities.end(); ++s )
            m_out << "," << static_cast<short>(*s) - 33;
        m_out << "],";
    }
    
    // write alignment's source BAM file
    m_out << "\"filename\":\"" << a.Filename << "\",";

    // write tag data
    const char* tagData = a.TagData.c_str();
    const size_t tagDataLength = a.TagData.length();
    size_t index = 0;
    if ( index < tagDataLength ) {

        m_out << "\"tags\":{";
        
        while ( index < tagDataLength ) {

            if ( index > 0 )
                m_out << ",";
            
            // write tag name
            m_out << "\"" << a.TagData.substr(index, 2) << "\":";
            index += 2;
            
            // get data type
            char type = a.TagData.at(index);
            ++index;
            switch ( type ) {
                case (Constants::BAM_TAG_TYPE_ASCII) :
                    m_out << "\"" << tagData[index] << "\"";
                    ++index; 
                    break;
                
                case (Constants::BAM_TAG_TYPE_INT8) :
                    // force value into integer-type (instead of char value)
                    m_out << 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 << static_cast<uint16_t>(tagData[index]);
                    ++index; 
                    break;
                
                case (Constants::BAM_TAG_TYPE_INT16) :
                    m_out << BamTools::UnpackSignedShort(&tagData[index]);
                    index += sizeof(int16_t);
                    break;

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

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

                case (Constants::BAM_TAG_TYPE_FLOAT) :
                    m_out << BamTools::UnpackFloat(&tagData[index]);
                    index += sizeof(float);
                    break;
                
                case (Constants::BAM_TAG_TYPE_HEX)    :
                case (Constants::BAM_TAG_TYPE_STRING) :
                    m_out << "\""; 
                    while (tagData[index]) {
                        if (tagData[index] == '\"')
                            m_out << "\\\""; // escape for json
                        else
                            m_out << tagData[index];
                        ++index;
                    }
                    m_out << "\""; 
                    ++index; 
                    break;      
            }
            
            if ( tagData[index] == '\0') 
                break;
        }

        m_out << "}";
    }

    m_out << "}" << endl;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rsi, %r13
movq %rdi, %r14
leaq 0x20(%rdi), %rbx
leaq 0x45b21(%rip), %rsi     # 0x712fa
movl $0x9, %edx
movq %rbx, %rdi
callq 0x276b0
movq (%r13), %rsi
movq 0x8(%r13), %rdx
movq %rbx, %rdi
callq 0x276b0
movq %rax, %r12
leaq 0x45b04(%rip), %rsi     # 0x71304
movl $0x13, %edx
movq %rax, %rdi
callq 0x276b0
movl 0xb4(%r13), %esi
movq %r12, %rdi
callq 0x27400
leaq 0x45af5(%rip), %rsi     # 0x71318
movl $0x2, %edx
movq %rax, %rdi
callq 0x276b0
movl 0xa8(%r13), %eax
testl %eax, %eax
js 0x2b899
movq 0x10(%r14), %rcx
subq 0x8(%r14), %rcx
shrq $0x3, %rcx
imull $0xcccccccd, %ecx, %ecx # imm = 0xCCCCCCCD
cmpl %ecx, %eax
jge 0x2b899
leaq 0x45ac3(%rip), %rsi     # 0x7131b
movl $0xd, %edx
movq %rbx, %rdi
callq 0x276b0
movslq 0xa8(%r13), %rax
movq 0x8(%r14), %rcx
leaq (%rax,%rax,4), %rax
movq (%rcx,%rax,8), %rsi
movq 0x8(%rcx,%rax,8), %rdx
movq %rbx, %rdi
callq 0x276b0
leaq 0x45a8c(%rip), %rsi     # 0x71318
movl $0x2, %edx
movq %rax, %rdi
callq 0x276b0
leaq 0x45a89(%rip), %rsi     # 0x71329
movl $0xb, %edx
movq %rbx, %rdi
callq 0x276b0
movl 0xac(%r13), %esi
incl %esi
movq %rbx, %rdi
callq 0x27ab0
movq %rax, %r12
leaq 0x45a6d(%rip), %rsi     # 0x71335
movl $0xe, %edx
movq %rax, %rdi
callq 0x276b0
movzwl 0xb2(%r13), %esi
movq %r12, %rdi
callq 0x27400
leaq 0x45a2d(%rip), %rsi     # 0x71319
movl $0x1, %edx
movq %rax, %rdi
callq 0x276b0
movq 0xb8(%r13), %rax
cmpq 0xc0(%r13), %rax
movq %r13, 0x8(%rsp)
je 0x2b9d1
movq %r14, (%rsp)
leaq 0x45a27(%rip), %rsi     # 0x71344
movl $0x9, %edx
movq %rbx, %rdi
callq 0x276b0
movq 0xb8(%r13), %r14
movq 0xc0(%r13), %r15
cmpq %r15, %r14
je 0x2b9b4
xorl %ebp, %ebp
leaq 0x459e1(%rip), %r12     # 0x71327
leaq 0x10(%rsp), %r13
testq %rbp, %rbp
je 0x2b964
movl $0x1, %edx
movq %rbx, %rdi
leaq 0x459ba(%rip), %rsi     # 0x71319
callq 0x276b0
movl $0x1, %edx
movq %rbx, %rdi
movq %r12, %rsi
callq 0x276b0
movl 0x4(%r14), %esi
movq %rbx, %rdi
callq 0x27400
movb (%r14), %cl
movb %cl, 0x10(%rsp)
movl $0x1, %edx
movq %rax, %rdi
movq %r13, %rsi
callq 0x276b0
movl $0x1, %edx
movq %rax, %rdi
movq %r12, %rsi
callq 0x276b0
addq $0x8, %r14
addq $-0x8, %rbp
cmpq %r15, %r14
jne 0x2b94b
leaq 0x45993(%rip), %rsi     # 0x7134e
movl $0x2, %edx
movq %rbx, %rdi
callq 0x276b0
movq 0x8(%rsp), %r13
movq (%rsp), %r14
movq %r13, %rdi
callq 0x27330
testb %al, %al
je 0x2bac8
movl 0xd0(%r13), %eax
testl %eax, %eax
js 0x2bac8
movq 0x10(%r14), %rcx
subq 0x8(%r14), %rcx
shrq $0x3, %rcx
imull $0xcccccccd, %ecx, %ecx # imm = 0xCCCCCCCD
cmpl %ecx, %eax
jge 0x2bac8
leaq 0x45940(%rip), %rsi     # 0x71351
movl $0x8, %edx
movq %rbx, %rdi
callq 0x276b0
leaq 0x458f6(%rip), %rsi     # 0x7131b
movl $0xd, %edx
movq %rbx, %rdi
callq 0x276b0
movslq 0xd0(%r13), %rax
movq 0x8(%r14), %rcx
leaq (%rax,%rax,4), %rax
movq (%rcx,%rax,8), %rsi
movq 0x8(%rcx,%rax,8), %rdx
movq %rbx, %rdi
callq 0x276b0
movq %rax, %r15
leaq 0x458bc(%rip), %rsi     # 0x71318
movl $0x2, %edx
movq %rax, %rdi
callq 0x276b0
leaq 0x458b9(%rip), %rsi     # 0x71329
movl $0xb, %edx
movq %r15, %rdi
callq 0x276b0
movl 0xd4(%r13), %esi
incl %esi
movq %r15, %rdi
callq 0x27ab0
movq %rax, %r15
leaq 0x458c2(%rip), %rsi     # 0x7135a
movl $0xe, %edx
movq %rax, %rdi
callq 0x276b0
movl 0xd8(%r13), %esi
movq %r15, %rdi
callq 0x27ab0
leaq 0x458ae(%rip), %rsi     # 0x71369
movl $0x2, %edx
movq %rax, %rdi
callq 0x276b0
cmpq $0x0, 0x30(%r13)
je 0x2bb07
leaq 0x45896(%rip), %rsi     # 0x7136c
movl $0xe, %edx
movq %rbx, %rdi
callq 0x276b0
movq 0x28(%r13), %rsi
movq 0x30(%r13), %rdx
movq %rbx, %rdi
callq 0x276b0
leaq 0x4581e(%rip), %rsi     # 0x71318
movl $0x2, %edx
movq %rax, %rdi
callq 0x276b0
cmpq $0x0, 0x70(%r13)
je 0x2bb99
movq 0x68(%r13), %r14
cmpb $-0x1, (%r14)
je 0x2bb99
leaq 0x45858(%rip), %rsi     # 0x7137b
movl $0xd, %edx
movq %rbx, %rdi
callq 0x276b0
movsbl (%r14), %esi
addl $-0x21, %esi
movq %rbx, %rdi
callq 0x27ab0
incq %r14
movq 0x68(%r13), %rax
addq 0x70(%r13), %rax
cmpq %rax, %r14
je 0x2bb85
leaq 0x457c3(%rip), %r15     # 0x71319
movl $0x1, %edx
movq %rbx, %rdi
movq %r15, %rsi
callq 0x276b0
movsbl (%r14), %esi
addl $-0x21, %esi
movq %rbx, %rdi
callq 0x27ab0
incq %r14
movq 0x68(%r13), %rax
addq 0x70(%r13), %rax
cmpq %rax, %r14
jne 0x2bb56
leaq 0x457c2(%rip), %rsi     # 0x7134e
movl $0x2, %edx
movq %rbx, %rdi
callq 0x276b0
leaq 0x457e9(%rip), %rsi     # 0x71389
movl $0xc, %edx
movq %rbx, %rdi
callq 0x276b0
movq 0xe0(%r13), %rsi
movq 0xe8(%r13), %rdx
movq %rbx, %rdi
callq 0x276b0
leaq 0x4574e(%rip), %rsi     # 0x71318
movl $0x2, %edx
movq %rax, %rdi
callq 0x276b0
movq 0x90(%r13), %rax
movq %rax, (%rsp)
testq %rax, %rax
je 0x2be7a
leaq 0x88(%r13), %r12
movq 0x88(%r13), %r14
leaq 0x45796(%rip), %rsi     # 0x71396
movl $0x8, %edx
movq %rbx, %rdi
callq 0x276b0
xorl %r15d, %r15d
leaq 0x10(%rsp), %r13
leaq 0x45783(%rip), %rbp     # 0x7139f
movq %r12, 0x30(%rsp)
cmpq (%rsp), %r15
jae 0x2be66
testq %r15, %r15
je 0x2bc44
movl $0x1, %edx
movq %rbx, %rdi
leaq 0x456da(%rip), %rsi     # 0x71319
callq 0x276b0
movl $0x1, %edx
movq %rbx, %rdi
leaq 0x456d4(%rip), %rsi     # 0x71327
callq 0x276b0
movl $0x2, %ecx
movq %r13, %rdi
movq %r12, %rsi
movq %r15, %rdx
callq 0x27600
movq 0x10(%rsp), %rsi
movq 0x18(%rsp), %rdx
movq %rbx, %rdi
callq 0x276b0
movl $0x2, %edx
movq %rax, %rdi
leaq 0x456da(%rip), %rsi     # 0x71366
callq 0x276b0
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
je 0x2bca5
callq 0x27560
leaq 0x2(%r15), %rsi
movq 0x8(%rsp), %rax
movq 0x90(%rax), %rdx
cmpq %rsi, %rdx
jbe 0x2bec4
movq (%r12), %rax
movzbl 0x2(%rax,%r15), %eax
leaq 0x3(%r15), %r12
cmpl $0x59, %eax
jg 0x2bd32
leal -0x41(%rax), %ecx
cmpl $0x8, %ecx
ja 0x2bdec
leaq 0x45320(%rip), %rdx     # 0x71004
movslq (%rdx,%rcx,4), %rax
addq %rdx, %rax
jmpq *%rax
movl $0x1, %edx
movq %rbx, %rdi
leaq 0x4562b(%rip), %r12     # 0x71327
movq %r12, %rsi
callq 0x276b0
movb 0x3(%r14,%r15), %al
movb %al, 0x10(%rsp)
movl $0x1, %edx
movq %rbx, %rdi
movq %r13, %rsi
callq 0x276b0
movl $0x1, %edx
movq %rax, %rdi
movq %r12, %rsi
callq 0x276b0
jmp 0x2be16
cmpl $0x65, %eax
jle 0x2bd65
cmpl $0x66, %eax
je 0x2be2b
cmpl $0x69, %eax
je 0x2be3f
cmpl $0x73, %eax
jne 0x2be53
movswl 0x3(%r14,%r15), %esi
movq %rbx, %rdi
callq 0x27510
jmp 0x2bdff
cmpl $0x5a, %eax
je 0x2bd86
cmpl $0x63, %eax
jne 0x2be53
movsbl 0x3(%r14,%r15), %esi
movq %rbx, %rdi
callq 0x27510
jmp 0x2be16
movl $0x1, %edx
movq %rbx, %rdi
leaq 0x45592(%rip), %rsi     # 0x71327
callq 0x276b0
addq $0x4, %r15
movq %r15, %r12
movzbl -0x1(%r14,%r12), %eax
cmpl $0x22, %eax
je 0x2bdc1
testl %eax, %eax
je 0x2bdd6
movb %al, 0x10(%rsp)
movl $0x1, %edx
movq %rbx, %rdi
movq %r13, %rsi
jmp 0x2bdcc
movl $0x2, %edx
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x276b0
incq %r12
jmp 0x2bda1
movl $0x1, %edx
movq %rbx, %rdi
leaq 0x45542(%rip), %rsi     # 0x71327
callq 0x276b0
jmp 0x2be53
cmpl $0x53, %eax
jne 0x2be53
movzwl 0x3(%r14,%r15), %esi
movq %rbx, %rdi
callq 0x27400
addq $0x5, %r15
jmp 0x2be50
movsbq 0x3(%r14,%r15), %rax
movzwl %ax, %esi
movq %rbx, %rdi
callq 0x27400
addq $0x4, %r15
jmp 0x2be50
movl 0x3(%r14,%r15), %esi
movq %rbx, %rdi
callq 0x27400
jmp 0x2be4c
xorps %xmm0, %xmm0
cvtss2sd 0x3(%r14,%r15), %xmm0
movq %rbx, %rdi
callq 0x279d0
jmp 0x2be4c
movl 0x3(%r14,%r15), %esi
movq %rbx, %rdi
callq 0x27ab0
addq $0x7, %r15
movq %r15, %r12
cmpb $0x0, (%r14,%r12)
movq %r12, %r15
movq 0x30(%rsp), %r12
jne 0x2bc21
leaq 0x4a9c6(%rip), %rsi     # 0x76833
movl $0x1, %edx
movq %rbx, %rdi
callq 0x276b0
leaq 0x4a9b2(%rip), %rsi     # 0x76833
movl $0x1, %edx
movq %rbx, %rdi
callq 0x276b0
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 0x4583d(%rip), %rdi     # 0x71708
xorl %eax, %eax
callq 0x278b0
movq %rax, %rbx
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
je 0x2bee9
callq 0x27560
movq %rbx, %rdi
callq 0x27b10
nop