// File: /workspace/github_c_makefile_star2_O1/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 $0x68, %rsp
movl %edx, %ebx
movq %rsi, %r13
movq %rdi, %r15
leaq 0x24ff(%rip), %rsi      # 0x8257
movq %r13, %rdi
testl %edx, %edx
je 0x5d66
callq 0x3b82
jmp 0x5d6b
callq 0x3a86
movq %rax, %r12
movq %rax, %rdi
callq 0x2c15
testl %eax, %eax
je 0x5d8f
leaq 0x24e8(%rip), %rsi      # 0x8269
movq %r13, %rdi
testl %ebx, %ebx
je 0x5d9a
callq 0x3b82
jmp 0x5d9f
movl $0x2, %r14d
jmp 0x5f18
callq 0x3a86
movq %rax, %r14
movq %rax, %rdi
callq 0x2c15
testl %eax, %eax
movq %r15, -0x38(%rbp)
je 0x5e38
movq 0x20(%r14), %r14
leaq 0x24ab(%rip), %rsi      # 0x826c
movq %r14, %rdi
callq 0x2210
testl %eax, %eax
je 0x5e3d
leaq 0x249c(%rip), %rsi      # 0x8270
movq %r14, %rdi
callq 0x2210
testl %eax, %eax
je 0x5e45
leaq 0x2490(%rip), %rsi      # 0x8277
movq %r14, %rdi
callq 0x2210
testl %eax, %eax
je 0x5e4d
leaq 0x2478(%rip), %rsi      # 0x8272
movq %r14, %rdi
callq 0x2210
testl %eax, %eax
je 0x5e55
leaq 0x2472(%rip), %rsi      # 0x827f
movq %r14, %rdi
callq 0x2210
testl %eax, %eax
je 0x5e5d
leaq 0x2464(%rip), %rsi      # 0x8284
movq %r14, %rdi
callq 0x2210
xorl %ecx, %ecx
testl %eax, %eax
movl $0x6, %r15d
cmovnel %ecx, %r15d
jmp 0x5e63
xorl %r15d, %r15d
jmp 0x5e63
movl $0x1, %r15d
jmp 0x5e63
movl $0x2, %r15d
jmp 0x5e63
movl $0x3, %r15d
jmp 0x5e63
movl $0x4, %r15d
jmp 0x5e63
movl $0x5, %r15d
testl %r15d, %r15d
je 0x5e96
cmpl $0x6, %r15d
jne 0x5e9e
movq 0x20(%r12), %rsi
movq -0x38(%rbp), %rdi
movl %ebx, %edx
callq 0x5b39
movq %rax, %r14
leaq 0x23d4(%rip), %rsi      # 0x825c
movq %r13, %rdi
testl %ebx, %ebx
je 0x5efd
callq 0x3b82
jmp 0x5f02
movl $0x3, %r14d
jmp 0x5f18
movq 0x20(%r12), %rsi
cmpb $0x0, (%rsi)
je 0x5f4a
cmpl $0x2, %r15d
setne %al
movl %r15d, %r14d
andl $-0x2, %r14d
movq $0x0, -0x30(%rbp)
cmpl $0x2, %r14d
jne 0x6023
movq %rax, -0x40(%rbp)
movq -0x38(%rbp), %rdi
movl %ebx, %edx
callq 0x6daa
movq $0x0, -0x30(%rbp)
testq %rax, %rax
je 0x6014
movq %rax, %rdi
callq 0x2cdd
movq -0x40(%rbp), %rax
jmp 0x601f
callq 0x3a86
movq %r14, %rdi
movq %rax, %rsi
movl %ebx, %edx
callq 0x6c06
xorl %r14d, %r14d
testl %eax, %eax
sete %r14b
xorl %r13d, %r13d
xorl %r12d, %r12d
testq %r13, %r13
je 0x5f2b
movq %r13, %rdi
callq 0x2cdd
testq %r12, %r12
je 0x5f38
movq %r12, %rdi
callq 0x5174
movl %r14d, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpl $0x2, %r15d
jne 0x5f7a
xorps %xmm0, %xmm0
movups %xmm0, 0x30(%rsp)
movups %xmm0, 0x20(%rsp)
movups %xmm0, 0x10(%rsp)
movups %xmm0, (%rsp)
movq -0x38(%rbp), %rdi
callq 0x6d46
xorl %r13d, %r13d
xorl %r12d, %r12d
xorl %r14d, %r14d
jmp 0x5f1e
movl %r15d, %eax
andl $-0x3, %eax
cmpl $0x1, %eax
jne 0x5eac
leaq 0x22cc(%rip), %rsi      # 0x825c
movq %r13, %rdi
movl %ebx, %edx
callq 0x6bf2
testq %rax, %rax
je 0x60c3
movq %rax, %rdi
movl $0x1, %esi
callq 0x4c70
xorl %r14d, %r14d
testq %rax, %rax
je 0x60b8
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 -0x38(%rbp), %r15
movq %r15, %rdi
callq 0x6d46
movq %rbx, %rdi
callq 0x5174
movq 0x38(%r15), %rdi
testq %rdi, %rdi
je 0x5f18
callq 0x5174
movq $0x0, 0x38(%r15)
jmp 0x5f6f
movl $0xd, %eax
movq %rax, -0x30(%rbp)
xorl %eax, %eax
testb %al, %al
je 0x605b
cmpl $0x4, %r14d
jne 0x6042
testl %ebx, %ebx
movq -0x30(%rbp), %r14
leaq 0x222c(%rip), %rsi      # 0x8262
movq %r13, %rdi
je 0x606a
callq 0x3b82
jmp 0x606f
testl %ebx, %ebx
movq -0x30(%rbp), %r14
leaq 0x220d(%rip), %rsi      # 0x825c
movq %r13, %rdi
je 0x6095
callq 0x3b82
jmp 0x609a
xorl %r13d, %r13d
xorl %r12d, %r12d
movq -0x30(%rbp), %r14
jmp 0x5f1e
callq 0x3a86
testq %rax, %rax
je 0x60ce
movq %r14, -0x30(%rbp)
cmpl $0x4, %r15d
jne 0x60d6
movq %rax, %r14
movq 0x20(%rax), %rsi
movq -0x38(%rbp), %rdi
movl %ebx, %edx
callq 0x6daa
movq %rax, %r13
jmp 0x60dc
callq 0x3a86
testq %rax, %rax
je 0x60c3
movq %rax, %rdi
movl $0x1, %esi
callq 0x4c70
movq %rax, %r13
testq %rax, %rax
jne 0x6139
movl $0x8, %r14d
jmp 0x5f18
movl $0x7, %r14d
jmp 0x5f18
movl $0x4, %r14d
jmp 0x612c
movq %rax, %r14
xorl %r13d, %r13d
cmpl $0x5, %r15d
jne 0x60f4
movq 0x20(%r14), %rsi
movq -0x38(%rbp), %rdi
movl %ebx, %edx
callq 0x5b39
movq %rax, %r13
testq %r13, %r13
je 0x6126
cmpl $0x5, %r15d
jne 0x610f
movq %r13, %rdi
movl $0x1, %esi
callq 0x4c70
movq %rax, %r13
movq -0x30(%rbp), %rdx
testq %r13, %r13
setne %al
movl $0x6, %ecx
cmovnel %edx, %ecx
movl %ecx, %r14d
jmp 0x6131
movl $0x5, %r14d
xorl %r13d, %r13d
xorl %eax, %eax
testb %al, %al
je 0x5f1b
movq 0x20(%r12), %rdi
callq 0x5af5
movq %rax, %r12
movq %rax, %rdi
movl $0x2f, %esi
callq 0x2270
testq %rax, %rax
je 0x6163
movb $0x0, (%rax)
incq %rax
movq %rax, %r15
jmp 0x6166
xorl %r15d, %r15d
movq -0x38(%rbp), %rdi
movq %r12, %rsi
movl %ebx, %edx
callq 0x5b39
testq %r15, %r15
je 0x61b2
movq %r15, %rcx
movq %r15, %rdi
movzbl (%rcx), %edx
cmpl $0x7e, %edx
je 0x618d
testl %edx, %edx
jne 0x61a7
jmp 0x61af
movzbl 0x1(%rcx), %edx
incq %rcx
cmpl $0x31, %edx
je 0x61a3
cmpl $0x30, %edx
jne 0x61b2
movb $0x7e, (%rdi)
jmp 0x61a7
movb $0x2f, 0x1(%rdi)
incq %rdi
incq %rcx
jmp 0x617f
movb $0x0, (%rdi)
testq %r15, %r15
sete %dl
testq %rax, %rax
sete %cl
orb %dl, %cl
je 0x61cd
movl $0x9, %r14d
jmp 0x5f1e
movq %r15, -0x38(%rbp)
movq %rax, %r15
movq %rax, %rdi
callq 0x4f5d
testl %eax, %eax
je 0x6233
leaq 0x2080(%rip), %rsi      # 0x8267
movq -0x38(%rbp), %rbx
movq %rbx, %rdi
callq 0x2210
testl %eax, %eax
je 0x6260
leaq -0x48(%rbp), %rsi
movq $0x0, (%rsi)
movq %rbx, %rdi
callq 0x6b83
testl %eax, %eax
je 0x626d
movq -0x48(%rbp), %rsi
movq %r15, %rdi
movq %r13, %rdx
callq 0x6ef4
xorl %ecx, %ecx
testl %eax, %eax
cmovneq %rcx, %r13
movl $0xa, %eax
cmovel %eax, %r14d
jmp 0x5f1e
movq %r15, -0x30(%rbp)
movq %r15, %rdi
callq 0x4f71
testl %eax, %eax
je 0x61c2
testl %ebx, %ebx
je 0x6278
movq -0x30(%rbp), %r15
movq %r15, %rdi
movq -0x38(%rbp), %rbx
movq %rbx, %rsi
callq 0x4574
jmp 0x628b
movq %r15, %rdi
movq %r13, %rsi
callq 0x3ba8
jmp 0x6299
movl $0xb, %r14d
jmp 0x5f1e
movq -0x30(%rbp), %r15
movq %r15, %rdi
movq -0x38(%rbp), %rbx
movq %rbx, %rsi
callq 0x454b
movq %r15, %rdi
movq %rbx, %rsi
movq %r13, %rdx
callq 0x3bde
xorl %r13d, %r13d
jmp 0x5f1e