// File: /workspace/github_cpp_makefile_star2_O3/lemire[P]validateutf8-experiments/benchmarks/fushia.h
// Function: fidl_validate_string_ascii4(unsigned char const*, unsigned long)
fastvalidate::error_code fidl_validate_string_ascii4(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 + 32;
    if (next_pos <= size) { // if it is safe to read 8 more bytes, check that they are ascii 
        uint64_t v1;
        memcpy(&v1, data + pos, sizeof(uint64_t));
        uint64_t v2;
        memcpy(&v2, data + pos + sizeof(uint64_t), sizeof(uint64_t));
        uint64_t v3;
        memcpy(&v3, data + pos + 2 * sizeof(uint64_t), sizeof(uint64_t));
        uint64_t v4;
        memcpy(&v4, data + pos + 3 * sizeof(uint64_t), sizeof(uint64_t));
        uint64_t v{ v1 | v2 | v3 | v4};
        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:
testq %rsi, %rsi
je 0x589f
pushq %rbx
xorl %r8d, %r8d
movabsq $-0x7f7f7f7f7f7f7f80, %rcx # imm = 0x8080808080808080
leaq 0x20(%r8), %rdx
cmpq %rsi, %rdx
jbe 0x56f9
movb (%rdi,%r8), %r10b
jmp 0x5718
movq (%rdi,%r8), %r10
movq 0x8(%rdi,%r8), %rax
orq 0x10(%rdi,%r8), %rax
orq 0x18(%rdi,%r8), %rax
orq %r10, %rax
testq %rcx, %rax
je 0x588f
testb %r10b, %r10b
js 0x5728
incq %r8
movq %r8, %rdx
jmp 0x588f
movzbl %r10b, %r9d
movl %r9d, %eax
andl $-0x20, %eax
cmpl $0xc0, %eax
jne 0x576c
leaq 0x2(%r8), %rdx
movl $0x1, %eax
cmpq %rsi, %rdx
ja 0x589d
testb $0x1e, %r10b
je 0x589d
movb 0x1(%rdi,%r8), %r8b
andb $-0x40, %r8b
negb %r8b
jo 0x588f
jmp 0x589d
movl %r9d, %eax
andl $-0x10, %eax
cmpl $0xe0, %eax
jne 0x57f6
leaq 0x3(%r8), %rdx
movl $0x1, %eax
cmpq %rsi, %rdx
ja 0x589d
movzbl 0x1(%rdi,%r8), %r10d
movl %r10d, %r11d
andl $-0x40, %r11d
cmpl $0x80, %r11d
jne 0x589d
movb 0x2(%rdi,%r8), %r8b
andb $-0x40, %r8b
negb %r8b
jno 0x589d
shll $0xc, %r9d
movzwl %r9w, %r8d
andl $0x3f, %r10d
shll $0x6, %r10d
orl %r8d, %r10d
cmpl $0x800, %r10d           # imm = 0x800
jb 0x589d
cmpl $0xdfff, %r8d           # imm = 0xDFFF
ja 0x588f
cmpl $0xd7ff, %r10d          # imm = 0xD7FF
jbe 0x588f
jmp 0x589d
movl %r9d, %edx
andl $-0x8, %edx
movl $0x1, %eax
cmpl $0xf0, %edx
jne 0x589d
leaq 0x4(%r8), %rdx
cmpq %rsi, %rdx
ja 0x589d
movzbl 0x1(%rdi,%r8), %r10d
movl %r10d, %r11d
andl $-0x40, %r11d
cmpl $0x80, %r11d
jne 0x589d
movzbl 0x2(%rdi,%r8), %r11d
movl %r11d, %ebx
andl $-0x40, %ebx
cmpl $0x80, %ebx
jne 0x589d
movzbl 0x3(%rdi,%r8), %r8d
movl %r8d, %ebx
andl $-0x40, %ebx
cmpl $0x80, %ebx
jne 0x589d
andl $0x7, %r9d
shll $0x12, %r9d
andl $0x3f, %r10d
shll $0xc, %r10d
orl %r9d, %r10d
cmpl $0x10ffff, %r10d        # imm = 0x10FFFF
ja 0x589d
andl $0x3f, %r11d
shll $0x6, %r11d
andl $0x3f, %r8d
orl %r11d, %r8d
orl %r10d, %r8d
cmpl $0xfffe, %r8d           # imm = 0xFFFE
jbe 0x589d
movq %rdx, %r8
cmpq %rsi, %rdx
jb 0x56ea
xorl %eax, %eax
popq %rbx
retq
xorl %eax, %eax
retq