// File: /workspace/github_c_makefile_star2_O0/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
subq $0x140, %rsp            # imm = 0x140
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movq $0x0, -0x20(%rbp)
movq $0x0, -0x28(%rbp)
movq $0x0, -0x30(%rbp)
movl $0x0, -0x34(%rbp)
movq $0x0, -0x40(%rbp)
movq $0x0, -0x48(%rbp)
movl $0x0, -0x4c(%rbp)
movq -0x10(%rbp), %rdi
movl -0x14(%rbp), %edx
leaq 0x3ee5(%rip), %rsi      # 0xc33f
callq 0x98f0
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rdi
callq 0x30a0
cmpl $0x0, %eax
jne 0x847d
movl $0x2, -0x4c(%rbp)
jmp 0x8945
movq -0x10(%rbp), %rdi
movl -0x14(%rbp), %esi
callq 0x9940
movl %eax, -0x34(%rbp)
cmpl $0x0, -0x34(%rbp)
jne 0x849e
movl $0x3, -0x4c(%rbp)
jmp 0x8945
cmpl $0x6, -0x34(%rbp)
jne 0x84f9
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rax
movq 0x20(%rax), %rsi
movl -0x14(%rbp), %edx
callq 0x81d0
movq %rax, -0xf0(%rbp)
movq -0x10(%rbp), %rdi
movl -0x14(%rbp), %edx
leaq 0x3e77(%rip), %rsi      # 0xc344
callq 0x98f0
movq -0xf0(%rbp), %rdi
movq %rax, %rsi
movl -0x14(%rbp), %edx
callq 0x9a70
cmpl $0x0, %eax
setne %al
xorb $-0x1, %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, -0x4c(%rbp)
jmp 0x8945
jmp 0x84fb
movq -0x20(%rbp), %rax
movq 0x20(%rax), %rax
movsbl (%rax), %eax
cmpl $0x0, %eax
jne 0x8671
cmpl $0x2, -0x34(%rbp)
jne 0x8575
movq -0x8(%rbp), %rax
movq %rax, -0xf8(%rbp)
leaq -0x90(%rbp), %rdi
leaq 0x3e52(%rip), %rsi      # 0xc380
movl $0x40, %edx
callq 0x2040
movq -0xf8(%rbp), %rdi
movups -0x90(%rbp), %xmm0
movups -0x80(%rbp), %xmm1
movups -0x70(%rbp), %xmm2
movups -0x60(%rbp), %xmm3
movq %rsp, %rax
movups %xmm3, 0x30(%rax)
movups %xmm2, 0x20(%rax)
movups %xmm1, 0x10(%rax)
movups %xmm0, (%rax)
callq 0x9d00
movl $0x0, -0x4c(%rbp)
jmp 0x8945
cmpl $0x3, -0x34(%rbp)
je 0x8585
cmpl $0x1, -0x34(%rbp)
jne 0x866f
movq -0x10(%rbp), %rdi
movl -0x14(%rbp), %edx
leaq 0x3db1(%rip), %rsi      # 0xc344
callq 0x98f0
movq %rax, -0x28(%rbp)
cmpq $0x0, -0x28(%rbp)
jne 0x85af
movl $0x7, -0x4c(%rbp)
jmp 0x8945
movq -0x28(%rbp), %rdi
movl $0x1, %esi
callq 0x5770
movq %rax, -0x28(%rbp)
cmpq $0x0, -0x28(%rbp)
jne 0x85d4
movl $0x8, -0x4c(%rbp)
jmp 0x8945
movq -0x8(%rbp), %rax
movq %rax, -0x100(%rbp)
movq -0x28(%rbp), %rsi
leaq -0xd0(%rbp), %rdi
movl $0x40, %edx
callq 0x2040
movq -0x100(%rbp), %rdi
movups -0xd0(%rbp), %xmm0
movups -0xc0(%rbp), %xmm1
movups -0xb0(%rbp), %xmm2
movups -0xa0(%rbp), %xmm3
movq %rsp, %rax
movups %xmm3, 0x30(%rax)
movups %xmm2, 0x20(%rax)
movups %xmm1, 0x10(%rax)
movups %xmm0, (%rax)
callq 0x9d00
movq -0x28(%rbp), %rdi
callq 0x6220
movq $0x0, -0x28(%rbp)
movq -0x8(%rbp), %rax
cmpq $0x0, 0x38(%rax)
je 0x8663
movq -0x8(%rbp), %rax
movq 0x38(%rax), %rdi
callq 0x6220
movq -0x8(%rbp), %rax
movq $0x0, 0x38(%rax)
movl $0x0, -0x4c(%rbp)
jmp 0x8945
jmp 0x8671
cmpl $0x2, -0x34(%rbp)
je 0x867d
cmpl $0x3, -0x34(%rbp)
jne 0x86ce
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rax
movq 0x20(%rax), %rsi
movl -0x14(%rbp), %edx
callq 0x9d80
movq %rax, -0xd8(%rbp)
cmpq $0x0, -0xd8(%rbp)
jne 0x86ae
movl $0xd, -0x4c(%rbp)
jmp 0x8945
movq -0xd8(%rbp), %rdi
callq 0x31f0
cmpl $0x2, -0x34(%rbp)
jne 0x86cc
movl $0x0, -0x4c(%rbp)
jmp 0x8945
jmp 0x86ce
cmpl $0x4, -0x34(%rbp)
je 0x86de
cmpl $0x5, -0x34(%rbp)
jne 0x8790
movq -0x10(%rbp), %rdi
movl -0x14(%rbp), %edx
leaq 0x3c5e(%rip), %rsi      # 0xc34a
callq 0x98f0
movq %rax, -0xe0(%rbp)
cmpq $0x0, -0xe0(%rbp)
jne 0x870e
movl $0x4, -0x4c(%rbp)
jmp 0x8945
cmpl $0x4, -0x34(%rbp)
jne 0x872f
movq -0x8(%rbp), %rdi
movq -0xe0(%rbp), %rax
movq 0x20(%rax), %rsi
movl -0x14(%rbp), %edx
callq 0x9d80
movq %rax, -0x28(%rbp)
cmpl $0x5, -0x34(%rbp)
jne 0x8750
movq -0x8(%rbp), %rdi
movq -0xe0(%rbp), %rax
movq 0x20(%rax), %rsi
movl -0x14(%rbp), %edx
callq 0x81d0
movq %rax, -0x28(%rbp)
cmpq $0x0, -0x28(%rbp)
jne 0x8763
movl $0x5, -0x4c(%rbp)
jmp 0x8945
cmpl $0x5, -0x34(%rbp)
jne 0x877b
movq -0x28(%rbp), %rdi
movl $0x1, %esi
callq 0x5770
movq %rax, -0x28(%rbp)
cmpq $0x0, -0x28(%rbp)
jne 0x878e
movl $0x6, -0x4c(%rbp)
jmp 0x8945
jmp 0x87e1
movq -0x10(%rbp), %rdi
movl -0x14(%rbp), %edx
leaq 0x3ba6(%rip), %rsi      # 0xc344
callq 0x98f0
movq %rax, -0x28(%rbp)
cmpq $0x0, -0x28(%rbp)
jne 0x87ba
movl $0x7, -0x4c(%rbp)
jmp 0x8945
movq -0x28(%rbp), %rdi
movl $0x1, %esi
callq 0x5770
movq %rax, -0x28(%rbp)
cmpq $0x0, -0x28(%rbp)
jne 0x87df
movl $0x8, -0x4c(%rbp)
jmp 0x8945
jmp 0x87e1
movq -0x20(%rbp), %rax
movq 0x20(%rax), %rdi
callq 0x8020
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rdi
movl $0x2f, %esi
callq 0x22b0
movq %rax, -0x48(%rbp)
cmpq $0x0, -0x48(%rbp)
je 0x881e
movq -0x48(%rbp), %rax
movb $0x0, (%rax)
movq -0x48(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x48(%rbp)
movq -0x8(%rbp), %rdi
movq -0x40(%rbp), %rsi
movl -0x14(%rbp), %edx
callq 0x81d0
movq %rax, -0x30(%rbp)
movq -0x48(%rbp), %rdi
callq 0x9ea0
cmpq $0x0, -0x30(%rbp)
je 0x8849
cmpq $0x0, -0x48(%rbp)
jne 0x8855
movl $0x9, -0x4c(%rbp)
jmp 0x8945
movq -0x30(%rbp), %rdi
callq 0x5dc0
cmpl $0x0, %eax
je 0x88eb
movq -0x48(%rbp), %rdi
leaq 0x3add(%rip), %rsi      # 0xc34f
callq 0x2250
cmpl $0x0, %eax
jne 0x8893
movq -0x30(%rbp), %rdi
movq -0x28(%rbp), %rsi
callq 0x4220
movq $0x0, -0x28(%rbp)
jmp 0x88e9
movq $0x0, -0xe8(%rbp)
movq -0x48(%rbp), %rdi
leaq -0xe8(%rbp), %rsi
callq 0x95f0
cmpl $0x0, %eax
jne 0x88bf
movl $0xb, -0x4c(%rbp)
jmp 0x8945
movq -0x30(%rbp), %rdi
movq -0xe8(%rbp), %rsi
movq -0x28(%rbp), %rdx
callq 0x9f40
cmpl $0x0, %eax
jne 0x88e1
movl $0xa, -0x4c(%rbp)
jmp 0x8945
movq $0x0, -0x28(%rbp)
jmp 0x8941
movq -0x30(%rbp), %rdi
callq 0x5e00
cmpl $0x0, %eax
je 0x8936
cmpl $0x0, -0x14(%rbp)
je 0x890e
movq -0x30(%rbp), %rdi
movq -0x48(%rbp), %rsi
callq 0x4e60
jmp 0x891b
movq -0x30(%rbp), %rdi
movq -0x48(%rbp), %rsi
callq 0x4e30
movq -0x30(%rbp), %rdi
movq -0x48(%rbp), %rsi
movq -0x28(%rbp), %rdx
callq 0x42e0
movq $0x0, -0x28(%rbp)
jmp 0x893f
movl $0x9, -0x4c(%rbp)
jmp 0x8945
jmp 0x8941
jmp 0x8943
jmp 0x8945
cmpq $0x0, -0x28(%rbp)
je 0x8955
movq -0x28(%rbp), %rdi
callq 0x31f0
cmpq $0x0, -0x40(%rbp)
je 0x8965
movq -0x40(%rbp), %rdi
callq 0x6220
movl -0x4c(%rbp), %eax
addq $0x140, %rsp            # imm = 0x140
popq %rbp
retq
nopw %cs:(%rax,%rax)