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

```asm
<main>:
  endbr64
  push   %r15
  push   %r14
  push   %r13
  push   %r12
  push   %rbp
  push   %rbx
  sub    $0x28,%rsp
  mov    %fs:0x28,%rax
  mov    %rax,0x18(%rsp)
  xor    %eax,%eax
  cmp    $0x1,%edi
  jg     L72
  lea    D9(%rip),%rsi
  lea    D17(%rip),%rdi
  call   <fopen@plt>
  mov    %rax,%r12
  test   %rax,%rax
  je     L36
  movl   $0x0,0xc(%rsp)
  xor    %ebx,%ebx
  xor    %ebp,%ebp
  lea    0x10(%rsp),%r13
  jmp    L128
  nopl   (%rax)
L14:
  test   %rbp,%rbp
  je     L90
  mov    %rbp,%rdi
  call   <fclose@plt>
L90:
  mov    %ebx,%edx
  lea    D59(%rip),%rsi
  mov    %r13,%rdi
  call   <sprintf@plt>
  lea    D18(%rip),%rsi
  mov    %r13,%rdi
  call   <fopen@plt>
  mov    %rax,%rbp
  test   %rax,%rax
  je     L127
  add    $0x1,%ebx
L34:
  mov    %rbp,%rcx
  mov    $0x1,%edx
  mov    $0x200,%esi
  mov    %r15,%rdi
  call   <fwrite@plt>
  movl   $0x1,0xc(%rsp)
L106:
  mov    %r15,%rdi
  call   <free@plt>
L128:
  mov    $0x200,%edi
  call   <malloc@plt>
  mov    %r12,%rcx
  mov    $0x1,%edx
  mov    $0x200,%esi
  mov    %rax,%rdi
  mov    %rax,%r15
  call   <fread@plt>
  mov    %eax,%r14d
  cmp    $0x1,%eax
  jne    L119
  mov    0x8(%r15),%rax
  add    (%r15),%rax
  add    0x10(%r15),%rax
  add    0x18(%r15),%rax
  sub    $0x3b6,%rax
  cmp    $0x1,%rax
  jbe    L14
  mov    0xc(%rsp),%eax
  test   %eax,%eax
  je     L106
  jmp    L34
L72:
  lea    D63(%rip),%rdi
  mov    $0x1,%r14d
  call   <printf@plt>
L71:
  mov    0x18(%rsp),%rax
  sub    %fs:0x28,%rax
  jne    L94
  add    $0x28,%rsp
  mov    %r14d,%eax
  pop    %rbx
  pop    %rbp
  pop    %r12
  pop    %r13
  pop    %r14
  pop    %r15
  ret
L119:
  mov    %rbp,%rdi
  xor    %r14d,%r14d
  call   <fclose@plt>
  mov    %r15,%rdi
  call   <free@plt>
  mov    %r12,%rdi
  call   <fclose@plt>
  jmp    L71
L127:
  mov    %r13,%rsi
  lea    D7(%rip),%rdi
  xor    %eax,%eax
  call   <printf@plt>
  mov    %r12,%rdi
  call   <fclose@plt>
  mov    %r15,%rdi
  call   <free@plt>
  jmp    L71
L36:
  lea    D1(%rip),%rdi
  mov    $0x1,%r14d
  call   <puts@plt>
  jmp    L71
L94:
  call   <__stack_chk_fail@plt>
```

tool_calls: name: disassemble_data
arguments: label: D9
datatype: string
name: disassemble_data
arguments: label: D17
datatype: string
name: disassemble_data
arguments: label: D59
datatype: string
name: disassemble_data
arguments: label: D18
datatype: string
name: disassemble_data
arguments: label: D63
datatype: string
name: disassemble_data
arguments: label: D7
datatype: string
name: disassemble_data
arguments: label: D1
datatype: string

tool: D9: r

tool: D17: card.raw

tool: D59: %03d.jpg

tool: D18: w

tool: D63: Usage: no command-line arguments required.

tool: D7: Could not open %s for writing.


tool: D1: Could not open card.raw for reading.

assistant: ```c
int main(int argc, char *argv[]) {
  if (argc > 1) {
    printf("Usage: no command-line arguments required.");
    return 1;
  }
  FILE *inptr = fopen("card.raw", "r");
  FILE *outptr = NULL;
  if (inptr == NULL) {
    printf("Could not open card.raw for reading.\n");
    return 1;
  }
  uint16_t signatureA = 0xff + 0xd8 + 0xff + 0xe0;
  uint16_t signatureB = 0xff + 0xd8 + 0xff + 0xe1;
  bool foundFirstSignature = false;
  int fnumber = 0;
  while (true) {
    uint8_t *buffer = (uint8_t *)malloc(512);
    if (fread(buffer, 512, 1, inptr) != 1) {
      fclose(outptr);
      free(buffer);
      break;
    }
    uint16_t signature = buffer[0] + buffer[1] + buffer[2] + buffer[3];
    if (signature == signatureA || signature == signatureB) {
      foundFirstSignature = true;
      if (outptr != NULL) {
        fclose(outptr);
      }
      char fname[8];
      sprintf(fname, "%03d.jpg", fnumber);
      outptr = fopen(fname, "w");
      if (outptr == NULL) {
        printf("Could not open %s for writing.\n", fname);
        fclose(inptr);
        free(buffer);
        return 1;
      }
      fnumber += 1;
    }
    if (foundFirstSignature) {
      fwrite(buffer, 512, 1, outptr);
    }
    free(buffer);
  }
  fclose(inptr);
  return 0;
}
```