// File: Bartor[P]kompilator/middle/ast_optimizer/ASTOptimizer.cpp
// Function: ASTOptimizer::constantExpressionOptimizer(Node*)
Node *ASTOptimizer::constantExpressionOptimizer(Node *node) {
    if (auto assignNode = dynamic_cast<Assignment *>(node)) {
        try {
            auto binaryExpression = dynamic_cast<BinaryExpression &>(assignNode->expression);
            try {
                auto lhsConstant = dynamic_cast<NumberValue &>(binaryExpression.lhs);
                try {
                    auto rhsConstant = dynamic_cast<NumberValue &>(binaryExpression.rhs);

                    long long newValue = 0;
                    switch (binaryExpression.type) {
                        case ADDITION:
                            newValue = lhsConstant.value + rhsConstant.value;
                            break;
                        case SUBTRACTION:
                            newValue = lhsConstant.value - rhsConstant.value;
                            break;
                        case MULTIPLICATION:
                            newValue = lhsConstant.value * rhsConstant.value;
                            break;
                        case DIVISION:
                            newValue = floor((double) lhsConstant.value / (double) rhsConstant.value);
                            break;
                        case MODULO:
                            newValue = lhsConstant.value - floor((double) lhsConstant.value / (double) rhsConstant.value) * rhsConstant.value;
                            break;
                    }

                    originalProgram->constants.constants.push_back(newValue);

                    return new Assignment(assignNode->identifier, *new UnaryExpression(*new NumberValue(newValue)));
                } catch (std::bad_cast _) {}
            } catch (std::bad_cast _) {}
        } catch (std::bad_cast _) {}
    }
    return node;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x120, %rsp            # imm = 0x120
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0xa8(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0xa0(%rbp)
cmpq $0x0, %rax
je 0x26636
movq -0xa0(%rbp), %rdi
leaq 0x19943(%rip), %rsi     # 0x3ff60
leaq 0x19d6c(%rip), %rdx     # 0x40390
xorl %eax, %eax
movl %eax, %ecx
callq 0x62f0
movq %rax, -0xb0(%rbp)
jmp 0x26641
xorl %eax, %eax
movq %rax, -0xb0(%rbp)
jmp 0x26641
movq -0xb0(%rbp), %rax
movq %rax, -0x20(%rbp)
cmpq $0x0, -0x20(%rbp)
je 0x26ab0
movq -0x20(%rbp), %rax
movq 0x10(%rax), %rdi
leaq 0x199ca(%rip), %rsi     # 0x40030
leaq 0x19c4b(%rip), %rdx     # 0x402b8
xorl %eax, %eax
movl %eax, %ecx
callq 0x62f0
movq %rax, -0xb8(%rbp)
cmpq $0x0, %rax
jne 0x2668a
callq 0x6530
jmp 0x2668a
movq -0xb8(%rbp), %rsi
leaq -0x40(%rbp), %rdi
callq 0x28e50
movq -0x38(%rbp), %rdi
leaq 0x199d3(%rip), %rsi     # 0x40078
leaq 0x19b34(%rip), %rdx     # 0x401e0
xorl %eax, %eax
movl %eax, %ecx
callq 0x62f0
movq %rax, -0xc0(%rbp)
cmpq $0x0, %rax
jne 0x266c9
callq 0x6530
jmp 0x266c9
movq -0xc0(%rbp), %rsi
leaq -0x60(%rbp), %rdi
callq 0x28eb0
movq -0x30(%rbp), %rdi
leaq 0x19994(%rip), %rsi     # 0x40078
leaq 0x19af5(%rip), %rdx     # 0x401e0
xorl %eax, %eax
movl %eax, %ecx
callq 0x62f0
movq %rax, -0xc8(%rbp)
cmpq $0x0, %rax
jne 0x26708
callq 0x6530
jmp 0x26708
movq -0xc8(%rbp), %rsi
leaq -0x70(%rbp), %rdi
callq 0x28eb0
movq $0x0, -0x78(%rbp)
movl -0x28(%rbp), %eax
movq %rax, -0xd0(%rbp)
subq $0x4, %rax
ja 0x26828
movq -0xd0(%rbp), %rax
leaq 0xc096(%rip), %rcx      # 0x327d8
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0x26a66
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0x269e1
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0x2695e
movq -0x58(%rbp), %rax
addq -0x68(%rbp), %rax
movq %rax, -0x78(%rbp)
jmp 0x26828
movq -0x58(%rbp), %rax
subq -0x68(%rbp), %rax
movq %rax, -0x78(%rbp)
jmp 0x26828
movq -0x58(%rbp), %rax
imulq -0x68(%rbp), %rax
movq %rax, -0x78(%rbp)
jmp 0x26828
cvtsi2sdq -0x58(%rbp), %xmm0
cvtsi2sdq -0x68(%rbp), %xmm1
divsd %xmm1, %xmm0
callq 0x6470
cvttsd2si %xmm0, %rax
movq %rax, -0x78(%rbp)
jmp 0x26828
cvtsi2sdq -0x58(%rbp), %xmm0
movsd %xmm0, -0xd8(%rbp)
cvtsi2sdq -0x58(%rbp), %xmm0
cvtsi2sdq -0x68(%rbp), %xmm1
divsd %xmm1, %xmm0
callq 0x6470
movsd -0xd8(%rbp), %xmm1
cvtsi2sdq -0x68(%rbp), %xmm2
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %rax
movq %rax, -0x78(%rbp)
movq -0xa8(%rbp), %rax
movq (%rax), %rax
movq 0x10(%rax), %rdi
leaq -0x78(%rbp), %rsi
callq 0x82e0
jmp 0x26841
movl $0x18, %edi
callq 0x6260
movq %rax, -0xe0(%rbp)
jmp 0x26854
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0xf0(%rbp)
movl $0x10, %edi
callq 0x6260
movq %rax, -0xe8(%rbp)
jmp 0x26876
movl $0x10, %edi
callq 0x6260
movq %rax, -0xf8(%rbp)
jmp 0x26889
movq -0xf8(%rbp), %rdi
movq -0x78(%rbp), %rsi
callq 0x87e0
jmp 0x2689b
movq -0xf8(%rbp), %rsi
movq -0xe8(%rbp), %rdi
callq 0x86e0
jmp 0x268b0
movq -0xe8(%rbp), %rdx
movq -0xf0(%rbp), %rsi
movq -0xe0(%rbp), %rdi
callq 0x8460
jmp 0x268cc
movq -0xe0(%rbp), %rax
movq %rax, -0x8(%rbp)
movl $0x1, -0x7c(%rbp)
leaq -0x70(%rbp), %rdi
callq 0xfa50
jmp 0x269b8
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0x26955
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0x26944
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0x26933
movq -0xf8(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
movl $0x10, %esi
callq 0x6270
movq -0xe8(%rbp), %rdi
movl $0x10, %esi
callq 0x6270
movq -0xe0(%rbp), %rdi
movl $0x18, %esi
callq 0x6270
leaq -0x70(%rbp), %rdi
callq 0xfa50
movl -0x4c(%rbp), %eax
movl $0x1, %ecx
cmpl %ecx, %eax
jne 0x269d8
movq -0x48(%rbp), %rdi
movq %rdi, -0x108(%rbp)
callq 0x6360
movq %rax, %rsi
leaq -0x88(%rbp), %rdi
movq %rdi, -0x100(%rbp)
callq 0x203d0
movq -0x108(%rbp), %rdi
callq 0x60f0
movq -0x100(%rbp), %rdi
callq 0x6500
callq 0x6450
jmp 0x269af
jmp 0x269b1
movl $0x0, -0x7c(%rbp)
leaq -0x60(%rbp), %rdi
callq 0xfa50
movl -0x7c(%rbp), %eax
testl %eax, %eax
jne 0x26a3b
jmp 0x269ca
jmp 0x26a34
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x60(%rbp), %rdi
callq 0xfa50
movl -0x4c(%rbp), %eax
movl $0x1, %ecx
cmpl %ecx, %eax
jne 0x26a5d
movq -0x48(%rbp), %rdi
movq %rdi, -0x118(%rbp)
callq 0x6360
movq %rax, %rsi
leaq -0x90(%rbp), %rdi
movq %rdi, -0x110(%rbp)
callq 0x203d0
movq -0x118(%rbp), %rdi
callq 0x60f0
movq -0x110(%rbp), %rdi
callq 0x6500
callq 0x6450
jmp 0x26a32
jmp 0x26a34
movl $0x0, -0x7c(%rbp)
leaq -0x40(%rbp), %rdi
callq 0xfe70
movl -0x7c(%rbp), %eax
testl %eax, %eax
je 0x26a4f
jmp 0x26a4d
jmp 0x26ab8
jmp 0x26aae
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x40(%rbp), %rdi
callq 0xfe70
movl -0x4c(%rbp), %eax
movl $0x1, %ecx
cmpl %ecx, %eax
jne 0x26ac5
movq -0x48(%rbp), %rdi
movq %rdi, -0x120(%rbp)
callq 0x6360
movq %rax, %rsi
leaq -0x98(%rbp), %rdi
callq 0x203d0
movq -0x120(%rbp), %rdi
callq 0x60f0
leaq -0x98(%rbp), %rdi
callq 0x6500
callq 0x6450
jmp 0x26ab0
movq -0x18(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x120, %rsp            # imm = 0x120
popq %rbp
retq
movq -0x48(%rbp), %rdi
callq 0x64d0
nop