// File: /workspace/github_cpp_makefile_star2_O2/Myyura[P]MyyuraSat/./source/./solver_search.cpp
// Function: MyyuraSat::Solver::analyze(unsigned int, MyyuraSat::Vector<MyyuraSat::Literal, int>&, int&)
void Solver::analyze(CRARef conflict, Vector<Literal>& out_learnt, int& out_level) {
    if (conflict == CRAREF_UNDEF) {
        throw std::logic_error("No conflict clause needs to be analyzed!");
    }
    std::cout << "analyze begin=============" << std::endl;
    // Generate conflict clause:
    int path_conflict = 0;
    Literal p = LITERAL_UNDEF;

    // (leave room for the asserting literal)
    out_learnt.push();
    int index = _trail.size() - 1;

    do {
        std::cout << path_conflict << std::endl;
        if (conflict == CRAREF_UNDEF) {
            throw std::logic_error("Solver::analyze : no conflict to analyze!");
        }
        
        Clause& c = _ca[conflict];

        for (int j = (p == LITERAL_UNDEF) ? 0 : 1; j < c.size(); j++){
            Literal q = c[j];

            if (!_seen[q.variable()] && level(q.variable()) > 0){
                _seen[q.variable()] = 1;
                if (level(q.variable()) >= decision_level())
                    path_conflict++;
                else
                    out_learnt.push(q);
            }
        }
        
        // Select next clause to look at:
        while (!_seen[_trail[index--].variable()]);
        p = _trail[index + 1];
        conflict = reason(p.variable());
        _seen[p.variable()] = 0;
        path_conflict--;

    } while (path_conflict > 0);
    out_learnt[0] = ~p;

    // Find correct backtrack level:
    if (out_learnt.size() == 1) {
        out_level = 0;
    } else {
        int max_i = 1;
        // Find the first literal assigned at the next-highest level:
        for (int i = 2; i < out_learnt.size(); i++) {
            if (level(out_learnt[i].variable()) > 
                level(out_learnt[max_i].variable())) {
                max_i = i;
            }
        }
        // Swap-in this literal at index 1:
        Literal p = out_learnt[max_i];
        out_learnt[max_i] = out_learnt[1];
        out_learnt[1] = p;
        out_level = level(p.variable());
    }

    // Clear _seen
    for (Variable i = 0; i < n_variables(); i++) {
        _seen[i] = 0;
    }

    std::cout << "analyze end==========================" << std::endl;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rcx, -0x50(%rbp)
cmpl $-0x1, %esi
je 0x3b5c
movq %rdx, %r14
movl %esi, %r13d
movq %rdi, %rbx
movq 0x96a0(%rip), %rdi      # 0xcf98
leaq 0x4871(%rip), %rsi      # 0x8170
callq 0x2110
movq %rax, %rdi
callq 0x2090
movl 0x984a(%rip), %eax      # 0xd15c
movl %eax, -0x2c(%rbp)
movq %r14, -0x48(%rbp)
movq %r14, %rdi
callq 0x54d8
movl 0x30(%rbx), %r14d
decl %r14d
leaq 0xf8(%rbx), %rax
movq %rax, -0x40(%rbp)
leaq 0x298(%rbx), %r15
xorl %esi, %esi
movq 0x9655(%rip), %rdi      # 0xcf98
movq %rsi, -0x38(%rbp)
callq 0x2240
movq %rax, %rdi
callq 0x2090
cmpl $-0x1, %r13d
je 0x3b2c
movq -0x40(%rbp), %rdi
movl %r13d, %esi
callq 0x5a5e
movq %rax, %r13
xorl %r12d, %r12d
movl -0x2c(%rbp), %eax
cmpl 0x97e3(%rip), %eax      # 0xd15c
setne %r12b
movl (%r13), %eax
shrl $0x5, %eax
cmpq %rax, %r12
jae 0x39f3
movl 0x4(%r13,%r12,4), %esi
movl %esi, -0x30(%rbp)
sarl %esi
movq %r15, %rdi
callq 0x5502
cmpb $0x0, (%rax)
je 0x39a5
incq %r12
jmp 0x397d
movl -0x30(%rbp), %esi
sarl %esi
movq %rbx, %rdi
callq 0x555c
testl %eax, %eax
jle 0x39a0
movl -0x30(%rbp), %esi
sarl %esi
movq %r15, %rdi
callq 0x5502
movb $0x1, (%rax)
movl -0x30(%rbp), %esi
sarl %esi
movq %rbx, %rdi
callq 0x555c
cmpl 0x40(%rbx), %eax
jge 0x39e7
movq -0x48(%rbp), %rdi
leaq -0x30(%rbp), %rsi
callq 0x49e6
jmp 0x39a0
movq -0x38(%rbp), %rax
incl %eax
movq %rax, -0x38(%rbp)
jmp 0x39a0
movslq %r14d, %r14
movq 0x28(%rbx), %rax
movl (%rax,%r14,4), %esi
decq %r14
sarl %esi
movq %r15, %rdi
callq 0x5502
cmpb $0x0, (%rax)
je 0x39f6
movq 0x28(%rbx), %rax
movl 0x4(%rax,%r14,4), %r12d
movl %r12d, -0x2c(%rbp)
sarl %r12d
movq %rbx, %rdi
movl %r12d, %esi
callq 0x4cf0
movl %eax, %r13d
movq %r15, %rdi
movl %r12d, %esi
callq 0x5502
movb $0x0, (%rax)
movq -0x38(%rbp), %rcx
leal -0x1(%rcx), %eax
cmpl $0x1, %ecx
movl %eax, %esi
jg 0x393c
movl -0x2c(%rbp), %ecx
xorl $0x1, %ecx
movq -0x48(%rbp), %r13
movq (%r13), %rax
movl %ecx, (%rax)
movl 0x8(%r13), %ecx
xorl %r14d, %r14d
movl $0x0, %eax
cmpl $0x1, %ecx
je 0x3ae0
pushq $0x1
popq %rdx
pushq $0x2
popq %rdi
movslq %ecx, %rcx
movq (%r13), %rax
movslq %edx, %r12
cmpq %rcx, %rdi
jge 0x3ac8
movl %edx, -0x40(%rbp)
movl (%rax,%rdi,4), %esi
sarl %esi
movq %rdi, -0x38(%rbp)
movq %rbx, %rdi
callq 0x555c
movl %eax, -0x2c(%rbp)
movq (%r13), %rax
movl (%rax,%r12,4), %esi
sarl %esi
movq %rbx, %rdi
callq 0x555c
movq -0x38(%rbp), %rdi
movl %edi, %edx
cmpl %eax, -0x2c(%rbp)
jg 0x3abb
movl -0x40(%rbp), %edx
incq %rdi
movq -0x48(%rbp), %r13
movl 0x8(%r13), %ecx
jmp 0x3a75
movl (%rax,%r12,4), %esi
movl 0x4(%rax), %ecx
movl %ecx, (%rax,%r12,4)
movl %esi, 0x4(%rax)
sarl %esi
movq %rbx, %rdi
callq 0x555c
movq -0x50(%rbp), %rcx
movl %eax, (%rcx)
cmpl 0xf0(%rbx), %r14d
jge 0x3b02
movq %r15, %rdi
movl %r14d, %esi
callq 0x5502
movb $0x0, (%rax)
incl %r14d
jmp 0x3ae6
movq 0x948f(%rip), %rdi      # 0xcf98
leaq 0x46a5(%rip), %rsi      # 0x81b5
callq 0x2110
movq %rax, %rdi
callq 0x2090
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
pushq $0x10
popq %rdi
callq 0x20a0
movq %rax, %rbx
leaq 0x464d(%rip), %rsi      # 0x818b
movq %rax, %rdi
callq 0x21b0
movq 0x948b(%rip), %rsi      # 0xcfd8
movq 0x943c(%rip), %rdx      # 0xcf90
movq %rbx, %rdi
callq 0x2230
pushq $0x10
popq %rdi
callq 0x20a0
movq %rax, %rbx
leaq 0x45d9(%rip), %rsi      # 0x8147
movq %rax, %rdi
callq 0x21b0
jmp 0x3b46
jmp 0x3b7a
movq %rax, %r14
movq %rbx, %rdi
callq 0x20e0
movq %r14, %rdi
callq 0x2250
nop