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

```asm
<WriteBMP32>:
  endbr64
  push   %r15
  push   %r14
  push   %r13
  mov    %ecx,%r13d
  push   %r12
  push   %rbp
  mov    %rsi,%rbp
  lea    D3(%rip),%rsi
  push   %rbx
  mov    %edx,%ebx
  sub    $0x48,%rsp
  mov    %fs:0x28,%rax
  mov    %rax,0x38(%rsp)
  xor    %eax,%eax
  call   <fopen@plt>
  test   %rax,%rax
  je     L4
  mov    %ebx,%r9d
  lea    0x10(%rsp),%r12
  mov    $0xe,%edx
  xor    %esi,%esi
  imul   %r13d,%r9d
  mov    %r12,%rdi
  mov    %rax,%r15
  lea    0x0(,%r9,4),%r14d
  mov    %r9d,0xc(%rsp)
  call   <memset@plt>
  mov    $0x4d42,%eax
  lea    0x12(%rsp),%rdx
  lea    0x36(%r14),%edi
  mov    $0x1,%ecx
  mov    $0x4,%esi
  mov    %ax,0x10(%rsp)
  call   <bwrite_endian@plt>
  lea    0x16(%rsp),%rdx
  mov    $0x1,%ecx
  xor    %edi,%edi
  mov    $0x4,%esi
  call   <bwrite_endian@plt>
  lea    0x1a(%rsp),%rdx
  mov    $0x1,%ecx
  mov    $0x4,%esi
  mov    $0x36,%edi
  call   <bwrite_endian@plt>
  mov    %r15,%rcx
  mov    $0xe,%edx
  mov    %r12,%rdi
  mov    $0x1,%esi
  call   <fwrite@plt>
  mov    $0x28,%edx
  xor    %esi,%esi
  mov    %r12,%rdi
  call   <memset@plt>
  mov    $0x1,%ecx
  mov    %r12,%rdx
  mov    $0x4,%esi
  mov    $0x28,%edi
  call   <bwrite_endian@plt>
  lea    0x14(%rsp),%rdx
  mov    $0x1,%ecx
  mov    %ebx,%edi
  mov    $0x4,%esi
  call   <bwrite_endian@plt>
  lea    0x18(%rsp),%rdx
  mov    $0x1,%ecx
  mov    %r13d,%edi
  mov    $0x4,%esi
  call   <bwrite_endian@plt>
  lea    0x1c(%rsp),%rdx
  mov    $0x1,%ecx
  mov    $0x2,%esi
  mov    $0x1,%edi
  call   <bwrite_endian@plt>
  lea    0x1e(%rsp),%rdx
  mov    $0x1,%ecx
  mov    $0x2,%esi
  mov    $0x20,%edi
  call   <bwrite_endian@plt>
  lea    0x20(%rsp),%rdx
  mov    $0x1,%ecx
  xor    %edi,%edi
  mov    $0x4,%esi
  call   <bwrite_endian@plt>
  lea    0x24(%rsp),%rdx
  mov    $0x1,%ecx
  mov    %r14d,%edi
  mov    $0x4,%esi
  call   <bwrite_endian@plt>
  lea    0x28(%rsp),%rdx
  mov    $0x1,%ecx
  mov    $0x4,%esi
  mov    $0x10000,%edi
  call   <bwrite_endian@plt>
  lea    0x2c(%rsp),%rdx
  mov    $0x1,%ecx
  mov    $0x4,%esi
  mov    $0x10000,%edi
  call   <bwrite_endian@plt>
  lea    0x30(%rsp),%rdx
  mov    $0x1,%ecx
  mov    $0x4,%esi
  mov    $0x1000000,%edi
  call   <bwrite_endian@plt>
  lea    0x34(%rsp),%rdx
  mov    $0x1,%ecx
  xor    %edi,%edi
  mov    $0x4,%esi
  call   <bwrite_endian@plt>
  mov    %r15,%rcx
  mov    $0x28,%edx
  mov    %r12,%rdi
  mov    $0x1,%esi
  call   <fwrite@plt>
  movb   $0x0,0x10(%rsp)
  sub    $0x1,%r13d
  js     L26
  test   %ebx,%ebx
  jle    L26
  mov    %ebx,%eax
  mov    0xc(%rsp),%r9d
  lea    -0x1(%rbx),%edx
  mov    %ebx,%r14d
  neg    %eax
  cltq
  sub    %ebx,%r9d
  lea    (%rax,%rax,2),%rax
  mov    %rax,(%rsp)
  movslq %r9d,%rax
  add    %rdx,%rax
  lea    (%rax,%rax,2),%rax
  lea    0x5(%rbp,%rax,1),%rbp
  lea    0x0(,%r14,4),%rax
  sub    %rax,%r14
  nopl   (%rax)
L38:
  lea    (%r14,%rbp,1),%rbx
  nopl   0x0(%rax)
L59:
  mov    %rbx,%rdi
  mov    %r15,%rcx
  mov    $0x1,%edx
  mov    $0x1,%esi
  call   <fwrite@plt>
  lea    -0x1(%rbx),%rdi
  mov    %r15,%rcx
  mov    $0x1,%edx
  mov    $0x1,%esi
  call   <fwrite@plt>
  lea    -0x2(%rbx),%rdi
  mov    %r15,%rcx
  mov    $0x1,%edx
  mov    $0x1,%esi
  add    $0x3,%rbx
  call   <fwrite@plt>
  mov    %r15,%rcx
  mov    $0x1,%edx
  mov    %r12,%rdi
  mov    $0x1,%esi
  call   <fwrite@plt>
  cmp    %rbp,%rbx
  jne    L59
  sub    $0x1,%r13d
  add    (%rsp),%rbp
  cmp    $0xffffffff,%r13d
  jne    L38
L26:
  mov    %r15,%rdi
  call   <fclose@plt>
  xor    %eax,%eax
L48:
  mov    0x38(%rsp),%rdx
  sub    %fs:0x28,%rdx
  jne    L51
  add    $0x48,%rsp
  pop    %rbx
  pop    %rbp
  pop    %r12
  pop    %r13
  pop    %r14
  pop    %r15
  ret
L4:
  mov    $0xffffffff,%eax
  jmp    L48
L51:
  call   <__stack_chk_fail@plt>
```

tool_calls: name: unpack_data
arguments: tag: D3
type_infered: string

tool: D3: wb

assistant: ```c
int WriteBMP32(const char *fname, unsigned char (*data)[3], int w, int h) {
  FILE *fp = fopen(fname, "wb");
  int Endian = 1;
  unsigned char buffer[40];
  ulong fileSize;
  ulong imageSize;
  int x, y;
  if (!fp)
    return -1;
  imageSize = 4 * w * h;
  fileSize = 14 + 40 + imageSize;
  memset(buffer, 0x00, 14);
  buffer[0] = 66;
  buffer[1] = 77;
  bwrite_endian(fileSize, 4, buffer + 2, Endian);
  bwrite_endian(0, 4, buffer + 6, Endian);
  bwrite_endian(54, 4, buffer + 10, Endian);
  fwrite(buffer, 1, 14, fp);
  memset(buffer, 0x00, 40);
  bwrite_endian(40, 4, buffer + 0, Endian);
  bwrite_endian(w, 4, buffer + 4, Endian);
  bwrite_endian(h, 4, buffer + 8, Endian);
  bwrite_endian(1, 2, buffer + 12, Endian);
  bwrite_endian(32, 2, buffer + 14, Endian);
  bwrite_endian(0, 4, buffer + 16, Endian);
  bwrite_endian(imageSize, 4, buffer + 20, Endian);
  bwrite_endian(0x10000, 4, buffer + 24, Endian);
  bwrite_endian(0x10000, 4, buffer + 28, Endian);
  bwrite_endian(0x1000000, 4, buffer + 32, Endian);
  bwrite_endian(0, 4, buffer + 36, Endian);
  fwrite(buffer, 1, 40, fp);
  buffer[0] = 0;
  for (y = h - 1; y >= 0; --y) {
    for (x = 0; x < w; ++x) {
      fwrite(&data[x + y * w][2], 1, 1, fp);
      fwrite(&data[x + y * w][1], 1, 1, fp);
      fwrite(&data[x + y * w][0], 1, 1, fp);
      fwrite(buffer, 1, 1, fp);
    }
  }
  fclose(fp);
  return 0;
}
```