// File: /workspace/github_cpp_makefile_star2_O1/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 (%rdi), %rdi
callq 0x1acaa
movq 0x8(%rax), %rcx
cmpq (%rax), %rcx
je 0x1abf6
movabsq $0x100000000, %r15      # imm = 0x100000000
xorl %r13d, %r13d
leaq 0xcfe7(%rip), %rax      # 0x27918
addq $0x10, %rax
movq %rax, -0x50(%rbp)
xorl %r14d, %r14d
movl %eax, -0x2c(%rbp)
movq (%rbx), %rdi
callq 0x1acaa
movq (%rax), %rax
movq (%rax,%r14,8), %rdi
testq %rdi, %rdi
je 0x1a96a
leaq 0xcebe(%rip), %rsi      # 0x27818
leaq 0xcf9f(%rip), %rdx      # 0x27900
xorl %ecx, %ecx
callq 0x5240
jmp 0x1a96c
xorl %eax, %eax
movb $0x1, %dl
testq %rax, %rax
je 0x1abcf
movq %rax, -0x48(%rbp)
movq %r14, -0x38(%rbp)
movq %r13, %r12
movq (%rbx), %rdi
callq 0x1acaa
movq (%rax), %rax
movq 0x8(%rax,%r14,8), %rdi
testq %rdi, %rdi
je 0x1a9dd
leaq 0xce7a(%rip), %rsi      # 0x27818
leaq 0xcebb(%rip), %rdx      # 0x27860
xorl %ecx, %ecx
callq 0x5240
testq %rax, %rax
je 0x1a9dd
movq (%rbx), %rdi
callq 0x1acaa
movq 0x8(%rax), %rcx
subq (%rax), %rcx
sarq $0x3, %rcx
addq %r15, %r12
incq %r14
cmpq %rcx, %r14
jne 0x1a982
xorl %edx, %edx
movl $0x0, -0x2c(%rbp)
jmp 0x1abcb
movq (%rbx), %rdi
callq 0x1acaa
movq (%rax), %rax
movq 0x8(%rax,%r14,8), %rdi
testq %rdi, %rdi
je 0x1aa0c
leaq 0xce1f(%rip), %rsi      # 0x27818
leaq 0xcf50(%rip), %rdx      # 0x27950
xorl %ecx, %ecx
callq 0x5240
movq %rax, %r14
jmp 0x1aa0f
xorl %r14d, %r14d
movb $0x1, %dl
testq %r14, %r14
je 0x1abcb
movq %r13, -0x40(%rbp)
movq -0x48(%rbp), %rax
movq 0x18(%rax), %rdi
callq 0x17d30
movq %rax, %r13
movq 0x18(%r14), %rdi
callq 0x17d30
cmpq %rax, %r13
jne 0x1ab1b
movq (%rbx), %rdi
callq 0x1acaa
movq 0x8(%rax), %rcx
cmpq (%rax), %rcx
je 0x1ab2a
movq %r14, -0x58(%rbp)
movb $0x1, %r14b
xorl %r15d, %r15d
movq (%rbx), %rdi
callq 0x1acaa
movq (%rax), %rax
movq (%rax,%r15,8), %rdi
testq %rdi, %rdi
movb %r14b, -0x30(%rbp)
je 0x1aa91
leaq 0xcd9a(%rip), %rsi      # 0x27818
leaq 0xd05b(%rip), %rdx      # 0x27ae0
xorl %ecx, %ecx
callq 0x5240
movq %rax, %r13
jmp 0x1aa94
xorl %r13d, %r13d
testq %r13, %r13
je 0x1aadf
movq 0x18(%r13), %rdi
movq (%rdi), %rax
callq *0x8(%rax)
movq %rax, %r14
movq -0x58(%rbp), %rdi
movq (%rdi), %rax
callq *0x8(%rax)
xorl %edx, %edx
movl $0x0, %ecx
cmpq %rax, %r14
je 0x1aae6
movq 0x18(%r13), %rdi
movq (%rdi), %rax
callq *0x8(%rax)
movq %rax, %r14
movq -0x48(%rbp), %rdi
movq (%rdi), %rax
callq *0x8(%rax)
xorl %edx, %edx
movl $0x0, %ecx
cmpq %rax, %r14
je 0x1aae6
movb $0x1, %cl
movb -0x30(%rbp), %al
movl %eax, %edx
testb %cl, %cl
je 0x1ab0f
incq %r15
movq (%rbx), %rdi
movl %edx, %r14d
callq 0x1acaa
movl %r14d, %edx
movq 0x8(%rax), %rcx
subq (%rax), %rcx
sarq $0x3, %rcx
cmpq %r15, %rcx
ja 0x1aa5f
movabsq $0x100000000, %r15      # imm = 0x100000000
jmp 0x1ab2c
movq -0x40(%rbp), %r13
movq -0x38(%rbp), %r14
movb $0x1, %dl
jmp 0x1abcf
movb $0x1, %dl
testb $0x1, %dl
movq -0x40(%rbp), %r13
je 0x1abc8
movl %edx, -0x30(%rbp)
movq (%rbx), %rdi
callq 0x1acaa
movq %rax, %r14
movq (%rbx), %rdi
callq 0x1acaa
movq (%rax), %rax
movq -0x38(%rbp), %rcx
leaq (%rax,%rcx,8), %rsi
addq $0x8, %rsi
movq 0x8(%r14), %rdx
cmpq %rdx, %rsi
je 0x1ab73
leaq (%rax,%rcx,8), %rdi
subq %rsi, %rdx
callq 0x5340
addq $-0x8, 0x8(%r14)
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)
movq -0x50(%rbp), %rax
movq %rax, (%r14)
movq (%rbx), %rdi
callq 0x1acaa
movq (%rax), %rax
sarq $0x1d, %r12
movq %r14, (%rax,%r12)
movb $0x1, %al
movl %eax, -0x2c(%rbp)
movl -0x30(%rbp), %edx
xorb $0x1, %dl
movq -0x38(%rbp), %r14
testb $0x1, %dl
je 0x1abfa
incq %r14
movq (%rbx), %rdi
callq 0x1acaa
movq 0x8(%rax), %rcx
subq (%rax), %rcx
sarq $0x3, %rcx
addq %r15, %r13
cmpq %r14, %rcx
ja 0x1a93f
xorl %eax, %eax
jmp 0x1abfd
movl -0x2c(%rbp), %eax
andb $0x1, %al
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movl $0x20, %esi
movq %r14, %rdi
callq 0x5200
movq %rbx, %rdi
callq 0x53d0
nop