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

```asm
<twoway_memmem>:
  endbr64
  push   %r15
  push   %r14
  push   %r13
  push   %r12
  push   %rbp
  push   %rbx
  sub    $0x848,%rsp
  mov    %rdi,%rbp
  mov    %rsi,%r14
  mov    %rdx,%r12
  mov    %rcx,%rbx
  mov    %fs:0x28,%rax
  mov    %rax,0x838(%rsp)
  xor    %eax,%eax
  movq   $0x0,0x10(%rsp)
  movq   $0x0,0x18(%rsp)
  movq   $0x0,0x20(%rsp)
  movq   $0x0,0x28(%rsp)
  test   %rcx,%rcx
  je     L242
  mov    $0x1,%edi
L184:
  movzbl (%r12,%rax,1),%ecx
  mov    %ecx,%edx
  shr    $0x6,%dl
  movzbl %dl,%edx
  mov    %rdi,%rsi
  shl    %cl,%rsi
  or     %rsi,0x10(%rsp,%rdx,8)
  add    $0x1,%rax
  movzbl %cl,%ecx
  mov    %rax,0x30(%rsp,%rcx,8)
  cmp    %rbx,%rax
  jne    L184
  cmp    $0x1,%rbx
  jbe    L242
  mov    $0x1,%r8d
  mov    $0x1,%edx
  mov    $0x0,%r9d
  mov    $0xffffffffffffffff,%rdi
  mov    $0x1,%eax
  mov    $0x1,%r10d
  jmp    L212
L287:
  cmp    %r8,%rdx
  je     L258
  add    $0x1,%rdx
  jmp    L0
L258:
  add    %r8,%r9
  mov    $0x1,%edx
  jmp    L0
L394:
  mov    %r9,%rdi
  lea    0x1(%r9),%r9
  mov    %r10,%r8
  mov    $0x1,%edx
L0:
  lea    (%r9,%rdx,1),%rax
  cmp    %rbx,%rax
  jae    L189
L212:
  lea    (%r12,%rdi,1),%rcx
  movzbl (%rcx,%rdx,1),%esi
  movzbl (%r12,%rax,1),%ecx
  cmp    %cl,%sil
  je     L287
  jbe    L394
  mov    %rax,%r8
  sub    %rdi,%r8
  mov    %rax,%r9
  mov    $0x1,%edx
  jmp    L0
L189:
  mov    $0x1,%r13d
  mov    $0x1,%edx
  mov    $0x0,%r9d
  movq   $0xffffffffffffffff,0x8(%rsp)
  mov    $0x1,%eax
  jmp    L154
L253:
  cmp    %r13,%rdx
  je     L265
  add    $0x1,%rdx
  jmp    L146
L265:
  add    %r13,%r9
  mov    $0x1,%edx
  jmp    L146
L181:
  mov    %r9,0x8(%rsp)
  lea    0x1(%r9),%r9
  mov    $0x1,%r13d
  mov    $0x1,%edx
L146:
  lea    (%r9,%rdx,1),%rax
  cmp    %rbx,%rax
  jae    L221
L154:
  mov    0x8(%rsp),%rsi
  lea    (%r12,%rsi,1),%rcx
  movzbl (%rcx,%rdx,1),%esi
  movzbl (%r12,%rax,1),%ecx
  cmp    %cl,%sil
  je     L253
  jae    L181
  mov    %rax,%r13
  sub    0x8(%rsp),%r13
  mov    %rax,%r9
  mov    $0x1,%edx
  jmp    L146
L221:
  mov    0x8(%rsp),%rsi
  lea    0x1(%rsi),%rdx
  lea    0x1(%rdi),%rax
  cmp    %rax,%rdx
  cmova  %rsi,%rdi
  mov    %rdi,0x8(%rsp)
  cmovbe %r8,%r13
L227:
  mov    0x8(%rsp),%rdi
  lea    0x1(%rdi),%r15
  lea    (%r12,%r13,1),%rsi
  mov    %r15,%rdx
  mov    %r12,%rdi
  call   <memcmp@plt>
  mov    %rbx,%rdx
  sub    %r13,%rdx
  test   %rax,%rax
  je     L312
  lea    -0x1(%rbx),%rax
  mov    0x8(%rsp),%rdi
  sub    %rdi,%rax
  cmp    %rdi,%rax
  cmovb  %rdi,%rax
  lea    0x1(%rax),%r13
  mov    $0x0,%edx
L312:
  mov    %r14,%rax
  sub    %rbp,%rax
  cmp    %rax,%rbx
  ja     L174
  mov    $0x0,%r9d
  mov    %rdx,%r8
  mov    $0x0,%esi
  mov    %rbx,%rdi
  sub    %r13,%rdi
  jmp    L177
L97:
  add    %rcx,%rbp
  mov    %rsi,%r9
L403:
  mov    %r14,%rax
  sub    %rbp,%rax
  cmp    %rbx,%rax
  jb     L14
L177:
  movzbl -0x1(%rbp,%rbx,1),%ecx
  mov    %ecx,%eax
  shr    $0x6,%al
  movzbl %al,%eax
  mov    0x10(%rsp,%rax,8),%rax
  shr    %cl,%rax
  and    $0x1,%eax
  je     L130
  movzbl %cl,%ecx
  mov    %rbx,%rax
  sub    0x30(%rsp,%rcx,8),%rax
  mov    %rax,%rcx
  je     L276
  test   %rdx,%rdx
  setne  %r10b
  test   %r9,%r9
  setne  %al
  test   %al,%r10b
  je     L97
  cmp    %rcx,%r13
  cmova  %rdi,%rcx
  jmp    L97
L130:
  add    %rbx,%rbp
  mov    %rax,%r9
  jmp    L403
L276:
  cmp    %r9,%r15
  mov    %r9,%rax
  cmovae %r15,%rax
  cmp    %rax,%rbx
  jbe    L268
L309:
  movzbl 0x0(%rbp,%rax,1),%r11d
  cmp    %r11b,(%r12,%rax,1)
  jne    L347
  add    $0x1,%rax
  cmp    %rax,%rbx
  jne    L309
  mov    %r15,%rax
L144:
  cmp    %r9,%rax
  jbe    L306
  sub    $0x1,%rax
  movzbl 0x0(%rbp,%rax,1),%ecx
  cmp    %cl,(%r12,%rax,1)
  je     L144
  add    %r13,%rbp
  mov    %r8,%r9
  jmp    L403
L268:
  mov    %r15,%rax
  jmp    L144
L347:
  cmp    %rax,%rbx
  jbe    L107
  sub    0x8(%rsp),%rax
  add    %rax,%rbp
  mov    %rcx,%r9
  jmp    L403
L107:
  mov    %r15,%rax
  jmp    L144
L174:
  mov    $0x0,%ebp
  jmp    L306
L14:
  mov    $0x0,%ebp
L306:
  mov    0x838(%rsp),%rax
  sub    %fs:0x28,%rax
  jne    L237
  mov    %rbp,%rax
  add    $0x848,%rsp
  pop    %rbx
  pop    %rbp
  pop    %r12
  pop    %r13
  pop    %r14
  pop    %r15
  ret
L242:
  movq   $0xffffffffffffffff,0x8(%rsp)
  mov    $0x1,%r13d
  jmp    L227
L237:
  call   <__stack_chk_fail@plt>
```

assistant: ```c
char *twoway_memmem(const unsigned char *h, const unsigned char *z, const unsigned char *n, size_t l) {
  size_t i, ip, jp, k, p, ms, p0, mem, mem0;
  size_t byteset[32 / sizeof(size_t)] = {0};
  size_t shift[256];
  for (i = 0; i < l; i++)
    ((byteset)[(size_t)(n[i]) / (8 * sizeof *(byteset))] |= (size_t)1 << ((size_t)(n[i]) % (8 * sizeof *(byteset)))), shift[n[i]] = i + 1;
  ip = -1;
  jp = 0;
  k = p = 1;
  while (jp + k < l) {
    if (n[ip + k] == n[jp + k]) {
      if (k == p) {
        jp += p;
        k = 1;
      } else
        k++;
    } else if (n[ip + k] > n[jp + k]) {
      jp += k;
      k = 1;
      p = jp - ip;
    } else {
      ip = jp++;
      k = p = 1;
    }
  }
  ms = ip;
  p0 = p;
  ip = -1;
  jp = 0;
  k = p = 1;
  while (jp + k < l) {
    if (n[ip + k] == n[jp + k]) {
      if (k == p) {
        jp += p;
        k = 1;
      } else
        k++;
    } else if (n[ip + k] < n[jp + k]) {
      jp += k;
      k = 1;
      p = jp - ip;
    } else {
      ip = jp++;
      k = p = 1;
    }
  }
  if (ip + 1 > ms + 1)
    ms = ip;
  else
    p = p0;
  if (memcmp(n, n + p, ms + 1)) {
    mem0 = 0;
    p = ((ms) > (l - ms - 1) ? (ms) : (l - ms - 1)) + 1;
  } else
    mem0 = l - p;
  mem = 0;
  for (;;) {
    if (z - h < l)
      return 0;
    if (((byteset)[(size_t)(h[l - 1]) / (8 * sizeof *(byteset))] & (size_t)1 << ((size_t)(h[l - 1]) % (8 * sizeof *(byteset))))) {
      k = l - shift[h[l - 1]];
      if (k) {
        if (mem0 && mem && k < p)
          k = l - p;
        h += k;
        mem = 0;
        continue;
      }
    } else {
      h += l;
      mem = 0;
      continue;
    }
    for (k = ((ms + 1) > (mem) ? (ms + 1) : (mem)); k < l && n[k] == h[k]; k++)
      ;
    if (k < l) {
      h += k - ms;
      mem = 0;
      continue;
    }
    for (k = ms + 1; k > mem && n[k - 1] == h[k - 1]; k--)
      ;
    if (k <= mem)
      return (char *)h;
    h += p;
    mem = mem0;
  }
}
```