// File: Merulo[P]CompilerJFTT/src/IRs/SecondIR.cpp
// Function: SecondIR::checkIfThisCanBeRemoved(std::vector<Line, std::allocator<Line>>&, __gnu_cxx::__normal_iterator<Line*, std::vector<Line, std::allocator<Line>>>&)
void SecondIR::checkIfThisCanBeRemoved(std::vector<Line>& lines, std::vector<Line>::iterator& iterator)
{
    if (iterator->operation != "ADD" && iterator->operation != "SUB")
    {
        return;
    }
    std::string operation = iterator->operation;
    std::string arg = iterator->one;
    if (!_symbolTable->isConst(iterator->two))
    {
        return;
    }
    auto constString = _symbolTable->getConstValue(iterator->two);
    if (constString == "NAN")
    {
        return;
    }
    unsigned long long value = std::stoull(constString);
    if (value > 15)
    {
        return;
    }
    if ((iterator-1)->operation == "CONST")
    {
        iterator = lines.erase(iterator - 1);
    }
    else
    {
        iterator = lines.erase(iterator - 2);
        iterator++;
    }

    iterator = lines.erase(iterator);
    iterator--;

    Line l;
    l.operation = "DEC";
    if (operation == "ADD")
    {
        l.operation = "INC";
    }
    l.one = arg;
    for(unsigned long long i = 0; i < value; i++)
    {
        iterator++;
        iterator = lines.insert(iterator, l);
    }
}

// Assembly:
subq $0x238, %rsp            # imm = 0x238
movq %rdi, 0x230(%rsp)
movq %rsi, 0x228(%rsp)
movq %rdx, 0x220(%rsp)
movq 0x230(%rsp), %rax
movq %rax, 0x78(%rsp)
movq 0x220(%rsp), %rdi
callq 0x30d40
movq %rax, %rdi
leaq 0x229b7(%rip), %rsi     # 0x510da
callq 0x310a0
testb $0x1, %al
jne 0x2e72e
jmp 0x2e755
movq 0x220(%rsp), %rdi
callq 0x30d40
movq %rax, %rdi
leaq 0x22989(%rip), %rsi     # 0x510ce
callq 0x310a0
testb $0x1, %al
jne 0x2e750
jmp 0x2e755
jmp 0x2ecc9
movq 0x220(%rsp), %rdi
callq 0x30d40
movq %rax, %rsi
leaq 0x200(%rsp), %rdi
callq 0x4180
movq 0x220(%rsp), %rdi
callq 0x30d40
movq %rax, %rsi
addq $0x20, %rsi
leaq 0x1e0(%rsp), %rdi
callq 0x4180
jmp 0x2e795
movq 0x78(%rsp), %rdi
callq 0xdde0
movq %rax, 0x70(%rsp)
movq 0x220(%rsp), %rdi
callq 0x30d40
movq %rax, %rsi
addq $0x40, %rsi
leaq 0x1b0(%rsp), %rdi
callq 0x4180
jmp 0x2e7c7
movq 0x70(%rsp), %rdi
leaq 0x1b0(%rsp), %rsi
callq 0x20600
movb %al, 0x6f(%rsp)
jmp 0x2e7df
movb 0x6f(%rsp), %al
xorb $-0x1, %al
movb %al, 0x6e(%rsp)
leaq 0x1b0(%rsp), %rdi
callq 0x46e8
movb 0x6e(%rsp), %al
testb $0x1, %al
jne 0x2e800
jmp 0x2e868
movl $0x1, 0x1ac(%rsp)
jmp 0x2ecaf
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1d8(%rsp)
movl %eax, 0x1d4(%rsp)
jmp 0x2eceb
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1d8(%rsp)
movl %eax, 0x1d4(%rsp)
jmp 0x2ecde
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1d8(%rsp)
movl %eax, 0x1d4(%rsp)
leaq 0x1b0(%rsp), %rdi
callq 0x46e8
jmp 0x2ecde
movq 0x78(%rsp), %rdi
callq 0xdde0
movq %rax, 0x60(%rsp)
movq 0x220(%rsp), %rdi
callq 0x30d40
movq %rax, %rsi
addq $0x40, %rsi
leaq 0x168(%rsp), %rdi
callq 0x4180
jmp 0x2e89a
movq 0x60(%rsp), %rsi
leaq 0x188(%rsp), %rdi
leaq 0x168(%rsp), %rdx
callq 0x21240
jmp 0x2e8b6
leaq 0x168(%rsp), %rdi
callq 0x46e8
leaq 0x22bea(%rip), %rsi     # 0x514b4
leaq 0x188(%rsp), %rdi
callq 0xdd10
movb %al, 0x5f(%rsp)
jmp 0x2e8dd
movb 0x5f(%rsp), %al
testb $0x1, %al
jne 0x2e8e7
jmp 0x2e936
movl $0x1, 0x1ac(%rsp)
jmp 0x2eca2
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1d8(%rsp)
movl %eax, 0x1d4(%rsp)
leaq 0x168(%rsp), %rdi
callq 0x46e8
jmp 0x2ecde
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1d8(%rsp)
movl %eax, 0x1d4(%rsp)
jmp 0x2ecd1
xorl %eax, %eax
movl %eax, %esi
leaq 0x188(%rsp), %rdi
movl $0xa, %edx
callq 0xdd90
movq %rax, 0x50(%rsp)
jmp 0x2e953
movq 0x50(%rsp), %rax
movq %rax, 0x160(%rsp)
cmpq $0xf, 0x160(%rsp)
jbe 0x2e97b
movl $0x1, 0x1ac(%rsp)
jmp 0x2eca2
movq 0x220(%rsp), %rdi
movl $0x1, %esi
callq 0x118b0
movq %rax, 0x158(%rsp)
leaq 0x158(%rsp), %rdi
callq 0x30d40
movq %rax, %rdi
leaq 0x22bb6(%rip), %rsi     # 0x51562
callq 0xdd10
movb %al, 0x4f(%rsp)
jmp 0x2e9b7
movb 0x4f(%rsp), %al
testb $0x1, %al
jne 0x2e9c1
jmp 0x2ea3b
movq 0x228(%rsp), %rax
movq %rax, 0x38(%rsp)
movq 0x220(%rsp), %rdi
movl $0x1, %esi
callq 0x118b0
movq %rax, 0x140(%rsp)
leaq 0x148(%rsp), %rdi
leaq 0x140(%rsp), %rsi
callq 0xe3b0
movq 0x38(%rsp), %rdi
movq 0x148(%rsp), %rsi
callq 0x30d50
movq %rax, 0x40(%rsp)
jmp 0x2ea16
movq 0x40(%rsp), %rax
movq %rax, 0x150(%rsp)
movq 0x220(%rsp), %rax
movq 0x150(%rsp), %rcx
movq %rcx, (%rax)
jmp 0x2eac7
movq 0x228(%rsp), %rax
movq %rax, 0x28(%rsp)
movq 0x220(%rsp), %rdi
movl $0x2, %esi
callq 0x118b0
movq %rax, 0x128(%rsp)
leaq 0x130(%rsp), %rdi
leaq 0x128(%rsp), %rsi
callq 0xe3b0
movq 0x28(%rsp), %rdi
movq 0x130(%rsp), %rsi
callq 0x30d50
movq %rax, 0x30(%rsp)
jmp 0x2ea90
movq 0x30(%rsp), %rax
movq %rax, 0x138(%rsp)
movq 0x220(%rsp), %rax
movq 0x138(%rsp), %rcx
movq %rcx, (%rax)
movq 0x220(%rsp), %rdi
xorl %esi, %esi
callq 0x30e00
movq %rax, 0x120(%rsp)
movq 0x228(%rsp), %rax
movq %rax, 0x18(%rsp)
movq 0x220(%rsp), %rsi
leaq 0x110(%rsp), %rdi
callq 0xe3b0
movq 0x18(%rsp), %rdi
movq 0x110(%rsp), %rsi
callq 0x30d50
movq %rax, 0x20(%rsp)
jmp 0x2eb02
movq 0x20(%rsp), %rax
movq %rax, 0x118(%rsp)
movq 0x220(%rsp), %rax
movq 0x118(%rsp), %rcx
movq %rcx, (%rax)
movq 0x220(%rsp), %rdi
xorl %esi, %esi
callq 0x30dc0
movq %rax, 0x108(%rsp)
leaq 0xa0(%rsp), %rdi
callq 0xda30
jmp 0x2eb48
leaq 0x22583(%rip), %rsi     # 0x510d2
leaq 0xa0(%rsp), %rdi
callq 0x44a0
jmp 0x2eb5e
leaq 0x22575(%rip), %rsi     # 0x510da
leaq 0x200(%rsp), %rdi
callq 0xdd10
movb %al, 0x17(%rsp)
jmp 0x2eb78
movb 0x17(%rsp), %al
testb $0x1, %al
jne 0x2eb82
jmp 0x2ebc0
leaq 0x2254d(%rip), %rsi     # 0x510d6
leaq 0xa0(%rsp), %rdi
callq 0x44a0
jmp 0x2eb98
jmp 0x2ebc0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1d8(%rsp)
movl %eax, 0x1d4(%rsp)
leaq 0xa0(%rsp), %rdi
callq 0xd490
jmp 0x2ecd1
leaq 0xc0(%rsp), %rdi
leaq 0x1e0(%rsp), %rsi
callq 0x4050
jmp 0x2ebd7
movq $0x0, 0x98(%rsp)
movq 0x98(%rsp), %rax
cmpq 0x160(%rsp), %rax
jae 0x2ec8a
movq 0x220(%rsp), %rdi
xorl %esi, %esi
callq 0x30e00
movq %rax, 0x90(%rsp)
movq 0x228(%rsp), %rax
movq %rax, (%rsp)
movq 0x220(%rsp), %rsi
leaq 0x80(%rsp), %rdi
callq 0xe3b0
movq (%rsp), %rdi
movq 0x80(%rsp), %rsi
leaq 0xa0(%rsp), %rdx
callq 0x30e70
movq %rax, 0x8(%rsp)
jmp 0x2ec51
movq 0x8(%rsp), %rax
movq %rax, 0x88(%rsp)
movq 0x220(%rsp), %rax
movq 0x88(%rsp), %rcx
movq %rcx, (%rax)
movq 0x98(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x98(%rsp)
jmp 0x2ebe3
leaq 0xa0(%rsp), %rdi
callq 0xd490
movl $0x0, 0x1ac(%rsp)
leaq 0x188(%rsp), %rdi
callq 0x46e8
leaq 0x1e0(%rsp), %rdi
callq 0x46e8
leaq 0x200(%rsp), %rdi
callq 0x46e8
addq $0x238, %rsp            # imm = 0x238
retq
leaq 0x188(%rsp), %rdi
callq 0x46e8
leaq 0x1e0(%rsp), %rdi
callq 0x46e8
leaq 0x200(%rsp), %rdi
callq 0x46e8
movq 0x1d8(%rsp), %rdi
callq 0x45f0
nopw %cs:(%rax,%rax)