// File: Bartor[P]kompilator/middle/abstract_assembler/AbstractAssembler.cpp
// Function: AbstractAssembler::removeUselessConstants(InstructionList&)
void AbstractAssembler::removeUselessConstants(InstructionList &instructions) {
    std::vector < Constant * > toRemove;

    for (auto const &constant : constants->getConstants()) {
        bool found = false;
        for (auto const &ins : instructions.getInstructions()) {
            if (auto addrIns = dynamic_cast<InstructionUsingAddress *>(ins)) {
                if (&addrIns->address == &constant->getAddress()) {
                    found = true;
                    break;
                }
            }
        }
        if (!found) {
            toRemove.push_back(constant);
        }
    }

    for (auto const &constant : toRemove) {
        constants->removeConstant(constant);
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xf0, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0xc8(%rbp)
leaq -0x28(%rbp), %rdi
callq 0x1fd80
movq -0xc8(%rbp), %rax
movq 0x30(%rax), %rsi
leaq -0x48(%rbp), %rdi
callq 0x1f8a0
jmp 0x1219d
leaq -0x48(%rbp), %rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rdi
callq 0x1f8d0
movq %rax, -0x60(%rbp)
movq -0x30(%rbp), %rdi
callq 0x1f900
movq %rax, -0x68(%rbp)
leaq -0x60(%rbp), %rdi
leaq -0x68(%rbp), %rsi
callq 0x1f930
testb $0x1, %al
jne 0x121ef
leaq -0x48(%rbp), %rdi
callq 0x1f9b0
jmp 0x12363
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
jmp 0x12401
leaq -0x60(%rbp), %rdi
callq 0x1f970
movq %rax, -0x70(%rbp)
movb $0x0, -0x71(%rbp)
movq -0x10(%rbp), %rdi
callq 0x2b160
movq %rax, -0xd0(%rbp)
jmp 0x12212
movq -0xd0(%rbp), %rax
movq %rax, -0x80(%rbp)
movq -0x80(%rbp), %rdi
callq 0x1fda0
movq %rax, -0x88(%rbp)
movq -0x80(%rbp), %rdi
callq 0x1fdd0
movq %rax, -0x90(%rbp)
leaq -0x88(%rbp), %rdi
leaq -0x90(%rbp), %rsi
callq 0x1fe00
testb $0x1, %al
jne 0x12259
jmp 0x1233c
leaq -0x88(%rbp), %rdi
callq 0x1fe40
movq %rax, -0x98(%rbp)
movq -0x98(%rbp), %rax
movq (%rax), %rax
movq %rax, -0xd8(%rbp)
cmpq $0x0, %rax
je 0x122c4
movq -0xd8(%rbp), %rdi
leaq 0x2e57f(%rip), %rsi     # 0x40810
leaq 0x2e398(%rip), %rdx     # 0x40630
xorl %eax, %eax
movl %eax, %ecx
callq 0x62f0
movq %rax, -0xe0(%rbp)
jmp 0x122cf
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
leaq -0x48(%rbp), %rdi
callq 0x1f9b0
jmp 0x12401
xorl %eax, %eax
movq %rax, -0xe0(%rbp)
jmp 0x122cf
movq -0xe0(%rbp), %rax
movq %rax, -0xa0(%rbp)
cmpq $0x0, -0xa0(%rbp)
je 0x12329
movq -0xa0(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0xf0(%rbp)
movq -0x70(%rbp), %rax
movq (%rax), %rdi
callq 0x230c0
movq %rax, -0xe8(%rbp)
jmp 0x1230e
movq -0xf0(%rbp), %rax
movq -0xe8(%rbp), %rcx
cmpq %rcx, %rax
jne 0x12327
movb $0x1, -0x71(%rbp)
jmp 0x1233c
jmp 0x12329
jmp 0x1232b
leaq -0x88(%rbp), %rdi
callq 0x1fe60
jmp 0x1223d
testb $0x1, -0x71(%rbp)
jne 0x12353
movq -0x70(%rbp), %rsi
leaq -0x28(%rbp), %rdi
callq 0x1fe80
jmp 0x12351
jmp 0x12353
jmp 0x12355
leaq -0x60(%rbp), %rdi
callq 0x1f990
jmp 0x121bf
leaq -0x28(%rbp), %rax
movq %rax, -0xa8(%rbp)
movq -0xa8(%rbp), %rdi
callq 0x1f8d0
movq %rax, -0xb0(%rbp)
movq -0xa8(%rbp), %rdi
callq 0x1f900
movq %rax, -0xb8(%rbp)
leaq -0xb0(%rbp), %rdi
leaq -0xb8(%rbp), %rsi
callq 0x1f930
testb $0x1, %al
jne 0x123ad
jmp 0x123ef
leaq -0xb0(%rbp), %rdi
callq 0x1f970
movq %rax, %rcx
movq -0xc8(%rbp), %rax
movq %rcx, -0xc0(%rbp)
movq 0x30(%rax), %rdi
movq -0xc0(%rbp), %rax
movq (%rax), %rsi
callq 0x235f0
jmp 0x123df
jmp 0x123e1
leaq -0xb0(%rbp), %rdi
callq 0x1f990
jmp 0x12394
leaq -0x28(%rbp), %rdi
callq 0x1f9b0
addq $0xf0, %rsp
popq %rbp
retq
leaq -0x28(%rbp), %rdi
callq 0x1f9b0
movq -0x50(%rbp), %rdi
callq 0x64d0
nopw %cs:(%rax,%rax)