// File: /workspace/github_cpp_makefile_star2_O1/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
movq (%rdi), %rdi
callq 0x1acaa
movq 0x8(%rax), %rcx
cmpq (%rax), %rcx
je 0x1a8dc
xorl %r13d, %r13d
movl %eax, -0x2c(%rbp)
movq (%rbx), %rdi
callq 0x1acaa
movq (%rax), %rax
movq (%rax,%r13,8), %rdi
testq %rdi, %rdi
je 0x1a6d7
leaq 0xd154(%rip), %rsi      # 0x27818
leaq 0xd235(%rip), %rdx      # 0x27900
xorl %ecx, %ecx
callq 0x5240
movq %rax, %r12
jmp 0x1a6da
xorl %r12d, %r12d
testq %r12, %r12
je 0x1a849
movq $-0x1, %r14
movq %r13, %r15
movq (%rbx), %rdi
callq 0x1acaa
movq (%rax), %rax
movq 0x8(%rax,%r15,8), %rdi
testq %rdi, %rdi
je 0x1a749
leaq 0xd10f(%rip), %rsi      # 0x27818
leaq 0xd150(%rip), %rdx      # 0x27860
xorl %ecx, %ecx
callq 0x5240
testq %rax, %rax
je 0x1a749
movq (%rbx), %rdi
callq 0x1acaa
movq 0x8(%rax), %rcx
subq (%rax), %rcx
sarq $0x3, %rcx
decq %r14
incq %r15
cmpq %rcx, %r15
jne 0x1a6ed
xorl %r12d, %r12d
movl $0x0, -0x2c(%rbp)
jmp 0x1a84c
movq (%rbx), %rdi
callq 0x1acaa
movq (%rax), %rax
movq 0x8(%rax,%r15,8), %rdi
testq %rdi, %rdi
je 0x1a778
leaq 0xd0b3(%rip), %rsi      # 0x27818
leaq 0xd144(%rip), %rdx      # 0x278b0
xorl %ecx, %ecx
callq 0x5240
movq %rax, %r15
jmp 0x1a77b
xorl %r15d, %r15d
testq %r15, %r15
je 0x1a849
movq 0x18(%r12), %rdi
callq 0x17d30
movq %rax, %r12
movq 0x18(%r15), %rdi
callq 0x17d30
cmpq %rax, %r12
jne 0x1a849
movq (%rbx), %rdi
callq 0x1acaa
movq 0x8(%rax), %rcx
cmpq (%rax), %rcx
je 0x1a877
movq %r15, -0x40(%rbp)
movq %r13, -0x38(%rbp)
movb $0x1, %r15b
xorl %r13d, %r13d
movq (%rbx), %rdi
callq 0x1acaa
movq (%rax), %rax
movq (%rax,%r13,8), %rdi
testq %rdi, %rdi
je 0x1a7f1
leaq 0xd037(%rip), %rsi      # 0x27818
leaq 0xd2f8(%rip), %rdx      # 0x27ae0
xorl %ecx, %ecx
callq 0x5240
jmp 0x1a7f3
xorl %eax, %eax
testq %rax, %rax
je 0x1a81c
movq 0x18(%rax), %rdi
movq (%rdi), %rax
callq *0x8(%rax)
movq %rax, %r12
movq -0x40(%rbp), %rdi
movq (%rdi), %rax
callq *0x8(%rax)
xorl %ecx, %ecx
cmpq %rax, %r12
movl $0x0, %r12d
je 0x1a821
movb $0x1, %cl
movl %r15d, %r12d
testb %cl, %cl
je 0x1a843
incq %r13
movq (%rbx), %rdi
callq 0x1acaa
movq 0x8(%rax), %rcx
subq (%rax), %rcx
sarq $0x3, %rcx
movl %r12d, %r15d
cmpq %r13, %rcx
ja 0x1a7c6
movq -0x38(%rbp), %r13
jmp 0x1a87a
movb $0x1, %r12b
testb $0x1, %r12b
je 0x1a8e0
incq %r13
movq (%rbx), %rdi
callq 0x1acaa
movq 0x8(%rax), %rcx
subq (%rax), %rcx
sarq $0x3, %rcx
cmpq %r13, %rcx
ja 0x1a6a9
jmp 0x1a8dc
movb $0x1, %r12b
testb $0x1, %r12b
je 0x1a8d3
movl %r12d, %r15d
negq %r14
movq (%rbx), %rdi
callq 0x1acaa
movq %rax, %r12
movq (%rbx), %rdi
callq 0x1acaa
leaq (,%r13,8), %rcx
addq (%rax), %rcx
movl %r14d, %eax
leaq (%rcx,%rax,8), %rsi
addq $0x8, %rsi
movq 0x8(%r12), %rdx
cmpq %rdx, %rsi
je 0x1a8c5
leaq (%rcx,%rax,8), %rdi
subq %rsi, %rdx
callq 0x5340
addq $-0x8, 0x8(%r12)
movb $0x1, %al
movl %eax, -0x2c(%rbp)
movl %r15d, %r12d
xorb $0x1, %r12b
jmp 0x1a84c
xorl %eax, %eax
jmp 0x1a8e3
movl -0x2c(%rbp), %eax
andb $0x1, %al
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq