// File: /workspace/github_cpp_makefile_star2_O1/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 0x4111d
movq %rdx, %r14
movq %rdi, %r15
cmpl $0x3, %ebp
je 0x410f2
cmpl $0x4, %ebp
je 0x41418
cmpl $0x0, 0x28(%r12)
je 0x410f2
movq %rsp, %rbx
movq %rbx, %rdi
movq %r12, %rsi
callq 0x283ba
addq $0xa8, %r15
movq %r15, %rdi
movq %rbx, %rsi
callq 0x46a24
testq %rax, %rax
je 0x4122e
movq 0x28(%rax), %rbx
jmp 0x41230
movq %rsp, %rbx
movq %rbx, %rdi
movq %r12, %rsi
callq 0x283ba
addq $0xe0, %r15
movq %r15, %rdi
movq %rbx, %rsi
callq 0x46a24
testq %rax, %rax
je 0x4113c
movq 0x28(%rax), %r15
jmp 0x4113f
movl $0x50, %edi
callq 0x8290
movq %rax, %rbx
movl 0x4(%r12), %esi
movq %rax, %rdi
callq 0x4454c
jmp 0x413a3
xorl %r15d, %r15d
testq %r15, %r15
je 0x413b5
movq 0x8(%r15), %rdi
movq (%rdi), %rax
callq *0x30(%rax)
testb %al, %al
je 0x413b5
cmpl $0x3, %ebp
je 0x41189
movq (%r14), %rbx
leaq 0x4e347(%rip), %rsi     # 0x8f4b0
leaq 0x4e350(%rip), %rdx     # 0x8f4c0
movq %r15, %rdi
xorl %ecx, %ecx
callq 0x8320
movq %rbx, %rdi
movq %rax, %rsi
callq 0x3567c
testl %ebp, %ebp
je 0x41191
movq %r15, %rbx
jmp 0x41388
movl $0x68, %edi
callq 0x8290
movq %rax, %rbx
movl $0x10, %edi
callq 0x8290
leaq 0x4e711(%rip), %rcx     # 0x8f8c0
movq %rcx, (%rax)
movl $0x2, 0x8(%rax)
movl 0x4f140(%rip), %r13d    # 0x90300
leal 0x1(%r13), %ecx
movl %ecx, 0x4f136(%rip)     # 0x90300
movl %r13d, %ebp
negl %ebp
cmovsl %r13d, %ebp
movl $0x1, %r12d
cmpl $0xa, %ebp
jb 0x412a1
movl $0x4, %r12d
movl $0xd1b71759, %esi       # imm = 0xD1B71759
movl %ebp, %ecx
cmpl $0x63, %ecx
jbe 0x41298
cmpl $0x3e7, %ecx            # imm = 0x3E7
jbe 0x4129e
cmpl $0x2710, %ecx           # imm = 0x2710
jb 0x412a1
movl %ecx, %edx
imulq %rsi, %rdx
shrq $0x2d, %rdx
addl $0x4, %r12d
cmpl $0x1869f, %ecx          # imm = 0x1869F
movl %edx, %ecx
ja 0x411ef
addl $-0x3, %r12d
jmp 0x412a1
xorl %ebx, %ebx
testq %rbx, %rbx
jne 0x41388
testl %ebp, %ebp
jne 0x41468
movl $0x48, %edi
callq 0x8290
movq %rax, %rbx
movl $0x10, %edi
callq 0x8290
leaq 0x4e661(%rip), %rcx     # 0x8f8c0
movq %rcx, (%rax)
movl $0x2, 0x8(%rax)
movq %rsp, %rdx
movq %rbx, %rdi
movq %rax, %rsi
callq 0x34990
leaq 0x4e30a(%rip), %rax     # 0x8f588
addq $0x10, %rax
movq %rax, (%rbx)
movq %rsp, %rsi
movq %r15, %rdi
callq 0x46b20
movq %rbx, (%rax)
jmp 0x41388
addl $-0x2, %r12d
jmp 0x412a1
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 0xc65b
leaq 0x314db(%rip), %rcx     # 0x727bd
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 0x4131e
movq %rdx, 0x48(%rsp)
movq (%rcx), %rdx
movq %rdx, 0x58(%rsp)
jmp 0x41326
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 0x44600
movq 0x48(%rsp), %rdi
cmpq %r12, %rdi
je 0x4136c
movq 0x58(%rsp), %rsi
incq %rsi
callq 0x82a0
movq 0x20(%rsp), %rdi
leaq 0x30(%rsp), %rax
cmpq %rax, %rdi
je 0x41388
movq 0x30(%rsp), %rsi
incq %rsi
callq 0x82a0
leaq 0x10(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x413a3
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x82a0
movq %rbx, %rax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x4ec34(%rip), %rdi     # 0x8fff0
leaq 0x2dda5(%rip), %rsi     # 0x6f168
movl $0x8, %edx
callq 0x8330
movq 0x4ec1c(%rip), %rdi     # 0x8fff0
leaq 0x313c9(%rip), %rsi     # 0x727a4
movl $0x18, %edx
callq 0x8330
movq 0x4ec04(%rip), %rdi     # 0x8fff0
leaq 0x2efcd(%rip), %rsi     # 0x703c0
movl $0x1, %edx
callq 0x8330
movq 0x4ebec(%rip), %rdi     # 0x8fff0
movl $0x45, %esi
callq 0x8520
movq %rax, %rdi
callq 0x8180
jmp 0x4145e
movq 0x4ebd1(%rip), %rdi     # 0x8fff0
leaq 0x2dd42(%rip), %rsi     # 0x6f168
callq 0x8280
leaq 0x31372(%rip), %rsi     # 0x727a4
movq %rax, %rdi
callq 0x8280
leaq 0x2ef7f(%rip), %rsi     # 0x703c0
movq %rax, %rdi
callq 0x8280
movq %rax, %rdi
movl $0x3f, %esi
callq 0x8520
movq %rax, %rdi
callq 0x8180
movl $0xff, %edi
callq 0x8380
movq 0x4eb81(%rip), %rdi     # 0x8fff0
leaq 0x2dcf2(%rip), %rsi     # 0x6f168
movl $0x8, %edx
callq 0x8330
movq 0x4eb69(%rip), %rdi     # 0x8fff0
leaq 0x31316(%rip), %rsi     # 0x727a4
movl $0x18, %edx
callq 0x8330
movq 0x4eb51(%rip), %rdi     # 0x8fff0
leaq 0x2ef1a(%rip), %rsi     # 0x703c0
movl $0x1, %edx
callq 0x8330
movq 0x4eb39(%rip), %rdi     # 0x8fff0
movl $0x58, %esi
callq 0x8520
movq %rax, %rdi
callq 0x8180
jmp 0x4145e
movq %rax, %r14
movq 0x48(%rsp), %rdi
cmpq %r12, %rdi
je 0x414ea
movq 0x58(%rsp), %rsi
incq %rsi
callq 0x82a0
jmp 0x414ea
movq %rax, %r14
movq 0x20(%rsp), %rdi
leaq 0x30(%rsp), %rax
cmpq %rax, %rdi
je 0x4150d
movq 0x30(%rsp), %rsi
incq %rsi
callq 0x82a0
jmp 0x4150d
jmp 0x4150a
movq %rax, %r14
movl $0x68, %esi
jmp 0x4151c
movq %rax, %r14
movl $0x48, %esi
movq %rbx, %rdi
callq 0x82a0
jmp 0x41538
movq %rax, %r14
movl $0x50, %esi
movq %rbx, %rdi
jmp 0x4154e
jmp 0x41535
movq %rax, %r14
leaq 0x10(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x41553
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x82a0
movq %r14, %rdi
callq 0x8540