// File: /workspace/github_c_makefile_star2_O2/cktan[P]tomlc99/toml.c
// Function: toml_ucs_to_utf8
int toml_ucs_to_utf8(int64_t code, char buf[6]) {
  /* http://stackoverflow.com/questions/6240055/manually-converting-unicode-codepoints-into-utf-8-and-utf-16
   */
  /* The UCS code values 0xd800–0xdfff (UTF-16 surrogates) as well
   * as 0xfffe and 0xffff (UCS noncharacters) should not appear in
   * conforming UTF-8 streams.
   */
  if (0xd800 <= code && code <= 0xdfff)
    return -1;
  if (0xfffe <= code && code <= 0xffff)
    return -1;

  /* 0x00000000 - 0x0000007F:
     0xxxxxxx
  */
  if (code < 0)
    return -1;
  if (code <= 0x7F) {
    buf[0] = (unsigned char)code;
    return 1;
  }

  /* 0x00000080 - 0x000007FF:
     110xxxxx 10xxxxxx
  */
  if (code <= 0x000007FF) {
    buf[0] = (unsigned char)(0xc0 | (code >> 6));
    buf[1] = (unsigned char)(0x80 | (code & 0x3f));
    return 2;
  }

  /* 0x00000800 - 0x0000FFFF:
     1110xxxx 10xxxxxx 10xxxxxx
  */
  if (code <= 0x0000FFFF) {
    buf[0] = (unsigned char)(0xe0 | (code >> 12));
    buf[1] = (unsigned char)(0x80 | ((code >> 6) & 0x3f));
    buf[2] = (unsigned char)(0x80 | (code & 0x3f));
    return 3;
  }

  /* 0x00010000 - 0x001FFFFF:
     11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
  */
  if (code <= 0x001FFFFF) {
    buf[0] = (unsigned char)(0xf0 | (code >> 18));
    buf[1] = (unsigned char)(0x80 | ((code >> 12) & 0x3f));
    buf[2] = (unsigned char)(0x80 | ((code >> 6) & 0x3f));
    buf[3] = (unsigned char)(0x80 | (code & 0x3f));
    return 4;
  }

  /* 0x00200000 - 0x03FFFFFF:
     111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
  */
  if (code <= 0x03FFFFFF) {
    buf[0] = (unsigned char)(0xf8 | (code >> 24));
    buf[1] = (unsigned char)(0x80 | ((code >> 18) & 0x3f));
    buf[2] = (unsigned char)(0x80 | ((code >> 12) & 0x3f));
    buf[3] = (unsigned char)(0x80 | ((code >> 6) & 0x3f));
    buf[4] = (unsigned char)(0x80 | (code & 0x3f));
    return 5;
  }

  /* 0x04000000 - 0x7FFFFFFF:
     1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
  */
  if (code <= 0x7FFFFFFF) {
    buf[0] = (unsigned char)(0xfc | (code >> 30));
    buf[1] = (unsigned char)(0x80 | ((code >> 24) & 0x3f));
    buf[2] = (unsigned char)(0x80 | ((code >> 18) & 0x3f));
    buf[3] = (unsigned char)(0x80 | ((code >> 12) & 0x3f));
    buf[4] = (unsigned char)(0x80 | ((code >> 6) & 0x3f));
    buf[5] = (unsigned char)(0x80 | (code & 0x3f));
    return 6;
  }

  return -1;
}

// Assembly:
movq %rdi, %rcx
andq $-0x800, %rcx           # imm = 0xF800
pushq $-0x1
popq %rax
cmpq $0xd800, %rcx           # imm = 0xD800
je 0x1c94
movq %rdi, %rcx
andq $-0x2, %rcx
cmpq $0xfffe, %rcx           # imm = 0xFFFE
sete %cl
testq %rdi, %rdi
sets %dl
orb %cl, %dl
je 0x1c95
retq
cmpq $0x7f, %rdi
ja 0x1ca5
movb %dil, (%rsi)
pushq $0x1
jmp 0x1df2
cmpq $0x7ff, %rdi            # imm = 0x7FF
ja 0x1cca
movl %edi, %eax
shrl $0x6, %eax
orb $-0x40, %al
movb %al, (%rsi)
andb $0x3f, %dil
orb $-0x80, %dil
movb %dil, 0x1(%rsi)
pushq $0x2
jmp 0x1df2
cmpq $0xffff, %rdi           # imm = 0xFFFF
jg 0x1cfb
movl %edi, %eax
shrl $0xc, %eax
orb $-0x20, %al
movb %al, (%rsi)
movl %edi, %eax
shrl $0x6, %eax
andb $0x3f, %al
orb $-0x80, %al
movb %al, 0x1(%rsi)
andb $0x3f, %dil
orb $-0x80, %dil
movb %dil, 0x2(%rsi)
pushq $0x3
jmp 0x1df2
cmpq $0x1fffff, %rdi         # imm = 0x1FFFFF
ja 0x1d38
movl %edi, %eax
shrl $0x12, %eax
orb $-0x10, %al
movb %al, (%rsi)
movl %edi, %eax
shrl $0xc, %eax
andb $0x3f, %al
orb $-0x80, %al
movb %al, 0x1(%rsi)
movl %edi, %eax
shrl $0x6, %eax
andb $0x3f, %al
orb $-0x80, %al
movb %al, 0x2(%rsi)
andb $0x3f, %dil
orb $-0x80, %dil
movb %dil, 0x3(%rsi)
pushq $0x4
jmp 0x1df2
cmpq $0x3ffffff, %rdi        # imm = 0x3FFFFFF
ja 0x1d8e
movl %edi, %eax
shrl $0x18, %eax
orb $-0x8, %al
movb %al, (%rsi)
movl %edi, %eax
shrl $0x12, %eax
movl %edi, %ecx
shrl $0x4, %ecx
andl $0xff00, %ecx           # imm = 0xFF00
movzbl %al, %eax
orl %ecx, %eax
movl %edi, %ecx
shll $0xa, %ecx
andl $0xff0000, %ecx         # imm = 0xFF0000
orl %eax, %ecx
shll $0x18, %edi
orl %ecx, %edi
movd %edi, %xmm0
pand 0x33b3(%rip), %xmm0     # 0x5130
por 0x33bb(%rip), %xmm0     # 0x5140
movd %xmm0, 0x1(%rsi)
pushq $0x5
jmp 0x1df2
cmpq $0x7fffffff, %rdi       # imm = 0x7FFFFFFF
ja 0x1c94
movl %edi, %eax
shrl $0x1e, %eax
orb $-0x4, %al
movb %al, (%rsi)
movl %edi, %eax
shrl $0x18, %eax
andb $0x3f, %al
orb $-0x80, %al
movb %al, 0x1(%rsi)
movl %edi, %eax
shrl $0x12, %eax
movl %edi, %ecx
shrl $0x4, %ecx
andl $0xff00, %ecx           # imm = 0xFF00
movzbl %al, %eax
orl %ecx, %eax
movl %edi, %ecx
shll $0xa, %ecx
andl $0xff0000, %ecx         # imm = 0xFF0000
orl %eax, %ecx
shll $0x18, %edi
orl %ecx, %edi
movd %edi, %xmm0
pand 0x334d(%rip), %xmm0     # 0x5130
por 0x3355(%rip), %xmm0     # 0x5140
movd %xmm0, 0x2(%rsi)
pushq $0x6
popq %rax
retq