// File: 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
movq %rsp, %rbp
subq $0xd0, %rsp
movq %rdi, -0xa8(%rbp)
movq %rdi, %rax
movq %rax, -0xa0(%rbp)
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movl $0x0, -0x18(%rbp)
movl $0x6, %edi
callq 0x3490
movq %rax, -0x20(%rbp)
leaq -0x40(%rbp), %rdi
callq 0x34b0
movl $0x0, -0x44(%rbp)
movl -0x44(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0x19efc
movq -0x10(%rbp), %rax
movslq -0x44(%rbp), %rcx
movw (%rax,%rcx,2), %ax
movw %ax, -0x46(%rbp)
movq -0x20(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rdi
xorl %esi, %esi
movl $0x6, %edx
callq 0x3190
cmpl $0x0, -0x18(%rbp)
je 0x19c85
movzwl -0x46(%rbp), %eax
cmpl $0xdc00, %eax           # imm = 0xDC00
jl 0x19c25
movzwl -0x46(%rbp), %eax
cmpl $0xe000, %eax           # imm = 0xE000
jge 0x19c25
movl -0x18(%rbp), %eax
addl $0x40, %eax
sarl $0x8, %eax
andl $0x7, %eax
addl $0xf0, %eax
movb %al, %cl
movq -0x50(%rbp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, -0x50(%rbp)
movb %cl, (%rax)
movl -0x18(%rbp), %eax
addl $0x40, %eax
sarl $0x2, %eax
andl $0x3f, %eax
addl $0x80, %eax
movb %al, %cl
movq -0x50(%rbp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, -0x50(%rbp)
movb %cl, (%rax)
movzwl -0x46(%rbp), %eax
sarl $0x6, %eax
andl $0xf, %eax
addl $0x80, %eax
movl -0x18(%rbp), %ecx
andl $0x3, %ecx
shll $0x4, %ecx
addl %ecx, %eax
movb %al, %cl
movq -0x50(%rbp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, -0x50(%rbp)
movb %cl, (%rax)
movzwl -0x46(%rbp), %eax
andl $0x3f, %eax
addl $0x80, %eax
movb %al, %cl
movq -0x50(%rbp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, -0x50(%rbp)
movb %cl, (%rax)
movl $0x0, -0x18(%rbp)
jmp 0x19c80
leaq -0x51(%rbp), %rdi
movq %rdi, -0xb0(%rbp)
callq 0x35d0
movq -0xa8(%rbp), %rdi
movq -0xb0(%rbp), %rdx
leaq 0x2558(%rip), %rsi      # 0x1c1a2
callq 0xa400
jmp 0x19c51
leaq -0x51(%rbp), %rdi
callq 0x33c0
movl $0x1, -0x68(%rbp)
jmp 0x19f1c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
leaq -0x51(%rbp), %rdi
callq 0x33c0
jmp 0x19f35
jmp 0x19e56
movzwl -0x46(%rbp), %eax
cmpl $0x80, %eax
jge 0x19cac
movw -0x46(%rbp), %ax
movb %al, %cl
movq -0x50(%rbp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, -0x50(%rbp)
movb %cl, (%rax)
jmp 0x19e54
movzwl -0x46(%rbp), %eax
cmpl $0x800, %eax            # imm = 0x800
jge 0x19cfd
movzwl -0x46(%rbp), %eax
sarl $0x6, %eax
andl $0x3f, %eax
orl $0xc0, %eax
movb %al, %cl
movq -0x50(%rbp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, -0x50(%rbp)
movb %cl, (%rax)
movzwl -0x46(%rbp), %eax
andl $0x3f, %eax
orl $0x80, %eax
movb %al, %cl
movq -0x50(%rbp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, -0x50(%rbp)
movb %cl, (%rax)
jmp 0x19e52
movzwl -0x46(%rbp), %eax
cmpl $0xd800, %eax           # imm = 0xD800
jge 0x19d6d
movzwl -0x46(%rbp), %eax
sarl $0xc, %eax
orl $0xe0, %eax
movb %al, %cl
movq -0x50(%rbp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, -0x50(%rbp)
movb %cl, (%rax)
movzwl -0x46(%rbp), %eax
sarl $0x6, %eax
andl $0x3f, %eax
orl $0x80, %eax
movb %al, %cl
movq -0x50(%rbp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, -0x50(%rbp)
movb %cl, (%rax)
movzwl -0x46(%rbp), %eax
andl $0x3f, %eax
orl $0x80, %eax
movb %al, %cl
movq -0x50(%rbp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, -0x50(%rbp)
movb %cl, (%rax)
jmp 0x19e50
movzwl -0x46(%rbp), %eax
cmpl $0xdc00, %eax           # imm = 0xDC00
jge 0x19d84
movzwl -0x46(%rbp), %eax
movl %eax, -0x18(%rbp)
jmp 0x19e4e
movzwl -0x46(%rbp), %eax
cmpl $0xe000, %eax           # imm = 0xE000
jl 0x19df1
movzwl -0x46(%rbp), %eax
sarl $0xc, %eax
orl $0xe0, %eax
movb %al, %cl
movq -0x50(%rbp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, -0x50(%rbp)
movb %cl, (%rax)
movzwl -0x46(%rbp), %eax
sarl $0x6, %eax
andl $0x3f, %eax
orl $0x80, %eax
movb %al, %cl
movq -0x50(%rbp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, -0x50(%rbp)
movb %cl, (%rax)
movzwl -0x46(%rbp), %eax
andl $0x3f, %eax
orl $0x80, %eax
movb %al, %cl
movq -0x50(%rbp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, -0x50(%rbp)
movb %cl, (%rax)
jmp 0x19e4c
leaq -0x69(%rbp), %rdi
movq %rdi, -0xb8(%rbp)
callq 0x35d0
movq -0xa8(%rbp), %rdi
movq -0xb8(%rbp), %rdx
leaq 0x238c(%rip), %rsi      # 0x1c1a2
callq 0xa400
jmp 0x19e1d
leaq -0x69(%rbp), %rdi
callq 0x33c0
movl $0x1, -0x68(%rbp)
jmp 0x19f1c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
leaq -0x69(%rbp), %rdi
callq 0x33c0
jmp 0x19f35
jmp 0x19e4e
jmp 0x19e50
jmp 0x19e52
jmp 0x19e54
jmp 0x19e56
movq -0x20(%rbp), %rax
movq %rax, -0xc8(%rbp)
leaq -0x91(%rbp), %rdi
movq %rdi, -0xc0(%rbp)
callq 0x35d0
movq -0xc8(%rbp), %rsi
movq -0xc0(%rbp), %rdx
leaq -0x90(%rbp), %rdi
callq 0xa400
jmp 0x19e90
leaq -0x40(%rbp), %rdi
leaq -0x90(%rbp), %rsi
callq 0x3470
jmp 0x19ea2
leaq -0x90(%rbp), %rdi
callq 0x31d0
leaq -0x91(%rbp), %rdi
callq 0x33c0
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0x19b2c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
jmp 0x19eee
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
leaq -0x90(%rbp), %rdi
callq 0x31d0
leaq -0x91(%rbp), %rdi
callq 0x33c0
jmp 0x19f35
movq -0x20(%rbp), %rdi
callq 0x3430
movq -0xa8(%rbp), %rdi
leaq -0x40(%rbp), %rsi
callq 0x31c0
movl $0x1, -0x68(%rbp)
leaq -0x40(%rbp), %rdi
callq 0x31d0
movq -0xa0(%rbp), %rax
addq $0xd0, %rsp
popq %rbp
retq
leaq -0x40(%rbp), %rdi
callq 0x31d0
movq -0x60(%rbp), %rdi
callq 0x35c0
nopw (%rax,%rax)