// File: /workspace/github_cpp_makefile_star2_O3/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 $0xa8, %rsp
movq %rsi, %r15
leaq 0x18(%rdi), %r14
leaq 0x30(%rdi), %rcx
leaq 0x40(%rdi), %rax
xorps %xmm0, %xmm0
movups %xmm0, 0x20(%rdi)
movups %xmm0, 0x10(%rdi)
movups %xmm0, (%rdi)
movq %rdi, 0x18(%rsp)
movq %rax, 0x40(%rsp)
movq %rax, 0x30(%rdi)
leaq 0x5933(%rip), %rsi      # 0xc550
leaq 0x593f(%rip), %rdx      # 0xc563
movq %rcx, 0x38(%rsp)
movq %rcx, %rdi
callq 0x9834
leaq 0x78(%rsp), %rcx
xorps %xmm0, %xmm0
movaps %xmm0, -0x8(%rcx)
xorl %eax, %eax
movq %rax, 0x8(%rcx)
movq %rcx, 0x10(%rcx)
movq %rcx, 0x18(%rcx)
movq %rax, 0x20(%rcx)
movq (%r15), %rbx
movq 0x8(%r15), %r13
cmpq %r13, %rbx
je 0x6eea
leaq 0x58(%rsp), %r15
leaq 0x70(%rsp), %r12
movq %r15, %rdi
movq 0x18(%rsp), %rsi
callq 0x775e
movq 0x58(%rsp), %rdi
movq 0x60(%rsp), %rax
cmpq %rax, %rdi
je 0x6caa
movl (%rbx), %ecx
xorl %edx, %edx
movq %rdi, %rsi
xorl %r8d, %r8d
cmpl %ecx, (%rsi)
sete %r8b
addq %r8, %rdx
addq $0x4, %rsi
cmpq %rax, %rsi
jne 0x6c8c
testq %rdx, %rdx
setne %bpl
jmp 0x6cac
xorl %ebp, %ebp
testq %rdi, %rdi
je 0x6cbe
movq 0x68(%rsp), %rsi
subq %rdi, %rsi
callq 0x32a0
testb %bpl, %bpl
jne 0x6ce8
movq 0x18(%rsp), %rdi
movq 0x8(%rdi), %rsi
cmpq 0x10(%rdi), %rsi
je 0x6ce0
movl (%rbx), %eax
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x8(%rdi)
jmp 0x6ce8
movq %rbx, %rdx
callq 0x98e6
leaq 0x8(%rbx), %rsi
movq %r12, %rdi
callq 0x77de
leaq 0x28(%rbx), %rdx
movq %rax, %rdi
movq %rbx, %rsi
callq 0x785a
addq $0x30, %rbx
cmpq %r13, %rbx
jne 0x6c69
movq 0x88(%rsp), %rbx
leaq 0x78(%rsp), %rax
cmpq %rax, %rbx
je 0x6eea
leaq 0x8(%rsp), %r13
leaq 0x14(%rsp), %rbp
xorps %xmm0, %xmm0
movaps %xmm0, 0x20(%rsp)
movq $0x0, 0x30(%rsp)
movq 0x40(%rbx), %r15
movq 0x48(%rbx), %rax
movq %rax, 0x50(%rsp)
cmpq %rax, %r15
je 0x6ed1
movq %rbx, 0x48(%rsp)
xorl %r12d, %r12d
movl (%r15), %eax
movl %eax, 0xc(%rsp)
movl 0x4(%r15), %ecx
testl %ecx, %ecx
jne 0x6dfc
testl %r12d, %r12d
js 0x6d93
incl %r12d
movq 0x28(%rsp), %rsi
cmpq 0x30(%rsp), %rsi
je 0x6de9
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x28(%rsp)
jmp 0x6df8
cmpl $-0x1, %r12d
jne 0x6df8
movq 0x20(%rsp), %rcx
movl (%rcx), %ecx
movl %ecx, 0x8(%rsp)
movl %eax, 0x10(%rsp)
movl $0x1, 0x14(%rsp)
cmpl %eax, %ecx
je 0x6dcb
movq %r14, %rdi
movq %r13, %rsi
leaq 0x10(%rsp), %rdx
movq %rbp, %rcx
callq 0x79c0
movl 0xc(%rsp), %eax
movl %eax, 0x8(%rsp)
leaq 0x20(%rsp), %rdi
movq %r13, %rsi
leaq 0xc(%rsp), %rdx
callq 0x9ed2
movl $0x1, %r12d
jmp 0x6df8
leaq 0x20(%rsp), %rdi
leaq 0xc(%rsp), %rdx
callq 0x98e6
movl 0x4(%r15), %ecx
cmpl $0x1, %ecx
jne 0x6ea6
testl %r12d, %r12d
js 0x6e4e
movq 0x20(%rsp), %r12
movq 0x28(%rsp), %rbx
cmpq %rbx, %r12
je 0x6e86
movl (%r12), %eax
movl 0xc(%rsp), %ecx
movl %eax, 0x8(%rsp)
movl %ecx, 0x10(%rsp)
movl $0x0, 0x14(%rsp)
cmpl %ecx, %eax
je 0x6e48
movq %r14, %rdi
movq %r13, %rsi
leaq 0x10(%rsp), %rdx
movq %rbp, %rcx
callq 0x79c0
addq $0x4, %r12
jmp 0x6e14
cmpl $-0x1, %r12d
jne 0x6e86
movq 0x20(%rsp), %rax
movl (%rax), %eax
movl 0xc(%rsp), %ecx
movl %eax, 0x8(%rsp)
movl %ecx, 0x10(%rsp)
movl $0x2, 0x14(%rsp)
cmpl %ecx, %eax
je 0x6e86
movq %r14, %rdi
movq %r13, %rsi
leaq 0x10(%rsp), %rdx
movq %rbp, %rcx
callq 0x79c0
movl 0xc(%rsp), %eax
movl %eax, 0x8(%rsp)
leaq 0x20(%rsp), %rdi
movq %r13, %rsi
leaq 0xc(%rsp), %rdx
callq 0x9ed2
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
addq $0x8, %r15
cmpq 0x50(%rsp), %r15
jne 0x6d5f
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
movq 0x48(%rsp), %rbx
je 0x6ed1
movq 0x30(%rsp), %rsi
subq %rdi, %rsi
callq 0x32a0
movq %rbx, %rdi
callq 0x31d0
movq %rax, %rbx
leaq 0x78(%rsp), %rax
cmpq %rax, %rbx
jne 0x6d30
leaq 0x70(%rsp), %rdi
callq 0x8262
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
jmp 0x6f57
jmp 0x6f16
jmp 0x6f16
movq %rax, %rbx
jmp 0x6f30
jmp 0x6f16
movq %rax, %rbx
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x6f30
movq 0x30(%rsp), %rsi
subq %rdi, %rsi
callq 0x32a0
leaq 0x70(%rsp), %rdi
callq 0x8262
movq 0x38(%rsp), %rax
movq (%rax), %rdi
movq 0x40(%rsp), %rax
cmpq %rax, %rdi
je 0x6f57
movq (%rax), %rsi
incq %rsi
callq 0x32a0
movq 0x18(%rsp), %rax
movq (%r14), %rdi
testq %rdi, %rdi
je 0x6f75
movq 0x28(%rax), %rsi
subq %rdi, %rsi
callq 0x32a0
movq 0x18(%rsp), %rax
movq (%rax), %rdi
testq %rdi, %rdi
je 0x6f89
movq 0x10(%rax), %rsi
subq %rdi, %rsi
callq 0x32a0
movq %rbx, %rdi
callq 0x3410
nop