// File: /workspace/github_cpp_makefile_star2_O2/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 $0x28, %rsp
movq %rsi, %r14
movq %rdi, %r15
leaq 0x20(%rdi), %rbx
leaq 0x39253(%rip), %rsi     # 0x71304
movq %rbx, %rdi
callq 0x355b0
movq %rax, %rdi
movq %r14, %rsi
callq 0x35510
leaq 0x39243(%rip), %rsi     # 0x7130e
movq %rax, %rdi
callq 0x355b0
movl 0xb4(%r14), %esi
movq %rax, %rdi
callq 0x35400
leaq 0x39239(%rip), %rsi     # 0x71322
movq %rax, %rdi
callq 0x355b0
movl 0xa8(%r14), %ecx
pushq $0x28
popq %r12
testl %ecx, %ecx
js 0x38146
movq 0x10(%r15), %rax
subq 0x8(%r15), %rax
cqto
idivq %r12
cmpl %eax, %ecx
jge 0x38146
leaq 0x3920d(%rip), %rsi     # 0x71325
movq %rbx, %rdi
callq 0x355b0
movslq 0xa8(%r14), %rcx
imulq $0x28, %rcx, %rsi
addq 0x8(%r15), %rsi
movq %rax, %rdi
callq 0x35510
leaq 0x391e4(%rip), %rsi     # 0x71322
movq %rax, %rdi
callq 0x355b0
leaq 0x391e6(%rip), %rsi     # 0x71333
movq %rbx, %rdi
callq 0x355b0
movl 0xac(%r14), %esi
incl %esi
movq %rax, %rdi
callq 0x35b00
leaq 0x391d2(%rip), %rsi     # 0x7133f
movq %rax, %rdi
callq 0x355b0
movzwl 0xb2(%r14), %esi
movq %rax, %rdi
callq 0x35a90
leaq 0x39197(%rip), %r13     # 0x71323
movq %rax, %rdi
movq %r13, %rsi
callq 0x355b0
movq 0xb8(%r14), %rax
cmpq 0xc0(%r14), %rax
je 0x38243
movq %r15, (%rsp)
leaq 0x39198(%rip), %rsi     # 0x7134e
movq %rbx, %rdi
callq 0x355b0
movq 0xb8(%r14), %rbp
movq 0xc0(%r14), %r15
leaq 0x3915e(%rip), %r13     # 0x71331
movq %rbp, %r12
cmpq %r15, %r12
je 0x38225
cmpq %rbp, %r12
je 0x381ef
movq %rbx, %rdi
leaq 0x39139(%rip), %rsi     # 0x71323
callq 0x355b0
movq %rbx, %rdi
movq %r13, %rsi
callq 0x355b0
movl 0x4(%r12), %esi
movq %rax, %rdi
callq 0x35400
movsbl (%r12), %esi
movq %rax, %rdi
callq 0x356e0
movq %rax, %rdi
movq %r13, %rsi
callq 0x355b0
addq $0x8, %r12
jmp 0x381d6
leaq 0x3912c(%rip), %rsi     # 0x71358
movq %rbx, %rdi
callq 0x355b0
leaq 0x390e8(%rip), %r13     # 0x71323
movq (%rsp), %r15
pushq $0x28
popq %r12
movq %r14, %rdi
callq 0x35320
testb %al, %al
je 0x38308
movl 0xd0(%r14), %ecx
testl %ecx, %ecx
js 0x38308
movq 0x10(%r15), %rax
subq 0x8(%r15), %rax
cqto
idivq %r12
cmpl %eax, %ecx
jge 0x38308
leaq 0x390dd(%rip), %rsi     # 0x7135b
movq %rbx, %rdi
callq 0x355b0
leaq 0x39098(%rip), %rsi     # 0x71325
movq %rax, %rdi
callq 0x355b0
movslq 0xd0(%r14), %rcx
imulq $0x28, %rcx, %rsi
addq 0x8(%r15), %rsi
movq %rax, %rdi
callq 0x35510
leaq 0x3906f(%rip), %rsi     # 0x71322
movq %rax, %rdi
callq 0x355b0
leaq 0x39071(%rip), %rsi     # 0x71333
movq %rax, %rdi
callq 0x355b0
movl 0xd4(%r14), %esi
incl %esi
movq %rax, %rdi
callq 0x35b00
leaq 0x39082(%rip), %rsi     # 0x71364
movq %rax, %rdi
callq 0x355b0
movl 0xd8(%r14), %esi
movq %rax, %rdi
callq 0x35b00
leaq 0x39073(%rip), %rsi     # 0x71373
movq %rax, %rdi
callq 0x355b0
cmpq $0x0, 0x30(%r14)
je 0x3833c
leaq 0x28(%r14), %r12
leaq 0x3905c(%rip), %rsi     # 0x71376
movq %rbx, %rdi
callq 0x355b0
movq %rax, %rdi
movq %r12, %rsi
callq 0x35510
leaq 0x38fee(%rip), %rsi     # 0x71322
movq %rax, %rdi
callq 0x355b0
cmpq $0x0, 0x70(%r14)
je 0x3839a
leaq 0x68(%r14), %r12
movq %r12, %rdi
xorl %esi, %esi
callq 0x35710
cmpb $-0x1, (%rax)
je 0x3839a
movq (%r12), %r15
leaq 0x39024(%rip), %rsi     # 0x71385
movq %rbx, %rdi
callq 0x355b0
movsbl (%r15), %esi
addl $-0x21, %esi
movq %rax, %rdi
callq 0x35b00
incq %r15
movq 0x68(%r14), %rax
addq 0x70(%r14), %rax
movq %r13, %rsi
cmpq %rax, %r15
jne 0x38361
leaq 0x38fc6(%rip), %rsi     # 0x71358
movq %rbx, %rdi
callq 0x355b0
leaq 0x38ff2(%rip), %rsi     # 0x71393
movq %rbx, %rdi
callq 0x355b0
leaq 0xe0(%r14), %rsi
movq %rax, %rdi
callq 0x35510
leaq 0x38f63(%rip), %rsi     # 0x71322
movq %rax, %rdi
callq 0x355b0
movq 0x90(%r14), %rax
movq %rax, (%rsp)
testq %rax, %rax
je 0x385f0
movq 0x88(%r14), %r12
addq $0x88, %r14
leaq 0x38fb0(%rip), %rsi     # 0x713a0
movq %rbx, %rdi
callq 0x355b0
xorl %ebp, %ebp
cmpq (%rsp), %rbp
jae 0x385e1
movq %r14, %r13
testq %rbp, %rbp
je 0x3841b
movq %rbx, %rdi
leaq 0x38f0d(%rip), %rsi     # 0x71323
callq 0x355b0
movq %rbx, %rdi
leaq 0x38f0c(%rip), %rsi     # 0x71331
callq 0x355b0
movq %rax, %r15
leaq 0x8(%rsp), %r14
movq %r14, %rdi
movq %r13, %rsi
movq %rbp, %rdx
pushq $0x2
popq %rcx
callq 0x35600
movq %r15, %rdi
movq %r14, %rsi
callq 0x35510
movq %rax, %rdi
leaq 0x38f18(%rip), %rsi     # 0x71370
callq 0x355b0
leaq 0x8(%rsp), %rdi
callq 0x35730
leaq 0x2(%rbp), %rsi
movq %r13, %r14
movq %r13, %rdi
callq 0x35710
movzbl (%rax), %eax
leaq 0x3(%rbp), %r15
leal -0x41(%rax), %ecx
cmpl $0x8, %ecx
jbe 0x384ca
cmpl $0x53, %eax
je 0x385be
cmpl $0x5a, %eax
je 0x3850a
cmpl $0x63, %eax
je 0x38586
cmpl $0x66, %eax
je 0x385a9
cmpl $0x69, %eax
je 0x3859a
cmpl $0x73, %eax
jne 0x385d3
movswl 0x3(%r12,%rbp), %esi
movq %rbx, %rdi
callq 0x35520
jmp 0x385cc
leaq 0x38b33(%rip), %rdx     # 0x71004
movslq (%rdx,%rcx,4), %rax
addq %rdx, %rax
jmpq *%rax
movq %rbx, %rdi
leaq 0x38e4d(%rip), %r15     # 0x71331
movq %r15, %rsi
callq 0x355b0
movsbl 0x3(%r12,%rbp), %esi
movq %rax, %rdi
callq 0x356e0
movq %rax, %rdi
movq %r15, %rsi
callq 0x355b0
jmp 0x38594
movq %rbx, %rdi
leaq 0x38e1d(%rip), %rsi     # 0x71331
callq 0x355b0
addq $0x4, %rbp
movq %rbp, %r15
leaq 0x38e82(%rip), %r13     # 0x713a9
movzbl -0x1(%r12,%r15), %eax
cmpl $0x22, %eax
je 0x38543
testl %eax, %eax
je 0x38553
movsbl %al, %esi
movq %rbx, %rdi
callq 0x356e0
jmp 0x3854e
movq %rbx, %rdi
movq %r13, %rsi
callq 0x355b0
incq %r15
jmp 0x38527
movq %rbx, %rdi
leaq 0x38dd4(%rip), %rsi     # 0x71331
callq 0x355b0
jmp 0x385d3
movl 0x3(%r12,%rbp), %esi
movq %rbx, %rdi
callq 0x35400
jmp 0x385b8
movsbl 0x3(%r12,%rbp), %eax
movzwl %ax, %esi
movq %rbx, %rdi
callq 0x35a90
jmp 0x38594
movsbl 0x3(%r12,%rbp), %esi
movq %rbx, %rdi
callq 0x35520
addq $0x4, %rbp
jmp 0x385d0
movl 0x3(%r12,%rbp), %esi
movq %rbx, %rdi
callq 0x35b00
jmp 0x385b8
movss 0x3(%r12,%rbp), %xmm0
movq %rbx, %rdi
callq 0x35480
addq $0x7, %rbp
jmp 0x385d0
movzwl 0x3(%r12,%rbp), %esi
movq %rbx, %rdi
callq 0x35a90
addq $0x5, %rbp
movq %rbp, %r15
cmpb $0x0, (%r12,%r15)
movq %r15, %rbp
jne 0x383fa
leaq 0x3e22b(%rip), %rsi     # 0x76813
movq %rbx, %rdi
callq 0x355b0
leaq 0x3e21c(%rip), %rsi     # 0x76813
movq %rbx, %rdi
callq 0x355b0
movq %rax, %rdi
callq 0x35300
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq 0x8(%rsp), %rdi
callq 0x35730
movq %rbx, %rdi
callq 0x35b50
movq %rax, %rdi
callq 0x3a198
nop