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

```asm
<nj_sum_of_branch_lengths>:
  endbr64
  push   %r15
  push   %r14
  push   %r13
  push   %r12
  push   %rbp
  push   %rbx
  sub    $0x58,%rsp
  mov    %rdi,%r14
  mov    %rsi,%rbx
  mov    %edx,%ebp
  mov    %ecx,%r13d
  mov    %r8d,%r12d
  mov    %r9,0x10(%rsp)
  mov    0x90(%rsp),%rax
  mov    %rax,0x18(%rsp)
  mov    %fs:0x28,%rax
  mov    %rax,0x48(%rsp)
  xor    %eax,%eax
  mov    %edx,0x28(%rsp)
  mov    %ecx,0x2c(%rsp)
  movslq %edx,%rax
  movslq %ecx,%rdx
  mov    (%rdi,%rax,8),%rax
  movsd  D4(%rip),%xmm0
  mulsd  (%rax,%rdx,8),%xmm0
  movsd  %xmm0,0x8(%rsp)
  mov    $0x0,%r15d
  jmp    L247
L138:
  add    $0x1,%rax
  cmp    %rsi,%rax
  je     L126
L278:
  cmpl   $0x0,(%rbx,%rax,4)
  je     L138
  cmp    %eax,%ebp
  je     L138
  cmp    %eax,%r13d
  je     L138
  movslq 0x28(%rsp,%rdx,4),%rdi
  mov    (%r14,%rdi,8),%rdi
  movsd  (%rdi,%rax,8),%xmm0
  addsd  0x30(%rsp,%rdx,8),%xmm0
  movsd  %xmm0,0x30(%rsp,%rdx,8)
  add    $0x1,%ecx
  jmp    L138
L37:
  add    $0x1,%r15
  cmp    %r15d,%r12d
  jle    L86
L155:
  mov    %r15d,%esi
  cmpl   $0x0,(%rbx,%r15,4)
  je     L37
  lea    D42(%rip),%rdi
  mov    $0x0,%eax
  call   <printf@plt>
  jmp    L37
L86:
  test   %r12d,%r12d
  mov    $0x1,%r15d
  cmovg  %r12d,%r15d
  mov    %r12d,0x4(%rsp)
L91:
  mov    $0xa,%edi
  call   <putchar@plt>
  mov    $0x1,%edi
  call   <exit@plt>
  mov    $0x0,%ecx
  jmp    L11
L140:
  movsd  0x30(%rsp),%xmm2
  movsd  0x38(%rsp),%xmm0
  movapd %xmm2,%xmm1
  addsd  %xmm0,%xmm1
  mulsd  D4(%rip),%xmm1
  addsd  0x8(%rsp),%xmm1
  mov    0x10(%rsp),%rax
  movsd  %xmm2,(%rax)
  mov    0x18(%rsp),%rax
  movsd  %xmm0,(%rax)
  test   %r12d,%r12d
  jle    L308
  mov    $0x1,%ecx
  mov    $0x0,%edx
  mov    $0x0,%esi
  pxor   %xmm0,%xmm0
  lea    -0x1(%r12),%r8d
  mov    0x4(%rsp),%eax
  jmp    L39
L305:
  mov    (%r14,%rdx,8),%rdi
  addsd  (%rdi,%rax,8),%xmm0
  add    $0x1,%esi
L302:
  add    $0x1,%rax
  cmp    %eax,%r12d
  jle    L58
L36:
  cmpl   $0x0,(%rbx,%rax,4)
  je     L302
  cmp    %eax,%ebp
  je     L302
  cmp    %eax,%r13d
  jne    L305
  jmp    L302
L205:
  cmp    %ecx,%r12d
  jle    L303
L14:
  add    $0x1,%rdx
  add    $0x1,%ecx
L39:
  cmpl   $0x0,(%rbx,%rdx,4)
  je     L205
  cmp    %edx,%ebp
  je     L205
  cmp    %eax,%r13d
  je     L205
  cmp    %ecx,%r12d
  jle    L303
  movslq %ecx,%rax
  jmp    L36
L308:
  mov    $0x0,%esi
  pxor   %xmm0,%xmm0
L303:
  pxor   %xmm2,%xmm2
  cvtsi2sd %esi,%xmm2
  divsd  %xmm2,%xmm0
  addsd  %xmm1,%xmm0
  mov    0x48(%rsp),%rax
  sub    %fs:0x28,%rax
  jne    L191
  add    $0x58,%rsp
  pop    %rbx
  pop    %rbp
  pop    %r12
  pop    %r13
  pop    %r14
  pop    %r15
  ret
L58:
  lea    (%r8,%rcx,1),%eax
  sub    %edx,%eax
  jmp    L14
L146:
  mov    %r13d,%edx
  mov    %ebp,%esi
  lea    D1(%rip),%rdi
  mov    $0x0,%eax
  call   <printf@plt>
  lea    D33(%rip),%rdi
  call   <puts@plt>
  movl   $0x0,0x4(%rsp)
  mov    $0x0,%r15d
  jmp    L91
L168:
  mov    %r13d,%edx
  mov    %ebp,%esi
  lea    D1(%rip),%rdi
  mov    $0x0,%eax
  call   <printf@plt>
  lea    D33(%rip),%rdi
  call   <puts@plt>
  mov    $0x0,%r15d
  jmp    L155
L126:
  test   %ecx,%ecx
  je     L168
  mov    %r12d,0x4(%rsp)
L11:
  movslq %r15d,%rax
  pxor   %xmm1,%xmm1
  cvtsi2sd %ecx,%xmm1
  movsd  0x30(%rsp,%rax,8),%xmm0
  divsd  %xmm1,%xmm0
  movsd  %xmm0,0x30(%rsp,%rax,8)
  add    $0x1,%r15d
  cmp    $0x1,%r15d
  jg     L140
L247:
  movslq %r15d,%rax
  movq   $0x0,0x30(%rsp,%rax,8)
  test   %r12d,%r12d
  jle    L146
  mov    %r12d,%esi
  mov    $0x0,%eax
  mov    $0x0,%ecx
  movslq %r15d,%rdx
  jmp    L278
L191:
  call   <__stack_chk_fail@plt>
```

tool_calls: name: unpack_data
arguments: label: D4
type: byte[8]
name: unpack_data
arguments: label: D42
type: string
name: unpack_data
arguments: label: D1
type: string
name: unpack_data
arguments: label: D33
type: string

tool: {"D4": [0x00,0x00,0x00,0x00,0x00,0x00,0xe0,0x3f]}

tool: D42: %4d

tool: D1: ** %d %d 


tool: D33: available:  

assistant: ```c
double nj_sum_of_branch_lengths(double **dist_table, int *current_list, int node_ctr1, int node_ctr2, int tree_size, double *avg1_ptr, double *avg2_ptr) {
  double sum, term;
  double avg[2];
  int current_pair[2] = {node_ctr1, node_ctr2};
  int ctr1, ctr2, n;
  sum = dist_table[node_ctr1][node_ctr2] / 2;
  for (ctr1 = 0; ctr1 < 2; ctr1++) {
    n = 0;
    avg[ctr1] = 0;
    for (ctr2 = 0; ctr2 < tree_size; ctr2++) {
      if (!current_list[ctr2])
        continue;
      if (ctr2 == node_ctr1 || ctr2 == node_ctr2)
        continue;
      avg[ctr1] += dist_table[current_pair[ctr1]][ctr2];
      n++;
    }
    if (!n) {
      printf("** %d %d \n", node_ctr1, node_ctr2);
      printf("available:  \n");
      for (ctr1 = 0; ctr1 < tree_size; ctr1++) {
        if (!current_list[ctr1])
          continue;
        printf("%4d", ctr1);
      }
      printf("\n");
      exit(1);
    }
    avg[ctr1] /= n;
  }
  sum += (avg[0] + avg[1]) / 2;
  *avg1_ptr = avg[0];
  *avg2_ptr = avg[1];
  term = 0;
  n = 0;
  for (ctr1 = 0; ctr1 < tree_size; ctr1++) {
    if (!current_list[ctr1])
      continue;
    if (ctr1 == node_ctr1 || ctr2 == node_ctr2)
      continue;
    for (ctr2 = ctr1 + 1; ctr2 < tree_size; ctr2++) {
      if (!current_list[ctr2])
        continue;
      if (ctr2 == node_ctr1 || ctr2 == node_ctr2)
        continue;
      term += dist_table[ctr1][ctr2];
      n++;
    }
  }
  sum += term / n;
  return sum;
}
```