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

```asm
<range>:
  endbr64
  push   %r15
  mov    %rdx,%r15
  mov    %r9d,%eax
  push   %r14
  imul   %r9d,%eax
  push   %r13
  push   %r12
  push   %rbp
  mov    %r8,%rbp
  push   %rbx
  mov    %rdi,%rbx
  sub    $0x98,%rsp
  mov    %rsi,0x10(%rsp)
  mov    %rcx,0x48(%rsp)
  mov    %rdx,0x78(%rsp)
  mov    %fs:0x28,%rdx
  mov    %rdx,0x88(%rsp)
  mov    %r9d,%edx
  mov    %eax,0x84(%rsp)
  cltq
  lea    (%rax,%rax,1),%rdi
  shr    $0x1f,%edx
  add    %r9d,%edx
  sar    %edx
  mov    %edx,%r12d
  mov    %edx,0x20(%rsp)
  call   <malloc@plt>
  cmp    $0x4,%rbp
  mov    $0x10,%edx
  mov    $0xa,%edi
  mov    %rax,%r14
  mov    $0x3,%eax
  cmovne %edx,%eax
  mov    $0xfa,%edx
  mov    %eax,0x5c(%rsp)
  mov    $0xa,%eax
  cmovne %edx,%eax
  mov    %eax,0x58(%rsp)
  call   <putchar@plt>
  movslq %r12d,%rax
  sub    %rax,%r15
  mov    %rax,0x40(%rsp)
  mov    %r15,0x60(%rsp)
  cmp    %r15,%rax
  jge    L75
  mov    0x20(%rsp),%esi
  mov    0x40(%rsp),%rcx
  mov    0x48(%rsp),%rax
  mov    %esi,%edi
  lea    (%rsi,%rsi,1),%r13d
  sub    %rcx,%rax
  neg    %edi
  lea    (%rcx,%r13,1),%rdx
  mov    %rax,0x50(%rsp)
  movslq %edi,%rax
  add    %rax,%rdx
  mov    %edi,0x24(%rsp)
  mov    %esi,%edi
  mov    %rcx,%rsi
  lea    0x8(%rbx,%rdx,8),%r12
  lea    (%rbx,%rax,8),%rbx
  mov    %rsi,0x8(%rsp)
  lea    (%rcx,%rax,1),%rdx
  neg    %rax
  lea    0x1(%rdi,%rdi,1),%ebp
  mov    %rbx,0x70(%rsp)
  lea    (%rdx,%rdx,1),%rcx
  add    %rax,%rax
  mov    %ebp,%ebx
  mov    %r12,%rbp
  mov    %rcx,0x68(%rsp)
  mov    %rax,0x30(%rsp)
L96:
  mov    0x8(%rsp),%rax
  mov    %eax,%esi
  imul   $0xcccccccd,%eax,%eax
  add    $0x19999998,%eax
  ror    %eax
  cmp    $0x19999998,%eax
  jbe    L130
L98:
  mov    0x50(%rsp),%rdi
  cmp    %rdi,0x40(%rsp)
  jge    L116
  mov    0x50(%rsp),%rax
  sub    0x40(%rsp),%rax
  xor    %r15d,%r15d
  mov    0x8(%rsp),%rdi
  mov    0x68(%rsp),%r12
  mov    %rax,0x18(%rsp)
  mov    0x70(%rsp),%rax
  lea    (%rax,%rdi,8),%rax
  mov    %rbp,%rdi
  mov    %r15,%rbp
  mov    %rax,0x28(%rsp)
  lea    0x84(%rsp),%rax
  mov    %rdi,%r15
  mov    %rax,0x38(%rsp)
  mov    %ebx,%eax
  mov    %r14,%rbx
  mov    %r12,%r14
  mov    %eax,%r12d
  nopl   0x0(%rax)
L7:
  mov    0x28(%rsp),%r10
  mov    0x24(%rsp),%edi
  xor    %r11d,%r11d
  cmp    %edi,0x20(%rsp)
  jl     L101
  nopw   0x0(%rax,%rax,1)
L4:
  mov    (%r10),%rcx
  movslq %r11d,%rax
  lea    (%rbx,%rax,2),%rsi
  xor    %eax,%eax
  add    %r14,%rcx
  nop
L99:
  movzwl (%rcx,%rax,2),%edx
  mov    %dx,(%rsi,%rax,2)
  mov    %rax,%rdx
  add    $0x1,%rax
  cmp    %r13,%rdx
  jne    L99
  add    $0x8,%r10
  add    %r12d,%r11d
  cmp    %r15,%r10
  jne    L4
L101:
  mov    0x38(%rsp),%rsi
  mov    %rbx,%rdi
  xor    %eax,%eax
  add    $0x1,%rbp
  call   <sort_elements@plt>
  mov    0x10(%rsp),%rax
  mov    0x8(%rsp),%rdi
  mov    (%rax,%rdi,8),%rdx
  movslq 0x84(%rsp),%rax
  mov    0x30(%rsp),%rdi
  movzwl -0x2(%rbx,%rax,2),%eax
  add    %r14,%rdx
  sub    (%rbx),%ax
  add    $0x2,%r14
  mov    %ax,(%rdx,%rdi,1)
  cmp    0x18(%rsp),%rbp
  jne    L7
  mov    %rbx,%r14
  mov    %r15,%rbp
  mov    %r12d,%ebx
L116:
  addq   $0x1,0x8(%rsp)
  add    $0x8,%rbp
  mov    0x8(%rsp),%rax
  cmp    0x60(%rsp),%rax
  jne    L96
L75:
  cmpl   $0x1,0xd0(%rsp)
  je     L40
L43:
  mov    %r14,%rdi
  call   <free@plt>
  mov    0x88(%rsp),%rax
  sub    %fs:0x28,%rax
  jne    L54
  add    $0x98,%rsp
  mov    $0x1,%eax
  pop    %rbx
  pop    %rbp
  pop    %r12
  pop    %r13
  pop    %r14
  pop    %r15
  ret
L130:
  lea    D0(%rip),%rdi
  xor    %eax,%eax
  call   <printf@plt>
  jmp    L98
L40:
  mov    0x78(%rsp),%rax
  test   %rax,%rax
  jle    L43
  mov    0x10(%rsp),%rdx
  mov    0x58(%rsp),%r9d
  mov    0x5c(%rsp),%r10d
  mov    0xd8(%rsp),%r11d
  lea    (%rdx,%rax,8),%r8
  mov    0x48(%rsp),%rax
  lea    (%rax,%rax,1),%rdi
L70:
  cmpq   $0x0,0x48(%rsp)
  jle    L12
  mov    (%rdx),%rax
  lea    (%rdi,%rax,1),%rsi
L10:
  movswl (%rax),%ecx
  cmp    %r11d,%ecx
  mov    %r9d,%ecx
  cmovle %r10d,%ecx
  add    $0x2,%rax
  mov    %cx,-0x2(%rax)
  cmp    %rax,%rsi
  jne    L10
L12:
  add    $0x8,%rdx
  cmp    %rdx,%r8
  jne    L70
  jmp    L43
L54:
  call   <__stack_chk_fail@plt>
```

tool_calls: name: retrieve_data
arguments: tag: D0
type: string

tool: D0: %4d 

assistant: ```c
int range(the_image, out_image, rows, cols, bits_per_pixel, size, threshold, high)
int high, threshold, size;
short **the_image, **out_image;
long rows, cols, bits_per_pixel;
{
  int a, b, count, i, j, k, new_hi, new_low, length, sd2, sd2p1, ss, width;
  short *elements;
  sd2 = size / 2;
  sd2p1 = sd2 + 1;
  ss = size * size;
  elements = (short *)malloc(ss * sizeof(short));
  new_hi = 250;
  new_low = 16;
  if (bits_per_pixel == 4) {
    new_hi = 10;
    new_low = 3;
  }
  printf("\n");
  for (i = sd2; i < rows - sd2; i++) {
    if ((i % 10) == 0)
      printf("%4d ", i);
    for (j = sd2; j < cols - sd2; j++) {
      count = 0;
      for (a = -sd2; a < sd2p1; a++) {
        for (b = -sd2; b < sd2p1; b++) {
          elements[count] = the_image[i + a][j + b];
          count++;
        }
      }
      sort_elements(elements, &ss);
      out_image[i][j] = elements[ss - 1] - elements[0];
    }
  }
  if (threshold == 1) {
    for (i = 0; i < rows; i++) {
      for (j = 0; j < cols; j++) {
        if (out_image[i][j] > high) {
          out_image[i][j] = new_hi;
        } else {
          out_image[i][j] = new_low;
        }
      }
    }
  }
  free(elements);
  return (1);
}
```