// File: /workspace/github_cpp_makefile_star2_O2/Bartor[P]kompilator/middle/ast_optimizer/ASTOptimizer.cpp
// Function: ASTOptimizer::constantLoopUnroller(Node*)
Node *ASTOptimizer::constantLoopUnroller(Node *node) {
    if (auto forNode = dynamic_cast<For *>(node)) {
        try {
            auto startConstant = dynamic_cast<NumberValue &>(forNode->startValue);
            try {
                auto endConstant = dynamic_cast<NumberValue &>(forNode->endValue);

                CommandList *cmdList = new CommandList();

                if (forNode->reversed) {
                    for (long long i = startConstant.value; i >= endConstant.value; i--) {
                        Callback replacer = iteratorReplacer(forNode->variableName, i);
                        originalProgram->constants.constants.push_back(i);
                        cmdList->append(*static_cast<CommandList *>(forNode->commands.copy(replacer)));
                    }
                } else {
                    for (long long i = startConstant.value; i <= endConstant.value; i++) {
                        Callback replacer = iteratorReplacer(forNode->variableName, i);
                        originalProgram->constants.constants.push_back(i);
                        cmdList->append(*static_cast<CommandList *>(forNode->commands.copy(replacer)));
                    }
                }

                return cmdList;
            } catch (std::bad_cast _) {}
        } catch (std::bad_cast _) {}
    }
    return node;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
testq %rsi, %rsi
je 0x13c73
movq %rsi, %r15
movq %rdi, %r14
leaq 0xbad2(%rip), %rsi      # 0x1f618
leaq 0xb94b(%rip), %rdx      # 0x1f498
movq %r15, %rdi
xorl %ecx, %ecx
callq 0x51f0
testq %rax, %rax
je 0x13c7a
movq %rax, %r12
movq 0x28(%rax), %rdi
leaq 0xbad2(%rip), %rsi      # 0x1f640
leaq 0xb653(%rip), %rdx      # 0x1f1c8
xorl %ecx, %ecx
callq 0x51f0
testq %rax, %rax
movq %r15, -0x40(%rbp)
je 0x13d0f
movq 0x8(%rax), %rbx
movq 0x30(%r12), %rdi
leaq 0xbaa7(%rip), %rsi      # 0x1f640
leaq 0xb628(%rip), %rdx      # 0x1f1c8
xorl %ecx, %ecx
callq 0x51f0
testq %rax, %rax
je 0x13d14
movq 0x8(%rax), %rax
movq %rax, -0x50(%rbp)
pushq $0x20
popq %rdi
callq 0x51c0
leaq 0xb739(%rip), %rcx      # 0x1f300
addq $0x10, %rcx
movq %rcx, (%rax)
xorps %xmm0, %xmm0
movups %xmm0, 0x8(%rax)
movq %rax, -0x38(%rbp)
andq $0x0, 0x18(%rax)
movq %r12, %r13
addq $0x8, %r13
movq %r12, -0x48(%rbp)
cmpb $0x1, 0x40(%r12)
jne 0x13c7f
leaq -0x70(%rbp), %r15
leaq -0xb0(%rbp), %r12
movq %rbx, -0x30(%rbp)
cmpq -0x50(%rbp), %rbx
jl 0x13cf9
movq %r15, %rdi
movq %r14, %rsi
movq %r13, %rdx
movq %rbx, %rcx
callq 0x13de6
movq (%r14), %rax
movq 0x10(%rax), %rdi
leaq -0x30(%rbp), %rsi
callq 0x6086
movq -0x48(%rbp), %rax
movq 0x38(%rax), %rbx
movq %r12, %rdi
movq %r15, %rsi
callq 0xa87a
movq (%rbx), %rax
movq %rbx, %rdi
movq %r12, %rsi
callq *0x8(%rax)
movq -0x38(%rbp), %rdi
movq %rax, %rsi
callq 0x1459e
movq %r12, %rdi
callq 0xa8c8
movq %r15, %rdi
callq 0xa8c8
movq -0x30(%rbp), %rbx
decq %rbx
jmp 0x13c00
xorl %eax, %eax
jmp 0x13cfd
movq %r15, %rax
jmp 0x13cfd
leaq -0x70(%rbp), %r15
leaq -0x90(%rbp), %r12
movq %rbx, -0x30(%rbp)
cmpq -0x50(%rbp), %rbx
jg 0x13cf9
movq %r15, %rdi
movq %r14, %rsi
movq %r13, %rdx
movq %rbx, %rcx
callq 0x13de6
movq (%r14), %rax
movq 0x10(%rax), %rdi
leaq -0x30(%rbp), %rsi
callq 0x6086
movq -0x48(%rbp), %rax
movq 0x38(%rax), %rbx
movq %r12, %rdi
movq %r15, %rsi
callq 0xa87a
movq (%rbx), %rax
movq %rbx, %rdi
movq %r12, %rsi
callq *0x8(%rax)
movq -0x38(%rbp), %rdi
movq %rax, %rsi
callq 0x1459e
movq %r12, %rdi
callq 0xa8c8
movq %r15, %rdi
callq 0xa8c8
movq -0x30(%rbp), %rbx
incq %rbx
jmp 0x13c8a
movq -0x38(%rbp), %rax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x53d0
callq 0x53d0
jmp 0x13d27
jmp 0x13d27
movq %rdx, %r15
movq %rax, %r14
jmp 0x13d9a
jmp 0x13d27
movq %rdx, %r15
movq %rax, %r14
jmp 0x13d63
movq %rdx, %r15
movq %rax, %r14
leaq -0x90(%rbp), %rdi
jmp 0x13d4b
movq %rdx, %r15
movq %rax, %r14
leaq -0xb0(%rbp), %rdi
callq 0xa8c8
jmp 0x13d5a
jmp 0x13d54
movq %rdx, %r15
movq %rax, %r14
leaq -0x70(%rbp), %rdi
callq 0xa8c8
cmpl $0x1, %r15d
jne 0x13d9a
movq %r14, %rdi
callq 0x5250
movq 0xc238(%rip), %rax      # 0x1ffb0
addq $0x10, %rax
leaq -0x70(%rbp), %rbx
movq %rax, (%rbx)
movq %r14, %rdi
callq 0x50d0
movq %rbx, %rdi
callq 0x53b0
callq 0x5320
jmp 0x13dcf
cmpl $0x1, %r15d
jne 0x13dd8
movq %r14, %rdi
callq 0x5250
movq 0xc201(%rip), %rax      # 0x1ffb0
addq $0x10, %rax
leaq -0x70(%rbp), %rbx
movq %rax, (%rbx)
movq %r14, %rdi
callq 0x50d0
movq %rbx, %rdi
callq 0x53b0
callq 0x5320
movq -0x40(%rbp), %rax
jmp 0x13cfd
movq %r14, %rdi
callq 0x5390
jmp 0x13d1d
nop