// File: /workspace/github_c_makefile_star2_O2/ammarfaizi2[P]riyuu/src/third_party/cjson/cJSON_Utils.c
// Function: create_patches(cJSON*, unsigned char const*, cJSON*, cJSON*, int)
static void create_patches(cJSON * const patches, const unsigned char * const path, cJSON * const from, cJSON * const to, const cJSON_bool case_sensitive)
{
    if ((from == NULL) || (to == NULL))
    {
        return;
    }

    if ((from->type & 0xFF) != (to->type & 0xFF))
    {
        compose_patch(patches, (const unsigned char*)"replace", path, 0, to);
        return;
    }

    switch (from->type & 0xFF)
    {
        case cJSON_Number:
            if ((from->valueint != to->valueint) || (from->valuedouble != to->valuedouble))
            {
                compose_patch(patches, (const unsigned char*)"replace", path, NULL, to);
            }
            return;

        case cJSON_String:
            if (strcmp(from->valuestring, to->valuestring) != 0)
            {
                compose_patch(patches, (const unsigned char*)"replace", path, NULL, to);
            }
            return;

        case cJSON_Array:
        {
            size_t index = 0;
            cJSON *from_child = from->child;
            cJSON *to_child = to->child;
            unsigned char *new_path = (unsigned char*)cJSON_malloc(strlen((const char*)path) + 20 + sizeof("/")); /* Allow space for 64bit int. log10(2^64) = 20 */

            /* generate patches for all array elements that exist in both "from" and "to" */
            for (index = 0; (from_child != NULL) && (to_child != NULL); (void)(from_child = from_child->next), (void)(to_child = to_child->next), index++)
            {
                /* check if conversion to unsigned long is valid
                 * This should be eliminated at compile time by dead code elimination
                 * if size_t is an alias of unsigned long, or if it is bigger */
                if (index > ULONG_MAX)
                {
                    cJSON_free(new_path);
                    return;
                }
                sprintf((char*)new_path, "%s/%lu", path, (unsigned long)index); /* path of the current array element */
                create_patches(patches, new_path, from_child, to_child, case_sensitive);
            }

            /* remove leftover elements from 'from' that are not in 'to' */
            for (; (from_child != NULL); (void)(from_child = from_child->next))
            {
                /* check if conversion to unsigned long is valid
                 * This should be eliminated at compile time by dead code elimination
                 * if size_t is an alias of unsigned long, or if it is bigger */
                if (index > ULONG_MAX)
                {
                    cJSON_free(new_path);
                    return;
                }
                sprintf((char*)new_path, "%lu", (unsigned long)index);
                compose_patch(patches, (const unsigned char*)"remove", path, new_path, NULL);
            }
            /* add new elements in 'to' that were not in 'from' */
            for (; (to_child != NULL); (void)(to_child = to_child->next), index++)
            {
                compose_patch(patches, (const unsigned char*)"add", path, (const unsigned char*)"-", to_child);
            }
            cJSON_free(new_path);
            return;
        }

        case cJSON_Object:
        {
            cJSON *from_child = NULL;
            cJSON *to_child = NULL;
            sort_object(from, case_sensitive);
            sort_object(to, case_sensitive);

            from_child = from->child;
            to_child = to->child;
            /* for all object values in the object with more of them */
            while ((from_child != NULL) || (to_child != NULL))
            {
                int diff;
                if (from_child == NULL)
                {
                    diff = 1;
                }
                else if (to_child == NULL)
                {
                    diff = -1;
                }
                else
                {
                    diff = compare_strings((unsigned char*)from_child->string, (unsigned char*)to_child->string, case_sensitive);
                }

                if (diff == 0)
                {
                    /* both object keys are the same */
                    size_t path_length = strlen((const char*)path);
                    size_t from_child_name_length = pointer_encoded_length((unsigned char*)from_child->string);
                    unsigned char *new_path = (unsigned char*)cJSON_malloc(path_length + from_child_name_length + sizeof("/"));

                    sprintf((char*)new_path, "%s/", path);
                    encode_string_as_pointer(new_path + path_length + 1, (unsigned char*)from_child->string);

                    /* create a patch for the element */
                    create_patches(patches, new_path, from_child, to_child, case_sensitive);
                    cJSON_free(new_path);

                    from_child = from_child->next;
                    to_child = to_child->next;
                }
                else if (diff < 0)
                {
                    /* object element doesn't exist in 'to' --> remove it */
                    compose_patch(patches, (const unsigned char*)"remove", path, (unsigned char*)from_child->string, NULL);

                    from_child = from_child->next;
                }
                else
                {
                    /* object element doesn't exist in 'from' --> add it */
                    compose_patch(patches, (const unsigned char*)"add", path, (unsigned char*)to_child->string, to_child);

                    to_child = to_child->next;
                }
            }
            return;
        }

        default:
            break;
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movl %r8d, -0x34(%rbp)
movq %rcx, %r13
movq %rsi, -0x30(%rbp)
testq %rdx, %rdx
sete %al
testq %rcx, %rcx
sete %cl
orb %al, %cl
je 0x5b09
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rdx, %r12
movq %rdi, %r14
movzbl 0x18(%rdx), %eax
movzbl 0x18(%r13), %ecx
cmpl %ecx, %eax
jne 0x5c57
addl $-0x8, %eax
roll $0x1d, %eax
cmpl $0x7, %eax
ja 0x5afa
leaq 0x1696(%rip), %rcx      # 0x71c8
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movl 0x28(%r12), %eax
cmpl 0x28(%r13), %eax
jne 0x5c57
movsd 0x30(%r12), %xmm0
ucomisd 0x30(%r13), %xmm0
jne 0x5c57
jp 0x5c57
jmp 0x5afa
movq 0x10(%r12), %r15
movq 0x10(%r13), %r12
movq -0x30(%rbp), %rdi
callq 0x2140
leaq 0x16(%rax), %rdi
callq 0x4abb
movq %rax, %r13
xorl %ebx, %ebx
testq %r15, %r15
je 0x5bf8
testq %r12, %r12
je 0x5bf8
movq %r13, %rdi
leaq 0x16ec(%rip), %rsi      # 0x7285
movq -0x30(%rbp), %rdx
movq %rbx, %rcx
xorl %eax, %eax
callq 0x22d0
movq %r14, %rdi
movq %r13, %rsi
movq %r15, %rdx
movq %r12, %rcx
movl -0x34(%rbp), %r8d
callq 0x5ace
movq (%r15), %r15
movq (%r12), %r12
incq %rbx
jmp 0x5b85
leaq 0x16b9(%rip), %rsi      # 0x7288
movq %r13, %rdi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x22d0
leaq 0x1685(%rip), %rsi      # 0x7268
movq %r14, %rdi
movq -0x30(%rbp), %rdx
movq %r13, %rcx
xorl %r8d, %r8d
callq 0x5933
movq (%r15), %r15
testq %r15, %r15
jne 0x5bc8
leaq 0x1660(%rip), %rbx      # 0x7264
leaq 0x1654(%rip), %r15      # 0x725f
testq %r12, %r12
je 0x5c2b
movq %r14, %rdi
movq %rbx, %rsi
movq -0x30(%rbp), %rdx
movq %r15, %rcx
movq %r12, %r8
callq 0x5933
movq (%r12), %r12
jmp 0x5c0b
movq %r13, %rdi
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x4ac6
movq 0x20(%r12), %rdi
movq 0x20(%r13), %rsi
callq 0x2220
testl %eax, %eax
je 0x5afa
leaq 0x1611(%rip), %rsi      # 0x726f
movq %r14, %rdi
movq -0x30(%rbp), %rdx
xorl %ecx, %ecx
movq %r13, %r8
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x5933
movq %r12, %rdi
movl -0x34(%rbp), %ebx
movl %ebx, %esi
callq 0x5de0
movq %r13, %rdi
movl %ebx, %esi
callq 0x5de0
addq $0x10, %r12
movq 0x10(%r13), %r13
movq (%r12), %r12
movq %r12, %rax
orq %r13, %rax
je 0x5afa
testq %r12, %r12
je 0x5cd5
movq 0x38(%r12), %rbx
testq %r13, %r13
je 0x5cf8
movq 0x38(%r13), %r15
movq %rbx, %rdi
movq %r15, %rsi
movl -0x34(%rbp), %edx
callq 0x63d2
testl %eax, %eax
je 0x5d13
jns 0x5cd9
jmp 0x5cf8
movq 0x38(%r13), %r15
movq %r14, %rdi
leaq 0x1581(%rip), %rsi      # 0x7264
movq -0x30(%rbp), %rdx
movq %r15, %rcx
movq %r13, %r8
callq 0x5933
movq (%r13), %r13
jmp 0x5ca0
movq %r14, %rdi
leaq 0x1566(%rip), %rsi      # 0x7268
movq -0x30(%rbp), %rdx
movq %rbx, %rcx
xorl %r8d, %r8d
callq 0x5933
jmp 0x5c9c
movq -0x30(%rbp), %rdi
callq 0x2140
movq %rax, %r15
movq %rbx, %rdi
callq 0x51f2
leaq (%r15,%rax), %rdi
addq $0x2, %rdi
callq 0x4abb
movq %rax, %rbx
movq %rax, %rdi
leaq 0x1540(%rip), %rsi      # 0x7281
movq -0x30(%rbp), %rdx
xorl %eax, %eax
callq 0x22d0
leaq (%rbx,%r15), %rdi
incq %rdi
movq 0x38(%r12), %rsi
callq 0x5218
movq %r14, %rdi
movq %rbx, %rsi
movq %r12, %rdx
movq %r13, %rcx
movl -0x34(%rbp), %r8d
callq 0x5ace
movq %rbx, %rdi
callq 0x4ac6
movq (%r13), %r13
jmp 0x5c9c