// File: /workspace/github_cpp_makefile_star2_O2/momohatt[P]seccampDB/database.cpp
// Function: ConflictGraph::ConflictGraph(std::vector<Scheduler::Log, std::allocator<Scheduler::Log>>)
ConflictGraph::ConflictGraph(vector<Scheduler::Log> logs) {
    map<Key, vector<pair<int, BaseOp>>> tbl = {};

    for (const auto& log : logs) {
        if (!vexists(nodes_, log.id))
            nodes_.push_back(log.id);
        tbl[log.key].emplace_back(log.id, log.op);
    }

    for (const auto& [key, logs] : tbl) {
        int nlock = 0;
        vector<int> lock_holder_ids = {};

        for (const auto& l : logs) {
            int myid = l.first;
            int action = l.second;

            if (action == Read) {
                if (nlock >= 0) {
                    // read -> read (no conflict)
                    nlock++;
                    lock_holder_ids.push_back(myid);
                } else if (nlock == -1) {
                    // write -> read conflict
                    add_edge(lock_holder_ids[0], myid, WriteRead);
                    lock_holder_ids = {myid};
                    nlock = 1;
                }
            }

            if (l.second == Write) {
                if (nlock >= 0) {
                    // read -> write conflict
                    for (const auto& other_id : lock_holder_ids) {
                        add_edge(other_id, myid, ReadWrite);
                    }
                } else if (nlock == -1) {
                    // write -> write conflict
                    add_edge(lock_holder_ids[0], myid, WriteWrite);
                }
                lock_holder_ids = {myid};
                nlock = -1;
            }
            // cout << key << " " << l.first << " " << l.second << endl;
        }
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movq %rsi, %r15
movq %rdi, %rbx
addq $0x30, %rdi
xorps %xmm0, %xmm0
movups %xmm0, 0x20(%rbx)
movups %xmm0, 0x10(%rbx)
movups %xmm0, (%rbx)
leaq 0x5b25(%rip), %rsi      # 0xb570
leaq 0x30(%rsp), %rdx
movq %rdi, 0x8(%rsp)
callq 0x3c1a
leaq 0x38(%rsp), %rax
xorps %xmm0, %xmm0
movaps %xmm0, -0x8(%rax)
andq $0x0, 0x8(%rax)
movq %rax, 0x10(%rax)
movq %rax, 0x18(%rax)
andq $0x0, 0x20(%rax)
movq (%r15), %r14
movq 0x8(%r15), %r13
leaq 0x60(%rsp), %r15
leaq 0x30(%rsp), %r12
cmpq %r13, %r14
je 0x5adf
movq %r15, %rdi
movq %rbx, %rsi
callq 0x65de
movl (%r14), %esi
movq %r15, %rdi
callq 0x65bf
movl %eax, %ebp
movq %r15, %rdi
callq 0x7eae
testb %bpl, %bpl
jne 0x5abe
movq %rbx, %rdi
movq %r14, %rsi
callq 0x662c
leaq 0x8(%r14), %rsi
movq %r12, %rdi
callq 0x664a
leaq 0x28(%r14), %rdx
movq %rax, %rdi
movq %r14, %rsi
callq 0x66b0
addq $0x30, %r14
jmp 0x5a89
movq 0x48(%rsp), %rcx
leaq 0x10(%rsp), %rbp
leaq 0x38(%rsp), %rax
cmpq %rax, %rcx
je 0x5c06
andq $0x0, 0x20(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rsp)
movq 0x40(%rcx), %r13
movq %rcx, 0x28(%rsp)
movq 0x48(%rcx), %r12
xorl %r14d, %r14d
cmpq %r12, %r13
je 0x5bec
movl (%r13), %edx
movl %edx, 0x4(%rsp)
movl 0x4(%r13), %eax
testl %eax, %eax
jne 0x5b7c
testl %r14d, %r14d
js 0x5b45
movq %rbp, %rdi
leaq 0x4(%rsp), %rsi
callq 0x662c
incl %r14d
jmp 0x5b78
cmpl $-0x1, %r14d
jne 0x5b78
movq 0x10(%rsp), %rax
movl (%rax), %esi
movq %rbx, %rdi
pushq $0x1
popq %rcx
callq 0x5c80
movl 0x4(%rsp), %eax
movl %eax, (%rsp)
movq %rbp, %rdi
movq %rsp, %rsi
leaq 0x4(%rsp), %rdx
callq 0x8d2e
pushq $0x1
popq %r14
movl 0x4(%r13), %eax
cmpl $0x1, %eax
jne 0x5be3
testl %r14d, %r14d
js 0x5bac
movq 0x10(%rsp), %r14
movq 0x18(%rsp), %r15
cmpq %r15, %r14
je 0x5bc8
movl (%r14), %esi
movl 0x4(%rsp), %edx
movq %rbx, %rdi
xorl %ecx, %ecx
callq 0x5c80
addq $0x4, %r14
jmp 0x5b90
cmpl $-0x1, %r14d
jne 0x5bc8
movq 0x10(%rsp), %rax
movl (%rax), %esi
movl 0x4(%rsp), %edx
movq %rbx, %rdi
pushq $0x2
popq %rcx
callq 0x5c80
movl 0x4(%rsp), %eax
movl %eax, (%rsp)
movq %rbp, %rdi
movq %rsp, %rsi
leaq 0x4(%rsp), %rdx
callq 0x8d2e
pushq $-0x1
popq %r14
addq $0x8, %r13
jmp 0x5b15
movq %rbp, %rdi
callq 0x7eae
movq 0x28(%rsp), %rdi
callq 0x31d0
movq %rax, %rcx
jmp 0x5ae9
leaq 0x30(%rsp), %rdi
callq 0x70f6
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %r14
jmp 0x5c67
movq %rax, %r14
leaq 0x60(%rsp), %rdi
callq 0x7eae
jmp 0x5c38
jmp 0x5c43
movq %rax, %r14
movq 0x8(%rsp), %r15
jmp 0x5c55
jmp 0x5c43
jmp 0x5c43
movq %rax, %r14
movq 0x8(%rsp), %r15
leaq 0x10(%rsp), %rdi
callq 0x7eae
leaq 0x30(%rsp), %rdi
callq 0x70f6
movq %r15, %rdi
callq 0x3498
leaq 0x18(%rbx), %rdi
callq 0x7ee2
movq %rbx, %rdi
callq 0x7eae
movq %r14, %rdi
callq 0x3410