// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rdi, %rbx
movq $0x0, -0x50(%rbp)
xorl %r12d, %r12d
movq (%rbx), %rdi
callq 0x14d9e
movq 0x8(%rax), %r14
subq (%rax), %r14
sarq $0x3, %r14
cmpq %r12, %r14
jbe 0x14c53
movq (%rbx), %rdi
callq 0x14d9e
movq (%rax), %rax
movq %r12, -0x30(%rbp)
movq (%rax,%r12,8), %rdi
testq %rdi, %rdi
je 0x14c37
leaq 0xad76(%rip), %rsi      # 0x1f820
leaq 0xae57(%rip), %rdx      # 0x1f908
xorl %ecx, %ecx
callq 0x51f0
movq %rax, -0x48(%rbp)
testq %rax, %rax
je 0x14c37
movq -0x30(%rbp), %r12
movq %r12, %r15
movq -0x50(%rbp), %r13
movq (%rbx), %rdi
callq 0x14d9e
movq (%rax), %rax
movq 0x8(%rax,%r15,8), %rdi
testq %rdi, %rdi
je 0x14b2c
leaq 0xad34(%rip), %rsi      # 0x1f820
leaq 0xad75(%rip), %rdx      # 0x1f868
xorl %ecx, %ecx
callq 0x51f0
testq %rax, %rax
je 0x14b2c
movq (%rbx), %rdi
callq 0x14d9e
movq 0x8(%rax), %rcx
subq (%rax), %rcx
sarq $0x3, %rcx
movabsq $0x100000000, %rax      # imm = 0x100000000
addq %rax, %r13
incq %r15
cmpq %rcx, %r15
jne 0x14ad0
jmp 0x14c51
movq (%rbx), %rdi
callq 0x14d9e
movq (%rax), %rax
movq 0x8(%rax,%r15,8), %rdi
testq %rdi, %rdi
je 0x14c37
leaq 0xacd4(%rip), %rsi      # 0x1f820
leaq 0xae05(%rip), %rdx      # 0x1f958
xorl %ecx, %ecx
callq 0x51f0
movq %rax, -0x40(%rbp)
testq %rax, %rax
je 0x14c37
movq -0x48(%rbp), %rax
movq 0x18(%rax), %rdi
callq 0x12a60
movq %rax, %r12
movq -0x40(%rbp), %rax
movq 0x18(%rax), %rdi
callq 0x12a60
cmpq %rax, %r12
jne 0x14c37
movq %rbx, -0x38(%rbp)
xorl %ebx, %ebx
movq -0x38(%rbp), %r12
movq (%r12), %rdi
callq 0x14d9e
movq 0x8(%rax), %r15
subq (%rax), %r15
sarq $0x3, %r15
movq (%r12), %rdi
callq 0x14d9e
movq %rax, %r12
cmpq %rbx, %r15
jbe 0x14c6a
movq %rbx, %r15
movq (%r12), %rax
movq (%rax,%rbx,8), %rdi
testq %rdi, %rdi
movq -0x38(%rbp), %rbx
je 0x14c2c
leaq 0xac45(%rip), %rsi      # 0x1f820
leaq 0xaf06(%rip), %rdx      # 0x1fae8
xorl %ecx, %ecx
callq 0x51f0
testq %rax, %rax
je 0x14c2c
movq %rax, %r12
movq 0x18(%rax), %rdi
movq (%rdi), %rax
callq *0x8(%rax)
movq %rax, -0x58(%rbp)
movq -0x40(%rbp), %rdi
movq (%rdi), %rax
callq *0x8(%rax)
cmpq %rax, -0x58(%rbp)
je 0x14c37
movq 0x18(%r12), %rdi
movq (%rdi), %rax
callq *0x8(%rax)
movq %rax, %r12
movq -0x48(%rbp), %rdi
movq (%rdi), %rax
callq *0x8(%rax)
cmpq %rax, %r12
je 0x14c37
incq %r15
movq %r15, %rbx
jmp 0x14b93
movq -0x30(%rbp), %r12
incq %r12
movabsq $0x100000000, %rax      # imm = 0x100000000
addq %rax, -0x50(%rbp)
jmp 0x14a6b
xorl %ecx, %ecx
cmpq %r12, %r14
seta %al
andb %cl, %al
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq -0x38(%rbp), %rbx
movq (%rbx), %rdi
callq 0x14d9e
movq -0x30(%rbp), %rcx
leaq (,%rcx,8), %rsi
addq (%rax), %rsi
movq %r12, %rdi
callq 0x14cf6
pushq $0x20
popq %rdi
callq 0x51c0
movq %rax, %r15
pushq $0x10
popq %rdi
callq 0x51c0
orq $-0x1, 0x8(%r15)
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
movb $0x0, 0x10(%r15)
movq %rax, 0x18(%r15)
leaq 0xac65(%rip), %rax      # 0x1f920
addq $0x10, %rax
movq %rax, (%r15)
movq (%rbx), %rdi
callq 0x14d9e
movq (%rax), %rax
sarq $0x1d, %r13
movq %r15, (%rax,%r13)
movb $0x1, %cl
movq -0x30(%rbp), %r12
jmp 0x14c53
movq %rax, %rbx
pushq $0x20
popq %rsi
movq %r15, %rdi
callq 0x51d0
movq %rbx, %rdi
callq 0x5390