// File: /workspace/github_c_makefile_star2_O2/sisshiki1969[P]monocc/src/codegen.c
// Function: gen_call
void gen_call(Node *node) {
  // function call
  // kind: ND_CALL
  // lhs: callee (ND_IDENT)
  // nodes: vector of arguments
  // token: Token *token
  // type: return type of callee
  // TODO: Currently, only ND_IDENT is allowed as a callee.
  int len = vec_len(node->nodes);
  Node **args = node->nodes->data;
  for (int i = len - 1; i >= 0; i--) {
    gen(args[i]);
  }
  for (int i = 0; i < len; i++) {
    fprintf(output, "\tpop  %s\n", registers[0][i]);
  }
  Token *name;
  if (node->lhs->kind == ND_IDENT) {
    name = node->lhs->token;
    if (cmp_token_str(name, "__builtin_va_start")) {
      builtin_va_start(node);
      return;
    }
  } else {
    error_at_node(node->lhs, "Currently, callee must be function designator.");
  }

  char *label1 = new_label();
  char *label2 = new_label();
  fprintf(output, "\tmov  rax, rsp\n");
  fprintf(output, "\tand  rax, 15\n");
  fprintf(output, "\tjnz  %s\n", label1);
  fprintf(output, "\tmov  rax, 0\n");
  fprintf(output, "\tcall %.*s\n", name->len, name->str);
  emit_jmp(label2);

  emit_label(label1);
  fprintf(output, "\tsub  rsp, 8\n");
  fprintf(output, "\tmov  rax, 0\n");
  fprintf(output, "\tcall %.*s\n", name->len, name->str);
  fprintf(output, "\tadd  rsp, 8\n");

  emit_label(label2);
  fprintf(output, "\tpush rax\n");
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rdi, %rbx
movq 0x48(%rdi), %rdi
callq 0x34f2
movq 0x48(%rbx), %rcx
movq (%rcx), %r14
movl %eax, %r12d
movq %r12, %r15
testl %r15d, %r15d
jle 0x248c
movq -0x8(%r14,%r15,8), %rdi
decq %r15
callq 0x17e5
jmp 0x2478
xorl %eax, %eax
testl %r12d, %r12d
cmovlel %eax, %r12d
leaq 0xed84(%rip), %r15      # 0x11220
leaq 0xec7d(%rip), %r13      # 0x11120
leaq 0x8087(%rip), %r14      # 0xa531
xorl %ebp, %ebp
cmpq %rbp, %r12
je 0x24c8
movq (%r15), %rdi
movq (%r13,%rbp,8), %rdx
movq %r14, %rsi
xorl %eax, %eax
callq 0x10f0
incq %rbp
jmp 0x24ac
movq 0x8(%rbx), %rdi
cmpl $0x0, (%rdi)
je 0x24e1
leaq 0x8076(%rip), %rsi      # 0xa54e
xorl %eax, %eax
callq 0x4a9e
jmp 0x250e
movq 0x50(%rdi), %r14
leaq 0x804f(%rip), %rsi      # 0xa53b
movq %r14, %rdi
callq 0x5a98
testb %al, %al
je 0x250e
movq %rbx, %rdi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x23cb
callq 0x12e8
movq %rax, %r12
callq 0x12e8
movq %rax, %rbp
movq (%r15), %rcx
leaq 0x8055(%rip), %rdi      # 0xa57d
pushq $0xf
popq %rsi
pushq $0x1
popq %rbx
movq %rbx, %rdx
callq 0x1190
movq (%r15), %rcx
leaq 0x804d(%rip), %rdi      # 0xa58d
pushq $0xe
popq %rsi
movq %rbx, %rdx
callq 0x1190
movq (%r15), %rdi
leaq 0x8047(%rip), %rsi      # 0xa59c
movq %r12, %rdx
xorl %eax, %eax
callq 0x10f0
movq (%r15), %rcx
leaq 0x803d(%rip), %rdi      # 0xa5a6
pushq $0xd
popq %r13
movq %r13, %rsi
movq %rbx, %rdx
callq 0x1190
movq (%r15), %rdi
movl 0x28(%r14), %edx
movq 0x20(%r14), %rcx
leaq 0x802a(%rip), %rsi      # 0xa5b4
xorl %eax, %eax
callq 0x10f0
movq %rbp, %rdi
callq 0x1347
movq %r12, %rdi
callq 0x132c
movq (%r15), %rcx
leaq 0x8015(%rip), %rdi      # 0xa5c0
movq %r13, %rsi
movq %rbx, %rdx
callq 0x1190
movq (%r15), %rcx
leaq 0x7fe6(%rip), %rdi      # 0xa5a6
movq %r13, %rsi
movq %rbx, %rdx
callq 0x1190
movq (%r15), %rdi
movl 0x28(%r14), %edx
movq 0x20(%r14), %rcx
leaq 0x7fd7(%rip), %rsi      # 0xa5b4
xorl %eax, %eax
callq 0x10f0
movq (%r15), %rcx
leaq 0x7fe0(%rip), %rdi      # 0xa5ce
movq %r13, %rsi
movq %rbx, %rdx
callq 0x1190
movq %rbp, %rdi
callq 0x132c
movq (%r15), %rcx
leaq 0x7d2e(%rip), %rdi      # 0xa339
pushq $0xa
popq %rsi
movq %rbx, %rdx
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x1190