// File: /workspace/github_cpp_makefile_star2_O2/lemire[P]validateutf8-experiments/benchmarks/fushia.h
// Function: fidl_validate_string_ascii(unsigned char const*, unsigned long)
fastvalidate::error_code fidl_validate_string_ascii(const unsigned char *data,
                                              uint64_t size) {
  uint64_t pos = 0;
  uint64_t next_pos = 0;
  uint32_t code_point = 0;
  while (pos < size) {

    // check of the next 8 bytes are ascii.
    next_pos = pos + 8;
    if (next_pos <= size) { // if it is safe to read 8 more bytes, check that they are ascii 
        uint64_t v;
        memcpy(&v, data + pos, sizeof(uint64_t));
        if((v & 0x8080808080808080)  == 0) {
          pos = next_pos;
          continue;
        }
    }
    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 %rbp
pushq %rbx
xorl %eax, %eax
movl $0x1, %ecx
movabsq $-0x7f7f7f7f7f7f7f80, %rdx # imm = 0x8080808080808080
xorl %r8d, %r8d
cmpq %rsi, %r8
jae 0x29f2
movq %r8, %r9
addq $0x8, %r8
cmpq %rsi, %r8
jbe 0x2889
movb (%rdi,%r9), %r11b
jmp 0x2892
movq (%rdi,%r9), %r11
testq %rdx, %r11
je 0x286e
testb %r11b, %r11b
js 0x289f
incq %r9
movq %r9, %r8
jmp 0x286e
movzbl %r11b, %r10d
movl %r10d, %r8d
andl $-0x20, %r8d
cmpl $0xc0, %r8d
jne 0x28dd
leaq 0x2(%r9), %r8
cmpq %rsi, %r8
ja 0x29f0
testb $0x1e, %r11b
je 0x29f0
movb 0x1(%rdi,%r9), %r9b
andb $-0x40, %r9b
negb %r9b
jo 0x286e
jmp 0x29f0
movl %r10d, %r8d
andl $-0x10, %r8d
cmpl $0xe0, %r8d
jne 0x2963
leaq 0x3(%r9), %r8
cmpq %rsi, %r8
ja 0x29f0
movzbl 0x1(%rdi,%r9), %r11d
movl %r11d, %ebx
andl $-0x40, %ebx
cmpl $0x80, %ebx
jne 0x29f0
movb 0x2(%rdi,%r9), %r9b
andb $-0x40, %r9b
negb %r9b
jno 0x29f0
shll $0xc, %r10d
movzwl %r10w, %r9d
andl $0x3f, %r11d
shll $0x6, %r11d
orl %r9d, %r11d
cmpl $0x800, %r11d           # imm = 0x800
jb 0x29f0
cmpl $0xdfff, %r9d           # imm = 0xDFFF
ja 0x286e
cmpl $0xd7ff, %r11d          # imm = 0xD7FF
jbe 0x286e
jmp 0x29f0
movl %r10d, %r8d
andl $-0x8, %r8d
cmpl $0xf0, %r8d
jne 0x29f0
leaq 0x4(%r9), %r8
cmpq %rsi, %r8
ja 0x29f0
movzbl 0x1(%rdi,%r9), %r11d
movl %r11d, %ebx
andl $-0x40, %ebx
cmpl $0x80, %ebx
jne 0x29f0
movzbl 0x2(%rdi,%r9), %ebx
movl %ebx, %ebp
andl $-0x40, %ebp
cmpl $0x80, %ebp
jne 0x29f0
movzbl 0x3(%rdi,%r9), %r9d
movl %r9d, %ebp
andl $-0x40, %ebp
cmpl $0x80, %ebp
jne 0x29f0
andl $0x7, %r10d
shll $0x12, %r10d
andl $0x3f, %r11d
shll $0xc, %r11d
orl %r10d, %r11d
cmpl $0x10ffff, %r11d        # imm = 0x10FFFF
ja 0x29f0
andl $0x3f, %ebx
shll $0x6, %ebx
andl $0x3f, %r9d
orl %ebx, %r9d
orl %r11d, %r9d
cmpl $0xfffe, %r9d           # imm = 0xFFFE
ja 0x286e
movl %ecx, %eax
popq %rbx
popq %rbp
retq