// File: Bartor[P]kompilator/middle/peephole/PeepholeOptimizer.cpp
// Function: PeepholeOptimizer::removeUselessStoreLoads()
bool PeepholeOptimizer::removeUselessStoreLoads() {
    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 loadInstruction = dynamic_cast<Load *>(instructions.getInstructions()[i + offsetToNonStub])) {
                if (storeInstruction->address.getAddress() == loadInstruction->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() == loadInstruction->getAddress()) {
                                canRemove = false;
                                break;
                            }
                        }
                    }

                    if (canRemove) {
                        instructions.getInstructions().erase(instructions.getInstructions().begin() + i + offsetToNonStub);

                        return true;
                    }
                }
            }
        }
    }

    return false;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xe0, %rsp
movq %rdi, -0x10(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x70(%rbp)
movl $0x0, -0x14(%rbp)
movq -0x70(%rbp), %rax
movslq -0x14(%rbp), %rcx
movq %rcx, -0x78(%rbp)
movq (%rax), %rdi
callq 0x2b160
movq %rax, %rdi
callq 0x2aba0
movq %rax, %rcx
movq -0x78(%rbp), %rax
cmpq %rcx, %rax
jae 0x2a6f9
movq -0x70(%rbp), %rax
movq (%rax), %rdi
callq 0x2b160
movq %rax, %rdi
movslq -0x14(%rbp), %rsi
callq 0x2abc0
movq (%rax), %rax
movq %rax, -0x80(%rbp)
cmpq $0x0, %rax
je 0x2a3f3
movq -0x80(%rbp), %rdi
leaq 0x16436(%rip), %rsi     # 0x40810
leaq 0x16517(%rip), %rdx     # 0x408f8
xorl %eax, %eax
movl %eax, %ecx
callq 0x62f0
movq %rax, -0x88(%rbp)
jmp 0x2a3fe
xorl %eax, %eax
movq %rax, -0x88(%rbp)
jmp 0x2a3fe
movq -0x88(%rbp), %rax
movq %rax, -0x20(%rbp)
cmpq $0x0, -0x20(%rbp)
je 0x2a6e9
movl $0x0, -0x24(%rbp)
movq -0x70(%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, -0x90(%rbp)
cmpq $0x0, %rax
je 0x2a475
movq -0x90(%rbp), %rdi
leaq 0x163b4(%rip), %rsi     # 0x40810
leaq 0x163f5(%rip), %rdx     # 0x40858
xorl %eax, %eax
movl %eax, %ecx
callq 0x62f0
movq %rax, -0x98(%rbp)
jmp 0x2a480
xorl %eax, %eax
movq %rax, -0x98(%rbp)
jmp 0x2a480
movq -0x98(%rbp), %rax
cmpq $0x0, %rax
je 0x2a4ce
movq -0x70(%rbp), %rax
movl -0x14(%rbp), %ecx
addl -0x24(%rbp), %ecx
movslq %ecx, %rcx
movq %rcx, -0xa0(%rbp)
movq (%rax), %rdi
callq 0x2b160
movq %rax, %rdi
callq 0x2aba0
movq %rax, %rcx
movq -0xa0(%rbp), %rax
cmpq %rcx, %rax
jne 0x2a4c9
movb $0x0, -0x1(%rbp)
jmp 0x2a6fd
jmp 0x2a41b
movq -0x70(%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, -0xa8(%rbp)
cmpq $0x0, %rax
je 0x2a522
movq -0xa8(%rbp), %rdi
leaq 0x16307(%rip), %rsi     # 0x40810
leaq 0x16398(%rip), %rdx     # 0x408a8
xorl %eax, %eax
movl %eax, %ecx
callq 0x62f0
movq %rax, -0xb0(%rbp)
jmp 0x2a52d
xorl %eax, %eax
movq %rax, -0xb0(%rbp)
jmp 0x2a52d
movq -0xb0(%rbp), %rax
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x30(%rbp)
je 0x2a6e7
movq -0x20(%rbp), %rax
movq 0x18(%rax), %rdi
callq 0x24490
movq %rax, -0xb8(%rbp)
movq -0x30(%rbp), %rax
movq 0x18(%rax), %rdi
callq 0x24490
movq %rax, %rcx
movq -0xb8(%rbp), %rax
cmpq %rcx, %rax
jne 0x2a6e5
movb $0x1, -0x31(%rbp)
movl $0x0, -0x38(%rbp)
movq -0x70(%rbp), %rax
movslq -0x38(%rbp), %rcx
movq %rcx, -0xc0(%rbp)
movq (%rax), %rdi
callq 0x2b160
movq %rax, %rdi
callq 0x2aba0
movq %rax, %rcx
movq -0xc0(%rbp), %rax
cmpq %rcx, %rax
jae 0x2a666
movq -0x70(%rbp), %rax
movq (%rax), %rdi
callq 0x2b160
movq %rax, %rdi
movslq -0x38(%rbp), %rsi
callq 0x2abc0
movq (%rax), %rax
movq %rax, -0xc8(%rbp)
cmpq $0x0, %rax
je 0x2a603
movq -0xc8(%rbp), %rdi
leaq 0x16226(%rip), %rsi     # 0x40810
leaq 0x164e7(%rip), %rdx     # 0x40ad8
xorl %eax, %eax
movl %eax, %ecx
callq 0x62f0
movq %rax, -0xd0(%rbp)
jmp 0x2a60e
xorl %eax, %eax
movq %rax, -0xd0(%rbp)
jmp 0x2a60e
movq -0xd0(%rbp), %rax
movq %rax, -0x40(%rbp)
cmpq $0x0, -0x40(%rbp)
je 0x2a656
movq -0x40(%rbp), %rax
movq 0x18(%rax), %rdi
movq (%rdi), %rax
callq *0x8(%rax)
movq %rax, -0xd8(%rbp)
movq -0x30(%rbp), %rdi
movq (%rdi), %rax
callq *0x8(%rax)
movq %rax, %rcx
movq -0xd8(%rbp), %rax
cmpq %rcx, %rax
jne 0x2a654
movb $0x0, -0x31(%rbp)
jmp 0x2a666
jmp 0x2a656
jmp 0x2a658
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
jmp 0x2a582
testb $0x1, -0x31(%rbp)
je 0x2a6e3
movq -0x70(%rbp), %rax
movq (%rax), %rdi
callq 0x2b160
movq %rax, %rcx
movq -0x70(%rbp), %rax
movq %rcx, -0xe0(%rbp)
movq (%rax), %rdi
callq 0x2b160
movq %rax, %rdi
callq 0x1fda0
movq %rax, -0x60(%rbp)
movslq -0x14(%rbp), %rsi
leaq -0x60(%rbp), %rdi
callq 0x2ac50
movq %rax, -0x58(%rbp)
movslq -0x24(%rbp), %rsi
leaq -0x58(%rbp), %rdi
callq 0x2ac50
movq %rax, -0x50(%rbp)
leaq -0x48(%rbp), %rdi
leaq -0x50(%rbp), %rsi
callq 0x2ac90
movq -0xe0(%rbp), %rdi
movq -0x48(%rbp), %rsi
callq 0x2abe0
movq %rax, -0x68(%rbp)
movb $0x1, -0x1(%rbp)
jmp 0x2a6fd
jmp 0x2a6e5
jmp 0x2a6e7
jmp 0x2a6e9
jmp 0x2a6eb
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
jmp 0x2a37e
movb $0x0, -0x1(%rbp)
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0xe0, %rsp
popq %rbp
retq
nopl (%rax,%rax)