// File: /workspace/github_cpp_makefile_star2_O2/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, %r14
movq %rsi, -0x70(%rbp)
movq %rdi, %rbx
movl 0x8(%r8), %eax
movl 0x1c(%r8), %ecx
xorl %edx, %edx
divl %ecx
movl %eax, %esi
movl %ecx, %edx
callq 0xdbc2
movq %rbx, %rdi
callq 0x1c4ae
movl 0x20(%r15), %eax
movq %rbx, -0x30(%rbp)
cmpl 0x8(%rbx), %eax
jne 0x1afb7
testl %eax, %eax
je 0x1b118
movq 0x18(%r15), %rbx
movq (%r13), %rcx
movq %rcx, -0x38(%rbp)
movq -0x30(%rbp), %r13
movq (%r13), %rcx
movq %rcx, -0x40(%rbp)
movl 0x10(%r15), %ecx
orl 0x8(%r15), %ecx
setne %cl
testb %r12b, %cl
je 0x1ad12
leaq -0x60(%rbp), %rcx
andq $0x0, 0x10(%rcx)
xorps %xmm0, %xmm0
movq %rcx, %r13
movaps %xmm0, (%rcx)
xorl %r12d, %r12d
movl %eax, %eax
cmpq %rax, %r12
jae 0x1adaa
movl 0x10(%r15), %eax
testl %eax, %eax
je 0x1ac5d
movl 0xc(%r15), %ecx
cmpq %rcx, %r12
setae %dl
addl %ecx, %eax
cmpq %rax, %r12
setb %al
testb %al, %dl
jne 0x1ad06
movl (%rbx,%r12,4), %eax
cmpl $0x1, %eax
ja 0x1acc2
movq -0x38(%rbp), %rcx
movl (%rcx,%r12,4), %esi
cmpb $0x0, 0x10(%rbp)
je 0x1ac7c
leal 0x1(%rsi), %ecx
cmpl $0x3, %ecx
jae 0x1acf0
cmpl $0x2, %esi
jb 0x1acd4
cmpl $-0x1, %esi
je 0x1acf0
shrl %esi
decl %esi
movq -0x70(%rbp), %rdi
movq (%rdi), %rax
callq *0x60(%rax)
movq -0x38(%rbp), %rcx
movl (%rcx,%r12,4), %ecx
andl $0x1, %ecx
xorl (%rbx,%r12,4), %eax
xorl %ecx, %eax
cmpl $0x1, %eax
je 0x1ad06
movl %r12d, -0x1408(%rbp)
movq %r13, %rdi
leaq -0x1408(%rbp), %rsi
callq 0xe0d2
jmp 0x1ad06
cmpl $-0x1, %eax
je 0x1ad06
movq -0x38(%rbp), %rcx
movl (%rcx,%r12,4), %esi
cmpl $-0x1, %esi
je 0x1acf0
cmpl %eax, %esi
je 0x1ad06
movl %r12d, -0x1408(%rbp)
movq %r13, %rdi
leaq -0x1408(%rbp), %rsi
callq 0xe0d2
jmp 0x1ad06
movl %r12d, -0x1408(%rbp)
movq %r13, %rdi
leaq -0x1408(%rbp), %rsi
callq 0xe0d2
incq %r12
movl 0x20(%r15), %eax
jmp 0x1ac30
movq %r14, -0x68(%rbp)
xorl %r14d, %r14d
xorl %r12d, %r12d
movl %eax, %eax
cmpq %rax, %r14
jae 0x1ada1
movl (%rbx,%r14,4), %ecx
cmpl $0x1, %ecx
ja 0x1ad74
movq -0x38(%rbp), %rax
movl (%rax,%r14,4), %esi
cmpb $0x0, 0x10(%rbp)
je 0x1ad42
leal 0x1(%rsi), %eax
cmpl $0x3, %eax
jae 0x1ad8a
cmpl $0x2, %esi
jb 0x1ad86
cmpl $-0x1, %esi
je 0x1ad8a
shrl %esi
decl %esi
movq -0x70(%rbp), %rdi
movq (%rdi), %rax
callq *0x60(%rax)
movl (%rbx,%r14,4), %ecx
movq -0x38(%rbp), %rdx
movl (%rdx,%r14,4), %edx
andl $0x1, %edx
xorl %ecx, %edx
xorl %edx, %eax
cmpl $0x1, %eax
jne 0x1ad8a
jmp 0x1ad95
cmpl $-0x1, %ecx
je 0x1ad95
movq -0x38(%rbp), %rax
movl (%rax,%r14,4), %esi
cmpl $-0x1, %esi
je 0x1ad8a
cmpl %ecx, %esi
je 0x1ad95
movq -0x40(%rbp), %rax
movl %ecx, (%rax,%r14,4)
incl %r12d
incq %r14
movl 0x20(%r15), %eax
jmp 0x1ad1c
movq -0x68(%rbp), %r14
jmp 0x1aee5
movl 0x8(%r15), %ecx
movq -0x60(%rbp), %rax
testq %rcx, %rcx
je 0x1ae4c
movq -0x58(%rbp), %rdx
movq %rdx, %rsi
subq %rax, %rsi
sarq $0x2, %rsi
cmpq %rcx, %rsi
movq -0x30(%rbp), %r13
jbe 0x1ae56
movq %r14, -0x68(%rbp)
leaq -0x1408(%rbp), %rdi
callq 0x1c3ce
movabsq $-0x100000000, %r13     # imm = 0xFFFFFFFF00000000
xorl %r14d, %r14d
movl 0x8(%r15), %eax
cmpq %rax, %r14
jae 0x1ae6b
movq -0x58(%rbp), %rax
subq -0x60(%rbp), %rax
sarq $0x2, %rax
movq %r13, %r12
sarq $0x20, %r12
addq %r12, %rax
andq $0x0, -0x80(%rbp)
movq %rax, -0x78(%rbp)
leaq -0x80(%rbp), %rdi
leaq -0x1408(%rbp), %rsi
callq 0x1c4ca
movq -0x60(%rbp), %rcx
movq -0x58(%rbp), %rdx
movl (%rdx,%r12,4), %edx
movl %edx, (%rcx,%rax,4)
incq %r14
movabsq $-0x100000000, %rax     # imm = 0xFFFFFFFF00000000
addq %rax, %r13
jmp 0x1adf3
movq -0x40(%rbp), %r8
movq -0x30(%rbp), %r13
jmp 0x1ae9e
jne 0x1b137
cmpq %rax, %rdx
movq -0x40(%rbp), %r8
je 0x1ae9e
movq %rax, -0x58(%rbp)
jmp 0x1ae9e
leaq -0x60(%rbp), %rdi
movq 0x8(%rdi), %rsi
subq (%rdi), %rsi
sarq $0x2, %rsi
subq %rax, %rsi
callq 0x1c4d8
leaq -0x1408(%rbp), %rdi
callq 0x1c454
movq -0x60(%rbp), %rax
movq -0x30(%rbp), %r13
movq -0x68(%rbp), %r14
movq -0x40(%rbp), %r8
movq -0x58(%rbp), %rcx
movq %rcx, %r12
subq %rax, %r12
je 0x1aed9
shrq $0x2, %r12
movq -0x38(%rbp), %rdi
cmpq %rcx, %rax
je 0x1aedc
movl (%rax), %edx
movl (%rbx,%rdx,4), %esi
cmpl $0x1, %esi
ja 0x1af98
cmpl $0x1, (%rdi,%rdx,4)
jbe 0x1af98
movl %esi, (%r8,%rdx,4)
addq $0x4, %rax
jmp 0x1aeb2
xorl %r12d, %r12d
leaq -0x60(%rbp), %rdi
callq 0x12486
testl %r12d, %r12d
je 0x1af83
movq 0x360bb(%rip), %rdi     # 0x50fb0
leaq 0x1d7b4(%rip), %rsi     # 0x386b0
callq 0xc290
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
movl 0x18(%rax,%rcx), %edx
andl $-0x4b, %edx
orl $0x2, %edx
movl %edx, 0x18(%rax,%rcx)
movl %r12d, %esi
movq %rax, %rdi
callq 0xc190
leaq 0x1d792(%rip), %rsi     # 0x386ba
movq %rax, %rdi
callq 0xc290
movq %rax, %rdi
movq %r14, %rsi
callq 0xc230
leaq 0x21ca1(%rip), %rsi     # 0x3cbe3
movq %rax, %rdi
callq 0xc290
movq %rax, %rdi
callq 0xc120
movq 0x36057(%rip), %rdi     # 0x50fb0
movq %r14, %rsi
callq 0xc230
leaq 0x16e2e(%rip), %rsi     # 0x31d96
movq %rax, %rdi
callq 0xc290
movq %rax, %rdi
movq %r13, %rsi
callq 0x2fd93
movq %rax, %rdi
callq 0xc120
movq %r13, %rax
addq $0x13e8, %rsp           # imm = 0x13E8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1d6af(%rip), %rdi     # 0x3864e
leaq 0x1d43f(%rip), %rsi     # 0x383e5
leaq 0x1d577(%rip), %rcx     # 0x38524
movl $0x13f, %edx            # imm = 0x13F
callq 0xc180
movq %r14, %rbx
leaq -0x1408(%rbp), %rdi
callq 0xc1c0
leaq -0x13f8(%rbp), %r14
leaq 0x1d50c(%rip), %rsi     # 0x384e0
movq %r14, %rdi
callq 0xc290
movq %rax, %rdi
movq %rbx, %rsi
callq 0xc230
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 0x1d4e7(%rip), %rsi     # 0x384f4
movq %r14, %rdi
callq 0xc290
movq %rax, %rdi
movq -0x30(%rbp), %rcx
movl 0x8(%rcx), %eax
xorl %edx, %edx
divl 0x1c(%rcx)
movq %rax, %rsi
callq 0xc190
leaq 0x1d4c0(%rip), %rsi     # 0x384f3
movq %rax, %rdi
callq 0xc290
movq -0x30(%rbp), %rcx
movl 0x1c(%rcx), %esi
movq %rax, %rdi
callq 0xc190
leaq 0x1a07e(%rip), %rsi     # 0x350cf
movq %rax, %rdi
callq 0xc290
leaq 0x1d496(%rip), %rsi     # 0x384f6
movq %r14, %rdi
callq 0xc290
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 0x1d466(%rip), %rsi     # 0x384f4
movq %r14, %rdi
callq 0xc290
movq %rax, %rdi
movl 0x20(%r15), %eax
xorl %edx, %edx
divl 0x34(%r15)
movq %rax, %rsi
callq 0xc190
leaq 0x1d441(%rip), %rsi     # 0x384f3
movq %rax, %rdi
callq 0xc290
movl 0x34(%r15), %esi
movq %rax, %rdi
callq 0xc190
leaq 0x1a002(%rip), %rsi     # 0x350cf
movq %rax, %rdi
callq 0xc290
pushq $0x10
popq %rdi
callq 0xc130
movq %rax, %r15
leaq -0x13f0(%rbp), %rsi
leaq -0x60(%rbp), %rdi
callq 0xc410
movb $0x1, %bl
leaq -0x60(%rbp), %rsi
movq %r15, %rdi
callq 0xc4b0
xorl %ebx, %ebx
movq 0x35ed1(%rip), %rsi     # 0x50fd8
movq 0x35eb2(%rip), %rdx     # 0x50fc0
movq %r15, %rdi
callq 0xc490
jmp 0x1b167
leaq 0x1d3f1(%rip), %rdi     # 0x38510
leaq 0x1d2bf(%rip), %rsi     # 0x383e5
leaq 0x1d3f7(%rip), %rcx     # 0x38524
movl $0x10e, %edx            # imm = 0x10E
callq 0xc180
pushq $0x10
popq %rdi
callq 0xc130
movq %rax, %r15
leaq 0x1d47f(%rip), %rsi     # 0x385c8
movq %rax, %rdi
callq 0xc4e0
movq 0x35e80(%rip), %rsi     # 0x50fd8
movq 0x35e61(%rip), %rdx     # 0x50fc0
movq %r15, %rdi
callq 0xc490
movq %rax, %r14
leaq -0x60(%rbp), %rdi
callq 0xc110
testb %bl, %bl
jne 0x1b17c
jmp 0x1b19c
movq %rax, %r14
movq %r15, %rdi
callq 0xc1d0
jmp 0x1b19c
jmp 0x1b1b4
movq %rax, %r14
movq %r15, %rdi
callq 0xc1d0
jmp 0x1b1c8
jmp 0x1b1b9
jmp 0x1b1b4
movq %rax, %r14
leaq -0x1408(%rbp), %rdi
callq 0xc1f0
jmp 0x1b1d6
jmp 0x1b1d3
jmp 0x1b1b4
jmp 0x1b1b4
jmp 0x1b1d3
jmp 0x1b1b4
movq %rax, %r14
jmp 0x1b1c8
movq %rax, %r14
leaq -0x1408(%rbp), %rdi
callq 0x1c454
leaq -0x60(%rbp), %rdi
callq 0x12486
jmp 0x1b1d6
movq %rax, %r14
movq -0x30(%rbp), %rdi
callq 0xde58
movq %r14, %rdi
callq 0xc4c0