// File: /workspace/github_cpp_makefile_star2_O3/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, %r13
movl $0x6, %edi
callq 0x3320
movq %rax, %r15
leaq 0x28(%rsp), %r12
movq %r12, -0x10(%r12)
movq $0x0, -0x8(%r12)
movb $0x0, (%r12)
testl %ebp, %ebp
jle 0xdd51
movq %r13, 0x10(%rsp)
movl %ebp, %ebx
xorl %r12d, %r12d
leaq 0x18(%rsp), %rbp
xorl %r13d, %r13d
movzwl (%r14,%r12,2), %eax
movw $0x0, 0x4(%r15)
movl $0x0, (%r15)
testl %r13d, %r13d
je 0xdc53
movl %eax, %ecx
andl $0xfc00, %ecx           # imm = 0xFC00
cmpl $0xdc00, %ecx           # imm = 0xDC00
jne 0xdd87
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 0xdccf
cmpw $0x7f, %ax
ja 0xdc5e
movb %al, (%r15)
jmp 0xdccf
cmpl $0x7ff, %eax            # imm = 0x7FF
ja 0xdc7a
movl %eax, %ecx
shrl $0x6, %ecx
orb $-0x40, %cl
movb %cl, (%r15)
andb $0x3f, %al
orb $-0x80, %al
movb %al, 0x1(%r15)
jmp 0xdccf
cmpl $0xd7ff, %eax           # imm = 0xD7FF
ja 0xdc99
movl %eax, %ecx
shrl $0xc, %ecx
orb $-0x20, %cl
movb %cl, (%r15)
movl %eax, %ecx
shrl $0x6, %ecx
andb $0x3f, %cl
orb $-0x80, %cl
jmp 0xdcc3
cmpl $0xdc00, %eax           # imm = 0xDC00
jae 0xdca5
movl %eax, %r13d
jmp 0xdcd2
cmpl $0xe000, %eax           # imm = 0xE000
jb 0xddd8
movl %eax, %ecx
shrl $0xc, %ecx
orb $-0x20, %cl
movb %cl, (%r15)
movl %eax, %ecx
shrl $0x6, %ecx
andb $-0x41, %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 0xf(%rsp), %rdx
callq 0x8e9c
movq 0x38(%rsp), %rsi
movq 0x40(%rsp), %rdx
movq %rbp, %rdi
callq 0x30b0
movq 0x38(%rsp), %rdi
leaq 0x48(%rsp), %rax
cmpq %rax, %rdi
je 0xdd12
movq 0x48(%rsp), %rsi
incq %rsi
callq 0x3230
incq %r12
cmpq %r12, %rbx
jne 0xdbe6
movq 0x18(%rsp), %rbx
movq %r15, %rdi
callq 0x32e0
movq 0x10(%rsp), %r13
leaq 0x10(%r13), %rax
movq %rax, (%r13)
leaq 0x28(%rsp), %r12
cmpq %r12, %rbx
je 0xdd61
movq %rbx, (%r13)
movq 0x28(%rsp), %rax
movq %rax, 0x10(%r13)
jmp 0xdd69
movq %r15, %rdi
callq 0x32e0
leaq 0x10(%r13), %rax
movq %rax, (%r13)
movups (%r12), %xmm0
movups %xmm0, (%rax)
movq 0x20(%rsp), %rax
movq %rax, 0x8(%r13)
movq %r12, 0x18(%rsp)
movq $0x0, 0x20(%rsp)
movb $0x0, 0x28(%rsp)
jmp 0xddaf
movq 0x10(%rsp), %r13
leaq 0x10(%r13), %rax
movq %rax, (%r13)
leaq 0x1405(%rip), %rsi      # 0xf1a0
leaq 0x140d(%rip), %rdx      # 0xf1af
movq %r13, %rdi
leaq 0x28(%rsp), %r12
callq 0x981e
movq 0x18(%rsp), %rdi
cmpq %r12, %rdi
je 0xddc6
movq 0x28(%rsp), %rsi
incq %rsi
callq 0x3230
movq %r13, %rax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x10(%rsp), %r13
leaq 0x10(%r13), %rax
movq %rax, (%r13)
leaq 0x13b4(%rip), %rsi      # 0xf1a0
leaq 0x13bc(%rip), %rdx      # 0xf1af
movq %r13, %rdi
leaq 0x28(%rsp), %r12
callq 0x981e
jmp 0xddaf
jmp 0xde06
jmp 0xde06
movq %rax, %r14
jmp 0xde2a
movq %rax, %r14
movq 0x38(%rsp), %rdi
leaq 0x48(%rsp), %rax
cmpq %rax, %rdi
je 0xde2a
movq 0x48(%rsp), %rsi
incq %rsi
callq 0x3230
movq 0x18(%rsp), %rdi
leaq 0x28(%rsp), %rax
cmpq %rax, %rdi
je 0xde46
movq 0x28(%rsp), %rsi
incq %rsi
callq 0x3230
movq %r14, %rdi
callq 0x3460