// File: /workspace/github_c_makefile_star2_O2/ammarfaizi2[P]riyuu/src/third_party/cjson/cJSON_Utils.c
// Function: apply_patch(cJSON*, cJSON const*, int)
static int apply_patch(cJSON *object, const cJSON *patch, const cJSON_bool case_sensitive)
{
    cJSON *path = NULL;
    cJSON *value = NULL;
    cJSON *parent = NULL;
    enum patch_operation opcode = INVALID;
    unsigned char *parent_pointer = NULL;
    unsigned char *child_pointer = NULL;
    int status = 0;

    path = get_object_item(patch, "path", case_sensitive);
    if (!cJSON_IsString(path))
    {
        /* malformed patch. */
        status = 2;
        goto cleanup;
    }

    opcode = decode_patch_operation(patch, case_sensitive);
    if (opcode == INVALID)
    {
        status = 3;
        goto cleanup;
    }
    else if (opcode == TEST)
    {
        /* compare value: {...} with the given path */
        status = !compare_json(get_item_from_pointer(object, path->valuestring, case_sensitive), get_object_item(patch, "value", case_sensitive), case_sensitive);
        goto cleanup;
    }

    /* special case for replacing the root */
    if (path->valuestring[0] == '\0')
    {
        if (opcode == REMOVE)
        {
            static const cJSON invalid = { NULL, NULL, NULL, cJSON_Invalid, NULL, 0, 0, NULL};

            overwrite_item(object, invalid);

            status = 0;
            goto cleanup;
        }

        if ((opcode == REPLACE) || (opcode == ADD))
        {
            value = get_object_item(patch, "value", case_sensitive);
            if (value == NULL)
            {
                /* missing "value" for add/replace. */
                status = 7;
                goto cleanup;
            }

            value = cJSON_Duplicate(value, 1);
            if (value == NULL)
            {
                /* out of memory for add/replace. */
                status = 8;
                goto cleanup;
            }

            overwrite_item(object, *value);

            /* delete the duplicated value */
            cJSON_free(value);
            value = NULL;

            /* the string "value" isn't needed */
            if (object->string != NULL)
            {
                cJSON_free(object->string);
                object->string = NULL;
            }

            status = 0;
            goto cleanup;
        }
    }

    if ((opcode == REMOVE) || (opcode == REPLACE))
    {
        /* Get rid of old. */
        cJSON *old_item = detach_path(object, (unsigned char*)path->valuestring, case_sensitive);
        if (old_item == NULL)
        {
            status = 13;
            goto cleanup;
        }
        cJSON_Delete(old_item);
        if (opcode == REMOVE)
        {
            /* For Remove, this job is done. */
            status = 0;
            goto cleanup;
        }
    }

    /* Copy/Move uses "from". */
    if ((opcode == MOVE) || (opcode == COPY))
    {
        cJSON *from = get_object_item(patch, "from", case_sensitive);
        if (from == NULL)
        {
            /* missing "from" for copy/move. */
            status = 4;
            goto cleanup;
        }

        if (opcode == MOVE)
        {
            value = detach_path(object, (unsigned char*)from->valuestring, case_sensitive);
        }
        if (opcode == COPY)
        {
            value = get_item_from_pointer(object, from->valuestring, case_sensitive);
        }
        if (value == NULL)
        {
            /* missing "from" for copy/move. */
            status = 5;
            goto cleanup;
        }
        if (opcode == COPY)
        {
            value = cJSON_Duplicate(value, 1);
        }
        if (value == NULL)
        {
            /* out of memory for copy/move. */
            status = 6;
            goto cleanup;
        }
    }
    else /* Add/Replace uses "value". */
    {
        value = get_object_item(patch, "value", case_sensitive);
        if (value == NULL)
        {
            /* missing "value" for add/replace. */
            status = 7;
            goto cleanup;
        }
        value = cJSON_Duplicate(value, 1);
        if (value == NULL)
        {
            /* out of memory for add/replace. */
            status = 8;
            goto cleanup;
        }
    }

    /* Now, just add "value" to "path". */

    /* split pointer in parent and child */
    parent_pointer = cJSONUtils_strdup((unsigned char*)path->valuestring);
    child_pointer = (unsigned char*)strrchr((char*)parent_pointer, '/');
    if (child_pointer != NULL)
    {
        child_pointer[0] = '\0';
        child_pointer++;
    }
    parent = get_item_from_pointer(object, (char*)parent_pointer, case_sensitive);
    decode_pointer_inplace(child_pointer);

    /* add, remove, replace, move, copy, test. */
    if ((parent == NULL) || (child_pointer == NULL))
    {
        /* Couldn't find object to add to. */
        status = 9;
        goto cleanup;
    }
    else if (cJSON_IsArray(parent))
    {
        if (strcmp((char*)child_pointer, "-") == 0)
        {
            cJSON_AddItemToArray(parent, value);
            value = NULL;
        }
        else
        {
            size_t index = 0;
            if (!decode_array_index_from_pointer(child_pointer, &index))
            {
                status = 11;
                goto cleanup;
            }

            if (!insert_item_in_array(parent, index, value))
            {
                status = 10;
                goto cleanup;
            }
            value = NULL;
        }
    }
    else if (cJSON_IsObject(parent))
    {
        if (case_sensitive)
        {
            cJSON_DeleteItemFromObjectCaseSensitive(parent, (char*)child_pointer);
        }
        else
        {
            cJSON_DeleteItemFromObject(parent, (char*)child_pointer);
        }
        cJSON_AddItemToObject(parent, (char*)child_pointer, value);
        value = NULL;
    }
    else /* parent is not an object */
    {
        /* Couldn't find object to add to. */
        status = 9;
        goto cleanup;
    }

cleanup:
    if (value != NULL)
    {
        cJSON_Delete(value);
    }
    if (parent_pointer != NULL)
    {
        cJSON_free(parent_pointer);
    }

    return status;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movl %edx, %ebx
movq %rsi, %r12
movq %rdi, %r14
leaq 0x1df9(%rip), %rsi      # 0x724f
movq %r12, %rdi
callq 0x60e1
movq %rax, %r15
movq %rax, %rdi
callq 0x2b71
testl %eax, %eax
je 0x551f
leaq 0x1de9(%rip), %rsi      # 0x7261
movq %r12, %rdi
movl %ebx, %edx
callq 0x60e1
movq %rax, %r13
movq %rax, %rdi
callq 0x2b71
testl %eax, %eax
je 0x551b
movq 0x20(%r13), %r13
leaq 0x1dc4(%rip), %rsi      # 0x7264
movq %r13, %rdi
callq 0x2220
testl %eax, %eax
je 0x5535
leaq 0x1db1(%rip), %rsi      # 0x7268
movq %r13, %rdi
callq 0x2220
testl %eax, %eax
je 0x5539
leaq 0x1da5(%rip), %rsi      # 0x726f
movq %r13, %rdi
callq 0x2220
testl %eax, %eax
je 0x5552
leaq 0x1d8d(%rip), %rsi      # 0x726a
movq %r13, %rdi
callq 0x2220
testl %eax, %eax
je 0x56da
leaq 0x1d83(%rip), %rsi      # 0x7277
movq %r13, %rdi
callq 0x2220
testl %eax, %eax
je 0x570b
leaq 0x1d71(%rip), %rsi      # 0x727c
movq %r13, %rdi
callq 0x2220
testl %eax, %eax
je 0x57c6
pushq $0x3
jmp 0x5521
pushq $0x2
popq %r13
movl %r13d, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
pushq $0x1
jmp 0x5554
movq 0x20(%r15), %rsi
cmpb $0x0, (%rsi)
je 0x56e5
addq $0x20, %r15
movb $0x1, %r13b
jmp 0x5611
pushq $0x3
popq %rax
xorl %r13d, %r13d
xorl %edi, %edi
movq 0x20(%r15), %rsi
cmpb $0x0, (%rsi)
sete %cl
movl %eax, %edx
andl $-0x3, %edx
cmpl $0x1, %edx
sete %dl
testb %dl, %cl
je 0x55f9
leaq 0x1cd6(%rip), %rsi      # 0x7254
movq %r12, %rdi
movl %ebx, %edx
callq 0x60e1
testq %rax, %rax
je 0x5690
pushq $0x1
popq %rsi
movq %rax, %rdi
callq 0x466e
testq %rax, %rax
je 0x5697
movq %rax, %rbx
movups (%rax), %xmm0
movups 0x10(%rax), %xmm1
movups 0x20(%rax), %xmm2
movups 0x30(%rax), %xmm3
movups %xmm3, 0x30(%rsp)
movups %xmm2, 0x20(%rsp)
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
movq %r14, %rdi
callq 0x6226
movq %rbx, %rdi
callq 0x4ac6
movq 0x38(%r14), %rdi
xorl %r13d, %r13d
testq %rdi, %rdi
je 0x5523
callq 0x4ac6
andq $0x0, 0x38(%r14)
jmp 0x5523
addq $0x20, %r15
andl $-0x2, %eax
cmpl $0x4, %eax
je 0x569e
cmpl $0x2, %eax
jne 0x563a
xorl %r13d, %r13d
movq %r14, %rdi
movl %ebx, %edx
callq 0x628a
testq %rax, %rax
je 0x56d3
movq %rax, %rdi
callq 0x2c35
movl %r13d, %eax
xorl %r13d, %r13d
testb %al, %al
jne 0x5523
leaq 0x1c13(%rip), %rsi      # 0x7254
movq %r12, %rdi
movl %ebx, %edx
callq 0x60e1
testq %rax, %rax
je 0x5690
pushq $0x1
popq %rsi
movq %rax, %rdi
callq 0x466e
testq %rax, %rax
je 0x5697
movq %rax, %r12
movq (%r15), %rdi
callq 0x51b9
movq %rax, %r13
pushq $0x2f
popq %rsi
movq %rax, %rdi
callq 0x2280
testq %rax, %rax
je 0x5720
movq %rax, %r15
movb $0x0, (%rax)
incq %r15
jmp 0x5723
pushq $0x7
jmp 0x5521
pushq $0x8
jmp 0x5521
movl %edi, -0x30(%rbp)
leaq 0x1bb2(%rip), %rsi      # 0x725a
movq %r12, %rdi
movl %ebx, %edx
callq 0x60e1
testq %rax, %rax
je 0x5719
movq 0x20(%rax), %rsi
movq %r14, %rdi
movl %ebx, %edx
testb %r13b, %r13b
je 0x5803
callq 0x628a
jmp 0x5808
pushq $0xd
jmp 0x5521
pushq $0x4
popq %rax
movb $0x1, %r13b
jmp 0x5558
xorps %xmm0, %xmm0
movups %xmm0, 0x30(%rsp)
movups %xmm0, 0x20(%rsp)
movups %xmm0, 0x10(%rsp)
movups %xmm0, (%rsp)
movq %r14, %rdi
callq 0x6226
xorl %r13d, %r13d
jmp 0x5523
pushq $0x5
popq %rax
movb $0x1, %dil
xorl %r13d, %r13d
jmp 0x555a
pushq $0x4
jmp 0x5521
xorl %r15d, %r15d
movq %r14, %rdi
movq %r13, -0x30(%rbp)
movq %r13, %rsi
movl %ebx, %edx
callq 0x5255
movq %rax, %r14
movq %r15, %rdi
callq 0x6391
testq %r14, %r14
sete %al
testq %r15, %r15
sete %cl
orb %al, %cl
pushq $0x9
popq %r13
jne 0x585e
movq %r14, %rdi
callq 0x48cc
testl %eax, %eax
je 0x583d
cmpb $0x2d, (%r15)
jne 0x5778
cmpb $0x0, 0x1(%r15)
je 0x588d
leaq -0x38(%rbp), %rsi
andq $0x0, (%rsi)
movq %r15, %rdi
callq 0x6084
testl %eax, %eax
je 0x585a
movq -0x38(%rbp), %rdx
movq %r14, %rax
addq $0x10, %rax
movq %rax, %rcx
movq %rdx, %rsi
movq (%rcx), %rcx
addq $-0x1, %rdx
setb %dil
testq %rcx, %rcx
je 0x57b6
testb %dil, %dil
jne 0x579e
testq %rsi, %rsi
je 0x5868
pushq $0xa
jmp 0x585c
movq 0x20(%r15), %rsi
movq %r14, %rdi
movl %ebx, %edx
callq 0x5255
movq %rax, %r14
leaq 0x1a76(%rip), %rsi      # 0x7254
movq %r12, %rdi
movl %ebx, %edx
callq 0x60e1
movq %r14, %rdi
movq %rax, %rsi
movl %ebx, %edx
callq 0x60f5
xorl %r13d, %r13d
testl %eax, %eax
sete %r13b
jmp 0x5523
callq 0x5255
movq %rax, %r12
testq %rax, %rax
movl -0x30(%rbp), %eax
je 0x5836
testb %al, %al
je 0x5663
pushq $0x1
popq %rsi
movq %r12, %rdi
callq 0x466e
testq %rax, %rax
jne 0x5660
pushq $0x6
jmp 0x5521
pushq $0x5
jmp 0x5521
movq %r14, %rdi
callq 0x48e0
testl %eax, %eax
je 0x585e
movq %r14, %rdi
movq %r15, %rsi
testl %ebx, %ebx
je 0x589a
callq 0x41e6
jmp 0x589f
pushq $0xb
popq %r13
movq %r12, %rdi
callq 0x2c35
jmp 0x58b0
testq %rcx, %rcx
je 0x588d
movq %rcx, (%r12)
movq 0x8(%rcx), %rdx
movq %rdx, 0x8(%r12)
movq %r12, 0x8(%rcx)
cmpq (%rax), %rcx
je 0x5888
movq 0x8(%r12), %rax
movq %r12, (%rax)
jmp 0x58ad
movq %r14, %rdi
movq %r12, %rsi
callq 0x3ab2
jmp 0x58ad
callq 0x41d4
movq %r14, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x3afb
xorl %r13d, %r13d
movq -0x30(%rbp), %rdi
testq %rdi, %rdi
je 0x5523
callq 0x4ac6
jmp 0x5523