// File: /workspace/github_cpp_makefile_star2_O2/luooofan[P]hblzbd_compiler/src/generate_ir.cpp
// Function: ast::BinaryExpression::GenerateIR(ir::ContextInfo&, std::vector<ir::IR*, std::allocator<ir::IR*>>&)
void BinaryExpression::GenerateIR(ir::ContextInfo &ctx, std::vector<ir::IR *> &gIRList) {
  ir::Opn opn1, opn2;
  IROpKind op;

  auto number = dynamic_cast<Number *>(lhs_.get());
  auto number2 = dynamic_cast<Number *>(&rhs_);
  if ((nullptr != number && number->value_ == 0) || (nullptr != number2 && number2->value_ == 0)) {
    switch (op_) {
      case MUL:
      case DIV:  // 实际上0不能作为div和mod的rhs
      case MOD:
        ctx.opn_ = ir::Opn(OpnType::Imm, 0);
        ctx.shape_.clear();
        return;
    }
  }

  lhs_->GenerateIR(ctx, gIRList);
  opn1 = ctx.opn_;
  CHECK_OPN_INT("lhs exp");

  rhs_.GenerateIR(ctx, gIRList);
  opn2 = ctx.opn_;
  CHECK_OPN_INT("rhs exp");

  if (opn1.type_ == OpnType::Imm && opn2.type_ == OpnType::Imm) {
    int result;
    int num1 = opn1.imm_num_, num2 = opn2.imm_num_;
    switch (op_) {
      case ADD:
        result = num1 + num2;
        break;
      case SUB:
        result = num1 - num2;
        break;
      case MUL:
        result = num1 * num2;
        break;
      case DIV:
        result = num1 / num2;
        break;
      case MOD:
        result = num1 % num2;
        break;
      default:
        // printf("mystery operator code: %d", op_);
        MyAssert(0);
        break;
    }
    ctx.opn_ = ir::Opn(OpnType::Imm, result);
  } else {
    switch (op_) {
      case ADD:
        op = IROpKind::ADD;
        break;
      case SUB:
        op = IROpKind::SUB;
        break;
      case MUL:
        op = IROpKind::MUL;
        break;
      case DIV:
        op = IROpKind::DIV;
        break;
      case MOD:
        op = IROpKind::MOD;
        break;
      default:
        // printf("mystery operator code: %d", op_);
        MyAssert(0);
        break;
    }
    const std::string &res_temp_var = ir::NewTemp();
    // NOTE: 中间变量栈偏移-1 并且不维护scope的dynamic offset
    ir::gScopes[ctx.scope_id_].symbol_table_.insert({res_temp_var, {false, false}});

    ir::Opn temp = ir::Opn(OpnType::Var, res_temp_var, ctx.scope_id_);
    ctx.opn_ = temp;
    gIRList.push_back(new ir::IR(op, opn1, opn2, temp));
  }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x248, %rsp            # imm = 0x248
movq %rdx, %rbx
movq %rsi, %r14
movq %rdi, %r12
leaq 0x148(%rsp), %rdi
callq 0x1aac2
leaq 0x108(%rsp), %rdi
callq 0x1aac2
movq 0x10(%r12), %r15
testq %r15, %r15
je 0x10a61
leaq 0x6f40d(%rip), %rsi     # 0x7fe58
leaq 0x6f0fe(%rip), %rdx     # 0x7fb50
movq %r15, %rdi
xorl %ecx, %ecx
callq 0x82c0
movq %rax, %r13
jmp 0x10a64
xorl %r13d, %r13d
movq 0x20(%r12), %rdi
testq %rdi, %rdi
je 0x10a85
leaq 0x6f3e3(%rip), %rsi     # 0x7fe58
leaq 0x6f0d4(%rip), %rdx     # 0x7fb50
xorl %ecx, %ecx
callq 0x82c0
jmp 0x10a87
xorl %eax, %eax
testq %r13, %r13
je 0x10a93
cmpl $0x0, 0xc(%r13)
je 0x10a9e
testq %rax, %rax
je 0x10aeb
cmpl $0x0, 0xc(%rax)
jne 0x10aeb
movl $0xfffffefb, %eax       # imm = 0xFFFFFEFB
addl 0xc(%r12), %eax
cmpl $0x2, %eax
ja 0x10aeb
leaq 0x28(%rsp), %rdi
pushq $0x1
popq %rsi
xorl %edx, %edx
callq 0xf6ba
leaq 0x8(%r14), %rdi
leaq 0x28(%rsp), %rsi
callq 0x1db84
leaq 0x30(%rsp), %rdi
callq 0x8578
movq 0x50(%r14), %rax
cmpq %rax, 0x58(%r14)
je 0x10e63
movq %rax, 0x58(%r14)
jmp 0x10e63
movq (%r15), %rax
movq %r15, %rdi
movq %r14, %rsi
movq %rbx, %rdx
callq *0x10(%rax)
leaq 0x8(%r14), %r15
leaq 0x148(%rsp), %rdi
movq %r15, %rsi
callq 0x1daba
movq 0x50(%r14), %rax
cmpq 0x58(%r14), %rax
je 0x10b9c
cmpl $0x4, 0x8(%r14)
jne 0x10b9c
movl 0x8(%r12), %ebp
leaq 0x10(%r14), %rsi
leaq 0x4671d(%rip), %rdx     # 0x57250
leaq 0x8(%rsp), %rdi
callq 0xa334
leaq 0x462f0(%rip), %rdx     # 0x56e34
leaq 0x98(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0xa440
leaq 0x45f88(%rip), %rdx     # 0x56ae5
leaq 0x28(%rsp), %rdi
leaq 0x98(%rsp), %rsi
callq 0xa440
leaq 0x28(%rsp), %rsi
movl %ebp, %edi
callq 0x1f699
leaq 0x28(%rsp), %rdi
callq 0x8578
leaq 0x98(%rsp), %rdi
callq 0x8578
leaq 0x8(%rsp), %rdi
callq 0x8578
movq 0x20(%r12), %rdi
movq (%rdi), %rax
movq %r14, %rsi
movq %rbx, %rdx
callq *0x10(%rax)
leaq 0x108(%rsp), %rdi
movq %r15, %rsi
callq 0x1daba
movq 0x50(%r14), %rax
cmpq 0x58(%r14), %rax
je 0x10c4b
cmpl $0x4, 0x8(%r14)
jne 0x10c4b
movl 0x8(%r12), %ebp
leaq 0x10(%r14), %rsi
leaq 0x4666e(%rip), %rdx     # 0x57250
leaq 0x8(%rsp), %rdi
callq 0xa334
leaq 0x46186(%rip), %rdx     # 0x56d79
leaq 0x98(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0xa440
leaq 0x45ed9(%rip), %rdx     # 0x56ae5
leaq 0x28(%rsp), %rdi
leaq 0x98(%rsp), %rsi
callq 0xa440
leaq 0x28(%rsp), %rsi
movl %ebp, %edi
callq 0x1f699
leaq 0x28(%rsp), %rdi
callq 0x8578
leaq 0x98(%rsp), %rdi
callq 0x8578
leaq 0x8(%rsp), %rdi
callq 0x8578
cmpl $0x1, 0x148(%rsp)
jne 0x10c97
cmpl $0x1, 0x108(%rsp)
jne 0x10c97
movl $0xfffffefe, %edx       # imm = 0xFFFFFEFE
addl 0xc(%r12), %edx
cmpl $0x5, %edx
ja 0x10edf
movl 0x14c(%rsp), %eax
movl 0x10c(%rsp), %ecx
leaq 0x45da9(%rip), %rsi     # 0x56a30
movslq (%rsi,%rdx,4), %rdx
addq %rsi, %rdx
jmpq *%rdx
addl %eax, %ecx
jmp 0x10e3d
movl $0xfffffefe, %eax       # imm = 0xFFFFFEFE
addl 0xc(%r12), %eax
cmpl $0x6, %eax
jae 0x10e8f
pushq $0x3b
popq %rcx
btl %eax, %ecx
jae 0x10e8f
movl %eax, %eax
leaq 0x45de9(%rip), %rcx     # 0x56aa8
movl (%rcx,%rax,4), %ebp
leaq 0x8(%rsp), %rdi
callq 0x1d95d
movslq (%r14), %r12
leaq 0x70872(%rip), %rax     # 0x81548
shlq $0x6, %r12
addq (%rax), %r12
leaq 0x98(%rsp), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x1a960
leaq 0x28(%rsp), %rdi
leaq 0x8(%rsp), %rsi
leaq 0x98(%rsp), %rdx
callq 0x1aaf8
leaq 0x28(%rsp), %rsi
movq %r12, %rdi
callq 0x1c154
leaq 0x28(%rsp), %rdi
callq 0x1aa18
leaq 0x98(%rsp), %rdi
callq 0x1aa2e
leaq 0xe8(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x8390
movl (%r14), %ecx
leaq 0x28(%rsp), %rdi
leaq 0xe8(%rsp), %rdx
xorl %esi, %esi
callq 0x1a8d4
leaq 0xe8(%rsp), %rdi
callq 0x8578
leaq 0x28(%rsp), %rsi
movq %r15, %rdi
callq 0x1daba
movl $0xe0, %edi
callq 0x8250
movq %rax, %r14
leaq 0x208(%rsp), %rdi
leaq 0x148(%rsp), %rsi
callq 0x1da22
leaq 0x1c8(%rsp), %rdi
leaq 0x108(%rsp), %rsi
callq 0x1da22
leaq 0x188(%rsp), %rdi
leaq 0x28(%rsp), %rsi
callq 0x1da22
movb $0x1, %r15b
leaq 0x208(%rsp), %rdx
leaq 0x1c8(%rsp), %rcx
leaq 0x188(%rsp), %r8
movq %r14, %rdi
movl %ebp, %esi
callq 0x1ab2e
leaq 0x98(%rsp), %rsi
movq %r14, (%rsi)
xorl %r15d, %r15d
movq %rbx, %rdi
callq 0xe358
leaq 0x190(%rsp), %rdi
callq 0x8578
leaq 0x1d0(%rsp), %rdi
callq 0x8578
leaq 0x210(%rsp), %rdi
callq 0x8578
leaq 0x30(%rsp), %rdi
callq 0x8578
leaq 0x8(%rsp), %rdi
jmp 0x10e5e
cltd
idivl %ecx
movl %edx, %ecx
jmp 0x10e3d
imull %eax, %ecx
jmp 0x10e3d
cltd
idivl %ecx
jmp 0x10e3b
subl %ecx, %eax
movl %eax, %ecx
leaq 0x28(%rsp), %rdi
pushq $0x1
popq %rsi
movl %ecx, %edx
callq 0xf6ba
leaq 0x28(%rsp), %rsi
movq %r15, %rdi
callq 0x1db84
leaq 0x30(%rsp), %rdi
callq 0x8578
leaq 0x110(%rsp), %rdi
callq 0x8578
leaq 0x150(%rsp), %rdi
callq 0x8578
addq $0x248, %rsp            # imm = 0x248
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x70152(%rip), %rdi     # 0x80fe8
leaq 0x4524b(%rip), %rsi     # 0x560e8
callq 0x8240
leaq 0x45c4a(%rip), %rsi     # 0x56af3
movq %rax, %rdi
callq 0x8240
leaq 0x46468(%rip), %rsi     # 0x57320
movq %rax, %rdi
callq 0x8240
movq %rax, %rdi
movl $0x11a, %esi            # imm = 0x11A
callq 0x84b0
movq %rax, %rdi
callq 0x8170
movl $0xff, %edi
callq 0x8300
movq 0x70102(%rip), %rdi     # 0x80fe8
leaq 0x451fb(%rip), %rsi     # 0x560e8
callq 0x8240
leaq 0x45bfa(%rip), %rsi     # 0x56af3
movq %rax, %rdi
callq 0x8240
leaq 0x46418(%rip), %rsi     # 0x57320
movq %rax, %rdi
callq 0x8240
movq %rax, %rdi
movl $0x103, %esi            # imm = 0x103
callq 0x84b0
movq %rax, %rdi
callq 0x8170
jmp 0x10ed5
jmp 0x11029
jmp 0x10f98
jmp 0x11029
movq %rax, %rbx
movb $0x1, %r15b
jmp 0x10fe2
movq %rax, %rbx
movb $0x1, %r15b
jmp 0x10fef
movq %rax, %rbx
jmp 0x11001
jmp 0x10f53
movq %rax, %rbx
jmp 0x1100e
movq %rax, %rbx
leaq 0xe8(%rsp), %rdi
jmp 0x11013
jmp 0x10fcb
movq %rax, %rbx
leaq 0x28(%rsp), %rdi
callq 0x1aa18
jmp 0x10f7f
movq %rax, %rbx
leaq 0x98(%rsp), %rdi
callq 0x1aa2e
jmp 0x11018
jmp 0x10fcb
jmp 0x11029
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
jmp 0x1101d
jmp 0x10faf
jmp 0x11029
jmp 0x10fbe
jmp 0x10fcb
jmp 0x11029
movq %rax, %rbx
leaq 0x28(%rsp), %rdi
callq 0x8578
jmp 0x10fc1
movq %rax, %rbx
leaq 0x98(%rsp), %rdi
jmp 0x11013
movq %rax, %rbx
jmp 0x11018
jmp 0x11029
movq %rax, %rbx
leaq 0x190(%rsp), %rdi
callq 0x8578
leaq 0x1d0(%rsp), %rdi
callq 0x8578
leaq 0x210(%rsp), %rdi
callq 0x8578
testb %r15b, %r15b
je 0x1100e
movl $0xe0, %esi
movq %r14, %rdi
callq 0x8260
leaq 0x30(%rsp), %rdi
callq 0x8578
leaq 0x8(%rsp), %rdi
callq 0x8578
jmp 0x1102c
movq %rax, %rbx
jmp 0x11039
movq %rax, %rbx
leaq 0x110(%rsp), %rdi
callq 0x8578
leaq 0x150(%rsp), %rdi
callq 0x8578
movq %rbx, %rdi
callq 0x84d0