// File: zeeev[P]DASVC/bamtools/src/api/internal/io/RollingBuffer_p.cpp
// Function: BamTools::Internal::RollingBuffer::Reserve(unsigned long)
char* RollingBuffer::Reserve(size_t n) {

    // if empty buffer
    if ( m_totalBufferSize == 0 ) {
        m_data[0].Resize( std::max(m_bufferGrowth, n) );
        m_totalBufferSize += n;
        m_tail = n;
        return m_data[m_tailBufferIndex].Data();
    }

    // increment buffer's byte count
    m_totalBufferSize += n;

    // if buffer already contains enough space to fit @n more bytes
    if ( (m_tail + n) <= m_data.at(m_tailBufferIndex).Size() ) {

        // fetch write pointer at current 'tail', increment tail by @n & return
        char* ptr = m_data[m_tailBufferIndex].Data(); //+ m_tail;
        m_tail += n;
        return ptr;
    }

    // if last byte array isn't half full
    if ( m_tail < m_data.at(m_tailBufferIndex).Size()/2 ) {

        // we'll allow simple resize
        m_data[m_tailBufferIndex].Resize(m_tail + n);

        // fetch write pointer at current 'tail', increment tail by @n & return
        char* ptr = m_data[m_tailBufferIndex].Data(); //+ m_tail;
        m_tail += n;
        return ptr;
    }

    // otherwise, shrink last byte array to current used size
    m_data[m_tailBufferIndex].Resize(m_tail);

    // then append new byte array
    m_data.push_back( ByteArray() );
    ++m_tailBufferIndex;
    m_data[m_tailBufferIndex].Resize( std::max(m_bufferGrowth, n) );
    m_tail = n;

    // return write-able pointer on new array
    return m_data[m_tailBufferIndex].Data();
}

// Assembly:
subq $0x88, %rsp
movq %rdi, 0x78(%rsp)
movq %rsi, 0x70(%rsp)
movq 0x78(%rsp), %rax
movq %rax, 0x30(%rsp)
cmpq $0x0, 0x18(%rax)
jne 0xdc599
movq 0x30(%rsp), %rdi
addq $0x28, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x79510
movq 0x30(%rsp), %rdi
movq %rax, 0x28(%rsp)
addq $0x20, %rdi
leaq 0x70(%rsp), %rsi
callq 0x78ca0
movq 0x28(%rsp), %rdi
movq (%rax), %rsi
callq 0x744c0
movq 0x30(%rsp), %rax
movq 0x70(%rsp), %rcx
addq 0x18(%rax), %rcx
movq %rcx, 0x18(%rax)
movq 0x70(%rsp), %rcx
movq %rcx, 0x8(%rax)
movq %rax, %rdi
addq $0x28, %rdi
movq 0x10(%rax), %rsi
callq 0x79510
movq %rax, %rdi
callq 0x795d0
movq %rax, 0x80(%rsp)
jmp 0xdc7b6
movq 0x30(%rsp), %rax
movq 0x70(%rsp), %rcx
addq 0x18(%rax), %rcx
movq %rcx, 0x18(%rax)
movq 0x8(%rax), %rcx
addq 0x70(%rsp), %rcx
movq %rcx, 0x20(%rsp)
movq %rax, %rdi
addq $0x28, %rdi
movq 0x10(%rax), %rsi
callq 0x74d90
movq %rax, %rdi
callq 0x75810
movq %rax, %rcx
movq 0x20(%rsp), %rax
cmpq %rcx, %rax
ja 0xdc627
movq 0x30(%rsp), %rax
movq %rax, %rdi
addq $0x28, %rdi
movq 0x10(%rax), %rsi
callq 0x79510
movq %rax, %rdi
callq 0x795d0
movq %rax, %rcx
movq 0x30(%rsp), %rax
movq %rcx, 0x68(%rsp)
movq 0x70(%rsp), %rcx
addq 0x8(%rax), %rcx
movq %rcx, 0x8(%rax)
movq 0x68(%rsp), %rax
movq %rax, 0x80(%rsp)
jmp 0xdc7b6
movq 0x30(%rsp), %rax
movq 0x8(%rax), %rcx
movq %rcx, 0x18(%rsp)
movq %rax, %rdi
addq $0x28, %rdi
movq 0x10(%rax), %rsi
callq 0x74d90
movq %rax, %rdi
callq 0x75810
movq %rax, %rcx
movq 0x18(%rsp), %rax
shrq %rcx
cmpq %rcx, %rax
jae 0xdc6d1
movq 0x30(%rsp), %rax
movq %rax, %rdi
addq $0x28, %rdi
movq 0x10(%rax), %rsi
callq 0x79510
movq %rax, %rdi
movq 0x30(%rsp), %rax
movq 0x8(%rax), %rsi
addq 0x70(%rsp), %rsi
callq 0x744c0
movq 0x30(%rsp), %rax
movq %rax, %rdi
addq $0x28, %rdi
movq 0x10(%rax), %rsi
callq 0x79510
movq %rax, %rdi
callq 0x795d0
movq %rax, %rcx
movq 0x30(%rsp), %rax
movq %rcx, 0x60(%rsp)
movq 0x70(%rsp), %rcx
addq 0x8(%rax), %rcx
movq %rcx, 0x8(%rax)
movq 0x60(%rsp), %rax
movq %rax, 0x80(%rsp)
jmp 0xdc7b6
movq 0x30(%rsp), %rax
movq %rax, %rdi
addq $0x28, %rdi
movq %rdi, 0x8(%rsp)
movq 0x10(%rax), %rsi
callq 0x79510
movq %rax, %rdi
movq 0x30(%rsp), %rax
movq 0x8(%rax), %rsi
callq 0x744c0
leaq 0x48(%rsp), %rdi
movq %rdi, 0x10(%rsp)
callq 0x78440
movq 0x8(%rsp), %rdi
movq 0x10(%rsp), %rsi
callq 0x76360
jmp 0xdc71c
leaq 0x48(%rsp), %rdi
callq 0x76060
movq 0x30(%rsp), %rax
movq 0x10(%rax), %rcx
addq $0x1, %rcx
movq %rcx, 0x10(%rax)
movq %rax, %rdi
addq $0x28, %rdi
movq 0x10(%rax), %rsi
callq 0x79510
movq 0x30(%rsp), %rdi
movq %rax, (%rsp)
addq $0x20, %rdi
leaq 0x70(%rsp), %rsi
callq 0x78ca0
movq (%rsp), %rdi
movq (%rax), %rsi
callq 0x744c0
movq 0x30(%rsp), %rax
movq 0x70(%rsp), %rcx
movq %rcx, 0x8(%rax)
movq %rax, %rdi
addq $0x28, %rdi
movq 0x10(%rax), %rsi
callq 0x79510
movq %rax, %rdi
callq 0x795d0
movq %rax, 0x80(%rsp)
jmp 0xdc7b6
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x40(%rsp)
movl %eax, 0x3c(%rsp)
leaq 0x48(%rsp), %rdi
callq 0x76060
jmp 0xdc7b4
jmp 0xdc7c6
movq 0x80(%rsp), %rax
addq $0x88, %rsp
retq
movq 0x40(%rsp), %rdi
callq 0x7b550
movq %rax, %rdi
callq 0x88ef0
nopl (%rax,%rax)