// File: /workspace/github_c_makefile_star2_O1/ammarfaizi2[P]riyuu/src/third_party/cjson/cJSON_Utils.c
// Function: sort_list(cJSON*, int)
static cJSON *sort_list(cJSON *list, const cJSON_bool case_sensitive)
{
    cJSON *first = list;
    cJSON *second = list;
    cJSON *current_item = list;
    cJSON *result = list;
    cJSON *result_tail = NULL;

    if ((list == NULL) || (list->next == NULL))
    {
        /* One entry is sorted already. */
        return result;
    }

    while ((current_item != NULL) && (current_item->next != NULL) && (compare_strings((unsigned char*)current_item->string, (unsigned char*)current_item->next->string, case_sensitive) < 0))
    {
        /* Test for list sorted. */
        current_item = current_item->next;
    }
    if ((current_item == NULL) || (current_item->next == NULL))
    {
        /* Leave sorted lists unmodified. */
        return result;
    }

    /* reset pointer to the beginning */
    current_item = list;
    while (current_item != NULL)
    {
        /* Walk two pointers to find the middle. */
        second = second->next;
        current_item = current_item->next;
        /* advances current_item two steps at a time */
        if (current_item != NULL)
        {
            current_item = current_item->next;
        }
    }
    if ((second != NULL) && (second->prev != NULL))
    {
        /* Split the lists */
        second->prev->next = NULL;
        second->prev = NULL;
    }

    /* Recursively sort the sub-lists. */
    first = sort_list(first, case_sensitive);
    second = sort_list(second, case_sensitive);
    result = NULL;

    /* Merge the sub-lists */
    while ((first != NULL) && (second != NULL))
    {
        cJSON *smaller = NULL;
        if (compare_strings((unsigned char*)first->string, (unsigned char*)second->string, case_sensitive) < 0)
        {
            smaller = first;
        }
        else
        {
            smaller = second;
        }

        if (result == NULL)
        {
            /* start merged list with the smaller element */
            result_tail = smaller;
            result = smaller;
        }
        else
        {
            /* add smaller element to the list */
            result_tail->next = smaller;
            smaller->prev = result_tail;
            result_tail = smaller;
        }

        if (first == smaller)
        {
            first = first->next;
        }
        else
        {
            second = second->next;
        }
    }

    if (first != NULL)
    {
        /* Append rest of first list. */
        if (result == NULL)
        {
            return first;
        }
        result_tail->next = first;
        first->prev = result_tail;
    }
    if (second != NULL)
    {
        /* Append rest of second list */
        if (result == NULL)
        {
            return second;
        }
        result_tail->next = second;
        second->prev = result_tail;
    }

    return result;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rdi, %r13
testq %rdi, %rdi
je 0x714e
cmpq $0x0, (%r13)
je 0x714e
movl %esi, %r14d
movq %r13, %rcx
movq (%rcx), %rbx
testq %rbx, %rbx
je 0x714e
movq 0x38(%rcx), %rdi
movq 0x38(%rbx), %rsi
movl %r14d, %edx
callq 0x6f52
movq %rbx, %rcx
testl %eax, %eax
js 0x702f
testq %rbx, %rbx
je 0x714e
movq %r13, %rax
movq %r13, %r15
movq (%rax), %rax
testq %rax, %rax
je 0x706e
movq (%rax), %rax
jmp 0x7070
xorl %eax, %eax
movq (%r15), %r15
testq %rax, %rax
jne 0x7061
testq %r15, %r15
je 0x708f
movq 0x8(%r15), %rax
testq %rax, %rax
je 0x708f
xorl %ecx, %ecx
movq %rcx, (%rax)
movq %rcx, 0x8(%r15)
movq %r13, %rdi
movl %r14d, %esi
callq 0x7004
movq %rax, %r12
movq %r15, %rdi
movl %r14d, %esi
callq 0x7004
movq %rax, %r15
testq %r12, %r12
setne %dl
testq %rax, %rax
setne %al
movl %edx, %ecx
andb %al, %cl
cmpb $0x1, %cl
jne 0x711c
xorl %r13d, %r13d
xorl %ebx, %ebx
movq 0x38(%r12), %rdi
movq 0x38(%r15), %rsi
movl %r14d, %edx
callq 0x6f52
movq %r12, %rcx
testl %eax, %eax
js 0x70e0
movq %r15, %rcx
movq %rcx, %rsi
testq %rbx, %rbx
je 0x70f3
movq %rcx, (%r13)
movq %r13, 0x8(%rcx)
movq %rbx, %rsi
cmpq %rcx, %r12
je 0x70fd
movq (%r15), %r15
jmp 0x7101
movq (%r12), %r12
testq %r15, %r15
setne %al
testq %r12, %r12
setne %dl
je 0x7120
movq %rcx, %r13
movq %rsi, %rbx
testq %r15, %r15
jne 0x70c5
jmp 0x7120
xorl %esi, %esi
xorl %ecx, %ecx
testb %dl, %dl
movq %rsi, %r13
je 0x7134
testq %r13, %r13
je 0x7146
movq %r12, (%rcx)
movq %rcx, 0x8(%r12)
testb %al, %al
je 0x714e
testq %r13, %r13
je 0x714b
movq %r15, (%rcx)
movq %rcx, 0x8(%r15)
jmp 0x714e
movq %r12, %r13
jmp 0x714e
movq %r15, %r13
movq %r13, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq