// File: /workspace/github_c_makefile_star2_O2/cogbee[P]vmir-latest/src/vmir_transform.c
// Function: exit_ssa_edge
static void
exit_ssa_edge(ir_unit_t *iu, ir_function_t *f, ir_bb_t *bb,
              ir_bb_edge_t *ibe)
{
  phi_lift_node_t *dst, *src;
  phi_lift_egde_t *ple, *ple_next;
  ir_instr_t *ii;
  struct phi_lift_node_list nodes;
  struct phi_lift_edge_list edges;

  LIST_INIT(&nodes);
  LIST_INIT(&edges);
  ir_bb_t *prebb = ibe->ibe_from;
  const int predecessor = prebb->ib_id;

  TAILQ_FOREACH(ii, &bb->ib_instrs, ii_link) {
    if(ii->ii_class != IR_IC_PHI)
      break;

    ir_instr_phi_t *iip = (ir_instr_phi_t *)ii;
    for(int i = 0; i < iip->num_nodes; i++) {
      if(iip->nodes[i].predecessor != predecessor)
        continue;

      if(iip->super.ii_ret.value == iip->nodes[i].value.value)
        continue;

      dst = phi_lift_node_find(&nodes, iip->super.ii_ret.value);
      if(dst == NULL) {
        dst = alloca(sizeof(phi_lift_node_t));
        phi_lift_node_init(&nodes, dst, iip->super.ii_ret);
      }

      src = phi_lift_node_find(&nodes, iip->nodes[i].value.value);
      if(src == NULL) {
        src = alloca(sizeof(phi_lift_node_t));
        phi_lift_node_init(&nodes, src, iip->nodes[i].value);
      }
      if(dst->pln_dst != NULL)
        parser_error(iu, "Multiple PHI entries point to same register");

      ple = alloca(sizeof(phi_lift_egde_t));
      ple->ple_src = src;
      ple->ple_dst = dst;
      LIST_INSERT_HEAD(&src->pln_src, ple, ple_src_link);
      dst->pln_dst = ple;
      LIST_INSERT_HEAD(&edges,        ple, ple_link);
    }
  }

  if(LIST_FIRST(&edges) == NULL)
    return;

  // First moves where destination is not also a source
  ir_instr_t *last = TAILQ_LAST(&prebb->ib_instrs, ir_instr_queue);


  while(1) {
    int progress = 0;

    for(ple = LIST_FIRST(&edges); ple != NULL; ple = ple_next) {
      ple_next = LIST_NEXT(ple, ple_link);
      if(LIST_FIRST(&ple->ple_dst->pln_src) != NULL)
        continue;

      insert_move(iu, ple->ple_dst->pln_vt, ple->ple_src->pln_vt, last);

      progress = 1;
      LIST_REMOVE(ple, ple_link);
      LIST_REMOVE(ple, ple_src_link);
      LIST_REMOVE(ple->ple_dst, pln_link);
      if(LIST_FIRST(&ple->ple_src->pln_src) == NULL)
        LIST_REMOVE(ple->ple_src, pln_link);
    }

    if(LIST_FIRST(&edges) == NULL)
      return;

    if(!progress)
      break;
  }



  while(1) {
    phi_lift_node_t *start = LIST_FIRST(&nodes);
    if(start == NULL)
      break;
    // Add a move to temporary register to resolve cycle

    ir_valuetype_t tmpreg = value_alloc_temporary(iu, start->pln_vt.type);
    insert_move(iu, tmpreg, start->pln_vt, last);

    phi_lift_node_t *cur = start;
    while(1) {
      phi_lift_node_t *src = cur->pln_dst->ple_src;
      if(src == start)
        break;

      insert_move(iu, cur->pln_vt, src->pln_vt, last);
      LIST_REMOVE(cur, pln_link);
      cur = src;
    }

    insert_move(iu, cur->pln_vt, tmpreg, last);
    LIST_REMOVE(cur, pln_link);
  }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
leaq -0x40(%rbp), %rax
andq $0x0, (%rax)
leaq -0x48(%rbp), %rax
andq $0x0, (%rax)
movq %rsi, %rcx
movq %rdi, -0x30(%rbp)
movq 0x30(%rdx), %rax
movq %rax, -0x68(%rbp)
movl 0x28(%rax), %eax
addq $0x10, %rcx
xorl %ebx, %ebx
xorl %edi, %edi
movl %eax, -0x4c(%rbp)
movq (%rcx), %rcx
testq %rcx, %rcx
je 0x148d0
cmpl $0x9, 0x28(%rcx)
jne 0x148d0
movslq 0x58(%rcx), %rdx
leaq 0x60(%rcx), %r14
xorl %r15d, %r15d
movq %rbx, %r8
movq %rdi, -0x38(%rbp)
movq %rcx, -0x58(%rbp)
movq %rdx, -0x70(%rbp)
cmpq %rdx, %r15
jge 0x14759
cmpl %eax, -0x4(%r14)
jne 0x148c4
movl 0x2c(%rcx), %esi
movl (%r14), %r13d
cmpl %r13d, %esi
je 0x148c4
movq %r8, -0x78(%rbp)
movq %rdi, -0x60(%rbp)
leaq -0x40(%rbp), %rbx
movq %rbx, %rdi
callq 0x14a7f
testq %rax, %rax
je 0x147c5
movq %rax, %r12
jmp 0x14809
movq %rsp, %rax
leaq -0x30(%rax), %r12
movq %r12, %rsp
movq -0x58(%rbp), %rcx
movq 0x2c(%rcx), %rcx
movq %rcx, -0x10(%rax)
movq -0x38(%rbp), %rcx
movq %rcx, -0x30(%rax)
testq %rcx, %rcx
je 0x147ec
movq %r12, 0x8(%rcx)
movq %r12, -0x40(%rbp)
movq %rbx, 0x8(%r12)
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%r12)
movl (%r14), %r13d
movq %r12, -0x38(%rbp)
movq %r12, -0x60(%rbp)
movq %rbx, %rdi
movl %r13d, %esi
callq 0x14a7f
testq %rax, %rax
je 0x1481f
movq -0x60(%rbp), %rdi
jmp 0x14857
movq %rsp, %rax
leaq -0x30(%rax), %rdi
movq %rdi, %rsp
movq (%r14), %rcx
movq %rcx, -0x10(%rax)
movq -0x38(%rbp), %rcx
movq %rcx, -0x30(%rax)
testq %rcx, %rcx
je 0x14841
movq %rdi, 0x8(%rcx)
movq %rdi, -0x40(%rbp)
movq %rbx, 0x8(%rdi)
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rdi)
movq %rdi, -0x38(%rbp)
movq %rdi, %rax
cmpq $0x0, 0x18(%r12)
jne 0x14a61
movq %rsp, %rcx
leaq -0x30(%rcx), %rbx
movq %rbx, %rsp
movq %rax, -0x10(%rcx)
movq %r12, -0x8(%rcx)
movq 0x10(%rax), %rdx
movq %rdx, -0x20(%rcx)
testq %rdx, %rdx
je 0x1488a
addq $-0x20, %rcx
movq %rcx, 0x18(%rdx)
addq $0x10, %rax
movq %rbx, (%rax)
movq %rax, 0x18(%rbx)
movq %rbx, 0x18(%r12)
movq -0x78(%rbp), %rax
movq %rax, (%rbx)
testq %rax, %rax
je 0x148aa
movq %rbx, 0x8(%rax)
movq %rbx, -0x48(%rbp)
leaq -0x48(%rbp), %rax
movq %rax, 0x8(%rbx)
movq %rbx, %r8
movq -0x58(%rbp), %rcx
movl -0x4c(%rbp), %eax
movq -0x70(%rbp), %rdx
incq %r15
addq $0xc, %r14
jmp 0x14789
testq %rbx, %rbx
je 0x14a52
movq -0x68(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x8(%rax), %rax
movq (%rax), %r14
xorl %eax, %eax
pushq $0x11
popq %r15
pushq $0x1
popq %r12
testq %rbx, %rbx
je 0x1498c
movq %rbx, %r13
movq (%rbx), %rbx
movq 0x28(%r13), %rcx
cmpq $0x0, 0x10(%rcx)
jne 0x148f2
movq 0x20(%r13), %rax
movq 0x20(%rcx), %rsi
movq 0x20(%rax), %rdx
movq -0x30(%rbp), %rdi
movq %r14, %rcx
callq 0x14a90
movq (%r13), %rax
movq 0x8(%r13), %rcx
testq %rax, %rax
je 0x14935
movq %rcx, 0x8(%rax)
movq %rax, (%rcx)
movq 0x10(%r13), %rax
movq 0x18(%r13), %rcx
testq %rax, %rax
je 0x14949
movq %rcx, 0x18(%rax)
movq %rax, (%rcx)
movq 0x28(%r13), %rcx
movq (%rcx), %rax
movq 0x8(%rcx), %rcx
testq %rax, %rax
je 0x14960
movq %rcx, 0x8(%rax)
movq %rax, (%rcx)
movq 0x20(%r13), %rcx
cmpq $0x0, 0x10(%rcx)
movl %r12d, %eax
jne 0x148f2
movq (%rcx), %rax
movq 0x8(%rcx), %rcx
testq %rax, %rax
je 0x14981
movq %rcx, 0x8(%rax)
movq %rax, (%rcx)
movl %r12d, %eax
jmp 0x148f2
movq -0x48(%rbp), %rbx
testl %eax, %eax
movl $0x0, %eax
cmovel %r15d, %eax
testq %rbx, %rbx
cmovel %r12d, %eax
testl %eax, %eax
je 0x148f2
cmpl $0x11, %eax
jne 0x14a52
movq -0x40(%rbp), %rbx
testq %rbx, %rbx
je 0x14a52
movl 0x24(%rbx), %esi
movq -0x30(%rbp), %r12
movq %r12, %rdi
callq 0x101ec
movq %rax, %r15
movq 0x20(%rbx), %rdx
movq %r12, %rdi
movq %rax, %rsi
movq %r14, %rcx
callq 0x14a90
movq %rbx, %r12
movq 0x18(%r12), %rax
movq 0x20(%rax), %r13
cmpq %rbx, %r13
je 0x14a24
movq 0x20(%r12), %rsi
movq 0x20(%r13), %rdx
movq -0x30(%rbp), %rdi
movq %r14, %rcx
callq 0x14a90
movq (%r12), %rax
movq 0x8(%r12), %rcx
testq %rax, %rax
je 0x14a1c
movq %rcx, 0x8(%rax)
movq %rax, (%rcx)
movq %r13, %r12
jmp 0x149e7
movq 0x20(%r12), %rsi
movq -0x30(%rbp), %rdi
movq %r15, %rdx
movq %r14, %rcx
callq 0x14a90
movq (%r12), %rax
movq 0x8(%r12), %rcx
testq %rax, %rax
je 0x14a4a
movq %rcx, 0x8(%rax)
movq %rax, (%rcx)
jmp 0x149b3
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xc861(%rip), %rsi      # 0x212c9
leaq 0xcc8a(%rip), %rcx      # 0x216f9
movq -0x30(%rbp), %rdi
movl $0x3a8, %edx            # imm = 0x3A8
xorl %eax, %eax
callq 0xbed2