// File: /workspace/github_c_makefile_star2_O3/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:
movq %rsi, %rcx
subq %rdi, %rcx
xorl %eax, %eax
cmpq $0x6, %rcx
jl 0x7ab4
pushq %rbp
pushq %r14
pushq %rbx
movq (%rdx), %rcx
xorl %eax, %eax
movabsq $0x7e0000007e03ff, %r9  # imm = 0x7E0000007E03FF
leaq 0x3019(%rip), %r10      # 0xa994
xorl %r11d, %r11d
xorl %r8d, %r8d
movzbl 0x2(%rdi,%r11), %ebx
leal -0x30(%rbx), %ebp
cmpb $0x36, %bpl
ja 0x7a9b
movzbl %bpl, %r14d
btq %r14, %r9
jae 0x7a9b
shll $0x4, %r8d
addl %ebx, %r8d
addl (%r10,%r14,4), %r8d
incq %r11
cmpq $0x4, %r11
jne 0x7981
movl %r8d, %r11d
andl $0xfffffc00, %r11d      # imm = 0xFFFFFC00
xorl %eax, %eax
cmpl $0xdc00, %r11d          # imm = 0xDC00
je 0x7ab0
cmpl $0xd800, %r11d          # imm = 0xD800
jne 0x7a75
leaq 0x6(%rdi), %r11
subq %r11, %rsi
cmpq $0x6, %rsi
jl 0x7ab0
cmpb $0x5c, (%r11)
jne 0x7ab0
cmpb $0x75, 0x7(%rdi)
jne 0x7ab0
xorl %eax, %eax
xorl %r11d, %r11d
xorl %esi, %esi
movzbl 0x8(%rdi,%r11), %ebx
leal -0x30(%rbx), %ebp
cmpb $0x36, %bpl
ja 0x7ab0
movzbl %bpl, %r14d
btq %r14, %r9
jae 0x7ab0
shll $0x4, %esi
addl %ebx, %esi
addl (%r10,%r14,4), %esi
incq %r11
cmpq $0x4, %r11
jne 0x7a08
leal -0xe000(%rsi), %edi
cmpl $0xfffffc00, %edi       # imm = 0xFFFFFC00
jb 0x7ab0
shll $0xa, %r8d
andl $0xffc00, %r8d          # imm = 0xFFC00
andl $0x3ff, %esi            # imm = 0x3FF
addl %esi, %r8d
addl $0x10000, %r8d          # imm = 0x10000
movl $0xf0, %edi
movl $0x4, %eax
movl $0xc, %esi
jmp 0x7add
cmpl $0x80, %r8d
jb 0x7a98
movl $0x6, %esi
cmpl $0x800, %r8d            # imm = 0x800
jae 0x7ab5
movl $0xc0, %edi
movl $0x2, %eax
jmp 0x7add
movl %r8d, %eax
movl %eax, %r8d
movl $0x1, %eax
movl $0x6, %esi
movb %r8b, (%rcx)
addq %rax, (%rdx)
movl %esi, %eax
popq %rbx
popq %r14
popq %rbp
retq
cmpl $0x10000, %r8d          # imm = 0x10000
jae 0x7aca
movl $0xe0, %edi
movl $0x3, %eax
jmp 0x7add
cmpl $0x10ffff, %r8d         # imm = 0x10FFFF
ja 0x7ab0
movl $0xf0, %edi
movl $0x4, %eax
movl %r8d, %r8d
leaq 0x1(%rax), %r9
movl %r8d, %r10d
andb $0x3f, %r10b
orb $-0x80, %r10b
movb %r10b, -0x2(%rcx,%r9)
shrq $0x6, %r8
decq %r9
cmpq $0x2, %r9
ja 0x7ae4
orq %rdi, %r8
jmp 0x7aa8