// File: /workspace/github_c_makefile_star2_O1/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 %rbx
pushq %rax
movq %rsi, %rax
subq %rdi, %rax
cmpq $0x6, %rax
jge 0x79dd
xorl %ebp, %ebp
jmp 0x7b19
movq %rdx, %rbx
movq (%rdx), %r14
xorl %eax, %eax
movabsq $0x7e0000007e03ff, %rcx # imm = 0x7E0000007E03FF
leaq 0x2f9e(%rip), %rdx      # 0xa994
xorl %r15d, %r15d
movzbl 0x2(%rdi,%rax), %r8d
leal -0x30(%r8), %r9d
cmpb $0x36, %r9b
ja 0x7a29
movzbl %r9b, %r9d
btq %r9, %rcx
jae 0x7a29
shll $0x4, %r15d
addl (%rdx,%r9,4), %r15d
addl %r8d, %r15d
incq %rax
cmpq $0x4, %rax
jne 0x79f9
jmp 0x7a2c
xorl %r15d, %r15d
movl %r15d, %eax
andl $0xfffffc00, %eax       # imm = 0xFFFFFC00
xorl %ebp, %ebp
cmpl $0xdc00, %eax           # imm = 0xDC00
je 0x7b19
cmpl $0xd800, %eax           # imm = 0xD800
jne 0x7aa8
leaq 0x6(%rdi), %rax
subq %rax, %rsi
cmpq $0x6, %rsi
jl 0x7b19
cmpb $0x5c, (%rax)
jne 0x7b19
cmpb $0x75, 0x7(%rdi)
jne 0x7b19
addq $0x8, %rdi
callq 0x7b50
leal -0xe000(%rax), %ecx
cmpl $0xfffffc00, %ecx       # imm = 0xFFFFFC00
jb 0x7b19
shll $0xa, %r15d
andl $0xffc00, %r15d         # imm = 0xFFC00
andl $0x3ff, %eax            # imm = 0x3FF
addl %eax, %r15d
addl $0x10000, %r15d         # imm = 0x10000
movl $0xc, %ebp
jmp 0x7aad
movl $0x6, %ebp
movl %r15d, %eax
cmpl $0x80, %r15d
jb 0x7acc
cmpl $0x800, %eax            # imm = 0x800
jae 0x7b26
movl $0xc0, %edx
movl $0x2, %ecx
jmp 0x7ad3
movl $0x1, %ecx
xorl %edx, %edx
movq %rax, %rsi
cmpl $0x80, %eax
jb 0x7b01
leaq 0x1(%rcx), %rdi
movq %rax, %rsi
movl %esi, %r8d
andb $0x3f, %r8b
orb $-0x80, %r8b
movb %r8b, -0x2(%r14,%rdi)
shrq $0x6, %rsi
decq %rdi
cmpq $0x2, %rdi
jg 0x7ae4
movl %esi, %edi
andb $0x7f, %dil
orl %edx, %esi
cmpl $0x7f, %eax
movzbl %dil, %eax
cmoval %esi, %eax
movb %al, (%r14)
addq %rcx, (%rbx)
movl %ebp, %eax
addq $0x8, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
cmpl $0x10000, %eax          # imm = 0x10000
jae 0x7b39
movl $0xe0, %edx
movl $0x3, %ecx
jmp 0x7ad3
cmpl $0x10ffff, %eax         # imm = 0x10FFFF
ja 0x79d6
movl $0xf0, %edx
movl $0x4, %ecx
jmp 0x7ad3