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

```asm
<gms_establish_filenames>:
  endbr64
  push   %r15
  push   %r14
  push   %r13
  push   %r12
  push   %rbp
  push   %rbx
  sub    $0x18,%rsp
  mov    %rdi,%rbp
  mov    %rsi,%r15
  mov    %rdx,(%rsp)
  mov    %rcx,0x8(%rsp)
  test   %rdi,%rdi
  je     L73
  test   %rsi,%rsi
  je     L73
  test   %rdx,%rdx
  setne  %al
  test   %rcx,%rcx
  setne  %dil
  movzbl %dil,%edi
  and    %eax,%edi
  jmp    L33
L73:
  mov    $0x0,%edi
L33:
  call   <assert@plt>
  mov    %rbp,%rdi
  call   <strlen@plt>
  lea    0x1(%rax),%rdi
  call   <gms_malloc@plt>
  mov    %rax,%rbx
  mov    %rbp,%rsi
  mov    %rax,%rdi
  call   <strcpy@plt>
  mov    %rbx,%rdi
  call   <strlen@plt>
  cmp    $0x4,%rax
  ja     L175
L5:
  mov    %rbx,%rdi
  call   <strlen@plt>
  lea    0x5(%rax),%rdi
  call   <gms_malloc@plt>
  mov    %rax,%rbp
  mov    %rbx,%rsi
  mov    %rax,%rdi
  call   <strcpy@plt>
  lea    D12(%rip),%rsi
  mov    %rbp,%rdi
  call   <strcat@plt>
  lea    D62(%rip),%rsi
  mov    %rbp,%rdi
  call   <fopen@plt>
  mov    %rax,%rdi
  test   %rax,%rax
  je     L145
L100:
  call   <fclose@plt>
  mov    %rbx,%rdi
  call   <strlen@plt>
  lea    0x5(%rax),%rdi
  call   <gms_malloc@plt>
  mov    %rax,%r12
  mov    %rbx,%rsi
  mov    %rax,%rdi
  call   <strcpy@plt>
  lea    D104(%rip),%rsi
  mov    %r12,%rdi
  call   <strcat@plt>
  lea    D62(%rip),%rsi
  mov    %r12,%rdi
  call   <fopen@plt>
  mov    %rax,%r13
  test   %rax,%rax
  je     L134
L2:
  mov    %r13,%rdi
  call   <fclose@plt>
L123:
  mov    %rbx,%rdi
  call   <strlen@plt>
  lea    0x5(%rax),%rdi
  call   <gms_malloc@plt>
  mov    %rax,%r13
  mov    %rbx,%rsi
  mov    %rax,%rdi
  call   <strcpy@plt>
  lea    D125(%rip),%rsi
  mov    %r13,%rdi
  call   <strcat@plt>
  lea    D62(%rip),%rsi
  mov    %r13,%rdi
  call   <fopen@plt>
  mov    %rax,%r14
  test   %rax,%rax
  je     L10
L164:
  mov    %r14,%rdi
  call   <fclose@plt>
L37:
  mov    %rbp,(%r15)
  mov    (%rsp),%rax
  mov    %r12,(%rax)
  mov    0x8(%rsp),%rax
  mov    %r13,(%rax)
  mov    %rbx,%rdi
  call   <free@plt>
L119:
  add    $0x18,%rsp
  pop    %rbx
  pop    %rbp
  pop    %r12
  pop    %r13
  pop    %r14
  pop    %r15
  ret
L175:
  lea    -0x4(%rbx,%rax,1),%rdi
  lea    D12(%rip),%rsi
  call   <gms_strcasecmp@plt>
  test   %rax,%rax
  jne    L55
L115:
  mov    %rbx,%rdi
  call   <strlen@plt>
  movb   $0x0,-0x4(%rbx,%rax,1)
  jmp    L5
L55:
  mov    %rbx,%rdi
  call   <strlen@plt>
  lea    -0x4(%rbx,%rax,1),%rdi
  lea    D104(%rip),%rsi
  call   <gms_strcasecmp@plt>
  test   %rax,%rax
  je     L115
  mov    %rbx,%rdi
  call   <strlen@plt>
  lea    -0x4(%rbx,%rax,1),%rdi
  lea    D125(%rip),%rsi
  call   <gms_strcasecmp@plt>
  test   %rax,%rax
  jne    L5
  jmp    L115
L145:
  mov    %rbx,%rsi
  mov    %rbp,%rdi
  call   <strcpy@plt>
  lea    D106(%rip),%rsi
  mov    %rbp,%rdi
  call   <strcat@plt>
  lea    D62(%rip),%rsi
  mov    %rbp,%rdi
  call   <fopen@plt>
  mov    %rax,%rdi
  test   %rax,%rax
  jne    L100
  movq   $0x0,(%r15)
  mov    (%rsp),%rax
  movq   $0x0,(%rax)
  mov    0x8(%rsp),%rax
  movq   $0x0,(%rax)
  mov    %rbp,%rdi
  call   <free@plt>
  mov    %rbx,%rdi
  call   <free@plt>
  jmp    L119
L134:
  mov    %rbx,%rsi
  mov    %r12,%rdi
  call   <strcpy@plt>
  lea    D66(%rip),%rsi
  mov    %r12,%rdi
  call   <strcat@plt>
  lea    D62(%rip),%rsi
  mov    %r12,%rdi
  call   <fopen@plt>
  mov    %rax,%r13
  test   %rax,%rax
  jne    L2
  mov    %r12,%rdi
  call   <free@plt>
  mov    %r13,%r12
  jmp    L123
L10:
  mov    %rbx,%rsi
  mov    %r13,%rdi
  call   <strcpy@plt>
  lea    D56(%rip),%rsi
  mov    %r13,%rdi
  call   <strcat@plt>
  lea    D62(%rip),%rsi
  mov    %r13,%rdi
  call   <fopen@plt>
  mov    %rax,%r14
  test   %rax,%rax
  jne    L164
  mov    %r13,%rdi
  call   <free@plt>
  mov    %r14,%r13
  jmp    L37
```

tool_calls: name: read_data
arguments: tag: D12
datatype: string
name: read_data
arguments: tag: D62
datatype: string
name: read_data
arguments: tag: D104
datatype: string
name: read_data
arguments: tag: D125
datatype: string
name: read_data
arguments: tag: D106
datatype: string
name: read_data
arguments: tag: D66
datatype: string
name: read_data
arguments: tag: D56
datatype: string

tool: D12: .MAG

tool: D62: rb

tool: D104: .GFX

tool: D125: .HNT

tool: D106: .mag

tool: D66: .gfx

tool: D56: .hnt

assistant: ```c
void gms_establish_filenames(char *name, char **text, char **graphics, char **hints) {
  char *base, *text_file, *graphics_file, *hints_file;
  FILE *stream;
  assert(name && text && graphics && hints);
  base = gms_malloc(strlen(name) + 1);
  strcpy(base, name);
  if (strlen(base) > strlen(".XXX")) {
    if (gms_strcasecmp(base + strlen(base) - strlen(".MAG"), ".MAG") == 0 || gms_strcasecmp(base + strlen(base) - strlen(".GFX"), ".GFX") == 0 || gms_strcasecmp(base + strlen(base) - strlen(".HNT"), ".HNT") == 0)
      base[strlen(base) - strlen(".XXX")] = '\0';
  }
  text_file = gms_malloc(strlen(base) + strlen(".MAG") + 1);
  strcpy(text_file, base);
  strcat(text_file, ".MAG");
  stream = fopen(text_file, "rb");
  if (!stream) {
    strcpy(text_file, base);
    strcat(text_file, ".mag");
    stream = fopen(text_file, "rb");
    if (!stream) {
      *text = NULL;
      *graphics = NULL;
      *hints = NULL;
      free(text_file);
      free(base);
      return;
    }
  }
  if (stream)
    fclose(stream);
  graphics_file = gms_malloc(strlen(base) + strlen(".GFX") + 1);
  strcpy(graphics_file, base);
  strcat(graphics_file, ".GFX");
  stream = fopen(graphics_file, "rb");
  if (!stream) {
    strcpy(graphics_file, base);
    strcat(graphics_file, ".gfx");
    stream = fopen(graphics_file, "rb");
    if (!stream) {
      free(graphics_file);
      graphics_file = NULL;
    }
  }
  if (stream)
    fclose(stream);
  hints_file = gms_malloc(strlen(base) + strlen(".HNT") + 1);
  strcpy(hints_file, base);
  strcat(hints_file, ".HNT");
  stream = fopen(hints_file, "rb");
  if (!stream) {
    strcpy(hints_file, base);
    strcat(hints_file, ".hnt");
    stream = fopen(hints_file, "rb");
    if (!stream) {
      free(hints_file);
      hints_file = NULL;
    }
  }
  if (stream)
    fclose(stream);
  *text = text_file;
  *graphics = graphics_file;
  *hints = hints_file;
  free(base);
}
```