// File: /workspace/github_c_makefile_star2_O1/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 $0x48, %rsp
movq %rdx, 0x10(%rsp)
movq %rsi, %rbx
movq 0x10(%rdi), %rax
xorl %ebp, %ebp
testq %rax, %rax
je 0x91f9
incl %ebp
movq 0x8(%rax), %rax
jmp 0x91ec
movq 0x8(%rbx), %r15
testq %r15, %r15
jne 0x9213
leaq 0x45be(%rip), %rsi      # 0xd7c7
movq %rbx, %rdi
xorl %eax, %eax
callq 0x52f9
movq %rbx, 0x8(%rsp)
cmpl $0x1d, (%r15)
je 0x922f
leaq 0x45a2(%rip), %rsi      # 0xd7c7
movq %r15, %rdi
xorl %eax, %eax
callq 0x52f9
leaq 0x459f(%rip), %r12      # 0xd7d5
xorl %r14d, %r14d
movq 0x8(%r15), %r13
testq %r13, %r13
je 0x9319
cmpl $0x1e, (%r13)
je 0x9319
movq 0x8(%r15), %rax
movl (%rax), %ecx
cmpl $0x1d, %ecx
je 0x927f
cmpl $0x1e, %ecx
je 0x92a2
cmpl $0x23, %ecx
jne 0x9288
jmp 0x92a2
movq %rax, %r15
cmpq $0x0, 0x8(%rax)
jne 0x927f
movq %r15, %rdi
movq %r12, %rsi
xorl %eax, %eax
callq 0x52f9
movq 0x8(%r15), %rax
cmpl $0x1e, (%rax)
jne 0x9268
movq 0x8(%r15), %r15
cmpq $0x0, 0x8(%r15)
jne 0x9251
movq %r15, %rdi
movq %r12, %rsi
xorl %eax, %eax
callq 0x52f9
jmp 0x9251
movq $0x0, 0x8(%r15)
movq %rax, 0x20(%rsp)
movl $0x1, %edi
movl $0x20, %esi
callq 0x10d0
movq %r13, 0x10(%rax)
movq 0x10(%rsp), %rdx
movq (%rdx), %rcx
movq %rax, (%rcx)
movq %rax, (%rdx)
leal 0x1(%r14), %ebx
cmpl %ebp, %r14d
jb 0x92fc
movq 0x8(%rsp), %rax
movl 0x28(%rax), %edx
movq 0x20(%rax), %rcx
movq %r13, %rdi
leaq 0x450e(%rip), %rsi      # 0xd7fd
movl %ebx, %r8d
movl %ebp, %r9d
xorl %eax, %eax
callq 0x52f9
movq 0x20(%rsp), %r15
cmpl $0x1e, (%r15)
leaq 0x18(%rsp), %rax
cmoveq %rax, %r15
movl %ebx, %r14d
jne 0x9239
jmp 0x931c
movl %r14d, %ebx
cmpq $0x0, 0x8(%r15)
jne 0x9334
leaq 0x451b(%rip), %rsi      # 0xd845
movq %r15, %rdi
xorl %eax, %eax
callq 0x52f9
movq 0x8(%r15), %rax
movq 0x8(%rax), %rax
movq 0x8(%rsp), %rdi
movq %rax, 0x8(%rdi)
cmpl %ebp, %ebx
jge 0x9357
leaq 0x4503(%rip), %rsi      # 0xd853
xorl %eax, %eax
callq 0x52f9
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq