user: Please show me the C implementation that matches the assembly code listed below:

```asm
<pmesh_get_invariant_index>:
  endbr64
  push   %rbp
  mov    %rsp,%rbp
  push   %r15
  mov    %edi,%r15d
  push   %r14
  push   %r13
  push   %r12
  mov    %edx,%r12d
  push   %rbx
  mov    %rsi,%rbx
  sub    $0x68,%rsp
  mov    %edi,-0x70(%rbp)
  mov    %ecx,-0x44(%rbp)
  mov    %fs:0x28,%rax
  mov    %rax,-0x38(%rbp)
  xor    %eax,%eax
  call   <absmax@plt>
  mov    %eax,%r8d
  test   %eax,%eax
  je     L28
  test   %r15d,%r15d
  jle    L26
  movslq %r15d,%rsi
  xor    %eax,%eax
  nopl   0x0(%rax)
L173:
  bt     %eax,%r12d
  jae    L151
  mov    (%rbx,%rax,4),%edx
  test   %edx,%edx
  js     L282
L151:
  add    $0x1,%rax
  cmp    %rax,%rsi
  jne    L173
  lea    0xf(,%rsi,4),%rax
  mov    %rsp,%rcx
  mov    %rax,%rdx
  and    $0xfffffffffffff000,%rax
  sub    %rax,%rcx
  and    $0xfffffffffffffff0,%rdx
  cmp    %rcx,%rsp
  je     L57
L124:
  sub    $0x1000,%rsp
  orq    $0x0,0xff8(%rsp)
  cmp    %rcx,%rsp
  jne    L124
L57:
  and    $0xfff,%edx
  sub    %rdx,%rsp
  test   %rdx,%rdx
  je     L175
  orq    $0x0,-0x8(%rsp,%rdx,1)
L175:
  mov    %rsp,%rdi
  xor    %edx,%edx
  xor    %r9d,%r9d
  xor    %r13d,%r13d
  mov    %rdi,-0x90(%rbp)
  xor    %r10d,%r10d
  xor    %r14d,%r14d
  mov    $0x1,%r11d
  jmp    L118
  nopl   0x0(%rax,%rax,1)
L165:
  movslq %r9d,%rcx
  mov    %eax,(%rdi,%rcx,4)
  mov    %edx,%ecx
  mov    %r12d,%eax
  add    $0x1,%rdx
  sar    %cl,%eax
  mov    %r9d,%ecx
  add    $0x1,%r9d
  and    $0x1,%eax
  shl    %cl,%eax
  or     %eax,%r13d
  cmp    %rdx,%rsi
  je     L211
L118:
  mov    (%rbx,%rdx,4),%eax
  mov    %eax,%ecx
  neg    %ecx
  cmovs  %eax,%ecx
  cmp    %r8d,%ecx
  jne    L165
  mov    %edx,%ecx
  mov    %r11d,%r15d
  shr    $0x1f,%eax
  add    $0x1,%rdx
  shl    %cl,%r15d
  shl    %cl,%eax
  or     %r15d,%r14d
  or     %eax,%r10d
  cmp    %rdx,%rsi
  jne    L118
L211:
  movzbl -0x70(%rbp),%ecx
  mov    $0x1,%eax
  mov    %rsp,%rsi
  mov    %r14d,-0x6c(%rbp)
  mov    %r13d,-0x84(%rbp)
  shl    %cl,%eax
  movslq %eax,%rcx
  mov    %eax,-0x7c(%rbp)
  lea    0xf(,%rcx,4),%rax
  mov    %rax,%rdx
  and    $0xfffffffffffff000,%rax
  sub    %rax,%rsi
  and    $0xfffffffffffffff0,%rdx
  cmp    %rsi,%rsp
  je     L194
L71:
  sub    $0x1000,%rsp
  orq    $0x0,0xff8(%rsp)
  cmp    %rsi,%rsp
  jne    L71
L194:
  and    $0xfff,%edx
  sub    %rdx,%rsp
  test   %rdx,%rdx
  je     L388
  orq    $0x0,-0x8(%rsp,%rdx,1)
L388:
  mov    %rsp,%r11
  lea    0x0(,%rcx,4),%edx
  xor    %esi,%esi
  mov    %r8d,-0x48(%rbp)
  mov    %r11,%rdi
  mov    %r9d,-0x68(%rbp)
  mov    %r10d,-0x60(%rbp)
  mov    %rcx,-0x58(%rbp)
  mov    %r11,-0x50(%rbp)
  call   <memset@plt>
  mov    -0x7c(%rbp),%eax
  mov    -0x50(%rbp),%r11
  mov    -0x58(%rbp),%rcx
  mov    -0x60(%rbp),%r10d
  test   %eax,%eax
  mov    -0x68(%rbp),%r9d
  mov    -0x48(%rbp),%r8d
  jle    L282
L191:
  lea    (%r8,%r8,1),%eax
  mov    %r8d,-0x78(%rbp)
  xor    %r14d,%r14d
  xor    %r15d,%r15d
  mov    %eax,-0x74(%rbp)
  mov    -0x44(%rbp),%eax
  mov    %r9d,-0x80(%rbp)
  not    %eax
  mov    %eax,%r9d
L85:
  xor    %ebx,%ebx
  jmp    L97
  nopl   0x0(%rax)
L21:
  add    $0x1,%rbx
  cmp    %rbx,%rcx
  je     L182
L97:
  mov    %ebx,%esi
  mov    %r12d,%eax
  mov    %ebx,%edi
  not    %esi
  or     %esi,%eax
  test   %r14d,%eax
  jne    L21
  cmp    %r14d,%r10d
  jne    L356
  cmp    %ebx,-0x6c(%rbp)
  je     L127
L356:
  mov    (%r11,%rbx,4),%r13d
  test   %r13d,%r13d
  je     L327
L16:
  add    %r13d,%r15d
  test   %r9d,%r9d
  jns    L21
  cmp    -0x44(%rbp),%r15d
  jl     L21
L282:
  mov    $0xffffffff,%r8d
L28:
  mov    -0x38(%rbp),%rax
  sub    %fs:0x28,%rax
  jne    L237
  lea    -0x28(%rbp),%rsp
  mov    %r8d,%eax
  pop    %rbx
  pop    %r12
  pop    %r13
  pop    %r14
  pop    %r15
  pop    %rbp
  ret
  nopl   (%rax)
L327:
  mov    %r9d,-0x48(%rbp)
  mov    %rcx,-0x68(%rbp)
  mov    %r11,-0x60(%rbp)
  mov    %r10d,-0x58(%rbp)
  mov    %esi,-0x50(%rbp)
  call   <get_nbits@plt>
  mov    -0x50(%rbp),%edi
  mov    %eax,%r13d
  and    %r12d,%edi
  call   <get_nbits@plt>
  mov    -0x70(%rbp),%esi
  mov    %eax,-0x50(%rbp)
  sub    %r13d,%esi
  sub    %eax,%esi
  mov    -0x74(%rbp),%eax
  lea    -0x1(%rax),%edi
  call   <ipow@plt>
  mov    -0x50(%rbp),%esi
  mov    -0x78(%rbp),%edi
  mov    %eax,%r13d
  call   <ipow@plt>
  mov    -0x60(%rbp),%r11
  mov    -0x48(%rbp),%r9d
  imul   %eax,%r13d
  mov    -0x68(%rbp),%rcx
  mov    -0x58(%rbp),%r10d
  mov    %r13d,(%r11,%rbx,4)
  jmp    L16
  nopw   0x0(%rax,%rax,1)
L182:
  add    $0x1,%r14d
  cmp    %r14d,-0x7c(%rbp)
  jne    L85
  jmp    L282
  nopl   0x0(%rax,%rax,1)
L127:
  mov    -0x44(%rbp),%ebx
  mov    -0x80(%rbp),%r9d
  test   %ebx,%ebx
  js     L187
  mov    %ebx,%ecx
  mov    -0x84(%rbp),%edx
  mov    -0x90(%rbp),%rsi
  mov    %r9d,%edi
  sub    %r15d,%ecx
  call   <pmesh_get_invariant_index>
  cmp    $0xffffffff,%eax
  je     L282
  lea    (%rax,%r15,1),%r8d
  cmp    %r8d,%ebx
  jg     L28
  jmp    L282
  nopl   0x0(%rax,%rax,1)
L187:
  mov    -0x84(%rbp),%edx
  mov    -0x90(%rbp),%rsi
  mov    $0xffffffff,%ecx
  mov    %r9d,%edi
  call   <pmesh_get_invariant_index>
  cmp    $0xffffffff,%eax
  je     L282
  lea    (%r15,%rax,1),%r8d
  jmp    L28
L26:
  movslq -0x70(%rbp),%rax
  mov    %rsp,%rcx
  lea    0xf(,%rax,4),%rax
  mov    %rax,%rdx
  and    $0xfffffffffffff000,%rax
  sub    %rax,%rcx
  and    $0xfffffffffffffff0,%rdx
  cmp    %rcx,%rsp
  je     L201
L307:
  sub    $0x1000,%rsp
  orq    $0x0,0xff8(%rsp)
  cmp    %rcx,%rsp
  jne    L307
L201:
  mov    %rdx,%rax
  and    $0xfff,%eax
  sub    %rax,%rsp
  test   %rax,%rax
  je     L133
  orq    $0x0,-0x8(%rsp,%rax,1)
L133:
  mov    %rsp,%rax
  mov    %rsp,-0x90(%rbp)
  cmp    %rax,%rsp
  je     L181
L67:
  sub    $0x1000,%rsp
  orq    $0x0,0xff8(%rsp)
  cmp    %rax,%rsp
  jne    L67
L181:
  mov    %r8d,-0x58(%rbp)
  sub    $0x10,%rsp
  orq    $0x0,0x8(%rsp)
  mov    $0x4,%edx
  xor    %esi,%esi
  mov    %rsp,%r11
  mov    %r11,%rdi
  mov    %r11,-0x50(%rbp)
  call   <memset@plt>
  mov    -0x50(%rbp),%r11
  mov    -0x58(%rbp),%r8d
  xor    %r9d,%r9d
  movl   $0x0,-0x6c(%rbp)
  xor    %r10d,%r10d
  mov    $0x1,%ecx
  movl   $0x0,-0x84(%rbp)
  movl   $0x1,-0x7c(%rbp)
  jmp    L191
L237:
  call   <__stack_chk_fail@plt>
```

assistant: ```c
ptrdiff_t pmesh_get_invariant_index(int ndim, const ptrdiff_t x[], const uint32_t cmask, ptrdiff_t max_length) {
  int d;
  ptrdiff_t shell = absmax(ndim, x);
  if (shell == 0)
    return 0;
  ptrdiff_t side = shell * 2 + 1;
  ptrdiff_t ind = 0;
  uint32_t icapset = 0;
  uint32_t icapset_x = 0;
  uint32_t icap_x = 0;
  for (d = 0; d < ndim; d++) {
    if ((cmask & (1 << d)) && x[d] < 0) {
      return -1;
    }
  }
  ptrdiff_t x1[ndim];
  uint32_t cmask1 = 0;
  int ndim1;
  for (ndim1 = 0, d = 0; d < ndim; d++) {
    if (abs(x[d]) == shell) {
      icapset_x |= (1 << d);
      icap_x |= (x[d] < 0) * (1 << d);
    } else {
      x1[ndim1] = x[d];
      cmask1 |= (1 << ndim1) * ((cmask >> d) & 1);
      ndim1++;
    }
  }
  int ncapsets = 1 << ndim;
  int ncapsmax = 1 << ndim;
  int icap;
  ptrdiff_t size[ncapsets];
  memset(size, 0, sizeof(size[0]) * ncapsets);
  for (icap = 0; icap < ncapsmax; icap++) {
    for (icapset = 0; icapset < ncapsets; icapset++) {
      if (icap & ~icapset)
        continue;
      if (icap & cmask)
        continue;
      if (icap == icap_x && icapset == icapset_x) {
        ptrdiff_t max_length1 = -1;
        if (max_length >= 0) {
          max_length1 = max_length - ind;
        }
        ptrdiff_t subind = pmesh_get_invariant_index(ndim1, x1, cmask1, max_length1);
        if (subind == -1) {
          return -1;
        }
        ind += subind;
        if (max_length >= 0 && ind >= max_length) {
          return -1;
        }
        return ind;
      }
      if (size[icapset] == 0) {
        int n = get_nbits(icapset);
        int nhalf = get_nbits(cmask & ~icapset);
        size[icapset] = ipow(side - 2, ndim - n - nhalf) * ipow(shell, nhalf);
      }
      ind += size[icapset];
      if (max_length >= 0 && ind >= max_length) {
        return -1;
      }
    }
  }
  return -1;
}
```