// File: /workspace/github_cpp_makefile_star2_O2/kc885[P]SAT-Solver-with-CDCL/cdcl.cpp
// Function: ck_cdcl_solver::Solver::unitPropagate(std::unordered_set<int, std::hash<int>, std::equal_to<int>, std::allocator<int>>&, int)
bool unitPropagate(std::unordered_set<int> &implied, int level){
            //visited array
            std::vector<int> visited(clauses.size(),0);
            
            std::unordered_set<int> intermediate;
            

            //unit propagation
            
            int new_imply_flag;
            do{
                new_imply_flag = 0;
                for (int i =0; i < (int)clauses.size(); i++){
                    if(visited[i] == 0){
                        
                        visited[i] = 1;
                        
                        //analyze this clause
                        int val = 0;
                        int undef_num = 0;
                        int target_var;
                        
                        for(auto iter = clauses[i].begin(); iter != clauses[i].end(); iter++){
                            if(val_decisionLevel[std::abs(*iter)] == 0){
                                undef_num++;
                                target_var = *iter;
                                if(undef_num > 1)
                                    break;
                            }
                            else if(val_decisionLevel[std::abs(*iter)] * (*iter) > 0){
                                val = 1;
                                break;
                            }
                        }
                        
                        if(val == 0 ){
                            //conflict
                            if(undef_num == 0){
                               
                                //learn a new clause
                                std::unordered_set<int> learnt_level;
                                std::unordered_set<int> learnt;
                                
                                for(auto iter = intermediate.begin(); iter!= intermediate.end();iter++){
                                    if(intermediate.count(-(*iter)) == 0){
                                        varToClause[*iter].push_back(clauses.size());
                                        learnt.insert(*iter);
                                    }
                                }

 
                                clauses.push_back(learnt);
                                
                                for(auto iter = implied.begin(); iter != implied.end(); iter++)
                                    val_decisionLevel[*iter] = 0;
                                return false;
                            }
                            //imply a new variable
                            else if(undef_num == 1){
                                new_imply_flag = 1;
                                implied.insert(std::abs(target_var));
                                
                                
                                val_decisionLevel[std::abs(target_var)] = (((target_var) > 0) - ((target_var) < 0))*level;
                                
                                //get variables which help to imply this variable(inclusing itself)
                                for(auto iter = clauses[i].begin(); iter != clauses[i].end(); iter++){
                                    intermediate.insert(*iter);
                                }
                                
                                for(auto iter = varToClause[target_var].begin();iter != varToClause[target_var].end(); iter++)
                                    visited[*iter] = 0;
                                for(auto iter = varToClause[-target_var].begin();iter != varToClause[-target_var].end(); iter++)
                                    visited[*iter] = 0;
                                break;        
                            }
                        }
                          
                    }  
                }

            }while(new_imply_flag == 1);
            return true;
        }

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xf8, %rsp
movl %edx, 0x1c(%rsp)
movq %rsi, 0x20(%rsp)
movq %rdi, %r14
leaq 0x38(%rdi), %rax
movq %rax, 0x28(%rsp)
movq 0x40(%rdi), %rax
subq 0x38(%rdi), %rax
pushq $0x38
popq %rcx
cqto
idivq %rcx
leaq 0x88(%rsp), %r12
andl $0x0, (%r12)
leaq 0x38(%rsp), %rdi
leaq 0x50(%rsp), %r13
movq %rax, %rsi
movq %r12, %rdx
movq %r13, %rcx
callq 0x4cbe
leaq 0xb8(%rsp), %rax
movq %rax, -0x30(%rax)
movq $0x1, -0x28(%rax)
leaq 0x98(%rsp), %rax
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
movl $0x3f800000, 0x10(%rax) # imm = 0x3F800000
movq %rax, 0x10(%rsp)
movups %xmm0, 0x18(%rax)
leaq 0x8(%rsp), %r15
movq 0x38(%r14), %rcx
movq 0x40(%r14), %rax
subq %rcx, %rax
cqto
pushq $0x38
popq %rsi
idivq %rsi
movq %rax, %rdx
movq 0x38(%rsp), %rax
testl %edx, %edx
movq %rdx, 0x30(%rsp)
movl $0x0, %esi
cmovlel %esi, %edx
xorl %ebx, %ebx
cmpq %rdx, %rbx
je 0x4ac2
cmpl $0x0, (%rax,%rbx,4)
je 0x487c
incq %rbx
jmp 0x4868
movl $0x1, (%rax,%rbx,4)
imulq $0x38, %rbx, %rbp
leaq (%rcx,%rbp), %rdi
addq $0x10, %rdi
movq 0x50(%r14), %r8
movb $0x1, %r9b
movq (%rdi), %rdi
testq %rdi, %rdi
je 0x48d6
movl 0x8(%rdi), %r10d
movl %r10d, %r11d
negl %r11d
cmovsl %r10d, %r11d
movl (%r8,%r11,4), %r11d
testl %r11d, %r11d
je 0x48c0
imull %r10d, %r11d
testl %r11d, %r11d
jle 0x4896
jmp 0x4877
movl %r10d, 0x8(%rsp)
movl %r10d, %esi
testb $0x1, %r9b
movl $0x0, %r9d
jne 0x4896
jmp 0x4877
testb $0x1, %r9b
jne 0x49b9
movl %esi, %eax
negl %eax
cmovsl %esi, %eax
movl %eax, 0x50(%rsp)
movq 0x20(%rsp), %rdi
movq %r13, %rsi
callq 0x4d8c
movl 0x8(%rsp), %eax
movl %eax, %ecx
sarl $0x1f, %ecx
xorl %edx, %edx
testl %eax, %eax
setne %dl
orl %ecx, %edx
imull 0x1c(%rsp), %edx
movl %eax, %ecx
negl %ecx
cmovsl %eax, %ecx
movq 0x50(%r14), %rax
movl %edx, (%rax,%rcx,4)
movq 0x38(%r14), %rax
leaq (%rax,%rbp), %rbx
addq $0x10, %rbx
movq (%rbx), %rbx
testq %rbx, %rbx
je 0x493f
leaq 0x8(%rbx), %rsi
movq %r12, %rdi
callq 0x3300
jmp 0x4929
movq %r14, %rdi
movq %r15, %rsi
callq 0x360c
movq (%rax), %rbx
movq %r14, %rdi
movq %r15, %rsi
callq 0x360c
cmpq 0x8(%rax), %rbx
je 0x4970
movslq (%rbx), %rax
movq 0x38(%rsp), %rcx
andl $0x0, (%rcx,%rax,4)
addq $0x4, %rbx
jmp 0x494d
xorl %eax, %eax
subl 0x8(%rsp), %eax
movl %eax, 0x50(%rsp)
movq %r14, %rdi
movq %r13, %rsi
callq 0x2dc4
movq (%rax), %rbx
xorl %eax, %eax
subl 0x8(%rsp), %eax
movl %eax, 0x50(%rsp)
movq %r14, %rdi
movq %r13, %rsi
callq 0x2dc4
cmpq 0x8(%rax), %rbx
je 0x483c
movslq (%rbx), %rax
movq 0x38(%rsp), %rcx
andl $0x0, (%rcx,%rax,4)
addq $0x4, %rbx
jmp 0x4988
leaq 0x80(%rsp), %rax
movq %rax, -0x30(%rax)
pushq $0x1
popq %rcx
movq %rcx, -0x28(%rax)
xorps %xmm0, %xmm0
movups %xmm0, -0x20(%rax)
movl $0x3f800000, %edx       # imm = 0x3F800000
movl %edx, -0x10(%rax)
movups %xmm0, -0x8(%rax)
leaq 0xf0(%rsp), %rax
movq %rax, -0x30(%rax)
movq %rcx, -0x28(%rax)
movups %xmm0, -0x20(%rax)
movl %edx, -0x10(%rax)
movups %xmm0, -0x8(%rax)
leaq 0x88(%rsp), %r15
leaq 0xc(%rsp), %r12
leaq 0xc0(%rsp), %rbp
movq 0x10(%rsp), %rcx
movq (%rcx), %rcx
testq %rcx, %rcx
je 0x4a7a
xorl %eax, %eax
movq %rcx, 0x10(%rsp)
subl 0x8(%rcx), %eax
movl %eax, 0xc(%rsp)
movq %r15, %rdi
movq %r12, %rsi
callq 0x3270
testq %rax, %rax
jne 0x4a0f
movq 0x10(%rsp), %rax
leaq 0x8(%rax), %r13
movq %r14, %rdi
movq %r13, %rsi
callq 0x360c
movq %rax, %rdi
movq 0x40(%r14), %rax
subq 0x38(%r14), %rax
cqto
pushq $0x38
popq %rcx
idivq %rcx
movl %eax, 0xc(%rsp)
movq %r12, %rsi
callq 0x2c38
movq %rbp, %rdi
movq %r13, %rsi
callq 0x3300
jmp 0x4a0f
leaq 0xc0(%rsp), %rsi
movq 0x28(%rsp), %rdi
callq 0x2bdc
movq 0x20(%rsp), %rdx
addq $0x10, %rdx
movq 0x50(%r14), %rax
movq (%rdx), %rdx
testq %rdx, %rdx
je 0x4aab
movslq 0x8(%rdx), %rcx
andl $0x0, (%rax,%rcx,4)
jmp 0x4a99
leaq 0xc0(%rsp), %rdi
callq 0x3bec
leaq 0x50(%rsp), %rdi
callq 0x3bec
movslq 0x30(%rsp), %rax
cmpq %rax, %rbx
setge %bl
leaq 0x88(%rsp), %rdi
callq 0x3bec
leaq 0x38(%rsp), %rdi
callq 0x31bc
movl %ebx, %eax
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x4b00
jmp 0x4b00
jmp 0x4b00
jmp 0x4b24
movq %rax, %rbx
leaq 0xc0(%rsp), %rdi
callq 0x3bec
leaq 0x50(%rsp), %rdi
callq 0x3bec
jmp 0x4b27
jmp 0x4b24
jmp 0x4b24
jmp 0x4b24
jmp 0x4b24
movq %rax, %rbx
leaq 0x88(%rsp), %rdi
callq 0x3bec
leaq 0x38(%rsp), %rdi
callq 0x31bc
movq %rbx, %rdi
callq 0x2210