// File: 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:
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 0x3779
movq -0x20(%rsp), %rax
addq $0x8, %rax
movq %rax, -0x28(%rsp)
movq -0x28(%rsp), %rax
cmpq -0x18(%rsp), %rax
ja 0x3454
movq -0x10(%rsp), %rax
movq -0x20(%rsp), %rcx
movq (%rax,%rcx), %rax
movq %rax, -0x38(%rsp)
movabsq $-0x7f7f7f7f7f7f7f80, %rax # imm = 0x8080808080808080
andq -0x38(%rsp), %rax
cmpq $0x0, %rax
jne 0x3452
movq -0x28(%rsp), %rax
movq %rax, -0x20(%rsp)
jmp 0x33f4
jmp 0x3454
movq -0x10(%rsp), %rax
movq -0x20(%rsp), %rcx
movb (%rax,%rcx), %al
movb %al, -0x39(%rsp)
movzbl -0x39(%rsp), %eax
cmpl $0x80, %eax
jge 0x3484
movq -0x20(%rsp), %rax
addq $0x1, %rax
movq %rax, -0x20(%rsp)
jmp 0x33f4
movzbl -0x39(%rsp), %eax
andl $0xe0, %eax
cmpl $0xc0, %eax
jne 0x3532
movq -0x20(%rsp), %rax
addq $0x2, %rax
movq %rax, -0x28(%rsp)
movq -0x28(%rsp), %rax
cmpq -0x18(%rsp), %rax
jbe 0x34c0
movl $0x1, -0x4(%rsp)
jmp 0x3781
movq -0x10(%rsp), %rax
movq -0x20(%rsp), %rcx
movzbl 0x1(%rax,%rcx), %eax
andl $0xc0, %eax
cmpl $0x80, %eax
je 0x34e8
movl $0x1, -0x4(%rsp)
jmp 0x3781
movzbl -0x39(%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 0x3520
movl $0x7ff, %eax            # imm = 0x7FF
cmpl -0x2c(%rsp), %eax
jae 0x352d
movl $0x1, -0x4(%rsp)
jmp 0x3781
jmp 0x3768
movzbl -0x39(%rsp), %eax
andl $0xf0, %eax
cmpl $0xe0, %eax
jne 0x3634
movq -0x20(%rsp), %rax
addq $0x3, %rax
movq %rax, -0x28(%rsp)
movq -0x28(%rsp), %rax
cmpq -0x18(%rsp), %rax
jbe 0x356e
movl $0x1, -0x4(%rsp)
jmp 0x3781
movq -0x10(%rsp), %rax
movq -0x20(%rsp), %rcx
movzbl 0x1(%rax,%rcx), %eax
andl $0xc0, %eax
cmpl $0x80, %eax
je 0x3596
movl $0x1, -0x4(%rsp)
jmp 0x3781
movq -0x10(%rsp), %rax
movq -0x20(%rsp), %rcx
movzbl 0x2(%rax,%rcx), %eax
andl $0xc0, %eax
cmpl $0x80, %eax
je 0x35be
movl $0x1, -0x4(%rsp)
jmp 0x3781
movzbl -0x39(%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 0x3622
movl $0xffff, %eax           # imm = 0xFFFF
cmpl -0x2c(%rsp), %eax
jb 0x3622
movl $0xd7ff, %eax           # imm = 0xD7FF
cmpl -0x2c(%rsp), %eax
jae 0x362f
cmpl $0xe000, -0x2c(%rsp)    # imm = 0xE000
jae 0x362f
movl $0x1, -0x4(%rsp)
jmp 0x3781
jmp 0x3766
movzbl -0x39(%rsp), %eax
andl $0xf8, %eax
cmpl $0xf0, %eax
jne 0x375a
movq -0x20(%rsp), %rax
addq $0x4, %rax
movq %rax, -0x28(%rsp)
movq -0x28(%rsp), %rax
cmpq -0x18(%rsp), %rax
jbe 0x3670
movl $0x1, -0x4(%rsp)
jmp 0x3781
movq -0x10(%rsp), %rax
movq -0x20(%rsp), %rcx
movzbl 0x1(%rax,%rcx), %eax
andl $0xc0, %eax
cmpl $0x80, %eax
je 0x3698
movl $0x1, -0x4(%rsp)
jmp 0x3781
movq -0x10(%rsp), %rax
movq -0x20(%rsp), %rcx
movzbl 0x2(%rax,%rcx), %eax
andl $0xc0, %eax
cmpl $0x80, %eax
je 0x36c0
movl $0x1, -0x4(%rsp)
jmp 0x3781
movq -0x10(%rsp), %rax
movq -0x20(%rsp), %rcx
movzbl 0x3(%rax,%rcx), %eax
andl $0xc0, %eax
cmpl $0x80, %eax
je 0x36e8
movl $0x1, -0x4(%rsp)
jmp 0x3781
movzbl -0x39(%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 0x374e
movl $0x10ffff, %eax         # imm = 0x10FFFF
cmpl -0x2c(%rsp), %eax
jae 0x3758
movl $0x1, -0x4(%rsp)
jmp 0x3781
jmp 0x3764
movl $0x1, -0x4(%rsp)
jmp 0x3781
jmp 0x3766
jmp 0x3768
jmp 0x376a
movq -0x28(%rsp), %rax
movq %rax, -0x20(%rsp)
jmp 0x33f4
movl $0x0, -0x4(%rsp)
movl -0x4(%rsp), %eax
retq
nopw %cs:(%rax,%rax)