user: Converte this assembly snippet into its equivalent C code:

```asm
<main>:
  endbr64
  push   %r15
  push   %r14
  push   %r13
  push   %r12
  push   %rbp
  push   %rbx
  lea    -0x40000(%rsp),%r11
L81:
  sub    $0x1000,%rsp
  orq    $0x0,(%rsp)
  cmp    %r11,%rsp
  jne    L81
  sub    $0x28,%rsp
  mov    %fs:0x28,%rax
  mov    %rax,0x40018(%rsp)
  xor    %eax,%eax
  mov    %rsi,%rbx
  cmp    $0x1,%edi
  jle    L0
L172:
  mov    0x8(%rbx),%rdi
  lea    D48(%rip),%rsi
  call   <fopen@plt>
  mov    %rax,%r12
  test   %rax,%rax
  je     L136
L135:
  lea    0x10(%rsp),%rbp
  mov    %r12,%rcx
  mov    $0x40000,%edx
  mov    $0x1,%esi
  mov    %rbp,%rdi
  call   <fread@plt>
  cmp    $0x40000,%eax
  je     L134
  mov    0x8(%rbx),%rsi
  lea    D35(%rip),%rdi
  xor    %eax,%eax
  call   <printf@plt>
  mov    %r12,%rdi
  call   <fclose@plt>
  mov    $0x2,%edi
  call   <exit@plt>
L134:
  mov    %r12,%rdi
  call   <fclose@plt>
  lea    0x18(%rsp),%rdi
  call   <Read32@plt>
  lea    0x1c(%rsp),%rdi
  mov    %rax,%r12
  call   <Read32@plt>
  mov    %rax,%r13
  cmp    $0x2000100,%r12
  jne    L28
  mov    $0x3ff00,%eax
  mov    $0x100,%r12d
  cmp    $0x4e20,%r13
  jbe    L28
L73:
  cmp    $0x9c3f,%r13
  jbe    L102
  cmp    %rax,%r13
  ja     L102
L89:
  test   %r13,%r13
  je     L20
  lea    0x0(%r13,%r12,1),%rcx
  lea    0x0(%rbp,%r12,1),%rdx
  xor    %esi,%esi
  add    %rbp,%rcx
L162:
  movzbl (%rdx),%eax
  add    $0x1,%rdx
  ror    %al
  not    %eax
  movzbl %al,%eax
  add    %eax,%esi
  cmp    %rdx,%rcx
  jne    L162
L130:
  movzwl %si,%esi
  lea    0xc(%rsp),%rdi
  lea    0xa(%rsp),%r14
  call   <Write32@plt>
  mov    %r13,%rsi
  mov    %r14,%rdi
  call   <Write32@plt>
  mov    0x10(%rbx),%rdi
  lea    D61(%rip),%rsi
  call   <fopen@plt>
  mov    %rax,%r15
  test   %rax,%rax
  je     L101
L93:
  mov    %r15,%rcx
  mov    $0x6,%edx
  mov    $0x1,%esi
  mov    %r14,%rdi
  call   <fwrite@plt>
  cmp    $0x6,%rax
  je     L192
L116:
  lea    D4(%rip),%rdi
  call   <puts@plt>
  mov    %r15,%rdi
  call   <fclose@plt>
  mov    $0x5,%edi
  call   <exit@plt>
L124:
  mov    %r15,%rdi
  call   <fclose@plt>
  mov    0x40018(%rsp),%rax
  sub    %fs:0x28,%rax
  jne    L62
  add    $0x40028,%rsp
  xor    %eax,%eax
  pop    %rbx
  pop    %rbp
  pop    %r12
  pop    %r13
  pop    %r14
  pop    %r15
  ret
L102:
  mov    %r13,%rsi
  lea    D56(%rip),%rdi
  xor    %eax,%eax
  call   <printf@plt>
  mov    $0x3,%edi
  call   <exit@plt>
  jmp    L89
L192:
  lea    0x0(%rbp,%r12,1),%rdi
  mov    %r15,%rcx
  mov    %r13,%rdx
  mov    $0x1,%esi
  call   <fwrite@plt>
  cmp    %r13,%rax
  jne    L116
  jmp    L124
L28:
  lea    0x6014(%rsp),%rdi
  mov    $0x6020,%r12d
  call   <Read32@plt>
  mov    %rax,%r13
  mov    $0x39fe0,%eax
  jmp    L73
L0:
  lea    D97(%rip),%rdi
  call   <puts@plt>
  lea    D15(%rip),%rdi
  call   <puts@plt>
  lea    D37(%rip),%rdi
  call   <puts@plt>
  lea    D75(%rip),%rdi
  call   <puts@plt>
  lea    D60(%rip),%rdi
  call   <puts@plt>
  xor    %edi,%edi
  call   <exit@plt>
  jmp    L172
L136:
  mov    0x8(%rbx),%rsi
  lea    D13(%rip),%rdi
  xor    %eax,%eax
  call   <printf@plt>
  mov    $0x1,%edi
  call   <exit@plt>
  jmp    L135
L101:
  mov    0x10(%rbx),%rsi
  lea    D79(%rip),%rdi
  xor    %eax,%eax
  call   <printf@plt>
  mov    $0x4,%edi
  call   <exit@plt>
  jmp    L93
L20:
  xor    %esi,%esi
  jmp    L130
L62:
  call   <__stack_chk_fail@plt>
```

tool_calls: name: read_data
arguments: data_label: D48
type_infered: string
name: read_data
arguments: data_label: D35
type_infered: string
name: read_data
arguments: data_label: D61
type_infered: string
name: read_data
arguments: data_label: D4
type_infered: string
name: read_data
arguments: data_label: D56
type_infered: string
name: read_data
arguments: data_label: D97
type_infered: string
name: read_data
arguments: data_label: D15
type_infered: string
name: read_data
arguments: data_label: D37
type_infered: string
name: read_data
arguments: data_label: D75
type_infered: string
name: read_data
arguments: data_label: D60
type_infered: string
name: read_data
arguments: data_label: D13
type_infered: string
name: read_data
arguments: data_label: D79
type_infered: string

tool: D48: rb

tool: D35: Error reading input file %s, must be 256kB in size.


tool: D61: wb

tool: D4: Write error

tool: D56: Error: Impossible image size %d bytes.


tool: D97: Extract the software image out of an original Archos Flash ROM dump.

tool: D15: Result is a scrambled file, use the descramble tool to get the binary,

tool: D37:  always without the -fm option, even if processing an FM software.


tool: D75: Usage: extract <flash dump file> <output file>

tool: D60: Example: extract internal_rom_2000000-203FFFF.bin archos.ajz

tool: D13: Error opening input file %s


tool: D79: Error opening output file %s


assistant: ```c
int main(int argc, char *argv[]) {
  FILE *pInFile;
  FILE *pOutFile;
  unsigned char aHeader[6];
  unsigned char aImage[256 * 1024];
  unsigned long i;
  unsigned long uiSize, uiStart;
  unsigned short usChecksum = 0;
  if (argc < 2) {
    printf("Extract the software image out of an original Archos Flash ROM dump.\n");
    printf("Result is a scrambled file, use the descramble tool to get the binary,\n");
    printf(" always without the -fm option, even if processing an FM software.\n\n");
    printf("Usage: extract <flash dump file> <output file>\n");
    printf("Example: extract internal_rom_2000000-203FFFF.bin archos.ajz\n");
    exit(0);
  }
  pInFile = fopen(argv[1], "rb");
  if (pInFile == NULL) {
    printf("Error opening input file %s\n", argv[1]);
    exit(1);
  }
  if (fread(aImage, 1, sizeof(aImage), pInFile) != sizeof(aImage)) {
    printf("Error reading input file %s, must be 256kB in size.\n", argv[1]);
    fclose(pInFile);
    exit(2);
  }
  fclose(pInFile);
  uiStart = Read32(aImage + 8);
  uiSize = Read32(aImage + 12);
  if (uiStart == 0x02000100 && uiSize > 20000) {
    uiStart = 0x0100;
  } else {
    uiStart = 0x6020;
    uiSize = Read32(aImage + 0x6000 + 4);
  }
  if (uiSize > sizeof(aImage) - uiStart || uiSize < 40000) {
    printf("Error: Impossible image size %d bytes.\n", uiSize);
    exit(3);
  }
  for (i = 0; i < uiSize; i++) {
    unsigned char byte;
    byte = aImage[uiStart + i];
    byte = ~((byte >> 1) | ((byte << 7) & 0x80));
    usChecksum += byte;
  }
  Write32(aHeader + 2, usChecksum);
  Write32(aHeader, uiSize);
  pOutFile = fopen(argv[2], "wb");
  if (pOutFile == NULL) {
    printf("Error opening output file %s\n", argv[2]);
    exit(4);
  }
  if (fwrite(aHeader, 1, sizeof(aHeader), pOutFile) != sizeof(aHeader) || fwrite(aImage + uiStart, 1, uiSize, pOutFile) != uiSize) {
    printf("Write error\n");
    fclose(pOutFile);
    exit(5);
  }
  fclose(pOutFile);
  return 0;
}
```