// File: /workspace/github_cpp_makefile_star2_O2/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 0x16ec
testb %al, %al
je 0x1bd2
xorl %r14d, %r14d
movq 0x40(%rbx), %rax
cmpq 0x38(%rbx), %rax
je 0x1bd6
movq 0x88(%rbx), %rax
testq %rax, %rax
je 0x1af1
movq 0x90(%rbx), %rdi
callq *%rax
testl %eax, %eax
jne 0x1bf9
incq %r14
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 0x1ac6
addq %rax, %r15
cmpb $0x1, (%r15)
jne 0x1b48
movq 0x8(%r15), %rax
movl (%rax), %ebp
jmp 0x1bc3
callq 0x1060
movsbl (%r15), %ecx
cltd
idivl %ecx
movl %edx, %ebp
callq 0x1060
movsbl (%r15), %ecx
cltd
idivl %ecx
cmpl %edx, %ebp
je 0x1b48
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 0x1354
movl %eax, %r12d
movq %rbx, %rdi
movl %r15d, %esi
callq 0x1396
subl %eax, %r12d
movq %rbx, %rdi
movl %ebp, %esi
callq 0x1354
movl %eax, %r13d
movq %rbx, %rdi
movl %ebp, %esi
callq 0x1396
subl %eax, %r13d
cmpl %r13d, %r12d
jne 0x1bbf
movq %rbx, %rdi
movl %r15d, %esi
callq 0x13de
jmp 0x1bc3
cmovgl %r15d, %ebp
movq %rbx, %rdi
movl %ebp, %esi
callq 0x13de
jmp 0x1ac6
pushq $0x14
jmp 0x1be9
leaq 0x1430(%rip), %rdi      # 0x300d
movq %r14, %rsi
xorl %eax, %eax
callq 0x1040
pushq $0xa
popq %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %eax, %eax
jmp 0x1bea
nop