// File: /workspace/github_c_makefile_star2_O2/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 $0x438, %rsp            # imm = 0x438
movq %rdx, %rbp
movq %rdi, %r15
movq %rsi, 0x8(%rsp)
movl $0x1, 0x420(%rsi)
xorl %eax, %eax
testl %ecx, %ecx
cmovlel %eax, %ecx
movl $0x7e0, %ebx            # imm = 0x7E0
addq 0x65fc(%rip), %rbx      # 0x9fe0
xorl %r12d, %r12d
movq %rcx, 0x10(%rsp)
cmpq %r12, %rcx
je 0x3a4c
movq (%rbp,%r12,8), %rsi
cmpq $0x0, (%rsi)
je 0x3a09
movq %r15, %rdi
callq 0x368f
movq %rax, %r14
jmp 0x3a3d
xorl %eax, %eax
callq 0x1d2f
movq %rax, %r13
movl $0x3f, (%rax)
movq %rbx, 0x8(%rax)
callq 0x325c
movq %rax, %r14
movq %rax, %rdi
movq %r13, %rsi
callq 0x32b4
movq (%rbp,%r12,8), %rdi
movq %r13, %rsi
callq 0x32b4
movq %r14, 0x40(%rsp,%r12,8)
incq %r12
movq 0x10(%rsp), %rcx
jmp 0x39ec
movq %rbp, 0x20(%rsp)
movq %r15, 0x30(%rsp)
callq 0x325c
movq %rax, %r14
movq 0x8(%rsp), %rax
movq 0x418(%rax), %r13
leaq 0x2a4f(%rip), %rax      # 0x64c0
subq $-0x80, %rax
movq %rax, 0x38(%rsp)
pushq $0x5c
popq %rbp
movq %r14, (%rsp)
movq (%r13), %r15
testq %r15, %r15
je 0x3d63
movq (%r15), %r14
movq 0x8(%r15), %r13
movl (%r14), %ebx
cmpl $0x39, %ebx
jne 0x3aea
movq (%r13), %rax
cmpl $0x3c, (%rax)
movq 0x10(%rsp), %r14
jne 0x3aaf
movq 0x8(%r13), %r13
xorl %ebx, %ebx
cmpq %rbx, %r14
je 0x3b12
movq (%r13), %rax
cmpl $0x1, (%rax)
jne 0x3ae5
movq 0x10(%rax), %rax
movq (%rax), %rdi
movq 0x8(%rsp), %rax
movq 0x18(%rax,%rbx,8), %rax
movq (%rax), %rsi
callq 0x1080
testl %eax, %eax
movl 0x1c(%rsp), %eax
cmovel %ebx, %eax
movl %eax, 0x1c(%rsp)
incq %rbx
jmp 0x3ab1
testq %r13, %r13
je 0x3be0
movq (%r13), %rax
cmpl $0x3c, (%rax)
jne 0x3bb4
movq 0x8(%r13), %rcx
testq %rcx, %rcx
jne 0x3bb7
jmp 0x3be0
xorl %eax, %eax
callq 0x1d2f
movq %rax, %r14
movl $0x4, (%rax)
movq 0x38(%rsp), %rax
movq %rax, 0x8(%r14)
movq 0x10(%r14), %rdi
pushq $0x22
popq %rsi
callq 0x1931
movslq 0x1c(%rsp), %rax
movq 0x20(%rsp), %rcx
movq (%rcx,%rax,8), %rbx
movq (%rbx), %rbx
testq %rbx, %rbx
je 0x3b97
xorl %r12d, %r12d
movq (%rbx), %rax
movq 0x10(%rax), %rax
movslq 0x8(%rax), %rcx
cmpq %rcx, %r12
jge 0x3b91
movq (%rax), %rax
movzbl (%rax,%r12), %r15d
cmpl $0x5c, %r15d
je 0x3b74
cmpl $0x22, %r15d
jne 0x3b7f
movq 0x10(%r14), %rdi
movl %ebp, %esi
callq 0x1931
movq 0x10(%r14), %rdi
movzbl %r15b, %esi
callq 0x1931
incq %r12
jmp 0x3b50
addq $0x8, %rbx
jmp 0x3b45
movq 0x10(%r14), %rdi
pushq $0x22
popq %rsi
callq 0x1931
movq (%rsp), %rdi
movq %r14, %rsi
movq %rdi, %r14
callq 0x32b4
jmp 0x3c23
movq %r13, %rcx
movq 0x8(%rcx), %r13
testq %r13, %r13
je 0x3bcf
movq (%r13), %rax
cmpl $0x3c, (%rax)
jne 0x3bd2
movq 0x8(%r13), %r13
jmp 0x3bd2
xorl %r13d, %r13d
movq (%rcx), %rax
cmpl $0x3a, (%rax)
jne 0x3be0
cmpl $0x0, 0x18(%rax)
je 0x3c4e
xorl %r12d, %r12d
cmpq %r12, 0x10(%rsp)
je 0x3c11
cmpl $0x1, %ebx
jne 0x3c0c
movq 0x10(%r14), %rax
movq (%rax), %rdi
movq 0x8(%rsp), %rax
movq 0x18(%rax,%r12,8), %rax
movq (%rax), %rsi
callq 0x1080
testl %eax, %eax
je 0x3c2c
incq %r12
jmp 0x3be3
movq (%rsp), %rdi
movq %r14, %rsi
movq %rdi, %r14
callq 0x32b4
movq %r15, %r13
addq $0x8, %r13
jmp 0x3a81
movq 0x40(%rsp,%r12,8), %rbx
movq (%rsp), %r14
movq (%rbx), %rbx
testq %rbx, %rbx
je 0x3c20
movq (%rbx), %rsi
movq %r14, %rdi
callq 0x32b4
addq $0x8, %rbx
jmp 0x3c35
movq %rcx, 0x28(%rsp)
xorl %r12d, %r12d
cmpq %r12, 0x10(%rsp)
je 0x3c88
cmpl $0x1, %ebx
jne 0x3c83
movq 0x10(%r14), %rax
movq (%rax), %rdi
movq 0x8(%rsp), %rax
movq 0x18(%rax,%r12,8), %rax
movq (%rax), %rsi
callq 0x1080
testl %eax, %eax
je 0x3d0a
incq %r12
jmp 0x3c56
movq (%rsp), %rdi
movq %r14, %rsi
movq %rdi, %r14
callq 0x32b4
movq 0x28(%rsp), %r12
movq 0x8(%r15), %rax
cmpq %r12, %rax
je 0x3cb0
movq (%rax), %rsi
movq %r14, %rdi
callq 0x32b4
movq (%r12), %rsi
movq %r14, %rdi
callq 0x32b4
movq 0x8(%r12), %rax
cmpq %r13, %rax
je 0x3cd1
movq (%rax), %rsi
movq %r14, %rdi
callq 0x32b4
movq (%r13), %r14
xorl %ebx, %ebx
cmpq %rbx, 0x10(%rsp)
je 0x3ba3
cmpl $0x1, (%r14)
jne 0x3d05
movq 0x10(%r14), %rax
movq (%rax), %rdi
movq 0x8(%rsp), %rax
movq 0x18(%rax,%rbx,8), %rax
movq (%rax), %rsi
callq 0x1080
testl %eax, %eax
je 0x3d39
incq %rbx
jmp 0x3cd7
movq 0x20(%rsp), %rax
movq (%rax,%r12,8), %rbx
movq (%rsp), %r14
movq 0x28(%rsp), %r12
movq (%rbx), %rbx
testq %rbx, %rbx
je 0x3c9c
movq (%rbx), %rsi
movq %r14, %rdi
callq 0x32b4
addq $0x8, %rbx
jmp 0x3d1c
movq 0x20(%rsp), %rax
movq (%rax,%rbx,8), %rbx
movq (%rsp), %r14
movq (%rbx), %rbx
testq %rbx, %rbx
je 0x3c23
movq (%rbx), %rsi
movq %r14, %rdi
callq 0x32b4
addq $0x8, %rbx
jmp 0x3d46
movq %r14, %rdi
callq 0x3d96
movq 0x30(%rsp), %rdi
movq %rax, %rsi
callq 0x368f
movq 0x8(%rsp), %rcx
andl $0x0, 0x420(%rcx)
addq $0x438, %rsp            # imm = 0x438
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq