// File: /workspace/github_cpp_makefile_star2_O1/4n6ist[P]usn_analytics/lib/utils.cpp
// Function: UTF16toUTF8[abi:cxx11](char16_t*, int)
string UTF16toUTF8(char16_t *s, int n) {
  int half = 0;
  char *out = (char*)malloc(6);
  string res;

  for (int i = 0; i < n; ++i) {
    uint16_t c = s[i];
    char *t = out;
    memset(t, 0, 6);
    if (half) {
      if ((c >= 0xdc00) && (c < 0xe000)) {
        *t++ = 0xf0 + (((half + 64) >> 8) & 7);
        *t++ = 0x80 + (((half + 64) >> 2) & 63);
        *t++ = 0x80 + ((c >> 6) & 15) + ((half & 3) << 4);
        *t++ = 0x80 + (c & 63);
        half = 0;
      } else {
        return "<Can't Convert>";
      }
    } else if (c < 0x80) {
      *t++ = c;
    } else {
      if (c < 0x800) {
        *t++ = (0xc0 | ((c >> 6) & 0x3f));
        *t++ = 0x80 | (c & 0x3f);
      } else if (c < 0xd800) {
        *t++ = 0xe0 | (c >> 12);
        *t++ = 0x80 | ((c >> 6) & 0x3f);
        *t++ = 0x80 | (c & 0x3f);
      } else if (c < 0xdc00)
        half = c;
      else if (c >= 0xe000) {
        *t++ = 0xe0 | (c >> 12);
        *t++ = 0x80 | ((c >> 6) & 0x3f);
        *t++ = 0x80 | (c & 0x3f);
      } else {
        return "<Can't Convert>";
      }
    }
    res += string(out);
  }
  free(out);
  return res;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movl %edx, %ebp
movq %rsi, %r14
movq %rdi, 0x8(%rsp)
movl $0x6, %edi
callq 0x3320
movq %rax, %r15
leaq 0x20(%rsp), %rax
movq %rax, -0x10(%rax)
movq $0x0, -0x8(%rax)
movb $0x0, (%rax)
testl %ebp, %ebp
jle 0xddb3
movq 0x8(%rsp), %rax
addq $0x10, %rax
movq %rax, 0x30(%rsp)
movl %ebp, %ebp
xorl %r12d, %r12d
xorl %r13d, %r13d
movzwl (%r14,%r12,2), %eax
movw $0x0, 0x4(%r15)
movl $0x0, (%r15)
testl %r13d, %r13d
je 0xdc8d
movl %eax, %ecx
andl $0xfc00, %ecx           # imm = 0xFC00
cmpl $0xdc00, %ecx           # imm = 0xDC00
jne 0xdc98
leal 0x40(%r13), %ecx
movl %ecx, %edx
shrl $0x8, %edx
andb $0x7, %dl
orb $-0x10, %dl
movb %dl, (%r15)
shrb $0x2, %cl
orb $-0x80, %cl
movb %cl, 0x1(%r15)
movl %eax, %ecx
shrl $0x6, %ecx
andl $0xf, %ecx
shll $0x4, %r13d
andl $0x30, %r13d
orl %ecx, %r13d
orb $-0x80, %r13b
movb %r13b, 0x2(%r15)
andb $0x3f, %al
orb $-0x80, %al
movb %al, 0x3(%r15)
jmp 0xdd04
cmpw $0x7f, %ax
ja 0xdcbf
movb %al, (%r15)
jmp 0xdd04
movq 0x8(%rsp), %rdi
movq 0x30(%rsp), %rax
movq %rax, (%rdi)
leaq 0x14f4(%rip), %rsi      # 0xf1a0
leaq 0x14fc(%rip), %rdx      # 0xf1af
callq 0x97be
xorl %ebx, %ebx
jmp 0xdd4b
cmpl $0x7ff, %eax            # imm = 0x7FF
ja 0xdcdb
movl %eax, %ecx
shrl $0x6, %ecx
orb $-0x40, %cl
movb %cl, (%r15)
andb $0x3f, %al
orb $-0x80, %al
movb %al, 0x1(%r15)
jmp 0xdd04
cmpl $0xd7ff, %eax           # imm = 0xD7FF
ja 0xdd61
movl %eax, %ecx
shrl $0xc, %ecx
orb $-0x20, %cl
movb %cl, (%r15)
movl %eax, %ecx
shrl $0x6, %ecx
andb $0x3f, %cl
orb $-0x80, %cl
movb %cl, 0x1(%r15)
andb $0x3f, %al
orb $-0x80, %al
movb %al, 0x2(%r15)
xorl %r13d, %r13d
leaq 0x38(%rsp), %rdi
movq %r15, %rsi
leaq 0x7(%rsp), %rdx
callq 0x8e42
movq 0x38(%rsp), %rsi
movq 0x40(%rsp), %rdx
leaq 0x10(%rsp), %rdi
callq 0x30b0
movq 0x38(%rsp), %rdi
leaq 0x48(%rsp), %rax
cmpq %rax, %rdi
je 0xdd49
movq 0x48(%rsp), %rsi
incq %rsi
callq 0x3230
movb $0x1, %bl
testb %bl, %bl
je 0xde06
incq %r12
cmpq %r12, %rbp
jne 0xdc24
jmp 0xddb3
cmpl $0xdc00, %eax           # imm = 0xDC00
jae 0xdd6d
movl %eax, %r13d
jmp 0xdd07
cmpl $0xe000, %eax           # imm = 0xE000
jb 0xdd8c
movl %eax, %ecx
shrl $0xc, %ecx
orb $-0x20, %cl
movb %cl, (%r15)
movl %eax, %ecx
shrl $0x6, %ecx
andb $-0x41, %cl
jmp 0xdcf8
movq 0x8(%rsp), %rdi
movq 0x30(%rsp), %rax
movq %rax, (%rdi)
xorl %ebx, %ebx
leaq 0x13fe(%rip), %rsi      # 0xf1a0
leaq 0x1406(%rip), %rdx      # 0xf1af
callq 0x97be
xorl %r13d, %r13d
jmp 0xdd4b
movq %r15, %rdi
callq 0x32e0
movq 0x8(%rsp), %rsi
leaq 0x10(%rsi), %rax
movq %rax, (%rsi)
movq 0x10(%rsp), %rcx
leaq 0x20(%rsp), %rdx
cmpq %rdx, %rcx
je 0xdde4
movq %rcx, (%rsi)
movq 0x20(%rsp), %rax
movq %rax, 0x10(%rsi)
jmp 0xddea
movups (%rdx), %xmm0
movups %xmm0, (%rax)
movq 0x18(%rsp), %rax
movq %rax, 0x8(%rsi)
movq %rdx, 0x10(%rsp)
movq $0x0, 0x18(%rsp)
movb $0x0, 0x20(%rsp)
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
je 0xde22
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x3230
movq 0x8(%rsp), %rax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0xde5b
jmp 0xde5b
movq %rax, %rbx
movq 0x38(%rsp), %rdi
leaq 0x48(%rsp), %rax
cmpq %rax, %rdi
je 0xde5e
movq 0x48(%rsp), %rsi
incq %rsi
callq 0x3230
jmp 0xde5e
movq %rax, %rbx
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
je 0xde7a
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x3230
movq %rbx, %rdi
callq 0x3470