// File: /workspace/github_c_makefile_star2_O3/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 $0x28, %rsp
movl %r8d, -0x2c(%rbp)
movq %rcx, %r13
testq %rdx, %rdx
sete %al
testq %rcx, %rcx
sete %cl
orb %al, %cl
je 0x63b9
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rdx, %r14
movq %rsi, %rbx
movzbl 0x18(%rdx), %eax
movzbl 0x18(%r13), %ecx
cmpl %ecx, %eax
jne 0x64c0
addl $-0x8, %eax
roll $0x1d, %eax
cmpl $0x7, %eax
ja 0x63aa
leaq 0x1de6(%rip), %rcx      # 0x81c8
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
movq %rdi, -0x38(%rbp)
jmpq *%rax
movl 0x28(%r14), %eax
cmpl 0x28(%r13), %eax
jne 0x64c0
movsd 0x30(%r14), %xmm0
ucomisd 0x30(%r13), %xmm0
jne 0x64c0
jp 0x64c0
jmp 0x63aa
movq 0x10(%r14), %r15
movq 0x10(%r13), %r12
movq %rbx, -0x48(%rbp)
movq %rbx, %rdi
callq 0x2140
leaq 0x16(%rax), %rdi
callq 0x5094
testq %r15, %r15
setne %dl
testq %r12, %r12
setne %cl
andb %dl, %cl
cmpb $0x1, %cl
movq %rax, -0x40(%rbp)
jne 0x664a
xorl %r14d, %r14d
movl -0x2c(%rbp), %r13d
movq %rax, %rbx
movq %rax, %rdi
leaq 0x1e22(%rip), %rsi      # 0x8285
movq -0x48(%rbp), %rdx
movq %r14, %rcx
xorl %eax, %eax
callq 0x22c0
movq -0x38(%rbp), %rdi
movq %rbx, %rsi
movq %r15, %rdx
movq %r12, %rcx
movl %r13d, %r8d
callq 0x6382
movq (%r15), %r15
movq (%r12), %r12
incq %r14
testq %r15, %r15
je 0x664f
testq %r12, %r12
movq -0x40(%rbp), %rax
jne 0x6456
jmp 0x6653
movq 0x20(%r14), %rdi
movq 0x20(%r13), %rsi
callq 0x2210
movq -0x38(%rbp), %rdi
testl %eax, %eax
je 0x63aa
leaq 0x1da8(%rip), %rsi      # 0x826f
movq %rbx, %rdx
xorl %ecx, %ecx
movq %r13, %r8
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x6198
movq 0x10(%r14), %rdi
movl -0x2c(%rbp), %r15d
movl %r15d, %esi
callq 0x6e82
movq %rax, 0x10(%r14)
movq 0x10(%r13), %rdi
movl %r15d, %esi
callq 0x6e82
movq -0x38(%rbp), %rdi
movq %rax, %r12
movq %rax, 0x10(%r13)
movq 0x10(%r14), %r13
movq %r13, %rax
orq %r12, %rax
je 0x63aa
testq %r13, %r13
je 0x6566
movq 0x38(%r13), %r14
testq %r12, %r12
je 0x6548
movq 0x38(%r12), %r15
movq %r14, %rdi
movq %r15, %rsi
movl -0x2c(%rbp), %edx
callq 0x6dd0
testl %eax, %eax
je 0x6585
movq -0x38(%rbp), %rdi
jns 0x656b
leaq 0x1d19(%rip), %rsi      # 0x8268
movq %rbx, %rdx
movq %r14, %rcx
xorl %r8d, %r8d
callq 0x6198
movq (%r13), %r13
jmp 0x6635
movq 0x38(%r12), %r15
leaq 0x1cf2(%rip), %rsi      # 0x8264
movq %rbx, %rdx
movq %r15, %rcx
movq %r12, %r8
callq 0x6198
jmp 0x6631
movq %rbx, %rdi
callq 0x2140
movq %rax, %r15
xorl %eax, %eax
movzbl (%r14), %ecx
cmpl $0x2f, %ecx
je 0x65a6
cmpl $0x7e, %ecx
je 0x65a6
testl %ecx, %ecx
jne 0x65a9
jmp 0x65b1
incq %rax
incq %r14
incq %rax
jmp 0x6592
leaq (%r15,%rax), %rdi
addq $0x2, %rdi
callq 0x5094
movq %rax, %r14
movq %rax, %rdi
leaq 0x1cb6(%rip), %rsi      # 0x8281
movq %rbx, %rdx
xorl %eax, %eax
callq 0x22c0
movq %r14, %rax
addq %r15, %rax
movq 0x38(%r13), %rcx
movq -0x38(%rbp), %rdi
movzbl (%rcx), %edx
cmpl $0x2f, %edx
je 0x6603
cmpl $0x7e, %edx
je 0x65fb
incq %rax
testl %edx, %edx
je 0x6610
movb %dl, (%rax)
jmp 0x660b
movw $0x317e, 0x1(%rax)      # imm = 0x317E
jmp 0x6607
movb $0x31, 0x2(%rax)
addq $0x2, %rax
incq %rcx
jmp 0x65e3
movb $0x0, (%rax)
movq %r14, %rsi
movq %r13, %rdx
movq %r12, %rcx
movl -0x2c(%rbp), %r8d
callq 0x6382
movq %r14, %rdi
callq 0x509f
movq (%r13), %r13
movq (%r12), %r12
movq %r13, %rax
orq %r12, %rax
movq -0x38(%rbp), %rdi
jne 0x651d
jmp 0x63aa
xorl %r14d, %r14d
jmp 0x6653
movq -0x40(%rbp), %rax
testq %r15, %r15
movq -0x38(%rbp), %rbx
je 0x6694
movq %rax, %r13
movq %r13, %rdi
leaq 0x1c1f(%rip), %rsi      # 0x8288
movq %r14, %rdx
xorl %eax, %eax
callq 0x22c0
movq %rbx, %rdi
leaq 0x1beb(%rip), %rsi      # 0x8268
movq -0x48(%rbp), %rdx
movq %r13, %rcx
xorl %r8d, %r8d
callq 0x6198
movq (%r15), %r15
testq %r15, %r15
jne 0x665f
testq %r12, %r12
movq -0x48(%rbp), %r13
je 0x66c8
leaq 0x1bc0(%rip), %r14      # 0x8264
leaq 0x1bb4(%rip), %r15      # 0x825f
movq %rbx, %rdi
movq %r14, %rsi
movq %r13, %rdx
movq %r15, %rcx
movq %r12, %r8
callq 0x6198
movq (%r12), %r12
testq %r12, %r12
jne 0x66ab
movq -0x40(%rbp), %rdi
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x509f