user: Please show me the C implementation that matches the assembly code listed below:

```asm
<generate_filename>:
  endbr64
  push   %r15
  lea    D79(%rip),%r10
  mov    %edx,%r15d
  lea    D67(%rip),%r11
  push   %r14
  lea    -0x1(%rdi),%rdx
  mov    %ecx,%r14d
  push   %r13
  mov    %rsi,%r13
  push   %r12
  push   %rbp
  lea    0xffa(%rsi),%rbp
  push   %rbx
  sub    $0x38,%rsp
  mov    %fs:0x28,%rax
  mov    %rax,0x28(%rsp)
  xor    %eax,%eax
  lea    0x20(%rsp),%rdi
  lea    -0x1(%rsi),%rax
  jmp    L191
  nop
L63:
  cmp    %rax,%rbp
  jbe    L220
  cmp    $0x25,%cl
  je     L99
L191:
  mov    %rdx,%r12
  movzbl 0x1(%rdx),%ecx
  mov    %rax,%rbx
  add    $0x1,%rdx
  add    $0x1,%rax
  mov    %cl,(%rax)
  test   %cl,%cl
  jne    L63
L220:
  movb   $0x0,(%rax)
  mov    0x28(%rsp),%rax
  sub    %fs:0x28,%rax
  jne    L201
  add    $0x38,%rsp
  mov    %r13,%rax
  pop    %rbx
  pop    %rbp
  pop    %r12
  pop    %r13
  pop    %r14
  pop    %r15
  ret
  nopl   0x0(%rax,%rax,1)
L99:
  movzbl 0x1(%rdx),%ecx
  cmp    $0x65,%cl
  je     L249
  jg     L231
  cmp    $0x25,%cl
  je     L267
  cmp    $0x63,%cl
  jne    L257
  test   %r8d,%r8d
  jne    L250
  cs nopw 0x0(%rax,%rax,1)
L267:
  lea    0x2(%r12),%rdx
  mov    %rbx,%rax
  jmp    L191
  nopw   0x0(%rax,%rax,1)
L231:
  cmp    $0x78,%cl
  je     L278
  cmp    $0x79,%cl
  jne    L257
  test   %r8d,%r8d
  je     L267
  mov    %r14d,%edx
  mov    %r10,%rsi
  xor    %eax,%eax
  mov    %r9,0x18(%rsp)
  mov    %r8d,0x14(%rsp)
  mov    %rdi,0x8(%rsp)
  call   <sprintf@plt>
  mov    0x14(%rsp),%r8d
  mov    0x18(%rsp),%r9
  lea    0x1f(%rsp),%rdx
  mov    0x8(%rsp),%rdi
  lea    D67(%rip),%r11
  lea    D79(%rip),%r10
  jmp    L322
L91:
  cmp    %rbx,%rbp
  jbe    L1
L322:
  movzbl 0x1(%rdx),%ecx
  mov    %rbx,%rax
  add    $0x1,%rdx
  add    $0x1,%rbx
  mov    %cl,(%rbx)
  test   %cl,%cl
  jne    L91
L1:
  movzbl 0x2(%r12),%ecx
  lea    0x2(%r12),%rdx
  jmp    L100
  nopw   0x0(%rax,%rax,1)
L278:
  test   %r8d,%r8d
  je     L267
  mov    %r15d,%edx
  mov    %r10,%rsi
  xor    %eax,%eax
  mov    %r9,0x18(%rsp)
  mov    %r8d,0x14(%rsp)
  mov    %rdi,0x8(%rsp)
  call   <sprintf@plt>
  mov    0x14(%rsp),%r8d
  mov    0x18(%rsp),%r9
  lea    0x1f(%rsp),%rdx
  mov    0x8(%rsp),%rdi
  lea    D67(%rip),%r11
  lea    D79(%rip),%r10
  jmp    L54
L321:
  test   %cl,%cl
  je     L1
L54:
  movzbl 0x1(%rdx),%ecx
  mov    %rbx,%rax
  add    $0x1,%rbx
  add    $0x1,%rdx
  mov    %cl,(%rbx)
  cmp    %rbx,%rbp
  ja     L321
  jmp    L1
  nopl   (%rax)
L257:
  movzbl 0x1(%r12),%ecx
L100:
  test   %cl,%cl
  jne    L191
  jmp    L220
L249:
  lea    -0x1(%r9),%rdx
  jmp    L116
  nopl   0x0(%rax)
L241:
  test   %cl,%cl
  je     L47
L116:
  movzbl 0x1(%rdx),%ecx
  mov    %rbx,%rax
  add    $0x1,%rbx
  add    $0x1,%rdx
  mov    %cl,(%rbx)
  cmp    %rbx,%rbp
  ja     L241
L47:
  movzbl 0x2(%r12),%ecx
  lea    0x2(%r12),%rdx
  jmp    L100
L250:
  mov    %r15d,%edx
  mov    %r11,%rsi
  mov    %r14d,%ecx
  xor    %eax,%eax
  mov    %r9,0x18(%rsp)
  mov    %r8d,0x14(%rsp)
  mov    %rdi,0x8(%rsp)
  call   <sprintf@plt>
  mov    0x14(%rsp),%r8d
  mov    0x18(%rsp),%r9
  lea    0x1f(%rsp),%rdx
  mov    0x8(%rsp),%rdi
  lea    D79(%rip),%r10
  lea    D67(%rip),%r11
  jmp    L140
L218:
  cmp    %rbx,%rbp
  jbe    L1
L140:
  movzbl 0x1(%rdx),%ecx
  mov    %rbx,%rax
  add    $0x1,%rdx
  add    $0x1,%rbx
  mov    %cl,(%rbx)
  test   %cl,%cl
  jne    L218
  jmp    L1
L201:
  call   <__stack_chk_fail@plt>
```

tool_calls: name: disassemble_data
arguments: data_tag: D79
type_infered: string
name: disassemble_data
arguments: data_tag: D67
type_infered: string

tool: D79: %i

tool: D67: _%i_%i

assistant: ```c
char *generate_filename(const char *template, char *result, int x, int y, int pages, const char *extension) {
  char *a;
  const char *b;
  char *end = result + 4090;
  b = template - 1, a = result - 1;
  while ((*++a = *++b)) {
    if (a >= end)
      break;
    if (*b == '%') {
      switch (b[1]) {
      case 'x':
        a--;
        if (pages) {
          char text[8];
          char *e = text - 1;
          sprintf(text, "%i", x);
          while ((*++a = *++e))
            if (a >= end)
              break;
          a--;
        }
        b++;
        break;
      case 'y':
        a--;
        if (pages) {
          char text[8];
          char *e = text - 1;
          sprintf(text, "%i", y);
          while ((*++a = *++e))
            if (a >= end)
              break;
          a--;
        }
        b++;
        break;
      case 'c':
        a--;
        if (pages) {
          char text[8];
          char *e = text - 1;
          sprintf(text, "_%i_%i", x, y);
          while ((*++a = *++e))
            if (a >= end)
              break;
          a--;
        }
        b++;
        break;
      case 'e':
        a--;
        {
          const char *e = extension - 1;
          while ((*++a = *++e))
            if (a >= end)
              break;
          a--;
        }
        b++;
        break;
      case '%':
        a--;
        b++;
        break;
      }
      if (!*b)
        break;
    }
  }
  *a = 0;
  return result;
}
```