// File: /workspace/github_c_makefile_star2_O3/seojangho[P]clee/simclist.c
// Function: list_sort_quicksort
static void list_sort_quicksort(list_t *restrict l, int versus, 
        unsigned int first, struct list_entry_s *fel,
        unsigned int last, struct list_entry_s *lel) {
    unsigned int pivotid;
    unsigned int i;
    register struct list_entry_s *pivot;
    struct list_entry_s *left, *right;
    void *tmpdata;
#ifdef SIMCLIST_WITH_THREADS
    pthread_t tid;
    int traised;
#endif


    if (last <= first)      /* <= 1-element lists are always sorted */
        return;
    
    if (last - first+1 <= SIMCLIST_MINQUICKSORTELS) {
        list_sort_selectionsort(l, versus, first, fel, last, lel);
        return;
    }
    
    /* base of iteration: one element list */
    if (! (last > first)) return;

    pivotid = (get_random() % (last - first + 1));
    /* pivotid = (last - first + 1) / 2; */

    /* find pivot */
    if (pivotid < (last - first + 1)/2) {
        for (i = 0, pivot = fel; i < pivotid; pivot = pivot->next, i++);
    } else {
        for (i = last - first, pivot = lel; i > pivotid; pivot = pivot->prev, i--);
    }

    /* smaller PIVOT bigger */
    left = fel;
    right = lel;
    /* iterate     --- left ---> PIV <--- right --- */
    while (left != pivot && right != pivot) {
        for (; left != pivot && (l->attrs.comparator(left->data, pivot->data) * -versus <= 0); left = left->next);
        /* left points to a smaller element, or to pivot */
        for (; right != pivot && (l->attrs.comparator(right->data, pivot->data) * -versus >= 0); right = right->prev);
        /* right points to a bigger element, or to pivot */
        if (left != pivot && right != pivot) {
            /* swap, then move iterators */
            tmpdata = left->data;
            left->data = right->data;
            right->data = tmpdata;

            left = left->next;
            right = right->prev;
        }
    }

    /* now either left points to pivot (end run), or right */
    if (right == pivot) {    /* left part longer */
        while (left != pivot) {
            if (l->attrs.comparator(left->data, pivot->data) * -versus > 0) {
                tmpdata = left->data;
                left->data = pivot->prev->data;
                pivot->prev->data = pivot->data;
                pivot->data = tmpdata;
                pivot = pivot->prev;
                pivotid--;
                if (pivot == left) break;
            } else {
                left = left->next;
            }
        }
    } else {                /* right part longer */
        while (right != pivot) {
            if (l->attrs.comparator(right->data, pivot->data) * -versus < 0) {
                /* move current right before pivot */
                tmpdata = right->data;
                right->data = pivot->next->data;
                pivot->next->data = pivot->data;
                pivot->data = tmpdata;
                pivot = pivot->next;
                pivotid++;
                if (pivot == right) break;
            } else {
                right = right->prev;
            }
        }
    }

    /* sort sublists A and B :       |---A---| pivot |---B---| */

#ifdef SIMCLIST_WITH_THREADS
    traised = 0;
    if (pivotid > 0) {
        /* prepare wrapped args, then start thread */
        if (l->threadcount < SIMCLIST_MAXTHREADS-1) {
            struct list_sort_wrappedparams *wp = (struct list_sort_wrappedparams *)malloc(sizeof(struct list_sort_wrappedparams));
            l->threadcount++;
            traised = 1;
            wp->l = l;
            wp->versus = versus;
            wp->first = first;
            wp->fel = fel;
            wp->last = first+pivotid-1;
            wp->lel = pivot->prev;
            if (pthread_create(&tid, NULL, list_sort_quicksort_threadwrapper, wp) != 0) {
                free(wp);
                traised = 0;
                list_sort_quicksort(l, versus, first, fel, first+pivotid-1, pivot->prev);
            }
        } else {
            list_sort_quicksort(l, versus, first, fel, first+pivotid-1, pivot->prev);
        }
    }
    if (first + pivotid < last) list_sort_quicksort(l, versus, first+pivotid+1, pivot->next, last, lel);
    if (traised) {
        pthread_join(tid, (void **)NULL);
        l->threadcount--;
    }
#else
    if (pivotid > 0) list_sort_quicksort(l, versus, first, fel, first+pivotid-1, pivot->prev);
    if (first + pivotid < last) list_sort_quicksort(l, versus, first+pivotid+1, pivot->next, last, lel);
#endif
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rdi, 0x8(%rsp)
cmpl %edx, %r8d
jbe 0x4b0d
movq %r9, %r12
movq %rcx, %r13
movq %r9, 0x18(%rsp)
movl %esi, 0x4(%rsp)
movl %r8d, 0x2c(%rsp)
movl %r8d, %ebx
subl %edx, %ebx
leal 0x1(%rbx), %ebp
cmpl $0x18, %ebp
jbe 0x4aa4
movq %rdx, 0x30(%rsp)
callq 0x31e0
xorl %edx, %edx
divl %ebp
shrl %ebp
cmpl %ebp, %edx
movq %rdx, 0x10(%rsp)
jae 0x48d3
movq %r13, %r15
testl %edx, %edx
movl 0x4(%rsp), %ebp
je 0x48ef
xorl %eax, %eax
movq %r13, %r15
movq 0x8(%r15), %r15
incl %eax
cmpl %edx, %eax
jb 0x48c7
jmp 0x48ef
movq %r12, %r15
movq %r13, %r14
cmpl %edx, %ebx
movl 0x4(%rsp), %ebp
jbe 0x4a01
movq 0x10(%r15), %r15
decl %ebx
cmpl %edx, %ebx
ja 0x48e5
cmpq %r15, %r13
sete %al
cmpq %r12, %r15
sete %cl
orb %al, %cl
movq %r13, 0x20(%rsp)
movq %r13, %r14
movq %r12, %rbx
jne 0x4998
movq 0x8(%rsp), %rax
movq 0x40(%rax), %r13
movq 0x18(%rsp), %rbx
movq 0x20(%rsp), %r14
movq (%r14), %rdi
movq (%r15), %rsi
callq *%r13
movl %eax, %r12d
imull %ebp, %r12d
cmpl $0x80000000, %r12d      # imm = 0x80000000
ja 0x4943
movq 0x8(%r14), %r14
cmpq %r15, %r14
jne 0x4921
cmpq %r15, %rbx
je 0x498e
movq 0x8(%rsp), %rax
movq 0x40(%rax), %rbp
movq (%rbx), %rdi
movq (%r15), %rsi
callq *%rbp
imull 0x4(%rsp), %eax
decl %eax
jns 0x496d
movq 0x10(%rbx), %rbx
cmpq %r15, %rbx
jne 0x4951
jmp 0x498a
cmpl $0x80000001, %r12d      # imm = 0x80000001
jb 0x498a
movq (%r14), %rax
movq (%rbx), %rcx
movq %rcx, (%r14)
movq %rax, (%rbx)
movq 0x8(%r14), %r14
movq 0x10(%rbx), %rbx
movl 0x4(%rsp), %ebp
cmpq %r15, %r14
je 0x4998
cmpq %r15, %rbx
jne 0x4921
cmpq %r15, %rbx
je 0x49f7
movq 0x8(%rsp), %rax
movq 0x40(%rax), %r14
movq 0x18(%rsp), %r12
movq 0x20(%rsp), %r13
movq (%rbx), %rdi
movq (%r15), %rsi
callq *%r14
imull %ebp, %eax
decl %eax
js 0x49ec
movq (%rbx), %rax
movq 0x8(%r15), %rcx
movq (%rcx), %rdx
movq %rdx, (%rbx)
movq (%r15), %rdx
movq %rdx, (%rcx)
movq %rax, (%r15)
movq 0x10(%rsp), %rax
incl %eax
movq %rax, 0x10(%rsp)
movq %rcx, %r15
cmpq %rbx, %rcx
jne 0x49f0
jmp 0x4a56
movq 0x10(%rbx), %rbx
cmpq %r15, %rbx
jne 0x49b0
jmp 0x4a56
movq 0x18(%rsp), %r12
movq 0x20(%rsp), %r13
cmpq %r15, %r14
je 0x4a56
movq 0x8(%rsp), %rax
movq 0x40(%rax), %rbx
movq (%r14), %rdi
movq (%r15), %rsi
callq *%rbx
imull %ebp, %eax
cmpl $0x80000001, %eax       # imm = 0x80000001
jb 0x4a4d
movq (%r14), %rax
movq 0x10(%r15), %rcx
movq (%rcx), %rdx
movq %rdx, (%r14)
movq (%r15), %rdx
movq %rdx, (%rcx)
movq %rax, (%r15)
movq 0x10(%rsp), %rax
decl %eax
movq %rax, 0x10(%rsp)
movq %rcx, %r15
cmpq %r14, %rcx
jne 0x4a51
jmp 0x4a56
movq 0x8(%r14), %r14
cmpq %r15, %r14
jne 0x4a0f
movq 0x10(%rsp), %rax
testl %eax, %eax
movq 0x30(%rsp), %rbx
je 0x4a85
leal (%rbx,%rax), %r8d
decl %r8d
movq 0x10(%r15), %r9
movq 0x8(%rsp), %rdi
movl %ebp, %esi
movl %ebx, %edx
movq %r13, %rcx
callq 0x485d
movq 0x10(%rsp), %rax
addl %ebx, %eax
movl 0x2c(%rsp), %r8d
cmpl %r8d, %eax
jae 0x4b0d
incl %eax
movq 0x8(%r15), %r13
movl %eax, %edx
cmpl %r8d, %eax
jb 0x488d
jmp 0x4b0d
cmpq %r12, %r13
movl 0x4(%rsp), %ebp
je 0x4b0d
movq 0x8(%r12), %rax
movq 0x8(%r13), %rbx
cmpq %rax, %rbx
je 0x4b04
movq 0x8(%rsp), %rax
movq 0x40(%rax), %r14
movq %r13, %r15
movq (%r15), %rdi
movq (%rbx), %rsi
callq *%r14
imull %ebp, %eax
movq %rbx, %rcx
cmpl $0x80000001, %eax       # imm = 0x80000001
jae 0x4ae0
movq %r15, %rcx
movq 0x8(%rbx), %rbx
movq %rcx, %r15
cmpq 0x8(%r12), %rbx
jne 0x4ac7
cmpq %r13, %rcx
je 0x4b01
movq (%r13), %rax
movq (%rcx), %rdx
movq %rdx, (%r13)
movq %rax, (%rcx)
movq %rbx, %rax
movq 0x8(%r13), %r13
cmpq %r12, %r13
jne 0x4ab2
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq