user: 请将以下的汇编代码转换成C语言代码：

```asm
<bm_load_palette>:
  endbr64
  push   %r15
  push   %r14
  push   %r13
  push   %r12
  push   %rbp
  push   %rbx
  sub    $0x88,%rsp
  mov    %rsi,0x8(%rsp)
  mov    %fs:0x28,%rax
  mov    %rax,0x78(%rsp)
  xor    %eax,%eax
  movl   $0x8,0x1c(%rsp)
  test   %rdi,%rdi
  je     L269
  test   %rsi,%rsi
  je     L269
  movl   $0x0,(%rsi)
  lea    D29(%rip),%rsi
  call   <fopen@plt>
  mov    %rax,%r12
  test   %rax,%rax
  je     L229
  lea    0x30(%rsp),%rbx
  mov    %rax,%rdx
  mov    $0x40,%esi
  mov    %rbx,%rdi
  call   <fgets@plt>
  mov    $0x8,%edx
  lea    D20(%rip),%rsi
  mov    %rbx,%rdi
  call   <strncmp@plt>
  test   %eax,%eax
  jne    L236
  lea    0x1c(%rsp),%rcx
  lea    0x20(%rsp),%rdx
  lea    D1(%rip),%rsi
  mov    %r12,%rdi
  call   <fscanf@plt>
  mov    $0x0,%r14d
  cmp    $0x2,%eax
  je     L149
L102:
  mov    %r12,%rdi
  call   <fclose@plt>
  test   %r14,%r14
  je     L127
  mov    %r14,%rdi
  call   <free@plt>
  mov    $0x0,%r14d
  jmp    L127
L149:
  mov    $0x4,%esi
  mov    0x1c(%rsp),%edi
  call   <calloc@plt>
  mov    %rax,%r14
  test   %rax,%rax
  je     L102
  mov    0x1c(%rsp),%eax
  test   %eax,%eax
  je     L14
  mov    $0x0,%ebx
  lea    D32(%rip),%rbp
L161:
  lea    0x28(%rsp),%rcx
  lea    0x24(%rsp),%rdx
  lea    0x2c(%rsp),%r8
  mov    %rbp,%rsi
  mov    %r12,%rdi
  mov    $0x0,%eax
  call   <fscanf@plt>
  cmp    $0x3,%eax
  jne    L102
  mov    %ebx,%ecx
  mov    0x24(%rsp),%eax
  shl    $0x10,%eax
  mov    0x28(%rsp),%edx
  shl    $0x8,%edx
  or     %edx,%eax
  or     0x2c(%rsp),%eax
  mov    %eax,(%r14,%rcx,4)
  add    $0x1,%ebx
  mov    0x1c(%rsp),%eax
  cmp    %ebx,%eax
  ja     L161
L14:
  mov    0x8(%rsp),%rcx
  mov    %eax,(%rcx)
  mov    %r12,%rdi
  call   <fclose@plt>
  jmp    L127
L236:
  mov    %r12,%rdi
  call   <rewind@plt>
  mov    $0x4,%esi
  mov    0x1c(%rsp),%edi
  call   <calloc@plt>
  mov    %rax,%r14
  test   %rax,%rax
  je     L216
  mov    $0x0,%r13d
  lea    0x30(%rsp),%r15
  jmp    L285
L106:
  movzbl (%rbx),%eax
  mov    %rbx,%rbp
  test   %al,%al
  je     L285
L32:
  cmp    $0x3b,%al
  je     L280
  add    $0x1,%rbp
  movzbl 0x0(%rbp),%eax
  test   %al,%al
  jne    L32
  jmp    L333
L280:
  movb   $0x0,0x0(%rbp)
L333:
  sub    $0x1,%rbp
  cmp    %rbx,%rbp
  jbe    L285
L71:
  movsbl 0x0(%rbp),%edi
  call   <isspace@plt>
  test   %rax,%rax
  je     L103
  movb   $0x0,0x0(%rbp)
  sub    $0x1,%rbp
  cmp    %rbx,%rbp
  jne    L71
L285:
  mov    %r12,%rdx
  mov    $0x40,%esi
  mov    %r15,%rdi
  call   <fgets@plt>
  cmp    $0xff,%r13d
  ja     L249
  test   %rax,%rax
  je     L249
  movzbl 0x30(%rsp),%edi
  lea    0x30(%rsp),%rbx
  test   %dil,%dil
  je     L285
L177:
  movsbl %dil,%edi
  call   <isspace@plt>
  test   %rax,%rax
  je     L106
  add    $0x1,%rbx
  movzbl (%rbx),%edi
  test   %dil,%dil
  jne    L177
  jmp    L285
L103:
  cmp    %rbx,%rbp
  jbe    L285
  lea    0x1(%r13),%ebp
  mov    %r13d,%r13d
  lea    (%r14,%r13,4),%r13
  mov    %rbx,%rdi
  call   <bm_atoi@plt>
  mov    %eax,0x0(%r13)
  mov    0x1c(%rsp),%eax
  mov    %ebp,%r13d
  cmp    %ebp,%eax
  jne    L285
  lea    (%rax,%rax,1),%edx
  mov    %edx,0x1c(%rsp)
  lea    0x0(,%rax,8),%esi
  mov    %r14,%rdi
  call   <realloc@plt>
  test   %rax,%rax
  je     L102
  mov    %rax,%r14
  jmp    L285
L249:
  test   %r13d,%r13d
  je     L102
  mov    %r12,%rdi
  call   <fclose@plt>
  mov    0x8(%rsp),%rax
  mov    %r13d,(%rax)
  jmp    L127
L269:
  mov    $0x0,%r14d
L127:
  mov    0x78(%rsp),%rax
  sub    %fs:0x28,%rax
  jne    L158
  mov    %r14,%rax
  add    $0x88,%rsp
  pop    %rbx
  pop    %rbp
  pop    %r12
  pop    %r13
  pop    %r14
  pop    %r15
  ret
L229:
  mov    %rax,%r14
  jmp    L127
L216:
  mov    %r12,%rdi
  call   <fclose@plt>
  jmp    L127
L158:
  call   <__stack_chk_fail@plt>
```

tool_calls: name: read_data
arguments: label: D29
datatype: string
name: read_data
arguments: label: D20
datatype: string
name: read_data
arguments: label: D1
datatype: string
name: read_data
arguments: label: D32
datatype: string

tool: D29: r

tool: D20: JASC-PAL

tool: D1: %d %u

tool: D32: %u %u %u

assistant: ```c
unsigned int *bm_load_palette(const char *filename, unsigned int *npal) {
  unsigned int *pal = NULL, n = 0, an = 8;
  FILE *f = NULL;
  char buf[64];
  if (!filename || !npal)
    return NULL;
  *npal = 0;
  f = fopen(filename, "r");
  if (!f)
    return NULL;
  fgets(buf, sizeof buf, f);
  if (!strncmp(buf, "JASC-PAL", 8)) {
    int version;
    if (fscanf(f, "%d %u", &version, &an) != 2)
      goto error;
    (void)version;
    pal = calloc(an, sizeof *pal);
    if (!pal)
      goto error;
    for (n = 0; n < an; n++) {
      unsigned int r, g, b;
      if (fscanf(f, "%u %u %u", &r, &g, &b) != 3)
        goto error;
      pal[n] = (r << 16) | (g << 8) | b;
    }
    *npal = an;
    fclose(f);
    return pal;
  } else
    rewind(f);
  pal = calloc(an, sizeof *pal);
  if (!pal)
    goto error;
  while (fgets(buf, sizeof buf, f) && n < 256) {
    char *s, *e, *c = buf;
    while (*c && isspace(*c))
      c++;
    s = c;
    if (!*s)
      continue;
    while (*c) {
      if (*c == ';') {
        *c = '\0';
        break;
      }
      c++;
    }
    e = c - 1;
    while (e > s && isspace(*e)) {
      *e = '\0';
      e--;
    }
    if (e <= s)
      continue;
    pal[n++] = bm_atoi(s);
    if (n == an) {
      an <<= 1;
      void *tmp = realloc(pal, an * sizeof *pal);
      if (!tmp)
        goto error;
      pal = tmp;
    }
  }
  if (n == 0)
    goto error;
  fclose(f);
  *npal = n;
  return pal;
error:
  fclose(f);
  if (pal)
    free(pal);
  return NULL;
}
```