// File: /workspace/github_c_makefile_star2_O3/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 0x2659(%rip), %rsi      # 0x824f
movq %r12, %rdi
testl %edx, %edx
je 0x5c26
callq 0x3af1
movq %rax, %r15
movq %rax, %rdi
callq 0x2c19
testl %eax, %eax
je 0x5d21
leaq 0x2645(%rip), %rsi      # 0x8261
movq %r12, %rdi
callq 0x3af1
jmp 0x5c4d
callq 0x39e6
movq %rax, %r15
movq %rax, %rdi
callq 0x2c19
testl %eax, %eax
je 0x5d21
leaq 0x261c(%rip), %rsi      # 0x8261
movq %r12, %rdi
callq 0x39e6
movq %rax, %r13
movq %rax, %rdi
callq 0x2c19
testl %eax, %eax
je 0x5d2c
movq 0x20(%r13), %r13
leaq 0x25f9(%rip), %rsi      # 0x8264
movq %r13, %rdi
callq 0x2210
testl %eax, %eax
je 0x5d37
leaq 0x25e6(%rip), %rsi      # 0x8268
movq %r13, %rdi
callq 0x2210
testl %eax, %eax
je 0x5d43
leaq 0x25d6(%rip), %rsi      # 0x826f
movq %r13, %rdi
callq 0x2210
testl %eax, %eax
je 0x5d60
leaq 0x25ba(%rip), %rsi      # 0x826a
movq %r13, %rdi
callq 0x2210
testl %eax, %eax
je 0x5e02
leaq 0x25b0(%rip), %rsi      # 0x8277
movq %r13, %rdi
callq 0x2210
testl %eax, %eax
je 0x5e34
leaq 0x259e(%rip), %rsi      # 0x827c
movq %r13, %rdi
callq 0x2210
movl $0x3, %r13d
testl %eax, %eax
jne 0x611f
movq 0x20(%r15), %rsi
movq %r14, %rdi
movl %ebx, %edx
callq 0x59db
movq %rax, %r14
leaq 0x2548(%rip), %rsi      # 0x8254
movq %r12, %rdi
testl %ebx, %ebx
je 0x60bf
callq 0x3af1
jmp 0x60c4
movl $0x2, %r13d
jmp 0x611f
movl $0x3, %r13d
jmp 0x611f
movq %r14, -0x30(%rbp)
movl $0x1, %r14d
jmp 0x5d6a
movq 0x20(%r15), %rsi
cmpb $0x0, (%rsi)
je 0x5e0e
movq %r14, -0x30(%rbp)
addq $0x20, %r15
movb $0x1, %r14b
jmp 0x5e59
movq %r14, -0x30(%rbp)
movl $0x3, %r14d
movq 0x20(%r15), %rsi
cmpb $0x0, (%rsi)
jne 0x5e42
leaq 0x24d6(%rip), %rsi      # 0x8254
movq %r12, %rdi
movl %ebx, %edx
callq 0x6a74
testq %rax, %rax
je 0x5f66
movq %rax, %rdi
movl $0x1, %esi
callq 0x4b97
testq %rax, %rax
je 0x5f71
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 -0x30(%rbp), %r14
movq %r14, %rdi
callq 0x6bc8
movq %rbx, %rdi
callq 0x509f
movq 0x38(%r14), %rdi
xorl %r13d, %r13d
testq %rdi, %rdi
je 0x611f
callq 0x509f
movq $0x0, 0x38(%r14)
jmp 0x611f
movq %r14, -0x30(%rbp)
movl $0x4, %r14d
jmp 0x5e3e
xorps %xmm0, %xmm0
movups %xmm0, 0x30(%rsp)
movups %xmm0, 0x20(%rsp)
movups %xmm0, 0x10(%rsp)
movups %xmm0, (%rsp)
movq %r14, %rdi
callq 0x6bc8
xorl %r13d, %r13d
jmp 0x611f
movq %r14, -0x30(%rbp)
movl $0x5, %r14d
movq 0x20(%r15), %rsi
addq $0x20, %r15
movl %r14d, %eax
andl $-0x2, %eax
cmpl $0x4, %eax
je 0x5e82
cmpl $0x2, %eax
jne 0x5e9e
xorl %r14d, %r14d
movq -0x30(%rbp), %rdi
movl %ebx, %edx
callq 0x6c2c
testq %rax, %rax
je 0x5eb7
movq %rax, %rdi
callq 0x2ce3
xorl %r13d, %r13d
testb %r14b, %r14b
movq -0x30(%rbp), %r14
je 0x5ea2
jmp 0x611f
leaq 0x23d1(%rip), %rsi      # 0x825a
movq %r12, %rdi
testl %ebx, %ebx
je 0x5f7c
callq 0x3af1
jmp 0x5f81
movq -0x30(%rbp), %r14
leaq 0x23ab(%rip), %rsi      # 0x8254
movq %r12, %rdi
testl %ebx, %ebx
je 0x5ec2
callq 0x3af1
jmp 0x5ec7
movl $0xd, %r13d
jmp 0x611f
callq 0x39e6
testq %rax, %rax
je 0x5f66
movq %rax, %rdi
movl $0x1, %esi
callq 0x4b97
testq %rax, %rax
je 0x5f71
movq %rax, -0x38(%rbp)
movq (%r15), %rdi
callq 0x5999
movq %rax, %r15
movq %rax, %rdi
movl $0x2f, %esi
callq 0x2270
testq %rax, %rax
je 0x603a
movq %rax, %r12
movb $0x0, (%rax)
leaq 0x1(%rax), %r13
movq %r14, %rdi
movq %r15, %rsi
movl %ebx, %edx
callq 0x59db
movq %rax, %r14
movq %r12, %rax
addq $0x2, %rax
movq %r13, -0x30(%rbp)
movq %r13, %rcx
movzbl (%rcx), %edx
cmpl $0x7e, %edx
je 0x5f44
testl %edx, %edx
jne 0x5f5e
jmp 0x5fcb
movzbl 0x1(%rcx), %edx
incq %rcx
cmpl $0x31, %edx
je 0x5f5b
cmpl $0x30, %edx
jne 0x5fcf
movb $0x7e, -0x1(%rax)
jmp 0x5f5e
movb $0x2f, (%rax)
incq %rcx
incq %rax
jmp 0x5f33
movl $0x7, %r13d
jmp 0x611f
movl $0x8, %r13d
jmp 0x611f
callq 0x39e6
testq %rax, %rax
je 0x5fc0
movl $0x5, %r13d
cmpl $0x5, %r14d
je 0x605e
cmpl $0x4, %r14d
jne 0x611f
movq 0x20(%rax), %rsi
movq -0x30(%rbp), %r14
movq %r14, %rdi
movl %ebx, %edx
callq 0x6c2c
testq %rax, %rax
jne 0x5ee6
jmp 0x611f
movl $0x4, %r13d
jmp 0x611f
movb $0x0, -0x1(%rax)
movl $0x9, %r13d
testq %r14, %r14
je 0x604d
movq %r14, %rdi
callq 0x4e88
testl %eax, %eax
je 0x609a
movq -0x30(%rbp), %rdi
cmpb $0x2d, (%rdi)
jne 0x5fff
cmpb $0x0, 0x2(%r12)
je 0x60e7
leaq -0x40(%rbp), %rsi
movq $0x0, (%rsi)
callq 0x6a1a
testl %eax, %eax
je 0x60dc
movq -0x40(%rbp), %rsi
movq %r14, %rdi
movq -0x38(%rbp), %r12
movq %r12, %rdx
callq 0x6d72
testl %eax, %eax
jne 0x610f
movl $0xa, %r13d
jmp 0x6051
movq %r14, %rdi
movq %r15, %rsi
movl %ebx, %edx
callq 0x59db
movl $0x9, %r13d
movq -0x38(%rbp), %r12
movq %r12, %rdi
callq 0x2ce3
jmp 0x6112
movq 0x20(%rax), %rsi
movq -0x30(%rbp), %r14
movq %r14, %rdi
movl %ebx, %edx
callq 0x59db
testq %rax, %rax
je 0x611f
movq %rax, %rdi
movl $0x1, %esi
callq 0x4b97
testq %rax, %rax
jne 0x5ee6
movl $0x6, %r13d
jmp 0x611f
movq %r14, %rdi
callq 0x4e9c
testl %eax, %eax
movq -0x38(%rbp), %r12
je 0x6051
movq %r14, %rdi
testl %ebx, %ebx
je 0x60f5
movq -0x30(%rbp), %rbx
movq %rbx, %rsi
callq 0x4539
jmp 0x6101
callq 0x39e6
movq %r14, %rdi
movq %rax, %rsi
movl %ebx, %edx
callq 0x6a88
xorl %r13d, %r13d
testl %eax, %eax
sete %r13b
jmp 0x611f
movl $0xb, %r13d
jmp 0x604d
movq %r14, %rdi
movq -0x38(%rbp), %rsi
callq 0x3b17
jmp 0x610f
movq -0x30(%rbp), %rbx
movq %rbx, %rsi
callq 0x4527
movq %r14, %rdi
movq %rbx, %rsi
movq %r12, %rdx
callq 0x3b4d
xorl %r13d, %r13d
testq %r15, %r15
je 0x611f
movq %r15, %rdi
callq 0x509f
movl %r13d, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq