// File: 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:
movq %rdi, -0x10(%rsp)
movq %rsi, -0x18(%rsp)
movq $0x0, -0x20(%rsp)
movq $0x0, -0x28(%rsp)
movl $0x0, -0x2c(%rsp)
movq -0x20(%rsp), %rax
cmpq -0x18(%rsp), %rax
jae 0x2bb6
movq -0x10(%rsp), %rax
movq -0x20(%rsp), %rcx
movb (%rax,%rcx), %al
movb %al, -0x2d(%rsp)
movzbl -0x2d(%rsp), %eax
cmpl $0x80, %eax
jge 0x28c1
movq -0x20(%rsp), %rax
addq $0x1, %rax
movq %rax, -0x20(%rsp)
jmp 0x2884
movzbl -0x2d(%rsp), %eax
andl $0xe0, %eax
cmpl $0xc0, %eax
jne 0x296f
movq -0x20(%rsp), %rax
addq $0x2, %rax
movq %rax, -0x28(%rsp)
movq -0x28(%rsp), %rax
cmpq -0x18(%rsp), %rax
jbe 0x28fd
movl $0x1, -0x4(%rsp)
jmp 0x2bbe
movq -0x10(%rsp), %rax
movq -0x20(%rsp), %rcx
movzbl 0x1(%rax,%rcx), %eax
andl $0xc0, %eax
cmpl $0x80, %eax
je 0x2925
movl $0x1, -0x4(%rsp)
jmp 0x2bbe
movzbl -0x2d(%rsp), %eax
andl $0x1f, %eax
shll $0x6, %eax
movq -0x10(%rsp), %rcx
movq -0x20(%rsp), %rdx
movzbl 0x1(%rcx,%rdx), %ecx
andl $0x3f, %ecx
orl %ecx, %eax
movl %eax, -0x2c(%rsp)
cmpl $0x80, -0x2c(%rsp)
jb 0x295d
movl $0x7ff, %eax            # imm = 0x7FF
cmpl -0x2c(%rsp), %eax
jae 0x296a
movl $0x1, -0x4(%rsp)
jmp 0x2bbe
jmp 0x2ba5
movzbl -0x2d(%rsp), %eax
andl $0xf0, %eax
cmpl $0xe0, %eax
jne 0x2a71
movq -0x20(%rsp), %rax
addq $0x3, %rax
movq %rax, -0x28(%rsp)
movq -0x28(%rsp), %rax
cmpq -0x18(%rsp), %rax
jbe 0x29ab
movl $0x1, -0x4(%rsp)
jmp 0x2bbe
movq -0x10(%rsp), %rax
movq -0x20(%rsp), %rcx
movzbl 0x1(%rax,%rcx), %eax
andl $0xc0, %eax
cmpl $0x80, %eax
je 0x29d3
movl $0x1, -0x4(%rsp)
jmp 0x2bbe
movq -0x10(%rsp), %rax
movq -0x20(%rsp), %rcx
movzbl 0x2(%rax,%rcx), %eax
andl $0xc0, %eax
cmpl $0x80, %eax
je 0x29fb
movl $0x1, -0x4(%rsp)
jmp 0x2bbe
movzbl -0x2d(%rsp), %eax
andl $0xf, %eax
shll $0xc, %eax
movq -0x10(%rsp), %rcx
movq -0x20(%rsp), %rdx
movzbl 0x1(%rcx,%rdx), %ecx
andl $0x3f, %ecx
shll $0x6, %ecx
orl %ecx, %eax
movq -0x10(%rsp), %rcx
movq -0x20(%rsp), %rdx
movzbl 0x2(%rcx,%rdx), %ecx
andl $0x3f, %ecx
orl %ecx, %eax
movl %eax, -0x2c(%rsp)
cmpl $0x800, -0x2c(%rsp)     # imm = 0x800
jb 0x2a5f
movl $0xffff, %eax           # imm = 0xFFFF
cmpl -0x2c(%rsp), %eax
jb 0x2a5f
movl $0xd7ff, %eax           # imm = 0xD7FF
cmpl -0x2c(%rsp), %eax
jae 0x2a6c
cmpl $0xe000, -0x2c(%rsp)    # imm = 0xE000
jae 0x2a6c
movl $0x1, -0x4(%rsp)
jmp 0x2bbe
jmp 0x2ba3
movzbl -0x2d(%rsp), %eax
andl $0xf8, %eax
cmpl $0xf0, %eax
jne 0x2b97
movq -0x20(%rsp), %rax
addq $0x4, %rax
movq %rax, -0x28(%rsp)
movq -0x28(%rsp), %rax
cmpq -0x18(%rsp), %rax
jbe 0x2aad
movl $0x1, -0x4(%rsp)
jmp 0x2bbe
movq -0x10(%rsp), %rax
movq -0x20(%rsp), %rcx
movzbl 0x1(%rax,%rcx), %eax
andl $0xc0, %eax
cmpl $0x80, %eax
je 0x2ad5
movl $0x1, -0x4(%rsp)
jmp 0x2bbe
movq -0x10(%rsp), %rax
movq -0x20(%rsp), %rcx
movzbl 0x2(%rax,%rcx), %eax
andl $0xc0, %eax
cmpl $0x80, %eax
je 0x2afd
movl $0x1, -0x4(%rsp)
jmp 0x2bbe
movq -0x10(%rsp), %rax
movq -0x20(%rsp), %rcx
movzbl 0x3(%rax,%rcx), %eax
andl $0xc0, %eax
cmpl $0x80, %eax
je 0x2b25
movl $0x1, -0x4(%rsp)
jmp 0x2bbe
movzbl -0x2d(%rsp), %eax
andl $0x7, %eax
shll $0x12, %eax
movq -0x10(%rsp), %rcx
movq -0x20(%rsp), %rdx
movzbl 0x1(%rcx,%rdx), %ecx
andl $0x3f, %ecx
shll $0xc, %ecx
orl %ecx, %eax
movq -0x10(%rsp), %rcx
movq -0x20(%rsp), %rdx
movzbl 0x2(%rcx,%rdx), %ecx
andl $0x3f, %ecx
shll $0x6, %ecx
orl %ecx, %eax
movq -0x10(%rsp), %rcx
movq -0x20(%rsp), %rdx
movzbl 0x3(%rcx,%rdx), %ecx
andl $0x3f, %ecx
orl %ecx, %eax
movl %eax, -0x2c(%rsp)
cmpl $0xffff, -0x2c(%rsp)    # imm = 0xFFFF
jb 0x2b8b
movl $0x10ffff, %eax         # imm = 0x10FFFF
cmpl -0x2c(%rsp), %eax
jae 0x2b95
movl $0x1, -0x4(%rsp)
jmp 0x2bbe
jmp 0x2ba1
movl $0x1, -0x4(%rsp)
jmp 0x2bbe
jmp 0x2ba3
jmp 0x2ba5
jmp 0x2ba7
movq -0x28(%rsp), %rax
movq %rax, -0x20(%rsp)
jmp 0x2884
movl $0x0, -0x4(%rsp)
movl -0x4(%rsp), %eax
retq
nopw %cs:(%rax,%rax)