// File: lemire[P]validateutf8-experiments/benchmarks/fushia.h
// Function: fidl_validate_string_ascii2(unsigned char const*, unsigned long)
fastvalidate::error_code fidl_validate_string_ascii2(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 + 16;
    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 v{ v1 | v2};
        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 0x33bf
movq -0x20(%rsp), %rax
addq $0x10, %rax
movq %rax, -0x28(%rsp)
movq -0x28(%rsp), %rax
cmpq -0x18(%rsp), %rax
ja 0x309a
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 -0x38(%rsp), %rax
orq -0x40(%rsp), %rax
movq %rax, -0x48(%rsp)
movabsq $-0x7f7f7f7f7f7f7f80, %rax # imm = 0x8080808080808080
andq -0x48(%rsp), %rax
cmpq $0x0, %rax
jne 0x3098
movq -0x28(%rsp), %rax
movq %rax, -0x20(%rsp)
jmp 0x3014
jmp 0x309a
movq -0x10(%rsp), %rax
movq -0x20(%rsp), %rcx
movb (%rax,%rcx), %al
movb %al, -0x49(%rsp)
movzbl -0x49(%rsp), %eax
cmpl $0x80, %eax
jge 0x30ca
movq -0x20(%rsp), %rax
addq $0x1, %rax
movq %rax, -0x20(%rsp)
jmp 0x3014
movzbl -0x49(%rsp), %eax
andl $0xe0, %eax
cmpl $0xc0, %eax
jne 0x3178
movq -0x20(%rsp), %rax
addq $0x2, %rax
movq %rax, -0x28(%rsp)
movq -0x28(%rsp), %rax
cmpq -0x18(%rsp), %rax
jbe 0x3106
movl $0x1, -0x4(%rsp)
jmp 0x33c7
movq -0x10(%rsp), %rax
movq -0x20(%rsp), %rcx
movzbl 0x1(%rax,%rcx), %eax
andl $0xc0, %eax
cmpl $0x80, %eax
je 0x312e
movl $0x1, -0x4(%rsp)
jmp 0x33c7
movzbl -0x49(%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 0x3166
movl $0x7ff, %eax            # imm = 0x7FF
cmpl -0x2c(%rsp), %eax
jae 0x3173
movl $0x1, -0x4(%rsp)
jmp 0x33c7
jmp 0x33ae
movzbl -0x49(%rsp), %eax
andl $0xf0, %eax
cmpl $0xe0, %eax
jne 0x327a
movq -0x20(%rsp), %rax
addq $0x3, %rax
movq %rax, -0x28(%rsp)
movq -0x28(%rsp), %rax
cmpq -0x18(%rsp), %rax
jbe 0x31b4
movl $0x1, -0x4(%rsp)
jmp 0x33c7
movq -0x10(%rsp), %rax
movq -0x20(%rsp), %rcx
movzbl 0x1(%rax,%rcx), %eax
andl $0xc0, %eax
cmpl $0x80, %eax
je 0x31dc
movl $0x1, -0x4(%rsp)
jmp 0x33c7
movq -0x10(%rsp), %rax
movq -0x20(%rsp), %rcx
movzbl 0x2(%rax,%rcx), %eax
andl $0xc0, %eax
cmpl $0x80, %eax
je 0x3204
movl $0x1, -0x4(%rsp)
jmp 0x33c7
movzbl -0x49(%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 0x3268
movl $0xffff, %eax           # imm = 0xFFFF
cmpl -0x2c(%rsp), %eax
jb 0x3268
movl $0xd7ff, %eax           # imm = 0xD7FF
cmpl -0x2c(%rsp), %eax
jae 0x3275
cmpl $0xe000, -0x2c(%rsp)    # imm = 0xE000
jae 0x3275
movl $0x1, -0x4(%rsp)
jmp 0x33c7
jmp 0x33ac
movzbl -0x49(%rsp), %eax
andl $0xf8, %eax
cmpl $0xf0, %eax
jne 0x33a0
movq -0x20(%rsp), %rax
addq $0x4, %rax
movq %rax, -0x28(%rsp)
movq -0x28(%rsp), %rax
cmpq -0x18(%rsp), %rax
jbe 0x32b6
movl $0x1, -0x4(%rsp)
jmp 0x33c7
movq -0x10(%rsp), %rax
movq -0x20(%rsp), %rcx
movzbl 0x1(%rax,%rcx), %eax
andl $0xc0, %eax
cmpl $0x80, %eax
je 0x32de
movl $0x1, -0x4(%rsp)
jmp 0x33c7
movq -0x10(%rsp), %rax
movq -0x20(%rsp), %rcx
movzbl 0x2(%rax,%rcx), %eax
andl $0xc0, %eax
cmpl $0x80, %eax
je 0x3306
movl $0x1, -0x4(%rsp)
jmp 0x33c7
movq -0x10(%rsp), %rax
movq -0x20(%rsp), %rcx
movzbl 0x3(%rax,%rcx), %eax
andl $0xc0, %eax
cmpl $0x80, %eax
je 0x332e
movl $0x1, -0x4(%rsp)
jmp 0x33c7
movzbl -0x49(%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 0x3394
movl $0x10ffff, %eax         # imm = 0x10FFFF
cmpl -0x2c(%rsp), %eax
jae 0x339e
movl $0x1, -0x4(%rsp)
jmp 0x33c7
jmp 0x33aa
movl $0x1, -0x4(%rsp)
jmp 0x33c7
jmp 0x33ac
jmp 0x33ae
jmp 0x33b0
movq -0x28(%rsp), %rax
movq %rax, -0x20(%rsp)
jmp 0x3014
movl $0x0, -0x4(%rsp)
movl -0x4(%rsp), %eax
retq
nopl (%rax)