// File: /workspace/github_c_makefile_star2_O1/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 0x85d2
movl %ecx, %r9d
movl %edx, %r15d
movl %esi, %ebx
movq %rdi, %r14
movl $0x1, %eax
cmpl $0x1, %esi
jne 0x85e8
xorl %ebp, %ebp
testl %r15d, %r15d
je 0x85d7
movl (%r13), %ecx
movl 0x10(%r13), %r12d
leal (%rcx,%rax), %edi
movl %edi, %r8d
subl %ebx, %r8d
addl %r15d, %ecx
movl (%r14), %edx
movl %edx, %esi
xorl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
cmpl %r12d, %ecx
jle 0x861a
movl %edi, 0x14(%rsp)
movl $0x1, %ebp
cmpl %r15d, %esi
jl 0x85d7
cmpl %r15d, %r12d
movl %r15d, %esi
cmovgl %r12d, %esi
movl 0x10(%r14), %edx
movsd 0x18(%r14), %xmm0
movsbl %r9b, %ecx
leaq 0x18(%rsp), %rdi
movl %r9d, 0xc(%rsp)
movl %r8d, 0x10(%rsp)
callq 0x83e1
movl 0x10(%rsp), %r8d
movl %eax, %ebp
testl %eax, %eax
jne 0x85d7
cmpl %r15d, %r12d
jle 0x8579
leal 0x1(%r15), %esi
leaq 0x18(%rsp), %rdi
movl (%rdi), %edx
callq 0x868b
movl 0x10(%rsp), %r8d
movq 0x8(%r13), %rax
movq 0x20(%rsp), %rcx
movq %rax, 0x8(%rcx)
movq %rcx, 0x8(%r13)
addl %r15d, (%r14)
leal (%r15,%rbx), %ebp
movq %r14, %rdi
movl %ebx, %esi
movq %r14, %rdx
movl %ebp, %ecx
callq 0x8821
cmpb $0x0, 0xc(%rsp)
je 0x85b7
decl %ebp
movq %r14, %rdi
movl %ebx, %esi
movl %ebp, %edx
xorl %ecx, %ecx
callq 0x8e41
movq %r14, %rdi
movl 0x14(%rsp), %ecx
movl %ecx, %esi
movq %r14, %rdx
movl %r15d, %r8d
callq 0x8821
movl 0xc(%rsp), %r9d
jmp 0x864b
movl $0x3, %ebp
movl %ebp, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x2, %ebp
jle 0x85d7
leal -0x1(%rbx), %ecx
cmpl %ecx, (%r14)
jl 0x85d7
movl (%r13), %edx
cmpl %ecx, %edx
jge 0x84e3
movl %edx, %eax
movq 0x8(%r13), %r13
movl (%r13), %edx
addl %eax, %edx
cmpl %ecx, %edx
jl 0x8603
incl %eax
jmp 0x84e3
movl $0x1, %ebp
cmpl %r15d, %esi
jl 0x85d7
movl %ecx, (%r13)
addl %r15d, %edx
movl %edx, (%r14)
addl %ecx, %eax
cmpl %ebx, %eax
jle 0x864b
leal (%r15,%rbx), %ecx
movq %r14, %rdi
movl %ebx, %esi
movq %r14, %rdx
movl %r9d, %ebp
callq 0x8821
movl %ebp, %r9d
movl $0x0, %ebp
testb %r9b, %r9b
je 0x85d7
leal (%r15,%rbx), %edx
decl %edx
xorl %ebp, %ebp
movq %r14, %rdi
movl %ebx, %esi
xorl %ecx, %ecx
callq 0x8e41
jmp 0x85d7