// File: /workspace/github_cpp_makefile_star2_O3/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
testq %rsi, %rsi
je 0x18ff8
movq %rsi, %rbx
movq %rdi, %r15
leaq 0xd6d5(%rip), %rsi      # 0x26610
leaq 0xd42e(%rip), %rdx      # 0x26370
movq %rbx, %rdi
xorl %ecx, %ecx
callq 0x5240
testq %rax, %rax
je 0x190e7
movq %rax, %r14
movq 0x10(%rax), %rdi
leaq 0xd6ed(%rip), %rsi      # 0x26650
leaq 0xd32e(%rip), %rdx      # 0x26298
xorl %ecx, %ecx
callq 0x5240
testq %rax, %rax
je 0x1910f
movq %rax, %r12
movq 0x8(%rax), %rdi
movl 0x18(%rax), %r13d
leaq 0xd6ac(%rip), %rsi      # 0x26638
leaq 0xd22d(%rip), %rdx      # 0x261c0
xorl %ecx, %ecx
callq 0x5240
testq %rax, %rax
je 0x19114
movq 0x10(%r12), %rdi
movq 0x8(%rax), %r12
leaq 0xd685(%rip), %rsi      # 0x26638
leaq 0xd206(%rip), %rdx      # 0x261c0
xorl %ecx, %ecx
callq 0x5240
testq %rax, %rax
je 0x19119
movq 0x8(%rax), %rax
movq $0x0, -0x30(%rbp)
cmpl $0x4, %r13d
ja 0x19108
movl %r13d, %ecx
leaq 0x6736(%rip), %rdx      # 0x1f720
movslq (%rdx,%rcx,4), %rcx
addq %rdx, %rcx
jmpq *%rcx
addq %r12, %rax
jmp 0x1904f
xorl %ebx, %ebx
jmp 0x190e7
cvtsi2sd %r12, %xmm0
movsd %xmm0, -0x40(%rbp)
cvtsi2sd %rax, %xmm1
movsd %xmm1, -0x38(%rbp)
divsd %xmm1, %xmm0
callq 0x5370
mulsd -0x38(%rbp), %xmm0
movsd -0x40(%rbp), %xmm1
subsd %xmm0, %xmm1
cvttsd2si %xmm1, %rax
jmp 0x1904f
imulq %r12, %rax
jmp 0x1904f
cvtsi2sd %r12, %xmm0
cvtsi2sd %rax, %xmm1
divsd %xmm1, %xmm0
callq 0x5370
cvttsd2si %xmm0, %rax
movq %rax, -0x30(%rbp)
movq (%r15), %rcx
movq 0x10(%rcx), %rdi
movq 0x8(%rdi), %rsi
cmpq 0x10(%rdi), %rsi
je 0x19071
movq %rax, (%rsi)
addq $0x8, %rsi
movq %rsi, 0x8(%rdi)
jmp 0x1907a
leaq -0x30(%rbp), %rdx
callq 0x66e0
movl $0x18, %edi
callq 0x51f0
movq %rax, %r12
movq 0x8(%r14), %r14
movl $0x10, %edi
callq 0x51f0
movq %rax, %r13
movl $0x10, %edi
callq 0x51f0
movq -0x30(%rbp), %rcx
leaq 0xd0e3(%rip), %rdx      # 0x26190
addq $0x10, %rdx
movq %rdx, (%rax)
movq %rcx, 0x8(%rax)
leaq 0xd161(%rip), %rcx      # 0x26220
addq $0x10, %rcx
movq %rcx, (%r13)
movq %rax, 0x8(%r13)
leaq 0xd26e(%rip), %rax      # 0x26340
addq $0x10, %rax
movq %rax, (%r12)
movq %r14, 0x8(%r12)
movq %r13, 0x10(%r12)
movq %r12, %rbx
movq %rbx, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
subq %rax, %r12
movq %r12, -0x30(%rbp)
movq %r12, %rax
jmp 0x19053
xorl %eax, %eax
jmp 0x19053
callq 0x5410
callq 0x5410
callq 0x5410
movq %rdx, %r15
movq %rax, %r14
movl $0x10, %esi
movq %r13, %rdi
callq 0x5200
jmp 0x19139
movq %rdx, %r15
movq %rax, %r14
movl $0x18, %esi
movq %r12, %rdi
callq 0x5200
jmp 0x19160
jmp 0x1915a
movq %rdx, %r15
movq %rax, %r14
jmp 0x1919a
movq %rdx, %r15
movq %rax, %r14
jmp 0x191d4
movq %rdx, %r15
movq %rax, %r14
cmpl $0x1, %r15d
jne 0x1919a
movq %r14, %rdi
callq 0x52b0
movq 0xde33(%rip), %rax      # 0x26fa8
addq $0x10, %rax
leaq -0x30(%rbp), %r15
movq %rax, (%r15)
movq %r14, %rdi
callq 0x50f0
movq %r15, %rdi
callq 0x53f0
callq 0x5350
jmp 0x190e7
cmpl $0x1, %r15d
jne 0x191d4
movq %r14, %rdi
callq 0x52b0
movq 0xddf9(%rip), %rax      # 0x26fa8
addq $0x10, %rax
leaq -0x30(%rbp), %r15
movq %rax, (%r15)
movq %r14, %rdi
callq 0x50f0
movq %r15, %rdi
callq 0x53f0
callq 0x5350
jmp 0x190e7
cmpl $0x1, %r15d
jne 0x1920e
movq %r14, %rdi
callq 0x52b0
movq 0xddbf(%rip), %rax      # 0x26fa8
addq $0x10, %rax
leaq -0x30(%rbp), %r15
movq %rax, (%r15)
movq %r14, %rdi
callq 0x50f0
movq %r15, %rdi
callq 0x53f0
callq 0x5350
jmp 0x190e7
movq %r14, %rdi
callq 0x53d0
jmp 0x19152
jmp 0x1914a