// File: /workspace/github_cpp_makefile_star2_O2/Merulo[P]CompilerJFTT/src/IRs/SecondIR.cpp
// Function: SecondIR::removeUnusedIterators()
void SecondIR::removeUnusedIterators()
{
    for(auto& b : _blocks)
    {
        if (b.blockName.find("_initialForControl") != std::string::npos)
        {
            auto lastLine = b.lines.back();
            std::string iteratorName = lastLine.one;
            iteratorName = iteratorName.substr(0, iteratorName.find(_symbolTable->getForControl()));

            Block& nextBlock = getBlockByName(b.blockIfFalse, _blocks);
            std::string name = b.blockName;
            name = name.substr(0, name.find_last_of("_"));
            name = name + "_exitFor";
            Block& endingBlock = getBlockByName(name, _blocks);
            resetBlocks();
            bool result = isUsedInEndingBlock(iteratorName, nextBlock, endingBlock);
            
            if (!result)
            {
                continue;
            }
            stripVariable(iteratorName, endingBlock);
            stripForIterator(iteratorName, b);
        }
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x178, %rsp            # imm = 0x178
movq %rdi, %rbx
leaq 0x10(%rdi), %rax
movq %rax, (%rsp)
movq 0x10(%rdi), %r15
movq 0x18(%rdi), %rax
movq %rax, 0x8(%rsp)
cmpq 0x8(%rsp), %r15
je 0x14cab
movq %r15, %rdi
leaq 0xea22(%rip), %rsi      # 0x234b4
xorl %edx, %edx
callq 0x4520
cmpq $-0x1, %rax
je 0x14c9f
movq 0x28(%r15), %rsi
addq $-0x68, %rsi
leaq 0x110(%rsp), %rdi
callq 0x8a26
leaq 0x10(%rsp), %rdi
leaq 0x130(%rsp), %rsi
callq 0x4390
movq (%rbx), %rsi
leaq 0x90(%rsp), %r12
movq %r12, %rdi
callq 0x108a6
leaq 0x10(%rsp), %r14
movq %r14, %rdi
movq %r12, %rsi
xorl %edx, %edx
callq 0x4530
leaq 0x30(%rsp), %r13
movq %r13, %rdi
movq %r14, %rsi
xorl %edx, %edx
movq %rax, %rcx
callq 0x42a0
movq %r14, %rdi
movq %r13, %rsi
callq 0x4340
movq %r13, %rdi
callq 0x4548
movq %r12, %rdi
callq 0x4548
leaq 0x58(%r15), %rsi
leaq 0xf0(%rsp), %r12
movq %r12, %rdi
callq 0x4390
movq %rbx, %rdi
movq %r12, %rsi
movq (%rsp), %rdx
callq 0x1b1b0
movq %rax, %r14
movq %r12, %rdi
callq 0x4548
leaq 0x30(%rsp), %rbp
movq %rbp, %rdi
movq %r15, %rsi
callq 0x4390
movq %rbp, %rdi
leaq 0xe613(%rip), %rsi      # 0x2317a
pushq $-0x1
popq %rdx
callq 0x4260
leaq 0x90(%rsp), %r12
movq %r12, %rdi
movq %rbp, %rsi
xorl %edx, %edx
movq %rax, %rcx
callq 0x42a0
movq %rbx, %r13
movq %rbp, %rdi
movq %r12, %rsi
callq 0x4340
movq %r12, %rdi
callq 0x4548
movq %r12, %rdi
movq %rbp, %rsi
leaq 0xe91d(%rip), %rdx      # 0x234c7
callq 0x13a99
movq %rbp, %rdi
movq %r12, %rsi
callq 0x4340
movq %r12, %rdi
callq 0x4548
leaq 0xd0(%rsp), %rbx
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x4390
movq %r13, %rbp
movq %r13, %rdi
movq %rbx, %rsi
movq (%rsp), %rdx
callq 0x1b1b0
movq %rax, %r12
movq %rbx, %rdi
callq 0x4548
movq %rbp, %rdi
callq 0x1b192
leaq 0xb0(%rsp), %rbp
movq %rbp, %rdi
leaq 0x10(%rsp), %rsi
callq 0x4390
movq %r13, %rbx
movq %r13, %rdi
movq %rbp, %rsi
movq %r14, %rdx
movq %r12, %rcx
callq 0x14f50
movl %eax, %r14d
movq %rbp, %rdi
callq 0x4548
testb %r14b, %r14b
je 0x14c7e
leaq 0x70(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0x4390
leaq 0x70(%rsp), %r14
movq %r14, %rsi
movq %r12, %rdx
callq 0x15246
movq %r14, %rdi
callq 0x4548
leaq 0x50(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0x4390
movq %rbx, %rdi
movq %r15, %rdx
callq 0x152a8
leaq 0x50(%rsp), %rdi
callq 0x4548
leaq 0x30(%rsp), %rdi
callq 0x4548
leaq 0x10(%rsp), %rdi
callq 0x4548
leaq 0x110(%rsp), %rdi
callq 0x81fe
addq $0xa0, %r15
jmp 0x14a7d
addq $0x178, %rsp            # imm = 0x178
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq 0x70(%rsp), %rdi
jmp 0x14d0e
movq %rax, %rbx
leaq 0x50(%rsp), %rdi
jmp 0x14d0e
movq %rax, %rbx
leaq 0x90(%rsp), %rdi
jmp 0x14d26
jmp 0x14d1e
jmp 0x14d17
jmp 0x14d1e
movq %rax, %rbx
leaq 0xb0(%rsp), %rdi
jmp 0x14d0e
movq %rax, %rbx
jmp 0x14d35
movq %rax, %rbx
leaq 0xf0(%rsp), %rdi
jmp 0x14d26
movq %rax, %rbx
leaq 0xd0(%rsp), %rdi
callq 0x4548
jmp 0x14d21
jmp 0x14d17
movq %rax, %rbx
jmp 0x14d2b
jmp 0x14d1e
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
callq 0x4548
leaq 0x10(%rsp), %rdi
callq 0x4548
leaq 0x110(%rsp), %rdi
callq 0x81fe
movq %rbx, %rdi
callq 0x4490