// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rdi, %rbx
xorl %r14d, %r14d
movq (%rbx), %rdi
callq 0x14d9e
movq 0x8(%rax), %r15
subq (%rax), %r15
sarq $0x3, %r15
cmpq %r14, %r15
jbe 0x14a35
movq (%rbx), %rdi
callq 0x14d9e
movq (%rax), %rax
movq %r14, -0x30(%rbp)
movq (%rax,%r14,8), %rdi
testq %rdi, %rdi
je 0x149f2
leaq 0xaf80(%rip), %rsi      # 0x1f820
leaq 0xb061(%rip), %rdx      # 0x1f908
xorl %ecx, %ecx
callq 0x51f0
testq %rax, %rax
je 0x149f2
movq %rax, %r13
movq -0x30(%rbp), %r12
pushq $-0x1
popq %r14
movq (%rbx), %rdi
callq 0x14d9e
movq (%rax), %rax
movq 0x8(%rax,%r12,8), %rdi
testq %rdi, %rdi
je 0x14914
leaq 0xaf42(%rip), %rsi      # 0x1f820
leaq 0xaf83(%rip), %rdx      # 0x1f868
xorl %ecx, %ecx
callq 0x51f0
testq %rax, %rax
je 0x14914
movq (%rbx), %rdi
callq 0x14d9e
movq 0x8(%rax), %rcx
subq (%rax), %rcx
sarq $0x3, %rcx
decq %r14
incq %r12
cmpq %rcx, %r12
jne 0x148c2
jmp 0x149fe
movq (%rbx), %rdi
callq 0x14d9e
movq (%rax), %rax
movq 0x8(%rax,%r12,8), %rdi
testq %rdi, %rdi
je 0x149f2
leaq 0xaeec(%rip), %rsi      # 0x1f820
leaq 0xaf7d(%rip), %rdx      # 0x1f8b8
xorl %ecx, %ecx
callq 0x51f0
movq %rax, -0x40(%rbp)
testq %rax, %rax
je 0x149f2
movq 0x18(%r13), %rdi
callq 0x12a60
movq %rax, %r13
movq -0x40(%rbp), %rax
movq 0x18(%rax), %rdi
callq 0x12a60
cmpq %rax, %r13
jne 0x149f2
movq %rbx, -0x38(%rbp)
xorl %ebx, %ebx
movq -0x38(%rbp), %r13
movq (%r13), %rdi
callq 0x14d9e
movq 0x8(%rax), %r12
subq (%rax), %r12
sarq $0x3, %r12
movq (%r13), %rdi
callq 0x14d9e
movq %rax, %r13
cmpq %rbx, %r12
jbe 0x14a06
movq %rbx, %r12
movq (%r13), %rax
movq (%rax,%rbx,8), %rdi
testq %rdi, %rdi
movq -0x38(%rbp), %rbx
je 0x149ea
leaq 0xae65(%rip), %rsi      # 0x1f820
leaq 0xb126(%rip), %rdx      # 0x1fae8
xorl %ecx, %ecx
callq 0x51f0
testq %rax, %rax
je 0x149ea
movq 0x18(%rax), %rdi
movq (%rdi), %rax
callq *0x8(%rax)
movq %rax, %r13
movq -0x40(%rbp), %rdi
movq (%rdi), %rax
callq *0x8(%rax)
cmpq %rax, %r13
je 0x149f2
incq %r12
movq %r12, %rbx
jmp 0x14977
movq -0x30(%rbp), %r14
incq %r14
jmp 0x14861
xorl %ecx, %ecx
movq -0x30(%rbp), %r14
jmp 0x14a35
movq -0x38(%rbp), %rax
movq (%rax), %rdi
callq 0x14d9e
movq -0x30(%rbp), %rdx
leaq (,%rdx,8), %rcx
addq (%rax), %rcx
negl %r14d
leaq (%rcx,%r14,8), %rsi
movq %rdx, %r14
movq %r13, %rdi
callq 0x14cf6
movb $0x1, %cl
cmpq %r14, %r15
seta %al
andb %cl, %al
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq