// File: Bartor[P]kompilator/middle/ast_optimizer/ASTOptimizer.cpp
// Function: ASTOptimizer::constantConditionRemover(Node*)
Node *ASTOptimizer::constantConditionRemover(Node *node) {
    if (auto whileNode = dynamic_cast<While *>(node)) {
        if (checkTautology(whileNode->condition) == ALWAYS) {
            std::cout << "  |[w] Infinite loop detected" << std::endl;
            return node;
        } else if (checkTautology(whileNode->condition) == NEVER) {
            return new CommandList();
        }
    } else if (auto ifNode = dynamic_cast<If *>(node)) {
        if (checkTautology(ifNode->condition) == ALWAYS) {
            return ifNode->commands.copy(identity);
        } else if (checkTautology(ifNode->condition) == NEVER) {
            return new CommandList();
        }
    } else if (auto ifElseNode = dynamic_cast<IfElse *>(node)) {
        if (checkTautology(ifElseNode->condition) == ALWAYS) {
            return ifElseNode->commands.copy(identity);
        } else if (checkTautology(ifElseNode->condition) == NEVER) {
            return ifElseNode->elseCommands.copy(identity);
        }
    }
    return node;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x130, %rsp            # imm = 0x130
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0xa8(%rbp)
cmpq $0x0, %rax
je 0x2707b
movq -0xa8(%rbp), %rdi
leaq 0x18efe(%rip), %rsi     # 0x3ff60
leaq 0x193ff(%rip), %rdx     # 0x40468
xorl %eax, %eax
movl %eax, %ecx
callq 0x62f0
movq %rax, -0xb0(%rbp)
jmp 0x27086
xorl %eax, %eax
movq %rax, -0xb0(%rbp)
jmp 0x27086
movq -0xb0(%rbp), %rax
movq %rax, -0x20(%rbp)
cmpq $0x0, -0x20(%rbp)
je 0x27146
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x25710
cmpl $0x2, %eax
jne 0x270dd
movq 0x19f13(%rip), %rdi     # 0x40fc8
leaq 0xb847(%rip), %rsi      # 0x32903
callq 0x6250
movq %rax, %rdi
movq 0x19ec5(%rip), %rsi     # 0x40f90
callq 0x6280
movq -0x18(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x27415
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x25710
cmpl $0x0, %eax
jne 0x2713f
movl $0x20, %edi
callq 0x6260
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0xb8(%rbp)
callq 0x8420
jmp 0x2710d
movq -0xb8(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x27415
movq -0xb8(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x28(%rbp)
movl %eax, -0x2c(%rbp)
movl $0x20, %esi
callq 0x6270
jmp 0x27422
jmp 0x27141
jmp 0x2740d
movq -0x18(%rbp), %rax
movq %rax, -0xc0(%rbp)
cmpq $0x0, %rax
je 0x2717e
movq -0xc0(%rbp), %rdi
leaq 0x18dfb(%rip), %rsi     # 0x3ff60
leaq 0x1926c(%rip), %rdx     # 0x403d8
xorl %eax, %eax
movl %eax, %ecx
callq 0x62f0
movq %rax, -0xc8(%rbp)
jmp 0x27189
xorl %eax, %eax
movq %rax, -0xc8(%rbp)
jmp 0x27189
movq -0xc8(%rbp), %rax
movq %rax, -0x38(%rbp)
cmpq $0x0, -0x38(%rbp)
je 0x27293
movq -0x38(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x25710
cmpl $0x2, %eax
jne 0x2722a
movq -0x38(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0xe0(%rbp)
leaq 0x1a279(%rip), %rsi     # 0x41440
leaq -0x58(%rbp), %rdi
movq %rdi, -0xd8(%rbp)
callq 0x11590
movq -0xe0(%rbp), %rdi
movq -0xd8(%rbp), %rsi
movq (%rdi), %rax
movq 0x8(%rax), %rax
callq *%rax
movq %rax, -0xd0(%rbp)
jmp 0x271f7
movq -0xd0(%rbp), %rax
movq %rax, -0x8(%rbp)
leaq -0x58(%rbp), %rdi
callq 0x11640
jmp 0x27415
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x28(%rbp)
movl %eax, -0x2c(%rbp)
leaq -0x58(%rbp), %rdi
callq 0x11640
jmp 0x27422
movq -0x38(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x25710
cmpl $0x0, %eax
jne 0x2728c
movl $0x20, %edi
callq 0x6260
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0xe8(%rbp)
callq 0x8420
jmp 0x2725a
movq -0xe8(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x27415
movq -0xe8(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x28(%rbp)
movl %eax, -0x2c(%rbp)
movl $0x20, %esi
callq 0x6270
jmp 0x27422
jmp 0x2728e
jmp 0x2740b
movq -0x18(%rbp), %rax
movq %rax, -0xf0(%rbp)
cmpq $0x0, %rax
je 0x272cb
movq -0xf0(%rbp), %rdi
leaq 0x18cae(%rip), %rsi     # 0x3ff60
leaq 0x19167(%rip), %rdx     # 0x40420
xorl %eax, %eax
movl %eax, %ecx
callq 0x62f0
movq %rax, -0xf8(%rbp)
jmp 0x272d6
xorl %eax, %eax
movq %rax, -0xf8(%rbp)
jmp 0x272d6
movq -0xf8(%rbp), %rax
movq %rax, -0x60(%rbp)
cmpq $0x0, -0x60(%rbp)
je 0x27409
movq -0x60(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x25710
cmpl $0x2, %eax
jne 0x27377
movq -0x60(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x110(%rbp)
leaq 0x1a12c(%rip), %rsi     # 0x41440
leaq -0x80(%rbp), %rdi
movq %rdi, -0x108(%rbp)
callq 0x11590
movq -0x110(%rbp), %rdi
movq -0x108(%rbp), %rsi
movq (%rdi), %rax
movq 0x8(%rax), %rax
callq *%rax
movq %rax, -0x100(%rbp)
jmp 0x27344
movq -0x100(%rbp), %rax
movq %rax, -0x8(%rbp)
leaq -0x80(%rbp), %rdi
callq 0x11640
jmp 0x27415
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x28(%rbp)
movl %eax, -0x2c(%rbp)
leaq -0x80(%rbp), %rdi
callq 0x11640
jmp 0x27422
movq -0x60(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x25710
cmpl $0x0, %eax
jne 0x27405
movq -0x60(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0x128(%rbp)
leaq 0x1a0a1(%rip), %rsi     # 0x41440
leaq -0xa0(%rbp), %rdi
movq %rdi, -0x120(%rbp)
callq 0x11590
movq -0x128(%rbp), %rdi
movq -0x120(%rbp), %rsi
movq (%rdi), %rax
movq 0x8(%rax), %rax
callq *%rax
movq %rax, -0x118(%rbp)
jmp 0x273d2
movq -0x118(%rbp), %rax
movq %rax, -0x8(%rbp)
leaq -0xa0(%rbp), %rdi
callq 0x11640
jmp 0x27415
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x28(%rbp)
movl %eax, -0x2c(%rbp)
leaq -0xa0(%rbp), %rdi
callq 0x11640
jmp 0x27422
jmp 0x27407
jmp 0x27409
jmp 0x2740b
jmp 0x2740d
movq -0x18(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x130, %rsp            # imm = 0x130
popq %rbp
retq
movq -0x28(%rbp), %rdi
callq 0x64d0
nopl (%rax,%rax)