// File: 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
movq %rsp, %rbp
subq $0x70, %rsp
movq %rdi, -0x10(%rbp)
movq -0x10(%rbp), %rdi
movq %rdi, -0x50(%rbp)
callq 0x1b20
testb $0x1, %al
jne 0x2399
movl $0x14, -0x4(%rbp)
jmp 0x25f1
movq $0x0, -0x18(%rbp)
movq -0x50(%rbp), %rdi
addq $0x38, %rdi
callq 0x28c0
cmpq $0x0, %rax
jbe 0x25d8
movq -0x50(%rbp), %rax
cmpq $0x0, 0x88(%rax)
je 0x23ed
movq -0x50(%rbp), %rcx
movq 0x88(%rcx), %rax
movq 0x90(%rcx), %rdi
callq *%rax
cmpl $0x0, %eax
je 0x23eb
movl $0x0, -0x4(%rbp)
jmp 0x25f1
jmp 0x23ed
movq -0x18(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x18(%rbp)
callq 0x1060
movq -0x50(%rbp), %rdi
cltq
movq %rax, -0x60(%rbp)
addq $0x38, %rdi
callq 0x28c0
movq -0x50(%rbp), %rdi
movq %rax, %rcx
movq -0x60(%rbp), %rax
xorl %edx, %edx
divq %rcx
movl %edx, %eax
movl %eax, -0x1c(%rbp)
addq $0x38, %rdi
movslq -0x1c(%rbp), %rsi
callq 0x2cb0
movq -0x50(%rbp), %rdi
movl (%rax), %eax
movl %eax, -0x20(%rbp)
addq $0x38, %rdi
callq 0x2e70
movq -0x50(%rbp), %rdi
movl (%rax), %eax
movl %eax, -0x54(%rbp)
addq $0x38, %rdi
movslq -0x1c(%rbp), %rsi
callq 0x2cb0
movl -0x54(%rbp), %ecx
movq -0x50(%rbp), %rdi
movl %ecx, (%rax)
addq $0x38, %rdi
callq 0x2eb0
movq -0x50(%rbp), %rdi
addq $0x10, %rdi
movslq -0x20(%rbp), %rsi
callq 0x29c0
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rax
movsbl 0x1(%rax), %eax
cmpl $0x0, %eax
jle 0x2494
jmp 0x23a1
movq -0x28(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x1, %eax
jne 0x24c1
movq -0x28(%rbp), %rdi
addq $0x8, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x2cb0
movq -0x50(%rbp), %rdi
movl (%rax), %esi
callq 0x1580
jmp 0x23a1
callq 0x1060
movq -0x28(%rbp), %rcx
movsbl (%rcx), %ecx
cltd
idivl %ecx
movl %edx, -0x2c(%rbp)
callq 0x1060
movq -0x28(%rbp), %rcx
movsbl (%rcx), %ecx
cltd
idivl %ecx
movl %edx, -0x30(%rbp)
movl -0x2c(%rbp), %eax
cmpl -0x30(%rbp), %eax
jne 0x2513
callq 0x1060
movq -0x28(%rbp), %rcx
movsbl (%rcx), %ecx
cltd
idivl %ecx
movl %edx, -0x2c(%rbp)
callq 0x1060
movq -0x28(%rbp), %rcx
movsbl (%rcx), %ecx
cltd
idivl %ecx
movl %edx, -0x30(%rbp)
jmp 0x24e5
movq -0x28(%rbp), %rdi
addq $0x8, %rdi
movslq -0x2c(%rbp), %rsi
callq 0x2cb0
movl (%rax), %eax
movl %eax, -0x34(%rbp)
movq -0x28(%rbp), %rdi
addq $0x8, %rdi
movslq -0x30(%rbp), %rsi
callq 0x2cb0
movq -0x50(%rbp), %rdi
movl (%rax), %eax
movl %eax, -0x38(%rbp)
movl -0x34(%rbp), %esi
callq 0x13c0
movq -0x50(%rbp), %rdi
movl %eax, -0x68(%rbp)
movl -0x34(%rbp), %esi
callq 0x14a0
movq -0x50(%rbp), %rdi
movl %eax, %ecx
movl -0x68(%rbp), %eax
subl %ecx, %eax
movl %eax, -0x3c(%rbp)
movl -0x38(%rbp), %esi
callq 0x13c0
movq -0x50(%rbp), %rdi
movl %eax, -0x64(%rbp)
movl -0x38(%rbp), %esi
callq 0x14a0
movl %eax, %ecx
movl -0x64(%rbp), %eax
subl %ecx, %eax
movl %eax, -0x40(%rbp)
movl -0x3c(%rbp), %eax
cmpl -0x40(%rbp), %eax
jne 0x25ab
movq -0x50(%rbp), %rdi
movl -0x34(%rbp), %esi
callq 0x1580
movq -0x50(%rbp), %rdi
movl -0x38(%rbp), %esi
callq 0x1580
jmp 0x25d3
movl -0x3c(%rbp), %eax
cmpl -0x40(%rbp), %eax
jle 0x25bb
movl -0x34(%rbp), %eax
movl %eax, -0x6c(%rbp)
jmp 0x25c1
movl -0x38(%rbp), %eax
movl %eax, -0x6c(%rbp)
movq -0x50(%rbp), %rdi
movl -0x6c(%rbp), %eax
movl %eax, -0x44(%rbp)
movl -0x44(%rbp), %esi
callq 0x1580
jmp 0x23a1
movq -0x18(%rbp), %rsi
leaq 0x5a32(%rip), %rdi      # 0x8015
movb $0x0, %al
callq 0x1040
movl $0xa, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x70, %rsp
popq %rbp
retq
nopw (%rax,%rax)