// File: Manuel123cool[P]smiley/instruction.cpp
// Function: Instruction::checkLoop(int, Stack&)
int Instruction::checkLoop(int whichInstr, Stack &stack)
{
    std::string code{ m_instructions.at(whichInstr) };
    if (code.length() <= 0)
        return -1;

    if (!MyRegex::testString(code, "<* "))
        return -1;
    
    std::string identifier;
    bool start{ false };
    for(const auto &c : code) 
    {
        if (c == ' ' && start)
            break; 

        if (start)
            identifier += c;

        if (c == ' ' && !start)
            start = true;
    }     
    std::string value{ stack().getValueVar(identifier) };

    if (value == "true")
    {
        int countBlockStarts{ 0 };
        for (int i{ whichInstr }; i < m_instructions.size(); ++i)  
        {
            std::string code{ m_instructions.at(i) };

            if (MyRegex::testString(code, "><"))
                ++countBlockStarts;

            if (MyRegex::testString(code, "<>"))
                --countBlockStarts;

            if (MyRegex::testString(code, "<>") && countBlockStarts == 0)
            {
                Jump jump;
                jump.start = i;
                jump.jumpTo = whichInstr;
                m_jumps.push_back(jump); 
            }
        }       
    }
    else 
    {
        int countBlockStarts{ 0 };
        for (int i{ whichInstr }; i < m_instructions.size(); ++i)  
        {
            std::string code{ m_instructions.at(i) };

            if (MyRegex::testString(code, "><"))
                ++countBlockStarts;

            if (MyRegex::testString(code, "<>"))
                --countBlockStarts;

            if (MyRegex::testString(code, "<>") && countBlockStarts == 0)
                return i;
        } 
    }
    return -1;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x3e0, %rsp            # imm = 0x3E0
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movq %rdx, -0x20(%rbp)
movq -0x10(%rbp), %rdi
movq %rdi, -0x338(%rbp)
addq $0x18, %rdi
movslq -0x14(%rbp), %rsi
callq 0x12e90
movq %rax, %rsi
leaq -0x40(%rbp), %rdi
callq 0x30e0
leaq -0x40(%rbp), %rdi
callq 0x3330
cmpq $0x0, %rax
ja 0x115dc
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
movl $0x1, -0x44(%rbp)
jmp 0x1201c
leaq -0x68(%rbp), %rdi
leaq -0x40(%rbp), %rsi
callq 0x30e0
jmp 0x115eb
leaq -0x99(%rbp), %rdi
movq %rdi, -0x340(%rbp)
callq 0x3390
movq -0x340(%rbp), %rdx
leaq 0x3cf7(%rip), %rsi      # 0x15303
leaq -0x98(%rbp), %rdi
callq 0x8eb0
jmp 0x1161a
leaq -0x68(%rbp), %rdi
leaq -0x98(%rbp), %rsi
callq 0xa430
movb %al, -0x341(%rbp)
jmp 0x11632
movb -0x341(%rbp), %al
xorb $-0x1, %al
movb %al, -0x342(%rbp)
leaq -0x98(%rbp), %rdi
callq 0x3160
leaq -0x99(%rbp), %rdi
callq 0x3270
leaq -0x68(%rbp), %rdi
callq 0x3160
movb -0x342(%rbp), %al
testb $0x1, %al
jne 0x1166d
jmp 0x116d1
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
movl $0x1, -0x44(%rbp)
jmp 0x1201c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
jmp 0x12031
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
jmp 0x116b7
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
leaq -0x98(%rbp), %rdi
callq 0x3160
leaq -0x99(%rbp), %rdi
callq 0x3270
leaq -0x68(%rbp), %rdi
callq 0x3160
jmp 0x12031
leaq -0xc0(%rbp), %rdi
callq 0x3300
movb $0x0, -0xc1(%rbp)
leaq -0x40(%rbp), %rax
movq %rax, -0xd0(%rbp)
movq -0xd0(%rbp), %rdi
callq 0x31e0
movq %rax, -0xd8(%rbp)
movq -0xd0(%rbp), %rdi
callq 0x3130
movq %rax, -0xe0(%rbp)
leaq -0xd8(%rbp), %rdi
leaq -0xe0(%rbp), %rsi
callq 0x9180
testb $0x1, %al
jne 0x11731
jmp 0x117c4
leaq -0xd8(%rbp), %rdi
callq 0x91c0
movq %rax, -0xe8(%rbp)
movq -0xe8(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x20, %eax
jne 0x1175e
testb $0x1, -0xc1(%rbp)
je 0x1175e
jmp 0x117c4
testb $0x1, -0xc1(%rbp)
je 0x11792
movq -0xe8(%rbp), %rax
movsbl (%rax), %esi
leaq -0xc0(%rbp), %rdi
callq 0x3170
jmp 0x1177f
jmp 0x11792
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
jmp 0x1200e
movq -0xe8(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x20, %eax
jne 0x117b1
testb $0x1, -0xc1(%rbp)
jne 0x117b1
movb $0x1, -0xc1(%rbp)
jmp 0x117b3
leaq -0xd8(%rbp), %rdi
callq 0x91e0
jmp 0x11715
movq -0x20(%rbp), %rdi
callq 0xdf30
movq %rax, -0x350(%rbp)
jmp 0x117d6
leaq -0x128(%rbp), %rdi
leaq -0xc0(%rbp), %rsi
callq 0x30e0
jmp 0x117eb
movq -0x350(%rbp), %rsi
leaq -0x108(%rbp), %rdi
leaq -0x128(%rbp), %rdx
callq 0x5200
jmp 0x11807
leaq -0x128(%rbp), %rdi
callq 0x3160
leaq 0x38a1(%rip), %rsi      # 0x150bb
leaq -0x108(%rbp), %rdi
callq 0x9240
movb %al, -0x351(%rbp)
jmp 0x1182e
movb -0x351(%rbp), %al
testb $0x1, %al
jne 0x1183d
jmp 0x11c36
movl $0x0, -0x12c(%rbp)
movl -0x14(%rbp), %eax
movl %eax, -0x130(%rbp)
movq -0x338(%rbp), %rdi
movslq -0x130(%rbp), %rax
movq %rax, -0x360(%rbp)
addq $0x18, %rdi
callq 0x12e70
movq %rax, %rcx
movq -0x360(%rbp), %rax
cmpq %rcx, %rax
jae 0x11c31
movq -0x338(%rbp), %rdi
addq $0x18, %rdi
movslq -0x130(%rbp), %rsi
callq 0x12e90
movq %rax, -0x368(%rbp)
jmp 0x118a1
movq -0x368(%rbp), %rsi
leaq -0x150(%rbp), %rdi
callq 0x30e0
jmp 0x118b6
leaq -0x170(%rbp), %rdi
leaq -0x150(%rbp), %rsi
callq 0x30e0
jmp 0x118cb
leaq -0x191(%rbp), %rdi
movq %rdi, -0x370(%rbp)
callq 0x3390
movq -0x370(%rbp), %rdx
leaq 0x3a0d(%rip), %rsi      # 0x152f9
leaq -0x190(%rbp), %rdi
callq 0x8eb0
jmp 0x118fa
leaq -0x170(%rbp), %rdi
leaq -0x190(%rbp), %rsi
callq 0xa430
movb %al, -0x371(%rbp)
jmp 0x11915
leaq -0x190(%rbp), %rdi
callq 0x3160
leaq -0x191(%rbp), %rdi
callq 0x3270
leaq -0x170(%rbp), %rdi
callq 0x3160
movb -0x371(%rbp), %al
testb $0x1, %al
jne 0x11948
jmp 0x119de
movl -0x12c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x12c(%rbp)
jmp 0x119de
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
leaq -0x128(%rbp), %rdi
callq 0x3160
jmp 0x1200e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
jmp 0x12002
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
jmp 0x11c20
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
jmp 0x119c1
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
leaq -0x190(%rbp), %rdi
callq 0x3160
leaq -0x191(%rbp), %rdi
callq 0x3270
leaq -0x170(%rbp), %rdi
callq 0x3160
jmp 0x11c20
leaq -0x1b8(%rbp), %rdi
leaq -0x150(%rbp), %rsi
callq 0x30e0
jmp 0x119f3
leaq -0x1d9(%rbp), %rdi
movq %rdi, -0x380(%rbp)
callq 0x3390
movq -0x380(%rbp), %rdx
leaq 0x38e8(%rip), %rsi      # 0x152fc
leaq -0x1d8(%rbp), %rdi
callq 0x8eb0
jmp 0x11a22
leaq -0x1b8(%rbp), %rdi
leaq -0x1d8(%rbp), %rsi
callq 0xa430
movb %al, -0x381(%rbp)
jmp 0x11a3d
leaq -0x1d8(%rbp), %rdi
callq 0x3160
leaq -0x1d9(%rbp), %rdi
callq 0x3270
leaq -0x1b8(%rbp), %rdi
callq 0x3160
movb -0x381(%rbp), %al
testb $0x1, %al
jne 0x11a6d
jmp 0x11ac1
movl -0x12c(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x12c(%rbp)
jmp 0x11ac1
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
jmp 0x11aa4
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
leaq -0x1d8(%rbp), %rdi
callq 0x3160
leaq -0x1d9(%rbp), %rdi
callq 0x3270
leaq -0x1b8(%rbp), %rdi
callq 0x3160
jmp 0x11c20
leaq -0x200(%rbp), %rdi
leaq -0x150(%rbp), %rsi
callq 0x30e0
jmp 0x11ad6
leaq -0x221(%rbp), %rdi
movq %rdi, -0x390(%rbp)
callq 0x3390
movq -0x390(%rbp), %rdx
leaq 0x3805(%rip), %rsi      # 0x152fc
leaq -0x220(%rbp), %rdi
callq 0x8eb0
jmp 0x11b05
leaq -0x200(%rbp), %rdi
leaq -0x220(%rbp), %rsi
callq 0xa430
movb %al, -0x391(%rbp)
jmp 0x11b20
movb -0x391(%rbp), %cl
xorl %eax, %eax
testb $0x1, %cl
movb %al, -0x392(%rbp)
jne 0x11b35
jmp 0x11b45
cmpl $0x0, -0x12c(%rbp)
sete %al
movb %al, -0x392(%rbp)
movb -0x392(%rbp), %al
movb %al, -0x393(%rbp)
leaq -0x220(%rbp), %rdi
callq 0x3160
leaq -0x221(%rbp), %rdi
callq 0x3270
leaq -0x200(%rbp), %rdi
callq 0x3160
movb -0x393(%rbp), %al
testb $0x1, %al
jne 0x11b81
jmp 0x11c00
leaq -0x22c(%rbp), %rdi
movq %rdi, -0x3a0(%rbp)
callq 0x12ef0
movq -0x338(%rbp), %rdi
movq -0x3a0(%rbp), %rsi
movl -0x130(%rbp), %eax
movl %eax, -0x22c(%rbp)
movl -0x14(%rbp), %eax
movl %eax, -0x228(%rbp)
callq 0x12f10
jmp 0x11bbe
jmp 0x11c00
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
jmp 0x11be6
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
leaq -0x220(%rbp), %rdi
callq 0x3160
leaq -0x221(%rbp), %rdi
callq 0x3270
leaq -0x200(%rbp), %rdi
callq 0x3160
jmp 0x11c20
leaq -0x150(%rbp), %rdi
callq 0x3160
movl -0x130(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x130(%rbp)
jmp 0x11850
leaq -0x150(%rbp), %rdi
callq 0x3160
jmp 0x12002
jmp 0x11fda
movl $0x0, -0x230(%rbp)
movl -0x14(%rbp), %eax
movl %eax, -0x234(%rbp)
movq -0x338(%rbp), %rdi
movslq -0x234(%rbp), %rax
movq %rax, -0x3a8(%rbp)
addq $0x18, %rdi
callq 0x12e70
movq %rax, %rcx
movq -0x3a8(%rbp), %rax
cmpq %rcx, %rax
jae 0x11fd8
movq -0x338(%rbp), %rdi
addq $0x18, %rdi
movslq -0x234(%rbp), %rsi
callq 0x12e90
movq %rax, -0x3b0(%rbp)
jmp 0x11c9a
movq -0x3b0(%rbp), %rsi
leaq -0x258(%rbp), %rdi
callq 0x30e0
jmp 0x11caf
leaq -0x278(%rbp), %rdi
leaq -0x258(%rbp), %rsi
callq 0x30e0
jmp 0x11cc4
leaq -0x299(%rbp), %rdi
movq %rdi, -0x3b8(%rbp)
callq 0x3390
movq -0x3b8(%rbp), %rdx
leaq 0x3614(%rip), %rsi      # 0x152f9
leaq -0x298(%rbp), %rdi
callq 0x8eb0
jmp 0x11cf3
leaq -0x278(%rbp), %rdi
leaq -0x298(%rbp), %rsi
callq 0xa430
movb %al, -0x3b9(%rbp)
jmp 0x11d0e
leaq -0x298(%rbp), %rdi
callq 0x3160
leaq -0x299(%rbp), %rdi
callq 0x3270
leaq -0x278(%rbp), %rdi
callq 0x3160
movb -0x3b9(%rbp), %al
testb $0x1, %al
jne 0x11d3e
jmp 0x11da3
movl -0x230(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x230(%rbp)
jmp 0x11da3
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
jmp 0x11fca
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
jmp 0x11d86
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
leaq -0x298(%rbp), %rdi
callq 0x3160
leaq -0x299(%rbp), %rdi
callq 0x3270
leaq -0x278(%rbp), %rdi
callq 0x3160
jmp 0x11fca
leaq -0x2c0(%rbp), %rdi
leaq -0x258(%rbp), %rsi
callq 0x30e0
jmp 0x11db8
leaq -0x2e1(%rbp), %rdi
movq %rdi, -0x3c8(%rbp)
callq 0x3390
movq -0x3c8(%rbp), %rdx
leaq 0x3523(%rip), %rsi      # 0x152fc
leaq -0x2e0(%rbp), %rdi
callq 0x8eb0
jmp 0x11de7
leaq -0x2c0(%rbp), %rdi
leaq -0x2e0(%rbp), %rsi
callq 0xa430
movb %al, -0x3c9(%rbp)
jmp 0x11e02
leaq -0x2e0(%rbp), %rdi
callq 0x3160
leaq -0x2e1(%rbp), %rdi
callq 0x3270
leaq -0x2c0(%rbp), %rdi
callq 0x3160
movb -0x3c9(%rbp), %al
testb $0x1, %al
jne 0x11e32
jmp 0x11e86
movl -0x230(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x230(%rbp)
jmp 0x11e86
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
jmp 0x11e69
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
leaq -0x2e0(%rbp), %rdi
callq 0x3160
leaq -0x2e1(%rbp), %rdi
callq 0x3270
leaq -0x2c0(%rbp), %rdi
callq 0x3160
jmp 0x11fca
leaq -0x308(%rbp), %rdi
leaq -0x258(%rbp), %rsi
callq 0x30e0
jmp 0x11e9b
leaq -0x329(%rbp), %rdi
movq %rdi, -0x3d8(%rbp)
callq 0x3390
movq -0x3d8(%rbp), %rdx
leaq 0x3440(%rip), %rsi      # 0x152fc
leaq -0x328(%rbp), %rdi
callq 0x8eb0
jmp 0x11eca
leaq -0x308(%rbp), %rdi
leaq -0x328(%rbp), %rsi
callq 0xa430
movb %al, -0x3d9(%rbp)
jmp 0x11ee5
movb -0x3d9(%rbp), %cl
xorl %eax, %eax
testb $0x1, %cl
movb %al, -0x3da(%rbp)
jne 0x11efa
jmp 0x11f0a
cmpl $0x0, -0x230(%rbp)
sete %al
movb %al, -0x3da(%rbp)
movb -0x3da(%rbp), %al
movb %al, -0x3db(%rbp)
leaq -0x328(%rbp), %rdi
callq 0x3160
leaq -0x329(%rbp), %rdi
callq 0x3270
leaq -0x308(%rbp), %rdi
callq 0x3160
movb -0x3db(%rbp), %al
testb $0x1, %al
jne 0x11f46
jmp 0x11f98
movl -0x234(%rbp), %eax
movl %eax, -0x4(%rbp)
movl $0x1, -0x44(%rbp)
jmp 0x11f9f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
jmp 0x11f7e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
leaq -0x328(%rbp), %rdi
callq 0x3160
leaq -0x329(%rbp), %rdi
callq 0x3270
leaq -0x308(%rbp), %rdi
callq 0x3160
jmp 0x11fca
movl $0x0, -0x44(%rbp)
leaq -0x258(%rbp), %rdi
callq 0x3160
movl -0x44(%rbp), %eax
testl %eax, %eax
jne 0x11fe8
jmp 0x11fb4
jmp 0x11fb6
movl -0x234(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x234(%rbp)
jmp 0x11c49
leaq -0x258(%rbp), %rdi
callq 0x3160
jmp 0x12002
jmp 0x11fda
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
movl $0x1, -0x44(%rbp)
leaq -0x108(%rbp), %rdi
callq 0x3160
leaq -0xc0(%rbp), %rdi
callq 0x3160
jmp 0x1201c
leaq -0x108(%rbp), %rdi
callq 0x3160
leaq -0xc0(%rbp), %rdi
callq 0x3160
jmp 0x12031
leaq -0x40(%rbp), %rdi
callq 0x3160
movl -0x4(%rbp), %eax
addq $0x3e0, %rsp            # imm = 0x3E0
popq %rbp
retq
leaq -0x40(%rbp), %rdi
callq 0x3160
movq -0x70(%rbp), %rdi
callq 0x3380
nopw %cs:(%rax,%rax)