// File: /workspace/github_cpp_makefile_star2_O2/luooofan[P]hblzbd_compiler/src/generate_ir.cpp
// Function: ast::FunctionCall::GenerateIR(ir::ContextInfo&, std::vector<ir::IR*, std::allocator<ir::IR*>>&)
void FunctionCall::GenerateIR(ir::ContextInfo &ctx, std::vector<ir::IR *> &gIRList) {
  ir::Opn opn1, opn2;
  auto func_item_iter = ir::gFuncTable.find(name_.name_);
  if (func_item_iter == ir::gFuncTable.end()) {
    ir::SemanticError(line_no_, "调用的函数不存在");
    return;
  }
  auto &func_item = (*func_item_iter).second;
  if (func_item.shape_list_.size() != args_.arg_list_.size()) {
    ir::SemanticError(line_no_, "参数个数不匹配");
    return;
  }

  std::stack<ir::IR *> param_list;
  // NOTE: 正着走
  // for (int i = args_.arg_list_.size() - 1; i >= 0; --i) {
  for (int i = 0; i < args_.arg_list_.size(); ++i) {
    args_.arg_list_[i]->GenerateIR(ctx, gIRList);
    if (name_.name_ != "putarray" && name_.name_ != "memset") {  // NOTE: putarray和memset不检查参数的合法性
      if (ctx.shape_.size() != func_item.shape_list_[i].size()) {
        // std::cerr << ctx.shape_.size() << ' ' << func_item.shape_list_[i].size() << std::endl;
        ir::SemanticError(line_no_, "函数调用参数类型不匹配");
        return;
      } else {
        for (int j = 1; j < ctx.shape_.size(); j++) {
          if (ctx.shape_[j] != func_item.shape_list_[i][j]) {
            ir::SemanticError(line_no_, "函数调用参数类型不匹配");
            return;
          }
        }
      }
    }

    ir::SymbolTableItem *s = nullptr;
    int scope_id = ir::FindSymbol(ctx.scope_id_, ctx.opn_.name_, s);
    if (name_.name_ != "putarray" && name_.name_ != "memset" && s && s->is_array_ && s->is_const_) {
      ir::SemanticError(line_no_, "实参不能是const数组");
      return;
    }

    opn1 = ctx.opn_;
    if (s && s->is_array_ && nullptr == opn1.offset_) {
      ir::Opn *offset = new ir::Opn(OpnType::Imm, 0);
      opn1 = ir::Opn(OpnType::Array, opn1.name_, opn1.scope_id_, offset);
    }

    // NOTE: 全局变量需要先赋值到一个temp中
    // FIX: 全局数组不用
    if (0 == scope_id && opn1.type_ == OpnType::Var) {
      std::string temp = ir::NewTemp();
      ir::gScopes[ctx.scope_id_].symbol_table_.insert({temp, {false, false}});
      auto temp_opn = ir::Opn(OpnType::Var, temp, ctx.scope_id_);
      gIRList.push_back(new ir::IR(IROpKind::ASSIGN, opn1, temp_opn));
      opn1 = temp_opn;
    }

    param_list.push(new ir::IR(IROpKind::PARAM, opn1));
  }

  // NOTE: 倒着写Param语句
  while (!param_list.empty()) {
    gIRList.push_back(param_list.top());
    param_list.pop();
  }

  opn1 = ir::Opn(OpnType::Func, name_.name_);
  opn2 = ir::Opn(OpnType::Imm, func_item.shape_list_.size());

  ir::IR *ir;
  // 当call调用的函数返回INT时 生成(call, func, arg-num, temp) 返回VOID时 (call, func, arg-num, -/NULL)
  if (func_item.ret_type_ == INT) {
    auto scope_id = ctx.scope_id_;

    std::string ret_var = ir::NewTemp();
    // NOTE: 中间变量栈偏移-1 并且不维护scope的dynamic offset
    ir::gScopes[scope_id].symbol_table_.insert({ret_var, {false, false}});

    ir::Opn temp = ir::Opn(OpnType::Var, ret_var, scope_id);
    ctx.opn_ = temp;
    ctx.shape_.clear();
    ir = new ir::IR(IROpKind::CALL, opn1, opn2, temp);
  } else {
    ir::Opn temp = ir::Opn(OpnType::Null);
    ctx.opn_ = temp;
    ctx.shape_.clear();
    ir = new ir::IR(IROpKind::CALL, opn1, opn2, temp);
  }
  // printf("/*********************\n");
  gIRList.push_back(ir);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x4d8, %rsp            # imm = 0x4D8
movq %rdx, 0x20(%rsp)
movq %rsi, %rbx
movq %rdi, %r14
leaq 0xd8(%rsp), %rdi
callq 0x1aac2
leaq 0x218(%rsp), %rdi
callq 0x1aac2
movq 0x10(%r14), %rax
movq 0x10(%rax), %rsi
leaq 0x700b7(%rip), %rdi     # 0x81560
callq 0xe8da
movq %rax, %rsi
testq %rax, %rax
je 0x118f0
movq 0x38(%rsi), %rax
subq 0x30(%rsi), %rax
pushq $0x18
popq %rcx
cqto
idivq %rcx
movq 0x18(%r14), %rcx
movq 0x18(%rcx), %rdx
subq 0x10(%rcx), %rdx
sarq $0x3, %rdx
cmpq %rdx, %rax
jne 0x1191b
movq %r14, 0x8(%rsp)
movq %rsi, 0xa0(%rsp)
leaq 0x168(%rsp), %rdi
callq 0x1aba2
leaq 0x8(%rbx), %rax
movq %rax, 0x28(%rsp)
movq %rbx, 0x10(%rsp)
leaq 0x10(%rbx), %rax
movq %rax, 0xc8(%rsp)
xorl %ebx, %ebx
leaq 0x438(%rsp), %rbp
leaq 0x418(%rsp), %r15
movq 0x8(%rsp), %rax
movq 0x18(%rax), %rcx
movq 0x10(%rcx), %rax
movq 0x18(%rcx), %rcx
subq %rax, %rcx
sarq $0x3, %rcx
cmpq %rbx, %rcx
jbe 0x119c0
movq (%rax,%rbx,8), %rdi
movq (%rdi), %rax
movq 0x10(%rsp), %rsi
movq 0x20(%rsp), %rdx
callq *0x10(%rax)
movq 0x8(%rsp), %rax
movq 0x10(%rax), %rax
movq 0x10(%rax), %rdi
leaq 0x45369(%rip), %rsi     # 0x568db
callq 0xa45f
testb %al, %al
je 0x115ed
movq 0x8(%rsp), %rax
movq 0x10(%rax), %rax
movq 0x10(%rax), %rdi
leaq 0x45374(%rip), %rsi     # 0x56903
callq 0xa45f
testb %al, %al
je 0x115ed
movq 0x10(%rsp), %rcx
movq 0x50(%rcx), %rax
movq 0x58(%rcx), %rcx
subq %rax, %rcx
movq 0xa0(%rsp), %rdx
movq 0x30(%rdx), %rsi
imulq $0x18, %rbx, %rdi
movq (%rsi,%rdi), %rdx
movq 0x8(%rsi,%rdi), %rsi
subq %rdx, %rsi
cmpq %rsi, %rcx
jne 0x11d13
sarq $0x2, %rcx
pushq $0x1
popq %rsi
cmpq %rsi, %rcx
jbe 0x115ed
movl (%rax,%rsi,4), %edi
leaq 0x1(%rsi), %r8
cmpl (%rdx,%rsi,4), %edi
movq %r8, %rsi
je 0x115d4
jmp 0x11950
movq %rbx, 0xd0(%rsp)
andq $0x0, 0x18(%rsp)
movq %rbp, %rdi
movq 0x10(%rsp), %rax
movl (%rax), %ebx
movq 0xc8(%rsp), %rsi
callq 0x8390
movl %ebx, %edi
movq %rbp, %r12
movq %rbp, %rsi
leaq 0x18(%rsp), %rdx
callq 0x1d90c
movl %eax, %ebx
movq %r12, %rdi
callq 0x8578
movq 0x8(%rsp), %rax
movq 0x10(%rax), %rax
movq 0x10(%rax), %rdi
leaq 0x45299(%rip), %rsi     # 0x568db
callq 0xa45f
testb %al, %al
je 0x11684
movq 0x8(%rsp), %rax
movq 0x10(%rax), %rax
movq 0x10(%rax), %rdi
leaq 0x452a4(%rip), %rsi     # 0x56903
callq 0xa45f
movq 0x18(%rsp), %rcx
testq %rcx, %rcx
setne %dl
andb %dl, %al
cmpb $0x1, %al
jne 0x11684
cmpb $0x1, (%rcx)
jne 0x11684
cmpb $0x1, 0x1(%rcx)
je 0x11d45
leaq 0xd8(%rsp), %rdi
movq 0x28(%rsp), %rsi
callq 0x1daba
movq 0x18(%rsp), %rax
testq %rax, %rax
je 0x11728
cmpb $0x1, (%rax)
jne 0x11728
cmpq $0x0, 0x108(%rsp)
jne 0x11728
pushq $0x40
popq %rdi
callq 0x8250
movq %rax, %r12
movq %rax, %rdi
pushq $0x1
popq %rsi
xorl %edx, %edx
callq 0xf6ba
leaq 0x1f8(%rsp), %r13
movq %r13, %rdi
leaq 0xe0(%rsp), %rsi
callq 0x8390
movl 0x100(%rsp), %ecx
leaq 0x30(%rsp), %r14
movq %r14, %rdi
pushq $0x5
popq %rsi
movq %r13, %rdx
movq %r12, %r8
callq 0x1a934
leaq 0xd8(%rsp), %rdi
movq %r14, %rsi
callq 0x1db84
leaq 0x38(%rsp), %rdi
callq 0x8578
leaq 0x1f8(%rsp), %rdi
callq 0x8578
orl 0xd8(%rsp), %ebx
jne 0x11883
leaq 0xa8(%rsp), %rdi
callq 0x1d95d
movq 0x10(%rsp), %rax
movslq (%rax), %rbx
shlq $0x6, %rbx
leaq 0x6fdf3(%rip), %rax     # 0x81548
addq (%rax), %rbx
leaq 0x118(%rsp), %r12
movq %r12, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x1a960
leaq 0x30(%rsp), %r14
movq %r14, %rdi
leaq 0xa8(%rsp), %rsi
movq %r12, %rdx
callq 0x1a9e2
movq %rbx, %rdi
movq %r14, %rsi
callq 0x1c154
movq %r14, %rdi
callq 0x1aa18
leaq 0x118(%rsp), %rdi
callq 0x1aa2e
movq %r15, %rbx
movq %r15, %rdi
leaq 0xa8(%rsp), %rsi
callq 0x8390
movq 0x10(%rsp), %rax
movl (%rax), %ecx
movq %r14, %rdi
xorl %esi, %esi
movq %rbx, %rdx
callq 0x1a8d4
movq %rbx, %rdi
callq 0x8578
movl $0xe0, %edi
callq 0x8250
movq %rax, %rbx
leaq 0x3d8(%rsp), %rdi
leaq 0xd8(%rsp), %rsi
callq 0x1da22
leaq 0x498(%rsp), %r14
movq %r14, %rdi
leaq 0x30(%rsp), %rsi
callq 0x1da22
movb $0x1, %r12b
movq %rbx, %rdi
pushq $0xc
popq %rsi
leaq 0x3d8(%rsp), %rdx
movq %r14, %rcx
callq 0x1aa52
movq %rbx, 0x118(%rsp)
xorl %r12d, %r12d
movq 0x20(%rsp), %rdi
leaq 0x118(%rsp), %rsi
callq 0xe358
leaq 0x4a0(%rsp), %rdi
callq 0x8578
leaq 0x3e0(%rsp), %rdi
callq 0x8578
leaq 0xd8(%rsp), %rdi
leaq 0x30(%rsp), %rsi
callq 0x1daba
leaq 0x38(%rsp), %rdi
callq 0x8578
leaq 0xa8(%rsp), %rdi
callq 0x8578
movl $0xe0, %edi
callq 0x8250
movq %rax, %rbx
leaq 0x458(%rsp), %r14
movq %r14, %rdi
leaq 0xd8(%rsp), %rsi
callq 0x1da22
movb $0x1, %r12b
movq %rbx, %rdi
pushq $0x8
popq %rsi
movq %r14, %rdx
callq 0x1abbe
movq %rbx, 0x30(%rsp)
xorl %r12d, %r12d
leaq 0x168(%rsp), %rdi
leaq 0x30(%rsp), %rsi
callq 0x1c650
leaq 0x460(%rsp), %rdi
callq 0x8578
movq 0xd0(%rsp), %rbx
incq %rbx
jmp 0x11529
movl 0x8(%r14), %ebx
leaq 0x4520c(%rip), %rsi     # 0x56b07
leaq 0x30(%rsp), %rdi
leaq 0x168(%rsp), %rdx
callq 0xa292
leaq 0x30(%rsp), %rsi
movl %ebx, %edi
callq 0x1f699
jmp 0x11944
movl 0x8(%r14), %ebx
leaq 0x451fa(%rip), %rsi     # 0x56b20
leaq 0x30(%rsp), %rdi
leaq 0x168(%rsp), %rdx
callq 0xa292
leaq 0x30(%rsp), %rsi
movl %ebx, %edi
callq 0x1f699
leaq 0x30(%rsp), %rdi
callq 0x8578
jmp 0x11994
movq 0x8(%rsp), %rax
movl 0x8(%rax), %ebx
leaq 0x451d7(%rip), %rsi     # 0x56b36
leaq 0x30(%rsp), %rdi
leaq 0x118(%rsp), %rdx
callq 0xa292
leaq 0x30(%rsp), %rsi
movl %ebx, %edi
callq 0x1f699
leaq 0x30(%rsp), %rdi
callq 0x8578
leaq 0x168(%rsp), %rdi
callq 0x1b556
leaq 0x220(%rsp), %rdi
callq 0x8578
leaq 0xe0(%rsp), %rdi
callq 0x8578
addq $0x4d8, %rsp            # imm = 0x4D8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x200, %r15d           # imm = 0x200
leaq 0x168(%rsp), %rbx
movq 0x10(%rsp), %r14
movq 0x198(%rsp), %rsi
cmpq 0x178(%rsp), %rsi
je 0x11a16
cmpq 0x1a0(%rsp), %rsi
jne 0x119fe
movq 0x1b0(%rsp), %rax
movq -0x8(%rax), %rsi
addq %r15, %rsi
addq $-0x8, %rsi
movq 0x20(%rsp), %rdi
callq 0x1ac28
movq %rbx, %rdi
callq 0x1c974
jmp 0x119d3
movq 0x8(%rsp), %rax
movq 0x10(%rax), %rax
movq 0x10(%rax), %rsi
leaq 0x1d8(%rsp), %rdi
callq 0x8390
leaq 0x30(%rsp), %rdi
pushq $0x3
popq %rsi
leaq 0x1d8(%rsp), %rdx
callq 0x1ac46
movq 0xa0(%rsp), %rbx
pushq $0x18
popq %r15
leaq 0xd8(%rsp), %rdi
leaq 0x30(%rsp), %rsi
callq 0x1db84
leaq 0x38(%rsp), %rdi
callq 0x8578
leaq 0x1d8(%rsp), %rdi
callq 0x8578
movq 0x38(%rbx), %rax
subq 0x30(%rbx), %rax
cqto
idivq %r15
leaq 0x30(%rsp), %rdi
pushq $0x1
popq %rsi
movl %eax, %edx
callq 0xf6ba
leaq 0x218(%rsp), %rdi
leaq 0x30(%rsp), %rsi
callq 0x1db84
leaq 0x38(%rsp), %rdi
callq 0x8578
cmpl $0x121, 0x28(%rbx)      # imm = 0x121
jne 0x11c33
movslq (%r14), %rbx
leaq 0xa8(%rsp), %rdi
callq 0x1d95d
movq %rbx, %r15
shlq $0x6, %r15
leaq 0x6fa6b(%rip), %rax     # 0x81548
addq (%rax), %r15
leaq 0x118(%rsp), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x1a960
leaq 0x30(%rsp), %rdi
leaq 0xa8(%rsp), %rsi
leaq 0x118(%rsp), %rdx
callq 0x1a9e2
leaq 0x30(%rsp), %rsi
movq %r15, %rdi
callq 0x1c154
leaq 0x30(%rsp), %rdi
callq 0x1aa18
leaq 0x118(%rsp), %rdi
callq 0x1aa2e
leaq 0x1b8(%rsp), %rdi
leaq 0xa8(%rsp), %rsi
callq 0x8390
leaq 0x30(%rsp), %rdi
leaq 0x1b8(%rsp), %rdx
xorl %esi, %esi
movl %ebx, %ecx
callq 0x1a8d4
leaq 0x1b8(%rsp), %rdi
callq 0x8578
leaq 0x30(%rsp), %rsi
movq 0x28(%rsp), %rdi
callq 0x1daba
movq 0x50(%r14), %rax
cmpq %rax, 0x58(%r14)
je 0x11b84
movq %rax, 0x58(%r14)
movl $0xe0, %edi
callq 0x8250
movq %rax, %r14
leaq 0x398(%rsp), %rdi
leaq 0xd8(%rsp), %rsi
callq 0x1da22
leaq 0x358(%rsp), %rdi
leaq 0x218(%rsp), %rsi
callq 0x1da22
leaq 0x318(%rsp), %rdi
leaq 0x30(%rsp), %rsi
callq 0x1da22
pushq $0x9
popq %rsi
leaq 0x398(%rsp), %rdx
leaq 0x358(%rsp), %rcx
leaq 0x318(%rsp), %r8
movq %r14, %rdi
callq 0x1ab2e
movq %r14, 0x18(%rsp)
leaq 0x320(%rsp), %rdi
callq 0x8578
leaq 0x360(%rsp), %rdi
callq 0x8578
leaq 0x3a0(%rsp), %rdi
callq 0x8578
leaq 0x38(%rsp), %rdi
callq 0x8578
leaq 0xa8(%rsp), %rdi
jmp 0x11cfa
leaq 0x30(%rsp), %rdi
pushq $0x4
popq %rsi
callq 0x1ac68
leaq 0x30(%rsp), %rsi
movq 0x28(%rsp), %rdi
callq 0x1daba
movq 0x50(%r14), %rax
cmpq %rax, 0x58(%r14)
je 0x11c5d
movq %rax, 0x58(%r14)
movl $0xe0, %edi
callq 0x8250
movq %rax, %r14
leaq 0x2d8(%rsp), %rdi
leaq 0xd8(%rsp), %rsi
callq 0x1da22
leaq 0x298(%rsp), %rdi
leaq 0x218(%rsp), %rsi
callq 0x1da22
leaq 0x258(%rsp), %rdi
leaq 0x30(%rsp), %rsi
callq 0x1da22
pushq $0x9
popq %rsi
leaq 0x2d8(%rsp), %rdx
leaq 0x298(%rsp), %rcx
leaq 0x258(%rsp), %r8
movq %r14, %rdi
callq 0x1ab2e
movq %r14, 0x18(%rsp)
leaq 0x260(%rsp), %rdi
callq 0x8578
leaq 0x2a0(%rsp), %rdi
callq 0x8578
leaq 0x2e0(%rsp), %rdi
callq 0x8578
leaq 0x38(%rsp), %rdi
callq 0x8578
leaq 0x18(%rsp), %rsi
movq 0x20(%rsp), %rdi
callq 0x1ac28
jmp 0x11987
movq 0x8(%rsp), %rax
movl 0x8(%rax), %ebx
leaq 0x44e14(%rip), %rsi     # 0x56b36
leaq 0x30(%rsp), %rdi
leaq 0x118(%rsp), %rdx
callq 0xa292
leaq 0x30(%rsp), %rsi
movl %ebx, %edi
callq 0x1f699
jmp 0x1197d
movq 0x8(%rsp), %rax
movl 0x8(%rax), %ebx
leaq 0x44e04(%rip), %rsi     # 0x56b58
leaq 0x30(%rsp), %rdi
leaq 0x118(%rsp), %rdx
callq 0xa292
leaq 0x30(%rsp), %rsi
movl %ebx, %edi
callq 0x1f699
jmp 0x1197d
jmp 0x11e89
jmp 0x11fda
jmp 0x11e89
jmp 0x11fda
movq %rax, %r15
leaq 0x320(%rsp), %rdi
callq 0x8578
jmp 0x11da0
movq %rax, %r15
leaq 0x360(%rsp), %rdi
callq 0x8578
jmp 0x11db2
movq %rax, %r15
leaq 0x3a0(%rsp), %rdi
callq 0x8578
jmp 0x11dc4
movq %rax, %r15
movl $0xe0, %esi
movq %r14, %rdi
jmp 0x11f87
movq %rax, %r15
leaq 0x260(%rsp), %rdi
callq 0x8578
jmp 0x11de6
movq %rax, %r15
leaq 0x2a0(%rsp), %rdi
callq 0x8578
jmp 0x11e08
movq %rax, %r15
leaq 0x1b8(%rsp), %rdi
jmp 0x11f91
movq %rax, %r15
leaq 0x2e0(%rsp), %rdi
callq 0x8578
jmp 0x11e1f
jmp 0x11f3c
movq %rax, %r15
movl $0xe0, %esi
movq %r14, %rdi
callq 0x8260
jmp 0x11e56
jmp 0x11f1b
jmp 0x11f2a
jmp 0x11f3c
jmp 0x11fda
jmp 0x11fda
jmp 0x11fda
jmp 0x11f04
jmp 0x11e53
movq %rax, %r15
leaq 0x38(%rsp), %rdi
jmp 0x11f9e
jmp 0x11fda
movq %rax, %r15
leaq 0x38(%rsp), %rdi
callq 0x8578
jmp 0x11e77
movq %rax, %r15
leaq 0x1d8(%rsp), %rdi
jmp 0x11f9e
jmp 0x11fda
movq %rax, %r15
leaq 0x30(%rsp), %rdi
jmp 0x11f9e
jmp 0x11fda
jmp 0x11ea7
jmp 0x11f46
jmp 0x11f46
movq %rax, %r15
leaq 0x30(%rsp), %rdi
callq 0x8578
jmp 0x11fea
jmp 0x11f46
movq %rax, %r15
leaq 0x38(%rsp), %rdi
callq 0x8578
jmp 0x11ed0
movq %rax, %r15
leaq 0x1f8(%rsp), %rdi
jmp 0x11f9e
jmp 0x11fda
movq %rax, %r15
pushq $0x40
popq %rsi
movq %r12, %rdi
jmp 0x11fd1
jmp 0x11fda
jmp 0x11f04
movq %rax, %r15
movb $0x1, %r12b
jmp 0x11f6d
movq %rax, %r15
jmp 0x11f7f
movq %rax, %r15
jmp 0x11f8c
movq %rax, %r15
leaq 0x418(%rsp), %rdi
jmp 0x11f91
jmp 0x11f3c
movq %rax, %r15
leaq 0x30(%rsp), %rdi
callq 0x1aa18
jmp 0x11f2d
movq %rax, %r15
leaq 0x118(%rsp), %rdi
callq 0x1aa2e
jmp 0x11f96
movq %rax, %r15
jmp 0x11f96
jmp 0x11fda
movq %rax, %r15
jmp 0x11fea
movq %rax, %r15
jmp 0x11ff7
movq %rax, %r15
jmp 0x11fc9
jmp 0x11fda
movq %rax, %r15
leaq 0x4a0(%rsp), %rdi
callq 0x8578
leaq 0x3e0(%rsp), %rdi
callq 0x8578
testb %r12b, %r12b
je 0x11f8c
movl $0xe0, %esi
movq %rbx, %rdi
callq 0x8260
leaq 0x38(%rsp), %rdi
callq 0x8578
leaq 0xa8(%rsp), %rdi
callq 0x8578
jmp 0x11fdd
movq %rax, %r15
leaq 0x438(%rsp), %rdi
jmp 0x11f9e
jmp 0x11fda
movq %rax, %r15
leaq 0x460(%rsp), %rdi
callq 0x8578
testb %r12b, %r12b
je 0x11fdd
movl $0xe0, %esi
movq %rbx, %rdi
callq 0x8260
jmp 0x11fdd
jmp 0x11fda
movq %rax, %r15
leaq 0x168(%rsp), %rdi
callq 0x1b556
leaq 0x220(%rsp), %rdi
callq 0x8578
leaq 0xe0(%rsp), %rdi
callq 0x8578
movq %r15, %rdi
callq 0x84d0