// File: /workspace/github_cpp_makefile_star2_O3/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 $0x38, %rsp
movq %rdi, -0x38(%rbp)
cmpl $-0x1, %esi
je 0x4683
movq %rcx, %r15
movq %rdx, %r12
movl %esi, %ebx
movq 0x7c65(%rip), %r14      # 0xbf98
leaq 0x3e52(%rip), %rsi      # 0x818c
movl $0x1a, %edx
movq %r14, %rdi
callq 0x2150
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x2120
movsbl %al, %esi
movq %r14, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x20e0
movl 0x7df8(%rip), %eax      # 0xc16c
movl %eax, -0x30(%rbp)
movl 0x8(%r12), %esi
cmpl 0xc(%r12), %esi
movq %r15, -0x58(%rbp)
jne 0x4396
incl %esi
movq %r12, %rdi
callq 0x5fe4
movl 0x8(%r12), %esi
incl %esi
movq %r12, -0x48(%rbp)
movl %esi, 0x8(%r12)
movq -0x38(%rbp), %rax
movl 0x30(%rax), %r14d
decl %r14d
leaq 0xf8(%rax), %rcx
movq %rcx, -0x60(%rbp)
leaq 0x298(%rax), %r13
leaq 0x70(%rax), %r15
xorl %esi, %esi
movq %r15, -0x40(%rbp)
movq 0x7bc9(%rip), %rdi      # 0xbf98
movq %rsi, -0x50(%rbp)
callq 0x2260
movq %rax, %r12
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r12, %rdi
movl $0xa, %esi
callq 0x2120
movsbl %al, %esi
movq %r12, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x20e0
cmpl $-0x1, %ebx
je 0x4651
movq -0x60(%rbp), %rdi
movl %ebx, %esi
callq 0x687e
movq %rax, %r12
movl 0x7d4d(%rip), %eax      # 0xc16c
xorl %ecx, %ecx
movl -0x30(%rbp), %esi
cmpl %eax, %esi
setne %cl
movl (%r12), %edx
shrl $0x5, %edx
cmpl %ecx, %edx
jbe 0x44c4
xorl %r15d, %r15d
cmpl %eax, %esi
setne %r15b
movl 0x4(%r12,%r15,4), %esi
movl %esi, -0x2c(%rbp)
sarl %esi
movq %r13, %rdi
callq 0x6310
cmpb $0x0, (%rax)
je 0x4469
incq %r15
movl (%r12), %eax
shrl $0x5, %eax
cmpq %rax, %r15
jb 0x4441
jmp 0x44c4
movl -0x2c(%rbp), %esi
sarl %esi
movq -0x40(%rbp), %rbx
movq %rbx, %rdi
callq 0x6cda
cmpl $0x0, 0x4(%rax)
jle 0x4458
movl -0x2c(%rbp), %esi
sarl %esi
movq %r13, %rdi
callq 0x6310
movb $0x1, (%rax)
movl -0x2c(%rbp), %esi
sarl %esi
movq %rbx, %rdi
callq 0x6cda
movl 0x4(%rax), %eax
movq -0x38(%rbp), %rcx
cmpl 0x40(%rcx), %eax
jge 0x44b8
movq -0x48(%rbp), %rdi
leaq -0x2c(%rbp), %rsi
callq 0x59c8
jmp 0x4458
movq -0x50(%rbp), %rax
incl %eax
movq %rax, -0x50(%rbp)
jmp 0x4458
movslq %r14d, %r14
movq -0x38(%rbp), %rbx
movq 0x28(%rbx), %rax
movl (%rax,%r14,4), %esi
decq %r14
sarl %esi
movq %r13, %rdi
callq 0x6310
cmpb $0x0, (%rax)
je 0x44cb
movq 0x28(%rbx), %rax
movl 0x4(%rax,%r14,4), %r12d
movl %r12d, -0x30(%rbp)
sarl %r12d
movq -0x40(%rbp), %r15
movq %r15, %rdi
movl %r12d, %esi
callq 0x6cda
movl (%rax), %ebx
movq %r13, %rdi
movl %r12d, %esi
callq 0x6310
movb $0x0, (%rax)
movq -0x50(%rbp), %rcx
leal -0x1(%rcx), %eax
cmpl $0x1, %ecx
movl %eax, %esi
jg 0x43c8
movl -0x30(%rbp), %ecx
xorl $0x1, %ecx
movq -0x48(%rbp), %r12
movq (%r12), %rax
movl %ecx, (%rax)
movl 0x8(%r12), %edx
xorl %ecx, %ecx
cmpl $0x1, %edx
je 0x45d0
movl $0x1, %ecx
cmpl $0x3, %edx
jl 0x45b7
movl $0x1, %r15d
movl $0x2, %r14d
movq -0x40(%rbp), %rbx
movq (%r12), %rax
movl (%rax,%r14,4), %esi
sarl %esi
movq %rbx, %rdi
callq 0x6cda
movq %r12, %rcx
movl 0x4(%rax), %r12d
movq (%rcx), %rax
movslq %r15d, %rcx
movl (%rax,%rcx,4), %esi
sarl %esi
movq %rbx, %rdi
callq 0x6cda
movl %r14d, %ecx
cmpl 0x4(%rax), %r12d
jg 0x4598
movl %r15d, %ecx
incq %r14
movq -0x48(%rbp), %r12
movslq 0x8(%r12), %rax
movl %ecx, %r15d
cmpq %rax, %r14
jl 0x4560
movq (%r12), %rax
movslq %ecx, %rcx
movq -0x40(%rbp), %r15
movl (%rax,%rcx,4), %esi
movl 0x4(%rax), %edx
movl %edx, (%rax,%rcx,4)
movl %esi, 0x4(%rax)
sarl %esi
movq %r15, %rdi
callq 0x6cda
movl 0x4(%rax), %ecx
movq -0x58(%rbp), %rax
movl %ecx, (%rax)
movq -0x38(%rbp), %rbx
cmpl $0x0, 0xf0(%rbx)
jle 0x4600
xorl %r14d, %r14d
movq %r13, %rdi
movl %r14d, %esi
callq 0x6310
movb $0x0, (%rax)
incl %r14d
cmpl 0xf0(%rbx), %r14d
jl 0x45e6
leaq 0x3bca(%rip), %rsi      # 0x81d1
movl $0x25, %edx
movq 0x7985(%rip), %rbx      # 0xbf98
movq %rbx, %rdi
callq 0x2150
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x2120
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x20e0
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x10, %edi
callq 0x20a0
movq %rax, %rbx
leaq 0x3b42(%rip), %rsi      # 0x81a7
movq %rax, %rdi
callq 0x21d0
movq 0x7964(%rip), %rsi      # 0xbfd8
movq 0x7915(%rip), %rdx      # 0xbf90
movq %rbx, %rdi
callq 0x2250
movl $0x10, %edi
callq 0x20a0
movq %rax, %rbx
leaq 0x3acc(%rip), %rsi      # 0x8163
movq %rax, %rdi
callq 0x21d0
jmp 0x466d
jmp 0x46a3
movq %rax, %r14
movq %rbx, %rdi
callq 0x20f0
movq %r14, %rdi
callq 0x2270