// File: Bartor[P]kompilator/middle/peephole/PeepholeOptimizer.cpp
// Function: PeepholeOptimizer::removeUselessStoreLoadis()
bool PeepholeOptimizer::removeUselessStoreLoadis() {
    for (int i = 0; i < instructions.getInstructions().size(); i++) {
        if (auto storeInstruction = dynamic_cast<Store *>(instructions.getInstructions()[i])) {

            int offsetToNonStub = 0;
            while (dynamic_cast<Stub *>(instructions.getInstructions()[++offsetToNonStub + i])) {
                if (i + offsetToNonStub == instructions.getInstructions().size()) return false;
            }

            if (auto loadiInstruction = dynamic_cast<Loadi *>(instructions.getInstructions()[i + offsetToNonStub])) {

                if (storeInstruction->address.getAddress() == loadiInstruction->address.getAddress()) {

                    bool canRemove = true;
                    for (int j = 0; j < instructions.getInstructions().size(); j++) {
                        if (auto jumpInstruction = dynamic_cast<Jump *>(instructions.getInstructions()[j])) {
                            if (jumpInstruction->target->getAddress() == loadiInstruction->getAddress() || jumpInstruction->target->getAddress() == storeInstruction->getAddress()) {
                                canRemove = false;
                                break;
                            }
                        }
                    }

                    if (canRemove) {
                        instructions.getInstructions().erase(instructions.getInstructions().begin() + i);
                        instructions.getInstructions()[i + offsetToNonStub - 1] = new Loadi(*new ResolvableAddress(0));

                        return true;
                    }
                }
            }
        }

    }

    return false;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x100, %rsp            # imm = 0x100
movq %rdi, -0x10(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x78(%rbp)
movl $0x0, -0x14(%rbp)
movq -0x78(%rbp), %rax
movslq -0x14(%rbp), %rcx
movq %rcx, -0x80(%rbp)
movq (%rax), %rdi
callq 0x2b160
movq %rax, %rdi
callq 0x2aba0
movq %rax, %rcx
movq -0x80(%rbp), %rax
cmpq %rcx, %rax
jae 0x2ab83
movq -0x78(%rbp), %rax
movq (%rax), %rdi
callq 0x2b160
movq %rax, %rdi
movslq -0x14(%rbp), %rsi
callq 0x2abc0
movq (%rax), %rax
movq %rax, -0x88(%rbp)
cmpq $0x0, %rax
je 0x2a7a9
movq -0x88(%rbp), %rdi
leaq 0x16080(%rip), %rsi     # 0x40810
leaq 0x16161(%rip), %rdx     # 0x408f8
xorl %eax, %eax
movl %eax, %ecx
callq 0x62f0
movq %rax, -0x90(%rbp)
jmp 0x2a7b4
xorl %eax, %eax
movq %rax, -0x90(%rbp)
jmp 0x2a7b4
movq -0x90(%rbp), %rax
movq %rax, -0x20(%rbp)
cmpq $0x0, -0x20(%rbp)
je 0x2ab73
movl $0x0, -0x24(%rbp)
movq -0x78(%rbp), %rax
movq (%rax), %rdi
callq 0x2b160
movq %rax, %rdi
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
addl -0x14(%rbp), %eax
movslq %eax, %rsi
callq 0x2abc0
movq (%rax), %rax
movq %rax, -0x98(%rbp)
cmpq $0x0, %rax
je 0x2a82b
movq -0x98(%rbp), %rdi
leaq 0x15ffe(%rip), %rsi     # 0x40810
leaq 0x1603f(%rip), %rdx     # 0x40858
xorl %eax, %eax
movl %eax, %ecx
callq 0x62f0
movq %rax, -0xa0(%rbp)
jmp 0x2a836
xorl %eax, %eax
movq %rax, -0xa0(%rbp)
jmp 0x2a836
movq -0xa0(%rbp), %rax
cmpq $0x0, %rax
je 0x2a884
movq -0x78(%rbp), %rax
movl -0x14(%rbp), %ecx
addl -0x24(%rbp), %ecx
movslq %ecx, %rcx
movq %rcx, -0xa8(%rbp)
movq (%rax), %rdi
callq 0x2b160
movq %rax, %rdi
callq 0x2aba0
movq %rax, %rcx
movq -0xa8(%rbp), %rax
cmpq %rcx, %rax
jne 0x2a87f
movb $0x0, -0x1(%rbp)
jmp 0x2ab87
jmp 0x2a7d1
movq -0x78(%rbp), %rax
movq (%rax), %rdi
callq 0x2b160
movq %rax, %rdi
movl -0x14(%rbp), %eax
addl -0x24(%rbp), %eax
movslq %eax, %rsi
callq 0x2abc0
movq (%rax), %rax
movq %rax, -0xb0(%rbp)
cmpq $0x0, %rax
je 0x2a8d8
movq -0xb0(%rbp), %rdi
leaq 0x15f51(%rip), %rsi     # 0x40810
leaq 0x16082(%rip), %rdx     # 0x40948
xorl %eax, %eax
movl %eax, %ecx
callq 0x62f0
movq %rax, -0xb8(%rbp)
jmp 0x2a8e3
xorl %eax, %eax
movq %rax, -0xb8(%rbp)
jmp 0x2a8e3
movq -0xb8(%rbp), %rax
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x30(%rbp)
je 0x2ab71
movq -0x20(%rbp), %rax
movq 0x18(%rax), %rdi
callq 0x24490
movq %rax, -0xc0(%rbp)
movq -0x30(%rbp), %rax
movq 0x18(%rax), %rdi
callq 0x24490
movq %rax, %rcx
movq -0xc0(%rbp), %rax
cmpq %rcx, %rax
jne 0x2ab6f
movb $0x1, -0x31(%rbp)
movl $0x0, -0x38(%rbp)
movq -0x78(%rbp), %rax
movslq -0x38(%rbp), %rcx
movq %rcx, -0xc8(%rbp)
movq (%rax), %rdi
callq 0x2b160
movq %rax, %rdi
callq 0x2aba0
movq %rax, %rcx
movq -0xc8(%rbp), %rax
cmpq %rcx, %rax
jae 0x2aa4a
movq -0x78(%rbp), %rax
movq (%rax), %rdi
callq 0x2b160
movq %rax, %rdi
movslq -0x38(%rbp), %rsi
callq 0x2abc0
movq (%rax), %rax
movq %rax, -0xd0(%rbp)
cmpq $0x0, %rax
je 0x2a9b9
movq -0xd0(%rbp), %rdi
leaq 0x15e70(%rip), %rsi     # 0x40810
leaq 0x16131(%rip), %rdx     # 0x40ad8
xorl %eax, %eax
movl %eax, %ecx
callq 0x62f0
movq %rax, -0xd8(%rbp)
jmp 0x2a9c4
xorl %eax, %eax
movq %rax, -0xd8(%rbp)
jmp 0x2a9c4
movq -0xd8(%rbp), %rax
movq %rax, -0x40(%rbp)
cmpq $0x0, -0x40(%rbp)
je 0x2aa3a
movq -0x40(%rbp), %rax
movq 0x18(%rax), %rdi
movq (%rdi), %rax
callq *0x8(%rax)
movq %rax, -0xe0(%rbp)
movq -0x30(%rbp), %rdi
movq (%rdi), %rax
callq *0x8(%rax)
movq %rax, %rcx
movq -0xe0(%rbp), %rax
cmpq %rcx, %rax
je 0x2aa32
movq -0x40(%rbp), %rax
movq 0x18(%rax), %rdi
movq (%rdi), %rax
callq *0x8(%rax)
movq %rax, -0xe8(%rbp)
movq -0x20(%rbp), %rdi
movq (%rdi), %rax
callq *0x8(%rax)
movq %rax, %rcx
movq -0xe8(%rbp), %rax
cmpq %rcx, %rax
jne 0x2aa38
movb $0x0, -0x31(%rbp)
jmp 0x2aa4a
jmp 0x2aa3a
jmp 0x2aa3c
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
jmp 0x2a938
testb $0x1, -0x31(%rbp)
je 0x2ab6d
movq -0x78(%rbp), %rax
movq (%rax), %rdi
callq 0x2b160
movq %rax, %rcx
movq -0x78(%rbp), %rax
movq %rcx, -0x100(%rbp)
movq (%rax), %rdi
callq 0x2b160
movq %rax, %rdi
callq 0x1fda0
movq %rax, -0x58(%rbp)
movslq -0x14(%rbp), %rsi
leaq -0x58(%rbp), %rdi
callq 0x2ac50
movq %rax, -0x50(%rbp)
leaq -0x48(%rbp), %rdi
leaq -0x50(%rbp), %rsi
callq 0x2ac90
movq -0x100(%rbp), %rdi
movq -0x48(%rbp), %rsi
callq 0x2abe0
movq %rax, -0x60(%rbp)
movl $0x20, %edi
callq 0x6260
movq %rax, -0xf8(%rbp)
movl $0x10, %edi
callq 0x6260
movq %rax, -0xf0(%rbp)
jmp 0x2aad8
movq -0xf0(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x1fa40
jmp 0x2aaea
movq -0xf0(%rbp), %rsi
movq -0xf8(%rbp), %rdi
callq 0x20160
jmp 0x2aaff
movq -0x78(%rbp), %rax
movq (%rax), %rdi
callq 0x2b160
movq %rax, %rdi
movl -0x14(%rbp), %eax
addl -0x24(%rbp), %eax
subl $0x1, %eax
movslq %eax, %rsi
callq 0x2abc0
movq -0xf8(%rbp), %rcx
movq %rcx, (%rax)
movb $0x1, -0x1(%rbp)
jmp 0x2ab87
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
jmp 0x2ab5a
movq -0xf0(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
movl $0x10, %esi
callq 0x6270
movq -0xf8(%rbp), %rdi
movl $0x20, %esi
callq 0x6270
jmp 0x2ab95
jmp 0x2ab6f
jmp 0x2ab71
jmp 0x2ab73
jmp 0x2ab75
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
jmp 0x2a72e
movb $0x0, -0x1(%rbp)
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0x100, %rsp            # imm = 0x100
popq %rbp
retq
movq -0x68(%rbp), %rdi
callq 0x64d0
nop