// File: /workspace/github_cpp_makefile_star2_O2/vsklad[P]cgen/./bal/cnf/processor/cnfoptimizer.cpp
// Function: bal::CnfOptimizer::normalize_cu(unsigned int const*, unsigned int*) const
inline processor_result_t CnfOptimizer::normalize_cu(const uint32_t* const p_src, uint32_t* const p_dst) const {
        processor_result_t result = erUndetermined;
        
        const literalid_t* literals = _clause_literals(p_src);
        const clause_size_t clause_size = _clause_size(p_src);
        const literalid_t* const var_values = variables_.data();
            
        // an unaggregated clause indeed
        _assert_level_1(!_clause_size_is_aggregated(clause_size));
        
        literalid_t* new_literals = _clause_literals(p_dst);
        clause_size_t new_clause_size = 0;
        
        for (auto i = 0; i < clause_size; i++) {
            const literalid_t original_value = literals[i]; // because p_src may == p_dst
            const literalid_t new_value = literal_t::resolve(var_values, original_value);
            // count resolved literal if not a constant
            if (literal_t__is_variable(new_value)) {
                if (new_value != original_value) {
                    __TRACE_ASSIGN_LITERAL;
                    if (result == erUndetermined || result == erChangedC) {
                        result = erChangedV;
                    };
                };
                // insert into the sorted sequence
                auto j = new_clause_size;
                while (j > 0) {
                    if (new_value >= new_literals[j - 1]) {
                        new_literals[j] = new_value;
                        break;
                    } else {
                        new_literals[j] = new_literals[j - 1];
                        j--;
                    };
                };
                if (j == 0) {
                    new_literals[0] = new_value;
                    new_clause_size++;
                } else if (new_value == new_literals[j - 1]) {
                    // check duplicate; should be rare
                    for (auto k = j; k < new_clause_size; k++) {
                        new_literals[k] = new_literals[k + 1];
                    };
                } else {
                    // check presence of a variable together with its negation
                    if (literal_t__is_same_variable(new_value, new_literals[j - 1])) {
                        result = erSatisfied;
                        break;
                    };
                    new_clause_size++;
                };
                // check presence of a variable together with its negation
                if (j < new_clause_size - 1 && literal_t__is_same_variable(new_value, new_literals[j + 1])) {
                    result = erSatisfied;
                    break;
                };
            } else if (literal_t__is_constant_1(new_value)) {
                result = erSatisfied;
                break;
            } else {
                // a literal is 0; the clause is shortened
                __TRACE_ASSIGN_LITERAL;
                if (result == erUndetermined) {
                    result = erChangedC;
                };
            };
        };
        
        if (result != erSatisfied) {
            if (_clause_size_is_aggregated(new_clause_size)) {
                // the clause has become aggregated; unnegate literals and build flags
                uint16_t flags = 0x1;
                for (auto i = 0; i < new_clause_size; i++) {
                    if (literal_t__is_negation(new_literals[i])) {
                        literal_t__unnegate(new_literals[i]);
                    } else {
                        flags <<= (0x1 << i);
                    };
                };
                _clause_header_set(p_dst, flags, new_clause_size);
            } else {
                _clause_header_set(p_dst, 0, new_clause_size);
            }
        } else {
            _clause_header_set(p_dst, 0, 0);
        };
    
        return result;
    }

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rsi, -0x40(%rbp)
movl (%rsi), %eax
andl $0x7fff, %eax           # imm = 0x7FFF
movq %rax, -0x48(%rbp)
cmpl $0x4, %eax
jbe 0x119a2
movq %rdx, %rbx
movq 0xb8(%rdi), %rax
movq (%rax), %rax
movq %rax, -0x38(%rbp)
xorl %r12d, %r12d
xorl %r15d, %r15d
movl $0x0, -0x2c(%rbp)
cmpq -0x48(%rbp), %r12
je 0x11942
movq -0x40(%rbp), %rax
movl 0x4(%rax,%r12,4), %r14d
movq -0x38(%rbp), %rdi
movl %r14d, %esi
callq 0xeae2
movl %eax, %r13d
leal -0x2(%r13), %eax
cmpl $-0x4, %eax
ja 0x11887
cmpl %r14d, %r13d
je 0x118d7
movq 0x3fa9c(%rip), %rdi     # 0x51300
pushq $0x2
popq %rax
movl %eax, -0x2c(%rbp)
testq %rdi, %rdi
je 0x118d7
testb $0x1, %r14b
jne 0x118c5
orl $0x1, %r14d
movl %r13d, %edx
xorl $0x1, %edx
movq (%rdi), %rax
movl %r14d, %esi
jmp 0x118ce
cmpl $0x1, %r13d
je 0x11982
movq 0x3fa68(%rip), %rdi     # 0x51300
testq %rdi, %rdi
je 0x118b7
testb $0x1, %r14b
jne 0x118ab
orl $0x1, %r14d
xorl $0x1, %r13d
movq (%rdi), %rax
movl %r14d, %esi
movl %r13d, %edx
callq *0x48(%rax)
movl -0x2c(%rbp), %eax
cmpl $0x1, %eax
adcl $0x0, %eax
movl %eax, -0x2c(%rbp)
jmp 0x1193a
movq (%rdi), %rax
movl %r14d, %esi
movl %r13d, %edx
callq *0x48(%rax)
pushq $0x2
popq %rax
movl %eax, -0x2c(%rbp)
movzwl %r15w, %eax
testw %ax, %ax
je 0x118f3
movzwl %ax, %ecx
movl (%rbx,%rcx,4), %edx
cmpl %edx, %r13d
jae 0x118f9
movl %edx, 0x4(%rbx,%rcx,4)
decl %eax
jmp 0x118db
movl %r13d, 0x4(%rbx)
jmp 0x1191e
movl %r13d, 0x4(%rbx,%rcx,4)
jne 0x11916
movzwl %r15w, %edx
cmpq %rdx, %rcx
jae 0x11921
movl 0x8(%rbx,%rcx,4), %esi
movl %esi, 0x4(%rbx,%rcx,4)
incq %rcx
jmp 0x11904
xorl %r13d, %edx
cmpl $0x2, %edx
jb 0x11982
incl %r15d
movzwl %r15w, %ecx
decl %ecx
cmpl %eax, %ecx
jle 0x1193a
movzwl %ax, %eax
movl 0x8(%rbx,%rax,4), %eax
xorl %r13d, %eax
cmpl $0x2, %eax
jb 0x11982
incq %r12
jmp 0x1182d
movzwl %r15w, %esi
cmpw $0x4, %r15w
ja 0x1198e
movzwl %r15w, %r8d
movw $0x1, %di
xorl %eax, %eax
pushq $0x1
popq %r9
cmpq %rax, %r8
je 0x11989
movl 0x4(%rbx,%rax,4), %ecx
testb $0x1, %cl
jne 0x11972
orl $0x1, %ecx
movl %ecx, 0x4(%rbx,%rax,4)
jmp 0x1197d
movl %r9d, %edx
movl %eax, %ecx
shll %cl, %edx
movl %edx, %ecx
shll %cl, %edi
incq %rax
jmp 0x1195b
xorl %esi, %esi
pushq $0x3
popq %rax
jmp 0x11991
shll $0x10, %edi
orl %edi, %esi
movl -0x2c(%rbp), %eax
movl %esi, (%rbx)
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x22b55(%rip), %rdi     # 0x344fe
leaq 0x22458(%rip), %rsi     # 0x33e08
leaq 0x22b5b(%rip), %rcx     # 0x34512
movl $0x12a, %edx            # imm = 0x12A
callq 0xc180