// File: /workspace/github_cpp_makefile_star2_O1/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x118, %rsp            # imm = 0x118
movq 0x8(%rdi), %rbx
movq (%rbx), %rdi
testq %rdi, %rdi
je 0x4a9b4
leaq 0x43c78(%rip), %rsi     # 0x8e618
leaq 0x446d1(%rip), %rdx     # 0x8f078
xorl %ecx, %ecx
callq 0x8320
movq %rax, (%rsp)
jmp 0x4a9bc
movq $0x0, (%rsp)
leaq 0x88(%rsp), %rcx
movq %rbx, -0x18(%rcx)
movq %rcx, -0x10(%rcx)
movq $0x0, -0x8(%rcx)
xorl %eax, %eax
movb %al, (%rcx)
movb %al, 0x10(%rcx)
leaq 0x45236(%rip), %rax     # 0x8fc18
addq $0x10, %rax
movq %rax, -0x20(%rcx)
leaq 0x68(%rsp), %rdi
callq 0x5a9d8
leaq 0x110(%rsp), %rax
movq %rax, -0x30(%rax)
movq $0x1, -0x28(%rax)
xorps %xmm0, %xmm0
movups %xmm0, -0x20(%rax)
movl $0x3f800000, -0x10(%rax) # imm = 0x3F800000
movups %xmm0, -0x8(%rax)
leaq 0x45b6f(%rip), %rax     # 0x90590
movq 0x10(%rax), %rbp
testq %rbp, %rbp
je 0x4aaae
leaq 0x50(%rsp), %rbx
leaq 0x38(%rsp), %r13
leaq 0x28(%rsp), %r14
leaq 0x2511c(%rip), %r15     # 0x6fb60
leaq 0xe0(%rsp), %r12
leaq 0x8(%rbp), %rsi
movq %r14, %rdi
callq 0x4ad94
cmpl $0x121, 0x48(%rsp)      # imm = 0x121
jne 0x4aa86
movq %r14, %rdi
movq %r15, %rsi
callq 0x8100
testl %eax, %eax
je 0x4aa86
movq %r12, 0x20(%rsp)
movq %r12, %rdi
movq %r14, %rsi
leaq 0x20(%rsp), %rdx
callq 0x48e46
movq %rbx, %rdi
callq 0x25816
movq 0x28(%rsp), %rdi
cmpq %r13, %rdi
je 0x4aaa5
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x82a0
movq (%rbp), %rbp
testq %rbp, %rbp
jne 0x4aa4c
movq (%rsp), %rax
movq 0x28(%rax), %rcx
movq 0x30(%rax), %rax
movq %rax, 0x10(%rsp)
cmpq %rax, %rcx
je 0x4abd9
movq %rcx, 0x18(%rsp)
movq (%rcx), %rax
movq 0x30(%rax), %r15
movq 0x38(%rax), %rax
movq %rax, 0x8(%rsp)
cmpq %rax, %r15
je 0x4abc5
movq (%r15), %rbp
movq 0x8(%rbp), %r12
movq 0x10(%rbp), %r14
cmpq %r14, %r12
je 0x4abb6
movq (%r12), %r13
cmpl $0x9, (%r13)
jne 0x4abad
leaq 0x10(%r13), %rbx
leaq 0x45a7c(%rip), %rdi     # 0x90590
movq %rbx, %rsi
callq 0x3d232
movq (%rax), %rcx
movq %rcx, 0x28(%rsp)
addq $0x8, %rax
leaq 0x30(%rsp), %rdi
movq %rax, %rsi
callq 0x2562c
cmpl $0x121, 0x28(%rsp)      # imm = 0x121
jne 0x4aba3
leaq 0x88(%r13), %rsi
leaq 0xa0(%rsp), %rdi
callq 0x28186
movq %rbp, %rdi
movq %r13, %rsi
leaq 0xa0(%rsp), %rdx
callq 0x4a601
movl %eax, %r13d
movq 0xa8(%rsp), %rdi
leaq 0xb8(%rsp), %rax
cmpq %rax, %rdi
je 0x4ab8e
movq 0xb8(%rsp), %rsi
incq %rsi
callq 0x82a0
testb %r13b, %r13b
je 0x4aba3
leaq 0xe0(%rsp), %rdi
movq %rbx, %rsi
callq 0x4b108
leaq 0x30(%rsp), %rdi
callq 0x25816
addq $0x8, %r12
jmp 0x4aaf1
addq $0x8, %r15
cmpq 0x8(%rsp), %r15
jne 0x4aae6
movq 0x18(%rsp), %rcx
addq $0x8, %rcx
cmpq 0x10(%rsp), %rcx
jne 0x4aac8
movq (%rsp), %rax
movq 0x28(%rax), %r12
movq 0x30(%rax), %rax
movq %rax, 0x8(%rsp)
cmpq %rax, %r12
je 0x4ac9a
movq (%r12), %rbx
leaq 0x8(%rbx), %r15
leaq 0xe0(%rsp), %rdi
movq %r15, %rsi
callq 0x48ae0
testq %rax, %rax
je 0x4ac8b
leaq 0x45979(%rip), %rdi     # 0x90590
movq %r15, %rsi
callq 0x3d232
movl $0x122, (%rax)          # imm = 0x122
movq 0x30(%rbx), %rbp
movq 0x38(%rbx), %r14
cmpq %r14, %rbp
je 0x4ac8b
movq (%rbp), %rbx
movq 0x8(%rbx), %rax
cmpq %rax, 0x10(%rbx)
je 0x4ac85
xorl %r13d, %r13d
movq (%rax,%r13,8), %rax
cmpl $0xa, (%rax)
jne 0x4ac6e
movl $0xe0, %edi
callq 0x8290
movq %rax, %r15
movq %rax, %rdi
movl $0xa, %esi
callq 0x2530e
movq 0x8(%rbx), %rax
movq %r15, (%rax,%r13,8)
incq %r13
movq 0x8(%rbx), %rax
movq 0x10(%rbx), %rcx
subq %rax, %rcx
sarq $0x3, %rcx
cmpq %r13, %rcx
ja 0x4ac43
addq $0x8, %rbp
jmp 0x4ac2d
addq $0x8, %r12
cmpq 0x8(%rsp), %r12
jne 0x4abf3
leaq 0xe0(%rsp), %rdi
callq 0x25458
leaq 0x44422(%rip), %rax     # 0x8f0d0
movq %rax, 0x68(%rsp)
movq 0x78(%rsp), %rdi
leaq 0x88(%rsp), %rax
cmpq %rax, %rdi
je 0x4acd5
movq 0x88(%rsp), %rsi
incq %rsi
callq 0x82a0
addq $0x118, %rsp            # imm = 0x118
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
jmp 0x4ad5e
movq %rax, %rbx
leaq 0x28(%rsp), %rdi
callq 0x24814
jmp 0x4ad51
jmp 0x4ad4e
jmp 0x4ad4e
jmp 0x4ad4e
movq %rax, %rbx
movq 0xa8(%rsp), %rdi
leaq 0xb8(%rsp), %rax
cmpq %rax, %rdi
je 0x4ad42
movq 0xb8(%rsp), %rsi
incq %rsi
callq 0x82a0
jmp 0x4ad42
jmp 0x4ad4e
movq %rax, %rbx
movl $0xe0, %esi
movq %r15, %rdi
callq 0x82a0
jmp 0x4ad51
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
callq 0x25816
jmp 0x4ad51
movq %rax, %rbx
leaq 0xe0(%rsp), %rdi
callq 0x25458
leaq 0x4436b(%rip), %rax     # 0x8f0d0
movq %rax, 0x68(%rsp)
movq 0x78(%rsp), %rdi
leaq 0x88(%rsp), %rax
cmpq %rax, %rdi
je 0x4ad8c
movq 0x88(%rsp), %rsi
incq %rsi
callq 0x82a0
movq %rbx, %rdi
callq 0x8540