// File: /workspace/github_c_makefile_star2_O2/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
addq $0x10, %rdi
xorl %eax, %eax
movq (%rdi), %rdi
testq %rdi, %rdi
je 0x7d64
incl %eax
addq $0x8, %rdi
jmp 0x7d54
movl %eax, 0x4(%rsp)
movq 0x8(%r14), %rbx
testq %rbx, %rbx
jne 0x7d82
leaq 0x3a73(%rip), %rsi      # 0xb7eb
movq %r14, %rdi
xorl %eax, %eax
callq 0x48b3
movq %r14, 0x8(%rsp)
cmpl $0x1d, (%rbx)
je 0x7d9d
leaq 0x3a58(%rip), %rsi      # 0xb7eb
movq %rbx, %rdi
xorl %eax, %eax
callq 0x48b3
leaq 0x3a55(%rip), %r15      # 0xb7f9
xorl %eax, %eax
movq %rax, %r14
movq 0x8(%rbx), %r12
testq %r12, %r12
je 0x7e85
movl (%r12), %ebp
cmpl $0x1e, %ebp
je 0x7e9c
movq %r12, %r13
cmpl $0x1d, %ebp
je 0x7df6
cmpl $0x1e, %ebp
je 0x7e1e
cmpl $0x23, %ebp
jne 0x7dfb
jmp 0x7e1e
movq 0x8(%r13), %rax
testq %rax, %rax
jne 0x7df1
movq %r13, %rdi
movq %r15, %rsi
xorl %eax, %eax
callq 0x48b3
movq 0x8(%r13), %rax
movl (%rax), %ebp
movq %rax, %r13
cmpl $0x1e, %ebp
jne 0x7dd7
movq %r13, %rbx
movq 0x8(%r13), %r13
testq %r13, %r13
jne 0x7e18
movq %rbx, %rdi
movq %r15, %rsi
xorl %eax, %eax
callq 0x48b3
movq 0x8(%rbx), %r13
movl (%r13), %ebp
jmp 0x7dc6
andq $0x0, 0x8(%rbx)
pushq $0x1
popq %rdi
pushq $0x20
popq %rsi
callq 0x10d0
movq %r12, 0x10(%rax)
movq 0x10(%rsp), %rdx
movq (%rdx), %rcx
movq %rax, (%rcx)
movq %rax, (%rdx)
movq %r14, %rax
incl %r14d
movl 0x4(%rsp), %r9d
cmpl %r9d, %eax
jb 0x7e74
movq 0x8(%rsp), %rax
movl 0x28(%rax), %edx
movq 0x20(%rax), %rcx
movq %r12, %rdi
leaq 0x39bb(%rip), %rsi      # 0xb821
movl %r14d, %r8d
xorl %eax, %eax
callq 0x48b3
movl (%r13), %ebp
movl %r14d, %eax
movq %r13, %rbx
cmpl $0x1e, %ebp
jne 0x7da6
jmp 0x7e9f
leaq 0x39dd(%rip), %rsi      # 0xb869
movq %rbx, %rdi
xorl %eax, %eax
callq 0x48b3
movq 0x8(%rbx), %r13
jmp 0x7e9f
movq %r12, %r13
movq 0x8(%r13), %rax
movq 0x8(%rsp), %rdi
movq %rax, 0x8(%rdi)
cmpl 0x4(%rsp), %r14d
jge 0x7ecf
leaq 0x39bd(%rip), %rsi      # 0xb877
xorl %eax, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x48b3
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq