// File: /workspace/github_cpp_makefile_star2_O2/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rsi, %r14
movq %rdi, %rbx
movq 0x18(%rdi), %rax
testq %rax, %rax
je 0x55145
addq %r14, %rax
movq %rax, 0x18(%rbx)
movq 0x8(%rbx), %r15
addq %r14, %r15
leaq 0x28(%rbx), %r12
movq 0x10(%rbx), %rsi
movq %r12, %rdi
callq 0x32790
movq %rax, %rdi
callq 0x32b70
cmpq %rax, %r15
jbe 0x55190
movq 0x8(%rbx), %rbp
movq 0x10(%rbx), %rsi
movq %r12, %rdi
callq 0x32790
movq %rax, %rdi
callq 0x32b70
movq %rax, %r13
shrq %r13
movq 0x10(%rbx), %rsi
leaq 0x38(%rbx), %r15
movq %r15, %rdi
callq 0x32b20
movq 0x8(%rbx), %rsi
cmpq %r13, %rbp
jae 0x551ab
addq %r14, %rsi
movq %rax, %rdi
callq 0x323d0
movq 0x10(%rbx), %rsi
movq %r15, %rdi
jmp 0x55198
leaq 0x38(%rbx), %r15
movq %r15, %rdi
xorl %esi, %esi
callq 0x32b20
movq 0x20(%rbx), %rsi
cmpq %r14, %rsi
cmovbeq %r14, %rsi
movq %rax, %rdi
callq 0x323d0
addq %r14, 0x18(%rbx)
movq %r14, 0x8(%rbx)
movq 0x10(%rbx), %rsi
movq %r15, %rdi
callq 0x32b20
movq %rax, %rdi
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x34220
movq 0x10(%rbx), %rsi
leaq 0x38(%rbx), %rdi
callq 0x32b20
movq %rax, %rdi
callq 0x34220
addq %r14, 0x8(%rbx)
jmp 0x5520f
movq %rax, %rdi
callq 0x323d0
movq %rsp, %r13
movq %r13, %rdi
callq 0x33ba0
movq %r12, %rdi
movq %r13, %rsi
callq 0x32f10
movq %rsp, %rdi
callq 0x32e40
movq 0x10(%rbx), %rsi
incq %rsi
movq %rsi, 0x10(%rbx)
movq %r15, %rdi
callq 0x32b20
movq 0x20(%rbx), %rsi
cmpq %r14, %rsi
cmovbeq %r14, %rsi
movq %rax, %rdi
callq 0x323d0
movq %r14, 0x8(%rbx)
movq 0x10(%rbx), %rsi
movq %r15, %rdi
callq 0x32b20
movq %rax, %rdi
callq 0x34220
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movq %rsp, %rdi
callq 0x32e40
movq %rbx, %rdi
callq 0x34ed0
movq %rax, %rdi
callq 0x39b2f
nop