// File: /workspace/github_cpp_makefile_star2_O1/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 0x58ff(%rip), %rsi      # 0xc550
leaq 0x590b(%rip), %rdx      # 0xc563
movq %rcx, 0x38(%rsp)
movq %rcx, %rdi
callq 0x98da
leaq 0x78(%rsp), %rax
xorps %xmm0, %xmm0
movaps %xmm0, 0x8(%rax)
movaps %xmm0, 0x18(%rax)
movaps %xmm0, -0x8(%rax)
movq %rax, 0x10(%rax)
movq %rax, 0x18(%rax)
movq $0x0, 0x20(%rax)
movq (%r15), %rbx
movq 0x8(%r15), %r13
cmpq %r13, %rbx
je 0x6d4a
leaq 0x58(%rsp), %r15
leaq 0x70(%rsp), %r12
movq %r15, %rdi
movq 0x18(%rsp), %rsi
callq 0x77ca
movq 0x58(%rsp), %rdi
movq 0x60(%rsp), %rax
cmpq %rax, %rdi
je 0x6ce4
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 0x6cc6
testq %rdx, %rdx
setne %bpl
jmp 0x6ce6
xorl %ebp, %ebp
testq %rdi, %rdi
je 0x6cf8
movq 0x68(%rsp), %rsi
subq %rdi, %rsi
callq 0x32a0
testb %bpl, %bpl
jne 0x6d22
movq 0x18(%rsp), %rdi
movq 0x8(%rdi), %rsi
cmpq 0x10(%rdi), %rsi
je 0x6d1a
movl (%rbx), %eax
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x8(%rdi)
jmp 0x6d22
movq %rbx, %rdx
callq 0x9986
leaq 0x8(%rbx), %rsi
movq %r12, %rdi
callq 0x784a
leaq 0x28(%rbx), %rdx
movq %rax, %rdi
movq %rbx, %rsi
callq 0x78c6
addq $0x30, %rbx
cmpq %r13, %rbx
jne 0x6ca3
movq 0x88(%rsp), %rcx
leaq 0x78(%rsp), %rax
cmpq %rax, %rcx
je 0x6f37
leaq 0x8(%rsp), %r13
leaq 0x14(%rsp), %rbp
xorps %xmm0, %xmm0
movaps %xmm0, 0x20(%rsp)
movq $0x0, 0x30(%rsp)
movq 0x40(%rcx), %r15
movq %rcx, 0x48(%rsp)
movq 0x48(%rcx), %rax
movq %rax, 0x50(%rsp)
cmpq %rax, %r15
je 0x6f05
xorl %r12d, %r12d
movl (%r15), %eax
movl %eax, 0xc(%rsp)
cmpl $0x0, 0x4(%r15)
je 0x6dff
cmpl $0x1, 0x4(%r15)
jne 0x6e83
testl %r12d, %r12d
js 0x6e2b
movq 0x20(%rsp), %r12
movq 0x28(%rsp), %rbx
cmpq %rbx, %r12
je 0x6e63
movl (%r12), %eax
movl 0xc(%rsp), %ecx
movl %eax, 0x8(%rsp)
movl %ecx, 0x10(%rsp)
movl $0x0, 0x14(%rsp)
cmpl %ecx, %eax
je 0x6df9
movq %r14, %rdi
movq %r13, %rsi
leaq 0x10(%rsp), %rdx
movq %rbp, %rcx
callq 0x7a30
addq $0x4, %r12
jmp 0x6dc1
testl %r12d, %r12d
js 0x6e94
incl %r12d
movq 0x28(%rsp), %rsi
cmpq 0x30(%rsp), %rsi
je 0x6ef1
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x28(%rsp)
jmp 0x6da7
cmpl $-0x1, %r12d
jne 0x6e63
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 0x6e63
movq %r14, %rdi
movq %r13, %rsi
leaq 0x10(%rsp), %rdx
movq %rbp, %rcx
callq 0x7a30
movl 0xc(%rsp), %eax
movl %eax, 0x8(%rsp)
leaq 0x20(%rsp), %rdi
movq %r13, %rsi
leaq 0xc(%rsp), %rdx
callq 0x9f72
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
addq $0x8, %r15
cmpq 0x50(%rsp), %r15
jne 0x6d99
jmp 0x6f05
cmpl $-0x1, %r12d
jne 0x6da7
movq 0x20(%rsp), %rcx
movl (%rcx), %ecx
movl %ecx, 0x8(%rsp)
movl %eax, 0x10(%rsp)
movl $0x1, 0x14(%rsp)
cmpl %eax, %ecx
je 0x6ecc
movq %r14, %rdi
movq %r13, %rsi
leaq 0x10(%rsp), %rdx
movq %rbp, %rcx
callq 0x7a30
movl 0xc(%rsp), %eax
movl %eax, 0x8(%rsp)
leaq 0x20(%rsp), %rdi
movq %r13, %rsi
leaq 0xc(%rsp), %rdx
callq 0x9f72
movl $0x1, %r12d
jmp 0x6da7
leaq 0x20(%rsp), %rdi
leaq 0xc(%rsp), %rdx
callq 0x9986
jmp 0x6da7
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x6f1c
movq 0x30(%rsp), %rsi
subq %rdi, %rsi
callq 0x32a0
movq 0x48(%rsp), %rdi
callq 0x31d0
movq %rax, %rcx
leaq 0x78(%rsp), %rax
cmpq %rax, %rcx
jne 0x6d6a
leaq 0x70(%rsp), %rdi
callq 0x82d2
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
jmp 0x6fa4
jmp 0x6f63
jmp 0x6f63
movq %rax, %rbx
jmp 0x6f7d
jmp 0x6f63
movq %rax, %rbx
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x6f7d
movq 0x30(%rsp), %rsi
subq %rdi, %rsi
callq 0x32a0
leaq 0x70(%rsp), %rdi
callq 0x82d2
movq 0x38(%rsp), %rax
movq (%rax), %rdi
movq 0x40(%rsp), %rax
cmpq %rax, %rdi
je 0x6fa4
movq (%rax), %rsi
incq %rsi
callq 0x32a0
movq 0x18(%rsp), %rax
movq (%r14), %rdi
testq %rdi, %rdi
je 0x6fc2
movq 0x28(%rax), %rsi
subq %rdi, %rsi
callq 0x32a0
movq 0x18(%rsp), %rax
movq (%rax), %rdi
testq %rdi, %rdi
je 0x6fd6
movq 0x10(%rax), %rsi
subq %rdi, %rsi
callq 0x32a0
movq %rbx, %rdi
callq 0x3410