// File: 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:
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 0x2fd5
movq -0x20(%rsp), %rax
addq $0x20, %rax
movq %rax, -0x28(%rsp)
movq -0x28(%rsp), %rax
cmpq -0x18(%rsp), %rax
ja 0x2cb0
movq -0x10(%rsp), %rax
movq -0x20(%rsp), %rcx
movq (%rax,%rcx), %rax
movq %rax, -0x38(%rsp)
movq -0x10(%rsp), %rax
movq -0x20(%rsp), %rcx
movq 0x8(%rax,%rcx), %rax
movq %rax, -0x40(%rsp)
movq -0x10(%rsp), %rax
movq -0x20(%rsp), %rcx
movq 0x10(%rax,%rcx), %rax
movq %rax, -0x48(%rsp)
movq -0x10(%rsp), %rax
movq -0x20(%rsp), %rcx
movq 0x18(%rax,%rcx), %rax
movq %rax, -0x50(%rsp)
movq -0x38(%rsp), %rax
orq -0x40(%rsp), %rax
orq -0x48(%rsp), %rax
orq -0x50(%rsp), %rax
movq %rax, -0x58(%rsp)
movabsq $-0x7f7f7f7f7f7f7f80, %rax # imm = 0x8080808080808080
andq -0x58(%rsp), %rax
cmpq $0x0, %rax
jne 0x2cae
movq -0x28(%rsp), %rax
movq %rax, -0x20(%rsp)
jmp 0x2bf4
jmp 0x2cb0
movq -0x10(%rsp), %rax
movq -0x20(%rsp), %rcx
movb (%rax,%rcx), %al
movb %al, -0x59(%rsp)
movzbl -0x59(%rsp), %eax
cmpl $0x80, %eax
jge 0x2ce0
movq -0x20(%rsp), %rax
addq $0x1, %rax
movq %rax, -0x20(%rsp)
jmp 0x2bf4
movzbl -0x59(%rsp), %eax
andl $0xe0, %eax
cmpl $0xc0, %eax
jne 0x2d8e
movq -0x20(%rsp), %rax
addq $0x2, %rax
movq %rax, -0x28(%rsp)
movq -0x28(%rsp), %rax
cmpq -0x18(%rsp), %rax
jbe 0x2d1c
movl $0x1, -0x4(%rsp)
jmp 0x2fdd
movq -0x10(%rsp), %rax
movq -0x20(%rsp), %rcx
movzbl 0x1(%rax,%rcx), %eax
andl $0xc0, %eax
cmpl $0x80, %eax
je 0x2d44
movl $0x1, -0x4(%rsp)
jmp 0x2fdd
movzbl -0x59(%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 0x2d7c
movl $0x7ff, %eax            # imm = 0x7FF
cmpl -0x2c(%rsp), %eax
jae 0x2d89
movl $0x1, -0x4(%rsp)
jmp 0x2fdd
jmp 0x2fc4
movzbl -0x59(%rsp), %eax
andl $0xf0, %eax
cmpl $0xe0, %eax
jne 0x2e90
movq -0x20(%rsp), %rax
addq $0x3, %rax
movq %rax, -0x28(%rsp)
movq -0x28(%rsp), %rax
cmpq -0x18(%rsp), %rax
jbe 0x2dca
movl $0x1, -0x4(%rsp)
jmp 0x2fdd
movq -0x10(%rsp), %rax
movq -0x20(%rsp), %rcx
movzbl 0x1(%rax,%rcx), %eax
andl $0xc0, %eax
cmpl $0x80, %eax
je 0x2df2
movl $0x1, -0x4(%rsp)
jmp 0x2fdd
movq -0x10(%rsp), %rax
movq -0x20(%rsp), %rcx
movzbl 0x2(%rax,%rcx), %eax
andl $0xc0, %eax
cmpl $0x80, %eax
je 0x2e1a
movl $0x1, -0x4(%rsp)
jmp 0x2fdd
movzbl -0x59(%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 0x2e7e
movl $0xffff, %eax           # imm = 0xFFFF
cmpl -0x2c(%rsp), %eax
jb 0x2e7e
movl $0xd7ff, %eax           # imm = 0xD7FF
cmpl -0x2c(%rsp), %eax
jae 0x2e8b
cmpl $0xe000, -0x2c(%rsp)    # imm = 0xE000
jae 0x2e8b
movl $0x1, -0x4(%rsp)
jmp 0x2fdd
jmp 0x2fc2
movzbl -0x59(%rsp), %eax
andl $0xf8, %eax
cmpl $0xf0, %eax
jne 0x2fb6
movq -0x20(%rsp), %rax
addq $0x4, %rax
movq %rax, -0x28(%rsp)
movq -0x28(%rsp), %rax
cmpq -0x18(%rsp), %rax
jbe 0x2ecc
movl $0x1, -0x4(%rsp)
jmp 0x2fdd
movq -0x10(%rsp), %rax
movq -0x20(%rsp), %rcx
movzbl 0x1(%rax,%rcx), %eax
andl $0xc0, %eax
cmpl $0x80, %eax
je 0x2ef4
movl $0x1, -0x4(%rsp)
jmp 0x2fdd
movq -0x10(%rsp), %rax
movq -0x20(%rsp), %rcx
movzbl 0x2(%rax,%rcx), %eax
andl $0xc0, %eax
cmpl $0x80, %eax
je 0x2f1c
movl $0x1, -0x4(%rsp)
jmp 0x2fdd
movq -0x10(%rsp), %rax
movq -0x20(%rsp), %rcx
movzbl 0x3(%rax,%rcx), %eax
andl $0xc0, %eax
cmpl $0x80, %eax
je 0x2f44
movl $0x1, -0x4(%rsp)
jmp 0x2fdd
movzbl -0x59(%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 0x2faa
movl $0x10ffff, %eax         # imm = 0x10FFFF
cmpl -0x2c(%rsp), %eax
jae 0x2fb4
movl $0x1, -0x4(%rsp)
jmp 0x2fdd
jmp 0x2fc0
movl $0x1, -0x4(%rsp)
jmp 0x2fdd
jmp 0x2fc2
jmp 0x2fc4
jmp 0x2fc6
movq -0x28(%rsp), %rax
movq %rax, -0x20(%rsp)
jmp 0x2bf4
movl $0x0, -0x4(%rsp)
movl -0x4(%rsp), %eax
retq
nopw %cs:(%rax,%rax)