// File: /workspace/github_cpp_makefile_star2_O2/lemire[P]validateutf8-experiments/benchmarks/hoehrmann.h
// Function: shiftless_validate_dfa_utf8_three(signed char const*, unsigned long)
static inline fastvalidate::error_code
shiftless_validate_dfa_utf8_three(const signed char *c, size_t len) {
  if (len < 32) { // if too small, do not bother
    return shiftless_validate_dfa_utf8(c, len);
  }
  size_t middle_point1 = len / 3;
  // backtrack until we find a leading byte
  while (!is_leading(c[middle_point1])) {
    if (middle_point1 == 0) {
      // we have determined that there is an error
      return fastvalidate::error_code::UTF8_ERROR;
    }
    middle_point1 -= 1;
  }
  size_t middle_point2 = 2 * len / 3;
  // backtrack until we find a leading byte
  while (!is_leading(c[middle_point2])) {
    if (middle_point2 == middle_point1) {
      // we have determined that there is an error
      return fastvalidate::error_code::UTF8_ERROR;
    }
    middle_point2 -= 1;
  }

  uint32_t state1 = 0;
  uint32_t state2 = 0;
  uint32_t state3 = 0;

  const unsigned char *cu1 = (const unsigned char *)c;
  const unsigned char *cu2 = (const unsigned char *)(c + middle_point1);
  const unsigned char *cu3 = (const unsigned char *)(c + middle_point2);
  size_t smallest_run = middle_point1;
  if (middle_point2 - middle_point1 < smallest_run) {
    smallest_run = middle_point2 - middle_point1;
  }
  if (len - middle_point2 < smallest_run) {
    smallest_run = len - middle_point2;
  }
  for (size_t i = 0; i < smallest_run; i++) {
    uint32_t byteval1 = (uint32_t)cu1[i];
    uint32_t byteval2 = (uint32_t)cu2[i];
    uint32_t byteval3 = (uint32_t)cu3[i];

    if ((shiftless_updatestate(&state1, byteval1) == SHIFTLESS_UTF8_REJECT) ||
        (shiftless_updatestate(&state2, byteval2) == SHIFTLESS_UTF8_REJECT) ||
        (shiftless_updatestate(&state3, byteval3) == SHIFTLESS_UTF8_REJECT)) {
      return fastvalidate::error_code::UTF8_ERROR;
    }
  }
  if (middle_point1 > smallest_run) {
    for (size_t i = smallest_run; i < middle_point1; i++) {
      uint32_t byteval1 = (uint32_t)cu1[i];
      if (shiftless_updatestate(&state1, byteval1) == SHIFTLESS_UTF8_REJECT) {
        return fastvalidate::error_code::UTF8_ERROR;
      }
    }
  }
  if (shiftless_updatestate(&state1, 0) == SHIFTLESS_UTF8_REJECT) {
    return fastvalidate::error_code::UTF8_ERROR;
  }

  if (middle_point2 - middle_point1 > smallest_run) {
    for (size_t i = smallest_run; i < middle_point2 - middle_point1; i++) {
      uint32_t byteval2 = (uint32_t)cu2[i];
      if (shiftless_updatestate(&state2, byteval2) == SHIFTLESS_UTF8_REJECT) {
        return fastvalidate::error_code::UTF8_ERROR;
      }
    }
  }
  if (shiftless_updatestate(&state2, 0) == SHIFTLESS_UTF8_REJECT) {
    return fastvalidate::error_code::UTF8_ERROR;
  }

  if (len - middle_point2 > smallest_run) {
    for (size_t i = smallest_run; i < len - middle_point2; i++) {
      uint32_t byteval3 = (uint32_t)cu3[i];
      if (shiftless_updatestate(&state3, byteval3) == SHIFTLESS_UTF8_REJECT) {
        return fastvalidate::error_code::UTF8_ERROR;
      }
    }
  }
  if (shiftless_updatestate(&state3, 0) == SHIFTLESS_UTF8_REJECT) {
    return fastvalidate::error_code::UTF8_ERROR;
  }
  return fastvalidate::error_code::SUCCESS;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
movq %rdi, %r13
cmpq $0x1f, %rsi
ja 0x2fed
movq %r13, %rdi
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2eac
movabsq $0x3, %rdi
movq %rsi, %rax
xorl %edx, %edx
divq %rdi
movq %rax, %rcx
leaq (%rsi,%rsi), %rax
xorl %edx, %edx
divq %rdi
movq %rax, %rdx
movq %rax, %r8
subq %rcx, %r8
movq %rcx, %r10
addq %r13, %r10
movl $0x1, %eax
cmpb $-0x41, (%r13,%rcx)
jg 0x3039
subq $0x1, %rcx
jb 0x31c2
incq %r8
decq %r10
jmp 0x301f
movq %rsi, %r11
subq %rdx, %r11
movq %rdx, %r9
addq %r13, %r9
cmpb $-0x41, (%r13,%rdx)
jg 0x3064
cmpq %rcx, %rdx
je 0x31c2
decq %rdx
decq %r8
incq %r11
decq %r9
jmp 0x3045
movq %rdx, %rdi
subq %rcx, %rdi
cmpq %rcx, %rdi
movq %rcx, %rax
movq %rdi, -0x10(%rsp)
cmovbq %rdi, %rax
subq %rdx, %rsi
cmpq %rax, %rsi
movq %rax, -0x20(%rsp)
movq %rsi, -0x18(%rsp)
cmovbq %rsi, %rax
movq %rax, -0x8(%rsp)
cmpq %r8, %rcx
movq %r8, %r12
cmovbq %rcx, %r12
cmpq %r11, %r12
cmovaeq %r11, %r12
negq %r12
xorl %eax, %eax
movq %r13, %rdi
xorl %ebx, %ebx
xorl %esi, %esi
leaq 0x507a(%rip), %r15      # 0x8130
testq %r12, %r12
je 0x311f
movzbl (%rdi), %r14d
leaq 0x4f6a(%rip), %rbp      # 0x8030
movzbl (%r14,%rbp), %r14d
movl %eax, %eax
addq %r14, %rax
movzbl (%r15,%rax), %eax
cmpl $0x10, %eax
je 0x31bd
movzbl (%rdi,%rcx), %r14d
movzbl (%r14,%rbp), %r14d
movl %ebx, %ebx
addq %r14, %rbx
movzbl (%r15,%rbx), %ebx
cmpl $0x10, %ebx
je 0x31bd
movzbl (%rdi,%rdx), %r14d
movzbl (%r14,%rbp), %r14d
movl %esi, %ebp
addq %r14, %rbp
movzbl (%r15,%rbp), %esi
incq %r12
incq %rdi
cmpl $0x10, %esi
jne 0x30b6
jmp 0x31bd
movq -0x8(%rsp), %r14
cmpq %r14, %rcx
leaq 0x4f02(%rip), %r15      # 0x8030
jbe 0x315e
movq %r14, %rdx
leaq 0x4ff6(%rip), %r12      # 0x8130
cmpq %rcx, %rdx
jae 0x3165
movzbl (%r13,%rdx), %edi
movzbl (%rdi,%r15), %edi
movl %eax, %eax
addq %rdi, %rax
movzbl (%r12,%rax), %eax
incq %rdx
cmpl $0x10, %eax
jne 0x313a
jmp 0x31bd
leaq 0x4fcb(%rip), %r12      # 0x8130
movl %eax, %eax
cmpb $0x10, (%rax,%r12)
movq -0x10(%rsp), %r13
je 0x31bd
cmpq %r14, %r13
jbe 0x31b4
cmpq %r8, %rcx
movq %r8, %rax
cmovbq %rcx, %rax
cmpq %r11, %rax
cmovaeq %r11, %rax
addq %rax, %r10
movq %r14, %rax
cmpq %r13, %rax
jae 0x31b4
movzbl (%r10), %edx
movzbl (%rdx,%r15), %edx
movl %ebx, %edi
addq %rdx, %rdi
movzbl (%r12,%rdi), %ebx
incq %rax
incq %r10
cmpl $0x10, %ebx
jne 0x318f
jmp 0x31bd
movl %ebx, %eax
cmpb $0x10, (%rax,%r12)
jne 0x31cd
movl $0x1, %eax
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq -0x18(%rsp), %rdx
cmpq %rdx, -0x20(%rsp)
jae 0x320f
cmpq %r8, %rcx
cmovbq %rcx, %r8
cmpq %r11, %r8
cmovaeq %r11, %r8
addq %r8, %r9
cmpq %rdx, %r14
jae 0x320f
movzbl (%r9), %eax
movzbl (%rax,%r15), %eax
movl %esi, %ecx
addq %rax, %rcx
movzbl (%r12,%rcx), %esi
incq %r14
incq %r9
cmpl $0x10, %esi
jne 0x31ea
jmp 0x31bd
movl %esi, %ecx
xorl %eax, %eax
cmpb $0x10, (%rcx,%r12)
sete %al
jmp 0x31c2