// File: /workspace/github_c_makefile_star2_O1/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
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpq $0xd800, %rcx           # imm = 0xD800
je 0x2020
movq %rdi, %rcx
andq $-0x2, %rcx
cmpq $0xfffe, %rcx           # imm = 0xFFFE
sete %cl
testq %rdi, %rdi
sets %dl
orb %cl, %dl
je 0x2021
retq
cmpq $0x7f, %rdi
jg 0x2030
movb %dil, (%rsi)
movl $0x1, %eax
retq
cmpq $0x7ff, %rdi            # imm = 0x7FF
jg 0x2054
movl %edi, %eax
shrl $0x6, %eax
orb $-0x40, %al
movb %al, (%rsi)
andb $0x3f, %dil
orb $-0x80, %dil
movb %dil, 0x1(%rsi)
movl $0x2, %eax
retq
cmpq $0xffff, %rdi           # imm = 0xFFFF
jg 0x2084
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)
movl $0x3, %eax
retq
cmpq $0x1fffff, %rdi         # imm = 0x1FFFFF
jg 0x20c0
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)
movl $0x4, %eax
retq
cmpq $0x3ffffff, %rdi        # imm = 0x3FFFFFF
jg 0x2108
movl %edi, %eax
shrl $0x18, %eax
orb $-0x8, %al
movb %al, (%rsi)
movl %edi, %eax
shrl $0x12, %eax
andb $0x3f, %al
orb $-0x80, %al
movb %al, 0x1(%rsi)
movl %edi, %eax
shrl $0xc, %eax
andb $0x3f, %al
orb $-0x80, %al
movb %al, 0x2(%rsi)
movl %edi, %eax
shrl $0x6, %eax
andb $0x3f, %al
orb $-0x80, %al
movb %al, 0x3(%rsi)
andb $0x3f, %dil
orb $-0x80, %dil
movb %dil, 0x4(%rsi)
movl $0x5, %eax
retq
cmpq $0x7fffffff, %rdi       # imm = 0x7FFFFFFF
jg 0x2020
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
andb $0x3f, %al
orb $-0x80, %al
movb %al, 0x2(%rsi)
movl %edi, %eax
shrl $0xc, %eax
andb $0x3f, %al
orb $-0x80, %al
movb %al, 0x3(%rsi)
movl %edi, %eax
shrl $0x6, %eax
andb $0x3f, %al
orb $-0x80, %al
movb %al, 0x4(%rsi)
andb $0x3f, %dil
orb $-0x80, %dil
movb %dil, 0x5(%rsi)
movl $0x6, %eax
retq