// File: /workspace/github_cpp_makefile_star2_O3/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
pushq %rax
movq %rdi, %rbx
movq (%rdi), %rdi
callq 0x1a8b0
movq 0x8(%rax), %rcx
cmpq (%rax), %rcx
je 0x1a4ff
xorl %r12d, %r12d
movq (%rbx), %rdi
callq 0x1a8b0
movq (%rax), %rax
movq (%rax,%r12,8), %rdi
testq %rdi, %rdi
je 0x1a4e0
leaq 0xc486(%rip), %rsi      # 0x26818
leaq 0xc567(%rip), %rdx      # 0x26900
xorl %ecx, %ecx
callq 0x5240
testq %rax, %rax
je 0x1a4e0
movq %rax, %r13
movq $-0x1, %r15
movq %r12, %r14
movq (%rbx), %rdi
callq 0x1a8b0
movq (%rax), %rax
movq 0x8(%rax,%r14,8), %rdi
testq %rdi, %rdi
je 0x1a408
leaq 0xc446(%rip), %rsi      # 0x26818
leaq 0xc487(%rip), %rdx      # 0x26860
xorl %ecx, %ecx
callq 0x5240
testq %rax, %rax
je 0x1a408
movq (%rbx), %rdi
callq 0x1a8b0
movq 0x8(%rax), %rcx
subq (%rax), %rcx
sarq $0x3, %rcx
decq %r15
incq %r14
cmpq %rcx, %r14
jne 0x1a3b6
jmp 0x1a503
movq (%rbx), %rdi
callq 0x1a8b0
movq (%rax), %rax
movq 0x8(%rax,%r14,8), %rdi
testq %rdi, %rdi
je 0x1a4e0
leaq 0xc3f0(%rip), %rsi      # 0x26818
leaq 0xc481(%rip), %rdx      # 0x268b0
xorl %ecx, %ecx
callq 0x5240
movq %rax, -0x30(%rbp)
testq %rax, %rax
je 0x1a4e0
movq 0x18(%r13), %rdi
callq 0x17aec
movq %rax, %r13
movq -0x30(%rbp), %rax
movq 0x18(%rax), %rdi
callq 0x17aec
cmpq %rax, %r13
jne 0x1a4e0
movq (%rbx), %rdi
callq 0x1a8b0
movq 0x8(%rax), %rcx
cmpq (%rax), %rcx
je 0x1a509
xorl %r14d, %r14d
movq (%rbx), %rdi
callq 0x1a8b0
movq (%rax), %rax
movq (%rax,%r14,8), %rdi
testq %rdi, %rdi
je 0x1a4c3
leaq 0xc384(%rip), %rsi      # 0x26818
leaq 0xc645(%rip), %rdx      # 0x26ae0
xorl %ecx, %ecx
callq 0x5240
testq %rax, %rax
je 0x1a4c3
movq 0x18(%rax), %rdi
movq (%rdi), %rax
callq *0x8(%rax)
movq %rax, %r13
movq -0x30(%rbp), %rdi
movq (%rdi), %rax
callq *0x8(%rax)
cmpq %rax, %r13
je 0x1a4e0
incq %r14
movq (%rbx), %rdi
callq 0x1a8b0
movq 0x8(%rax), %rcx
subq (%rax), %rcx
sarq $0x3, %rcx
cmpq %r14, %rcx
ja 0x1a479
jmp 0x1a509
incq %r12
movq (%rbx), %rdi
callq 0x1a8b0
movq 0x8(%rax), %rcx
subq (%rax), %rcx
sarq $0x3, %rcx
cmpq %r12, %rcx
ja 0x1a373
xorl %eax, %eax
jmp 0x1a553
movb $0x1, %al
xorl %ecx, %ecx
jmp 0x1a553
movq (%rbx), %rdi
callq 0x1a8b0
movq %rax, %r14
movq (%rbx), %rdi
callq 0x1a8b0
shlq $0x3, %r12
addq (%rax), %r12
negl %r15d
leaq (%r12,%r15,8), %rsi
addq $0x8, %rsi
movq 0x8(%r14), %rdx
cmpq %rdx, %rsi
je 0x1a547
leaq (%r12,%r15,8), %rdi
subq %rsi, %rdx
callq 0x5340
movq 0x8(%r14), %rdx
addq $-0x8, %rdx
movq %rdx, 0x8(%r14)
movb $0x1, %al
movb $0x1, %cl
andb %cl, %al
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq