// File: /workspace/github_cpp_makefile_star2_O2/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 0x1391f
movq %rsi, %rbx
movq %rdi, %r15
leaq 0xbdaf(%rip), %rsi      # 0x1f618
leaq 0xbb08(%rip), %rdx      # 0x1f378
movq %rbx, %rdi
xorl %ecx, %ecx
callq 0x51f0
testq %rax, %rax
je 0x139f1
movq %rax, %r14
movq 0x10(%rax), %rdi
leaq 0xbdc7(%rip), %rsi      # 0x1f658
leaq 0xba08(%rip), %rdx      # 0x1f2a0
xorl %ecx, %ecx
callq 0x51f0
testq %rax, %rax
je 0x13a0f
movq %rax, %r12
movq 0x8(%rax), %rdi
movl 0x18(%rax), %r13d
leaq 0xbd86(%rip), %rsi      # 0x1f640
leaq 0xb907(%rip), %rdx      # 0x1f1c8
xorl %ecx, %ecx
callq 0x51f0
testq %rax, %rax
je 0x13a14
movq 0x10(%r12), %rdi
movq 0x8(%rax), %r12
leaq 0xbd5f(%rip), %rsi      # 0x1f640
leaq 0xb8e0(%rip), %rdx      # 0x1f1c8
xorl %ecx, %ecx
callq 0x51f0
testq %rax, %rax
je 0x13a19
movq 0x8(%rax), %rax
andq $0x0, -0x30(%rbp)
cmpl $0x4, %r13d
ja 0x1397a
movl %r13d, %ecx
leaq 0x4e0f(%rip), %rdx      # 0x18720
movslq (%rdx,%rcx,4), %rcx
addq %rdx, %rcx
jmpq *%rcx
addq %r12, %rax
jmp 0x13976
xorl %ebx, %ebx
jmp 0x139f1
cvtsi2sd %r12, %xmm0
movsd %xmm0, -0x40(%rbp)
cvtsi2sd %rax, %xmm1
movsd %xmm1, -0x38(%rbp)
divsd %xmm1, %xmm0
callq 0x5340
mulsd -0x38(%rbp), %xmm0
movsd -0x40(%rbp), %xmm1
subsd %xmm0, %xmm1
cvttsd2si %xmm1, %rax
jmp 0x13976
imulq %r12, %rax
jmp 0x13976
cvtsi2sd %r12, %xmm0
cvtsi2sd %rax, %xmm1
divsd %xmm1, %xmm0
callq 0x5340
cvttsd2si %xmm0, %rax
movq %rax, -0x30(%rbp)
movq (%r15), %rax
movq 0x10(%rax), %rdi
leaq -0x30(%rbp), %rsi
callq 0x6086
pushq $0x18
popq %rdi
callq 0x51c0
movq %rax, %r12
movq 0x8(%r14), %r14
pushq $0x10
popq %rdi
callq 0x51c0
movq %rax, %r13
pushq $0x10
popq %rdi
callq 0x51c0
movq -0x30(%rbp), %rcx
leaq 0xb7e1(%rip), %rdx      # 0x1f198
addq $0x10, %rdx
movq %rdx, (%rax)
movq %rcx, 0x8(%rax)
leaq 0xb85f(%rip), %rcx      # 0x1f228
addq $0x10, %rcx
movq %rcx, (%r13)
movq %rax, 0x8(%r13)
leaq 0xb96c(%rip), %rax      # 0x1f348
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)
jmp 0x1397a
callq 0x53d0
callq 0x53d0
callq 0x53d0
movq %rdx, %r15
movq %rax, %r14
pushq $0x10
popq %rsi
movq %r13, %rdi
callq 0x51d0
jmp 0x13a37
movq %rdx, %r15
movq %rax, %r14
pushq $0x18
popq %rsi
movq %r12, %rdi
callq 0x51d0
jmp 0x13a5c
jmp 0x13a56
movq %rdx, %r15
movq %rax, %r14
jmp 0x13a96
movq %rdx, %r15
movq %rax, %r14
jmp 0x13ad0
movq %rdx, %r15
movq %rax, %r14
cmpl $0x1, %r15d
jne 0x13a96
movq %r14, %rdi
callq 0x5250
movq 0xc53f(%rip), %rax      # 0x1ffb0
addq $0x10, %rax
leaq -0x30(%rbp), %r15
movq %rax, (%r15)
movq %r14, %rdi
callq 0x50d0
movq %r15, %rdi
callq 0x53b0
callq 0x5320
jmp 0x139f1
cmpl $0x1, %r15d
jne 0x13ad0
movq %r14, %rdi
callq 0x5250
movq 0xc505(%rip), %rax      # 0x1ffb0
addq $0x10, %rax
leaq -0x30(%rbp), %r15
movq %rax, (%r15)
movq %r14, %rdi
callq 0x50d0
movq %r15, %rdi
callq 0x53b0
callq 0x5320
jmp 0x139f1
cmpl $0x1, %r15d
jne 0x13b0a
movq %r14, %rdi
callq 0x5250
movq 0xc4cb(%rip), %rax      # 0x1ffb0
addq $0x10, %rax
leaq -0x30(%rbp), %r15
movq %rax, (%r15)
movq %r14, %rdi
callq 0x50d0
movq %r15, %rdi
callq 0x53b0
callq 0x5320
jmp 0x139f1
movq %r14, %rdi
callq 0x5390
jmp 0x13a4e
jmp 0x13a46