// File: /workspace/github_cpp_makefile_star2_O3/luooofan[P]hblzbd_compiler/src/Pass/convert_ssa.cpp
// Function: ConvertSSA::ResolveOpn2Value(ir::Opn*, SSABasicBlock*)
Value* ConvertSSA::ResolveOpn2Value(Opn* opn, SSABasicBlock* ssabb) {
  // std::cout << "Resolve Begin:" << std::endl;
  Value* res = nullptr;
  auto type = opn->type_;
  if (type == Opn::Type::Null) {
    MyAssert(0);
  } else if (type == Opn::Type::Imm) {  // new一个常量value直接返回
    res = new ConstantInt(opn->imm_num_);
  } else if (type == Opn::Type::Func || 0 == opn->scope_id_) {
    auto comp_name = opn->GetCompName();
    res = this->FindValueFromCompName(comp_name, true);
    MyAssert(nullptr != res && res->GetType()->IsPointer());

    if (type != Opn::Type::Func) {  // var & array
      // maintain used_glob_var
      ssabb->GetFunction()->AddUsedGlobVar(dynamic_cast<GlobalVariable*>(res));
    }

    // new load load出的结果不需要记录 全局量每次都要load
    if (type == Opn::Type::Var) {
      static int glob_temp_index = 1;
      res = new LoadInst(new Type(Type::IntegerTyID), "glotmp-" + std::to_string(glob_temp_index++), res, ssabb);
    }  // 全局数组直接返回i32 pointer

  } else {  // label标签 或 局部变量数组 局部变量可能未定义 局部数组只会返回i32 pointer类型的value
    auto comp_name = opn->GetCompName();
    res = this->FindValueFromCompName(comp_name, false);

    if (nullptr == res) {  // 未定义变量
      // std::cout << "Undefined variable:" << std::string(*opn) << std::endl;
      MyAssert(type == Opn::Type::Var);
      // new undefval
      res = new UndefVariable(new Type(Type::IntegerTyID), comp_name);
      work_map[comp_name] = res;
    }
  }
  MyAssert(nullptr != res);
  return res;
  // std::cout << "Resolve End." << std::endl;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %rsi, %r12
movl (%rsi), %ebp
cmpl $0x1, %ebp
je 0x40a7f
movq %rdx, %r14
movq %rdi, %r15
cmpl $0x3, %ebp
je 0x409e8
cmpl $0x4, %ebp
je 0x40c94
cmpl $0x0, 0x28(%r12)
je 0x409e8
movq %rsp, %rbx
movq %rbx, %rdi
movq %r12, %rsi
callq 0x28a4a
addq $0xa8, %r15
movq %r15, %rdi
movq %rbx, %rsi
callq 0x462be
testq %rax, %rax
je 0x4098c
movq 0x28(%rax), %rbx
testq %rbx, %rbx
jne 0x40a62
testl %ebp, %ebp
jne 0x40ce4
movl $0x48, %edi
callq 0x8290
movq %rax, %rbx
movl $0x10, %edi
callq 0x8290
leaq 0x4dede(%rip), %rcx     # 0x8e890
movq %rcx, (%rax)
movl $0x2, 0x8(%rax)
movq %rsp, %rdx
movq %rbx, %rdi
movq %rax, %rsi
callq 0x34ade
leaq 0x4db87(%rip), %rax     # 0x8e558
addq $0x10, %rax
movq %rax, (%rbx)
movq %rsp, %rsi
movq %r15, %rdi
callq 0x463ba
movq %rbx, (%rax)
jmp 0x40a62
movq %rsp, %rbx
movq %rbx, %rdi
movq %r12, %rsi
callq 0x28a4a
addq $0xe0, %r15
movq %r15, %rdi
movq %rbx, %rsi
callq 0x462be
testq %rax, %rax
je 0x40c31
movq 0x28(%rax), %r15
testq %r15, %r15
je 0x40c31
movq 0x8(%r15), %rdi
movq (%rdi), %rax
callq *0x30(%rax)
testb %al, %al
je 0x40c31
cmpl $0x3, %ebp
je 0x40a5f
movq (%r14), %rbx
leaq 0x4da41(%rip), %rsi     # 0x8e480
leaq 0x4da4a(%rip), %rdx     # 0x8e490
movq %r15, %rdi
xorl %ecx, %ecx
callq 0x8320
movq %rbx, %rdi
movq %rax, %rsi
callq 0x357dc
testl %ebp, %ebp
je 0x40aab
movq %r15, %rbx
leaq 0x10(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x40a99
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x82a0
jmp 0x40a99
movl $0x50, %edi
callq 0x8290
movq %rax, %rbx
movl 0x4(%r12), %esi
movq %rax, %rdi
callq 0x43db8
movq %rbx, %rax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x68, %edi
callq 0x8290
movq %rax, %rbx
movl $0x10, %edi
callq 0x8290
leaq 0x4ddc7(%rip), %rcx     # 0x8e890
movq %rcx, (%rax)
movl $0x2, 0x8(%rax)
movl 0x4e826(%rip), %r13d    # 0x8f300
leal 0x1(%r13), %ecx
movl %ecx, 0x4e81c(%rip)     # 0x8f300
movl %r13d, %ebp
negl %ebp
cmovsl %r13d, %ebp
movl $0x1, %r12d
cmpl $0xa, %ebp
jb 0x40b41
movl $0x4, %r12d
movl $0xd1b71759, %esi       # imm = 0xD1B71759
movl %ebp, %ecx
cmpl $0x63, %ecx
jbe 0x40b38
cmpl $0x3e7, %ecx            # imm = 0x3E7
jbe 0x40b3e
cmpl $0x2710, %ecx           # imm = 0x2710
jb 0x40b41
movl %ecx, %edx
imulq %rsi, %rdx
shrq $0x2d, %rdx
addl $0x4, %r12d
cmpl $0x1869f, %ecx          # imm = 0x1869F
movl %edx, %ecx
ja 0x40b05
addl $-0x3, %r12d
jmp 0x40b41
addl $-0x2, %r12d
jmp 0x40b41
decl %r12d
movq %rax, 0x40(%rsp)
shrl $0x1f, %r13d
leal (%r12,%r13), %esi
leaq 0x30(%rsp), %rax
movq %rax, -0x10(%rax)
leaq 0x20(%rsp), %rdi
movl $0x2d, %edx
callq 0x8350
leaq 0x20(%rsp), %rax
addq (%rax), %r13
movq %r13, %rdi
movl %r12d, %esi
movl %ebp, %edx
callq 0xc35d
leaq 0x30c3f(%rip), %rcx     # 0x717c1
movl $0x7, %r8d
leaq 0x20(%rsp), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x8310
leaq 0x58(%rsp), %r12
movq %r12, -0x10(%r12)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
je 0x40bbe
movq %rdx, 0x48(%rsp)
movq (%rcx), %rdx
movq %rdx, 0x58(%rsp)
jmp 0x40bc6
movups (%rcx), %xmm0
movups %xmm0, (%r12)
movq 0x8(%rax), %rsi
leaq 0x48(%rsp), %rdx
movq %rsi, 0x8(%rdx)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
movq %rbx, %rdi
movq 0x40(%rsp), %rsi
movq %r15, %rcx
movq %r14, %r8
callq 0x43e6c
movq 0x48(%rsp), %rdi
cmpq %r12, %rdi
je 0x40c0c
movq 0x58(%rsp), %rsi
incq %rsi
callq 0x82a0
movq 0x20(%rsp), %rdi
leaq 0x30(%rsp), %rax
cmpq %rax, %rdi
je 0x40a62
movq 0x30(%rsp), %rsi
incq %rsi
callq 0x82a0
jmp 0x40a62
movq 0x4e3b0(%rip), %rdi     # 0x8efe8
leaq 0x2d529(%rip), %rsi     # 0x6e168
movl $0x8, %edx
callq 0x8330
movq 0x4e398(%rip), %rdi     # 0x8efe8
leaq 0x30b51(%rip), %rsi     # 0x717a8
movl $0x18, %edx
callq 0x8330
movq 0x4e380(%rip), %rdi     # 0x8efe8
leaq 0x2e751(%rip), %rsi     # 0x6f3c0
movl $0x1, %edx
callq 0x8330
movq 0x4e368(%rip), %rdi     # 0x8efe8
movl $0x45, %esi
callq 0x8520
movq %rax, %rdi
callq 0x8180
jmp 0x40cda
movq 0x4e34d(%rip), %rdi     # 0x8efe8
leaq 0x2d4c6(%rip), %rsi     # 0x6e168
callq 0x8280
leaq 0x30afa(%rip), %rsi     # 0x717a8
movq %rax, %rdi
callq 0x8280
leaq 0x2e703(%rip), %rsi     # 0x6f3c0
movq %rax, %rdi
callq 0x8280
movq %rax, %rdi
movl $0x3f, %esi
callq 0x8520
movq %rax, %rdi
callq 0x8180
movl $0xff, %edi
callq 0x8380
movq 0x4e2fd(%rip), %rdi     # 0x8efe8
leaq 0x2d476(%rip), %rsi     # 0x6e168
movl $0x8, %edx
callq 0x8330
movq 0x4e2e5(%rip), %rdi     # 0x8efe8
leaq 0x30a9e(%rip), %rsi     # 0x717a8
movl $0x18, %edx
callq 0x8330
movq 0x4e2cd(%rip), %rdi     # 0x8efe8
leaq 0x2e69e(%rip), %rsi     # 0x6f3c0
movl $0x1, %edx
callq 0x8330
movq 0x4e2b5(%rip), %rdi     # 0x8efe8
movl $0x58, %esi
callq 0x8520
movq %rax, %rdi
callq 0x8180
jmp 0x40cda
movq %rax, %r14
movq 0x48(%rsp), %rdi
cmpq %r12, %rdi
je 0x40d66
movq 0x58(%rsp), %rsi
incq %rsi
callq 0x82a0
jmp 0x40d66
movq %rax, %r14
movq 0x20(%rsp), %rdi
leaq 0x30(%rsp), %rax
cmpq %rax, %rdi
je 0x40d89
movq 0x30(%rsp), %rsi
incq %rsi
callq 0x82a0
jmp 0x40d89
jmp 0x40d86
movq %rax, %r14
movl $0x68, %esi
jmp 0x40d98
movq %rax, %r14
movl $0x48, %esi
movq %rbx, %rdi
callq 0x82a0
jmp 0x40db4
movq %rax, %r14
movl $0x50, %esi
movq %rbx, %rdi
jmp 0x40dca
jmp 0x40db1
movq %rax, %r14
leaq 0x10(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x40dcf
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x82a0
movq %r14, %rdi
callq 0x8540