// File: /workspace/github_c_makefile_star2_O3/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 $0x28, %rsp
movq %rdi, -0x30(%rbp)
xorl %eax, %eax
movq %rax, -0x38(%rbp)
movq %rax, -0x40(%rbp)
movq 0x10(%rsi), %rax
testq %rax, %rax
je 0x1762b
movq %rdx, -0x48(%rbp)
movl 0x28(%rdx), %ecx
xorl %r8d, %r8d
leaq -0x38(%rbp), %rdx
xorps %xmm0, %xmm0
xorl %r14d, %r14d
cmpl $0x9, 0x28(%rax)
jne 0x17471
movslq 0x58(%rax), %r9
testq %r9, %r9
jle 0x17465
leaq 0x5c(%rax), %r10
xorl %r11d, %r11d
movq %r8, %r12
movq %r14, %r15
leaq (%r11,%r11,2), %rsi
cmpl %ecx, (%r10,%rsi,4)
jne 0x17459
leaq (%r10,%rsi,4), %rdi
movl 0x2c(%rax), %esi
movl 0x4(%rdi), %ebx
cmpl %ebx, %esi
je 0x17459
movq %rdx, %r13
movq (%r13), %r13
testq %r13, %r13
je 0x17382
cmpl %esi, 0x20(%r13)
jne 0x17371
jmp 0x173b8
movq %rsp, %rsi
leaq -0x30(%rsi), %r8
movq %r8, %rsp
movq 0x2c(%rax), %rbx
movq %rbx, -0x10(%rsi)
movq %r12, -0x30(%rsi)
testq %r12, %r12
je 0x173a2
movq %r8, 0x8(%r12)
movq %r8, -0x38(%rbp)
movq %rdx, 0x8(%r8)
movaps %xmm0, 0x10(%r8)
movl 0x4(%rdi), %ebx
movq %r8, %r12
movq %r8, %r13
movq %rdx, %rsi
movq (%rsi), %rsi
testq %rsi, %rsi
je 0x173ca
cmpl %ebx, 0x20(%rsi)
jne 0x173bb
jmp 0x173fd
movq %rsp, %rsi
leaq -0x30(%rsi), %r8
movq %r8, %rsp
movq 0x4(%rdi), %rdi
movq %rdi, -0x10(%rsi)
movq %r12, -0x30(%rsi)
testq %r12, %r12
je 0x173ea
movq %r8, 0x8(%r12)
movq %r8, -0x38(%rbp)
movq %rdx, 0x8(%r8)
movaps %xmm0, 0x10(%r8)
movq %r8, %r12
movq %r8, %rsi
cmpq $0x0, 0x18(%r13)
jne 0x1763a
movq %rsp, %rdi
leaq -0x30(%rdi), %r14
movq %r14, %rsp
movq %rsi, -0x10(%rdi)
movq %r13, -0x8(%rdi)
movq 0x10(%rsi), %rbx
movq %rbx, -0x20(%rdi)
testq %rbx, %rbx
je 0x1742f
addq $-0x20, %rdi
movq %rdi, 0x18(%rbx)
addq $0x10, %rsi
movq %r14, (%rsi)
movq %rsi, 0x18(%r14)
movq %r14, 0x18(%r13)
movq %r15, (%r14)
testq %r15, %r15
je 0x1744a
movq %r14, 0x8(%r15)
movq %r14, -0x40(%rbp)
leaq -0x40(%rbp), %rsi
movq %rsi, 0x8(%r14)
movq %r14, %r15
incq %r11
cmpq %r9, %r11
jb 0x1734e
movq (%rax), %rax
testq %rax, %rax
jne 0x1732a
testq %r14, %r14
movq -0x48(%rbp), %rax
je 0x1762b
movq 0x18(%rax), %rax
movq 0x8(%rax), %rax
movq (%rax), %r13
movl $0x11, %ebx
movl $0x1, %r12d
testq %r14, %r14
je 0x1762b
xorl %eax, %eax
movq %r14, %r15
movq (%r14), %r14
movq 0x28(%r15), %rcx
cmpq $0x0, 0x10(%rcx)
jne 0x17529
movq 0x20(%r15), %rax
movq 0x20(%rcx), %rsi
movq 0x20(%rax), %rdx
movq -0x30(%rbp), %rdi
movq %r13, %rcx
callq 0x17658
movq (%r15), %rax
movq 0x8(%r15), %rcx
testq %rax, %rax
je 0x174d8
movq %rcx, 0x8(%rax)
movq %rax, (%rcx)
movq 0x10(%r15), %rax
movq 0x18(%r15), %rcx
testq %rax, %rax
je 0x174ec
movq %rcx, 0x18(%rax)
movq %rax, (%rcx)
movq 0x28(%r15), %rcx
movq (%rcx), %rax
movq 0x8(%rcx), %rcx
testq %rax, %rax
je 0x17503
movq %rcx, 0x8(%rax)
movq %rax, (%rcx)
movq 0x20(%r15), %rdx
movl $0x1, %eax
cmpq $0x0, 0x10(%rdx)
jne 0x17529
movq (%rdx), %rcx
movq 0x8(%rdx), %rdx
testq %rcx, %rcx
je 0x17526
movq %rdx, 0x8(%rcx)
movq %rcx, (%rdx)
testq %r14, %r14
jne 0x1749f
movq -0x40(%rbp), %r14
testl %eax, %eax
movl $0x0, %eax
cmovel %ebx, %eax
testq %r14, %r14
cmovel %r12d, %eax
testl %eax, %eax
je 0x17494
cmpl $0x11, %eax
jne 0x1762b
movq -0x38(%rbp), %r12
testq %r12, %r12
je 0x1762b
movl 0x24(%r12), %ebx
movq -0x30(%rbp), %r14
movq %r14, %rdi
callq 0xf50d
movq 0x180(%r14), %rcx
cltq
movq (%rcx,%rax,8), %rcx
movl $0x4, (%rcx)
movl $0xffffffff, 0x38(%rcx) # imm = 0xFFFFFFFF
movl %ebx, 0x24(%rcx)
shlq $0x20, %rbx
movl %eax, %r15d
orq %rbx, %r15
movq 0x20(%r12), %rdx
movq %r14, %rdi
movq %r15, %rsi
movq %r13, %rcx
callq 0x17658
movq 0x18(%r12), %rax
movq 0x20(%rax), %rax
cmpq %r12, %rax
je 0x175fd
movq %r12, %r14
movq %r14, %rbx
movq %rax, %r14
movq 0x20(%rbx), %rsi
movq 0x20(%rax), %rdx
movq -0x30(%rbp), %rdi
movq %r13, %rcx
callq 0x17658
movq (%rbx), %rax
movq 0x8(%rbx), %rcx
testq %rax, %rax
je 0x175eb
movq %rcx, 0x8(%rax)
movq %rax, (%rcx)
movq 0x18(%r14), %rax
movq 0x20(%rax), %rax
cmpq %r12, %rax
jne 0x175c1
jmp 0x17600
movq %r12, %r14
movq 0x20(%r14), %rsi
movq -0x30(%rbp), %rdi
movq %r15, %rdx
movq %r13, %rcx
callq 0x17658
movq (%r14), %rax
movq 0x8(%r14), %rcx
testq %rax, %rax
je 0x17623
movq %rcx, 0x8(%rax)
movq %rax, (%rcx)
jmp 0x17558
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xeb1d(%rip), %rsi      # 0x2615e
leaq 0xef46(%rip), %rcx      # 0x2658e
movq -0x30(%rbp), %rdi
movl $0x3a8, %edx            # imm = 0x3A8
xorl %eax, %eax
callq 0xca98