// File: /workspace/github_cpp_makefile_star2_O3/vsklad[P]cgen/commands.cpp
// Function: variable_generate_value(bal::Formula const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, CGenVariableInfo const&, bal::VariablesArray const&, bool, bool)
bal::VariablesArray variable_generate_value(const bal::Formula& formula,
                             const std::string name,
                             const CGenVariableInfo& src,
                             const bal::VariablesArray& dst_template,
                             const bool apply_except,
                             const bool reassign_in_formula) {
    bal::VariablesArray dst(dst_template.size() / dst_template.element_size(), dst_template.element_size());
    dst.assign_unassigned();
    
    // ensure that the value is provided/generated/computed
    if (src.data.size() != dst.size()) {
        std::stringstream error_message;
        error_message << "Invalid value for " << name;
        error_message << std::dec << "[" << dst.size() / dst.element_size() << "][" << dst.element_size() << "]";
        error_message << " due to wrong dimensions ";
        error_message << std::dec << "[" << src.data.size() / src.data.element_size() << "][" << src.data.element_size() << "]";
        throw std::invalid_argument(error_message.str());
    };
    
    _assert_level_0(src.data.size() > 0);
    bal::variables_size_t dst_changes_count = 0;
    
    const bal::literalid_t* p_src = src.data.data();
    const bal::literalid_t* p_tmp = dst_template.data();
    bal::literalid_t* p_dst = dst.data();
    
    if ((src.except_count > 0 || src.except_range_size > 0) && apply_except) {
        // generate list of element indexes to assign
        std::vector<unsigned int> assignable_indexes;

        // assignable are non-constant dst elements with corresponding constants in src
        // except for the exclusion range
        for (auto i = 0; i < src.data.size(); i++) {
            if ((src.except_range_size == 0 ||
                 i < src.except_range_first || i >= src.except_range_first + src.except_range_size)) {
                if ((literal_t__is_constant(p_src[i]) && literal_t__is_variable(p_tmp[i]) && reassign_in_formula) ||
                    (!literal_t__is_unassigned(p_src[i]) && literal_t__is_unassigned(p_tmp[i]))) {
                    assignable_indexes.push_back(i);
                } else if (literal_t__is_constant(p_src[i]) && literal_t__is_variable(p_tmp[i])) {
                    const bal::literalid_t formula_value = formula.get_variable_value(literal_t__variable_id(p_tmp[i]));
                    if (p_src[i] != literal_t__substitute_literal(p_tmp[i], formula_value)) {
                        assignable_indexes.push_back(i);
                    };
                } else if (!literal_t__is_unassigned(p_src[i]) && p_tmp[i] != p_src[i]) {
                    assignable_indexes.push_back(i);
                };
            };
        };
        
        if (src.except_count > 0) {
            if (assignable_indexes.size() > src.except_count) {
                // pick except_count elements randomly and remove them
                std::random_device rd;
                for (auto i = 0; i < src.except_count; i++) {
                    std::uniform_int_distribution<size_t> index_distribution(0, assignable_indexes.size() - i - 1);
                    size_t random_index = index_distribution(rd);
                    assignable_indexes[random_index] = assignable_indexes[assignable_indexes.size() - i - 1];
                };
                assignable_indexes.resize(assignable_indexes.size() - src.except_count);
            } else if (assignable_indexes.size() == src.except_count) {
                assignable_indexes.clear();
            } else {
                throw std::invalid_argument(ERROR_EXCEPT_NO_VARIABLES);
            };
        };
        
        if (assignable_indexes.size() > 0) {
            for (auto index: assignable_indexes) {
                _assert_level_0(literal_t__is_constant(p_src[index]) && !literal_t__is_constant(p_tmp[index]));
                p_dst[index] = p_src[index];
            };
            dst_changes_count = (bal::variables_size_t)assignable_indexes.size();
        };
    } else {
        // no except, simply copy the values, ignore unassigned src
        for (auto i = 0; i < src.data.size(); i++) {
            if ((literal_t__is_constant(p_src[i]) && literal_t__is_variable(p_tmp[i]) && reassign_in_formula) ||
                (!literal_t__is_unassigned(p_src[i]) && literal_t__is_unassigned(p_tmp[i]))) {
                p_dst[i] = p_src[i];
                dst_changes_count++;
            } else if (literal_t__is_constant(p_src[i]) && literal_t__is_variable(p_tmp[i])) {
                const bal::literalid_t formula_value = formula.get_variable_value(literal_t__variable_id(p_tmp[i]));
                if (p_src[i] != literal_t__substitute_literal(p_tmp[i], formula_value)) {
                    p_dst[i] = p_src[i];
                    dst_changes_count++;
                };
            } else if (!literal_t__is_unassigned(p_src[i]) && p_tmp[i] != p_src[i]) {
                p_dst[i] = p_src[i];
                dst_changes_count++;
            };
        };
    };
    
    if (dst_changes_count > 0) {
        std::cout << "Assigned " << std::dec << dst_changes_count << " bit(s) in \"" << name << "\"" << std::endl;
        std::cout << name << " = " << dst << std::endl;
    };
    
    return dst;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x13e8, %rsp           # imm = 0x13E8
movl %r9d, %r12d
movq %r8, %r13
movq %rcx, %r15
movq %rdx, -0x70(%rbp)
movq %rsi, -0x60(%rbp)
movq %rdi, %rbx
movl 0x8(%r8), %eax
movl 0x1c(%r8), %ecx
xorl %edx, %edx
divl %ecx
movl %eax, %esi
movl %ecx, %edx
callq 0xe5fc
cmpl $0x0, 0x8(%rbx)
movq %rbx, -0x58(%rbp)
je 0x1f5c0
movq (%rbx), %rax
xorl %ecx, %ecx
movl $0xffffffff, (%rax,%rcx,4) # imm = 0xFFFFFFFF
incq %rcx
movl 0x8(%rbx), %edx
cmpq %rdx, %rcx
jb 0x1f15c
cmpl %edx, 0x20(%r15)
jne 0x1f5e6
testl %edx, %edx
je 0x1f5c7
movq 0x18(%r15), %r14
movq (%r13), %r13
movq (%rbx), %rbx
movl 0x10(%r15), %eax
orl 0x8(%r15), %eax
setne %al
testb %r12b, %al
je 0x1f37d
xorps %xmm0, %xmm0
leaq -0x50(%rbp), %rax
movaps %xmm0, (%rax)
movq $0x0, 0x10(%rax)
xorl %r12d, %r12d
movl 0x10(%r15), %eax
testl %eax, %eax
je 0x1f1d6
movl 0xc(%r15), %ecx
cmpq %rcx, %r12
setae %dl
addl %ecx, %eax
cmpq %rax, %r12
setb %al
testb %al, %dl
jne 0x1f2ac
movl (%r14,%r12,4), %eax
cmpl $0x1, %eax
ja 0x1f248
movl (%r13,%r12,4), %esi
cmpb $0x0, 0x10(%rbp)
je 0x1f1f6
leal 0x1(%rsi), %ecx
cmpl $0x3, %ecx
jae 0x1f27e
cmpl $0x2, %esi
jb 0x1f257
cmpl $-0x1, %esi
je 0x1f27e
shrl %esi
decl %esi
movq -0x60(%rbp), %rdi
movq (%rdi), %rax
callq *0x60(%rax)
movl (%r13,%r12,4), %ecx
andl $0x1, %ecx
xorl (%r14,%r12,4), %eax
xorl %ecx, %eax
cmpl $0x1, %eax
je 0x1f2ac
movl %r12d, -0x1408(%rbp)
movq -0x48(%rbp), %rsi
cmpq -0x40(%rbp), %rsi
jne 0x1f28f
leaq -0x50(%rbp), %rdi
leaq -0x1408(%rbp), %rdx
callq 0xe7da
jmp 0x1f2ac
cmpl $-0x1, %eax
je 0x1f2ac
movl (%r13,%r12,4), %esi
cmpl $-0x1, %esi
je 0x1f27e
cmpl %eax, %esi
je 0x1f2ac
movl %r12d, -0x1408(%rbp)
movq -0x48(%rbp), %rsi
cmpq -0x40(%rbp), %rsi
jne 0x1f28f
leaq -0x50(%rbp), %rdi
leaq -0x1408(%rbp), %rdx
callq 0xe7da
jmp 0x1f2ac
movl %r12d, -0x1408(%rbp)
movq -0x48(%rbp), %rsi
cmpq -0x40(%rbp), %rsi
je 0x1f29c
movl %r12d, (%rsi)
addq $0x4, %rsi
movq %rsi, -0x48(%rbp)
jmp 0x1f2ac
leaq -0x50(%rbp), %rdi
leaq -0x1408(%rbp), %rdx
callq 0xe7da
incq %r12
movl 0x20(%r15), %eax
cmpq %rax, %r12
jb 0x1f1b4
movl 0x8(%r15), %eax
movq -0x50(%rbp), %rdi
testq %rax, %rax
je 0x1f432
movq -0x48(%rbp), %rcx
movq %rcx, %rdx
subq %rdi, %rdx
sarq $0x2, %rdx
cmpq %rax, %rdx
jbe 0x1f3fc
leaq -0x1408(%rbp), %rdi
callq 0x2180a
cmpl $0x0, 0x8(%r15)
movq -0x50(%rbp), %rcx
movq -0x48(%rbp), %rsi
je 0x1f40d
movq %rbx, -0x68(%rbp)
movq %r13, -0x60(%rbp)
xorl %ebx, %ebx
movabsq $-0x100000000, %r13     # imm = 0xFFFFFFFF00000000
subq %rcx, %rsi
sarq $0x2, %rsi
movq %r13, %r12
sarq $0x20, %r12
addq %r12, %rsi
movq $0x0, -0x80(%rbp)
movq %rsi, -0x78(%rbp)
leaq -0x80(%rbp), %rdi
leaq -0x1408(%rbp), %rsi
movq %rdi, %rdx
callq 0x2c5f4
movq -0x50(%rbp), %rcx
movq -0x48(%rbp), %rsi
movl (%rsi,%r12,4), %edx
movl %edx, (%rcx,%rax,4)
movl 0x8(%r15), %eax
movabsq $-0x100000000, %rdx     # imm = 0xFFFFFFFF00000000
addq %rdx, %r13
incl %ebx
cmpl %ebx, %eax
ja 0x1f317
subq %rcx, %rsi
movq -0x60(%rbp), %r13
movq -0x68(%rbp), %rbx
jmp 0x1f412
movq %rbx, -0x68(%rbp)
xorl %ebx, %ebx
xorl %r12d, %r12d
movl (%r14,%rbx,4), %ecx
cmpl $0x1, %ecx
ja 0x1f3d1
movl (%r13,%rbx,4), %esi
cmpb $0x0, 0x10(%rbp)
je 0x1f3a2
leal 0x1(%rsi), %eax
cmpl $0x3, %eax
jae 0x1f3e4
cmpl $0x2, %esi
jb 0x1f3e0
cmpl $-0x1, %esi
je 0x1f3e4
shrl %esi
decl %esi
movq -0x60(%rbp), %rdi
movq (%rdi), %rax
callq *0x60(%rax)
movl (%r14,%rbx,4), %ecx
movl (%r13,%rbx,4), %edx
andl $0x1, %edx
xorl %ecx, %edx
xorl %edx, %eax
cmpl $0x1, %eax
jne 0x1f3e4
jmp 0x1f3ee
cmpl $-0x1, %ecx
je 0x1f3ee
movl (%r13,%rbx,4), %esi
cmpl $-0x1, %esi
je 0x1f3e4
cmpl %ecx, %esi
je 0x1f3ee
movq -0x68(%rbp), %rax
movl %ecx, (%rax,%rbx,4)
incl %r12d
incq %rbx
movl 0x20(%r15), %eax
cmpq %rax, %rbx
jb 0x1f386
jmp 0x1f47b
jne 0x1f77c
cmpq %rdi, %rcx
je 0x1f432
movq %rdi, -0x48(%rbp)
jmp 0x1f432
subq %rcx, %rsi
xorl %eax, %eax
sarq $0x2, %rsi
subq %rax, %rsi
leaq -0x50(%rbp), %rdi
callq 0x219d2
leaq -0x1408(%rbp), %rdi
callq 0xb270
movq -0x50(%rbp), %rdi
movq -0x48(%rbp), %rax
movq %rax, %r12
subq %rdi, %r12
je 0x1f46e
shrq $0x2, %r12
movq %rdi, %rcx
movl (%rcx), %edx
movl (%r14,%rdx,4), %esi
cmpl $0x1, %esi
ja 0x1f5a1
cmpl $0x1, (%r13,%rdx,4)
jbe 0x1f5a1
movl %esi, (%rbx,%rdx,4)
addq $0x4, %rcx
cmpq %rax, %rcx
jne 0x1f445
jmp 0x1f471
xorl %r12d, %r12d
testq %rdi, %rdi
je 0x1f47b
callq 0xb280
testl %r12d, %r12d
movq -0x58(%rbp), %rbx
movq -0x70(%rbp), %r15
je 0x1f58c
movq 0x3cb1d(%rip), %r14     # 0x5bfb0
leaq 0x272d6(%rip), %rsi     # 0x46770
movl $0x9, %edx
movq %r14, %rdi
callq 0xb320
movq (%r14), %rax
movq -0x18(%rax), %rax
movl 0x18(%r14,%rax), %ecx
andl $-0x4b, %ecx
orl $0x2, %ecx
movl %ecx, 0x18(%r14,%rax)
movl %r12d, %esi
movq %r14, %rdi
callq 0xb190
movq %rax, %r14
leaq 0x272a7(%rip), %rsi     # 0x4677a
movl $0xc, %edx
movq %rax, %rdi
callq 0xb320
movq (%r15), %rsi
movq 0x8(%r15), %rdx
movq %r14, %rdi
callq 0xb320
movq %rax, %r14
leaq 0x2b4e3(%rip), %rsi     # 0x4a9dc
movl $0x1, %edx
movq %rax, %rdi
callq 0xb320
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0xb2a0
movsbl %al, %esi
movq %r14, %rdi
callq 0xb030
movq %rax, %rdi
callq 0xb1a0
movq (%r15), %rsi
movq 0x8(%r15), %rdx
movq 0x3ca75(%rip), %rdi     # 0x5bfb0
callq 0xb320
movq %rax, %r14
leaq 0x20838(%rip), %rsi     # 0x3fd82
movl $0x3, %edx
movq %rax, %rdi
callq 0xb320
movq %r14, %rdi
movq %rbx, %rsi
callq 0x3d449
movq %rax, %r14
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0xb2a0
movsbl %al, %esi
movq %r14, %rdi
callq 0xb030
movq %rax, %rdi
callq 0xb1a0
movq %rbx, %rax
addq $0x13e8, %rsp           # imm = 0x13E8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x27166(%rip), %rdi     # 0x4670e
leaq 0x26ef6(%rip), %rsi     # 0x464a5
leaq 0x2702e(%rip), %rcx     # 0x465e4
movl $0x13f, %edx            # imm = 0x13F
callq 0xb180
cmpl $0x0, 0x20(%r15)
jne 0x1f5e6
leaq 0x27002(%rip), %rdi     # 0x465d0
leaq 0x26ed0(%rip), %rsi     # 0x464a5
leaq 0x27008(%rip), %rcx     # 0x465e4
movl $0x10e, %edx            # imm = 0x10E
callq 0xb180
leaq -0x1408(%rbp), %rdi
callq 0xb1c0
leaq -0x13f8(%rbp), %r14
leaq 0x26fa0(%rip), %rsi     # 0x465a0
movl $0x12, %edx
movq %r14, %rdi
callq 0xb320
movq -0x70(%rbp), %rax
movq (%rax), %rsi
movq 0x8(%rax), %rdx
movq %r14, %rdi
callq 0xb320
movq -0x13f8(%rbp), %rax
movq -0x18(%rax), %rax
movl -0x13e0(%rbp,%rax), %ecx
andl $-0x4b, %ecx
orl $0x2, %ecx
movl %ecx, -0x13e0(%rbp,%rax)
leaq 0x26f6e(%rip), %rsi     # 0x465b4
movl $0x1, %edx
movq %r14, %rdi
callq 0xb320
movq -0x58(%rbp), %rcx
movl 0x8(%rcx), %eax
xorl %edx, %edx
divl 0x1c(%rcx)
movq %r14, %rdi
movq %rax, %rsi
callq 0xb190
movq %rax, %r12
leaq 0x26f3f(%rip), %rsi     # 0x465b3
movl $0x2, %edx
movq %rax, %rdi
callq 0xb320
movq -0x58(%rbp), %rax
movl 0x1c(%rax), %esi
movq %r12, %rdi
callq 0xb190
leaq 0x23a38(%rip), %rsi     # 0x430cf
movl $0x1, %edx
movq %rax, %rdi
callq 0xb320
leaq 0x26f0b(%rip), %rsi     # 0x465b6
movl $0x19, %edx
movq %r14, %rdi
callq 0xb320
movq -0x13f8(%rbp), %rax
movq -0x18(%rax), %rax
movl -0x13e0(%rbp,%rax), %ecx
andl $-0x4b, %ecx
orl $0x2, %ecx
movl %ecx, -0x13e0(%rbp,%rax)
leaq 0x26ed6(%rip), %rsi     # 0x465b4
movl $0x1, %edx
movq %r14, %rdi
callq 0xb320
movl 0x20(%r15), %eax
xorl %edx, %edx
divl 0x34(%r15)
movq %r14, %rdi
movq %rax, %rsi
callq 0xb190
movq %rax, %r14
leaq 0x26ea9(%rip), %rsi     # 0x465b3
movl $0x2, %edx
movq %rax, %rdi
callq 0xb320
movl 0x34(%r15), %esi
movq %r14, %rdi
callq 0xb190
leaq 0x239a5(%rip), %rsi     # 0x430cf
movl $0x1, %edx
movq %rax, %rdi
callq 0xb320
movl $0x10, %edi
callq 0xb130
movq %rax, %r15
leaq -0x13f0(%rbp), %rsi
leaq -0x50(%rbp), %rdi
callq 0xb430
movb $0x1, %bl
leaq -0x50(%rbp), %rsi
movq %r15, %rdi
callq 0xb4d0
xorl %ebx, %ebx
movq 0x3c86d(%rip), %rsi     # 0x5bfd8
movq 0x3c84e(%rip), %rdx     # 0x5bfc0
movq %r15, %rdi
callq 0xb4b0
jmp 0x1f7ae
movl $0x10, %edi
callq 0xb130
movq %rax, %r15
leaq 0x26ef8(%rip), %rsi     # 0x46688
movq %rax, %rdi
callq 0xb520
movq 0x3c839(%rip), %rsi     # 0x5bfd8
movq 0x3c81a(%rip), %rdx     # 0x5bfc0
movq %r15, %rdi
callq 0xb4b0
movq %rax, %r14
leaq -0x40(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x1f7c3
callq 0xb280
testb %bl, %bl
jne 0x1f7cc
jmp 0x1f7f0
movq %rax, %r14
movq %r15, %rdi
callq 0xb1e0
jmp 0x1f7f0
jmp 0x1f817
movq %rax, %r14
movq %r15, %rdi
callq 0xb1e0
jmp 0x1f82d
jmp 0x1f83d
jmp 0x1f81e
jmp 0x1f817
jmp 0x1f817
movq %rax, %r14
movq 0x3c789(%rip), %rsi     # 0x5bf80
leaq -0x1408(%rbp), %rdi
callq 0xb1f0
leaq -0x1388(%rbp), %rdi
callq 0xb0d0
jmp 0x1f848
jmp 0x1f845
jmp 0x1f817
jmp 0x1f817
movq %rax, %r14
jmp 0x1f82d
jmp 0x1f845
movq %rax, %r14
leaq -0x1408(%rbp), %rdi
callq 0xb270
movq -0x50(%rbp), %rdi
testq %rdi, %rdi
je 0x1f848
callq 0xb280
jmp 0x1f848
movq %rax, %rdi
callq 0x1d8eb
movq %rax, %r14
movq -0x58(%rbp), %rdi
callq 0x27774
movq %r14, %rdi
callq 0xb4e0