// File: /workspace/github_cpp_makefile_star2_O2/luooofan[P]hblzbd_compiler/src/Pass/loop_invariant_extrapolation.cpp
// Function: walk(IRBasicBlock*, std::set<int, std::less<int>, std::allocator<int>>, std::map<int, IRBasicBlock*, std::less<int>, std::allocator<std::pair<int const, IRBasicBlock*>>>, std::vector<int, std::allocator<int>>, std::map<IRBasicBlock*, int, std::less<IRBasicBlock*>, std::allocator<std::pair<IRBasicBlock* const, int>>>)
vector<IRBasicBlock*> walk(IRBasicBlock* enter, set<int> in_loop, map<int, IRBasicBlock*> id_bb, vector<int> loop,
                           map<IRBasicBlock*, int> bb_id) {
  set<IRBasicBlock*> new_in_loop;
  for (auto it : in_loop) new_in_loop.insert(id_bb[it]);
  vector<IRBasicBlock*> new_loop;
  for (auto bid : loop) new_loop.push_back(id_bb[bid]);
  vector<IRBasicBlock*> res;
  if (enter->ir_list_.size() != 2) return res;
  res.push_back(enter);
  while (true) {
    auto bb = res[res.size() - 1];
    // 下面条件就是要求:后继必须是2个，并且1个指向loop内，1个指向loop外(只要求2个的话，有可能是ifelse)
    if (bb->succ_.size() == 2 && ((new_in_loop.find(bb->succ_[0]) == new_in_loop.end()) +
                                  (new_in_loop.find(bb->succ_[1]) == new_in_loop.end())) == 1) {
      auto nxt = new_in_loop.find(bb->succ_[0]) == new_in_loop.end() ? bb->succ_[1] : bb->succ_[0];
      if (nxt->ir_list_.size() != 1) break;
      res.push_back(nxt);
    } else
      break;
  }
  vector<IRBasicBlock*> res1;
  for (auto bb0 : res) {
    auto op1 = (*(bb0->ir_list_.rbegin()))->opn1_, op2 = (*(bb0->ir_list_.rbegin()))->opn2_;
    if (op1.scope_id_ == 0 || op2.scope_id_ == 0) continue;
    vector<ir::Opn> ops;
    ops.push_back(op1), ops.push_back(op2);
    if (op1.type_ == ir::Opn::Type::Array) ops.push_back(*op1.offset_);
    if (op2.type_ == ir::Opn::Type::Array) ops.push_back(*op2.offset_);
    bool flag = true;
    for (auto bb : new_loop) {
      for (auto ir : bb->ir_list_) {
        for (auto op : ops) {
          if (ir->res_.name_ == op.name_) {
            flag = false;
            break;
          }
        }
      }
    }
    if (flag) res1.push_back(bb0);
  }
  return res1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x188, %rsp            # imm = 0x188
movq %r8, %r14
movq %rcx, %rbx
movq %rdx, %r15
movq %rdi, (%rsp)
movq %rsi, 0x50(%rsp)
leaq 0xe0(%rsp), %rax
andl $0x0, (%rax)
andq $0x0, 0x8(%rax)
movq %rax, 0x10(%rax)
movq %rax, 0x18(%rax)
andq $0x0, 0x20(%rax)
movq 0x18(%rdx), %r13
addq $0x8, %r15
leaq 0x98(%rsp), %r12
leaq 0xd8(%rsp), %rbp
cmpq %r15, %r13
je 0x47f0a
movl 0x20(%r13), %eax
movl %eax, 0x98(%rsp)
movq %rbx, %rdi
movq %r12, %rsi
callq 0x4b3d6
movq %rbp, %rdi
movq %rax, %rsi
callq 0x4c42e
movq %r13, %rdi
callq 0x82d0
movq %rax, %r13
jmp 0x47ed7
xorps %xmm0, %xmm0
leaq 0x80(%rsp), %r15
andq $0x0, 0x10(%r15)
movaps %xmm0, (%r15)
movq (%r14), %r12
movq 0x8(%r14), %r13
leaq 0x98(%rsp), %r14
leaq 0xe0(%rsp), %rbp
cmpq %r13, %r12
je 0x47f61
movl (%r12), %eax
movl %eax, 0x98(%rsp)
movq %rbx, %rdi
movq %r14, %rsi
callq 0x4b3d6
movq %r15, %rdi
movq %rax, %rsi
callq 0xdd4a
addq $0x4, %r12
jmp 0x47f35
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rsp)
andq $0x0, 0x20(%rsp)
movq 0x50(%rsp), %rax
movq 0x10(%rax), %rcx
subq 0x8(%rax), %rcx
cmpq $0x10, %rcx
jne 0x48039
leaq 0x10(%rsp), %rdi
leaq 0x50(%rsp), %rsi
callq 0xdd4a
leaq 0xd8(%rsp), %rbx
leaq 0x10(%rsp), %r14
leaq 0x98(%rsp), %r15
movq 0x18(%rsp), %rax
movq -0x8(%rax), %r13
movq 0x40(%r13), %rsi
movq 0x48(%r13), %rax
subq %rsi, %rax
cmpq $0x10, %rax
jne 0x4808a
movq %rbx, %rdi
callq 0x4c588
movq %rax, %r12
movq 0x40(%r13), %rsi
addq $0x8, %rsi
movq %rbx, %rdi
callq 0x4c588
cmpq %rbp, %r12
sete %cl
cmpq %rbp, %rax
sete %al
cmpb %al, %cl
je 0x4808a
movq 0x40(%r13), %rsi
movq %rbx, %rdi
callq 0x4c588
xorl %ecx, %ecx
cmpq %rbp, %rax
sete %cl
movq 0x40(%r13), %rax
movq (%rax,%rcx,8), %rax
movq %rax, 0x98(%rsp)
movq 0x10(%rax), %rcx
subq 0x8(%rax), %rcx
cmpq $0x8, %rcx
jne 0x4808a
movq %r14, %rdi
movq %r15, %rsi
callq 0xdd4a
jmp 0x47faa
movq (%rsp), %rax
movups %xmm0, (%rax)
andq $0x0, 0x10(%rax)
movaps %xmm0, 0x10(%rsp)
andq $0x0, 0x20(%rsp)
leaq 0x10(%rsp), %rdi
callq 0x1ff9a
leaq 0x80(%rsp), %rdi
callq 0x1ff9a
leaq 0xd8(%rsp), %rdi
callq 0x4b9ea
movq (%rsp), %rax
addq $0x188, %rsp            # imm = 0x188
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorps %xmm0, %xmm0
movq (%rsp), %rax
andq $0x0, 0x10(%rax)
movups %xmm0, (%rax)
movq 0x10(%rsp), %rax
movq 0x18(%rsp), %rcx
movq %rcx, 0x58(%rsp)
leaq 0x150(%rsp), %r12
cmpq 0x58(%rsp), %rax
je 0x48050
movq %rax, 0x60(%rsp)
movq (%rax), %rax
movq %rax, 0x48(%rsp)
movq 0x10(%rax), %rax
movq -0x8(%rax), %rsi
addq $0x8, %rsi
leaq 0x98(%rsp), %rdi
callq 0x1da22
movq 0x48(%rsp), %rax
movq 0x10(%rax), %rax
movq -0x8(%rax), %rsi
addq $0x48, %rsi
leaq 0x108(%rsp), %rdi
callq 0x1da22
cmpl $0x0, 0xc0(%rsp)
je 0x48246
cmpl $0x0, 0x130(%rsp)
je 0x48246
xorps %xmm0, %xmm0
movaps %xmm0, 0x30(%rsp)
andq $0x0, 0x40(%rsp)
leaq 0x30(%rsp), %rdi
leaq 0x98(%rsp), %rsi
callq 0x4b440
leaq 0x30(%rsp), %rdi
leaq 0x108(%rsp), %rsi
callq 0x4b440
cmpl $0x5, 0x98(%rsp)
jne 0x48165
movq 0xc8(%rsp), %rsi
leaq 0x30(%rsp), %rdi
callq 0x4b440
cmpl $0x5, 0x108(%rsp)
jne 0x48181
movq 0x138(%rsp), %rsi
leaq 0x30(%rsp), %rdi
callq 0x4b440
movq 0x80(%rsp), %rax
movq 0x88(%rsp), %rcx
movq %rcx, 0x68(%rsp)
movb $0x1, %cl
movl %ecx, 0xc(%rsp)
cmpq 0x68(%rsp), %rax
je 0x48227
movq %rax, 0x70(%rsp)
movq (%rax), %rax
movq 0x8(%rax), %rbx
movq 0x10(%rax), %rax
movq %rax, 0x78(%rsp)
cmpq 0x78(%rsp), %rbx
je 0x48219
movq 0x30(%rsp), %r15
movq 0x38(%rsp), %r14
movq (%rbx), %r13
movl $0x90, %eax
addq %rax, %r13
cmpq %r14, %r15
je 0x48213
leaq 0x148(%rsp), %rdi
movq %r15, %rsi
callq 0x1da22
movq %r13, %rdi
movq %r12, %rsi
callq 0xe1ca
movl %eax, %ebp
movq %r12, %rdi
callq 0x8578
addq $0x40, %r15
testb %bpl, %bpl
je 0x481d8
movl $0x0, 0xc(%rsp)
addq $0x8, %rbx
jmp 0x481bc
movq 0x70(%rsp), %rax
addq $0x8, %rax
jmp 0x4819c
testb $0x1, 0xc(%rsp)
je 0x4823c
movq (%rsp), %rdi
leaq 0x48(%rsp), %rsi
callq 0xdd4a
leaq 0x30(%rsp), %rdi
callq 0x1f87e
leaq 0x110(%rsp), %rdi
callq 0x8578
leaq 0xa0(%rsp), %rdi
callq 0x8578
movq 0x60(%rsp), %rax
addq $0x8, %rax
jmp 0x480b0
jmp 0x48278
jmp 0x48293
jmp 0x48278
jmp 0x48278
jmp 0x48278
movq %rax, %rbx
jmp 0x482c3
movq %rax, %rbx
jmp 0x482ad
movq %rax, %rbx
jmp 0x482ba
jmp 0x48293
movq %rax, %rbx
jmp 0x482cd
movq %rax, %rbx
jmp 0x482da
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
callq 0x1f87e
leaq 0x110(%rsp), %rdi
callq 0x8578
leaq 0xa0(%rsp), %rdi
callq 0x8578
movq (%rsp), %rdi
callq 0x1ff9a
leaq 0x10(%rsp), %rdi
callq 0x1ff9a
leaq 0x80(%rsp), %rdi
callq 0x1ff9a
leaq 0xd8(%rsp), %rdi
callq 0x4b9ea
movq %rbx, %rdi
callq 0x84d0
nop