// File: /workspace/github_cpp_makefile_star2_O1/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 0x59aa
pushq %rbp
pushq %rbx
xorl %ecx, %ecx
movabsq $-0x7f7f7f7f7f7f7f80, %rax # imm = 0x8080808080808080
leaq 0x20(%rcx), %rdx
cmpq %rsi, %rdx
ja 0x581b
movdqu (%rdi,%rcx), %xmm0
movdqu 0x10(%rdi,%rcx), %xmm1
por %xmm0, %xmm1
pshufd $0xee, %xmm1, %xmm0     # xmm0 = xmm1[2,3,2,3]
por %xmm1, %xmm0
movq %xmm0, %r8
testq %rax, %r8
je 0x5816
movq %rcx, %rdx
movq %rdx, %rcx
je 0x5836
movzbl (%rdi,%rcx), %r9d
testb %r9b, %r9b
js 0x5840
incq %rcx
movl $0x2, %edx
testb $0x1, %dl
jne 0x59ad
cmpq %rsi, %rcx
jb 0x57e8
jmp 0x59a6
movl %r9d, %edx
andl $-0x20, %edx
cmpl $0xc0, %edx
jne 0x5877
leaq 0x2(%rcx), %r8
movl $0x1, %edx
cmpq %rsi, %r8
ja 0x582d
testb $0x1e, %r9b
je 0x582d
movb 0x1(%rdi,%rcx), %r9b
andb $-0x40, %r9b
negb %r9b
jno 0x582d
xorl %edx, %edx
movq %r8, %rcx
jmp 0x582d
movl %r9d, %edx
andl $-0x10, %edx
cmpl $0xe0, %edx
jne 0x58f6
leaq 0x3(%rcx), %r8
movl $0x1, %edx
cmpq %rsi, %r8
ja 0x582d
movzbl 0x1(%rdi,%rcx), %r10d
movl %r10d, %r11d
andl $-0x40, %r11d
cmpl $0x80, %r11d
jne 0x582d
movb 0x2(%rdi,%rcx), %r11b
andb $-0x40, %r11b
negb %r11b
jno 0x582d
shll $0xc, %r9d
movzwl %r9w, %r9d
andl $0x3f, %r10d
shll $0x6, %r10d
orl %r9d, %r10d
cmpl $0x800, %r10d           # imm = 0x800
jb 0x582d
cmpl $0xdfff, %r9d           # imm = 0xDFFF
ja 0x5870
cmpl $0xd7ff, %r10d          # imm = 0xD7FF
ja 0x582d
jmp 0x5870
movl %r9d, %r8d
andl $-0x8, %r8d
movl $0x1, %edx
cmpl $0xf0, %r8d
jne 0x582d
leaq 0x4(%rcx), %r8
cmpq %rsi, %r8
ja 0x582d
movzbl 0x1(%rdi,%rcx), %r10d
movl %r10d, %r11d
andl $-0x40, %r11d
cmpl $0x80, %r11d
jne 0x582d
movzbl 0x2(%rdi,%rcx), %r11d
movl %r11d, %ebx
andl $-0x40, %ebx
cmpl $0x80, %ebx
jne 0x582d
movzbl 0x3(%rdi,%rcx), %ebx
movl %ebx, %ebp
andl $-0x40, %ebp
cmpl $0x80, %ebp
jne 0x582d
andl $0x7, %r9d
shll $0x12, %r9d
andl $0x3f, %r10d
shll $0xc, %r10d
orl %r9d, %r10d
cmpl $0x10ffff, %r10d        # imm = 0x10FFFF
ja 0x582d
andl $0x3f, %r11d
shll $0x6, %r11d
andl $0x3f, %ebx
orl %r11d, %ebx
orl %r10d, %ebx
cmpl $0xffff, %ebx           # imm = 0xFFFF
jae 0x5870
jmp 0x582d
xorl %eax, %eax
jmp 0x59b2
xorl %eax, %eax
retq
movl $0x1, %eax
popq %rbx
popq %rbp
retq