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

```asm
<ff_dm_quad>:
  endbr64
  push   %r15
  push   %r14
  push   %r13
  push   %r12
  push   %rbp
  push   %rbx
  sub    $0xa8,%rsp
  mov    0xe0(%rsp),%rax
  mov    %edi,0x64(%rsp)
  mov    %rsi,0x20(%rsp)
  mov    %rax,0x48(%rsp)
  mov    0xe8(%rsp),%rax
  mov    %rdx,0x68(%rsp)
  mov    %rcx,0x70(%rsp)
  mov    %r8,0x38(%rsp)
  mov    %r9,0x40(%rsp)
  mov    %rax,0x50(%rsp)
  movsd  %xmm0,0x30(%rsp)
  mov    %fs:0x28,%rax
  mov    %rax,0x98(%rsp)
  xor    %eax,%eax
  test   %edi,%edi
  jle    L63
  cmp    $0x1,%edi
  je     L63
  movslq %edi,%rdi
  mov    $0x1,%r15d
  movq   $0x0,0x10(%rsp)
  lea    0x80(%rsp),%r13
  mov    %rdi,0x78(%rsp)
  mov    %edi,0x60(%rsp)
  nopl   0x0(%rax)
L76:
  mov    0x20(%rsp),%rdi
  lea    (%r15,%r15,2),%eax
  mov    %r15d,0x5c(%rsp)
  xor    %ebx,%ebx
  cltq
  lea    (%rdi,%rax,8),%r14
  movslq 0x60(%rsp),%rax
  mov    0x68(%rsp),%rdi
  shl    $0x3,%rax
  lea    (%rdi,%rax,1),%rbp
  add    0x70(%rsp),%rax
  mov    %rax,0x28(%rsp)
  jmp    L94
  nopl   0x0(%rax)
L119:
  add    $0x1,%rbx
  cmp    %rbx,%r15
  je     L101
L94:
  movsd  0x0(%rbp,%rbx,8),%xmm2
  pxor   %xmm5,%xmm5
  mov    %ebx,%r12d
  comisd %xmm5,%xmm2
  movsd  %xmm2,0x8(%rsp)
  jbe    L119
  mov    0x28(%rsp),%rax
  mov    0x20(%rsp),%rdx
  mov    %r14,%rdi
  mov    0x48(%rsp),%r9
  mov    0x40(%rsp),%r8
  movsd  (%rax,%rbx,8),%xmm3
  lea    (%rbx,%rbx,2),%eax
  mov    0x38(%rsp),%rcx
  cltq
  lea    (%rdx,%rax,8),%rsi
  mov    %r13,%rdx
  movsd  %xmm3,0x18(%rsp)
  call   <calc_delta@plt>
  pxor   %xmm7,%xmm7
  movsd  0x80(%rsp),%xmm0
  movsd  0x18(%rsp),%xmm3
  movsd  0x8(%rsp),%xmm2
  mulsd  %xmm0,%xmm0
  movapd %xmm0,%xmm1
  movsd  0x88(%rsp),%xmm0
  mulsd  %xmm0,%xmm0
  addsd  %xmm0,%xmm1
  movsd  0x90(%rsp),%xmm0
  mulsd  %xmm0,%xmm0
  addsd  %xmm1,%xmm0
  ucomisd %xmm0,%xmm7
  ja     L22
  sqrtsd %xmm0,%xmm0
L106:
  movsd  0x30(%rsp),%xmm6
  movapd %xmm0,%xmm4
  mov    0x50(%rsp),%rcx
  subsd  %xmm2,%xmm4
  movapd %xmm6,%xmm1
  mulsd  %xmm3,%xmm1
  mulsd  %xmm4,%xmm1
  mulsd  %xmm4,%xmm1
  addsd  0x10(%rsp),%xmm1
  movsd  %xmm1,0x10(%rsp)
  test   %rcx,%rcx
  je     L119
  movapd %xmm6,%xmm1
  mov    0x5c(%rsp),%edi
  mov    %r13,%rdx
  mov    %r12d,%esi
  addsd  %xmm6,%xmm1
  add    $0x1,%rbx
  mulsd  %xmm3,%xmm1
  mulsd  %xmm4,%xmm1
  divsd  %xmm0,%xmm1
  movapd %xmm1,%xmm0
  call   <add_grad@plt>
  cmp    %rbx,%r15
  jne    L94
L101:
  mov    0x64(%rsp),%edi
  add    $0x1,%r15
  add    %edi,0x60(%rsp)
  cmp    0x78(%rsp),%r15
  jne    L76
L52:
  mov    0x98(%rsp),%rax
  sub    %fs:0x28,%rax
  jne    L85
  movsd  0x10(%rsp),%xmm0
  add    $0xa8,%rsp
  pop    %rbx
  pop    %rbp
  pop    %r12
  pop    %r13
  pop    %r14
  pop    %r15
  ret
L63:
  pxor   %xmm7,%xmm7
  movsd  %xmm7,0x10(%rsp)
  jmp    L52
L22:
  movsd  %xmm3,0x18(%rsp)
  movsd  %xmm2,0x8(%rsp)
  call   <sqrt@plt>
  movsd  0x18(%rsp),%xmm3
  movsd  0x8(%rsp),%xmm2
  jmp    L106
L85:
  call   <__stack_chk_fail@plt>
```

assistant: ```c
double ff_dm_quad(int n, double *cor, double *dm0, double *dmk, double amp, double *unitcell, double *unitcell_reciproke, int *unitcell_active, double *gradient) {
  int i, j;
  double d, d0, k, tmp, result;
  double delta[3];
  result = 0.0;
  for (i = 0; i < n; i++) {
    for (j = 0; j < i; j++) {
      d0 = dm0[i * n + j];
      k = dmk[i * n + j];
      if (d0 > 0) {
        calc_delta(cor + 3 * i, cor + 3 * j, delta, unitcell, unitcell_reciproke, unitcell_active);
        tmp = delta[0];
        d = tmp * tmp;
        tmp = delta[1];
        d += tmp * tmp;
        tmp = delta[2];
        d += tmp * tmp;
        d = sqrt(d);
        tmp = (d - d0);
        result += amp * k * tmp * tmp;
        if (gradient != NULL) {
          tmp = 2 * amp * k * tmp / d;
          add_grad(i, j, tmp, delta, gradient);
        }
      }
    }
  }
  return result;
}
```