// File: /workspace/github_c_makefile_star2_O0/cogbee[P]vmir-latest/src/vmir_transform.c
// Function: eliminate_dead_code
static void
eliminate_dead_code(ir_unit_t *iu, ir_function_t *f)
{
  ir_bb_t *ib = TAILQ_FIRST(&f->if_bbs);
  if(ib == NULL)
    return;
  ir_bb_t *next;

  eliminate_dead_code_in_bb(iu, f, ib);

  ib = TAILQ_NEXT(ib, ib_link);
  for(; ib != NULL; ib = next) {
    next = TAILQ_NEXT(ib, ib_link);
    ir_bb_edge_t *in = LIST_FIRST(&ib->ib_incoming_edges);
    if(in == NULL) {
      // No incoming edges to this bb (ie, it's unreachable), so delete it
      bb_destroy(ib, f);
      continue;
    }

    eliminate_dead_code_in_bb(iu, f, ib);

    if(LIST_NEXT(in, ibe_to_link) == NULL) {
      // This BB only have one incoming edge..
      ir_bb_t *from = in->ibe_from;
      ir_instr_t *last = TAILQ_LAST(&from->ib_instrs, ir_instr_queue);
      if(last->ii_class == IR_IC_BR) {
        ir_instr_br_t *b = (ir_instr_br_t *)last;
        if(b->condition.value == -1) {
          // .. and previous bb have an unconditional branch to us
          // we may merge the basic blocks

          instr_destroy(last); // Kill branch instruction
          ibe_destroy(in);     // Kill CFG edge
          assert(LIST_FIRST(&from->ib_outgoing_edges) == NULL);

          // Move instructions
          ir_instr_t *ii;
          while((ii = TAILQ_FIRST(&ib->ib_instrs)) != NULL) {
            TAILQ_REMOVE(&ib->ib_instrs, ii, ii_link);
            TAILQ_INSERT_TAIL(&from->ib_instrs, ii, ii_link);
            ii->ii_bb = from;
          }

          // Move outgoing edges
          ir_bb_edge_t *edge;
          while((edge = LIST_FIRST(&ib->ib_outgoing_edges)) != NULL) {
            LIST_REMOVE(edge, ibe_from_link);
            LIST_INSERT_HEAD(&from->ib_outgoing_edges, edge, ibe_from_link);
            edge->ibe_from = from;
          }
          bb_destroy(ib, f);
          continue;
        }
      }
    }
  }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rax
movq %rax, -0x18(%rbp)
cmpq $0x0, -0x18(%rbp)
jne 0x25118
jmp 0x25373
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x18(%rbp), %rdx
callq 0x29360
movq -0x18(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x18(%rbp)
cmpq $0x0, -0x18(%rbp)
je 0x25373
movq -0x18(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x20(%rbp)
movq -0x18(%rbp), %rax
movq 0x30(%rax), %rax
movq %rax, -0x28(%rbp)
cmpq $0x0, -0x28(%rbp)
jne 0x2516f
movq -0x18(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x1c9f0
jmp 0x25366
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x18(%rbp), %rdx
callq 0x29360
movq -0x28(%rbp), %rax
cmpq $0x0, 0x10(%rax)
jne 0x25364
movq -0x28(%rbp), %rax
movq 0x30(%rax), %rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x8(%rax), %rax
movq (%rax), %rax
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rax
cmpl $0x8, 0x28(%rax)
jne 0x25362
movq -0x38(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rax
cmpl $-0x1, 0x58(%rax)
jne 0x25360
movq -0x38(%rbp), %rdi
callq 0x1cad0
movq -0x28(%rbp), %rdi
callq 0x1cb60
movq -0x30(%rbp), %rax
cmpq $0x0, 0x38(%rax)
jne 0x251f1
jmp 0x25210
leaq 0x1aa81(%rip), %rdi     # 0x3fc79
leaq 0x1a1ee(%rip), %rsi     # 0x3f3ed
movl $0x2d9, %edx            # imm = 0x2D9
leaq 0x1aa9b(%rip), %rcx     # 0x3fca6
callq 0x31c0
jmp 0x25212
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x48(%rbp)
cmpq $0x0, %rax
je 0x252b4
jmp 0x2522a
movq -0x48(%rbp), %rax
cmpq $0x0, (%rax)
je 0x25249
movq -0x48(%rbp), %rax
movq 0x8(%rax), %rcx
movq -0x48(%rbp), %rax
movq (%rax), %rax
movq %rcx, 0x8(%rax)
jmp 0x25259
movq -0x48(%rbp), %rax
movq 0x8(%rax), %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x18(%rax)
movq -0x48(%rbp), %rax
movq (%rax), %rcx
movq -0x48(%rbp), %rax
movq 0x8(%rax), %rax
movq %rcx, (%rax)
jmp 0x2526d
movq -0x48(%rbp), %rax
movq $0x0, (%rax)
movq -0x30(%rbp), %rax
movq 0x18(%rax), %rcx
movq -0x48(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x48(%rbp), %rcx
movq -0x30(%rbp), %rax
movq 0x18(%rax), %rax
movq %rcx, (%rax)
movq -0x48(%rbp), %rcx
movq -0x30(%rbp), %rax
movq %rcx, 0x18(%rax)
movq -0x30(%rbp), %rcx
movq -0x48(%rbp), %rax
movq %rcx, 0x10(%rax)
jmp 0x25212
jmp 0x252b6
movq -0x18(%rbp), %rax
movq 0x38(%rax), %rax
movq %rax, -0x50(%rbp)
cmpq $0x0, %rax
je 0x25351
jmp 0x252ce
movq -0x50(%rbp), %rax
cmpq $0x0, (%rax)
je 0x252eb
movq -0x50(%rbp), %rax
movq 0x8(%rax), %rcx
movq -0x50(%rbp), %rax
movq (%rax), %rax
movq %rcx, 0x8(%rax)
movq -0x50(%rbp), %rax
movq (%rax), %rcx
movq -0x50(%rbp), %rax
movq 0x8(%rax), %rax
movq %rcx, (%rax)
jmp 0x252ff
movq -0x30(%rbp), %rax
movq 0x38(%rax), %rax
movq -0x50(%rbp), %rcx
movq %rax, (%rcx)
cmpq $0x0, %rax
je 0x25324
movq -0x50(%rbp), %rcx
movq -0x30(%rbp), %rax
movq 0x38(%rax), %rax
movq %rcx, 0x8(%rax)
movq -0x50(%rbp), %rcx
movq -0x30(%rbp), %rax
movq %rcx, 0x38(%rax)
movq -0x30(%rbp), %rcx
addq $0x38, %rcx
movq -0x50(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x30(%rbp), %rcx
movq -0x50(%rbp), %rax
movq %rcx, 0x30(%rax)
jmp 0x252b6
movq -0x18(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x1c9f0
jmp 0x25366
jmp 0x25362
jmp 0x25364
jmp 0x25366
movq -0x20(%rbp), %rax
movq %rax, -0x18(%rbp)
jmp 0x25134
addq $0x50, %rsp
popq %rbp
retq
nopl (%rax)