// File: /workspace/github_c_makefile_star2_O3/sisshiki1969[P]monocc/src/pp.c
// Function: check_params
void check_params(Macro *m, Token *t, Macro **args) {
  // t->kind == TK_IDENT
  int param_count = 0;
  for (Token *param = m->params; param; param = param->next) param_count++;

  Token *start = t;
  t = t->next;
  if (!t) error_at_token(start, "Expected '('.");
  if (t->kind != TK_OP_PAREN) error_at_token(t, "Expected '('.");
  // t->kind == TK_OP_PAREN
  int arg_count = 0;
  Token anchor;
  while (t->next && t->next->kind != TK_CL_PAREN) {
    // t->next->kind != TK_CL_PAREN
    // invoke function-like macro
    Token *arg_token = t->next;
    while (t->next->kind != TK_CL_PAREN && t->next->kind != TK_COMMA) {
      if (t->next->kind == TK_OP_PAREN) {
        while (t->next->kind != TK_CL_PAREN) {
          t = t->next;
          if (!t->next)
            error_at_token(t, "Improperly terminated macro invocation.");
        }
        // t->next->kind != TK_CL_PAREN
      }
      t = t->next;
      if (!t->next)
        error_at_token(t, "Improperly terminated macro invocation.");
    }
    // t->next->kind == ( TK_CL_PAREN || TK_COMMA )

    Token *next = t->next;
    t->next = NULL;
    anchor.next = next;
    t = &anchor;
    // t->next->kind == ( TK_CL_PAREN || TK_COMMA )
    Macro *arg = calloc(1, sizeof(Macro));
    arg->params = arg_token;
    (*args)->next = arg;
    *args = arg;
    arg_count++;
    if (arg_count > param_count)
      error_at_token(arg_token,
                     "Too many arguments in invocation of macro '%.*s'. "
                     "actual:%d expected:%d",
                     start->len, start->str, arg_count, param_count);
    if (t->next->kind == TK_CL_PAREN) break;
    // t->next->kind == TK_COMMA
    t = t->next;
    // t->kind == TK_COMMA
  }
  // t->next->kind == TK_CL_PAREN or !t->next
  if (!t->next) error_at_token(t, "Expected ')'.");
  // t->next->kind == TK_CL_PAREN
  start->next = t->next->next;

  if (arg_count < param_count)
    error_at_token(start, "Too few arguments for invokation of macro.");
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rdx, 0x10(%rsp)
movq %rsi, %r14
movq 0x10(%rdi), %rax
xorl %ebp, %ebp
testq %rax, %rax
je 0x9135
incl %ebp
movq 0x8(%rax), %rax
jmp 0x9128
movq 0x8(%r14), %rbx
testq %rbx, %rbx
jne 0x914f
leaq 0x46be(%rip), %rsi      # 0xd803
movq %r14, %rdi
xorl %eax, %eax
callq 0x52cd
movq %r14, 0x8(%rsp)
cmpl $0x1d, (%rbx)
je 0x916a
leaq 0x46a3(%rip), %rsi      # 0xd803
movq %rbx, %rdi
xorl %eax, %eax
callq 0x52cd
leaq 0x46a0(%rip), %r15      # 0xd811
xorl %eax, %eax
movq %rax, (%rsp)
movq 0x8(%rbx), %r12
testq %r12, %r12
je 0x925f
movl (%r12), %r14d
cmpl $0x1e, %r14d
je 0x927d
movq %r12, %r13
cmpl $0x1d, %r14d
je 0x91ac
cmpl $0x1e, %r14d
je 0x91ee
cmpl $0x23, %r14d
je 0x91ee
movq %r13, %rbx
jmp 0x91ce
movq 0x8(%r13), %rbx
testq %rbx, %rbx
jne 0x91c6
movq %r13, %rdi
movq %r15, %rsi
xorl %eax, %eax
callq 0x52cd
movq 0x8(%r13), %rbx
cmpl $0x1e, (%rbx)
movq %rbx, %r13
jne 0x91ac
movq 0x8(%rbx), %r13
testq %r13, %r13
jne 0x91e8
movq %rbx, %rdi
movq %r15, %rsi
xorl %eax, %eax
callq 0x52cd
movq 0x8(%rbx), %r13
movl (%r13), %r14d
jmp 0x9195
movq $0x0, 0x8(%rbx)
movl $0x1, %edi
movl $0x20, %esi
callq 0x10d0
movq %r12, 0x10(%rax)
movq 0x10(%rsp), %rdx
movq (%rdx), %rcx
movq %rax, (%rcx)
movq %rax, (%rdx)
movq (%rsp), %rax
leal 0x1(%rax), %r8d
cmpl %ebp, %eax
jb 0x924d
movq 0x8(%rsp), %rax
movl 0x28(%rax), %edx
movq 0x20(%rax), %rcx
movq %r12, %rdi
leaq 0x4600(%rip), %rsi      # 0xd839
movl %r8d, %ebx
movl %ebp, %r9d
xorl %eax, %eax
callq 0x52cd
movl %ebx, %r8d
movl (%r13), %r14d
movl %r8d, %eax
movq %r13, %rbx
cmpl $0x1e, %r14d
jne 0x9173
jmp 0x9287
leaq 0x461b(%rip), %rsi      # 0xd881
movq %rbx, %rdi
xorl %eax, %eax
callq 0x52cd
movq 0x8(%rbx), %r13
movq (%rsp), %rax
movl %eax, %r8d
jmp 0x9287
movq (%rsp), %rax
movl %eax, %r8d
movq %r12, %r13
movq 0x8(%r13), %rax
movq 0x8(%rsp), %rdi
movq %rax, 0x8(%rdi)
cmpl %ebp, %r8d
jge 0x92b5
leaq 0x45ef(%rip), %rsi      # 0xd88f
xorl %eax, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x52cd
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq