// File: /workspace/github_cpp_makefile_star2_O1/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
movq %rsi, %rbx
movq %rdi, %r15
testq %rsi, %rsi
je 0x19211
leaq 0xde2d(%rip), %rsi      # 0x27028
leaq 0xe136(%rip), %rdx      # 0x27338
movq %rbx, %rdi
xorl %ecx, %ecx
callq 0x5240
movq %rax, %r14
jmp 0x19214
xorl %r14d, %r14d
testq %r14, %r14
je 0x194a7
movq 0x10(%r14), %rdi
leaq 0xe3c8(%rip), %rsi      # 0x275f0
leaq 0xe031(%rip), %rdx      # 0x27260
xorl %ecx, %ecx
callq 0x5240
testq %rax, %rax
je 0x193b3
movq %rax, %r12
movq 0x8(%rax), %rdi
movl 0x18(%rax), %r13d
leaq 0xe387(%rip), %rsi      # 0x275d8
leaq 0xdf30(%rip), %rdx      # 0x27188
xorl %ecx, %ecx
callq 0x5240
testq %rax, %rax
je 0x193b8
movq 0x10(%r12), %rdi
movq 0x8(%rax), %r12
leaq 0xe360(%rip), %rsi      # 0x275d8
leaq 0xdf09(%rip), %rdx      # 0x27188
xorl %ecx, %ecx
callq 0x5240
testq %rax, %rax
je 0x193bd
movq 0x8(%rax), %rax
movq $0x0, -0x30(%rbp)
cmpl $0x4, %r13d
ja 0x1930d
movl %r13d, %ecx
leaq 0x7485(%rip), %rdx      # 0x20730
movslq (%rdx,%rcx,4), %rcx
addq %rdx, %rcx
jmpq *%rcx
addq %r12, %rax
jmp 0x19309
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 0x19309
imulq %r12, %rax
jmp 0x19309
cvtsi2sd %r12, %xmm0
cvtsi2sd %rax, %xmm1
divsd %xmm1, %xmm0
callq 0x5370
cvttsd2si %xmm0, %rax
movq %rax, -0x30(%rbp)
movq (%r15), %rax
movq 0x10(%rax), %rdi
movq 0x8(%rdi), %rsi
cmpq 0x10(%rdi), %rsi
je 0x1932f
movq -0x30(%rbp), %rax
movq %rax, (%rsi)
addq $0x8, %rsi
movq %rsi, 0x8(%rdi)
jmp 0x19338
leaq -0x30(%rbp), %rdx
callq 0x6732
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 0xdded(%rip), %rdx      # 0x27158
addq $0x10, %rdx
movq %rdx, (%rax)
movq %rcx, 0x8(%rax)
leaq 0xde6b(%rip), %rcx      # 0x271e8
addq $0x10, %rcx
movq %rcx, (%r13)
movq %rax, 0x8(%r13)
leaq 0xdf78(%rip), %rax      # 0x27308
addq $0x10, %rax
movq %rax, (%r12)
movq %r14, 0x8(%r12)
movq %r13, 0x10(%r12)
jmp 0x194aa
subq %rax, %r12
movq %r12, -0x30(%rbp)
jmp 0x1930d
callq 0x5410
callq 0x5410
callq 0x5410
movq %rdx, %r15
movq %rax, %r14
movl $0x10, %esi
movq %r13, %rdi
callq 0x5200
jmp 0x193dd
movq %rdx, %r15
movq %rax, %r14
movl $0x18, %esi
movq %r12, %rdi
callq 0x5200
jmp 0x19404
jmp 0x193fe
movq %rdx, %r15
movq %rax, %r14
jmp 0x1943b
movq %rdx, %r15
movq %rax, %r14
jmp 0x19472
movq %rdx, %r15
movq %rax, %r14
cmpl $0x1, %r15d
jne 0x1943b
movq %r14, %rdi
callq 0x52b0
movq 0xeb8f(%rip), %rax      # 0x27fa8
addq $0x10, %rax
leaq -0x30(%rbp), %r15
movq %rax, (%r15)
movq %r14, %rdi
callq 0x50f0
movq %r15, %rdi
callq 0x53f0
callq 0x5350
jmp 0x194a7
cmpl $0x1, %r15d
jne 0x19472
movq %r14, %rdi
callq 0x52b0
movq 0xeb58(%rip), %rax      # 0x27fa8
addq $0x10, %rax
leaq -0x30(%rbp), %r15
movq %rax, (%r15)
movq %r14, %rdi
callq 0x50f0
movq %r15, %rdi
callq 0x53f0
callq 0x5350
jmp 0x194a7
cmpl $0x1, %r15d
jne 0x194bc
movq %r14, %rdi
callq 0x52b0
movq 0xeb21(%rip), %rax      # 0x27fa8
addq $0x10, %rax
leaq -0x30(%rbp), %r15
movq %rax, (%r15)
movq %r14, %rdi
callq 0x50f0
movq %r15, %rdi
callq 0x53f0
callq 0x5350
movq %rbx, %r12
movq %r12, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r14, %rdi
callq 0x53d0
jmp 0x193f6
jmp 0x193ee