// File: lvandeve[P]base256/base256.cpp
// Function: unicode_to_utf8(std::vector<int, std::allocator<int>> const&)
std::vector<uint8_t> unicode_to_utf8(const std::vector<int>& a) {
  std::vector<uint8_t> result;
  for(int i = 0; i < a.size(); i++) {
    int code_point = a[i];
    if(code_point < 128) {
      result.push_back(code_point);
    } else if(code_point <= 2047) {
      result.push_back((code_point >> 6) + 192);
      result.push_back((code_point & 63) + 128);
    } else if(code_point <= 65535) {
      result.push_back((code_point >> 12) + 224);
      result.push_back(((code_point >> 6) & 63) + 128);
      result.push_back((code_point & 63) + 128);
    } else if(code_point <= 1114111) {
      result.push_back((code_point >> 18) + 240);
      result.push_back(((code_point >> 12) & 63) + 128);
      result.push_back(((code_point >> 6) & 63) + 128);
      result.push_back((code_point & 63) + 128);
    } else {
      // error, add replacement character
      result.push_back(239);
      result.push_back(191);
      result.push_back(189);
    }
  }
  return result;
}

// Assembly:
subq $0x58, %rsp
movq %rdi, 0x10(%rsp)
movq %rdi, %rax
movq %rax, 0x18(%rsp)
movq %rdi, 0x50(%rsp)
movq %rsi, 0x48(%rsp)
movb $0x0, 0x47(%rsp)
callq 0xd5f0
movl $0x0, 0x40(%rsp)
movslq 0x40(%rsp), %rax
movq %rax, 0x8(%rsp)
movq 0x48(%rsp), %rdi
callq 0xd600
movq %rax, %rcx
movq 0x8(%rsp), %rax
cmpq %rcx, %rax
jae 0x6b73
movq 0x48(%rsp), %rdi
movslq 0x40(%rsp), %rsi
callq 0xd620
movl (%rax), %eax
movl %eax, 0x3c(%rsp)
cmpl $0x80, 0x3c(%rsp)
jge 0x69dc
movq 0x10(%rsp), %rdi
movb 0x3c(%rsp), %al
movb %al, 0x3b(%rsp)
leaq 0x3b(%rsp), %rsi
callq 0xd640
jmp 0x69ba
jmp 0x6b61
movq 0x10(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x30(%rsp)
movl %eax, 0x2c(%rsp)
callq 0xd670
jmp 0x6b93
cmpl $0x7ff, 0x3c(%rsp)      # imm = 0x7FF
jg 0x6a26
movq 0x10(%rsp), %rdi
movl 0x3c(%rsp), %eax
shrl $0x6, %eax
addb $-0x40, %al
movb %al, 0x2b(%rsp)
leaq 0x2b(%rsp), %rsi
callq 0xd640
jmp 0x6a04
movq 0x10(%rsp), %rdi
movb 0x3c(%rsp), %al
andb $0x3f, %al
orb $-0x80, %al
movb %al, 0x2a(%rsp)
leaq 0x2a(%rsp), %rsi
callq 0xd640
jmp 0x6a21
jmp 0x6b5f
cmpl $0xffff, 0x3c(%rsp)     # imm = 0xFFFF
jg 0x6a90
movq 0x10(%rsp), %rdi
movl 0x3c(%rsp), %eax
shrl $0xc, %eax
addb $-0x20, %al
movb %al, 0x29(%rsp)
leaq 0x29(%rsp), %rsi
callq 0xd640
jmp 0x6a4e
movq 0x10(%rsp), %rdi
movl 0x3c(%rsp), %eax
shrl $0x6, %eax
andb $0x3f, %al
orb $-0x80, %al
movb %al, 0x28(%rsp)
leaq 0x28(%rsp), %rsi
callq 0xd640
jmp 0x6a6e
movq 0x10(%rsp), %rdi
movb 0x3c(%rsp), %al
andb $0x3f, %al
orb $-0x80, %al
movb %al, 0x27(%rsp)
leaq 0x27(%rsp), %rsi
callq 0xd640
jmp 0x6a8b
jmp 0x6b5d
cmpl $0x10ffff, 0x3c(%rsp)   # imm = 0x10FFFF
jg 0x6b17
movq 0x10(%rsp), %rdi
movl 0x3c(%rsp), %eax
shrl $0x12, %eax
addb $-0x10, %al
movb %al, 0x26(%rsp)
leaq 0x26(%rsp), %rsi
callq 0xd640
jmp 0x6ab8
movq 0x10(%rsp), %rdi
movl 0x3c(%rsp), %eax
shrl $0xc, %eax
andb $0x3f, %al
orb $-0x80, %al
movb %al, 0x25(%rsp)
leaq 0x25(%rsp), %rsi
callq 0xd640
jmp 0x6ad8
movq 0x10(%rsp), %rdi
movl 0x3c(%rsp), %eax
shrl $0x6, %eax
andb $0x3f, %al
orb $-0x80, %al
movb %al, 0x24(%rsp)
leaq 0x24(%rsp), %rsi
callq 0xd640
jmp 0x6af8
movq 0x10(%rsp), %rdi
movb 0x3c(%rsp), %al
andb $0x3f, %al
orb $-0x80, %al
movb %al, 0x23(%rsp)
leaq 0x23(%rsp), %rsi
callq 0xd640
jmp 0x6b15
jmp 0x6b5b
movq 0x10(%rsp), %rdi
movb $-0x11, 0x22(%rsp)
leaq 0x22(%rsp), %rsi
callq 0xd640
jmp 0x6b2d
movq 0x10(%rsp), %rdi
movb $-0x41, 0x21(%rsp)
leaq 0x21(%rsp), %rsi
callq 0xd640
jmp 0x6b43
movq 0x10(%rsp), %rdi
movb $-0x43, 0x20(%rsp)
leaq 0x20(%rsp), %rsi
callq 0xd640
jmp 0x6b59
jmp 0x6b5b
jmp 0x6b5d
jmp 0x6b5f
jmp 0x6b61
jmp 0x6b63
movl 0x40(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x40(%rsp)
jmp 0x695d
movb $0x1, 0x47(%rsp)
testb $0x1, 0x47(%rsp)
jne 0x6b89
movq 0x10(%rsp), %rdi
callq 0xd670
movq 0x18(%rsp), %rax
addq $0x58, %rsp
retq
movq 0x30(%rsp), %rdi
callq 0x54e0
nopl (%rax)