// File: /workspace/github_c_makefile_star2_O1/ShinyaKato[P]skcc/preprocess.c
// Function: function_macro_invocation
struct pp_list *function_macro_invocation(struct preprocessor *pp, struct macro_entry *macro, struct pp_list **args, int args_size) {
  macro->expanded = 1;

  struct pp_list *replaced_args[MACRO_PARAMS_LIMIT];
  for(int i = 0; i < args_size; i++) {
    if(args[i]->head != NULL) {
      replaced_args[i] = scan_macro(pp, args[i]);
    } else {
      struct pp_token *place_marker = allocate_pp_token();
      place_marker->type = PP_PLACE_MARKER;
      place_marker->name = pp_token_name[PP_PLACE_MARKER];

      replaced_args[i] = allocate_pp_list();
      append_pp_list(replaced_args[i], place_marker);

      append_pp_list(args[i], place_marker);
    }
  }

  struct pp_list *list = allocate_pp_list();
  for(struct pp_node *node = macro->replacement_list->head; node != NULL; node = node->next) {
    if(node->token->type == PP_SHARP) {
      node = node->next;
      if(node->token->type == PP_SPACE) {
        node = node->next;
      }

      int matched;
      for(int i = 0; i < args_size; i++) {
        if(node->token->type == PP_IDENT && strcmp(node->token->text->head, macro->parameters[i]->head) == 0) {
          matched = i;
        }
      }

      struct pp_token *new_token = allocate_pp_token();
      new_token->type = PP_STR;
      new_token->name = pp_token_name[PP_STR];
      append_string(new_token->text, '"');
      for(struct pp_node *arg_node = args[matched]->head; arg_node != NULL; arg_node = arg_node->next) {
        for(int i = 0; i < arg_node->token->text->size; i++) {
          unsigned char c = arg_node->token->text->head[i];
          if(c == '\\' || c == '"') {
            append_string(new_token->text, '\\');
          }
          append_string(new_token->text, c);
        }
      }
      append_string(new_token->text, '"');
      append_pp_list(list, new_token);

      continue;
    }

    struct pp_node *left = node;
    struct pp_node *middle = left->next;
    if(middle != NULL && middle->token->type == PP_SPACE) {
      middle = middle->next;
    }
    struct pp_node *right = middle != NULL ? middle->next : NULL;
    if(right != NULL && right->token->type == PP_SPACE) {
      right = right->next;
    }
    if(middle != NULL && middle->token->type == PP_CONCAT && !middle->token->concat) {
      int left_replaced = 0;
      for(int i = 0; i < args_size; i++) {
        if(left->token->type == PP_IDENT && strcmp(left->token->text->head, macro->parameters[i]->head) == 0) {
          for(struct pp_node *arg_node = args[i]->head; arg_node != NULL; arg_node = arg_node->next) {
            append_pp_list(list, arg_node->token);
          }
          left_replaced = 1;
          break;
        }
      }
      if(!left_replaced) {
        append_pp_list(list, left->token);
      }
      if(left->next != middle) {
        append_pp_list(list, left->next->token);
      }
      append_pp_list(list, middle->token);
      if(middle->next != right) {
        append_pp_list(list, middle->next->token);
      }
      int right_replaced = 0;
      for(int i = 0; i < args_size; i++) {
        if(right->token->type == PP_IDENT && strcmp(right->token->text->head, macro->parameters[i]->head) == 0) {
          for(struct pp_node *arg_node = args[i]->head; arg_node != NULL; arg_node = arg_node->next) {
            append_pp_list(list, arg_node->token);
          }
          right_replaced = 1;
          break;
        }
      }
      if(!right_replaced) {
        append_pp_list(list, right->token);
      }
      node = right;
      continue;
    }

    int replaced = 0;
    for(int i = 0; i < args_size; i++) {
      if(node->token->type == PP_IDENT && strcmp(node->token->text->head, macro->parameters[i]->head) == 0) {
        for(struct pp_node *arg_node = replaced_args[i]->head; arg_node != NULL; arg_node = arg_node->next) {
          append_pp_list(list, arg_node->token);
        }
        replaced = 1;
        break;
      }
    }

    if(!replaced) {
      append_pp_list(list, node->token);
    }
  }

  struct pp_list *concat_list = concat_macro_token(list);
  struct pp_list *result = scan_macro(pp, concat_list);

  macro->expanded = 0;
  return result;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x448, %rsp            # imm = 0x448
movq %rdx, %r12
movq %rdi, 0x38(%rsp)
movq %rsi, 0x20(%rsp)
movl $0x1, 0x420(%rsi)
movl %ecx, %r14d
movl %ecx, 0x14(%rsp)
testl %ecx, %ecx
movq %rdx, 0x18(%rsp)
jle 0x4579
xorl %ebx, %ebx
movl $0x7e0, %r15d           # imm = 0x7E0
addq 0x6b05(%rip), %r15      # 0xafe0
xorl %ebp, %ebp
movq (%r12,%rbp,8), %rsi
cmpq $0x0, (%rsi)
je 0x44f8
movq 0x38(%rsp), %rdi
callq 0x402b
movq %rax, 0x50(%rsp,%rbp,8)
jmp 0x456d
xorl %eax, %eax
callq 0x1e32
movq %rax, %r12
movl $0x3f, (%rax)
movq %r15, 0x8(%rax)
callq 0x3ad0
movq %rax, %r13
movq %rax, 0x50(%rsp,%rbp,8)
movl $0x18, %edi
callq 0x10b0
movq %r12, (%rax)
leaq 0x8(%rax), %rcx
movq %rbx, 0x8(%rax)
movl %ebx, 0x10(%rax)
movq 0x8(%r13), %rdx
movq %rax, (%rdx)
movq %rcx, 0x8(%r13)
movq 0x18(%rsp), %rax
movq (%rax,%rbp,8), %r13
movl $0x18, %edi
callq 0x10b0
movq %r12, (%rax)
movq 0x18(%rsp), %r12
leaq 0x8(%rax), %rcx
movq %rbx, 0x8(%rax)
movl %ebx, 0x10(%rax)
movq 0x8(%r13), %rdx
movq %rax, (%rdx)
movq %rcx, 0x8(%r13)
incq %rbp
cmpq %rbp, %r14
jne 0x44dd
callq 0x3ad0
movq %rax, %r13
movq 0x20(%rsp), %rax
movq 0x418(%rax), %rax
movq (%rax), %rbp
testq %rbp, %rbp
je 0x4a16
movq %r13, %rax
addq $0x8, %rax
movq %rax, 0x30(%rsp)
leaq 0x2e34(%rip), %rax      # 0x73e0
subq $-0x80, %rax
movq %rax, 0x48(%rsp)
movq %r13, 0x28(%rsp)
movq (%rbp), %r13
movq 0x8(%rbp), %rcx
movl (%r13), %ebx
cmpl $0x39, %ebx
jne 0x46ad
movq (%rcx), %rax
cmpl $0x3c, (%rax)
jne 0x45db
movq 0x8(%rcx), %rcx
movq %rcx, 0x8(%rsp)
cmpl $0x0, 0x14(%rsp)
jle 0x4620
movq 0x8(%rsp), %rax
movq (%rax), %rbx
movl (%rbx), %ebp
xorl %r13d, %r13d
cmpl $0x1, %ebp
jne 0x4618
movq 0x10(%rbx), %rax
movq (%rax), %rdi
movq 0x20(%rsp), %rax
movq 0x18(%rax,%r13,8), %rax
movq (%rax), %rsi
callq 0x1080
testl %eax, %eax
cmovel %r13d, %r15d
incq %r13
cmpq %r13, %r14
jne 0x45f4
xorl %eax, %eax
callq 0x1e32
movq %rax, %r13
movl $0x4, (%rax)
movq 0x48(%rsp), %rax
movq %rax, 0x8(%r13)
movq 0x10(%r13), %rdi
movl $0x22, %esi
callq 0x1a19
movslq %r15d, %rax
movq (%r12,%rax,8), %rax
movq (%rax), %rbx
testq %rbx, %rbx
je 0x46c0
movq (%rbx), %rax
movq 0x10(%rax), %rax
cmpl $0x0, 0x8(%rax)
jle 0x46a7
xorl %ebp, %ebp
movq (%rax), %rax
movzbl (%rax,%rbp), %r12d
cmpl $0x5c, %r12d
je 0x4679
cmpl $0x22, %r12d
jne 0x4687
movq 0x10(%r13), %rdi
movl $0x5c, %esi
callq 0x1a19
movq 0x10(%r13), %rdi
movzbl %r12b, %esi
callq 0x1a19
incq %rbp
movq (%rbx), %rax
movq 0x10(%rax), %rax
movslq 0x8(%rax), %rcx
cmpq %rcx, %rbp
jl 0x4665
movq 0x8(%rbx), %rbx
jmp 0x4651
testq %rcx, %rcx
je 0x46d0
movq (%rcx), %rax
cmpl $0x3c, (%rax)
jne 0x46d4
movq 0x8(%rcx), %rdx
jmp 0x46d7
movq 0x10(%r13), %rdi
movl $0x22, %esi
callq 0x1a19
jmp 0x474d
xorl %edx, %edx
jmp 0x46d7
movq %rcx, %rdx
testq %rdx, %rdx
je 0x46e2
movq 0x8(%rdx), %rcx
jmp 0x46e4
xorl %ecx, %ecx
testq %rcx, %rcx
je 0x46f7
movq (%rcx), %rax
cmpl $0x3c, (%rax)
jne 0x46f9
movq 0x8(%rcx), %rcx
jmp 0x46f9
xorl %ecx, %ecx
testq %rdx, %rdx
je 0x4710
movq (%rdx), %rax
cmpl $0x3a, (%rax)
jne 0x4710
cmpl $0x0, 0x18(%rax)
je 0x47f5
cmpl $0x0, 0x14(%rsp)
jle 0x4744
xorl %r12d, %r12d
cmpl $0x1, %ebx
jne 0x473c
movq 0x10(%r13), %rax
movq (%rax), %rdi
movq 0x20(%rsp), %rax
movq 0x18(%rax,%r12,8), %rax
movq (%rax), %rsi
callq 0x1080
testl %eax, %eax
je 0x479c
incq %r12
cmpq %r12, %r14
jne 0x471a
movq (%rbp), %r13
movq %rbp, 0x8(%rsp)
movl $0x18, %edi
callq 0x10b0
movq %r13, (%rax)
leaq 0x8(%rax), %rcx
movq $0x0, 0x8(%rax)
movl $0x0, 0x10(%rax)
movq 0x30(%rsp), %rsi
movq (%rsi), %rdx
movq %rax, (%rdx)
movq %rcx, (%rsi)
movq 0x18(%rsp), %r12
movq 0x28(%rsp), %r13
movq 0x8(%rsp), %rax
movq 0x8(%rax), %rbp
testq %rbp, %rbp
jne 0x45ba
jmp 0x4a16
movq 0x50(%rsp,%r12,8), %rax
movq (%rax), %rbx
testq %rbx, %rbx
je 0x495c
movq 0x28(%rsp), %r13
movq (%rbx), %r12
movl $0x18, %edi
callq 0x10b0
movq %r12, (%rax)
leaq 0x8(%rax), %rcx
movq $0x0, 0x8(%rax)
movl $0x0, 0x10(%rax)
movq 0x8(%r13), %rdx
movq %rax, (%rdx)
movq %rcx, 0x8(%r13)
movq 0x8(%rbx), %rbx
testq %rbx, %rbx
jne 0x47b2
movq %rbp, 0x8(%rsp)
movq 0x18(%rsp), %r12
jmp 0x4785
movq %rdx, 0x40(%rsp)
movq %rcx, 0x8(%rsp)
cmpl $0x0, 0x14(%rsp)
jle 0x4837
xorl %r12d, %r12d
cmpl $0x1, %ebx
jne 0x482f
movq 0x10(%r13), %rax
movq (%rax), %rdi
movq 0x20(%rsp), %rax
movq 0x18(%rax,%r12,8), %rax
movq (%rax), %rsi
callq 0x1080
testl %eax, %eax
je 0x4966
incq %r12
cmpq %r12, %r14
jne 0x4809
movq (%rbp), %rbx
movl $0x18, %edi
callq 0x10b0
movq %rbx, (%rax)
leaq 0x8(%rax), %rcx
movq $0x0, 0x8(%rax)
movl $0x0, 0x10(%rax)
movq 0x28(%rsp), %r13
movq 0x8(%r13), %rdx
movq %rax, (%rdx)
movq %rcx, 0x8(%r13)
movq 0x8(%rbp), %rax
movq 0x40(%rsp), %r12
cmpq %r12, %rax
je 0x48a7
movq (%rax), %rbx
movl $0x18, %edi
callq 0x10b0
movq %rbx, (%rax)
leaq 0x8(%rax), %rcx
movq $0x0, 0x8(%rax)
movl $0x0, 0x10(%rax)
movq 0x8(%r13), %rdx
movq %rax, (%rdx)
movq %rcx, 0x8(%r13)
movq (%r12), %rbx
movl $0x18, %edi
callq 0x10b0
movq %rbx, (%rax)
leaq 0x8(%rax), %rbx
movq $0x0, 0x8(%rax)
movl $0x0, 0x10(%rax)
movq 0x30(%rsp), %rbp
movq (%rbp), %rcx
movq %rax, (%rcx)
movq %rbx, (%rbp)
movq 0x8(%r12), %rax
cmpq 0x8(%rsp), %rax
je 0x4911
movq (%rax), %r12
movl $0x18, %edi
callq 0x10b0
movq %r12, (%rax)
leaq 0x8(%rax), %rcx
movq $0x0, 0x8(%rax)
movl $0x0, 0x10(%rax)
movq %rax, (%rbx)
movq %rcx, (%rbp)
cmpl $0x0, 0x14(%rsp)
jle 0x494f
movq 0x8(%rsp), %rax
movq (%rax), %rbx
movl (%rbx), %ebp
xorl %r12d, %r12d
cmpl $0x1, %ebp
jne 0x4947
movq 0x10(%rbx), %rax
movq (%rax), %rdi
movq 0x20(%rsp), %rax
movq 0x18(%rax,%r12,8), %rax
movq (%rax), %rsi
callq 0x1080
testl %eax, %eax
je 0x49bc
incq %r12
cmpq %r12, %r14
jne 0x4925
movq 0x8(%rsp), %rax
movq (%rax), %r13
jmp 0x474d
movq %rbp, 0x8(%rsp)
jmp 0x477b
movq 0x18(%rsp), %rax
movq (%rax,%r12,8), %rax
movq (%rax), %rbx
testq %rbx, %rbx
movq 0x28(%rsp), %r13
je 0x486b
movq (%rbx), %r12
movl $0x18, %edi
callq 0x10b0
movq %r12, (%rax)
leaq 0x8(%rax), %rcx
movq $0x0, 0x8(%rax)
movl $0x0, 0x10(%rax)
movq 0x8(%r13), %rdx
movq %rax, (%rdx)
movq %rcx, 0x8(%r13)
movq 0x8(%rbx), %rbx
testq %rbx, %rbx
jne 0x4980
jmp 0x486b
movq 0x18(%rsp), %rcx
movq (%rcx,%r12,8), %rax
movq (%rax), %rbx
testq %rbx, %rbx
movq 0x30(%rsp), %rbp
je 0x4a0e
movq (%rbx), %r12
movl $0x18, %edi
callq 0x10b0
movq %r12, (%rax)
leaq 0x8(%rax), %rcx
movq $0x0, 0x8(%rax)
movl $0x0, 0x10(%rax)
movq (%rbp), %rdx
movq %rax, (%rdx)
movq %rcx, (%rbp)
movq 0x8(%rbx), %rbx
testq %rbx, %rbx
jne 0x49d2
jmp 0x47ee
movq %rcx, %r12
jmp 0x4785
movq %r13, %rdi
callq 0x4a4c
movq 0x38(%rsp), %rdi
movq %rax, %rsi
callq 0x402b
movq 0x20(%rsp), %rcx
movl $0x0, 0x420(%rcx)
addq $0x448, %rsp            # imm = 0x448
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq