// File: /workspace/github_cpp_makefile_star2_O2/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:
pushq %rbx
xorl %eax, %eax
movl $0x1, %ecx
xorl %edx, %edx
cmpq %rsi, %rdx
jae 0x2502
movzbl (%rdi,%rdx), %r8d
testb %r8b, %r8b
js 0x23a6
incq %rdx
jmp 0x238e
movl %r8d, %r9d
andl $-0x20, %r9d
cmpl $0xc0, %r9d
jne 0x23e0
leaq 0x2(%rdx), %r9
cmpq %rsi, %r9
ja 0x2500
testb $0x1e, %r8b
je 0x2500
movb 0x1(%rdi,%rdx), %dl
andb $-0x40, %dl
negb %dl
movq %r9, %rdx
jo 0x238e
jmp 0x2500
movl %r8d, %r9d
andl $-0x10, %r9d
cmpl $0xe0, %r9d
jne 0x246b
leaq 0x3(%rdx), %r9
cmpq %rsi, %r9
ja 0x2500
movzbl 0x1(%rdi,%rdx), %r10d
movl %r10d, %r11d
andl $-0x40, %r11d
cmpl $0x80, %r11d
jne 0x2500
movb 0x2(%rdi,%rdx), %dl
andb $-0x40, %dl
negb %dl
jno 0x2500
shll $0xc, %r8d
movzwl %r8w, %r8d
andl $0x3f, %r10d
shll $0x6, %r10d
orl %r8d, %r10d
cmpl $0x800, %r10d           # imm = 0x800
jb 0x2500
movq %r9, %rdx
cmpl $0xdfff, %r8d           # imm = 0xDFFF
ja 0x238e
movq %r9, %rdx
cmpl $0xd7ff, %r10d          # imm = 0xD7FF
jbe 0x238e
jmp 0x2500
movl %r8d, %r9d
andl $-0x8, %r9d
cmpl $0xf0, %r9d
jne 0x2500
leaq 0x4(%rdx), %r9
cmpq %rsi, %r9
ja 0x2500
movzbl 0x1(%rdi,%rdx), %r10d
movl %r10d, %r11d
andl $-0x40, %r11d
cmpl $0x80, %r11d
jne 0x2500
movzbl 0x2(%rdi,%rdx), %r11d
movl %r11d, %ebx
andl $-0x40, %ebx
cmpl $0x80, %ebx
jne 0x2500
movzbl 0x3(%rdi,%rdx), %ebx
movl %ebx, %edx
andl $-0x40, %edx
cmpl $0x80, %edx
jne 0x2500
andl $0x7, %r8d
shll $0x12, %r8d
andl $0x3f, %r10d
shll $0xc, %r10d
orl %r8d, %r10d
cmpl $0x10ffff, %r10d        # imm = 0x10FFFF
ja 0x2500
andl $0x3f, %r11d
shll $0x6, %r11d
andl $0x3f, %ebx
orl %r11d, %ebx
orl %r10d, %ebx
movq %r9, %rdx
cmpl $0xfffe, %ebx           # imm = 0xFFFE
ja 0x238e
movl %ecx, %eax
popq %rbx
retq