// File: /workspace/github_c_makefile_star2_O2/heltilda[P]cicada/lnklst.c
// Function: insertElements
ccInt insertElements(linkedlist *theList, ccInt insertionPoint, ccInt newElements, ccBool ifCleared)
{
    sublistHeader * baseSublist;
    linkedlist dummy;
    ccInt newLL_result;
    ccInt bottom, elementsOnTop, copyPoint, skipAhead, newSublistElements;
    
    if (theList->memory == NULL)  return init_err;
    
    if (insertionPoint == 1)  {
        baseSublist = theList->memory;
        bottom = 1;    }
    else  {
        if (!elementExists(theList, insertionPoint-1))  return out_of_range_err;
        baseSublist = findSublist(theList, insertionPoint-1, &bottom);    }
    
    if (newElements == 0)  return passed;
    elementsOnTop = baseSublist->numElements + bottom - insertionPoint;
    
    
        // If necessary, create another sublist.
        // This is a rather hokey way of doing it -- create a new linked list, and from that extract and insert the sublist.
    
    if (baseSublist->numElements + newElements > baseSublist->maxElements)  {
            
            // Try to balance it so that both sublists have spareRoom number of spare elements.
            // First we allocate more room in a new sublist, then shove everything up to make room for the new inserted elements
            // and then try to fill the lower sublist as full as we want it by copying the top of those higher up into the next sublist
            // and removing the extra elements from the lower one.
            // copyPoint is in 'local' (to the sublist) coordinates -- refers to where we start shoving stuff upwards.
            // skipAhead is how far we ought to push everything up so that the first sublist will be as full/empty as we want it.
            
            // In Cicada, we need to have copyPoint at the point of the new arrivals.  Since many LLs are 'pinned',
            // addElements() expects that the ones below stay put.
        
        copyPoint = baseSublist->numElements + 1;
        skipAhead = baseSublist->numElements - (copyPoint - 1);
        if (ccIntMax-theList->elementNum < newElements+skipAhead)  return out_of_memory_err;
        
        
            // Allocate enough room to hold the last max-filled sublist, plus extra spareRoom.
        
        newSublistElements = skipAhead+newElements;    // thus successive sublists get bigger
        if (baseSublist->maxElements > newSublistElements)  newSublistElements = baseSublist->maxElements;
        
        newLL_result = newLinkedList(&dummy, newSublistElements, theList->elementSize, theList->spareRoom, ifCleared);
        if (newLL_result != passed)  return newLL_result;
        
        if (newSublistElements > skipAhead+newElements)         // here we really can ignore return value -- never have 0 els
            deleteElements(&dummy, skipAhead+newElements+1, dummy.elementNum);
        
        
            // stitch the new list in
        
        dummy.memory->nextSublist = baseSublist->nextSublist;
        baseSublist->nextSublist = dummy.memory;
        theList->elementNum += newElements+skipAhead;         // fool the computer for now
        
        
            // First make room for new arrivals..
        
        copyElements(theList, insertionPoint, theList, insertionPoint+newElements, elementsOnTop);
        if (ifCleared) clearElements(theList, insertionPoint, insertionPoint+newElements-1);
        
        
            // Now push everything off the top of the lower list, and reset the number of variables it holds.
            // copyPoint hasn't changed from last CopyEls, since it is tied to a position in the LLs, not a particular element.
        
        copyElements(theList, copyPoint+bottom-1, theList, copyPoint+skipAhead+bottom-1, skipAhead+newElements);
        baseSublist->numElements -= skipAhead;
        theList->elementNum -= skipAhead;         }       // unfool the computer
    
    else  {         // if we don't need to allocate another list -- just increase numElements closer to maxElements
        if (ccIntMax-theList->elementNum < newElements)  return out_of_memory_err;
        baseSublist->numElements += newElements;
        theList->elementNum += newElements;
        if (bottom + baseSublist->numElements > insertionPoint)
            copyElements(theList, insertionPoint, theList, insertionPoint + newElements, elementsOnTop);   }
    
    
                // subtle point -- if insertionPoint+newElements-1 == 0, clearElements returns param_err, which we throw away.  Fine.
    
    if (ifCleared) clearElements(theList, insertionPoint, insertionPoint + newElements - 1);
    
    return passed;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq 0x8(%rdi), %r13
testq %r13, %r13
je 0x84c7
movl %ecx, %r8d
movl %edx, %r15d
movl %esi, %ebx
movq %rdi, %r14
cmpl $0x1, %esi
jne 0x84ce
movl $0x1, 0x10(%rsp)
jmp 0x84f9
pushq $0x3
jmp 0x85e1
pushq $0x2
popq %rax
jle 0x8625
leal -0x1(%rbx), %esi
cmpl %esi, (%r14)
jl 0x8625
leaq 0x10(%rsp), %rdx
movq %r14, %rdi
movl %r8d, %ebp
callq 0x83ba
movl %ebp, %r8d
movq %rax, %r13
testl %r15d, %r15d
je 0x8623
movl 0x10(%rsp), %eax
movl (%r13), %ecx
movl 0x10(%r13), %ebp
leal (%rax,%rcx), %edi
movl %edi, %r12d
subl %ebx, %r12d
addl %r15d, %ecx
movl (%r14), %edx
movl %edx, %esi
xorl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
cmpl %ebp, %ecx
jle 0x85da
cmpl %r15d, %esi
jl 0x85df
movl %edi, 0x14(%rsp)
cmpl %r15d, %ebp
movl %r15d, %esi
cmovgl %ebp, %esi
movl 0x10(%r14), %edx
movsd 0x18(%r14), %xmm0
movsbl %r8b, %ecx
leaq 0x18(%rsp), %rdi
movl %r8d, 0xc(%rsp)
callq 0x83d5
testl %eax, %eax
jne 0x8625
cmpl %r15d, %ebp
jle 0x857d
leal 0x1(%r15), %esi
leaq 0x18(%rsp), %rdi
movl (%rdi), %edx
callq 0x864f
movq 0x8(%r13), %rax
movq 0x20(%rsp), %rcx
movq %rax, 0x8(%rcx)
movq %rcx, 0x8(%r13)
addl %r15d, (%r14)
leal (%r15,%rbx), %r13d
movq %r14, %rdi
movl %ebx, %esi
movq %r14, %rdx
movl %r13d, %ecx
movl %r12d, %r8d
callq 0x87c7
cmpb $0x0, 0xc(%rsp)
je 0x85bf
decl %r13d
movq %r14, %rdi
movl %ebx, %esi
movl %r13d, %edx
callq 0x8b08
movq %r14, %rdi
movl 0x14(%rsp), %ecx
movl %ecx, %esi
movq %r14, %rdx
movl %r15d, %r8d
callq 0x87c7
movl 0xc(%rsp), %r8d
jmp 0x860e
cmpl %r15d, %esi
jge 0x85e4
pushq $0x1
popq %rax
jmp 0x8625
movl %ecx, (%r13)
addl %r15d, %edx
movl %edx, (%r14)
addl %ecx, %eax
cmpl %ebx, %eax
jle 0x860e
leal (%r15,%rbx), %ecx
movq %r14, %rdi
movl %ebx, %esi
movq %r14, %rdx
movl %r8d, %ebp
movl %r12d, %r8d
callq 0x87c7
movl %ebp, %r8d
testb %r8b, %r8b
je 0x8623
leal (%r15,%rbx), %edx
decl %edx
movq %r14, %rdi
movl %ebx, %esi
callq 0x8b08
xorl %eax, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq