// File: /workspace/github_cpp_makefile_star2_O2/luooofan[P]hblzbd_compiler/src/generate_ir.cpp
// Function: ast::VariableDefineWithInit::GenerateIR(ir::ContextInfo&, std::vector<ir::IR*, std::allocator<ir::IR*>>&)
void VariableDefineWithInit::GenerateIR(ir::ContextInfo &ctx, std::vector<ir::IR *> &gIRList) {
  auto &scope = ir::gScopes[ctx.scope_id_];
  auto &symbol_table = scope.symbol_table_;
  const auto &var_iter = symbol_table.find(this->name_.name_);
  if (var_iter == symbol_table.end()) {
    if (this->is_const_) {
      symbol_table.insert({this->name_.name_, ir::SymbolTableItem(false, true)});
    } else {
      symbol_table.insert({this->name_.name_, ir::SymbolTableItem(false, false)});
    }
    auto &tmp = symbol_table.find(this->name_.name_)->second;
    // NOTE: 如果是全局量或者常量 需要填写initval 如果是局部变量 生成一条赋值语句
    if (ctx.scope_id_ == 0 || this->is_const_) {
      value_.Evaluate(ctx, gIRList);
      tmp.initval_.push_back(ctx.opn_.imm_num_);
    } else {
      value_.GenerateIR(ctx, gIRList);
      ir::Opn lhs_opn = ir::Opn(OpnType::Var, name_.name_, ctx.scope_id_);
      // GenIR DECLARE
      // gIRList.push_back(new ir::IR(IROpKind::ALLOCA, lhs_opn, ir::Opn{OpnType::Imm, ir::kIntWidth}));
      gIRList.push_back(new ir::IR(IROpKind::ASSIGN, ctx.opn_, lhs_opn));
    }
  } else {
    ir::SemanticError(this->line_no_, this->name_.name_ + ": variable redefined");
  }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x168, %rsp            # imm = 0x168
movq %rdx, %rbx
movq %rsi, %r14
movq %rdi, %r15
movslq (%rsi), %r12
leaq 0x6f3f8(%rip), %rax     # 0x81548
shlq $0x6, %r12
addq (%rax), %r12
movq 0x10(%rdi), %rax
movq 0x10(%rax), %rsi
movq %r12, %rdi
callq 0xf75e
testq %rax, %rax
je 0x121a5
movl 0x8(%r15), %ebx
movq 0x10(%r15), %rax
movq 0x10(%rax), %rsi
leaq 0x449f4(%rip), %rdx     # 0x56b73
leaq 0x8(%rsp), %r14
movq %r14, %rdi
callq 0xa334
movl %ebx, %edi
movq %r14, %rsi
callq 0x1f699
leaq 0x8(%rsp), %rdi
callq 0x8578
jmp 0x12272
cmpb $0x1, 0xc(%r15)
jne 0x121e5
movq 0x10(%r15), %rax
movq 0x10(%rax), %r13
leaq 0x78(%rsp), %rbp
pushq $0x1
popq %rdx
movq %rbp, %rdi
xorl %esi, %esi
callq 0x1a960
leaq 0x8(%rsp), %rdi
movq %r13, %rsi
movq %rbp, %rdx
callq 0x1acd0
leaq 0x8(%rsp), %rsi
movq %r12, %rdi
callq 0x1c154
jmp 0x1221b
movq 0x10(%r15), %rax
movq 0x10(%rax), %r13
leaq 0x78(%rsp), %rbp
movq %rbp, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x1a960
leaq 0x8(%rsp), %rdi
movq %r13, %rsi
movq %rbp, %rdx
callq 0x1acd0
leaq 0x8(%rsp), %rsi
movq %r12, %rdi
callq 0x1c154
leaq 0x8(%rsp), %rdi
callq 0x1aa18
leaq 0x78(%rsp), %rdi
callq 0x1aa2e
movq 0x10(%r15), %rax
movq 0x10(%rax), %rsi
movq %r12, %rdi
callq 0xf75e
movq %rax, %r12
cmpl $0x0, (%r14)
je 0x1224f
cmpb $0x1, 0xc(%r15)
jne 0x12284
movq 0x18(%r15), %rdi
movq (%rdi), %rax
movq %r14, %rsi
movq %rbx, %rdx
callq *0x18(%rax)
addq $0x60, %r12
addq $0xc, %r14
movq %r12, %rdi
movq %r14, %rsi
callq 0xf720
addq $0x168, %rsp            # imm = 0x168
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x18(%r15), %rdi
movq (%rdi), %rax
movq %r14, %rsi
movq %rbx, %rdx
callq *0x10(%rax)
movq 0x10(%r15), %rax
movq 0x10(%rax), %rsi
leaq 0x148(%rsp), %r15
movq %r15, %rdi
callq 0x8390
movl (%r14), %ecx
leaq 0x8(%rsp), %rdi
xorl %esi, %esi
movq %r15, %rdx
callq 0x1a8d4
leaq 0x148(%rsp), %rdi
callq 0x8578
movl $0xe0, %edi
callq 0x8250
movq %rax, %r15
addq $0x8, %r14
leaq 0x108(%rsp), %rdi
movq %r14, %rsi
callq 0x1da22
leaq 0xc8(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x1da22
movb $0x1, %bpl
pushq $0xc
popq %rsi
leaq 0x108(%rsp), %rdx
leaq 0xc8(%rsp), %rcx
movq %r15, %rdi
callq 0x1aa52
leaq 0x78(%rsp), %rsi
movq %r15, (%rsi)
xorl %ebp, %ebp
movq %rbx, %rdi
callq 0xe358
leaq 0xd0(%rsp), %rdi
callq 0x8578
leaq 0x110(%rsp), %rdi
callq 0x8578
leaq 0x10(%rsp), %rdi
jmp 0x1219b
movq %rax, %rbx
movb $0x1, %bpl
jmp 0x1239b
movq %rax, %rbx
jmp 0x123ad
movq %rax, %rbx
jmp 0x123ba
movq %rax, %rbx
jmp 0x123bf
jmp 0x1236b
movq %rax, %rbx
leaq 0x8(%rsp), %rdi
callq 0x1aa18
jmp 0x1237f
jmp 0x1237c
movq %rax, %rbx
leaq 0x78(%rsp), %rdi
callq 0x1aa2e
jmp 0x123d1
movq %rax, %rbx
leaq 0xd0(%rsp), %rdi
callq 0x8578
leaq 0x110(%rsp), %rdi
callq 0x8578
testb %bpl, %bpl
je 0x123ba
movl $0xe0, %esi
movq %r15, %rdi
callq 0x8260
leaq 0x10(%rsp), %r15
movq %r15, %rdi
jmp 0x123cc
movq %rax, %rbx
leaq 0x8(%rsp), %rdi
callq 0x8578
movq %rbx, %rdi
callq 0x84d0
nop