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

```asm
<pgm_load>:
  endbr64
  push   %rbp
  mov    %rsp,%rbp
  sub    $0x60,%rsp
  mov    %rdi,-0x48(%rbp)
  mov    %rsi,-0x50(%rbp)
  mov    %rdx,-0x58(%rbp)
  mov    %rcx,-0x60(%rbp)
  mov    %fs:0x28,%rax
  mov    %rax,-0x8(%rbp)
  xor    %eax,%eax
  movl   $0xffffffff,-0x40(%rbp)
  mov    -0x48(%rbp),%rax
  lea    D18(%rip),%rdx
  mov    %rdx,%rsi
  mov    %rax,%rdi
  call   <fopen@plt>
  mov    %rax,-0x38(%rbp)
  cmpq   $0x0,-0x38(%rbp)
  je     L64
  lea    -0x30(%rbp),%rcx
  mov    -0x38(%rbp),%rax
  mov    $0x20,%edx
  mov    %rcx,%rsi
  mov    %rax,%rdi
  call   <nextToken@plt>
  mov    %eax,-0x3c(%rbp)
  lea    -0x30(%rbp),%rax
  lea    D27(%rip),%rdx
  mov    %rdx,%rsi
  mov    %rax,%rdi
  call   <strcmp@plt>
  test   %rax,%rax
  je     L75
  lea    -0x30(%rbp),%rdx
  mov    -0x48(%rbp),%rax
  mov    %rdx,%rcx
  mov    %rax,%rdx
  lea    D17(%rip),%rax
  mov    %rax,%rsi
  mov    $0x0,%edi
  call   <errx@plt>
  mov    $0xffffffff,%eax
  jmp    L38
L75:
  lea    -0x30(%rbp),%rcx
  mov    -0x38(%rbp),%rax
  mov    $0x20,%edx
  mov    %rcx,%rsi
  mov    %rax,%rdi
  call   <nextToken@plt>
  lea    -0x30(%rbp),%rax
  mov    %rax,%rdi
  call   <atoi@plt>
  mov    -0x50(%rbp),%rdx
  mov    %eax,(%rdx)
  lea    -0x30(%rbp),%rcx
  mov    -0x38(%rbp),%rax
  mov    $0x20,%edx
  mov    %rcx,%rsi
  mov    %rax,%rdi
  call   <nextToken@plt>
  lea    -0x30(%rbp),%rax
  mov    %rax,%rdi
  call   <atoi@plt>
  mov    -0x58(%rbp),%rdx
  mov    %eax,(%rdx)
  lea    -0x30(%rbp),%rcx
  mov    -0x38(%rbp),%rax
  mov    $0x20,%edx
  mov    %rcx,%rsi
  mov    %rax,%rdi
  call   <nextToken@plt>
  lea    -0x30(%rbp),%rax
  mov    %rax,%rdi
  call   <atoi@plt>
  mov    %eax,-0x40(%rbp)
  mov    -0x50(%rbp),%rax
  mov    (%rax),%edx
  mov    -0x58(%rbp),%rax
  mov    (%rax),%eax
  imul   %edx,%eax
  mov    %eax,-0x3c(%rbp)
  cmpl   $0xff,-0x40(%rbp)
  jg     L14
  mov    -0x3c(%rbp),%eax
  mov    $0x4,%esi
  mov    %eax,%edi
  call   <calloc@plt>
  mov    -0x60(%rbp),%rdx
  mov    %rax,(%rdx)
  mov    -0x60(%rbp),%rax
  mov    (%rax),%rax
  mov    -0x38(%rbp),%rcx
  mov    -0x3c(%rbp),%edx
  mov    $0x4,%esi
  mov    %rax,%rdi
  call   <fread@plt>
  cmp    %eax,-0x3c(%rbp)
  je     L61
  mov    -0x60(%rbp),%rax
  mov    (%rax),%rax
  mov    %rax,%rdi
  call   <free@plt>
  mov    $0xffffffff,%eax
  jmp    L38
L14:
  mov    -0x3c(%rbp),%eax
  mov    $0x4,%esi
  mov    %eax,%edi
  call   <calloc@plt>
  mov    -0x60(%rbp),%rdx
  mov    %rax,(%rdx)
  mov    -0x60(%rbp),%rax
  mov    (%rax),%rax
  mov    -0x38(%rbp),%rcx
  mov    -0x3c(%rbp),%edx
  mov    $0x4,%esi
  mov    %rax,%rdi
  call   <fread@plt>
  cmp    %eax,-0x3c(%rbp)
  je     L61
  mov    -0x60(%rbp),%rax
  mov    (%rax),%rax
  mov    %rax,%rdi
  call   <free@plt>
  mov    $0xffffffff,%eax
  jmp    L38
L61:
  mov    -0x38(%rbp),%rax
  mov    %rax,%rdi
  call   <fclose@plt>
L64:
  mov    -0x40(%rbp),%eax
L38:
  mov    -0x8(%rbp),%rdx
  sub    %fs:0x28,%rdx
  je     L32
  call   <__stack_chk_fail@plt>
L32:
  leave
  ret
```

tool_calls: name: unpack_data
arguments: data_label: D18
type: string
name: unpack_data
arguments: data_label: D27
type: string
name: unpack_data
arguments: data_label: D17
type: string

tool: D18: r

tool: D27: P5

tool: D17: File %s has signature "%s"; expected "P5"

assistant: ```c
int pgm_load(const char *name, int *w, int *h, uint8_t **img) {
  int n, maxval = -1;
  FILE *fp = fopen(name, "r");
  if (fp) {
    char buf[32];
    n = nextToken(fp, buf, 32);
    if (strcmp(buf, "P5")) {
      errx(0, "File %s has signature \"%s\"; expected \"P5\"", name, buf);
      return -1;
    }
    nextToken(fp, buf, 32);
    *w = atoi(buf);
    nextToken(fp, buf, 32);
    *h = atoi(buf);
    nextToken(fp, buf, 32);
    maxval = atoi(buf);
    n = (*w) * (*h);
    if (maxval < 256) {
      *img = calloc(n, sizeof(uint8_t));
      if (fread(*img, sizeof(uint8_t), n, fp) != n) {
        free(*img);
        return -1;
      }
    } else {
      *img = calloc(n, sizeof(uint16_t));
      if (fread(*img, sizeof(uint16_t), n, fp) != n) {
        free(*img);
        return -1;
      }
    }
    fclose(fp);
  }
  return maxval;
}
```