// File: /workspace/github_cpp_makefile_star2_O1/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
movl $0x4, %eax
leaq 0x160(%rsp), %rdi
movl %eax, -0x18(%rdi)
movq %rdi, -0x10(%rdi)
movw $0x2d, %cx
movw %cx, (%rdi)
movl $0x1, %edx
movq %rdx, -0x8(%rdi)
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
movl %esi, 0x10(%rdi)
xorl %r13d, %r13d
movq %r13, 0x18(%rdi)
movl %esi, 0x20(%rdi)
leaq 0x120(%rsp), %rdi
movl %eax, -0x18(%rdi)
movq %rdi, -0x10(%rdi)
movw %cx, (%rdi)
movq %rdx, -0x8(%rdi)
movl %esi, 0x10(%rdi)
movq %r13, 0x18(%rdi)
movl %esi, 0x20(%rdi)
movq 0x10(%r12), %r15
testq %r15, %r15
je 0x1492f
leaq 0x7a50d(%rip), %rsi     # 0x8ee28
leaq 0x7a1fe(%rip), %rdx     # 0x8eb20
movq %r15, %rdi
xorl %ecx, %ecx
callq 0x8320
movq %rax, %r13
movq 0x20(%r12), %rdi
testq %rdi, %rdi
je 0x14950
leaq 0x7a4e8(%rip), %rsi     # 0x8ee28
leaq 0x7a1d9(%rip), %rdx     # 0x8eb20
xorl %ecx, %ecx
callq 0x8320
jmp 0x14952
xorl %eax, %eax
testq %r13, %r13
je 0x1495e
cmpl $0x0, 0xc(%r13)
je 0x14969
testq %rax, %rax
je 0x149c9
cmpl $0x0, 0xc(%rax)
jne 0x149c9
movl $0xfffffefb, %eax       # imm = 0xFFFFFEFB
addl 0xc(%r12), %eax
cmpl $0x2, %eax
ja 0x149c9
leaq 0x28(%rsp), %rdi
movl $0x1, %esi
xorl %edx, %edx
callq 0x12ade
leaq 0x8(%r14), %rdi
leaq 0x28(%rsp), %rsi
callq 0x28312
leaq 0x40(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x149b2
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x82a0
movq 0x50(%r14), %rax
cmpq %rax, 0x58(%r14)
je 0x1505c
movq %rax, 0x58(%r14)
jmp 0x1505c
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 0x2822e
movq 0x50(%r14), %rax
cmpq 0x58(%r14), %rax
movq %r15, 0x20(%rsp)
je 0x14b56
cmpl $0x4, 0x8(%r14)
jne 0x14b56
movl 0x8(%r12), %ebp
leaq 0x10(%rsp), %r15
movq %r15, -0x10(%r15)
movq 0x10(%r14), %rsi
movq 0x18(%r14), %rdx
addq %rsi, %rdx
movq %rsp, %rdi
callq 0xca10
leaq 0x5b8be(%rip), %rsi     # 0x702f0
movq %rsp, %rdi
callq 0x8590
leaq 0x5b493(%rip), %rsi     # 0x6fed4
movq %rsp, %rdi
callq 0x8590
leaq 0xa8(%rsp), %r13
movq %r13, -0x10(%r13)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
je 0x14a79
movq %rdx, 0x98(%rsp)
movq (%rcx), %rdx
movq %rdx, 0xa8(%rsp)
jmp 0x14a81
movups (%rcx), %xmm0
movups %xmm0, (%r13)
movq %rbx, %r15
movq 0x8(%rax), %rdx
leaq 0x98(%rsp), %rdi
movq %rdx, 0x8(%rdi)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
leaq 0x5b0db(%rip), %rsi     # 0x6fb85
callq 0x8590
leaq 0x38(%rsp), %rbx
movq %rbx, -0x10(%rbx)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
je 0x14ad6
movq %rdx, 0x28(%rsp)
movq (%rcx), %rdx
movq %rdx, 0x38(%rsp)
jmp 0x14adc
movups (%rcx), %xmm0
movups %xmm0, (%rbx)
movq 0x8(%rax), %rdx
leaq 0x28(%rsp), %rsi
movq %rdx, 0x8(%rsi)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
movl %ebp, %edi
callq 0x2b12a
movq 0x28(%rsp), %rdi
cmpq %rbx, %rdi
je 0x14b16
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x82a0
movq 0x98(%rsp), %rdi
cmpq %r13, %rdi
movq %r15, %rbx
je 0x14b36
movq 0xa8(%rsp), %rsi
incq %rsi
callq 0x82a0
movq (%rsp), %rdi
leaq 0x10(%rsp), %rax
cmpq %rax, %rdi
movq 0x20(%rsp), %r15
je 0x14b56
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x82a0
movq 0x20(%r12), %rdi
movq (%rdi), %rax
movq %r14, %rsi
movq %rbx, %rdx
callq *0x10(%rax)
leaq 0x108(%rsp), %rdi
movq %r15, %rsi
callq 0x2822e
movq 0x50(%r14), %rax
cmpq 0x58(%r14), %rax
je 0x14cdc
cmpl $0x4, 0x8(%r14)
jne 0x14cdc
movl 0x8(%r12), %ebp
leaq 0x10(%rsp), %r15
movq %r15, -0x10(%r15)
movq 0x10(%r14), %rsi
movq 0x18(%r14), %rdx
addq %rsi, %rdx
movq %rsp, %rdi
callq 0xca10
leaq 0x5b738(%rip), %rsi     # 0x702f0
movq %rsp, %rdi
callq 0x8590
leaq 0x5b252(%rip), %rsi     # 0x6fe19
movq %rsp, %rdi
callq 0x8590
leaq 0xa8(%rsp), %r13
movq %r13, -0x10(%r13)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
je 0x14bff
movq %rdx, 0x98(%rsp)
movq (%rcx), %rdx
movq %rdx, 0xa8(%rsp)
jmp 0x14c07
movups (%rcx), %xmm0
movups %xmm0, (%r13)
movq %rbx, %r15
movq 0x8(%rax), %rdx
leaq 0x98(%rsp), %rdi
movq %rdx, 0x8(%rdi)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
leaq 0x5af55(%rip), %rsi     # 0x6fb85
callq 0x8590
leaq 0x38(%rsp), %rbx
movq %rbx, -0x10(%rbx)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
je 0x14c5c
movq %rdx, 0x28(%rsp)
movq (%rcx), %rdx
movq %rdx, 0x38(%rsp)
jmp 0x14c62
movups (%rcx), %xmm0
movups %xmm0, (%rbx)
movq 0x8(%rax), %rdx
leaq 0x28(%rsp), %rsi
movq %rdx, 0x8(%rsi)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
movl %ebp, %edi
callq 0x2b12a
movq 0x28(%rsp), %rdi
cmpq %rbx, %rdi
je 0x14c9c
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x82a0
movq 0x98(%rsp), %rdi
cmpq %r13, %rdi
movq %r15, %rbx
je 0x14cbc
movq 0xa8(%rsp), %rsi
incq %rsi
callq 0x82a0
movq (%rsp), %rdi
leaq 0x10(%rsp), %rax
cmpq %rax, %rdi
movq 0x20(%rsp), %r15
je 0x14cdc
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x82a0
cmpl $0x1, 0x148(%rsp)
jne 0x14d28
cmpl $0x1, 0x108(%rsp)
jne 0x14d28
movl $0xfffffefe, %edx       # imm = 0xFFFFFEFE
addl 0xc(%r12), %edx
cmpl $0x5, %edx
ja 0x15123
movl 0x14c(%rsp), %eax
movl 0x10c(%rsp), %ecx
leaq 0x5ada4(%rip), %rsi     # 0x6fabc
movslq (%rsi,%rdx,4), %rdx
addq %rsi, %rdx
jmpq *%rdx
addl %eax, %ecx
jmp 0x15023
movl $0xfffffefe, %eax       # imm = 0xFFFFFEFE
addl 0xc(%r12), %eax
cmpl $0x6, %eax
jae 0x150b8
movl $0x3b, %ecx
btl %eax, %ecx
jae 0x150b8
movl %eax, %eax
leaq 0x5ade2(%rip), %rcx     # 0x6fb34
movl (%rcx,%rax,4), %ebp
movq %rsp, %rdi
callq 0x27ed3
movslq (%r14), %r12
leaq 0x7b811(%rip), %rax     # 0x90578
shlq $0x6, %r12
addq (%rax), %r12
leaq 0x98(%rsp), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x24c9c
leaq 0x28(%rsp), %rdi
movq %rsp, %rsi
leaq 0x98(%rsp), %rdx
callq 0x24f32
leaq 0x28(%rsp), %rsi
movq %r12, %rdi
callq 0x26522
movq 0x80(%rsp), %rdi
testq %rdi, %rdi
je 0x14dbe
movq 0x90(%rsp), %rsi
subq %rdi, %rsi
callq 0x82a0
movq 0x68(%rsp), %rdi
testq %rdi, %rdi
je 0x14dd5
movq 0x78(%rsp), %rsi
subq %rdi, %rsi
callq 0x82a0
movq 0x50(%rsp), %rdi
testq %rdi, %rdi
je 0x14dec
movq 0x60(%rsp), %rsi
subq %rdi, %rsi
callq 0x82a0
leaq 0x38(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x14e07
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x82a0
movq 0xd0(%rsp), %rdi
testq %rdi, %rdi
je 0x14e24
movq 0xe0(%rsp), %rsi
subq %rdi, %rsi
callq 0x82a0
movq 0xb8(%rsp), %rdi
testq %rdi, %rdi
je 0x14e41
movq 0xc8(%rsp), %rsi
subq %rdi, %rsi
callq 0x82a0
movq 0xa0(%rsp), %rdi
testq %rdi, %rdi
je 0x14e5e
movq 0xb0(%rsp), %rsi
subq %rdi, %rsi
callq 0x82a0
movq %rbx, %r15
leaq 0xf8(%rsp), %r13
movq %r13, -0x10(%r13)
movq (%rsp), %rsi
movq 0x8(%rsp), %rdx
addq %rsi, %rdx
leaq 0xe8(%rsp), %rdi
callq 0xca10
movl (%r14), %ebx
leaq 0x30(%rsp), %rdi
movl $0x0, -0x8(%rdi)
leaq 0x40(%rsp), %r12
movq %r12, -0x10(%r12)
movq 0xe8(%rsp), %rsi
movq 0xf0(%rsp), %rdx
addq %rsi, %rdx
callq 0xca10
movl %ebx, 0x50(%rsp)
movq $0x0, 0x58(%rsp)
movl $0xffffffff, 0x60(%rsp) # imm = 0xFFFFFFFF
movq 0xe8(%rsp), %rdi
cmpq %r13, %rdi
je 0x14ee9
movq 0xf8(%rsp), %rsi
incq %rsi
callq 0x82a0
leaq 0x28(%rsp), %rsi
movq 0x20(%rsp), %rdi
callq 0x2822e
movq %r15, %rbx
movl $0xe0, %edi
callq 0x8290
movq %rax, %r14
leaq 0x208(%rsp), %rdi
leaq 0x148(%rsp), %rsi
callq 0x28186
leaq 0x1c8(%rsp), %rdi
leaq 0x108(%rsp), %rsi
callq 0x28186
leaq 0x188(%rsp), %rdi
leaq 0x28(%rsp), %rsi
callq 0x28186
movb $0x1, %r15b
leaq 0x208(%rsp), %rdx
leaq 0x1c8(%rsp), %rcx
leaq 0x188(%rsp), %r8
movq %r14, %rdi
movl %ebp, %esi
callq 0x24fee
leaq 0x98(%rsp), %rsi
movq %r14, (%rsi)
xorl %r15d, %r15d
movq %rbx, %rdi
callq 0x114b8
leaq 0x1a0(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x14fa0
movq 0x1a0(%rsp), %rsi
incq %rsi
callq 0x82a0
leaq 0x1e0(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x14fc1
movq 0x1e0(%rsp), %rsi
incq %rsi
callq 0x82a0
leaq 0x220(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x14fe2
movq 0x220(%rsp), %rsi
incq %rsi
callq 0x82a0
movq 0x30(%rsp), %rdi
cmpq %r12, %rdi
je 0x14ff9
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x82a0
leaq 0x10(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x1505c
movq 0x10(%rsp), %rsi
jmp 0x15054
cltd
idivl %ecx
movl %edx, %ecx
jmp 0x15023
imull %eax, %ecx
jmp 0x15023
cltd
idivl %ecx
jmp 0x15021
subl %ecx, %eax
movl %eax, %ecx
leaq 0x28(%rsp), %rdi
movl $0x1, %esi
movl %ecx, %edx
callq 0x12ade
leaq 0x28(%rsp), %rsi
movq %r15, %rdi
callq 0x28312
leaq 0x40(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x1505c
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x82a0
movq 0x110(%rsp), %rdi
leaq 0x120(%rsp), %rax
cmpq %rax, %rdi
je 0x15081
movq 0x120(%rsp), %rsi
incq %rsi
callq 0x82a0
movq 0x150(%rsp), %rdi
leaq 0x160(%rsp), %rax
cmpq %rax, %rdi
je 0x150a6
movq 0x160(%rsp), %rsi
incq %rsi
callq 0x82a0
addq $0x248, %rsp            # imm = 0x248
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x7af31(%rip), %rdi     # 0x8fff0
leaq 0x5a0a2(%rip), %rsi     # 0x6f168
movl $0x8, %edx
callq 0x8330
movq 0x7af19(%rip), %rdi     # 0x8fff0
leaq 0x5aab5(%rip), %rsi     # 0x6fb93
movl $0x13, %edx
callq 0x8330
movq 0x7af01(%rip), %rdi     # 0x8fff0
leaq 0x5b2ca(%rip), %rsi     # 0x703c0
movl $0x1, %edx
callq 0x8330
movq 0x7aee9(%rip), %rdi     # 0x8fff0
movl $0x11a, %esi            # imm = 0x11A
callq 0x8520
movq %rax, %rdi
callq 0x8180
movl $0xff, %edi
callq 0x8380
movq 0x7aec6(%rip), %rdi     # 0x8fff0
leaq 0x5a037(%rip), %rsi     # 0x6f168
movl $0x8, %edx
callq 0x8330
movq 0x7aeae(%rip), %rdi     # 0x8fff0
leaq 0x5aa4a(%rip), %rsi     # 0x6fb93
movl $0x13, %edx
callq 0x8330
movq 0x7ae96(%rip), %rdi     # 0x8fff0
leaq 0x5b25f(%rip), %rsi     # 0x703c0
movl $0x1, %edx
callq 0x8330
movq 0x7ae7e(%rip), %rdi     # 0x8fff0
movl $0x103, %esi            # imm = 0x103
callq 0x8520
movq %rax, %rdi
callq 0x8180
jmp 0x15119
jmp 0x1535a
jmp 0x15216
jmp 0x1535a
movq %rax, %rbp
movb $0x1, %r15b
jmp 0x152d2
movq %rax, %rbp
movb $0x1, %r15b
jmp 0x152f3
movq %rax, %rbp
movb $0x1, %r15b
jmp 0x15314
jmp 0x151b8
movq %rax, %rbp
jmp 0x15326
movq %rax, %rbp
movq 0xe8(%rsp), %rdi
cmpq %r13, %rdi
je 0x1533d
movq 0xf8(%rsp), %rsi
jmp 0x15335
jmp 0x15207
movq %rax, %rbp
leaq 0x28(%rsp), %rdi
callq 0x24df0
jmp 0x151f5
movq %rax, %rbp
leaq 0x98(%rsp), %rdi
callq 0x24e4e
jmp 0x1533d
movq %rax, %rbp
jmp 0x1533d
jmp 0x1535a
jmp 0x15245
movq %rax, %rbp
leaq 0x40(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x1535d
movq 0x40(%rsp), %rsi
jmp 0x15350
jmp 0x15261
jmp 0x1535a
jmp 0x15283
jmp 0x15294
jmp 0x1535a
movq %rax, %rbp
movq 0x28(%rsp), %rdi
cmpq %rbx, %rdi
je 0x15264
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x82a0
jmp 0x15264
movq %rax, %rbp
movq 0x98(%rsp), %rdi
cmpq %r13, %rdi
je 0x15286
movq 0xa8(%rsp), %rsi
incq %rsi
callq 0x82a0
jmp 0x15286
movq %rax, %rbp
movq (%rsp), %rdi
leaq 0x10(%rsp), %rax
jmp 0x15346
movq %rax, %rbp
movq (%rsp), %rdi
cmpq %r15, %rdi
jne 0x1534b
jmp 0x1535d
jmp 0x1535a
movq %rax, %rbp
leaq 0x1a0(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x152d2
movq 0x1a0(%rsp), %rsi
incq %rsi
callq 0x82a0
leaq 0x1e0(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x152f3
movq 0x1e0(%rsp), %rsi
incq %rsi
callq 0x82a0
leaq 0x220(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x15314
movq 0x220(%rsp), %rsi
incq %rsi
callq 0x82a0
testb %r15b, %r15b
je 0x15326
movl $0xe0, %esi
movq %r14, %rdi
callq 0x82a0
movq 0x30(%rsp), %rdi
cmpq %r12, %rdi
je 0x1533d
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x82a0
leaq 0x10(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x1535d
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x82a0
jmp 0x1535d
movq %rax, %rbp
movq 0x110(%rsp), %rdi
leaq 0x120(%rsp), %rax
cmpq %rax, %rdi
je 0x15382
movq 0x120(%rsp), %rsi
incq %rsi
callq 0x82a0
movq 0x150(%rsp), %rdi
leaq 0x160(%rsp), %rax
cmpq %rax, %rdi
je 0x153a7
movq 0x160(%rsp), %rsi
incq %rsi
callq 0x82a0
movq %rbp, %rdi
callq 0x8540
nop