// File: zeeev[P]DASVC/bamtools/src/api/internal/bam/BamWriter_p.cpp
// Function: BamTools::Internal::BamWriterPrivate::WriteCoreAlignment(BamTools::BamAlignment const&)
void BamWriterPrivate::WriteCoreAlignment(const BamAlignment& al) {

    // write the block size
    unsigned int blockSize = al.SupportData.BlockLength;
    if ( m_isBigEndian ) BamTools::SwapEndian_32(blockSize);
    m_stream.Write((char*)&blockSize, Constants::BAM_SIZEOF_INT);

    // re-calculate bin (in case BamAlignment's position has been previously modified)
    const uint32_t alignmentBin = CalculateMinimumBin(al.Position, al.GetEndPosition());

    // assign the BAM core data
    uint32_t buffer[Constants::BAM_CORE_BUFFER_SIZE];
    buffer[0] = al.RefID;
    buffer[1] = al.Position;
    buffer[2] = (alignmentBin << 16) | (al.MapQuality << 8) | al.SupportData.QueryNameLength;
    buffer[3] = (al.AlignmentFlag << 16) | al.SupportData.NumCigarOperations;
    buffer[4] = al.SupportData.QuerySequenceLength;
    buffer[5] = al.MateRefID;
    buffer[6] = al.MatePosition;
    buffer[7] = al.InsertSize;

    // swap BAM core endian-ness, if necessary
    if ( m_isBigEndian ) {
        for ( int i = 0; i < 8; ++i )
            BamTools::SwapEndian_32(buffer[i]);
    }

    // write the BAM core
    m_stream.Write((char*)&buffer, Constants::BAM_CORE_SIZE);

    // write the raw char data
    m_stream.Write((char*)al.SupportData.AllCharData.data(),
                   al.SupportData.BlockLength-Constants::BAM_CORE_SIZE);
}

// Assembly:
subq $0x58, %rsp
movq %rdi, 0x50(%rsp)
movq %rsi, 0x48(%rsp)
movq 0x50(%rsp), %rax
movq %rax, 0x10(%rsp)
movq 0x48(%rsp), %rcx
movl 0x120(%rcx), %ecx
movl %ecx, 0x44(%rsp)
testb $0x1, 0x40(%rax)
je 0xaef57
leaq 0x44(%rsp), %rdi
callq 0x77400
movq 0x10(%rsp), %rdi
leaq 0x44(%rsp), %rsi
movl $0x4, %edx
callq 0x7b740
movq 0x48(%rsp), %rax
movl 0xac(%rax), %eax
movl %eax, 0xc(%rsp)
movq 0x48(%rsp), %rdi
xorl %edx, %edx
movl %edx, %esi
callq 0x76240
movl 0xc(%rsp), %esi
movq 0x10(%rsp), %rdi
movl %eax, %edx
callq 0x73660
movl %eax, %ecx
movq 0x10(%rsp), %rax
movl %ecx, 0x40(%rsp)
movq 0x48(%rsp), %rcx
movl 0xa8(%rcx), %ecx
movl %ecx, 0x20(%rsp)
movq 0x48(%rsp), %rcx
movl 0xac(%rcx), %ecx
movl %ecx, 0x24(%rsp)
movl 0x40(%rsp), %ecx
shll $0x10, %ecx
movq 0x48(%rsp), %rdx
movzwl 0xb2(%rdx), %edx
shll $0x8, %edx
orl %edx, %ecx
movq 0x48(%rsp), %rdx
orl 0x128(%rdx), %ecx
movl %ecx, 0x28(%rsp)
movq 0x48(%rsp), %rcx
movl 0xb4(%rcx), %ecx
shll $0x10, %ecx
movq 0x48(%rsp), %rdx
orl 0x124(%rdx), %ecx
movl %ecx, 0x2c(%rsp)
movq 0x48(%rsp), %rcx
movl 0x12c(%rcx), %ecx
movl %ecx, 0x30(%rsp)
movq 0x48(%rsp), %rcx
movl 0xd0(%rcx), %ecx
movl %ecx, 0x34(%rsp)
movq 0x48(%rsp), %rcx
movl 0xd4(%rcx), %ecx
movl %ecx, 0x38(%rsp)
movq 0x48(%rsp), %rcx
movl 0xd8(%rcx), %ecx
movl %ecx, 0x3c(%rsp)
testb $0x1, 0x40(%rax)
je 0xaf07b
movl $0x0, 0x1c(%rsp)
cmpl $0x8, 0x1c(%rsp)
jge 0xaf079
movslq 0x1c(%rsp), %rax
leaq 0x20(%rsp), %rdi
shlq $0x2, %rax
addq %rax, %rdi
callq 0x77400
movl 0x1c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1c(%rsp)
jmp 0xaf04f
jmp 0xaf07b
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rsi
movl $0x20, %edx
callq 0x7b740
movq 0x48(%rsp), %rdi
addq $0x100, %rdi            # imm = 0x100
callq 0x785e0
movq 0x10(%rsp), %rdi
movq %rax, %rsi
movq 0x48(%rsp), %rax
movl 0x120(%rax), %eax
subl $0x20, %eax
movl %eax, %eax
movl %eax, %edx
callq 0x7b740
addq $0x58, %rsp
retq
nopw %cs:(%rax,%rax)