// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x298, %rsp            # imm = 0x298
movq %rdx, 0x18(%rsp)
movq %rdi, 0x20(%rsp)
addq $0x18, %rdi
movl %esi, 0x2c(%rsp)
movslq %esi, %rsi
movq %rdi, 0x30(%rsp)
movq %rsi, %rbx
callq 0x94c4
leaq 0x58(%rsp), %r15
movq %r15, %rdi
movq %rax, %rsi
callq 0x21e0
pushq $-0x1
popq %rax
movl %eax, 0x8(%rsp)
cmpq $0x0, 0x8(%r15)
je 0x926d
leaq 0x138(%rsp), %rdi
leaq 0x58(%rsp), %rsi
callq 0x21e0
leaq 0x14d9(%rip), %rsi      # 0xa303
leaq 0x118(%rsp), %rdi
leaq 0x38(%rsp), %rdx
callq 0x57ec
leaq 0x138(%rsp), %rdi
leaq 0x118(%rsp), %rsi
callq 0x5ddc
movl %eax, %ebp
leaq 0x118(%rsp), %rdi
callq 0x20c0
leaq 0x138(%rsp), %rdi
callq 0x20c0
testb %bpl, %bpl
je 0x926d
leaq 0x48(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
movq 0x58(%rsp), %r12
movq 0x60(%rsp), %r13
xorl %ebp, %ebp
leaq 0x38(%rsp), %r15
xorl %r14d, %r14d
cmpq %rbp, %r13
je 0x8ec9
movb (%r12,%rbp), %al
cmpb $0x20, %al
jne 0x8eb3
testb $0x1, %r14b
movb $0x1, %r14b
je 0x8ec4
jmp 0x8ec9
testb $0x1, %r14b
je 0x8ec4
movsbl %al, %esi
movq %r15, %rdi
callq 0x2140
incq %rbp
jmp 0x8e9b
movq 0x18(%rsp), %rdi
callq 0x77fe
movq %rax, %r14
leaq 0xd8(%rsp), %rdi
leaq 0x38(%rsp), %rsi
callq 0x21e0
leaq 0xf8(%rsp), %rdi
leaq 0xd8(%rsp), %rdx
movq %r14, %rsi
callq 0x310e
leaq 0xd8(%rsp), %rdi
callq 0x20c0
leaq 0x11a7(%rip), %rsi      # 0xa0bb
leaq 0xf8(%rsp), %rdi
callq 0x58aa
testb %al, %al
je 0x90db
movl $0x0, 0xc(%rsp)
pushq $-0x1
popq %rax
movl %eax, 0x8(%rsp)
leaq 0xb8(%rsp), %r12
leaq 0x98(%rsp), %r13
leaq 0x238(%rsp), %r15
movq %rbx, %rsi
movq 0x20(%rsp), %rcx
movq 0x20(%rcx), %rax
subq 0x18(%rcx), %rax
sarq $0x5, %rax
cmpq %rsi, %rax
jbe 0x9256
movq 0x30(%rsp), %rdi
movq %rsi, 0x18(%rsp)
callq 0x94c4
movq %r12, %rdi
movq %rax, %rsi
callq 0x21e0
movq %r13, %rdi
movq %r12, %rbx
movq %r12, %rsi
callq 0x21e0
leaq 0x278(%rsp), %r14
movq %r14, %rdi
leaq 0x1352(%rip), %rsi      # 0xa2f9
leaq 0x10(%rsp), %rdx
callq 0x57ec
leaq 0x98(%rsp), %r12
movq %r12, %rdi
movq %r14, %rsi
callq 0x5ddc
movq %r14, %rdi
movl %eax, %r14d
callq 0x20c0
movq %r12, %rdi
callq 0x20c0
leaq 0x1b8(%rsp), %rdi
movq %rbx, %rsi
callq 0x21e0
leaq 0x258(%rsp), %r12
movq %r12, %rdi
leaq 0x1303(%rip), %rsi      # 0xa2fc
leaq 0x10(%rsp), %rdx
callq 0x57ec
leaq 0x1b8(%rsp), %rbp
movq %rbp, %rdi
movq %r12, %rsi
callq 0x5ddc
movq %r12, %rdi
movl %eax, %r12d
callq 0x20c0
movq %rbp, %rdi
callq 0x20c0
leaq 0x198(%rsp), %rdi
movq %rbx, %rsi
callq 0x21e0
movq %r15, %rdi
leaq 0x12b9(%rip), %rsi      # 0xa2fc
leaq 0x10(%rsp), %rdx
callq 0x57ec
movq %r15, %r13
leaq 0x198(%rsp), %r15
movq %r15, %rdi
movq %r13, %rsi
callq 0x5ddc
movl %eax, %ebp
movzbl %r14b, %eax
movl 0xc(%rsp), %ecx
addl %eax, %ecx
movzbl %r12b, %eax
subl %eax, %ecx
movl %ecx, 0xc(%rsp)
sete %al
andb %al, %bpl
movq %r13, %r14
movq %r13, %rdi
callq 0x20c0
movq %r15, %rdi
callq 0x20c0
cmpb $0x1, %bpl
leaq 0x98(%rsp), %r13
movq %rbx, %r12
jne 0x90c3
movq 0x18(%rsp), %rax
movl %eax, 0x10(%rsp)
movl 0x2c(%rsp), %eax
movl %eax, 0x14(%rsp)
movq 0x20(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0x94e6
movq %r12, %rdi
callq 0x20c0
movq 0x18(%rsp), %rsi
incq %rsi
movq %r14, %r15
jmp 0x8f53
xorl %ebp, %ebp
pushq $-0x1
popq %rax
movl %eax, 0x8(%rsp)
leaq 0xb8(%rsp), %r12
leaq 0x78(%rsp), %r14
movq %rbx, %rsi
movq 0x20(%rsp), %rcx
movq 0x20(%rcx), %rax
subq 0x18(%rcx), %rax
sarq $0x5, %rax
cmpq %rsi, %rax
jbe 0x9256
movq 0x30(%rsp), %rdi
movq %rsi, 0x18(%rsp)
callq 0x94c4
movq %r12, %rdi
movq %rax, %rsi
callq 0x21e0
movq %r14, %rdi
movq %r12, %rbx
movq %r12, %rsi
callq 0x21e0
leaq 0x218(%rsp), %r12
movq %r12, %rdi
leaq 0x11b1(%rip), %rsi      # 0xa2f9
leaq 0x10(%rsp), %rdx
callq 0x57ec
leaq 0x78(%rsp), %r15
movq %r15, %rdi
movq %r12, %rsi
callq 0x5ddc
movb %al, 0xc(%rsp)
movq %r12, %rdi
callq 0x20c0
movq %r15, %rdi
callq 0x20c0
leaq 0x178(%rsp), %rdi
movq %rbx, %rsi
callq 0x21e0
leaq 0x1f8(%rsp), %r15
movq %r15, %rdi
leaq 0x1164(%rip), %rsi      # 0xa2fc
leaq 0x10(%rsp), %rdx
callq 0x57ec
leaq 0x178(%rsp), %r13
movq %r13, %rdi
movq %r15, %rsi
callq 0x5ddc
movq %r15, %rdi
movl %eax, %r15d
callq 0x20c0
movq %r13, %rdi
callq 0x20c0
leaq 0x158(%rsp), %rdi
movq %rbx, %rsi
callq 0x21e0
leaq 0x1d8(%rsp), %r12
movq %r12, %rdi
leaq 0x1112(%rip), %rsi      # 0xa2fc
leaq 0x10(%rsp), %rdx
callq 0x57ec
leaq 0x158(%rsp), %r14
movq %r14, %rdi
movq %r12, %rsi
callq 0x5ddc
movl %eax, %r13d
movzbl 0xc(%rsp), %eax
addl %eax, %ebp
movzbl %r15b, %eax
subl %eax, %ebp
sete %r15b
movq %r12, %rdi
callq 0x20c0
movq %r14, %rdi
callq 0x20c0
movq %rbx, %r12
movq %rbx, %rdi
callq 0x20c0
testb %r15b, %r13b
jne 0x924d
movq 0x18(%rsp), %rsi
incq %rsi
leaq 0x78(%rsp), %r14
jmp 0x90f4
movq 0x18(%rsp), %rax
movl %eax, 0x8(%rsp)
leaq 0xf8(%rsp), %rdi
callq 0x20c0
leaq 0x38(%rsp), %rdi
callq 0x20c0
leaq 0x58(%rsp), %rdi
callq 0x20c0
movl 0x8(%rsp), %eax
addq $0x298, %rsp            # imm = 0x298
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq 0xd8(%rsp), %rdi
jmp 0x93b6
jmp 0x92c9
movq %rax, %rbx
leaq 0x118(%rsp), %rdi
callq 0x20c0
jmp 0x92b4
movq %rax, %rbx
leaq 0x138(%rsp), %rdi
jmp 0x93c0
movq %rax, %rbx
jmp 0x93c5
movq %rax, %rbx
jmp 0x93bb
jmp 0x939e
movq %rax, %rbx
leaq 0x218(%rsp), %rdi
callq 0x20c0
jmp 0x9302
movq %rax, %rbx
leaq 0x1f8(%rsp), %rdi
callq 0x20c0
jmp 0x930f
movq %rax, %rbx
jmp 0x9329
movq %rax, %rbx
leaq 0x78(%rsp), %rdi
jmp 0x938e
movq %rax, %rbx
leaq 0x178(%rsp), %rdi
jmp 0x938e
movq %rax, %rbx
leaq 0x1d8(%rsp), %rdi
callq 0x20c0
leaq 0x158(%rsp), %rdi
jmp 0x938e
movq %rax, %rbx
jmp 0x934d
movq %rax, %rbx
jmp 0x936c
movq %rax, %rbx
leaq 0x278(%rsp), %rdi
callq 0x20c0
leaq 0x98(%rsp), %rdi
jmp 0x938e
movq %rax, %rbx
jmp 0x9386
movq %rax, %rbx
leaq 0x238(%rsp), %rdi
callq 0x20c0
leaq 0x198(%rsp), %rdi
jmp 0x938e
movq %rax, %rbx
leaq 0x258(%rsp), %rdi
callq 0x20c0
leaq 0x1b8(%rsp), %rdi
callq 0x20c0
jmp 0x93a1
jmp 0x9399
jmp 0x939e
movq %rax, %rbx
jmp 0x93ae
movq %rax, %rbx
leaq 0xb8(%rsp), %rdi
callq 0x20c0
leaq 0xf8(%rsp), %rdi
callq 0x20c0
leaq 0x38(%rsp), %rdi
callq 0x20c0
leaq 0x58(%rsp), %rdi
callq 0x20c0
movq %rbx, %rdi
callq 0x2250
nop