// File: /workspace/github_cpp_makefile_star2_O1/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
cmpl $-0x1, %esi
je 0x46c8
movq %rdx, %r15
movq %rdi, %rbx
movl %esi, %r12d
movq %rcx, -0x58(%rbp)
movq 0x8c16(%rip), %r14      # 0xcf98
leaq 0x4e03(%rip), %rsi      # 0x918c
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 0x8da9(%rip), %eax      # 0xd16c
movl %eax, -0x30(%rbp)
movl 0x8(%r15), %esi
cmpl 0xc(%r15), %esi
jne 0x43da
incl %esi
movq %r15, %rdi
callq 0x6170
movq %r15, -0x40(%rbp)
incl 0x8(%r15)
movl 0x30(%rbx), %r14d
decl %r14d
leaq 0xf8(%rbx), %rax
movq %rax, -0x60(%rbp)
leaq 0x298(%rbx), %r13
movq %rbx, -0x48(%rbp)
leaq 0x70(%rbx), %r15
xorl %esi, %esi
movl %r12d, %ebx
movq %r15, -0x38(%rbp)
movq 0x8b85(%rip), %rdi      # 0xcf98
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 0x4696
movq -0x60(%rbp), %rdi
movl %ebx, %esi
callq 0x6a4e
movq %rax, %r12
movl 0x8d09(%rip), %eax      # 0xd16c
xorl %ecx, %ecx
movl -0x30(%rbp), %esi
cmpl %eax, %esi
setne %cl
movl (%r12), %edx
shrl $0x5, %edx
cmpl %ecx, %edx
jbe 0x4508
xorl %r15d, %r15d
cmpl %eax, %esi
setne %r15b
movl 0x4(%r12,%r15,4), %esi
movl %esi, -0x2c(%rbp)
sarl %esi
movq %r13, %rdi
callq 0x6496
cmpb $0x0, (%rax)
je 0x44ad
incq %r15
movl (%r12), %eax
shrl $0x5, %eax
cmpq %rax, %r15
jb 0x4485
jmp 0x4508
movl -0x2c(%rbp), %esi
sarl %esi
movq -0x38(%rbp), %rbx
movq %rbx, %rdi
callq 0x6e98
cmpl $0x0, 0x4(%rax)
jle 0x449c
movl -0x2c(%rbp), %esi
sarl %esi
movq %r13, %rdi
callq 0x6496
movb $0x1, (%rax)
movl -0x2c(%rbp), %esi
sarl %esi
movq %rbx, %rdi
callq 0x6e98
movl 0x4(%rax), %eax
movq -0x48(%rbp), %rcx
cmpl 0x40(%rcx), %eax
jge 0x44fc
movq -0x40(%rbp), %rdi
leaq -0x2c(%rbp), %rsi
callq 0x5b5e
jmp 0x449c
movq -0x50(%rbp), %rax
incl %eax
movq %rax, -0x50(%rbp)
jmp 0x449c
movslq %r14d, %r14
movq -0x48(%rbp), %rbx
movq 0x28(%rbx), %rax
movl (%rax,%r14,4), %esi
decq %r14
sarl %esi
movq %r13, %rdi
callq 0x6496
cmpb $0x0, (%rax)
je 0x450f
movq 0x28(%rbx), %rax
movl 0x4(%rax,%r14,4), %r12d
movl %r12d, -0x30(%rbp)
sarl %r12d
movq -0x38(%rbp), %r15
movq %r15, %rdi
movl %r12d, %esi
callq 0x6e98
movl (%rax), %ebx
movq %r13, %rdi
movl %r12d, %esi
callq 0x6496
movb $0x0, (%rax)
movq -0x50(%rbp), %rcx
leal -0x1(%rcx), %eax
cmpl $0x1, %ecx
movl %eax, %esi
jg 0x440c
movl -0x30(%rbp), %ecx
xorl $0x1, %ecx
movq -0x40(%rbp), %r12
movq (%r12), %rax
movl %ecx, (%rax)
xorl %eax, %eax
cmpl $0x1, 0x8(%r12)
je 0x4615
movl $0x1, %eax
cmpl $0x3, 0x8(%r12)
jl 0x45f8
movl $0x1, %r15d
movl $0x2, %r14d
movq -0x38(%rbp), %rbx
movq (%r12), %rax
movl (%rax,%r14,4), %esi
sarl %esi
movq %rbx, %rdi
callq 0x6e98
movq %r12, %rcx
movl 0x4(%rax), %r12d
movq (%rcx), %rax
movslq %r15d, %rcx
movl (%rax,%rcx,4), %esi
sarl %esi
movq %rbx, %rdi
callq 0x6e98
movl %r14d, %ecx
cmpl 0x4(%rax), %r12d
jg 0x45dd
movl %r15d, %ecx
incq %r14
movq -0x40(%rbp), %r12
movslq 0x8(%r12), %rax
movl %ecx, %r15d
cmpq %rax, %r14
jl 0x45a5
movslq %ecx, %rax
movq -0x38(%rbp), %r15
movq (%r12), %rcx
movl (%rcx,%rax,4), %esi
movl 0x4(%rcx), %edx
movl %edx, (%rcx,%rax,4)
movl %esi, 0x4(%rcx)
sarl %esi
movq %r15, %rdi
callq 0x6e98
movl 0x4(%rax), %eax
movq -0x58(%rbp), %rcx
movl %eax, (%rcx)
movq -0x48(%rbp), %rbx
cmpl $0x0, 0xf0(%rbx)
jle 0x4645
xorl %r14d, %r14d
movq %r13, %rdi
movl %r14d, %esi
callq 0x6496
movb $0x0, (%rax)
incl %r14d
cmpl 0xf0(%rbx), %r14d
jl 0x462b
leaq 0x4b85(%rip), %rsi      # 0x91d1
movl $0x25, %edx
movq 0x8940(%rip), %rbx      # 0xcf98
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 0x4afd(%rip), %rsi      # 0x91a7
movq %rax, %rdi
callq 0x21d0
movq 0x891f(%rip), %rsi      # 0xcfd8
movq 0x88d0(%rip), %rdx      # 0xcf90
movq %rbx, %rdi
callq 0x2250
movl $0x10, %edi
callq 0x20a0
movq %rax, %rbx
leaq 0x4a87(%rip), %rsi      # 0x9163
movq %rax, %rdi
callq 0x21d0
jmp 0x46b2
jmp 0x46e8
movq %rax, %r14
movq %rbx, %rdi
callq 0x20f0
movq %r14, %rdi
callq 0x2270
nop