// File: /workspace/github_c_makefile_star2_O2/cktan[P]tomlc99/toml.c
// Function: toml_utf8_to_ucs
int toml_utf8_to_ucs(const char *orig, int len, int64_t *ret) {
  const unsigned char *buf = (const unsigned char *)orig;
  unsigned i = *buf++;
  int64_t v;

  /* 0x00000000 - 0x0000007F:
     0xxxxxxx
  */
  if (0 == (i >> 7)) {
    if (len < 1)
      return -1;
    v = i;
    return *ret = v, 1;
  }
  /* 0x00000080 - 0x000007FF:
     110xxxxx 10xxxxxx
  */
  if (0x6 == (i >> 5)) {
    if (len < 2)
      return -1;
    v = i & 0x1f;
    for (int j = 0; j < 1; j++) {
      i = *buf++;
      if (0x2 != (i >> 6))
        return -1;
      v = (v << 6) | (i & 0x3f);
    }
    return *ret = v, (const char *)buf - orig;
  }

  /* 0x00000800 - 0x0000FFFF:
     1110xxxx 10xxxxxx 10xxxxxx
  */
  if (0xE == (i >> 4)) {
    if (len < 3)
      return -1;
    v = i & 0x0F;
    for (int j = 0; j < 2; j++) {
      i = *buf++;
      if (0x2 != (i >> 6))
        return -1;
      v = (v << 6) | (i & 0x3f);
    }
    return *ret = v, (const char *)buf - orig;
  }

  /* 0x00010000 - 0x001FFFFF:
     11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
  */
  if (0x1E == (i >> 3)) {
    if (len < 4)
      return -1;
    v = i & 0x07;
    for (int j = 0; j < 3; j++) {
      i = *buf++;
      if (0x2 != (i >> 6))
        return -1;
      v = (v << 6) | (i & 0x3f);
    }
    return *ret = v, (const char *)buf - orig;
  }

  /* 0x00200000 - 0x03FFFFFF:
     111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
  */
  if (0x3E == (i >> 2)) {
    if (len < 5)
      return -1;
    v = i & 0x03;
    for (int j = 0; j < 4; j++) {
      i = *buf++;
      if (0x2 != (i >> 6))
        return -1;
      v = (v << 6) | (i & 0x3f);
    }
    return *ret = v, (const char *)buf - orig;
  }

  /* 0x04000000 - 0x7FFFFFFF:
     1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
  */
  if (0x7e == (i >> 1)) {
    if (len < 6)
      return -1;
    v = i & 0x01;
    for (int j = 0; j < 5; j++) {
      i = *buf++;
      if (0x2 != (i >> 6))
        return -1;
      v = (v << 6) | (i & 0x3f);
    }
    return *ret = v, (const char *)buf - orig;
  }
  return -1;
}

// Assembly:
movzbl (%rdi), %ecx
testb %cl, %cl
js 0x1adb
testl %esi, %esi
jle 0x1b0f
movl %ecx, %ecx
pushq $0x1
popq %rsi
movq %rcx, (%rdx)
movl %esi, %eax
retq
movl %ecx, %eax
andl $-0x20, %eax
cmpl $0xc0, %eax
jne 0x1b13
pushq $-0x1
popq %rax
cmpl $0x2, %esi
jl 0x1ada
movzbl 0x1(%rdi), %esi
movl %esi, %edi
andl $-0x40, %edi
cmpl $0x80, %edi
jne 0x1ada
andl $0x3f, %esi
andl $0x1f, %ecx
shll $0x6, %ecx
orl %esi, %ecx
pushq $0x2
jmp 0x1ad4
pushq $-0x1
popq %rax
retq
movl %ecx, %eax
andl $-0x10, %eax
cmpl $0xe0, %eax
jne 0x1b5c
pushq $-0x1
popq %rax
cmpl $0x3, %esi
jl 0x1ada
andl $0xf, %ecx
xorl %r8d, %r8d
pushq $0x3
popq %rsi
cmpl $0x2, %r8d
je 0x1ad5
movzbl 0x1(%rdi,%r8), %r9d
movl %r9d, %r10d
andl $-0x40, %r10d
cmpl $0x80, %r10d
jne 0x1ada
shlq $0x6, %rcx
andl $0x3f, %r9d
orq %r9, %rcx
incq %r8
jmp 0x1b30
movl %ecx, %eax
andl $-0x8, %eax
cmpl $0xf0, %eax
jne 0x1bb1
pushq $-0x1
popq %rax
cmpl $0x4, %esi
jl 0x1ada
andl $0x7, %ecx
pushq $0x1
popq %r8
pushq $0x4
popq %rsi
cmpl $0x4, %r8d
je 0x1ad5
movzbl (%rdi,%r8), %r9d
movl %r9d, %r10d
andl $-0x40, %r10d
cmpl $0x80, %r10d
jne 0x1ada
shlq $0x6, %rcx
andl $0x3f, %r9d
orq %r9, %rcx
incq %r8
jmp 0x1b7e
movl %ecx, %eax
andl $-0x4, %eax
cmpl $0xf8, %eax
jne 0x1c06
pushq $-0x1
popq %rax
cmpl $0x5, %esi
jl 0x1ada
andl $0x3, %ecx
xorl %r8d, %r8d
pushq $0x5
popq %rsi
cmpl $0x4, %r8d
je 0x1ad5
movzbl 0x1(%rdi,%r8), %r9d
movl %r9d, %r10d
andl $-0x40, %r10d
cmpl $0x80, %r10d
jne 0x1ada
shlq $0x6, %rcx
andl $0x3f, %r9d
orq %r9, %rcx
incq %r8
jmp 0x1bd2
movl %ecx, %eax
andl $-0x2, %eax
cmpl $0xfc, %eax
setne %al
cmpl $0x6, %esi
setl %sil
orb %al, %sil
pushq $-0x1
popq %rax
jne 0x1ada
andl $0x1, %ecx
pushq $0x1
popq %r8
pushq $0x6
popq %rsi
cmpl $0x6, %r8d
je 0x1ad5
movzbl (%rdi,%r8), %r9d
movl %r9d, %r10d
andl $-0x40, %r10d
cmpl $0x80, %r10d
jne 0x1ada
shlq $0x6, %rcx
andl $0x3f, %r9d
orq %r9, %rcx
incq %r8
jmp 0x1c30