// File: /workspace/github_cpp_makefile_star2_O1/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 0x46aeb(%rip), %rsi     # 0x722fa
movl $0x9, %edx
movq %rbx, %rdi
callq 0x276c0
movq (%r13), %rsi
movq 0x8(%r13), %rdx
movq %rbx, %rdi
callq 0x276c0
movq %rax, %r12
leaq 0x46ace(%rip), %rsi     # 0x72304
movl $0x13, %edx
movq %rax, %rdi
callq 0x276c0
movl 0xb4(%r13), %esi
movq %r12, %rdi
callq 0x27400
leaq 0x46abf(%rip), %rsi     # 0x72318
movl $0x2, %edx
movq %rax, %rdi
callq 0x276c0
movl 0xa8(%r13), %eax
testl %eax, %eax
js 0x2b8cf
movq 0x10(%r14), %rcx
subq 0x8(%r14), %rcx
shrq $0x3, %rcx
imull $0xcccccccd, %ecx, %ecx # imm = 0xCCCCCCCD
cmpl %ecx, %eax
jge 0x2b8cf
leaq 0x46a8d(%rip), %rsi     # 0x7231b
movl $0xd, %edx
movq %rbx, %rdi
callq 0x276c0
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 0x276c0
leaq 0x46a56(%rip), %rsi     # 0x72318
movl $0x2, %edx
movq %rax, %rdi
callq 0x276c0
leaq 0x46a53(%rip), %rsi     # 0x72329
movl $0xb, %edx
movq %rbx, %rdi
callq 0x276c0
movl 0xac(%r13), %esi
incl %esi
movq %rbx, %rdi
callq 0x27ac0
movq %rax, %r12
leaq 0x46a37(%rip), %rsi     # 0x72335
movl $0xe, %edx
movq %rax, %rdi
callq 0x276c0
movzwl 0xb2(%r13), %esi
movq %r12, %rdi
callq 0x27400
leaq 0x469f7(%rip), %rsi     # 0x72319
movl $0x1, %edx
movq %rax, %rdi
callq 0x276c0
movq 0xb8(%r13), %rax
cmpq 0xc0(%r13), %rax
movq %r13, 0x8(%rsp)
je 0x2ba07
movq %r14, (%rsp)
leaq 0x469f1(%rip), %rsi     # 0x72344
movl $0x9, %edx
movq %rbx, %rdi
callq 0x276c0
movq 0xb8(%r13), %r14
movq 0xc0(%r13), %r15
cmpq %r15, %r14
je 0x2b9ea
xorl %ebp, %ebp
leaq 0x469ab(%rip), %r12     # 0x72327
leaq 0x10(%rsp), %r13
testq %rbp, %rbp
je 0x2b99a
movl $0x1, %edx
movq %rbx, %rdi
leaq 0x46984(%rip), %rsi     # 0x72319
callq 0x276c0
movl $0x1, %edx
movq %rbx, %rdi
movq %r12, %rsi
callq 0x276c0
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 0x276c0
movl $0x1, %edx
movq %rax, %rdi
movq %r12, %rsi
callq 0x276c0
addq $0x8, %r14
addq $-0x8, %rbp
cmpq %r15, %r14
jne 0x2b981
leaq 0x4695d(%rip), %rsi     # 0x7234e
movl $0x2, %edx
movq %rbx, %rdi
callq 0x276c0
movq 0x8(%rsp), %r13
movq (%rsp), %r14
movq %r13, %rdi
callq 0x27330
testb %al, %al
je 0x2bafe
movl 0xd0(%r13), %eax
testl %eax, %eax
js 0x2bafe
movq 0x10(%r14), %rcx
subq 0x8(%r14), %rcx
shrq $0x3, %rcx
imull $0xcccccccd, %ecx, %ecx # imm = 0xCCCCCCCD
cmpl %ecx, %eax
jge 0x2bafe
leaq 0x4690a(%rip), %rsi     # 0x72351
movl $0x8, %edx
movq %rbx, %rdi
callq 0x276c0
leaq 0x468c0(%rip), %rsi     # 0x7231b
movl $0xd, %edx
movq %rbx, %rdi
callq 0x276c0
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 0x276c0
movq %rax, %r15
leaq 0x46886(%rip), %rsi     # 0x72318
movl $0x2, %edx
movq %rax, %rdi
callq 0x276c0
leaq 0x46883(%rip), %rsi     # 0x72329
movl $0xb, %edx
movq %r15, %rdi
callq 0x276c0
movl 0xd4(%r13), %esi
incl %esi
movq %r15, %rdi
callq 0x27ac0
movq %rax, %r15
leaq 0x4688c(%rip), %rsi     # 0x7235a
movl $0xe, %edx
movq %rax, %rdi
callq 0x276c0
movl 0xd8(%r13), %esi
movq %r15, %rdi
callq 0x27ac0
leaq 0x46878(%rip), %rsi     # 0x72369
movl $0x2, %edx
movq %rax, %rdi
callq 0x276c0
cmpq $0x0, 0x30(%r13)
je 0x2bb3d
leaq 0x46860(%rip), %rsi     # 0x7236c
movl $0xe, %edx
movq %rbx, %rdi
callq 0x276c0
movq 0x28(%r13), %rsi
movq 0x30(%r13), %rdx
movq %rbx, %rdi
callq 0x276c0
leaq 0x467e8(%rip), %rsi     # 0x72318
movl $0x2, %edx
movq %rax, %rdi
callq 0x276c0
cmpq $0x0, 0x70(%r13)
je 0x2bbcf
movq 0x68(%r13), %r14
cmpb $-0x1, (%r14)
je 0x2bbcf
leaq 0x46822(%rip), %rsi     # 0x7237b
movl $0xd, %edx
movq %rbx, %rdi
callq 0x276c0
movsbl (%r14), %esi
addl $-0x21, %esi
movq %rbx, %rdi
callq 0x27ac0
incq %r14
movq 0x68(%r13), %rax
addq 0x70(%r13), %rax
cmpq %rax, %r14
je 0x2bbbb
leaq 0x4678d(%rip), %r15     # 0x72319
movl $0x1, %edx
movq %rbx, %rdi
movq %r15, %rsi
callq 0x276c0
movsbl (%r14), %esi
addl $-0x21, %esi
movq %rbx, %rdi
callq 0x27ac0
incq %r14
movq 0x68(%r13), %rax
addq 0x70(%r13), %rax
cmpq %rax, %r14
jne 0x2bb8c
leaq 0x4678c(%rip), %rsi     # 0x7234e
movl $0x2, %edx
movq %rbx, %rdi
callq 0x276c0
leaq 0x467b3(%rip), %rsi     # 0x72389
movl $0xc, %edx
movq %rbx, %rdi
callq 0x276c0
movq 0xe0(%r13), %rsi
movq 0xe8(%r13), %rdx
movq %rbx, %rdi
callq 0x276c0
leaq 0x46718(%rip), %rsi     # 0x72318
movl $0x2, %edx
movq %rax, %rdi
callq 0x276c0
movq 0x90(%r13), %rax
movq %rax, (%rsp)
testq %rax, %rax
je 0x2beb0
leaq 0x88(%r13), %r12
movq 0x88(%r13), %r14
leaq 0x46760(%rip), %rsi     # 0x72396
movl $0x8, %edx
movq %rbx, %rdi
callq 0x276c0
xorl %r15d, %r15d
leaq 0x10(%rsp), %r13
leaq 0x4674d(%rip), %rbp     # 0x7239f
movq %r12, 0x30(%rsp)
cmpq (%rsp), %r15
jae 0x2be9c
testq %r15, %r15
je 0x2bc7a
movl $0x1, %edx
movq %rbx, %rdi
leaq 0x466a4(%rip), %rsi     # 0x72319
callq 0x276c0
movl $0x1, %edx
movq %rbx, %rdi
leaq 0x4669e(%rip), %rsi     # 0x72327
callq 0x276c0
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 0x276c0
movl $0x2, %edx
movq %rax, %rdi
leaq 0x466a4(%rip), %rsi     # 0x72366
callq 0x276c0
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
je 0x2bcdb
callq 0x27560
leaq 0x2(%r15), %rsi
movq 0x8(%rsp), %rax
movq 0x90(%rax), %rdx
cmpq %rsi, %rdx
jbe 0x2befa
movq (%r12), %rax
movzbl 0x2(%rax,%r15), %eax
leaq 0x3(%r15), %r12
cmpl $0x59, %eax
jg 0x2bd68
leal -0x41(%rax), %ecx
cmpl $0x8, %ecx
ja 0x2be22
leaq 0x462ea(%rip), %rdx     # 0x72004
movslq (%rdx,%rcx,4), %rax
addq %rdx, %rax
jmpq *%rax
movl $0x1, %edx
movq %rbx, %rdi
leaq 0x465f5(%rip), %r12     # 0x72327
movq %r12, %rsi
callq 0x276c0
movb 0x3(%r14,%r15), %al
movb %al, 0x10(%rsp)
movl $0x1, %edx
movq %rbx, %rdi
movq %r13, %rsi
callq 0x276c0
movl $0x1, %edx
movq %rax, %rdi
movq %r12, %rsi
callq 0x276c0
jmp 0x2be4c
cmpl $0x65, %eax
jle 0x2bd9b
cmpl $0x66, %eax
je 0x2be61
cmpl $0x69, %eax
je 0x2be75
cmpl $0x73, %eax
jne 0x2be89
movswl 0x3(%r14,%r15), %esi
movq %rbx, %rdi
callq 0x27510
jmp 0x2be35
cmpl $0x5a, %eax
je 0x2bdbc
cmpl $0x63, %eax
jne 0x2be89
movsbl 0x3(%r14,%r15), %esi
movq %rbx, %rdi
callq 0x27510
jmp 0x2be4c
movl $0x1, %edx
movq %rbx, %rdi
leaq 0x4655c(%rip), %rsi     # 0x72327
callq 0x276c0
addq $0x4, %r15
movq %r15, %r12
movzbl -0x1(%r14,%r12), %eax
cmpl $0x22, %eax
je 0x2bdf7
testl %eax, %eax
je 0x2be0c
movb %al, 0x10(%rsp)
movl $0x1, %edx
movq %rbx, %rdi
movq %r13, %rsi
jmp 0x2be02
movl $0x2, %edx
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x276c0
incq %r12
jmp 0x2bdd7
movl $0x1, %edx
movq %rbx, %rdi
leaq 0x4650c(%rip), %rsi     # 0x72327
callq 0x276c0
jmp 0x2be89
cmpl $0x53, %eax
jne 0x2be89
movzwl 0x3(%r14,%r15), %esi
movq %rbx, %rdi
callq 0x27400
addq $0x5, %r15
jmp 0x2be86
movsbq 0x3(%r14,%r15), %rax
movzwl %ax, %esi
movq %rbx, %rdi
callq 0x27400
addq $0x4, %r15
jmp 0x2be86
movl 0x3(%r14,%r15), %esi
movq %rbx, %rdi
callq 0x27400
jmp 0x2be82
xorps %xmm0, %xmm0
cvtss2sd 0x3(%r14,%r15), %xmm0
movq %rbx, %rdi
callq 0x279e0
jmp 0x2be82
movl 0x3(%r14,%r15), %esi
movq %rbx, %rdi
callq 0x27ac0
addq $0x7, %r15
movq %r15, %r12
cmpb $0x0, (%r14,%r12)
movq %r12, %r15
movq 0x30(%rsp), %r12
jne 0x2bc57
leaq 0x4b994(%rip), %rsi     # 0x77837
movl $0x1, %edx
movq %rbx, %rdi
callq 0x276c0
leaq 0x4b980(%rip), %rsi     # 0x77837
movl $0x1, %edx
movq %rbx, %rdi
callq 0x276c0
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 0x46807(%rip), %rdi     # 0x72708
xorl %eax, %eax
callq 0x278c0
movq %rax, %rbx
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
je 0x2bf1f
callq 0x27560
movq %rbx, %rdi
callq 0x27b20
nop