// File: /workspace/github_cpp_makefile_star2_O2/luooofan[P]hblzbd_compiler/src/main.cpp
// Function: main
int main(int argc, char **argv) {
  // MyAssert(0);
  bool opt = false, print_usage = false;
  char *src = nullptr, *output = nullptr, *log_file = nullptr;
  // parse command line options and check
  for (int ch; (ch = getopt(argc, argv, "Sl:o:O:h")) != -1;) {
    switch (ch) {
      case 'S':
        // do nothing
        break;
      case 'l':
        log_file = strdup(optarg);
        break;
      case 'o':
        output = strdup(optarg);
        break;
      case 'O':
        opt = atoi(optarg) > 0;
        break;
      case 'h':
        print_usage = true;
        break;
      default:
        break;
    }
  }

  if (optind <= argc) {
    src = argv[optind];
  }

  if (src == nullptr || print_usage) {
    std::cerr << "Usage: " << argv[0] << " [-S] [-l log_file] [-o output_file] [-O level] input_file" << std::endl;
    return 1;
  }

  if (nullptr != src) {
    freopen(src, "r", stdin);
  }
  std::ofstream logfile;
  if (nullptr != log_file) {
    logfile.open(log_file);
  }

#ifdef DEBUG_PROCESS
  std::cout << "Parser Start:" << std::endl;
#endif
  yyset_lineno(1);
  yyparse();  // if success, ast_root is valid
  // MyAssert(0);
#ifdef DEBUG_PROCESS
  std::cout << "Parser End." << std::endl;
#endif
  yylex_destroy();

  MyAssert(nullptr != ast_root);
  if (logfile.is_open() && AST_LOG) {
    logfile << "AST:" << std::endl;
    ast_root->PrintNode(0, logfile);
  }

#ifdef DEBUG_PROCESS
  std::cout << "Generate IR Start:" << std::endl;
#endif
  std::vector<IR *> *ir_list_ptr = new std::vector<IR *>();
  {
    ir::ContextInfo ctx;
    ast_root->GenerateIR(ctx, *ir_list_ptr);
  }
#ifdef DEBUG_PROCESS
  std::cout << "Generate IR End." << std::endl;
#endif
  // MyAssert(0);
  delete ast_root;

  if (logfile.is_open() && IRLIST_LOG) {
    ir::PrintFuncTable(logfile);
    ir::PrintScopes(logfile);
    logfile << "IRList:" << std::endl;
    for (auto ir : *ir_list_ptr) {
      ir->PrintIR(logfile);
    }
  }

  // it represents IRModule before GenerateArm Pass and ArmModule after GenerateArm Pass.
  // the source space will be released when running GenerateArm Pass.
  Module *module_ptr = static_cast<Module *>(ConstructModule(std::string(src), *ir_list_ptr));
  delete ir_list_ptr;
  // module_ptr->EmitCode(std::cout);
  Module **const module_ptr_addr = &module_ptr;

#ifdef DEBUG_PROCESS
  std::cout << "Passes Start:" << std::endl;
#endif
  PassManager pm(module_ptr_addr);

  // ==================Add Quad-Pass Below==================
  pm.AddPass<SimplifyCFG>(true);
  pm.AddPass<DeadFunctionEliminate>(true);
  // pm.AddPass<ReachDefine>(false);
  pm.AddPass<ConstantPropagation>(true);
  pm.AddPass<Instruction_Combining>(true);
  pm.AddPass<LoopUnroll>(true);
  pm.AddPass<InvariantExtrapolation>(true);
  // ==================Add Quad-Pass Above==================
  pm.AddPass<SimplifyCFG>(true);  // necessary
  pm.AddPass<ComputeDominance>(false);
  pm.AddPass<ConvertSSA>(true);
  // ==================Add SSA-Pass Below==================
  pm.AddPass<DeadCodeEliminate>(false);
  pm.AddPass<SimpleOptimize>(true);
  pm.AddPass<DeadCodeEliminate>(false);
  pm.AddPass<GlobalValueNumbering>(true);  // actually redundant common expression eliminate
  pm.AddPass<SimpleOptimize>(true);
  pm.AddPass<DeadCodeEliminate>(false);
  // ==================Add SSA-Pass Above==================
  pm.AddPass<GenerateArmFromSSA>(true);  // define macro control MUL_TO_SHIFT DIV_TO_SHIFT MOD_TO_AND optimize
  // ==================Add Arm(vreg)-Pass Below==================
  pm.AddPass<SimplifyArm>(true);
  pm.AddPass<CondBrToInsts>(true);
  // ==================Add Arm(vreg)-Pass Above==================
  pm.AddPass<RegAlloc>(true);
  pm.AddPass<SPOffsetFixup>(true);
  // ==================Add Arm-Pass Below==================
  pm.AddPass<SimplifyArm>(false);
  pm.AddPass<CondBrToInsts>(true);
  // ==================Add Arm-Pass Above==================
  if (logfile.is_open()) {
    pm.Run(PASS_LOG, logfile);
  } else {
    pm.Run();
  }

  // exit(0);
#ifdef DEBUG_PROCESS
  std::cout << "Passes End." << std::endl;
#endif
  MyAssert(typeid(*module_ptr) == typeid(ArmModule));

#ifdef DEBUG_PROCESS
  std::cout << "Emit Start:" << std::endl;
#endif
  {
    std::ofstream outfile;
    std::string file_name = "";
    if (nullptr == output) {
      file_name = std::string(src, src + strlen(src) - 1);
    } else {
      file_name = std::string(output);
    }
    outfile.open(file_name);
    if (logfile.is_open()) {
      logfile << "Emitting code to file: " << file_name << std::endl;
    }
    module_ptr->EmitCode(outfile);
    MyAssert(outfile.is_open());
    outfile << std::endl;
    if (logfile.is_open()) {
      logfile << "EmitCode finish." << std::endl;
    }
    outfile.close();
  }
#ifdef DEBUG_PROCESS
  std::cout << "Emit End." << std::endl;
#endif

  if (logfile.is_open()) {
    logfile.close();
  }

  // release the arm module space.
  delete module_ptr;
  module_ptr = nullptr;

  return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x498, %rsp            # imm = 0x498
movq %rsi, %r12
movl %edi, %ebp
xorl %ebx, %ebx
leaq 0x37107(%rip), %r15     # 0x572aa
movq 0x60de6(%rip), %r14     # 0x80f90
movq $0x0, 0x40(%rsp)
xorl %r13d, %r13d
movl %ebp, %edi
movq %r12, %rsi
movq %r15, %rdx
callq 0x80c0
cmpl $0x6f, %eax
je 0x201d9
cmpl $0x68, %eax
je 0x201e8
cmpl $0x6c, %eax
je 0x201ed
cmpl $-0x1, %eax
jne 0x201b6
jmp 0x201fa
movq (%r14), %rdi
callq 0x84f0
movq %rax, 0x40(%rsp)
jmp 0x201b6
movb $0x1, %r13b
jmp 0x201b6
movq (%r14), %rdi
callq 0x84f0
movq %rax, %rbx
jmp 0x201b6
movq 0x60dbf(%rip), %rax     # 0x80fc0
movslq (%rax), %rax
cmpl %ebp, %eax
jg 0x20219
movq (%r12,%rax,8), %r15
testq %r15, %r15
sete %al
orb %r13b, %al
testb $0x1, %al
je 0x20257
pushq $0x1
popq %rbx
movq 0x60dc5(%rip), %rdi     # 0x80fe8
leaq 0x37089(%rip), %rsi     # 0x572b3
callq 0x8240
movq (%r12), %rsi
movq %rax, %rdi
callq 0x8240
leaq 0x37079(%rip), %rsi     # 0x572bb
movq %rax, %rdi
callq 0x8240
movq %rax, %rdi
callq 0x8170
jmp 0x20810
movq 0x60d42(%rip), %rax     # 0x80fa0
movq (%rax), %rdx
leaq 0x35eeb(%rip), %rsi     # 0x56153
movq %r15, %rdi
callq 0x82a0
leaq 0x298(%rsp), %rdi
callq 0x84a0
testq %rbx, %rbx
je 0x20295
leaq 0x298(%rsp), %rdi
pushq $0x10
popq %rdx
movq %rbx, %rsi
callq 0x8060
movq 0x60d14(%rip), %rdi     # 0x80fb0
leaq 0x37053(%rip), %rsi     # 0x572f6
callq 0x8240
movq %rax, %rdi
callq 0x8170
pushq $0x1
popq %rdi
callq 0x266a0
callq 0x22724
movq 0x60cec(%rip), %rdi     # 0x80fb0
leaq 0x37039(%rip), %rsi     # 0x57304
callq 0x8240
movq %rax, %rdi
callq 0x8170
callq 0x266c5
cmpq $0x0, 0x6130b(%rip)     # 0x815f0
je 0x20824
leaq 0x308(%rsp), %rbx
movq %rbx, %rdi
callq 0x80d0
testb %al, %al
je 0x2033b
cmpb $0x1, 0x612f2(%rip)     # 0x815f8
jne 0x2033b
leaq 0x3700e(%rip), %rsi     # 0x5731d
leaq 0x298(%rsp), %rdi
callq 0x8240
movq %rax, %rdi
callq 0x8170
movq 0x612c5(%rip), %rdi     # 0x815f0
movq (%rdi), %rax
leaq 0x298(%rsp), %rdx
xorl %esi, %esi
callq *0x18(%rax)
movq 0x60c6e(%rip), %rdi     # 0x80fb0
leaq 0x36fd9(%rip), %rsi     # 0x57322
callq 0x8240
movq %rax, %rdi
callq 0x8170
pushq $0x18
popq %rdi
callq 0x8250
movq %rax, %r13
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
andq $0x0, 0x10(%rax)
leaq 0x88(%rsp), %rdi
callq 0x20986
movq 0x61270(%rip), %rdi     # 0x815f0
movq (%rdi), %rax
leaq 0x88(%rsp), %rsi
movq %r13, %rdx
callq *0x10(%rax)
leaq 0x88(%rsp), %rdi
callq 0x20ada
movq 0x60c0b(%rip), %rdi     # 0x80fb0
leaq 0x36f89(%rip), %rsi     # 0x57335
callq 0x8240
movq %rax, %rdi
callq 0x8170
movq 0x61230(%rip), %rdi     # 0x815f0
testq %rdi, %rdi
je 0x203cb
movq (%rdi), %rax
callq *0x8(%rax)
movq %rbx, %r12
movq %rbx, %rdi
callq 0x80d0
testb %al, %al
je 0x2043f
cmpb $0x1, 0x60eef(%rip)     # 0x812d0
jne 0x2043f
leaq 0x298(%rsp), %rdi
callq 0x1d66a
leaq 0x298(%rsp), %rdi
callq 0x1d538
leaq 0x36f42(%rip), %rsi     # 0x57346
leaq 0x298(%rsp), %rdi
callq 0x8240
movq %rax, %rdi
callq 0x8170
movq (%r13), %r14
movq 0x8(%r13), %rbx
leaq 0x298(%rsp), %rbp
cmpq %rbx, %r14
je 0x2043f
movq (%r14), %rdi
movq %rbp, %rsi
callq 0x1e1dc
addq $0x8, %r14
jmp 0x20429
leaq 0x88(%rsp), %rdi
leaq 0x10(%rsp), %rdx
movq %r15, %rsi
callq 0xa292
leaq 0x88(%rsp), %rdi
movq %r13, %rsi
callq 0xd128
movq %rax, %rbp
leaq 0x88(%rsp), %rdi
callq 0x8578
movq %rbp, 0x38(%rsp)
movq %r13, %rdi
callq 0x1ffb8
pushq $0x18
popq %rsi
movq %r13, %rdi
callq 0x8260
movq 0x60b1d(%rip), %r13     # 0x80fb0
leaq 0x36eb4(%rip), %rsi     # 0x5734e
movq %r13, %rdi
callq 0x8240
movq %rax, %rdi
callq 0x8170
leaq 0x10(%rsp), %rdi
andq $0x0, 0x10(%rdi)
xorps %xmm0, %xmm0
movaps %xmm0, (%rdi)
leaq 0x38(%rsp), %rax
movq %rax, 0x18(%rdi)
pushq $0x1
popq %rsi
callq 0x20b46
movq 0x40(%rsp), %r14
leaq 0x10(%rsp), %rdi
pushq $0x1
popq %rsi
callq 0x20c72
leaq 0x10(%rsp), %rdi
pushq $0x1
popq %rsi
callq 0x20d9e
leaq 0x10(%rsp), %rdi
pushq $0x1
popq %rsi
callq 0x20eca
leaq 0x10(%rsp), %rdi
pushq $0x1
popq %rsi
callq 0x20ff6
leaq 0x10(%rsp), %rdi
pushq $0x1
popq %rsi
callq 0x21122
leaq 0x10(%rsp), %rdi
pushq $0x1
popq %rsi
callq 0x20b46
leaq 0x10(%rsp), %rdi
xorl %esi, %esi
callq 0x2124e
leaq 0x10(%rsp), %rdi
pushq $0x1
popq %rsi
callq 0x2136e
leaq 0x10(%rsp), %rdi
xorl %esi, %esi
callq 0x2148e
leaq 0x10(%rsp), %rdi
pushq $0x1
popq %rsi
callq 0x215aa
leaq 0x10(%rsp), %rdi
xorl %esi, %esi
callq 0x2148e
leaq 0x10(%rsp), %rdi
pushq $0x1
popq %rsi
callq 0x216d6
leaq 0x10(%rsp), %rdi
pushq $0x1
popq %rsi
callq 0x215aa
leaq 0x10(%rsp), %rdi
xorl %esi, %esi
callq 0x2148e
leaq 0x10(%rsp), %rdi
pushq $0x1
popq %rsi
callq 0x217f6
leaq 0x10(%rsp), %rdi
pushq $0x1
popq %rsi
callq 0x21916
leaq 0x10(%rsp), %rdi
pushq $0x1
popq %rsi
callq 0x21a42
leaq 0x10(%rsp), %rdi
pushq $0x1
popq %rsi
callq 0x21b6e
leaq 0x10(%rsp), %rdi
pushq $0x1
popq %rsi
callq 0x21c9a
leaq 0x10(%rsp), %rdi
xorl %esi, %esi
callq 0x21916
leaq 0x10(%rsp), %rdi
pushq $0x1
popq %rsi
callq 0x21a42
movq %r12, %rdi
callq 0x80d0
movb 0x6100f(%rip), %cl      # 0x815f9
andb %al, %cl
testb %al, %al
leaq 0x298(%rsp), %rdx
jne 0x205fb
movq %r13, %rdx
movzbl %cl, %esi
leaq 0x10(%rsp), %rdi
callq 0x21dc6
movq 0x609a1(%rip), %rdi     # 0x80fb0
leaq 0x36d46(%rip), %rsi     # 0x5735c
callq 0x8240
movq %rax, %rdi
callq 0x8170
movq 0x38(%rsp), %rax
testq %rax, %rax
je 0x2086d
movq (%rax), %rax
movq -0x8(%rax), %rdi
leaq 0x5eff1(%rip), %rsi     # 0x7f630
callq 0x21ef0
testb %al, %al
je 0x20872
movq 0x6095d(%rip), %rdi     # 0x80fb0
leaq 0x36d0e(%rip), %rsi     # 0x57368
callq 0x8240
movq %rax, %rdi
callq 0x8170
leaq 0x88(%rsp), %rdi
callq 0x84a0
leaq 0x35ace(%rip), %rsi     # 0x56149
leaq 0x48(%rsp), %rdi
leaq 0x68(%rsp), %rdx
callq 0xa292
testq %r14, %r14
je 0x206a3
leaq 0x68(%rsp), %rdi
leaq 0xf(%rsp), %rdx
movq %r14, %rsi
callq 0xa292
jmp 0x206c8
movq %r15, %rdi
callq 0x8140
leaq (%r15,%rax), %rdx
decq %rdx
leaq 0x78(%rsp), %rax
movq %rax, -0x10(%rax)
leaq 0x68(%rsp), %rdi
movq %r15, %rsi
callq 0x221f2
leaq 0x48(%rsp), %rdi
leaq 0x68(%rsp), %r14
movq %r14, %rsi
callq 0x8320
movq %r14, %rdi
callq 0x8578
leaq 0x88(%rsp), %rdi
leaq 0x48(%rsp), %rsi
pushq $0x10
popq %rdx
callq 0x8160
movq %r12, %rdi
callq 0x80d0
testb %al, %al
je 0x2072c
leaq 0x36c6a(%rip), %rsi     # 0x57374
leaq 0x298(%rsp), %rdi
callq 0x8240
leaq 0x48(%rsp), %rsi
movq %rax, %rdi
callq 0x8230
movq %rax, %rdi
callq 0x8170
movq 0x38(%rsp), %rdi
movq (%rdi), %rax
leaq 0x88(%rsp), %rsi
callq *0x10(%rax)
leaq 0xf8(%rsp), %rdi
callq 0x80d0
testb %al, %al
je 0x208ba
leaq 0x88(%rsp), %rdi
callq 0x8170
movq %r12, %rdi
callq 0x80d0
testb %al, %al
je 0x20789
leaq 0x36c18(%rip), %rsi     # 0x5738c
leaq 0x298(%rsp), %rdi
callq 0x8240
movq %rax, %rdi
callq 0x8170
leaq 0x88(%rsp), %rdi
callq 0x83b0
leaq 0x48(%rsp), %rdi
callq 0x8578
leaq 0x88(%rsp), %rdi
callq 0x83f0
movq 0x607fc(%rip), %rdi     # 0x80fb0
leaq 0x36be2(%rip), %rsi     # 0x5739d
callq 0x8240
movq %rax, %rdi
callq 0x8170
movq %r12, %rdi
callq 0x80d0
testb %al, %al
je 0x207e1
leaq 0x298(%rsp), %rdi
callq 0x83b0
movq 0x38(%rsp), %rdi
testq %rdi, %rdi
je 0x207f1
movq (%rdi), %rax
callq *0x8(%rax)
andq $0x0, 0x38(%rsp)
leaq 0x10(%rsp), %rdi
callq 0x22288
leaq 0x298(%rsp), %rdi
callq 0x83f0
xorl %ebx, %ebx
movl %ebx, %eax
addq $0x498, %rsp            # imm = 0x498
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x607bd(%rip), %rdi     # 0x80fe8
leaq 0x358b6(%rip), %rsi     # 0x560e8
callq 0x8240
leaq 0x36ad2(%rip), %rsi     # 0x57310
movq %rax, %rdi
callq 0x8240
leaq 0x36ad3(%rip), %rsi     # 0x57320
movq %rax, %rdi
callq 0x8240
pushq $0x68
popq %rsi
movq %rax, %rdi
callq 0x84b0
movq %rax, %rdi
callq 0x8170
jmp 0x20900
callq 0x82b0
movq 0x6076f(%rip), %rdi     # 0x80fe8
leaq 0x35868(%rip), %rsi     # 0x560e8
callq 0x8240
leaq 0x36a84(%rip), %rsi     # 0x57310
movq %rax, %rdi
callq 0x8240
leaq 0x36a85(%rip), %rsi     # 0x57320
movq %rax, %rdi
callq 0x8240
movq %rax, %rdi
movl $0xba, %esi
callq 0x84b0
movq %rax, %rdi
callq 0x8170
jmp 0x20900
movq 0x60727(%rip), %rdi     # 0x80fe8
leaq 0x35820(%rip), %rsi     # 0x560e8
callq 0x8240
leaq 0x36a3c(%rip), %rsi     # 0x57310
movq %rax, %rdi
callq 0x8240
leaq 0x36a3d(%rip), %rsi     # 0x57320
movq %rax, %rdi
callq 0x8240
movq %rax, %rdi
movl $0xcc, %esi
callq 0x84b0
movq %rax, %rdi
callq 0x8170
movl $0xff, %edi
callq 0x8300
jmp 0x20941
jmp 0x20941
movq %rax, %rbx
jmp 0x2094e
jmp 0x20964
movq %rax, %rbx
leaq 0x88(%rsp), %rdi
callq 0x8578
jmp 0x20971
jmp 0x2095f
movq %rax, %rbx
leaq 0x88(%rsp), %rdi
callq 0x20ada
jmp 0x20971
jmp 0x2095f
jmp 0x2095f
jmp 0x2095f
movq %rax, %rbx
leaq 0x48(%rsp), %rdi
callq 0x8578
leaq 0x88(%rsp), %rdi
callq 0x83f0
jmp 0x20967
jmp 0x2095f
movq %rax, %rbx
jmp 0x20971
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x22288
leaq 0x298(%rsp), %rdi
callq 0x83f0
movq %rbx, %rdi
callq 0x84d0