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

```asm
<gms_establish_filenames>:
  endbr64
  push   %r15
  push   %r14
  push   %r13
  push   %r12
  mov    %rdi,%r12
  push   %rbp
  push   %rbx
  mov    %rsi,%rbx
  sub    $0x18,%rsp
  mov    %rdx,(%rsp)
  mov    %rcx,0x8(%rsp)
  test   %rdi,%rdi
  je     L20
  test   %rsi,%rsi
  je     L20
  test   %rdx,%rdx
  setne  %al
  xor    %edi,%edi
  test   %rcx,%rcx
  setne  %dil
  and    %eax,%edi
  jmp    L75
  xchg   %ax,%ax
L20:
  xor    %edi,%edi
L75:
  call   <assert@plt>
  mov    %r12,%rdi
  lea    D31(%rip),%r13
  call   <strlen@plt>
  lea    0x1(%rax),%rdi
  call   <gms_malloc@plt>
  mov    %r12,%rsi
  mov    %rax,%rbp
  mov    %rax,%rdi
  call   <strcpy@plt>
  mov    %rbp,%rdi
  call   <strlen@plt>
  cmp    $0x4,%rax
  ja     L54
L174:
  lea    0x5(%rax),%rdi
  lea    D33(%rip),%r15
  call   <gms_malloc@plt>
  mov    %rbp,%rsi
  mov    %rax,%r12
  mov    %rax,%rdi
  call   <strcpy@plt>
  mov    %r13,%rsi
  mov    %r12,%rdi
  call   <strcat@plt>
  mov    %r12,%rdi
  mov    %r15,%rsi
  call   <fopen@plt>
  mov    %rax,%rdi
  test   %rax,%rax
  je     L138
L51:
  call   <fclose@plt>
  mov    %rbp,%rdi
  call   <strlen@plt>
  lea    0x5(%rax),%rdi
  call   <gms_malloc@plt>
  mov    %rbp,%rsi
  mov    %rax,%r13
  mov    %rax,%rdi
  call   <strcpy@plt>
  lea    D21(%rip),%rsi
  mov    %r13,%rdi
  call   <strcat@plt>
  mov    %r13,%rdi
  mov    %r15,%rsi
  call   <fopen@plt>
  mov    %rax,%rdi
  test   %rax,%rax
  je     L120
L148:
  call   <fclose@plt>
L131:
  mov    %rbp,%rdi
  call   <strlen@plt>
  lea    0x5(%rax),%rdi
  call   <gms_malloc@plt>
  mov    %rbp,%rsi
  mov    %rax,%r14
  mov    %rax,%rdi
  call   <strcpy@plt>
  lea    D73(%rip),%rsi
  mov    %r14,%rdi
  call   <strcat@plt>
  mov    %r14,%rdi
  mov    %r15,%rsi
  call   <fopen@plt>
  mov    %rax,%rdi
  test   %rax,%rax
  je     L5
L91:
  call   <fclose@plt>
L114:
  mov    (%rsp),%rax
  mov    %r12,(%rbx)
  mov    %r13,(%rax)
  mov    0x8(%rsp),%rax
  mov    %r14,(%rax)
L152:
  add    $0x18,%rsp
  mov    %rbp,%rdi
  pop    %rbx
  pop    %rbp
  pop    %r12
  pop    %r13
  pop    %r14
  pop    %r15
  jmp    10d0 <free@plt>
L54:
  lea    -0x4(%rbp,%rax,1),%rdi
  mov    %r13,%rsi
  call   <gms_strcasecmp@plt>
  test   %rax,%rax
  jne    L116
L70:
  mov    %rbp,%rdi
  call   <strlen@plt>
  movb   $0x0,-0x4(%rbp,%rax,1)
L78:
  mov    %rbp,%rdi
  call   <strlen@plt>
  jmp    L174
L138:
  mov    %rbp,%rsi
  mov    %r12,%rdi
  call   <strcpy@plt>
  lea    D8(%rip),%rsi
  mov    %r12,%rdi
  call   <strcat@plt>
  mov    %r12,%rdi
  mov    %r15,%rsi
  call   <fopen@plt>
  mov    %rax,%rdi
  test   %rax,%rax
  jne    L51
  mov    (%rsp),%rax
  movq   $0x0,(%rbx)
  mov    %r12,%rdi
  movq   $0x0,(%rax)
  mov    0x8(%rsp),%rax
  movq   $0x0,(%rax)
  call   <free@plt>
  jmp    L152
  nopl   0x0(%rax)
L120:
  mov    %rbp,%rsi
  mov    %r13,%rdi
  call   <strcpy@plt>
  lea    D65(%rip),%rsi
  mov    %r13,%rdi
  call   <strcat@plt>
  mov    %r13,%rdi
  mov    %r15,%rsi
  call   <fopen@plt>
  mov    %rax,%rdi
  test   %rax,%rax
  jne    L148
  mov    %r13,%rdi
  xor    %r13d,%r13d
  call   <free@plt>
  jmp    L131
  nopl   0x0(%rax)
L5:
  mov    %rbp,%rsi
  mov    %r14,%rdi
  call   <strcpy@plt>
  lea    D68(%rip),%rsi
  mov    %r14,%rdi
  call   <strcat@plt>
  mov    %r14,%rdi
  mov    %r15,%rsi
  call   <fopen@plt>
  mov    %rax,%rdi
  test   %rax,%rax
  jne    L91
  mov    %r14,%rdi
  xor    %r14d,%r14d
  call   <free@plt>
  jmp    L114
  nopl   0x0(%rax)
L116:
  mov    %rbp,%rdi
  call   <strlen@plt>
  lea    D21(%rip),%rsi
  lea    -0x4(%rbp,%rax,1),%rdi
  call   <gms_strcasecmp@plt>
  test   %rax,%rax
  je     L70
  mov    %rbp,%rdi
  call   <strlen@plt>
  lea    D73(%rip),%rsi
  lea    -0x4(%rbp,%rax,1),%rdi
  call   <gms_strcasecmp@plt>
  test   %rax,%rax
  je     L70
  jmp    L78
```

tool_calls: name: read_data
arguments: label: D31
data_type: string
name: read_data
arguments: label: D33
data_type: string
name: read_data
arguments: label: D21
data_type: string
name: read_data
arguments: label: D73
data_type: string
name: read_data
arguments: label: D8
data_type: string
name: read_data
arguments: label: D65
data_type: string
name: read_data
arguments: label: D68
data_type: string

tool: D31: .MAG

tool: D33: rb

tool: D21: .GFX

tool: D73: .HNT

tool: D8: .mag

tool: D65: .gfx

tool: D68: .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);
}
```