// File: /workspace/github_c_makefile_star2_O0/tomcatzh[P]RealityBenchmark/external/cJSON.c
// Function: utf16_literal_to_utf8
static unsigned char utf16_literal_to_utf8(const unsigned char * const input_pointer, const unsigned char * const input_end, unsigned char **output_pointer)
{
    long unsigned int codepoint = 0;
    unsigned int first_code = 0;
    const unsigned char *first_sequence = input_pointer;
    unsigned char utf8_length = 0;
    unsigned char utf8_position = 0;
    unsigned char sequence_length = 0;
    unsigned char first_byte_mark = 0;

    if ((input_end - first_sequence) < 6)
    {
        /* input ends unexpectedly */
        goto fail;
    }

    /* get the first utf16 sequence */
    first_code = parse_hex4(first_sequence + 2);

    /* check that the code is valid */
    if (((first_code >= 0xDC00) && (first_code <= 0xDFFF)))
    {
        goto fail;
    }

    /* UTF16 surrogate pair */
    if ((first_code >= 0xD800) && (first_code <= 0xDBFF))
    {
        const unsigned char *second_sequence = first_sequence + 6;
        unsigned int second_code = 0;
        sequence_length = 12; /* \uXXXX\uXXXX */

        if ((input_end - second_sequence) < 6)
        {
            /* input ends unexpectedly */
            goto fail;
        }

        if ((second_sequence[0] != '\\') || (second_sequence[1] != 'u'))
        {
            /* missing second half of the surrogate pair */
            goto fail;
        }

        /* get the second utf16 sequence */
        second_code = parse_hex4(second_sequence + 2);
        /* check that the code is valid */
        if ((second_code < 0xDC00) || (second_code > 0xDFFF))
        {
            /* invalid second half of the surrogate pair */
            goto fail;
        }


        /* calculate the unicode codepoint from the surrogate pair */
        codepoint = 0x10000 + (((first_code & 0x3FF) << 10) | (second_code & 0x3FF));
    }
    else
    {
        sequence_length = 6; /* \uXXXX */
        codepoint = first_code;
    }

    /* encode as UTF-8
     * takes at maximum 4 bytes to encode:
     * 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx */
    if (codepoint < 0x80)
    {
        /* normal ascii, encoding 0xxxxxxx */
        utf8_length = 1;
    }
    else if (codepoint < 0x800)
    {
        /* two bytes, encoding 110xxxxx 10xxxxxx */
        utf8_length = 2;
        first_byte_mark = 0xC0; /* 11000000 */
    }
    else if (codepoint < 0x10000)
    {
        /* three bytes, encoding 1110xxxx 10xxxxxx 10xxxxxx */
        utf8_length = 3;
        first_byte_mark = 0xE0; /* 11100000 */
    }
    else if (codepoint <= 0x10FFFF)
    {
        /* four bytes, encoding 1110xxxx 10xxxxxx 10xxxxxx 10xxxxxx */
        utf8_length = 4;
        first_byte_mark = 0xF0; /* 11110000 */
    }
    else
    {
        /* invalid unicode codepoint */
        goto fail;
    }

    /* encode as utf8 */
    for (utf8_position = (unsigned char)(utf8_length - 1); utf8_position > 0; utf8_position--)
    {
        /* 10xxxxxx */
        (*output_pointer)[utf8_position] = (unsigned char)((codepoint | 0x80) & 0xBF);
        codepoint >>= 6;
    }
    /* encode first byte */
    if (utf8_length > 1)
    {
        (*output_pointer)[0] = (unsigned char)((codepoint | first_byte_mark) & 0xFF);
    }
    else
    {
        (*output_pointer)[0] = (unsigned char)(codepoint & 0x7F);
    }

    *output_pointer += utf8_length;

    return sequence_length;

fail:
    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq $0x0, -0x28(%rbp)
movl $0x0, -0x2c(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x38(%rbp)
movb $0x0, -0x39(%rbp)
movb $0x0, -0x3a(%rbp)
movb $0x0, -0x3b(%rbp)
movb $0x0, -0x3c(%rbp)
movq -0x18(%rbp), %rax
movq -0x38(%rbp), %rcx
subq %rcx, %rax
cmpq $0x6, %rax
jge 0xa3e1
jmp 0xa5b8
movq -0x38(%rbp), %rdi
addq $0x2, %rdi
callq 0xa5d0
movl %eax, -0x2c(%rbp)
cmpl $0xdc00, -0x2c(%rbp)    # imm = 0xDC00
jb 0xa408
cmpl $0xdfff, -0x2c(%rbp)    # imm = 0xDFFF
ja 0xa408
jmp 0xa5b8
cmpl $0xd800, -0x2c(%rbp)    # imm = 0xD800
jb 0xa4b7
cmpl $0xdbff, -0x2c(%rbp)    # imm = 0xDBFF
ja 0xa4b7
movq -0x38(%rbp), %rax
addq $0x6, %rax
movq %rax, -0x48(%rbp)
movl $0x0, -0x4c(%rbp)
movb $0xc, -0x3b(%rbp)
movq -0x18(%rbp), %rax
movq -0x48(%rbp), %rcx
subq %rcx, %rax
cmpq $0x6, %rax
jge 0xa44f
jmp 0xa5b8
movq -0x48(%rbp), %rax
movzbl (%rax), %eax
cmpl $0x5c, %eax
jne 0xa468
movq -0x48(%rbp), %rax
movzbl 0x1(%rax), %eax
cmpl $0x75, %eax
je 0xa46d
jmp 0xa5b8
movq -0x48(%rbp), %rdi
addq $0x2, %rdi
callq 0xa5d0
movl %eax, -0x4c(%rbp)
cmpl $0xdc00, -0x4c(%rbp)    # imm = 0xDC00
jb 0xa48f
cmpl $0xdfff, -0x4c(%rbp)    # imm = 0xDFFF
jbe 0xa494
jmp 0xa5b8
movl -0x2c(%rbp), %eax
andl $0x3ff, %eax            # imm = 0x3FF
shll $0xa, %eax
movl -0x4c(%rbp), %ecx
andl $0x3ff, %ecx            # imm = 0x3FF
orl %ecx, %eax
addl $0x10000, %eax          # imm = 0x10000
movl %eax, %eax
movq %rax, -0x28(%rbp)
jmp 0xa4c2
movb $0x6, -0x3b(%rbp)
movl -0x2c(%rbp), %eax
movq %rax, -0x28(%rbp)
cmpq $0x80, -0x28(%rbp)
jae 0xa4d2
movb $0x1, -0x39(%rbp)
jmp 0xa519
cmpq $0x800, -0x28(%rbp)     # imm = 0x800
jae 0xa4e6
movb $0x2, -0x39(%rbp)
movb $-0x40, -0x3c(%rbp)
jmp 0xa517
cmpq $0x10000, -0x28(%rbp)   # imm = 0x10000
jae 0xa4fa
movb $0x3, -0x39(%rbp)
movb $-0x20, -0x3c(%rbp)
jmp 0xa515
cmpq $0x10ffff, -0x28(%rbp)  # imm = 0x10FFFF
ja 0xa50e
movb $0x4, -0x39(%rbp)
movb $-0x10, -0x3c(%rbp)
jmp 0xa513
jmp 0xa5b8
jmp 0xa515
jmp 0xa517
jmp 0xa519
movzbl -0x39(%rbp), %eax
subl $0x1, %eax
movb %al, -0x3a(%rbp)
movzbl -0x3a(%rbp), %eax
cmpl $0x0, %eax
jle 0xa562
movq -0x28(%rbp), %rax
orq $0x80, %rax
andq $0xbf, %rax
movb %al, %dl
movq -0x20(%rbp), %rax
movq (%rax), %rax
movzbl -0x3a(%rbp), %ecx
movb %dl, (%rax,%rcx)
movq -0x28(%rbp), %rax
shrq $0x6, %rax
movq %rax, -0x28(%rbp)
movb -0x3a(%rbp), %al
addb $-0x1, %al
movb %al, -0x3a(%rbp)
jmp 0xa523
movzbl -0x39(%rbp), %eax
cmpl $0x1, %eax
jle 0xa589
movq -0x28(%rbp), %rax
movzbl -0x3c(%rbp), %ecx
orq %rcx, %rax
andq $0xff, %rax
movb %al, %cl
movq -0x20(%rbp), %rax
movq (%rax), %rax
movb %cl, (%rax)
jmp 0xa59c
movq -0x28(%rbp), %rax
andq $0x7f, %rax
movb %al, %cl
movq -0x20(%rbp), %rax
movq (%rax), %rax
movb %cl, (%rax)
movzbl -0x39(%rbp), %edx
movq -0x20(%rbp), %rax
movq (%rax), %rcx
movslq %edx, %rdx
addq %rdx, %rcx
movq %rcx, (%rax)
movb -0x3b(%rbp), %al
movb %al, -0x1(%rbp)
jmp 0xa5bc
movb $0x0, -0x1(%rbp)
movb -0x1(%rbp), %al
addq $0x50, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)