// File: /workspace/github_cpp_makefile_star2_O3/biotomas[P]ipasir/sat/lawa/lawa.cpp
// Function: Lawa::search()
int Lawa::search() {
    if (!initializeSearch()) {
        return 20;
    }
    unsigned long round = 0;
    while (unsatClauseIds.size() > 0) {
        if (terminateCallback != nullptr) {
            if (terminateCallback(terminateCallbackData)) {
                return 0;
            }
        }
        round++;
        //printf("c round %lu, unsat clauses: %lu\n", round, unsatClauseIds.size());
        int usidid = rand() % unsatClauseIds.size();
        int usid = unsatClauseIds[usidid];
        unsatClauseIds[usidid] = unsatClauseIds.back();
        unsatClauseIds.pop_back();
        Clause& cls = clauses[usid];
        if (cls.satLits > 0) {
            continue;
        }
        //printf("c will satisfy clause ");
        //printClause(cls);

        if (cls.flippableLits == 1) {
            flipLiteral(cls.lits[0]);
            continue;
        }

        int id1 = rand() % cls.flippableLits;
        int id2 = rand() % cls.flippableLits;
        while(id1 == id2) {
            id1 = rand() % cls.flippableLits;
            id2 = rand() % cls.flippableLits;
        }
        int lit1 = cls.lits[id1];
        int lit2 = cls.lits[id2];

        int score1 = computeMakeScore(lit1) - computeBreakScore(lit1);// - flippedCount[abs(lit1)];
        int score2 = computeMakeScore(lit2) - computeBreakScore(lit2);// - flippedCount[abs(lit2)];

        if (score1 == score2) {
            //printf("c flipping %d and %d, their score: %d\n", lit1, lit2, score1);
            flipLiteral(lit1);
            flipLiteral(lit2);
        } else {
            int toFlip = score1 > score2 ? lit1 : lit2;
            //printf("c flipping %d, its score is max (%d, %d)\n", toFlip, score1, score2);
            flipLiteral(toFlip);
        }
    }
    printf("c searched finished after %lu rounds.\n", round);
    return 10;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rdi, %rbx
callq 0x1824
movl %eax, %ecx
movl $0x14, %eax
testb %cl, %cl
je 0x1e93
movq 0x40(%rbx), %rax
cmpq 0x38(%rbx), %rax
je 0x1e76
xorl %r14d, %r14d
movq 0x88(%rbx), %rax
testq %rax, %rax
je 0x1d86
movq 0x90(%rbx), %rdi
callq *%rax
testl %eax, %eax
jne 0x1e91
callq 0x1060
cltq
movq 0x38(%rbx), %rcx
movq 0x40(%rbx), %rsi
movq %rsi, %rdi
subq %rcx, %rdi
sarq $0x2, %rdi
xorl %edx, %edx
divq %rdi
movslq %edx, %rdx
movslq (%rcx,%rdx,4), %rax
movl -0x4(%rsi), %edi
addq $-0x4, %rsi
movl %edi, (%rcx,%rdx,4)
movq %rsi, 0x40(%rbx)
movq 0x10(%rbx), %r15
shlq $0x5, %rax
cmpb $0x0, 0x1(%r15,%rax)
jg 0x1e63
addq %rax, %r15
cmpb $0x1, (%r15)
jne 0x1dde
movq 0x8(%r15), %rax
movl (%rax), %ebp
jmp 0x1e59
callq 0x1060
movsbl (%r15), %ecx
cltd
idivl %ecx
movl %edx, %ebp
callq 0x1060
movsbl (%r15), %ecx
cltd
idivl %ecx
cmpl %edx, %ebp
je 0x1dde
movslq %ebp, %rax
movq 0x8(%r15), %rcx
movl (%rcx,%rax,4), %r15d
movslq %edx, %rax
movl (%rcx,%rax,4), %ebp
movq %rbx, %rdi
movl %r15d, %esi
callq 0x13aa
movl %eax, %r12d
movq %rbx, %rdi
movl %r15d, %esi
callq 0x13f2
subl %eax, %r12d
movq %rbx, %rdi
movl %ebp, %esi
callq 0x13aa
movl %eax, %r13d
movq %rbx, %rdi
movl %ebp, %esi
callq 0x13f2
subl %eax, %r13d
cmpl %r13d, %r12d
jne 0x1e55
movq %rbx, %rdi
movl %r15d, %esi
callq 0x1440
jmp 0x1e59
cmovgl %r15d, %ebp
movq %rbx, %rdi
movl %ebp, %esi
callq 0x1440
incq %r14
movq 0x40(%rbx), %rax
cmpq 0x38(%rbx), %rax
jne 0x1d69
jmp 0x1e79
xorl %r14d, %r14d
leaq 0x118d(%rip), %rdi      # 0x300d
movq %r14, %rsi
xorl %eax, %eax
callq 0x1040
movl $0xa, %eax
jmp 0x1e93
xorl %eax, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq