// File: /workspace/github_cpp_makefile_star2_O3/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 $0x28, %rsp
movq %rdi, %rbx
movq (%rdi), %rdi
callq 0x1a8b0
movq 0x8(%rax), %rcx
cmpq (%rax), %rcx
je 0x1a767
movq $0x0, -0x40(%rbp)
xorl %r14d, %r14d
movq (%rbx), %rdi
callq 0x1a8b0
movq (%rax), %rax
movq (%rax,%r14,8), %rdi
testq %rdi, %rdi
je 0x1a73a
leaq 0xc261(%rip), %rsi      # 0x26818
leaq 0xc342(%rip), %rdx      # 0x26900
xorl %ecx, %ecx
callq 0x5240
movq %rax, -0x38(%rbp)
testq %rax, %rax
je 0x1a73a
movq %r14, %r15
movq -0x40(%rbp), %r13
movabsq $0x100000000, %r12      # imm = 0x100000000
movq (%rbx), %rdi
callq 0x1a8b0
movq (%rax), %rax
movq 0x8(%rax,%r15,8), %rdi
testq %rdi, %rdi
je 0x1a635
leaq 0xc219(%rip), %rsi      # 0x26818
leaq 0xc25a(%rip), %rdx      # 0x26860
xorl %ecx, %ecx
callq 0x5240
testq %rax, %rax
je 0x1a635
movq (%rbx), %rdi
callq 0x1a8b0
movq 0x8(%rax), %rcx
subq (%rax), %rcx
sarq $0x3, %rcx
addq %r12, %r13
incq %r15
cmpq %rcx, %r15
jne 0x1a5e3
jmp 0x1a76b
movq (%rbx), %rdi
callq 0x1a8b0
movq (%rax), %rax
movq 0x8(%rax,%r15,8), %rdi
testq %rdi, %rdi
je 0x1a73a
leaq 0xc1c3(%rip), %rsi      # 0x26818
leaq 0xc2f4(%rip), %rdx      # 0x26950
xorl %ecx, %ecx
callq 0x5240
movq %rax, -0x30(%rbp)
testq %rax, %rax
je 0x1a73a
movq -0x38(%rbp), %rax
movq 0x18(%rax), %rdi
callq 0x17aec
movq %rax, %r15
movq -0x30(%rbp), %rax
movq 0x18(%rax), %rdi
callq 0x17aec
cmpq %rax, %r15
jne 0x1a73a
movq (%rbx), %rdi
callq 0x1a8b0
movq 0x8(%rax), %rcx
cmpq (%rax), %rcx
je 0x1a780
xorl %r12d, %r12d
movq (%rbx), %rdi
callq 0x1a8b0
movq (%rax), %rax
movq (%rax,%r12,8), %rdi
testq %rdi, %rdi
je 0x1a719
leaq 0xc14f(%rip), %rsi      # 0x26818
leaq 0xc410(%rip), %rdx      # 0x26ae0
xorl %ecx, %ecx
callq 0x5240
testq %rax, %rax
je 0x1a719
movq %rax, %r15
movq 0x18(%rax), %rdi
movq (%rdi), %rax
callq *0x8(%rax)
movq %rax, -0x48(%rbp)
movq -0x30(%rbp), %rdi
movq (%rdi), %rax
callq *0x8(%rax)
cmpq %rax, -0x48(%rbp)
je 0x1a73a
movq 0x18(%r15), %rdi
movq (%rdi), %rax
callq *0x8(%rax)
movq %rax, %r15
movq -0x38(%rbp), %rdi
movq (%rdi), %rax
callq *0x8(%rax)
cmpq %rax, %r15
je 0x1a73a
incq %r12
movq (%rbx), %rdi
callq 0x1a8b0
movq 0x8(%rax), %rcx
subq (%rax), %rcx
sarq $0x3, %rcx
cmpq %r12, %rcx
ja 0x1a6ae
jmp 0x1a780
incq %r14
movq (%rbx), %rdi
callq 0x1a8b0
movq 0x8(%rax), %rcx
subq (%rax), %rcx
sarq $0x3, %rcx
movabsq $0x100000000, %rax      # imm = 0x100000000
addq %rax, -0x40(%rbp)
cmpq %r14, %rcx
ja 0x1a598
xorl %eax, %eax
jmp 0x1a76f
movb $0x1, %al
xorl %ecx, %ecx
andb %cl, %al
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq (%rbx), %rdi
callq 0x1a8b0
movq %rax, %r15
movq (%rbx), %rdi
callq 0x1a8b0
movq (%rax), %rax
leaq (%rax,%r14,8), %rsi
addq $0x8, %rsi
movq 0x8(%r15), %rdx
cmpq %rdx, %rsi
je 0x1a7b7
leaq (%rax,%r14,8), %rdi
subq %rsi, %rdx
callq 0x5340
movq 0x8(%r15), %rdx
addq $-0x8, %rdx
movq %rdx, 0x8(%r15)
movl $0x20, %edi
callq 0x51f0
movq %rax, %r14
movl $0x10, %edi
callq 0x51f0
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
movq $-0x1, 0x8(%r14)
movb $0x0, 0x10(%r14)
movq %rax, 0x18(%r14)
leaq 0xc124(%rip), %rax      # 0x26918
addq $0x10, %rax
movq %rax, (%r14)
movq (%rbx), %rdi
callq 0x1a8b0
movq (%rax), %rax
sarq $0x1d, %r13
movq %r14, (%rax,%r13)
movb $0x1, %al
movb $0x1, %cl
jmp 0x1a76f
movq %rax, %rbx
movl $0x20, %esi
movq %r14, %rdi
callq 0x5200
movq %rbx, %rdi
callq 0x53d0
nop