// File: /workspace/github_cpp_makefile_star2_O1/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 0x4e6ab
addq %r14, %rax
movq %rax, 0x18(%rbx)
movq 0x8(%rbx), %rbp
addq %r14, %rbp
leaq 0x28(%rbx), %r12
movq 0x10(%rbx), %r13
movq %r12, %rdi
movq %r13, %rsi
callq 0x262b0
leaq 0x38(%rbx), %r15
movq %r15, %rdi
movq %r13, %rsi
callq 0x27250
movq %rax, %rdi
callq 0x27270
cmpq %rax, %rbp
jbe 0x4e68e
movq 0x8(%rbx), %rbp
movq 0x10(%rbx), %r13
movq %r12, %rdi
movq %r13, %rsi
callq 0x262b0
movq %r15, %rdi
movq %r13, %rsi
callq 0x27250
movq %rax, %rdi
callq 0x27270
movq %rax, %r13
shrq %r13
movq 0x10(%rbx), %rsi
movq %r15, %rdi
callq 0x27250
movq 0x8(%rbx), %rsi
cmpq %r13, %rbp
jae 0x4e708
addq %r14, %rsi
movq %rax, %rdi
callq 0x26d90
movq 0x10(%rbx), %rsi
movq %r15, %rdi
callq 0x27250
movq %rax, %rdi
callq 0x28250
addq %r14, 0x8(%rbx)
jmp 0x4e76c
leaq 0x38(%rbx), %r15
movq 0x38(%rbx), %rdi
movq %rdi, %rsi
subq 0x40(%rbx), %rsi
movq %rsi, %rax
sarq $0x3, %rax
movabsq $-0x5555555555555555, %rcx # imm = 0xAAAAAAAAAAAAAAAB
imulq %rax, %rcx
cmpq $0x15, %rcx
jb 0x4e7ac
movq 0x50(%rbx), %r8
testq %rsi, %rsi
jle 0x4e77b
movabsq $-0x79e79e79e79e79e7, %rdx # imm = 0x8618618618618619
movq %rcx, %rax
mulq %rdx
subq %rdx, %rcx
shrq %rcx
addq %rdx, %rcx
shrq $0x4, %rcx
jmp 0x4e79e
movq %rax, %rdi
callq 0x26d90
movq %rsp, %r13
movq %r13, %rdi
callq 0x27de0
movq %r12, %rdi
movq %r13, %rsi
callq 0x274f0
movq %rsp, %rdi
callq 0x27470
movq 0x10(%rbx), %rsi
incq %rsi
movq %rsi, 0x10(%rbx)
movq %r15, %rdi
callq 0x27250
movq 0x20(%rbx), %rsi
cmpq %r14, %rsi
cmovbeq %r14, %rsi
movq %rax, %rdi
callq 0x26d90
movq %r14, 0x8(%rbx)
movq 0x10(%rbx), %rsi
movq %r15, %rdi
callq 0x27250
movq %rax, %rdi
callq 0x28250
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
notq %rcx
movabsq $-0x79e79e79e79e79e7, %rdx # imm = 0x8618618618618619
movq %rcx, %rax
mulq %rdx
subq %rdx, %rcx
shrq %rcx
addq %rdx, %rcx
shrq $0x4, %rcx
notq %rcx
imulq $-0x1f8, %rcx, %rdi     # imm = 0xFE08
addq (%r8,%rcx,8), %rdi
addq %rsi, %rdi
movq 0x20(%rbx), %rsi
cmpq %r14, %rsi
cmovbeq %r14, %rsi
callq 0x26d90
addq %r14, 0x18(%rbx)
movq %r14, 0x8(%rbx)
movq 0x10(%rbx), %rsi
movq %r15, %rdi
callq 0x27250
movq %rax, %rdi
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x28250
movq %rax, %rbx
movq %rsp, %rdi
callq 0x27470
movq %rbx, %rdi
callq 0x28c20
movq %rax, %rdi
callq 0x2f0bd
nop