// File: /workspace/github_c_makefile_star2_O3/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
testq %rdi, %rdi
je 0x6ed1
movq %rdi, %rbx
movq (%rdi), %r15
testq %r15, %r15
je 0x6fc2
movl %esi, %r14d
movq %rbx, %rax
movq 0x38(%rax), %rdi
movq 0x38(%r15), %rsi
movl %r14d, %edx
callq 0x6dd0
testl %eax, %eax
jns 0x6ed8
movq %r15, %rax
movq (%r15), %rcx
movq %rcx, %r15
testq %rcx, %rcx
jne 0x6eaa
jmp 0x6fc2
xorl %ebx, %ebx
jmp 0x6fc2
movq %rbx, %rax
movq %rbx, %r15
movq (%r15), %r15
movq (%rax), %rax
testq %rax, %rax
je 0x6ef1
movq (%rax), %rax
testq %rax, %rax
jne 0x6ede
testq %r15, %r15
je 0x6f08
movq 0x8(%r15), %rax
testq %rax, %rax
je 0x6f08
xorl %ecx, %ecx
movq %rcx, (%rax)
movq %rcx, 0x8(%r15)
movq %rbx, %rdi
movl %r14d, %esi
callq 0x6e82
movq %rax, %rbx
movq %r15, %rdi
movl %r14d, %esi
callq 0x6e82
movq %rax, %r15
testq %rbx, %rbx
sete %al
testq %r15, %r15
setne %dl
sete %cl
orb %al, %cl
jne 0x6fa3
xorl %r12d, %r12d
xorl %r13d, %r13d
movq 0x38(%rbx), %rdi
movq 0x38(%r15), %rsi
movl %r14d, %edx
callq 0x6dd0
movq %rbx, %rcx
testl %eax, %eax
js 0x6f57
movq %r15, %rcx
movq %rcx, %rax
testq %r13, %r13
je 0x6f6a
movq %rcx, (%r12)
movq %r12, 0x8(%rcx)
movq %r13, %rax
cmpq %rcx, %rbx
je 0x6f74
movq (%r15), %r15
jmp 0x6f77
movq (%rbx), %rbx
testq %r15, %r15
setne %dl
testq %rbx, %rbx
je 0x6f8d
movq %rcx, %r12
movq %rax, %r13
testq %r15, %r15
jne 0x6f3d
testq %rbx, %rbx
je 0x6fac
movq %rbx, (%rcx)
movq %rcx, 0x8(%rbx)
movq %rax, %rbx
testq %r15, %r15
jne 0x6fb8
jmp 0x6fc2
testq %rbx, %rbx
jne 0x6fc2
xorl %ecx, %ecx
xorl %eax, %eax
testb %dl, %dl
je 0x6fbf
movq %r15, %rbx
testq %rax, %rax
je 0x6fc2
movq %r15, (%rcx)
movq %rcx, 0x8(%r15)
movq %rax, %rbx
movq %rbx, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq