// File: zeeev[P]DASVC/bamtools/src/api/internal/index/BamStandardIndex_p.cpp
// Function: BamTools::Internal::BamStandardIndex::WriteAlignmentChunks(std::vector<BamTools::Internal::BaiAlignmentChunk, std::allocator<BamTools::Internal::BaiAlignmentChunk>>&)
void BamStandardIndex::WriteAlignmentChunks(BaiAlignmentChunkVector& chunks) {

    // make sure chunks are merged (simplified) before writing & saving summary
    MergeAlignmentChunks(chunks);

    // write chunks
    int32_t chunkCount = chunks.size();
    if ( m_isBigEndian ) SwapEndian_32(chunkCount);
    const int64_t numBytesWritten = m_resources.Device->Write((const char*)&chunkCount, sizeof(chunkCount));
    if ( numBytesWritten != sizeof(chunkCount) )
        throw BamException("BamStandardIndex::WriteAlignmentChunks", "could not write BAI chunk count");

    // iterate over chunks
    BaiAlignmentChunkVector::const_iterator chunkIter = chunks.begin();
    BaiAlignmentChunkVector::const_iterator chunkEnd  = chunks.end();
    for ( ; chunkIter != chunkEnd; ++chunkIter )
        WriteAlignmentChunk( (*chunkIter) );
}

// Assembly:
subq $0xc8, %rsp
movq %rdi, 0xc0(%rsp)
movq %rsi, 0xb8(%rsp)
movq 0xc0(%rsp), %rdi
movq %rdi, 0x20(%rsp)
movq 0xb8(%rsp), %rsi
callq 0x7c440
movq 0xb8(%rsp), %rdi
callq 0x7a280
movq %rax, %rcx
movq 0x20(%rsp), %rax
movl %ecx, 0xb4(%rsp)
testb $0x1, 0x30(%rax)
je 0xb5970
leaq 0xb4(%rsp), %rdi
callq 0x75b80
movq 0x20(%rsp), %rax
movq 0x58(%rax), %rdi
movq (%rdi), %rax
leaq 0xb4(%rsp), %rsi
movl $0x4, %edx
callq *0x40(%rax)
movq %rax, 0xa8(%rsp)
cmpq $0x4, 0xa8(%rsp)
je 0xb5ac2
movb $0x1, 0x4e(%rsp)
movl $0x28, %edi
callq 0x737c0
movq %rax, 0x10(%rsp)
leaq 0x87(%rsp), %rdi
movq %rdi, 0x18(%rsp)
callq 0x7b5f0
movq 0x18(%rsp), %rdx
leaq 0x35b7e(%rip), %rsi     # 0xeb553
leaq 0x88(%rsp), %rdi
callq 0x78e70
jmp 0xb59e4
leaq 0x4f(%rsp), %rdi
movq %rdi, 0x8(%rsp)
callq 0x7b5f0
movq 0x8(%rsp), %rdx
leaq 0x35b7b(%rip), %rsi     # 0xeb57a
leaq 0x50(%rsp), %rdi
callq 0x78e70
jmp 0xb5a0b
movq 0x10(%rsp), %rdi
leaq 0x88(%rsp), %rsi
leaq 0x50(%rsp), %rdx
callq 0x79cd0
jmp 0xb5a24
movq 0x10(%rsp), %rdi
movb $0x0, 0x4e(%rsp)
movq 0x5c4db(%rip), %rsi     # 0x111f10
movq 0x5c524(%rip), %rdx     # 0x111f60
callq 0x7b010
jmp 0xb5b56
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x78(%rsp)
movl %eax, 0x74(%rsp)
jmp 0xb5a9d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x78(%rsp)
movl %eax, 0x74(%rsp)
jmp 0xb5a82
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x78(%rsp)
movl %eax, 0x74(%rsp)
leaq 0x50(%rsp), %rdi
callq 0x7cce8
jmp 0xb5a80
jmp 0xb5a82
leaq 0x4f(%rsp), %rdi
callq 0x76f80
leaq 0x88(%rsp), %rdi
callq 0x7cce8
jmp 0xb5a9b
jmp 0xb5a9d
leaq 0x87(%rsp), %rdi
callq 0x76f80
testb $0x1, 0x4e(%rsp)
jne 0xb5ab3
jmp 0xb5abd
movq 0x10(%rsp), %rdi
callq 0x74e00
jmp 0xb5b44
movq 0xb8(%rsp), %rdi
callq 0x77940
movq %rax, 0x38(%rsp)
leaq 0x40(%rsp), %rdi
leaq 0x38(%rsp), %rsi
callq 0x7ca50
movq 0xb8(%rsp), %rdi
callq 0x75050
movq %rax, 0x28(%rsp)
leaq 0x30(%rsp), %rdi
leaq 0x28(%rsp), %rsi
callq 0x7ca50
leaq 0x40(%rsp), %rdi
leaq 0x30(%rsp), %rsi
callq 0x787a0
testb $0x1, %al
jne 0xb5b19
jmp 0xb5b3c
leaq 0x40(%rsp), %rdi
callq 0x729f0
movq 0x20(%rsp), %rdi
movq %rax, %rsi
callq 0x7c560
leaq 0x40(%rsp), %rdi
callq 0x75c80
jmp 0xb5b04
addq $0xc8, %rsp
retq
movq 0x78(%rsp), %rdi
callq 0x7b550
movq %rax, %rdi
callq 0x88ef0
nopw %cs:(%rax,%rax)