user: What is the c source code of the assembly code below?

```asm
<pgm_write>:
  endbr64
  push   %r15
  push   %r14
  push   %r13
  push   %r12
  mov    %rdx,%r12
  push   %rbp
  imul   %rcx,%r12
  push   %rbx
  sub    $0x48,%rsp
  test   %r12,%r12
  je     L97
  lea    -0x1(%r12),%rax
  mov    %rdi,%rbp
  mov    %rsi,%r13
  mov    %rdx,%r14
  mov    %rcx,%r15
  cmp    $0xe,%rax
  jbe    L139
  mov    %r12,%rdx
  mov    %rsi,%rax
  pxor   %xmm0,%xmm0
  and    $0xfffffffffffffff0,%rdx
  add    %rsi,%rdx
  xchg   %ax,%ax
L150:
  movdqu (%rax),%xmm2
  add    $0x10,%rax
  pmaxub %xmm2,%xmm0
  cmp    %rax,%rdx
  jne    L150
  movdqa %xmm0,%xmm1
  mov    %r12,%rcx
  psrldq $0x8,%xmm1
  and    $0xfffffffffffffff0,%rcx
  pmaxub %xmm1,%xmm0
  movdqa %xmm0,%xmm1
  psrldq $0x4,%xmm1
  pmaxub %xmm1,%xmm0
  movdqa %xmm0,%xmm1
  psrldq $0x2,%xmm1
  pmaxub %xmm1,%xmm0
  movdqa %xmm0,%xmm1
  psrldq $0x1,%xmm1
  pmaxub %xmm1,%xmm0
  movd   %xmm0,%ebx
  test   $0xf,%r12b
  je     L18
L74:
  mov    %r12,%rsi
  sub    %rcx,%rsi
  lea    -0x1(%rsi),%rax
  cmp    $0x6,%rax
  jbe    L41
  movzbl %bl,%eax
  shl    $0x8,%ebx
  movq   0x0(%r13,%rcx,1),%xmm1
  or     %eax,%ebx
  movd   %ebx,%xmm3
  pshuflw $0x0,%xmm3,%xmm0
  pmaxub %xmm0,%xmm1
  movq   %xmm1,%rbx
  movq   %xmm1,%rdx
  movq   %xmm1,0x38(%rsp)
  movzbl %bh,%edi
  mov    %ebx,%eax
  movq   %xmm1,0x30(%rsp)
  cmp    %dil,%bl
  movq   %xmm1,0x28(%rsp)
  cmovb  %edi,%eax
  movq   %xmm1,%rdi
  movq   %xmm1,0x20(%rsp)
  shr    $0x10,%rdi
  movq   %xmm1,0x18(%rsp)
  cmp    %dil,%al
  movq   %xmm1,0x10(%rsp)
  cmovb  %edi,%eax
  movq   %xmm1,%rdi
  movq   %xmm1,0x8(%rsp)
  shr    $0x18,%rdi
  movq   %xmm1,(%rsp)
  cmp    %dil,%al
  cmovb  %edi,%eax
  movq   %xmm1,%rdi
  shr    $0x20,%rdi
  cmp    %dil,%al
  cmovb  %edi,%eax
  movq   %xmm1,%rdi
  shr    $0x28,%rdi
  cmp    %dil,%al
  cmovb  %edi,%eax
  movq   %xmm1,%rdi
  shr    $0x30,%rdi
  cmp    %dil,%al
  cmovb  %edi,%eax
  shr    $0x38,%rdx
  mov    %edx,%ebx
  cmp    %dl,%al
  cmovae %eax,%ebx
  mov    %rsi,%rax
  and    $0xfffffffffffffff8,%rax
  add    %rax,%rcx
  cmp    %rax,%rsi
  je     L18
L41:
  movzbl 0x0(%r13,%rcx,1),%eax
  cmp    %al,%bl
  cmovb  %eax,%ebx
  lea    0x1(%rcx),%rax
  cmp    %rax,%r12
  jbe    L18
  movzbl 0x1(%r13,%rcx,1),%eax
  cmp    %al,%bl
  cmovb  %eax,%ebx
  lea    0x2(%rcx),%rax
  cmp    %rax,%r12
  jbe    L18
  movzbl 0x2(%r13,%rcx,1),%eax
  cmp    %al,%bl
  cmovb  %eax,%ebx
  lea    0x3(%rcx),%rax
  cmp    %rax,%r12
  jbe    L18
  movzbl 0x3(%r13,%rcx,1),%eax
  cmp    %al,%bl
  cmovb  %eax,%ebx
  lea    0x4(%rcx),%rax
  cmp    %rax,%r12
  jbe    L18
  movzbl 0x4(%r13,%rcx,1),%eax
  cmp    %al,%bl
  cmovb  %eax,%ebx
  lea    0x5(%rcx),%rax
  cmp    %rax,%r12
  jbe    L18
  movzbl 0x5(%r13,%rcx,1),%eax
  cmp    %al,%bl
  cmovb  %eax,%ebx
  lea    0x6(%rcx),%rax
  cmp    %rax,%r12
  jbe    L18
  movzbl 0x6(%r13,%rcx,1),%eax
  cmp    %al,%bl
  cmovb  %eax,%ebx
L18:
  test   %bl,%bl
  je     L97
  mov    %rbp,%rcx
  mov    $0x3,%edx
  mov    $0x1,%esi
  lea    D29(%rip),%rdi
  call   <fwrite@plt>
  mov    %r15,%rcx
  mov    %r14,%rdx
  mov    %rbp,%rdi
  lea    D9(%rip),%rsi
  xor    %eax,%eax
  call   <fprintf@plt>
  movzbl %bl,%edx
  mov    %rbp,%rdi
  xor    %eax,%eax
  lea    D18(%rip),%rsi
  xor    %ebx,%ebx
  call   <fprintf@plt>
  jmp    L67
  nopw   0x0(%rax,%rax,1)
L149:
  add    %rax,%rbx
  cmp    %rbx,%r12
  jbe    L90
L67:
  mov    %r12,%rdx
  lea    0x0(%r13,%rbx,1),%rdi
  mov    %rbp,%rcx
  mov    $0x1,%esi
  sub    %rbx,%rdx
  call   <fwrite@plt>
  test   %rax,%rax
  jne    L149
  lea    D37(%rip),%rdi
  call   <perror@plt>
L90:
  xor    %eax,%eax
  cmp    %rbx,%r12
  setne  %al
  add    $0x48,%rsp
  pop    %rbx
  neg    %eax
  pop    %rbp
  pop    %r12
  pop    %r13
  pop    %r14
  pop    %r15
  ret
  nopw   0x0(%rax,%rax,1)
L97:
  add    $0x48,%rsp
  mov    $0xffffffff,%eax
  pop    %rbx
  pop    %rbp
  pop    %r12
  pop    %r13
  pop    %r14
  pop    %r15
  ret
L139:
  xor    %ebx,%ebx
  xor    %ecx,%ecx
  jmp    L74
```

tool_calls: name: unpack_data
arguments: tag: D29
type: string
name: unpack_data
arguments: tag: D9
type: string
name: unpack_data
arguments: tag: D18
type: string
name: unpack_data
arguments: tag: D37
type: string

tool: D29: P5


tool: D9: %lu %lu


tool: D18: %u


tool: D37: pgm_write()

assistant: ```c
int pgm_write(FILE *fd, const unsigned char *img, size_t width, size_t height) {
  size_t i, n, saved;
  size_t img_len = width * height;
  unsigned char max_gray = 0x00;
  for (i = 0; i < img_len; i++) {
    if (img[i] > max_gray) {
      max_gray = img[i];
    }
  }
  if (max_gray == 0x00) {
    return -1;
  }
  fprintf(fd, "P5\n");
  fprintf(fd, "%lu %lu\n", width, height);
  fprintf(fd, "%u\n", max_gray);
  n = 0;
  saved = 0;
  while (saved < img_len) {
    n = fwrite(img + saved, 1, img_len - saved, fd);
    if (n <= 0) {
      perror("pgm_write()");
      break;
    }
    saved += n;
  }
  return saved == img_len ? 0 : -1;
}
```