// File: /workspace/github_c_makefile_star2_O2/lengjingzju[P]json/json.c
// Function: utf16_literal_to_utf8
static int utf16_literal_to_utf8(const unsigned char *start_str, const unsigned char *finish_str, unsigned char **pptr)
{ /* copy from cJSON */
    const unsigned char *str = start_str;
    unsigned char *ptr = *pptr;

    int seq_len = 0;
    int i = 0;
    unsigned long uc = 0;
    unsigned int  uc1 = 0, uc2 = 0;
    unsigned char len = 0;
    unsigned char first_byte_mark = 0;

    /* converts a UTF-16 literal to UTF-8, A literal can be one or two sequences of the form \uXXXX */
    if ((finish_str - str) < 6)                             /* input ends unexpectedly */
        goto fail;
    str += 2;
    uc1 = _parse_hex4(str);                                 /* get the first utf16 sequence */
    if (((uc1 >= 0xDC00) && (uc1 <= 0xDFFF)))               /* check first_code is valid */
        goto fail;
    if ((uc1 >= 0xD800) && (uc1 <= 0xDBFF)) {               /* UTF16 surrogate pair */
        str += 4;
        seq_len = 12;                                       /* \uXXXX\uXXXX */
        if ((finish_str - str) < 6)                         /* input ends unexpectedly */
            goto fail;
        if ((str[0] != '\\') || (str[1] != 'u'))            /* missing second half of the surrogate pair */
            goto fail;
        str += 2;
        uc2 = _parse_hex4(str);                             /* get the second utf16 sequence */
        if ((uc2 < 0xDC00) || (uc2 > 0xDFFF))               /* check second_code is valid */
            goto fail;
        uc = 0x10000 + (((uc1 & 0x3FF) << 10) | (uc2 & 0x3FF)); /* calculate the unicode uc from the surrogate pair */
    } else {
        seq_len = 6;                                        /* \uXXXX */
        uc = uc1;
    }

    /* encode as UTF-8 takes at maximum 4 bytes to encode: 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx */
    if (uc < 0x80) {                                        /* normal ascii, encoding 0xxxxxxx */
        len = 1;
    } else if (uc < 0x800) {                                /* two bytes, encoding 110xxxxx 10xxxxxx */
        len = 2;
        first_byte_mark = 0xC0;                             /* 11000000 */
    } else if (uc < 0x10000) {                              /* three bytes, encoding 1110xxxx 10xxxxxx 10xxxxxx */
        len = 3;
        first_byte_mark = 0xE0;                             /* 11100000 */
    } else if (uc <= 0x10FFFF) {                            /* four bytes, encoding 1110xxxx 10xxxxxx 10xxxxxx 10xxxxxx */
        len = 4;
        first_byte_mark = 0xF0;                             /* 11110000 */
    }  else {                                               /* invalid unicode */
        goto fail;
    }
    for (i = len - 1; i > 0; --i) {                         /* encode as utf8 */
        ptr[i] = (unsigned char)((uc | 0x80) & 0xBF);       /* 10xxxxxx */
        uc >>= 6;
    }
    if (len > 1) {                                          /* encode first byte */
        ptr[0] = (unsigned char)((uc | first_byte_mark) & 0xFF);
    } else {
        ptr[0] = (unsigned char)(uc & 0x7F);
    }

    *pptr += len;
    return seq_len;

fail:
    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rsi, %rax
subq %rdi, %rax
xorl %ebp, %ebp
cmpq $0x6, %rax
jl 0x625d
movq %rdx, %rbx
movq %rsi, %r12
movq %rdi, %r15
movq (%rdx), %r13
addq $0x2, %rdi
callq 0x626e
movl %eax, %r14d
andl $0xfffffc00, %eax       # imm = 0xFFFFFC00
cmpl $0xdc00, %eax           # imm = 0xDC00
je 0x625d
cmpl $0xd800, %eax           # imm = 0xD800
jne 0x61cf
leaq 0x6(%r15), %rax
subq %rax, %r12
cmpq $0x6, %r12
jl 0x625d
cmpb $0x5c, (%rax)
jne 0x625d
cmpb $0x75, 0x7(%r15)
jne 0x625d
addq $0x8, %r15
movq %r15, %rdi
callq 0x626e
leal -0xe000(%rax), %ecx
cmpl $0xfffffc00, %ecx       # imm = 0xFFFFFC00
jb 0x625d
shll $0xa, %r14d
andl $0xffc00, %r14d         # imm = 0xFFC00
andl $0x3ff, %eax            # imm = 0x3FF
addl %eax, %r14d
addl $0x10000, %r14d         # imm = 0x10000
movl $0xf0, %esi
pushq $0x4
popq %rdx
pushq $0xc
popq %rax
movb $0x1, %cl
jmp 0x621d
pushq $0x6
popq %rax
cmpl $0x80, %r14d
jb 0x61ef
movb $0x1, %cl
cmpl $0x800, %r14d           # imm = 0x800
jae 0x61f8
movl $0xc0, %esi
pushq $0x2
jmp 0x621c
pushq $0x1
popq %rdx
xorl %ecx, %ecx
xorl %esi, %esi
jmp 0x621d
cmpl $0x10000, %r14d         # imm = 0x10000
jae 0x620a
movl $0xe0, %esi
pushq $0x3
jmp 0x621c
xorl %ebp, %ebp
cmpl $0x10ffff, %r14d        # imm = 0x10FFFF
ja 0x625d
movl $0xf0, %esi
pushq $0x4
popq %rdx
movl %r14d, %edi
movq %rdx, %r8
cmpq $0x2, %r8
jl 0x6242
movl %edi, %r9d
andb $0x3f, %r9b
orb $-0x80, %r9b
movb %r9b, -0x1(%r13,%r8)
decq %r8
shrq $0x6, %rdi
jmp 0x6223
movl %edi, %r8d
andb $0x7f, %r8b
orl %esi, %edi
movzbl %r8b, %esi
testb %cl, %cl
cmovnel %edi, %esi
movb %sil, (%r13)
addq %rdx, (%rbx)
movl %eax, %ebp
movl %ebp, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq