// File: /workspace/github_c_makefile_star2_O1/canboat[P]canboat/analyzer/../common/utf.c
// Function: utf8_to_utf16
size_t utf8_to_utf16(utf8_t const *utf8, size_t utf8_len, utf16_t *utf16, size_t utf16_len)
{
  // The next codepoint that will be written in the UTF-16 string
  // or the size of the required buffer if utf16 is NULL
  size_t utf16_index = 0;

  for (size_t utf8_index = 0; utf8_index < utf8_len; utf8_index++)
  {
    codepoint_t codepoint = decode_utf8(utf8, utf8_len, &utf8_index);

    if (utf16 == NULL)
      utf16_index += calculate_utf16_len(codepoint);
    else
      utf16_index += encode_utf16(codepoint, utf16, utf16_len, utf16_index);
  }

  return utf16_index;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
movq %rcx, -0x10(%rsp)
testq %rsi, %rsi
je 0x27b1e
xorl %r11d, %r11d
leaq 0x18669(%rip), %r8      # 0x3ffa9
xorl %ebx, %ebx
movzbl (%rdi,%rbx), %r10d
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
xorl %r14d, %r14d
movq %r14, %r13
movl %eax, %r15d
movzbl (%r8,%r14,2), %ebp
movzbl 0x1(%r8,%r14,2), %r9d
incq %r14
movl %ebp, %r12d
andl %r10d, %r12d
cmpq $0x2, %r13
ja 0x27978
leal 0x1(%r15), %eax
cmpl %r9d, %r12d
jne 0x2794f
movl $0xfffd, %ecx           # imm = 0xFFFD
cmpl %r9d, %r12d
jne 0x27a9a
notl %ebp
andl %r10d, %ebp
cmpq $0x1, %r14
setne %r10b
je 0x27a31
leaq 0x1(%rbx), %r12
movb $0x1, %r10b
cmpq %rsi, %r12
jae 0x27a31
movzbl 0x1(%rdi,%rbx), %r13d
movl %r13d, %r9d
andl $-0x40, %r9d
movl %ebp, %eax
shll $0x6, %eax
andl $0x3f, %r13d
orl %eax, %r13d
cmpl $0x80, %r9d
cmovneq %rbx, %r12
cmovnel %ebp, %r13d
jne 0x27a2b
leaq -0x1(%r14), %rax
movq %rax, -0x8(%rsp)
addq $0x2, %rbx
movl $0x1, %r9d
movl %r9d, %r10d
addl $-0x1, %r15d
jae 0x27a22
cmpq %rsi, %rbx
jae 0x27a22
movzbl (%rdi,%rbx), %eax
movl %eax, %ebp
andl $-0x40, %ebp
movl %r13d, %r9d
shll $0x6, %r9d
andl $0x3f, %eax
orl %r9d, %eax
leal 0x1(%r10), %r9d
cmpl $0x80, %ebp
cmoveq %rbx, %r12
leaq 0x1(%rbx), %rbx
cmovel %eax, %r13d
je 0x279e6
cmpl -0x8(%rsp), %r10d
setb %r10b
movq %r12, %rbx
movl %r13d, %ebp
testb %r10b, %r10b
jne 0x27a9a
movl $0x1, %r9d
cmpl $0x80, %ebp
jb 0x27a62
movl $0x2, %r9d
cmpl $0x800, %ebp            # imm = 0x800
jb 0x27a62
cmpl $0x10000, %ebp          # imm = 0x10000
movl $0x4, %r9d
sbbl $0x0, %r9d
cmpl %r14d, %r9d
jne 0x27a9a
cmpl $0xffff, %ebp           # imm = 0xFFFF
setb %al
movl %ebp, %r9d
andl $0xf800, %r9d           # imm = 0xF800
cmpl $0xd800, %r9d           # imm = 0xD800
sete %r9b
testb %r9b, %al
jne 0x27a9a
cmpl $0x110000, %ebp         # imm = 0x110000
movl $0xfffd, %eax           # imm = 0xFFFD
cmovael %eax, %ebp
movl %ebp, %ecx
testq %rdx, %rdx
je 0x27aba
cmpq -0x10(%rsp), %r11
jae 0x27b08
cmpl $0xffff, %ecx           # imm = 0xFFFF
ja 0x27acb
movw %cx, (%rdx,%r11,2)
movl $0x1, %eax
jmp 0x27b0a
cmpl $0x10000, %ecx          # imm = 0x10000
movl $0x2, %eax
sbbq $0x0, %rax
jmp 0x27b0a
leaq 0x1(%r11), %rax
cmpq -0x10(%rsp), %rax
jae 0x27b08
leal 0xf0000(%rcx), %eax
movl $0x3ff, %r9d            # imm = 0x3FF
andl %r9d, %ecx
orl $0xdc00, %ecx           # imm = 0xDC00
shrl $0xa, %eax
andl %r9d, %eax
orl $0xd800, %eax           # imm = 0xD800
movw %ax, (%rdx,%r11,2)
movw %cx, 0x2(%rdx,%r11,2)
movl $0x2, %eax
jmp 0x27b0a
xorl %eax, %eax
addq %r11, %rax
incq %rbx
movq %rax, %r11
cmpq %rsi, %rbx
jb 0x27942
jmp 0x27b20
xorl %eax, %eax
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq