// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x60, %rsp
movb %cl, %al
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movl %edx, -0x18(%rbp)
movb %al, -0x19(%rbp)
movq -0x10(%rbp), %rax
cmpq $0x0, 0x8(%rax)
jne 0x85be
movl $0x3, -0x4(%rbp)
jmp 0x881a
cmpl $0x1, -0x14(%rbp)
jne 0x85d9
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x28(%rbp)
movl $0x1, -0x50(%rbp)
jmp 0x8610
movq -0x10(%rbp), %rdi
movl -0x14(%rbp), %esi
subl $0x1, %esi
callq 0x8830
cmpl $0x0, %eax
jne 0x85f9
movl $0x2, -0x4(%rbp)
jmp 0x881a
movq -0x10(%rbp), %rdi
movl -0x14(%rbp), %esi
subl $0x1, %esi
leaq -0x50(%rbp), %rdx
callq 0x83f0
movq %rax, -0x28(%rbp)
cmpl $0x0, -0x18(%rbp)
jne 0x8622
movl $0x0, -0x4(%rbp)
jmp 0x881a
movq -0x28(%rbp), %rax
movl (%rax), %eax
addl -0x50(%rbp), %eax
subl -0x14(%rbp), %eax
movl %eax, -0x54(%rbp)
movq -0x28(%rbp), %rax
movl (%rax), %eax
addl -0x18(%rbp), %eax
movq -0x28(%rbp), %rcx
cmpl 0x10(%rcx), %eax
jle 0x879f
movq -0x28(%rbp), %rax
movl (%rax), %eax
addl $0x1, %eax
movl %eax, -0x58(%rbp)
movq -0x28(%rbp), %rax
movl (%rax), %eax
movl -0x58(%rbp), %ecx
subl $0x1, %ecx
subl %ecx, %eax
movl %eax, -0x5c(%rbp)
movq -0x10(%rbp), %rcx
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
subl (%rcx), %eax
movl -0x18(%rbp), %ecx
addl -0x5c(%rbp), %ecx
cmpl %ecx, %eax
jge 0x8685
movl $0x1, -0x4(%rbp)
jmp 0x881a
movl -0x5c(%rbp), %eax
addl -0x18(%rbp), %eax
movl %eax, -0x60(%rbp)
movq -0x28(%rbp), %rax
movl 0x10(%rax), %eax
cmpl -0x60(%rbp), %eax
jle 0x86a4
movq -0x28(%rbp), %rax
movl 0x10(%rax), %eax
movl %eax, -0x60(%rbp)
movl -0x60(%rbp), %esi
movq -0x10(%rbp), %rax
movl 0x10(%rax), %edx
movq -0x10(%rbp), %rax
movsd 0x18(%rax), %xmm0
leaq -0x48(%rbp), %rdi
movsbl -0x19(%rbp), %ecx
callq 0x8460
movl %eax, -0x4c(%rbp)
cmpl $0x0, -0x4c(%rbp)
je 0x86d8
movl -0x4c(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x881a
movl -0x60(%rbp), %eax
movl -0x5c(%rbp), %ecx
addl -0x18(%rbp), %ecx
cmpl %ecx, %eax
jle 0x86fa
movl -0x5c(%rbp), %esi
addl -0x18(%rbp), %esi
addl $0x1, %esi
movl -0x48(%rbp), %edx
leaq -0x48(%rbp), %rdi
callq 0x8880
movq -0x28(%rbp), %rax
movq 0x8(%rax), %rcx
movq -0x40(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x40(%rbp), %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x8(%rax)
movl -0x18(%rbp), %ecx
addl -0x5c(%rbp), %ecx
movq -0x10(%rbp), %rax
addl (%rax), %ecx
movl %ecx, (%rax)
movq -0x10(%rbp), %rdi
movl -0x14(%rbp), %esi
movq -0x10(%rbp), %rdx
movl -0x14(%rbp), %ecx
addl -0x18(%rbp), %ecx
movl -0x54(%rbp), %r8d
callq 0x8a90
cmpb $0x0, -0x19(%rbp)
je 0x8759
movq -0x10(%rbp), %rdi
movl -0x14(%rbp), %esi
movl -0x14(%rbp), %edx
addl -0x18(%rbp), %edx
subl $0x1, %edx
callq 0x8e40
movq -0x10(%rbp), %rdi
movl -0x58(%rbp), %esi
addl -0x50(%rbp), %esi
subl $0x1, %esi
movq -0x10(%rbp), %rdx
movl -0x58(%rbp), %ecx
addl -0x5c(%rbp), %ecx
addl -0x50(%rbp), %ecx
subl $0x1, %ecx
movl -0x5c(%rbp), %r8d
addl -0x18(%rbp), %r8d
callq 0x8a90
movl -0x5c(%rbp), %edx
movq -0x28(%rbp), %rax
movl (%rax), %ecx
subl %edx, %ecx
movl %ecx, (%rax)
movl -0x5c(%rbp), %edx
movq -0x10(%rbp), %rax
movl (%rax), %ecx
subl %edx, %ecx
movl %ecx, (%rax)
jmp 0x87f8
movq -0x10(%rbp), %rcx
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
subl (%rcx), %eax
cmpl -0x18(%rbp), %eax
jge 0x87b8
movl $0x1, -0x4(%rbp)
jmp 0x881a
movl -0x18(%rbp), %ecx
movq -0x28(%rbp), %rax
addl (%rax), %ecx
movl %ecx, (%rax)
movl -0x18(%rbp), %ecx
movq -0x10(%rbp), %rax
addl (%rax), %ecx
movl %ecx, (%rax)
movl -0x50(%rbp), %eax
movq -0x28(%rbp), %rcx
addl (%rcx), %eax
cmpl -0x14(%rbp), %eax
jle 0x87f6
movq -0x10(%rbp), %rdi
movl -0x14(%rbp), %esi
movq -0x10(%rbp), %rdx
movl -0x14(%rbp), %ecx
addl -0x18(%rbp), %ecx
movl -0x54(%rbp), %r8d
callq 0x8a90
jmp 0x87f8
cmpb $0x0, -0x19(%rbp)
je 0x8813
movq -0x10(%rbp), %rdi
movl -0x14(%rbp), %esi
movl -0x14(%rbp), %edx
addl -0x18(%rbp), %edx
subl $0x1, %edx
callq 0x8e40
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x60, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)