// File: /workspace/github_cpp_makefile_star2_O1/lemire[P]validateutf8-experiments/benchmarks/fushia.h
// Function: fidl_validate_string(unsigned char const*, unsigned long)
fastvalidate::error_code fidl_validate_string(const unsigned char *data,
                                              uint64_t size) {
  uint64_t pos = 0;
  uint64_t next_pos = 0;
  uint32_t code_point = 0;
  while (pos < size) {
    // The following comparison relies on treating the byte as if it was an
    // unsigned 8-bit value. However, both signed and unsigned char are allowed
    // in the C++ spec, with x64 choosing signed, and arm64 choosing unsigned.
    // We therefore force the byte to be treated as unsigned, since we cannot
    // rely on the default.
    unsigned char byte = data[pos];
    if (byte < 0b10000000) {
      pos++;
      continue;
    } else if ((byte & 0b11100000) == 0b11000000) {
      next_pos = pos + 2;
      if (next_pos > size) {
        return fastvalidate::error_code::UTF8_ERROR;
      }
      if ((data[pos + 1] & 0b11000000) != 0b10000000) {
        return fastvalidate::error_code::UTF8_ERROR;
      }
      // range check
      code_point = (byte & 0b00011111) << 6 | (data[pos + 1] & 0b00111111);
      if (code_point < 0x80 || 0x7ff < code_point) {
        return fastvalidate::error_code::UTF8_ERROR;
      }
    } else if ((byte & 0b11110000) == 0b11100000) {
      next_pos = pos + 3;
      if (next_pos > size) {
        return fastvalidate::error_code::UTF8_ERROR;
      }
      if ((data[pos + 1] & 0b11000000) != 0b10000000) {
        return fastvalidate::error_code::UTF8_ERROR;
      }
      if ((data[pos + 2] & 0b11000000) != 0b10000000) {
        return fastvalidate::error_code::UTF8_ERROR;
      }
      // range check
      code_point = (byte & 0b00001111) << 12 |
                   (data[pos + 1] & 0b00111111) << 6 |
                   (data[pos + 2] & 0b00111111);
      if (code_point < 0x800 || 0xffff < code_point ||
          (0xd7ff < code_point && code_point < 0xe000)) {
        return fastvalidate::error_code::UTF8_ERROR;
      }
    } else if ((byte & 0b11111000) == 0b11110000) { // 0b11110000
      next_pos = pos + 4;
      if (next_pos > size) {
        return fastvalidate::error_code::UTF8_ERROR;
      }
      if ((data[pos + 1] & 0b11000000) != 0b10000000) {
        return fastvalidate::error_code::UTF8_ERROR;
      }
      if ((data[pos + 2] & 0b11000000) != 0b10000000) {
        return fastvalidate::error_code::UTF8_ERROR;
      }
      if ((data[pos + 3] & 0b11000000) != 0b10000000) {
        return fastvalidate::error_code::UTF8_ERROR;
      }
      // range check
      code_point =
          (byte & 0b00000111) << 18 | (data[pos + 1] & 0b00111111) << 12 |
          (data[pos + 2] & 0b00111111) << 6 | (data[pos + 3] & 0b00111111);
      if (code_point < 0xffff || 0x10ffff < code_point) {
        return fastvalidate::error_code::UTF8_ERROR;
      }
    } else {
      // we may have a continuation
      return fastvalidate::error_code::UTF8_ERROR;
    }
    pos = next_pos;
  }
  return fastvalidate::error_code::SUCCESS;
}

// Assembly:
testq %rsi, %rsi
je 0x57c7
pushq %rbx
xorl %eax, %eax
movzbl (%rdi,%rax), %r8d
testb %r8b, %r8b
js 0x5659
incq %rax
movl $0x2, %ecx
testb $0x1, %cl
jne 0x57ca
cmpq %rsi, %rax
jb 0x5634
jmp 0x57c3
movl %r8d, %ecx
andl $-0x20, %ecx
cmpl $0xc0, %ecx
jne 0x5690
leaq 0x2(%rax), %rdx
movl $0x1, %ecx
cmpq %rsi, %rdx
ja 0x5646
testb $0x1e, %r8b
je 0x5646
movb 0x1(%rdi,%rax), %r8b
andb $-0x40, %r8b
negb %r8b
jno 0x5646
xorl %ecx, %ecx
movq %rdx, %rax
jmp 0x5646
movl %r8d, %ecx
andl $-0x10, %ecx
cmpl $0xe0, %ecx
jne 0x570f
leaq 0x3(%rax), %rdx
movl $0x1, %ecx
cmpq %rsi, %rdx
ja 0x5646
movzbl 0x1(%rdi,%rax), %r9d
movl %r9d, %r10d
andl $-0x40, %r10d
cmpl $0x80, %r10d
jne 0x5646
movb 0x2(%rdi,%rax), %r10b
andb $-0x40, %r10b
negb %r10b
jno 0x5646
shll $0xc, %r8d
movzwl %r8w, %r8d
andl $0x3f, %r9d
shll $0x6, %r9d
orl %r8d, %r9d
cmpl $0x800, %r9d            # imm = 0x800
jb 0x5646
cmpl $0xdfff, %r8d           # imm = 0xDFFF
ja 0x5689
cmpl $0xd7ff, %r9d           # imm = 0xD7FF
ja 0x5646
jmp 0x5689
movl %r8d, %edx
andl $-0x8, %edx
movl $0x1, %ecx
cmpl $0xf0, %edx
jne 0x5646
leaq 0x4(%rax), %rdx
cmpq %rsi, %rdx
ja 0x5646
movzbl 0x1(%rdi,%rax), %r9d
movl %r9d, %r10d
andl $-0x40, %r10d
cmpl $0x80, %r10d
jne 0x5646
movzbl 0x2(%rdi,%rax), %r10d
movl %r10d, %r11d
andl $-0x40, %r11d
cmpl $0x80, %r11d
jne 0x5646
movzbl 0x3(%rdi,%rax), %r11d
movl %r11d, %ebx
andl $-0x40, %ebx
cmpl $0x80, %ebx
jne 0x5646
andl $0x7, %r8d
shll $0x12, %r8d
andl $0x3f, %r9d
shll $0xc, %r9d
orl %r8d, %r9d
cmpl $0x10ffff, %r9d         # imm = 0x10FFFF
ja 0x5646
andl $0x3f, %r10d
shll $0x6, %r10d
andl $0x3f, %r11d
orl %r10d, %r11d
orl %r9d, %r11d
cmpl $0xffff, %r11d          # imm = 0xFFFF
jae 0x5689
jmp 0x5646
xorl %eax, %eax
jmp 0x57cf
xorl %eax, %eax
retq
movl $0x1, %eax
popq %rbx
retq