// File: /workspace/github_c_makefile_star2_O2/sisshiki1969[P]monocc/src/codegen.c
// Function: gen_fdecl
void gen_fdecl(Node *node) {
  if (!node->lhs) return;
  current_fn = node;

  fprintf(output, "%.*s:\n", node->token->len, node->token->str);
  // Prologue
  fprintf(output, "\tpush rbp\n");
  fprintf(output, "\tmov  rbp, rsp\n");
  int offset = ((node->offset - 1) & (-16)) + 16;
  fprintf(output, "\tsub  rsp, %d\n", offset);
  fprintf(output, "\tmov  [rbp -  8], r12\n");
  fprintf(output, "\tmov  [rbp - 16], r13\n");
  fprintf(output, "\tmov  [rbp - 24], r14\n");
  fprintf(output, "\tmov  [rbp - 32], r15\n");

  if (node->type->variadic) {
    fprintf(output, "\tmov  [rbp - 128], rdi\n");
    fprintf(output, "\tmov  [rbp - 120], rsi\n");
    fprintf(output, "\tmov  [rbp - 112], rdx\n");
    fprintf(output, "\tmov  [rbp - 104], rcx\n");
    fprintf(output, "\tmov  [rbp -  96], r8\n");
    fprintf(output, "\tmov  [rbp -  88], r9\n");
  }

  int len = vec_len(node->nodes);
  Node **params = node->nodes->data;
  for (int i = 0; i < len; i++) {
    fprintf(output, "\tmov  [rbp - %d], %s\n", params[i]->lvar->offset,
            registers[reg_size(type(params[i]))][i]);
  }

  gen_block(node->lhs);

  // Epilogue
  emit_return_label(node);
  fprintf(output, "\tmov  r12, [rbp -  8]\n");
  fprintf(output, "\tmov  r13, [rbp - 16]\n");
  fprintf(output, "\tmov  r14, [rbp - 24]\n");
  fprintf(output, "\tmov  r15, [rbp - 32]\n");
  fprintf(output, "\tmov  rsp, rbp\n");
  fprintf(output, "\tpop  rbp\n");
  fprintf(output, "\tret\n");
}

// Assembly:
cmpq $0x0, 0x8(%rdi)
je 0x28e4
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rdi, %r13
movq %rdi, 0xebb4(%rip)      # 0x111f8
leaq 0xebd5(%rip), %rbx      # 0x11220
movq (%rbx), %rdi
movq 0x50(%r13), %rax
movl 0x28(%rax), %edx
movq 0x20(%rax), %rcx
leaq 0x7b34(%rip), %rsi      # 0xa194
xorl %r12d, %r12d
xorl %eax, %eax
callq 0x10f0
movq (%rbx), %rcx
leaq 0x7f68(%rip), %rdi      # 0xa5dc
pushq $0xa
popq %rsi
pushq $0x1
popq %r14
movq %r14, %rdx
callq 0x1190
movq (%rbx), %rcx
leaq 0x7f5a(%rip), %rdi      # 0xa5e7
pushq $0xf
popq %rsi
movq %r14, %rdx
callq 0x1190
movl 0x38(%r13), %edx
decl %edx
andl $-0x10, %edx
addl $0x10, %edx
movq (%rbx), %rdi
leaq 0x7f49(%rip), %rsi      # 0xa5f7
xorl %eax, %eax
callq 0x10f0
movq (%rbx), %rcx
leaq 0x7f47(%rip), %rdi      # 0xa606
pushq $0x16
popq %r15
movq %r15, %rsi
movq %r14, %rdx
callq 0x1190
movq (%rbx), %rcx
leaq 0x7f45(%rip), %rdi      # 0xa61d
movq %r15, %rsi
movq %r14, %rdx
callq 0x1190
movq (%rbx), %rcx
leaq 0x7f47(%rip), %rdi      # 0xa634
movq %r15, %rsi
movq %r14, %rdx
callq 0x1190
movq (%rbx), %rcx
leaq 0x7f49(%rip), %rdi      # 0xa64b
movq %r15, %rsi
movq %r14, %rdx
callq 0x1190
movq 0x58(%r13), %rax
cmpb $0x1, 0x38(%rax)
jne 0x27ac
leaq 0xeafe(%rip), %rbx      # 0x11220
movq (%rbx), %rcx
leaq 0x7f36(%rip), %rdi      # 0xa662
pushq $0x17
popq %r15
pushq $0x1
popq %r14
movq %r15, %rsi
movq %r14, %rdx
callq 0x1190
movq (%rbx), %rcx
leaq 0x7f31(%rip), %rdi      # 0xa67a
movq %r15, %rsi
movq %r14, %rdx
callq 0x1190
movq (%rbx), %rcx
leaq 0x7f34(%rip), %rdi      # 0xa692
movq %r15, %rsi
movq %r14, %rdx
callq 0x1190
movq (%rbx), %rcx
leaq 0x7f37(%rip), %rdi      # 0xa6aa
movq %r15, %rsi
movq %r14, %rdx
callq 0x1190
movq (%rbx), %rcx
leaq 0x7f3a(%rip), %rdi      # 0xa6c2
pushq $0x16
popq %r15
movq %r15, %rsi
movq %r14, %rdx
callq 0x1190
movq (%rbx), %rcx
leaq 0x7f38(%rip), %rdi      # 0xa6d9
movq %r15, %rsi
movq %r14, %rdx
callq 0x1190
movq 0x48(%r13), %rdi
callq 0x34f2
movl %eax, %r14d
movq %r13, (%rsp)
movq 0x48(%r13), %rax
testl %r14d, %r14d
cmovlel %r12d, %r14d
movq (%rax), %rbx
leaq 0x7f1f(%rip), %r15      # 0xa6f0
xorl %r13d, %r13d
cmpq %r13, %r14
je 0x2820
leaq 0xea40(%rip), %rax      # 0x11220
movq (%rax), %r12
movq (%rbx,%r13,8), %rdi
movq 0x30(%rdi), %rax
movl 0x18(%rax), %ebp
callq 0x9610
movq %rax, %rdi
callq 0x14be
cltq
imulq $0x30, %rax, %rax
addq 0xe7e0(%rip), %rax      # 0x10fe8
movq (%rax,%r13,8), %rcx
movq %r12, %rdi
movq %r15, %rsi
movl %ebp, %edx
xorl %eax, %eax
callq 0x10f0
incq %r13
jmp 0x27d4
movq (%rsp), %rbx
movq 0x8(%rbx), %rdi
callq 0x238d
movq %rbx, %rdi
callq 0x1362
leaq 0xe9e4(%rip), %r15      # 0x11220
movq (%r15), %rcx
leaq 0x7ec0(%rip), %rdi      # 0xa706
pushq $0x16
popq %r14
pushq $0x1
popq %rbx
movq %r14, %rsi
movq %rbx, %rdx
callq 0x1190
movq (%r15), %rcx
leaq 0x7ebb(%rip), %rdi      # 0xa71d
movq %r14, %rsi
movq %rbx, %rdx
callq 0x1190
movq (%r15), %rcx
leaq 0x7ebd(%rip), %rdi      # 0xa734
movq %r14, %rsi
movq %rbx, %rdx
callq 0x1190
movq (%r15), %rcx
leaq 0x7ebf(%rip), %rdi      # 0xa74b
movq %r14, %rsi
movq %rbx, %rdx
callq 0x1190
movq (%r15), %rcx
leaq 0x7ec1(%rip), %rdi      # 0xa762
pushq $0xf
popq %rsi
movq %rbx, %rdx
callq 0x1190
movq (%r15), %rcx
leaq 0x7ebc(%rip), %rdi      # 0xa772
pushq $0xa
popq %rsi
movq %rbx, %rdx
callq 0x1190
movq (%r15), %rcx
leaq 0x7eb2(%rip), %rdi      # 0xa77d
pushq $0x5
popq %rsi
movq %rbx, %rdx
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x1190
retq