// File: luooofan[P]hblzbd_compiler/src/Pass/dead_function_eliminate.cpp
// Function: DeadFunctionEliminate::Run()
void DeadFunctionEliminate::Run() {
  auto m = dynamic_cast<IRModule *>(*m_);
  IRLivenessAnalysis livenessanalysis = IRLivenessAnalysis(m_);
  livenessanalysis.Run();

  std::unordered_set<std::string> dead_ret_function;

  for (auto func_item : ir::gFuncTable) {
    if (func_item.second.ret_type_ == INT && func_item.first != "main") {
      dead_ret_function.insert(func_item.first);
    }
  }

  // #ifdef PRINT_ALL_INT_FUNCTION
  //   for(auto func_name : dead_ret_function){
  //     std::cout << func_name << std::endl;
  //   }
  // #endif

  for (auto func : m->func_list_) {
    for (auto bb : func->bb_list_) {
      for (auto ir : bb->ir_list_) {
        if (ir->op_ == ir::IR::OpKind::CALL) {
#ifdef PRINT_CALL_IR
          ir->PrintIR();
#endif
          auto func_table_item = ir::gFuncTable[ir->opn1_.name_];
          if (func_table_item.ret_type_ == INT) {
#ifdef PRINT_DEBUG
            if (!isLiveness(bb, ir, ir->res_)) {
              ir->PrintIR();
              printf("/*********   不活跃，可以删除\n");
            } else if (dead_ret_function.find(ir->opn1_.name_) != dead_ret_function.end()) {
              ir->PrintIR();
              printf("/*********   活跃，不可以删除\n");
            }
#endif
            if (!isLiveness(bb, ir, ir->res_)) {
            } else {
              dead_ret_function.erase(ir->opn1_.name_);
              // std::cout << ir->opn1_.name_ << std::endl;
            }
          }
        }
      }  // for ir
    }    // for bb
  }      // for func

#ifdef PRINT_DEAD_RET_FUNCTION
  print_set(dead_ret_function);
#endif

  for (auto func : m->func_list_) {
    if (dead_ret_function.find(func->name_) != dead_ret_function.end()) {
      ir::gFuncTable[func->name_].ret_type_ = VOID;
      for (auto bb : func->bb_list_) {
        for (int i = 0; i < bb->ir_list_.size(); ++i) {
          auto ir = bb->ir_list_[i];

          if (ir->op_ == ir::IR::OpKind::RET) {
            bb->ir_list_[i] = new ir::IR(ir::IR::OpKind::RET);
          }
        }
      }
    }
  }

#ifdef PRINT_FUNCTABLE
  ir::PrintFuncTable();
#endif
}

// Assembly:
subq $0x298, %rsp            # imm = 0x298
movq %rdi, 0x290(%rsp)
movq 0x290(%rsp), %rax
movq %rax, 0x70(%rsp)
movq 0x8(%rax), %rax
movq (%rax), %rax
movq %rax, 0x78(%rsp)
cmpq $0x0, %rax
je 0xa3641
movq 0x78(%rsp), %rdi
leaq 0xa6fae(%rip), %rsi     # 0x14a5d8
leaq 0xa7a07(%rip), %rdx     # 0x14b038
xorl %eax, %eax
movl %eax, %ecx
callq 0x8410
movq %rax, 0x68(%rsp)
jmp 0xa364a
xorl %eax, %eax
movq %rax, 0x68(%rsp)
jmp 0xa364a
movq 0x70(%rsp), %rax
movq 0x68(%rsp), %rcx
movq %rcx, 0x288(%rsp)
movq 0x8(%rax), %rsi
leaq 0x250(%rsp), %rdi
movq %rdi, 0x60(%rsp)
callq 0xa3f20
movq 0x60(%rsp), %rdi
callq 0xc9d80
jmp 0xa367e
leaq 0x208(%rsp), %rdi
callq 0x18d80
leaq 0xa8fae(%rip), %rax     # 0x14c640
movq %rax, 0x200(%rsp)
leaq 0xa8f9f(%rip), %rdi     # 0x14c640
callq 0x47ea0
movq %rax, 0x1f8(%rsp)
leaq 0xa8f8b(%rip), %rdi     # 0x14c640
callq 0x36170
movq %rax, 0x1f0(%rsp)
leaq 0x1f8(%rsp), %rdi
leaq 0x1f0(%rsp), %rsi
callq 0x47ed0
testb $0x1, %al
jne 0xa36e0
jmp 0xa37e8
leaq 0x1f8(%rsp), %rdi
callq 0x186b0
movq %rax, %rsi
leaq 0x1b0(%rsp), %rdi
callq 0xa3f60
jmp 0xa36ff
cmpl $0x121, 0x1d0(%rsp) # imm = 0x121
jne 0xa37c9
leaq 0x573c0(%rip), %rsi     # 0xfaad7
leaq 0x1b0(%rsp), %rdi
callq 0xcc30
movb %al, 0x5f(%rsp)
jmp 0xa372a
movb 0x5f(%rsp), %al
testb $0x1, %al
jne 0xa3737
jmp 0xa37c9
leaq 0x208(%rsp), %rdi
leaq 0x1b0(%rsp), %rsi
callq 0x8fa20
movb %dl, 0x4f(%rsp)
movq %rax, 0x50(%rsp)
jmp 0xa3757
movb 0x4f(%rsp), %al
movq 0x50(%rsp), %rcx
movq %rcx, 0x1a0(%rsp)
movb %al, 0x1a8(%rsp)
jmp 0xa37c9
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x248(%rsp)
movl %eax, 0x244(%rsp)
jmp 0xa3d76
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x248(%rsp)
movl %eax, 0x244(%rsp)
jmp 0xa3d69
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x248(%rsp)
movl %eax, 0x244(%rsp)
leaq 0x1b0(%rsp), %rdi
callq 0x35a80
jmp 0xa3d69
leaq 0x1b0(%rsp), %rdi
callq 0x35a80
leaq 0x1f8(%rsp), %rdi
callq 0x47ef0
jmp 0xa36c2
movq 0x288(%rsp), %rax
addq $0x28, %rax
movq %rax, 0x198(%rsp)
movq 0x198(%rsp), %rdi
callq 0x1d3c0
movq %rax, 0x190(%rsp)
movq 0x198(%rsp), %rdi
callq 0x1d240
movq %rax, 0x188(%rsp)
leaq 0x190(%rsp), %rdi
leaq 0x188(%rsp), %rsi
callq 0x49210
testb $0x1, %al
jne 0xa3844
jmp 0xa3abe
leaq 0x190(%rsp), %rdi
callq 0x49250
movq (%rax), %rax
movq %rax, 0x180(%rsp)
movq 0x180(%rsp), %rax
addq $0x30, %rax
movq %rax, 0x178(%rsp)
movq 0x178(%rsp), %rdi
callq 0x1cc40
movq %rax, 0x170(%rsp)
movq 0x178(%rsp), %rdi
callq 0x1cac0
movq %rax, 0x168(%rsp)
leaq 0x170(%rsp), %rdi
leaq 0x168(%rsp), %rsi
callq 0x492e0
testb $0x1, %al
jne 0xa38b8
jmp 0xa3aaa
leaq 0x170(%rsp), %rdi
callq 0x49320
movq (%rax), %rax
movq %rax, 0x160(%rsp)
movq 0x160(%rsp), %rax
addq $0x8, %rax
movq %rax, 0x158(%rsp)
movq 0x158(%rsp), %rdi
callq 0x1b070
movq %rax, 0x150(%rsp)
movq 0x158(%rsp), %rdi
callq 0x1aeb0
movq %rax, 0x148(%rsp)
leaq 0x150(%rsp), %rdi
leaq 0x148(%rsp), %rsi
callq 0x493c0
testb $0x1, %al
jne 0xa392c
jmp 0xa3a96
leaq 0x150(%rsp), %rdi
callq 0x1b4d0
movq (%rax), %rax
movq %rax, 0x140(%rsp)
movq 0x140(%rsp), %rax
cmpl $0x9, (%rax)
jne 0xa3a82
movq 0x140(%rsp), %rsi
addq $0x10, %rsi
leaq 0xa8cd8(%rip), %rdi     # 0x14c640
callq 0x7d0d0
movq %rax, 0x40(%rsp)
jmp 0xa3974
movq 0x40(%rsp), %rsi
leaq 0x120(%rsp), %rdi
callq 0x38690
jmp 0xa3988
cmpl $0x121, 0x120(%rsp) # imm = 0x121
jne 0xa3a61
movq 0x160(%rsp), %rax
movq %rax, 0x30(%rsp)
movq 0x140(%rsp), %rsi
movq %rsi, 0x38(%rsp)
addq $0x88, %rsi
leaq 0xe0(%rsp), %rdi
callq 0x43130
jmp 0xa39c9
movq 0x38(%rsp), %rsi
movq 0x30(%rsp), %rdi
leaq 0xe0(%rsp), %rdx
callq 0xa3170
movb %al, 0x2f(%rsp)
jmp 0xa39e6
movb 0x2f(%rsp), %al
xorb $-0x1, %al
movb %al, 0x2e(%rsp)
leaq 0xe0(%rsp), %rdi
callq 0x1f210
movb 0x2e(%rsp), %al
testb $0x1, %al
jne 0xa3a07
jmp 0xa3a42
jmp 0xa3a5f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x248(%rsp)
movl %eax, 0x244(%rsp)
jmp 0xa3a70
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x248(%rsp)
movl %eax, 0x244(%rsp)
leaq 0xe0(%rsp), %rdi
callq 0x1f210
jmp 0xa3a70
movq 0x140(%rsp), %rsi
addq $0x10, %rsi
leaq 0x208(%rsp), %rdi
callq 0xa3fc0
jmp 0xa3a5d
jmp 0xa3a5f
jmp 0xa3a61
leaq 0x120(%rsp), %rdi
callq 0x35ab0
jmp 0xa3a82
leaq 0x120(%rsp), %rdi
callq 0x35ab0
jmp 0xa3d69
jmp 0xa3a84
leaq 0x150(%rsp), %rdi
callq 0x49460
jmp 0xa390e
jmp 0xa3a98
leaq 0x170(%rsp), %rdi
callq 0x49330
jmp 0xa389a
jmp 0xa3aac
leaq 0x190(%rsp), %rdi
callq 0x49260
jmp 0xa3826
movq 0x288(%rsp), %rax
addq $0x28, %rax
movq %rax, 0xd8(%rsp)
movq 0xd8(%rsp), %rdi
callq 0x1d3c0
movq %rax, 0xd0(%rsp)
movq 0xd8(%rsp), %rdi
callq 0x1d240
movq %rax, 0xc8(%rsp)
leaq 0xd0(%rsp), %rdi
leaq 0xc8(%rsp), %rsi
callq 0x49210
testb $0x1, %al
jne 0xa3b1a
jmp 0xa3d47
leaq 0xd0(%rsp), %rdi
callq 0x49250
movq (%rax), %rax
movq %rax, 0xc0(%rsp)
movq 0xc0(%rsp), %rsi
addq $0x8, %rsi
leaq 0x208(%rsp), %rdi
callq 0x8f940
movq %rax, 0x20(%rsp)
jmp 0xa3b52
movq 0x20(%rsp), %rax
movq %rax, 0xb8(%rsp)
leaq 0x208(%rsp), %rdi
callq 0x8f6e0
movq %rax, 0xb0(%rsp)
leaq 0xb8(%rsp), %rdi
leaq 0xb0(%rsp), %rsi
callq 0x8f710
testb $0x1, %al
jne 0xa3b92
jmp 0xa3d33
movq 0xc0(%rsp), %rsi
addq $0x8, %rsi
leaq 0xa8a9b(%rip), %rdi     # 0x14c640
callq 0x7d0d0
movq %rax, 0x18(%rsp)
jmp 0xa3bb1
movq 0x18(%rsp), %rax
movl $0x122, (%rax)          # imm = 0x122
movq 0xc0(%rsp), %rax
addq $0x30, %rax
movq %rax, 0xa8(%rsp)
movq 0xa8(%rsp), %rdi
callq 0x1cc40
movq %rax, 0xa0(%rsp)
movq 0xa8(%rsp), %rdi
callq 0x1cac0
movq %rax, 0x98(%rsp)
leaq 0xa0(%rsp), %rdi
leaq 0x98(%rsp), %rsi
callq 0x492e0
testb $0x1, %al
jne 0xa3c18
jmp 0xa3d31
leaq 0xa0(%rsp), %rdi
callq 0x49320
movq (%rax), %rax
movq %rax, 0x90(%rsp)
movl $0x0, 0x8c(%rsp)
movslq 0x8c(%rsp), %rax
movq %rax, 0x10(%rsp)
movq 0x90(%rsp), %rdi
addq $0x8, %rdi
callq 0x18440
movq %rax, %rcx
movq 0x10(%rsp), %rax
cmpq %rcx, %rax
jae 0xa3d1d
movq 0x90(%rsp), %rdi
addq $0x8, %rdi
movslq 0x8c(%rsp), %rsi
callq 0x18460
movq (%rax), %rax
movq %rax, 0x80(%rsp)
movq 0x80(%rsp), %rax
cmpl $0xa, (%rax)
jne 0xa3d05
movl $0xe0, %edi
callq 0x8340
movq %rax, 0x8(%rsp)
jmp 0xa3cac
movq 0x8(%rsp), %rdi
movl $0xa, %esi
callq 0x373d0
jmp 0xa3cbd
movq 0x90(%rsp), %rdi
addq $0x8, %rdi
movslq 0x8c(%rsp), %rsi
callq 0x18460
movq 0x8(%rsp), %rcx
movq %rcx, (%rax)
jmp 0xa3d05
movq 0x8(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x248(%rsp)
movl %eax, 0x244(%rsp)
movl $0xe0, %esi
callq 0x8350
jmp 0xa3d69
jmp 0xa3d07
movl 0x8c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x8c(%rsp)
jmp 0xa3c3b
jmp 0xa3d1f
leaq 0xa0(%rsp), %rdi
callq 0x49330
jmp 0xa3bfa
jmp 0xa3d33
jmp 0xa3d35
leaq 0xd0(%rsp), %rdi
callq 0x49260
jmp 0xa3afc
leaq 0x208(%rsp), %rdi
callq 0x35900
leaq 0x250(%rsp), %rdi
callq 0xa3ff0
addq $0x298, %rsp            # imm = 0x298
retq
leaq 0x208(%rsp), %rdi
callq 0x35900
leaq 0x250(%rsp), %rdi
callq 0xa3ff0
movq 0x248(%rsp), %rdi
callq 0x8670