// File: /workspace/github_cpp_makefile_star2_O2/vsklad[P]cgen/./bal/anf/anf.cpp
// Function: bal::Anf::complete_equation(unsigned int, bool)
literalid_t Anf::complete_equation(const literalid_t r, const bool optimize_negation) {
        is_incomplete_last_equation_();
        
        const size_t equation_terms_size = terms_size_(equations_.size() - 1);
        const size_t const_term_index = equations_[equations_.size() - 1];
        
        literalid_t result = symbols_[terms_[const_term_index]];
        _assert_level_1(literal_t__is_constant(result));
        
        if (equation_terms_size == 1) {
            _assert_level_1(literal_t__is_unassigned(r));
            remove_last_equation_();
        } else if (optimize_negation && equation_terms_size == 2 && symbols_size_(const_term_index + 1) == 1) {
            // one term one one variable
            // negated because 1 means + 1 which means "1 negated"
            _assert_level_1(literal_t__is_unassigned(r));
            result = literal_t__substitute_literal(literal_t__negated(result), symbols_[terms_[const_term_index + 1]]);
            remove_last_equation_();
        } else if (literal_t__is_constant(r) && equation_terms_size == 2 && symbols_size_(const_term_index + 1) == 1) {
            // one term one one variable
            // unit clause
            _assert_level_1(!optimize_negation);
            _assert_level_1(literal_t__is_variable(symbols_[terms_[const_term_index + 1]]));
            result = symbols_[terms_[const_term_index + 1]];
            result = literal_t__substitute_literal(literal_t__negated(r), result);
            result = literal_t__substitute_literal(literal_t__negated(symbols_[terms_[const_term_index]]), result);
            last_equation_remove_term_(const_term_index + 1);
            symbols_[terms_[const_term_index]] = result;
            // variable value, negation because the equation (result == 0)
            result = literal_t__is_negation(result) ? 0b1 : 0b0;
        } else {
            // multiple terms
            literalid_t last_literal;
            if (literal_t__is_variable(r) || literal_t__is_constant(r)) {
                _assert_level_1(!optimize_negation);
                last_literal = r;
                if (literal_t__is_constant(last_literal)) {
                    // try to find a single variable term and merge with it
                    for (auto i = const_term_index + 1; i < const_term_index + equation_terms_size - 1; i++) {
                        if (symbols_size_(i) == 1) {
                            _assert_level_1(literal_t__is_variable(symbols_[terms_[i]]));
                            last_literal = literal_t__substitute_literal(literal_t__negated(r), symbols_[terms_[i]]);
                            last_equation_remove_term_(i);
                            break;
                        };
                    };
                };
            } else {
                _assert_level_1(literal_t__is_unassigned(r));
                last_literal = VariableGenerator::new_variable_literal();
            };
            
            // 1 + x = ~x, therefore apply negated sign to the last_literal
            result = literal_t__substitute_literal(literal_t__negated(result), last_literal);
            
            if (optimize_negation) {
                // negation is within the result, the equation should include the same unnegated
                symbols_[terms_[const_term_index]] = last_literal;
            } else {
                symbols_[terms_[const_term_index]] = result;
                result = last_literal;
            };
        };
        
        return result;
    }

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movl %edx, -0x2c(%rbp)
movl %esi, %r14d
movq %rdi, %rbx
callq 0xd8c4
movq 0xb0(%rbx), %rsi
subq 0xa8(%rbx), %rsi
sarq $0x2, %rsi
decq %rsi
movq %rbx, %rdi
callq 0xd994
movq %rax, %r13
movq 0xb0(%rbx), %rax
movl -0x4(%rax), %r12d
movq 0x78(%rbx), %rax
movq 0x90(%rbx), %rcx
movl (%rcx,%r12,4), %ecx
movl (%rax,%rcx,4), %r15d
cmpl $0x2, %r15d
jae 0xd0fe
cmpq $0x1, %r13
jne 0xcf56
cmpl $-0x1, %r14d
jne 0xd11d
movq %rbx, %rdi
callq 0xdb28
jmp 0xd0ec
cmpq $0x2, %r13
setne %al
movl -0x2c(%rbp), %ecx
xorb $0x1, %cl
orb %al, %cl
jne 0xcf9a
leaq 0x1(%r12), %rsi
movq %rbx, %rdi
callq 0xda06
cmpq $0x1, %rax
jne 0xcf9a
cmpl $-0x1, %r14d
jne 0xd199
movq 0x78(%rbx), %rax
movq 0x90(%rbx), %rcx
movl 0x4(%rcx,%r12,4), %ecx
xorl (%rax,%rcx,4), %r15d
jmp 0xcf49
cmpl $0x1, %r14d
ja 0xd028
cmpq $0x2, %r13
jne 0xd028
leaq 0x1(%r12), %rsi
movq %rbx, %rdi
movq %rsi, -0x38(%rbp)
callq 0xda06
cmpq $0x1, %rax
jne 0xd032
cmpb $0x0, -0x2c(%rbp)
jne 0xd1b8
movq 0x78(%rbx), %rax
movq 0x90(%rbx), %rdx
movl 0x4(%rdx,%r12,4), %ecx
movl (%rax,%rcx,4), %ecx
leal 0x1(%rcx), %esi
cmpl $0x2, %esi
jbe 0xd13c
movl (%rdx,%r12,4), %edx
movl (%rax,%rdx,4), %eax
andl $0x1, %eax
xorl %ecx, %r14d
xorl %eax, %r14d
movl %r14d, %r15d
notl %r15d
movq %rbx, %rdi
movq -0x38(%rbp), %rsi
callq 0xda44
movq 0x78(%rbx), %rax
movq 0x90(%rbx), %rcx
movl (%rcx,%r12,4), %ecx
movl %r14d, (%rax,%rcx,4)
andl $0x1, %r15d
jmp 0xd0ec
cmpl $-0x1, %r14d
je 0xd0b3
cmpb $0x0, -0x2c(%rbp)
jne 0xd15b
cmpl $0x1, %r14d
ja 0xd0a3
movq %r12, %rax
addq %r13, %r12
decq %r12
movq %rax, -0x38(%rbp)
movq %rax, %r13
incq %r13
cmpq %r12, %r13
jae 0xd09d
movq %rbx, %rdi
movq %r13, %rsi
callq 0xda06
cmpq $0x1, %rax
jne 0xd052
movq 0x78(%rbx), %rax
movq 0x90(%rbx), %rcx
movl (%rcx,%r13,4), %ecx
movl (%rax,%rcx,4), %r12d
leal 0x1(%r12), %eax
cmpl $0x2, %eax
jbe 0xd17a
xorl %r14d, %r12d
movq %rbx, %rdi
movq %r13, %rsi
callq 0xda44
movl %r12d, %r14d
movq -0x38(%rbp), %r12
jmp 0xd0c3
xorl %r14d, %r15d
movq 0x78(%rbx), %rax
movq 0x90(%rbx), %rcx
jmp 0xd0e1
movq (%rbx), %rax
movq %rbx, %rdi
callq *(%rax)
leal 0x3(,%rax,2), %r14d
xorl %r14d, %r15d
movq 0x78(%rbx), %rax
movq 0x90(%rbx), %rcx
cmpb $0x0, -0x2c(%rbp)
je 0xd0e1
movl (%rcx,%r12,4), %ecx
movl %r14d, (%rax,%rcx,4)
jmp 0xd0ec
movl (%rcx,%r12,4), %ecx
movl %r15d, (%rax,%rcx,4)
movl %r14d, %r15d
movl %r15d, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x23fd7(%rip), %rdi     # 0x310dc
leaq 0x23f13(%rip), %rsi     # 0x3101f
leaq 0x23ff2(%rip), %rcx     # 0x31105
movl $0xc5, %edx
callq 0xc180
leaq 0x24911(%rip), %rdi     # 0x31a35
leaq 0x23ef4(%rip), %rsi     # 0x3101f
leaq 0x23fd3(%rip), %rcx     # 0x31105
movl $0xc8, %edx
callq 0xc180
leaq 0x2401c(%rip), %rdi     # 0x3115f
leaq 0x23ed5(%rip), %rsi     # 0x3101f
leaq 0x23fb4(%rip), %rcx     # 0x31105
movl $0xd4, %edx
callq 0xc180
leaq 0x23fea(%rip), %rdi     # 0x3114c
leaq 0x23eb6(%rip), %rsi     # 0x3101f
leaq 0x23f95(%rip), %rcx     # 0x31105
movl $0xe0, %edx
callq 0xc180
leaq 0x24068(%rip), %rdi     # 0x311e9
leaq 0x23e97(%rip), %rsi     # 0x3101f
leaq 0x23f76(%rip), %rcx     # 0x31105
movl $0xe6, %edx
callq 0xc180
leaq 0x24895(%rip), %rdi     # 0x31a35
leaq 0x23e78(%rip), %rsi     # 0x3101f
leaq 0x23f57(%rip), %rcx     # 0x31105
movl $0xcd, %edx
callq 0xc180
leaq 0x23f8d(%rip), %rdi     # 0x3114c
leaq 0x23e59(%rip), %rsi     # 0x3101f
leaq 0x23f38(%rip), %rcx     # 0x31105
movl $0xd3, %edx
callq 0xc180
nop