// File: /workspace/github_cpp_makefile_star2_O3/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 $0x98, %rsp
testq %rsi, %rsi
je 0x193e4
movq %rsi, %r12
movq %rdi, %r14
leaq 0xd3c6(%rip), %rsi      # 0x26610
leaq 0xd23f(%rip), %rdx      # 0x26490
movq %r12, %rdi
xorl %ecx, %ecx
callq 0x5240
testq %rax, %rax
je 0x194c9
movq %rax, %rbx
movq 0x28(%rax), %rdi
leaq 0xd3c6(%rip), %rsi      # 0x26638
leaq 0xcf47(%rip), %rdx      # 0x261c0
xorl %ecx, %ecx
callq 0x5240
testq %rax, %rax
movq %r12, -0x50(%rbp)
je 0x194de
movq 0x8(%rax), %r13
movq 0x30(%rbx), %rdi
leaq 0xd39c(%rip), %rsi      # 0x26638
leaq 0xcf1d(%rip), %rdx      # 0x261c0
xorl %ecx, %ecx
callq 0x5240
testq %rax, %rax
je 0x194e3
movq 0x8(%rax), %rax
movq %rax, -0x40(%rbp)
movl $0x20, %edi
callq 0x51f0
movq %rax, %r12
leaq 0xd029(%rip), %rax      # 0x262f8
addq $0x10, %rax
movq %rax, (%r12)
movq %r12, %rax
addq $0x8, %rax
movq %rax, -0x58(%rbp)
xorps %xmm0, %xmm0
movups %xmm0, 0x8(%r12)
movq $0x0, 0x18(%r12)
movq %rbx, -0x38(%rbp)
cmpb $0x1, 0x40(%rbx)
jne 0x193ec
movq %r13, -0x30(%rbp)
cmpq -0x40(%rbp), %r13
jl 0x194c9
movq -0x38(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x48(%rbp)
leaq -0x78(%rbp), %rbx
leaq -0xb8(%rbp), %r15
movq %rbx, %rdi
movq %r14, %rsi
movq -0x48(%rbp), %rdx
movq %r13, %rcx
callq 0x19628
movq (%r14), %rax
movq 0x10(%rax), %rdi
movq 0x8(%rdi), %rsi
cmpq 0x10(%rdi), %rsi
je 0x1935b
movq -0x30(%rbp), %rax
movq %rax, (%rsi)
addq $0x8, %rsi
movq %rsi, 0x8(%rdi)
jmp 0x19364
leaq -0x30(%rbp), %rdx
callq 0x66e0
movq -0x38(%rbp), %rax
movq 0x38(%rax), %r13
movq %r15, %rdi
movq %rbx, %rsi
callq 0xeab4
movq (%r13), %rax
movq %r13, %rdi
movq %r15, %rsi
callq *0x8(%rax)
movq 0x10(%r12), %rsi
movq 0x8(%rax), %rdx
movq 0x10(%rax), %rcx
movq -0x58(%rbp), %rdi
callq 0x1a02c
movq -0xa8(%rbp), %rax
testq %rax, %rax
je 0x193b3
movq %r15, %rdi
movq %r15, %rsi
movl $0x3, %edx
callq *%rax
movq -0x68(%rbp), %rax
testq %rax, %rax
je 0x193c9
movq %rbx, %rdi
movq %rbx, %rsi
movl $0x3, %edx
callq *%rax
movq -0x30(%rbp), %rax
leaq -0x1(%rax), %r13
movq %r13, -0x30(%rbp)
cmpq -0x40(%rbp), %rax
jg 0x19327
jmp 0x194c9
xorl %r12d, %r12d
jmp 0x194c9
movq %r13, -0x30(%rbp)
cmpq -0x40(%rbp), %r13
jg 0x194c9
movq -0x38(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x48(%rbp)
leaq -0x78(%rbp), %rbx
leaq -0x98(%rbp), %r15
movq %rbx, %rdi
movq %r14, %rsi
movq -0x48(%rbp), %rdx
movq %r13, %rcx
callq 0x19628
movq (%r14), %rax
movq 0x10(%rax), %rdi
movq 0x8(%rdi), %rsi
cmpq 0x10(%rdi), %rsi
je 0x19445
movq -0x30(%rbp), %rax
movq %rax, (%rsi)
addq $0x8, %rsi
movq %rsi, 0x8(%rdi)
jmp 0x1944e
leaq -0x30(%rbp), %rdx
callq 0x66e0
movq -0x38(%rbp), %rax
movq 0x38(%rax), %r13
movq %r15, %rdi
movq %rbx, %rsi
callq 0xeab4
movq (%r13), %rax
movq %r13, %rdi
movq %r15, %rsi
callq *0x8(%rax)
movq 0x10(%r12), %rsi
movq 0x8(%rax), %rdx
movq 0x10(%rax), %rcx
movq -0x58(%rbp), %rdi
callq 0x1a02c
movq -0x88(%rbp), %rax
testq %rax, %rax
je 0x1949d
movq %r15, %rdi
movq %r15, %rsi
movl $0x3, %edx
callq *%rax
movq -0x68(%rbp), %rax
testq %rax, %rax
je 0x194b3
movq %rbx, %rdi
movq %rbx, %rsi
movl $0x3, %edx
callq *%rax
movq -0x30(%rbp), %rax
leaq 0x1(%rax), %r13
movq %r13, -0x30(%rbp)
cmpq -0x40(%rbp), %rax
jl 0x19411
movq %r12, %rax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x5410
callq 0x5410
jmp 0x19507
jmp 0x19507
movq %rdx, %r15
movq %rax, %r14
jmp 0x195d3
jmp 0x194fd
jmp 0x194fd
jmp 0x194fd
movq %rax, %rdi
callq 0xeb19
jmp 0x19507
movq %rdx, %r15
movq %rax, %r14
jmp 0x1959c
movq %rdx, %r15
movq %rax, %r14
jmp 0x19545
movq %rdx, %r15
movq %rax, %r14
jmp 0x19585
movq %rdx, %r15
movq %rax, %r14
movq -0x88(%rbp), %rax
testq %rax, %rax
je 0x19545
leaq -0x98(%rbp), %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
movq -0x68(%rbp), %rax
testq %rax, %rax
je 0x1959c
leaq -0x78(%rbp), %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
jmp 0x1959c
jmp 0x194fd
jmp 0x194fd
movq %rdx, %r15
movq %rax, %r14
movq -0xa8(%rbp), %rax
testq %rax, %rax
je 0x19585
leaq -0xb8(%rbp), %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
movq -0x68(%rbp), %rax
testq %rax, %rax
je 0x1959c
leaq -0x78(%rbp), %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
cmpl $0x1, %r15d
jne 0x195d3
movq %r14, %rdi
callq 0x52b0
movq 0xd9f7(%rip), %rax      # 0x26fa8
addq $0x10, %rax
leaq -0x78(%rbp), %rbx
movq %rax, (%rbx)
movq %r14, %rdi
callq 0x50f0
movq %rbx, %rdi
callq 0x53f0
callq 0x5350
jmp 0x19608
cmpl $0x1, %r15d
jne 0x19611
movq %r14, %rdi
callq 0x52b0
movq 0xd9c0(%rip), %rax      # 0x26fa8
addq $0x10, %rax
leaq -0x78(%rbp), %rbx
movq %rax, (%rbx)
movq %r14, %rdi
callq 0x50f0
movq %rbx, %rdi
callq 0x53f0
callq 0x5350
movq -0x50(%rbp), %r12
jmp 0x194c9
movq %r14, %rdi
callq 0x53d0
jmp 0x194fd
jmp 0x194fd
jmp 0x194ec