// File: /workspace/github_c_makefile_star2_O3/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 %rdi, 0x30(%rsp)
movq %rsi, 0x18(%rsp)
movl $0x1, 0x420(%rsi)
movl %ecx, %r14d
movl %ecx, 0xc(%rsp)
testl %ecx, %ecx
movq %rdx, 0x20(%rsp)
jle 0x3e20
movq %rdx, %r13
xorl %r15d, %r15d
movl $0x7e0, %eax            # imm = 0x7E0
addq 0x7269(%rip), %rax      # 0xafe0
movq %rax, (%rsp)
xorl %ebp, %ebp
movq (%r13,%rbp,8), %rsi
cmpq $0x0, (%rsi)
je 0x3d97
movq 0x30(%rsp), %rdi
callq 0x38da
movq %rax, %r12
jmp 0x3e0f
xorl %eax, %eax
callq 0x1e6c
movq %rax, %r13
movl $0x3f, (%rax)
movq (%rsp), %rax
movq %rax, 0x8(%r13)
callq 0x33b4
movq %rax, %r12
movl $0x18, %edi
callq 0x10b0
movq %r13, (%rax)
leaq 0x8(%rax), %rcx
movq %r15, 0x8(%rax)
movl %r15d, 0x10(%rax)
movq 0x8(%r12), %rdx
movq %rax, (%rdx)
movq %rcx, 0x8(%r12)
movq 0x20(%rsp), %rax
movq (%rax,%rbp,8), %rbx
movl $0x18, %edi
callq 0x10b0
movq %r13, (%rax)
movq 0x20(%rsp), %r13
leaq 0x8(%rax), %rcx
movq %r15, 0x8(%rax)
movl %r15d, 0x10(%rax)
movq 0x8(%rbx), %rdx
movq %rax, (%rdx)
movq %rcx, 0x8(%rbx)
movq %r12, 0x40(%rsp,%rbp,8)
incq %rbp
cmpq %rbp, %r14
jne 0x3d7d
callq 0x33b4
movq %rax, %rdi
movq 0x18(%rsp), %rax
movq 0x418(%rax), %rax
movq (%rax), %r13
testq %r13, %r13
je 0x42df
leaq 0x3679(%rip), %rax      # 0x74c0
subq $-0x80, %rax
movq %rax, 0x38(%rsp)
movq %rdi, (%rsp)
movq (%r13), %r12
movq 0x8(%r13), %rbx
movl (%r12), %r15d
cmpl $0x39, %r15d
jne 0x3f4c
movq (%rbx), %rax
cmpl $0x3c, (%rax)
jne 0x3e76
movq 0x8(%rbx), %rbx
cmpl $0x0, 0xc(%rsp)
jle 0x3eb3
movq (%rbx), %r15
movl (%r15), %r12d
xorl %r13d, %r13d
cmpl $0x1, %r12d
jne 0x3eab
movq 0x10(%r15), %rax
movq (%rax), %rdi
movq 0x18(%rsp), %rax
movq 0x18(%rax,%r13,8), %rax
movq (%rax), %rsi
callq 0x1080
testl %eax, %eax
cmovel %r13d, %ebp
incq %r13
cmpq %r13, %r14
jne 0x3e86
movq %rbx, 0x10(%rsp)
xorl %eax, %eax
callq 0x1e6c
movq %rax, %r13
movl $0x4, (%rax)
movq 0x38(%rsp), %rax
movq %rax, 0x8(%r13)
movq 0x10(%r13), %rdi
movl $0x22, %esi
callq 0x1a31
movslq %ebp, %rax
movq 0x20(%rsp), %rcx
movq (%rcx,%rax,8), %rax
movq (%rax), %r15
testq %r15, %r15
je 0x3f79
movq (%r15), %rax
movq 0x10(%rax), %rax
cmpl $0x0, 0x8(%rax)
jle 0x3f46
xorl %r12d, %r12d
movq (%rax), %rax
movzbl (%rax,%r12), %ebx
cmpl $0x5c, %ebx
je 0x3f19
cmpl $0x22, %ebx
jne 0x3f27
movq 0x10(%r13), %rdi
movl $0x5c, %esi
callq 0x1a31
movq 0x10(%r13), %rdi
movzbl %bl, %esi
callq 0x1a31
incq %r12
movq (%r15), %rax
movq 0x10(%rax), %rax
movslq 0x8(%rax), %rcx
cmpq %rcx, %r12
jl 0x3f07
movq 0x8(%r15), %r15
jmp 0x3eee
testq %rbx, %rbx
je 0x3fcd
movq (%rbx), %rax
cmpl $0x3c, (%rax)
jne 0x3f62
movq 0x8(%rbx), %rbx
testq %rbx, %rbx
je 0x3fcd
movq 0x8(%rbx), %rcx
testq %rcx, %rcx
je 0x3fb9
movq (%rcx), %rax
cmpl $0x3c, (%rax)
jne 0x3fbb
movq 0x8(%rcx), %rcx
jmp 0x3fbb
movq 0x10(%r13), %rdi
movl $0x22, %esi
callq 0x1a31
movl $0x18, %edi
callq 0x10b0
movq %r13, (%rax)
leaq 0x8(%rax), %rcx
movq $0x0, 0x8(%rax)
movl $0x0, 0x10(%rax)
movq (%rsp), %rdi
movq 0x8(%rdi), %rdx
movq %rax, (%rdx)
movq 0x10(%rsp), %r13
jmp 0x402d
xorl %ecx, %ecx
movq (%rbx), %rax
cmpl $0x3a, (%rax)
jne 0x3fcd
cmpl $0x0, 0x18(%rax)
je 0x4095
cmpl $0x0, 0xc(%rsp)
jle 0x4002
xorl %ebx, %ebx
cmpl $0x1, %r15d
jne 0x3ffa
movq 0x10(%r12), %rax
movq (%rax), %rdi
movq 0x18(%rsp), %rax
movq 0x18(%rax,%rbx,8), %rax
movq (%rax), %rsi
callq 0x1080
testl %eax, %eax
je 0x4043
incq %rbx
cmpq %rbx, %r14
jne 0x3fd6
movl $0x18, %edi
callq 0x10b0
movq %r12, (%rax)
leaq 0x8(%rax), %rcx
movq $0x0, 0x8(%rax)
movl $0x0, 0x10(%rax)
movq (%rsp), %rdi
movq 0x8(%rdi), %rdx
movq %rax, (%rdx)
movq %rcx, 0x8(%rdi)
movq 0x8(%r13), %r13
testq %r13, %r13
jne 0x3e54
jmp 0x42df
movq 0x40(%rsp,%rbx,8), %rax
movq (%rax), %r15
testq %r15, %r15
je 0x421a
movq (%rsp), %rax
movq 0x8(%rax), %rbx
movq (%r15), %r12
movl $0x18, %edi
callq 0x10b0
movq %r12, (%rax)
movq (%rsp), %rdi
movq $0x0, 0x8(%rax)
movl $0x0, 0x10(%rax)
movq %rax, (%rbx)
leaq 0x8(%rax), %rbx
movq %rbx, 0x8(%rdi)
movq 0x8(%r15), %r15
testq %r15, %r15
jne 0x405c
jmp 0x4031
movq %rbx, 0x28(%rsp)
movq %rcx, 0x10(%rsp)
cmpl $0x0, 0xc(%rsp)
jle 0x40d8
xorl %ebx, %ebx
cmpl $0x1, %r15d
jne 0x40d0
movq 0x10(%r12), %rax
movq (%rax), %rdi
movq 0x18(%rsp), %rax
movq 0x18(%rax,%rbx,8), %rax
movq (%rax), %rsi
callq 0x1080
testl %eax, %eax
je 0x4223
incq %rbx
cmpq %rbx, %r14
jne 0x40a8
movl $0x18, %edi
callq 0x10b0
movq %r12, (%rax)
leaq 0x8(%rax), %r12
movq $0x0, 0x8(%rax)
movl $0x0, 0x10(%rax)
movq (%rsp), %rcx
movq 0x8(%rcx), %rcx
movq %rax, (%rcx)
movq 0x8(%r13), %rax
movq 0x28(%rsp), %rcx
cmpq %rcx, %rax
je 0x413d
movq (%rax), %rbx
movl $0x18, %edi
callq 0x10b0
movq %rbx, (%rax)
movq 0x28(%rsp), %rcx
movq $0x0, 0x8(%rax)
movl $0x0, 0x10(%rax)
movq %rax, (%r12)
leaq 0x8(%rax), %r12
movq %rcx, %r13
movq (%rcx), %rbx
movl $0x18, %edi
callq 0x10b0
movq %rbx, (%rax)
leaq 0x8(%rax), %r15
movq $0x0, 0x8(%rax)
movl $0x0, 0x10(%rax)
movq %rax, (%r12)
movq (%rsp), %r12
movq %r15, 0x8(%r12)
movq 0x8(%r13), %rax
movq 0x10(%rsp), %rcx
cmpq %rcx, %rax
je 0x41ae
movq (%rax), %rbx
movl $0x18, %edi
callq 0x10b0
movq %rbx, (%rax)
movq 0x10(%rsp), %rcx
movq %rax, (%r15)
leaq 0x8(%rax), %r15
movq $0x0, 0x8(%rax)
movl $0x0, 0x10(%rax)
movq %r15, 0x8(%r12)
movq (%rcx), %r12
cmpl $0x0, 0xc(%rsp)
jle 0x41ee
movl (%r12), %r13d
xorl %ebx, %ebx
cmpl $0x1, %r13d
jne 0x41e6
movq 0x10(%r12), %rax
movq (%rax), %rdi
movq 0x18(%rsp), %rax
movq 0x18(%rax,%rbx,8), %rax
movq (%rax), %rsi
callq 0x1080
testl %eax, %eax
je 0x427f
incq %rbx
cmpq %rbx, %r14
jne 0x41be
movl $0x18, %edi
callq 0x10b0
movq %r12, (%rax)
leaq 0x8(%rax), %rcx
movq $0x0, 0x8(%rax)
movl $0x0, 0x10(%rax)
movq %rax, (%r15)
movq (%rsp), %rdi
jmp 0x3fb2
movq (%rsp), %rdi
jmp 0x4031
movq 0x20(%rsp), %rax
movq (%rax,%rbx,8), %rax
movq (%rax), %r15
movq (%rsp), %rax
movq 0x8(%rax), %rbx
testq %r15, %r15
je 0x42c9
movq (%r15), %r12
movl $0x18, %edi
callq 0x10b0
movq %r12, (%rax)
leaq 0x8(%rax), %r12
movq $0x0, 0x8(%rax)
movl $0x0, 0x10(%rax)
movq %rax, (%rbx)
movq (%rsp), %rax
movq %r12, 0x8(%rax)
movq 0x8(%r15), %r15
movq %r12, %rbx
testq %r15, %r15
jne 0x4240
jmp 0x4103
movq 0x20(%rsp), %rax
movq (%rax,%rbx,8), %rax
movq (%rax), %rbx
testq %rbx, %rbx
je 0x42d1
movq (%rbx), %r12
movl $0x18, %edi
callq 0x10b0
movq %r12, (%rax)
movq (%rsp), %rdi
movq $0x0, 0x8(%rax)
movl $0x0, 0x10(%rax)
movq %rax, (%r15)
leaq 0x8(%rax), %r15
movq %r15, 0x8(%rdi)
movq 0x8(%rbx), %rbx
testq %rbx, %rbx
jne 0x4290
jmp 0x42d5
movq %rbx, %r12
jmp 0x4103
movq (%rsp), %rdi
movq 0x10(%rsp), %r13
jmp 0x4031
callq 0x4312
movq 0x30(%rsp), %rdi
movq %rax, %rsi
callq 0x38da
movq 0x18(%rsp), %rcx
movl $0x0, 0x420(%rcx)
addq $0x438, %rsp            # imm = 0x438
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq