// File: 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
subq $0xc0, %rsp
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x68(%rbp)
cmpl $-0x1, -0xc(%rbp)
jne 0x4c0b
movl $0x10, %edi
callq 0x2090
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x70(%rbp)
leaq 0xa5f2(%rip), %rsi      # 0xf1c5
callq 0x21a0
jmp 0x4bda
movq -0x70(%rbp), %rdi
movq 0x113f3(%rip), %rsi     # 0x15fd8
movq 0x113a4(%rip), %rdx     # 0x15f90
callq 0x2210
movq -0x70(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x28(%rbp)
movl %eax, -0x2c(%rbp)
callq 0x20c0
jmp 0x506e
movq 0x11386(%rip), %rdi     # 0x15f98
leaq 0xa5d5(%rip), %rsi      # 0xf1ee
callq 0x20f0
movq %rax, %rdi
movq 0x11338(%rip), %rsi     # 0x15f60
callq 0x2120
movl $0x0, -0x30(%rbp)
movl 0x11512(%rip), %eax     # 0x1614c
movl %eax, -0x34(%rbp)
movq -0x18(%rbp), %rdi
callq 0x8fb0
movq -0x68(%rbp), %rdi
addq $0x28, %rdi
callq 0x7720
subl $0x1, %eax
movl %eax, -0x38(%rbp)
movl -0x30(%rbp), %esi
movq 0x11335(%rip), %rdi     # 0x15f98
callq 0x2220
movq %rax, %rdi
movq 0x112ee(%rip), %rsi     # 0x15f60
callq 0x2120
cmpl $-0x1, -0xc(%rbp)
jne 0x4cd0
movl $0x10, %edi
callq 0x2090
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x78(%rbp)
leaq 0xa571(%rip), %rsi      # 0xf209
callq 0x21a0
jmp 0x4c9f
movq -0x78(%rbp), %rdi
movq 0x1132e(%rip), %rsi     # 0x15fd8
movq 0x112df(%rip), %rdx     # 0x15f90
callq 0x2210
movq -0x78(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x28(%rbp)
movl %eax, -0x2c(%rbp)
callq 0x20c0
jmp 0x506e
movq -0x68(%rbp), %rdi
addq $0xf8, %rdi
movl -0xc(%rbp), %esi
callq 0x7aa0
movq %rax, -0x40(%rbp)
leaq -0x34(%rbp), %rdi
leaq 0x1145a(%rip), %rsi     # 0x1614c
callq 0x7820
movb %al, %dl
movl $0x1, %eax
xorl %ecx, %ecx
testb $0x1, %dl
cmovnel %ecx, %eax
movl %eax, -0x44(%rbp)
movl -0x44(%rbp), %eax
movl %eax, -0x7c(%rbp)
movq -0x40(%rbp), %rdi
callq 0x8b00
movl %eax, %ecx
movl -0x7c(%rbp), %eax
cmpl %ecx, %eax
jge 0x4e07
movq -0x40(%rbp), %rdi
movl -0x44(%rbp), %esi
callq 0x8ee0
movq %rax, %rcx
movq -0x68(%rbp), %rax
movl (%rcx), %ecx
movl %ecx, -0x48(%rbp)
addq $0x298, %rax            # imm = 0x298
movq %rax, -0x88(%rbp)
leaq -0x48(%rbp), %rdi
callq 0x7db0
movq -0x88(%rbp), %rdi
movl %eax, %esi
callq 0x9010
cmpb $0x0, (%rax)
jne 0x4df7
leaq -0x48(%rbp), %rdi
callq 0x7db0
movq -0x68(%rbp), %rdi
movl %eax, %esi
callq 0x90b0
cmpl $0x0, %eax
jle 0x4df7
movq -0x68(%rbp), %rax
addq $0x298, %rax            # imm = 0x298
movq %rax, -0x98(%rbp)
leaq -0x48(%rbp), %rdi
callq 0x7db0
movq -0x98(%rbp), %rdi
movl %eax, %esi
callq 0x9010
movb $0x1, (%rax)
leaq -0x48(%rbp), %rdi
callq 0x7db0
movq -0x68(%rbp), %rdi
movl %eax, %esi
callq 0x90b0
movq -0x68(%rbp), %rdi
movl %eax, -0x8c(%rbp)
callq 0x7670
movl %eax, %ecx
movl -0x8c(%rbp), %eax
cmpl %ecx, %eax
jl 0x4de8
movl -0x30(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
jmp 0x4df5
movq -0x18(%rbp), %rdi
leaq -0x48(%rbp), %rsi
callq 0x7550
jmp 0x4df7
jmp 0x4df9
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0x4d09
jmp 0x4e09
movq -0x68(%rbp), %rdi
movq %rdi, %rax
addq $0x298, %rax            # imm = 0x298
movq %rax, -0xa0(%rbp)
addq $0x28, %rdi
movl -0x38(%rbp), %esi
movl %esi, %eax
addl $-0x1, %eax
movl %eax, -0x38(%rbp)
callq 0x77a0
movq %rax, %rdi
callq 0x7db0
movq -0xa0(%rbp), %rdi
movl %eax, %esi
callq 0x9010
cmpb $0x0, (%rax)
setne %al
xorb $-0x1, %al
testb $0x1, %al
jne 0x4e55
jmp 0x4e57
jmp 0x4e09
movq -0x68(%rbp), %rdi
addq $0x28, %rdi
movl -0x38(%rbp), %esi
addl $0x1, %esi
callq 0x77a0
movl (%rax), %eax
movl %eax, -0x34(%rbp)
leaq -0x34(%rbp), %rdi
callq 0x7db0
movq -0x68(%rbp), %rdi
movl %eax, %esi
callq 0x7dd0
movl %eax, %ecx
movq -0x68(%rbp), %rax
movl %ecx, -0xc(%rbp)
addq $0x298, %rax            # imm = 0x298
movq %rax, -0xa8(%rbp)
leaq -0x34(%rbp), %rdi
callq 0x7db0
movq -0xa8(%rbp), %rdi
movl %eax, %esi
callq 0x9010
movb $0x0, (%rax)
movl -0x30(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x30(%rbp)
cmpl $0x0, -0x30(%rbp)
jg 0x4c59
leaq -0x34(%rbp), %rdi
callq 0x75b0
movl %eax, -0x4c(%rbp)
movq -0x18(%rbp), %rdi
xorl %esi, %esi
callq 0x77a0
movl -0x4c(%rbp), %ecx
movl %ecx, (%rax)
movq -0x18(%rbp), %rdi
callq 0x7720
cmpl $0x1, %eax
jne 0x4eff
movq -0x20(%rbp), %rax
movl $0x0, (%rax)
jmp 0x4ffd
movl $0x1, -0x50(%rbp)
movl $0x2, -0x54(%rbp)
movl -0x54(%rbp), %eax
movl %eax, -0xac(%rbp)
movq -0x18(%rbp), %rdi
callq 0x7720
movl %eax, %ecx
movl -0xac(%rbp), %eax
cmpl %ecx, %eax
jge 0x4f91
movq -0x18(%rbp), %rdi
movl -0x54(%rbp), %esi
callq 0x77a0
movq %rax, %rdi
callq 0x7db0
movq -0x68(%rbp), %rdi
movl %eax, %esi
callq 0x90b0
movl %eax, -0xb0(%rbp)
movq -0x18(%rbp), %rdi
movl -0x50(%rbp), %esi
callq 0x77a0
movq %rax, %rdi
callq 0x7db0
movq -0x68(%rbp), %rdi
movl %eax, %esi
callq 0x90b0
movl %eax, %ecx
movl -0xb0(%rbp), %eax
cmpl %ecx, %eax
jle 0x4f81
movl -0x54(%rbp), %eax
movl %eax, -0x50(%rbp)
jmp 0x4f83
movl -0x54(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x54(%rbp)
jmp 0x4f0d
movq -0x18(%rbp), %rdi
movl -0x50(%rbp), %esi
callq 0x77a0
movl (%rax), %eax
movl %eax, -0x58(%rbp)
movq -0x18(%rbp), %rdi
movl $0x1, %esi
callq 0x77a0
movq %rax, -0xb8(%rbp)
movq -0x18(%rbp), %rdi
movl -0x50(%rbp), %esi
callq 0x77a0
movq -0xb8(%rbp), %rcx
movl (%rcx), %ecx
movl %ecx, (%rax)
movq -0x18(%rbp), %rdi
movl $0x1, %esi
callq 0x77a0
movl -0x58(%rbp), %ecx
movl %ecx, (%rax)
leaq -0x58(%rbp), %rdi
callq 0x7db0
movq -0x68(%rbp), %rdi
movl %eax, %esi
callq 0x90b0
movl %eax, %ecx
movq -0x20(%rbp), %rax
movl %ecx, (%rax)
movl $0x0, -0x5c(%rbp)
movq -0x68(%rbp), %rdi
movl -0x5c(%rbp), %eax
movl %eax, -0xbc(%rbp)
callq 0x7530
movl %eax, %ecx
movl -0xbc(%rbp), %eax
cmpl %ecx, %eax
jge 0x5043
movq -0x68(%rbp), %rdi
addq $0x298, %rdi            # imm = 0x298
movl -0x5c(%rbp), %esi
callq 0x9010
movb $0x0, (%rax)
movl -0x5c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x5c(%rbp)
jmp 0x5004
movq 0x10f4e(%rip), %rdi     # 0x15f98
leaq 0xa1e2(%rip), %rsi      # 0xf233
callq 0x20f0
movq %rax, %rdi
movq 0x10f00(%rip), %rsi     # 0x15f60
callq 0x2120
addq $0xc0, %rsp
popq %rbp
retq
movq -0x28(%rbp), %rdi
callq 0x2230
nopw (%rax,%rax)