// File: /workspace/github_cpp_makefile_star2_O2/reddragon[P]packed-memory-array/impl2.cpp
// Function: PMA::insert(int)
void
    insert(int v) {
        /*
        if ((this->nelems + 2) * 2 > this->impl.size()) {
            // resize array
            this->resize(2 * this->impl.size());
        }
        */

        int i = lower_bound(v);
        if (i == this->impl.size()) {
            --i;
        }
        assert(i > -1);
        assert(i < this->impl.size());

        // Check in a window of size 'w'
        int w = chunk_size;
        int level = 0;
        int l = this->left_interval_boundary(i, w);

        // Number of elements in current window. We just need sz to be
        // less than w -- we don't need the exact value of 'sz' here.
        int sz = w - 1;

        bool in_limit = false;

        // If the current chunk has space, then the last element will
        // be unused (with significant probability). First check that
        // as a quick check.
        if (this->present[l + this->chunk_size - 1]) {
            get_interval_stats(l, level, in_limit, sz);
        }

        if (sz < w) {
            // There is some space in this interval. We can just
            // shuffle elements and insert.
            this->insert_merge(l, v);
        } else {
            // No space in this interval. Find an interval above this
            // interval that is within limits, re-balance, and
            // re-start insertion.
            in_limit = false;
            while (!in_limit) {
                w *= 2;
                level += 1;
                // assert(level <= this->nlevels);
                if (level > this->nlevels) {
                    // Root node is out of balance. Resize array.
                    this->resize(2 * this->impl.size());
                    this->insert(v);
                    return;
                }

                l = this->left_interval_boundary(i, w);
                get_interval_stats(l, level, in_limit, sz);
                dprintf("level: %d, this->nlevels: %d, in_limit: %d, sz: %d\n", level, this->nlevels, in_limit, sz);
            }
            this->rebalance_interval(l, level);
            this->insert(v);
        }

    }

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movl %esi, %ebx
movq %rdi, %r14
callq 0x1712
movslq %eax, %r15
movq 0x8(%r14), %rax
subq (%r14), %rax
sarq $0x2, %rax
xorl %ecx, %ecx
cmpq %r15, %rax
sete %cl
subl %ecx, %r15d
js 0x156f
movl %r15d, %ecx
cmpq %rcx, %rax
jbe 0x158e
movl 0x48(%r14), %r12d
movq %r14, %rdi
movl %r15d, %esi
movl %r12d, %edx
callq 0x17a0
movl %eax, %r13d
leal -0x1(%r12), %eax
movl %eax, 0x14(%rsp)
movb $0x0, 0xf(%rsp)
leaq 0x20(%r14), %rdi
movl 0x48(%r14), %eax
addl %r13d, %eax
decl %eax
movslq %eax, %rsi
callq 0x1816
testq %rdx, (%rax)
je 0x14dd
xorl %ebp, %ebp
leaq 0xf(%rsp), %rcx
movl %ebx, 0x10(%rsp)
leaq 0x14(%rsp), %rbx
movq %r14, %rdi
movl %r13d, %esi
xorl %edx, %edx
movq %rbx, %r8
callq 0x1834
cmpl %r12d, (%rbx)
movl 0x10(%rsp), %ebx
jge 0x14ec
movq %r14, %rdi
movl %r13d, %esi
movl %ebx, %edx
callq 0x18c6
jmp 0x1560
leaq 0xf(%rsp), %rbx
movb $0x0, (%rbx)
xorl %eax, %eax
testb $0x1, %al
jne 0x1530
cmpl 0x50(%r14), %ebp
jge 0x153f
incl %ebp
addl %r12d, %r12d
movq %r14, %rdi
movl %r15d, %esi
movl %r12d, %edx
callq 0x17a0
movl %eax, %r13d
movq %r14, %rdi
movl %eax, %esi
movl %ebp, %edx
movq %rbx, %rcx
leaq 0x14(%rsp), %r8
callq 0x1834
movb 0xf(%rsp), %al
jmp 0x14f6
movq %r14, %rdi
movl %r13d, %esi
movl %ebp, %edx
callq 0x1b8c
jmp 0x1554
movq 0x8(%r14), %rsi
subq (%r14), %rsi
shrq %rsi
andl $-0x2, %esi
movq %r14, %rdi
callq 0x19d0
movq %r14, %rdi
movl 0x10(%rsp), %esi
callq 0x1434
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1aec(%rip), %rdi      # 0x3062
leaq 0x1aec(%rip), %rsi      # 0x3069
leaq 0x1aef(%rip), %rcx      # 0x3073
movl $0x128, %edx            # imm = 0x128
callq 0x1090
leaq 0x1af4(%rip), %rdi      # 0x3089
leaq 0x1acd(%rip), %rsi      # 0x3069
leaq 0x1ad0(%rip), %rcx      # 0x3073
movl $0x129, %edx            # imm = 0x129
callq 0x1090
nop