user: Converte this assembly snippet into its equivalent C code:

```asm
<utf_last_valid2>:
  endbr64
  movslq %esi,%rdx
  add    %rdi,%rdx
  cmp    %rdx,%rdi
  jae    L58
  movzbl (%rdi),%ecx
  lea    0x1(%rdi),%rax
  mov    %rdi,%r8
  test   %cl,%cl
  js     L310
L74:
  cmp    %rax,%rdx
  jbe    L25
  movzbl (%rax),%ecx
  mov    %rax,%r8
  add    $0x1,%rax
  test   %cl,%cl
  jns    L74
L310:
  cmp    $0xc1,%cl
  jbe    L40
  cmp    $0xdf,%cl
  jbe    L304
  cmp    $0xe0,%cl
  je     L252
  cmp    $0xec,%cl
  jbe    L296
  cmp    $0xed,%cl
  je     L162
  cmp    $0xef,%cl
  jbe    L296
  cmp    $0xf0,%cl
  je     L87
  cmp    $0xf3,%cl
  ja     L117
  cmp    %rax,%rdx
  jbe    L40
  movzbl (%rax),%esi
  lea    -0x80(%rsi),%ecx
  cmp    $0x3f,%cl
  ja     L40
L289:
  lea    0x1(%rax),%rcx
  cmp    %rcx,%rdx
  jbe    L40
  lea    0x2(%rax),%rcx
  movzbl 0x1(%rax),%eax
L337:
  add    $0xffffff80,%eax
  cmp    $0x3f,%al
  ja     L40
  cmp    %rcx,%rdx
  jbe    L40
  lea    0x1(%rcx),%rax
  movzbl (%rcx),%ecx
  add    $0xffffff80,%ecx
  cmp    $0x3f,%cl
  jbe    L74
L40:
  mov    %r8,%rax
  ret
  nopl   0x0(%rax)
L304:
  cmp    %rax,%rdx
  jbe    L40
  movzbl (%rax),%ecx
  add    $0x1,%rax
  nopl   0x0(%rax)
L266:
  add    $0xffffff80,%ecx
  cmp    $0x3f,%cl
  jbe    L74
  mov    %r8,%rax
  ret
L58:
  mov    %rdi,%r8
  jmp    L40
L252:
  cmp    %rax,%rdx
  jbe    L40
  movzbl (%rax),%esi
  lea    0x60(%rsi),%ecx
  cmp    $0x1f,%cl
  ja     L40
L237:
  lea    0x1(%rax),%rcx
  cmp    %rcx,%rdx
  jbe    L40
  movzbl 0x1(%rax),%ecx
  add    $0x2,%rax
  jmp    L266
L25:
  mov    %rax,%r8
  jmp    L40
L296:
  cmp    %rax,%rdx
  jbe    L40
  lea    0x1(%rax),%rcx
  movzbl (%rax),%eax
  jmp    L337
L162:
  cmp    %rax,%rdx
  jbe    L40
  movzbl (%rax),%esi
  lea    -0x80(%rsi),%ecx
  cmp    $0x1f,%cl
  jbe    L237
  mov    %r8,%rax
  ret
L117:
  cmp    %rax,%rdx
  jbe    L40
  cmp    $0xf4,%cl
  jne    L40
  movzbl (%rax),%esi
  lea    -0x80(%rsi),%ecx
  cmp    $0xf,%cl
  jbe    L289
  mov    %r8,%rax
  ret
L87:
  cmp    %rax,%rdx
  jbe    L40
  movzbl (%rax),%esi
  lea    0x70(%rsi),%ecx
  cmp    $0x2f,%cl
  jbe    L289
  mov    %r8,%rax
  ret
```

assistant: ```c
const char *utf_last_valid2(const char *data, int len) {
  const char *start = data, *end = data + len;
  int state = 0;
  while (data < end) {
    unsigned char octet = *data++;
    switch (state) {
    case 0:
      if (octet <= 0x7F)
        break;
      else if (octet <= 0xC1)
        state = 8;
      else if (octet <= 0xDF)
        state = 1;
      else if (octet == 0xE0)
        state = 2;
      else if (octet <= 0xEC)
        state = 3;
      else if (octet == 0xED)
        state = 4;
      else if (octet <= 0xEF)
        state = 3;
      else if (octet == 0xF0)
        state = 5;
      else if (octet <= 0xF3)
        state = 6;
      else if (octet <= 0xF4)
        state = 7;
      else
        state = 8;
      break;
    case 1:
      if (octet >= 0x80 && octet <= 0xBF)
        state = 0;
      else
        state = 8;
      break;
    case 2:
      if (octet >= 0xA0 && octet <= 0xBF)
        state = 1;
      else
        state = 8;
      break;
    case 3:
      if (octet >= 0x80 && octet <= 0xBF)
        state = 1;
      else
        state = 8;
      break;
    case 4:
      if (octet >= 0x80 && octet <= 0x9F)
        state = 1;
      else
        state = 8;
      break;
    case 5:
      if (octet >= 0x90 && octet <= 0xBF)
        state = 3;
      else
        state = 8;
      break;
    case 6:
      if (octet >= 0x80 && octet <= 0xBF)
        state = 3;
      else
        state = 8;
      break;
    case 7:
      if (octet >= 0x80 && octet <= 0x8F)
        state = 3;
      else
        state = 8;
      break;
    default:
    case 8:
      return start;
    }
    if (state == 0)
      start = data;
  }
  return start;
}
```