// File: kelwinfc[P]ci4721-Fernandez-Gyomrey/src/optimizer.cpp
// Function: delete_unreachable_code()
void delete_unreachable_code(){
    
    int* reachable;
    reachable = (int*)malloc(list_of_blocks.size()*sizeof(int));
    memset(reachable, 0, list_of_blocks.size()*sizeof(int));
    
    stack<Block*> s;
    {
        Block* first_block = 0;
        
        vector<Block*>::iterator it = list_of_blocks.begin();
        for( ; it != list_of_blocks.end(); ++it){
            if ( (*((*it)->instructions.linst->begin()))->label == first_instruction ){
                first_block = *it;
                break;
            }
        }
        
        if ( first_block == 0 ){
            return;
        }
        
        s.push(first_block);
    }
    
    while ( !s.empty() ){
        Block* next_block = s.top();
        s.pop();
        
        if ( reachable[next_block->index] )
            continue;
        
        reachable[next_block->index] = 1;
        
        if ( next_block->mandatory_exit != 0 ){
            s.push( next_block->mandatory_exit );
        }
        
        vector<Block*>::iterator it = next_block->sucessors.begin();
        for (; it != next_block->sucessors.end(); ++it ){
            s.push(*it);
        }
    }
    
    vector<Block*> reachable_blocks;
    for (uint i = 0; i<list_of_blocks.size(); i++){
        if ( reachable[ list_of_blocks[i]->index ] ){
            reachable_blocks.push_back(list_of_blocks[i]);
        } else {
            //delete list_of_blocks[i];
        }
    }
    while (!list_of_blocks.empty()){
        list_of_blocks.pop_back();
    }
    for (uint i=0; i<reachable_blocks.size(); i++){
        list_of_blocks.push_back(reachable_blocks[i]);
    }
}

// Assembly:
subq $0xf8, %rsp
leaq 0x315fa(%rip), %rdi     # 0x6e488
callq 0x34dc0
movq %rax, %rdi
shlq $0x2, %rdi
callq 0x6530
movq %rax, 0xf0(%rsp)
movq 0xf0(%rsp), %rax
movq %rax, 0x28(%rsp)
leaq 0x315cd(%rip), %rdi     # 0x6e488
callq 0x34dc0
movq 0x28(%rsp), %rdi
movq %rax, %rdx
shlq $0x2, %rdx
xorl %esi, %esi
callq 0x61e0
leaq 0xa0(%rsp), %rdi
callq 0x3d330
movq $0x0, 0x98(%rsp)
leaq 0x31595(%rip), %rdi     # 0x6e488
callq 0x350a0
movq %rax, 0x90(%rsp)
leaq 0x31581(%rip), %rdi     # 0x6e488
callq 0x35110
movq %rax, 0x88(%rsp)
leaq 0x90(%rsp), %rdi
leaq 0x88(%rsp), %rsi
callq 0x350d0
testb $0x1, %al
jne 0x3cf2f
jmp 0x3cf9c
leaq 0x90(%rsp), %rdi
callq 0x35140
movq (%rax), %rax
movq 0x8(%rax), %rdi
callq 0x34c70
movq %rax, 0x80(%rsp)
leaq 0x80(%rsp), %rdi
callq 0x34cf0
movq (%rax), %rax
movl 0x8(%rax), %eax
leaq 0x314ee(%rip), %rcx     # 0x6e458
cmpl (%rcx), %eax
jne 0x3cf88
leaq 0x90(%rsp), %rdi
callq 0x35140
movq (%rax), %rax
movq %rax, 0x98(%rsp)
jmp 0x3cf9c
jmp 0x3cf8a
leaq 0x90(%rsp), %rdi
callq 0x35150
jmp 0x3cf00
cmpq $0x0, 0x98(%rsp)
jne 0x3cfb4
movl $0x1, 0x7c(%rsp)
jmp 0x3d23c
leaq 0xa0(%rsp), %rdi
leaq 0x98(%rsp), %rsi
callq 0x3d360
jmp 0x3cfcb
jmp 0x3cfcd
leaq 0xa0(%rsp), %rdi
callq 0x3d390
movb %al, 0x27(%rsp)
jmp 0x3cfe0
movb 0x27(%rsp), %al
xorb $-0x1, %al
testb $0x1, %al
jne 0x3cfef
jmp 0x3d0f1
leaq 0xa0(%rsp), %rdi
callq 0x3d3b0
movq %rax, 0x18(%rsp)
jmp 0x3d003
movq 0x18(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x60(%rsp)
leaq 0xa0(%rsp), %rdi
callq 0x3d3c0
jmp 0x3d01f
movq 0xf0(%rsp), %rax
movq 0x60(%rsp), %rcx
movslq (%rcx), %rcx
cmpl $0x0, (%rax,%rcx,4)
je 0x3d04a
jmp 0x3cfcd
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x70(%rsp)
movl %eax, 0x6c(%rsp)
jmp 0x3d251
movq 0xf0(%rsp), %rax
movq 0x60(%rsp), %rcx
movslq (%rcx), %rcx
movl $0x1, (%rax,%rcx,4)
movq 0x60(%rsp), %rax
cmpq $0x0, 0x10(%rax)
je 0x3d087
movq 0x60(%rsp), %rsi
addq $0x10, %rsi
leaq 0xa0(%rsp), %rdi
callq 0x3d360
jmp 0x3d085
jmp 0x3d087
movq 0x60(%rsp), %rdi
addq $0x18, %rdi
callq 0x350a0
movq %rax, 0x58(%rsp)
movq 0x60(%rsp), %rdi
addq $0x18, %rdi
callq 0x35110
movq %rax, 0x50(%rsp)
leaq 0x58(%rsp), %rdi
leaq 0x50(%rsp), %rsi
callq 0x350d0
testb $0x1, %al
jne 0x3d0c2
jmp 0x3d0ec
leaq 0x58(%rsp), %rdi
callq 0x35140
movq %rax, %rsi
leaq 0xa0(%rsp), %rdi
callq 0x3d360
jmp 0x3d0de
jmp 0x3d0e0
leaq 0x58(%rsp), %rdi
callq 0x35150
jmp 0x3d09a
jmp 0x3cfcd
leaq 0x38(%rsp), %rdi
callq 0x349e0
movl $0x0, 0x34(%rsp)
movl 0x34(%rsp), %eax
movq %rax, 0x10(%rsp)
leaq 0x31375(%rip), %rdi     # 0x6e488
callq 0x34dc0
movq %rax, %rcx
movq 0x10(%rsp), %rax
cmpq %rcx, %rax
jae 0x3d1b0
movq 0xf0(%rsp), %rax
movq %rax, 0x8(%rsp)
movl 0x34(%rsp), %eax
movl %eax, %esi
leaq 0x31345(%rip), %rdi     # 0x6e488
callq 0x34f80
movq %rax, %rcx
movq 0x8(%rsp), %rax
movq (%rcx), %rcx
movslq (%rcx), %rcx
cmpl $0x0, (%rax,%rcx,4)
je 0x3d19c
movl 0x34(%rsp), %eax
movl %eax, %esi
leaq 0x3131f(%rip), %rdi     # 0x6e488
callq 0x34f80
movq %rax, %rsi
leaq 0x38(%rsp), %rdi
callq 0x34de0
jmp 0x3d17d
jmp 0x3d19e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x70(%rsp)
movl %eax, 0x6c(%rsp)
leaq 0x38(%rsp), %rdi
callq 0x34a10
jmp 0x3d251
jmp 0x3d19e
jmp 0x3d1a0
movl 0x34(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x34(%rsp)
jmp 0x3d103
jmp 0x3d1b2
leaq 0x312cf(%rip), %rdi     # 0x6e488
callq 0x3d3d0
xorb $-0x1, %al
testb $0x1, %al
jne 0x3d1c6
jmp 0x3d1d4
leaq 0x312bb(%rip), %rdi     # 0x6e488
callq 0x3d420
jmp 0x3d1b2
movl $0x0, 0x30(%rsp)
movl 0x30(%rsp), %eax
movq %rax, (%rsp)
leaq 0x38(%rsp), %rdi
callq 0x34dc0
movq %rax, %rcx
movq (%rsp), %rax
cmpq %rcx, %rax
jae 0x3d22a
movl 0x30(%rsp), %eax
movl %eax, %esi
leaq 0x38(%rsp), %rdi
callq 0x34f80
movq %rax, %rsi
leaq 0x31274(%rip), %rdi     # 0x6e488
callq 0x34de0
jmp 0x3d21b
jmp 0x3d21d
movl 0x30(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x30(%rsp)
jmp 0x3d1dc
leaq 0x38(%rsp), %rdi
callq 0x34a10
movl $0x0, 0x7c(%rsp)
leaq 0xa0(%rsp), %rdi
callq 0x3d440
addq $0xf8, %rsp
retq
leaq 0xa0(%rsp), %rdi
callq 0x3d440
movq 0x70(%rsp), %rdi
callq 0x66b0
nopl (%rax,%rax)