// File: 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:
subq $0x398, %rsp            # imm = 0x398
movq %rdi, 0x390(%rsp)
movq %rsi, 0x388(%rsp)
movq %rdx, 0x380(%rsp)
movq 0x390(%rsp), %rax
movq %rax, 0x78(%rsp)
movq 0x388(%rsp), %rax
movslq (%rax), %rsi
leaq 0x12689a(%rip), %rdi    # 0x14c628
callq 0x183c0
movq %rax, %rcx
movq 0x78(%rsp), %rax
movq %rcx, 0x378(%rsp)
movq 0x378(%rsp), %rcx
movq %rcx, 0x370(%rsp)
movq 0x370(%rsp), %rdi
movq 0x10(%rax), %rax
movq 0x10(%rax), %rsi
callq 0x1f270
movq %rax, 0x360(%rsp)
leaq 0x360(%rsp), %rax
movq %rax, 0x368(%rsp)
movq 0x368(%rsp), %rax
movq %rax, 0x80(%rsp)
movq 0x370(%rsp), %rdi
callq 0x109d0
movq 0x80(%rsp), %rdi
movq %rax, 0x358(%rsp)
leaq 0x358(%rsp), %rsi
callq 0x36e80
testb $0x1, %al
jne 0x25e23
jmp 0x262a0
movq 0x78(%rsp), %rax
testb $0x1, 0xc(%rax)
je 0x25f20
movq 0x78(%rsp), %rax
movq 0x370(%rsp), %rcx
movq %rcx, 0x60(%rsp)
movq 0x10(%rax), %rax
movq 0x10(%rax), %rax
movq %rax, 0x68(%rsp)
leaq 0x298(%rsp), %rdi
movq %rdi, 0x70(%rsp)
xorl %esi, %esi
movl $0x1, %edx
callq 0x366c0
movq 0x68(%rsp), %rsi
movq 0x70(%rsp), %rdx
leaq 0x2e8(%rsp), %rdi
callq 0x36f40
jmp 0x25e83
movq 0x60(%rsp), %rdi
leaq 0x2e8(%rsp), %rsi
callq 0x36670
movb %dl, 0x57(%rsp)
movq %rax, 0x58(%rsp)
jmp 0x25ea0
movb 0x57(%rsp), %al
movq 0x58(%rsp), %rcx
movq %rcx, 0x278(%rsp)
movb %al, 0x280(%rsp)
leaq 0x2e8(%rsp), %rdi
callq 0x36810
leaq 0x298(%rsp), %rdi
callq 0x36840
jmp 0x26008
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x290(%rsp)
movl %eax, 0x28c(%rsp)
jmp 0x25f0e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x290(%rsp)
movl %eax, 0x28c(%rsp)
leaq 0x2e8(%rsp), %rdi
callq 0x36810
leaq 0x298(%rsp), %rdi
callq 0x36840
jmp 0x26317
movq 0x78(%rsp), %rax
movq 0x370(%rsp), %rcx
movq %rcx, 0x38(%rsp)
movq 0x10(%rax), %rax
movq 0x10(%rax), %rax
movq %rax, 0x40(%rsp)
leaq 0x1b8(%rsp), %rdi
movq %rdi, 0x48(%rsp)
xorl %edx, %edx
movl %edx, %esi
callq 0x366c0
movq 0x40(%rsp), %rsi
movq 0x48(%rsp), %rdx
leaq 0x208(%rsp), %rdi
callq 0x36f40
jmp 0x25f6e
movq 0x38(%rsp), %rdi
leaq 0x208(%rsp), %rsi
callq 0x36670
movb %dl, 0x2f(%rsp)
movq %rax, 0x30(%rsp)
jmp 0x25f8b
movb 0x2f(%rsp), %al
movq 0x30(%rsp), %rcx
movq %rcx, 0x1a8(%rsp)
movb %al, 0x1b0(%rsp)
leaq 0x208(%rsp), %rdi
callq 0x36810
leaq 0x1b8(%rsp), %rdi
callq 0x36840
jmp 0x26008
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x290(%rsp)
movl %eax, 0x28c(%rsp)
jmp 0x25ff6
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x290(%rsp)
movl %eax, 0x28c(%rsp)
leaq 0x208(%rsp), %rdi
callq 0x36810
leaq 0x1b8(%rsp), %rdi
callq 0x36840
jmp 0x26317
movq 0x78(%rsp), %rax
movq 0x370(%rsp), %rdi
movq 0x10(%rax), %rax
movq 0x10(%rax), %rsi
callq 0x1f270
movq %rax, 0x198(%rsp)
leaq 0x198(%rsp), %rdi
callq 0x36f80
addq $0x20, %rax
movq %rax, 0x1a0(%rsp)
movq 0x388(%rsp), %rax
cmpl $0x0, (%rax)
je 0x2605b
movq 0x78(%rsp), %rax
testb $0x1, 0xc(%rax)
je 0x260a0
movq 0x78(%rsp), %rax
movq 0x18(%rax), %rdi
movq 0x388(%rsp), %rsi
movq 0x380(%rsp), %rdx
movq (%rdi), %rax
callq *0x18(%rax)
movq 0x1a0(%rsp), %rdi
addq $0x38, %rdi
movq 0x388(%rsp), %rsi
addq $0x8, %rsi
addq $0x4, %rsi
callq 0x1f2a0
jmp 0x2629e
movq 0x78(%rsp), %rax
movq 0x18(%rax), %rdi
movq 0x388(%rsp), %rsi
movq 0x380(%rsp), %rdx
movq (%rdi), %rax
movq 0x10(%rax), %rax
callq *%rax
movq 0x78(%rsp), %rax
movq 0x10(%rax), %rax
movq 0x10(%rax), %rsi
leaq 0x138(%rsp), %rdi
movq %rdi, 0x20(%rsp)
callq 0x81a0
movq 0x20(%rsp), %rdx
movq 0x388(%rsp), %rax
movl (%rax), %ecx
leaq 0x158(%rsp), %rdi
xorl %esi, %esi
callq 0x36480
jmp 0x26101
leaq 0x138(%rsp), %rdi
callq 0x8728
movq 0x380(%rsp), %rax
movq %rax, 0x10(%rsp)
movl $0xe0, %edi
callq 0x8340
movq %rax, 0x18(%rsp)
jmp 0x2612c
movb $0x1, 0xaf(%rsp)
movq 0x388(%rsp), %rsi
addq $0x8, %rsi
leaq 0xf0(%rsp), %rdi
callq 0x43130
jmp 0x2614f
leaq 0xb0(%rsp), %rdi
leaq 0x158(%rsp), %rsi
callq 0x43130
jmp 0x26166
movq 0x18(%rsp), %rdi
movl $0xc, %esi
leaq 0xf0(%rsp), %rdx
leaq 0xb0(%rsp), %rcx
callq 0x36880
jmp 0x26187
movq 0x10(%rsp), %rdi
movq 0x18(%rsp), %rax
movb $0x0, 0xaf(%rsp)
movq %rax, 0x130(%rsp)
leaq 0x130(%rsp), %rsi
callq 0x185b0
jmp 0x261b0
leaq 0xb0(%rsp), %rdi
callq 0x1f210
leaq 0xf0(%rsp), %rdi
callq 0x1f210
leaq 0x158(%rsp), %rdi
callq 0x1f210
jmp 0x2629e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x290(%rsp)
movl %eax, 0x28c(%rsp)
leaq 0x138(%rsp), %rdi
callq 0x8728
jmp 0x26317
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x290(%rsp)
movl %eax, 0x28c(%rsp)
jmp 0x2628f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x290(%rsp)
movl %eax, 0x28c(%rsp)
jmp 0x26272
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x290(%rsp)
movl %eax, 0x28c(%rsp)
jmp 0x26265
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x290(%rsp)
movl %eax, 0x28c(%rsp)
leaq 0xb0(%rsp), %rdi
callq 0x1f210
leaq 0xf0(%rsp), %rdi
callq 0x1f210
testb $0x1, 0xaf(%rsp)
jne 0x2627e
jmp 0x2628d
movq 0x18(%rsp), %rdi
movl $0xe0, %esi
callq 0x8350
jmp 0x2628f
leaq 0x158(%rsp), %rdi
callq 0x1f210
jmp 0x26317
jmp 0x2630f
movq 0x78(%rsp), %rax
movl 0x8(%rax), %ecx
movl %ecx, 0x4(%rsp)
movq 0x10(%rax), %rax
movq 0x10(%rax), %rsi
leaq 0xd48cf(%rip), %rdx     # 0xfab8a
leaq 0x88(%rsp), %rdi
movq %rdi, 0x8(%rsp)
callq 0xc720
movl 0x4(%rsp), %edi
movq 0x8(%rsp), %rsi
callq 0x47ce0
jmp 0x262dd
leaq 0x88(%rsp), %rdi
callq 0x8728
jmp 0x2630f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x290(%rsp)
movl %eax, 0x28c(%rsp)
leaq 0x88(%rsp), %rdi
callq 0x8728
jmp 0x26317
addq $0x398, %rsp            # imm = 0x398
retq
movq 0x290(%rsp), %rdi
callq 0x8670
nopw %cs:(%rax,%rax)