// File: 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
movq %rsp, %rbp
subq $0x1a0, %rsp            # imm = 0x1A0
movq %rsi, -0x188(%rbp)
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x8(%rbp), %rdi
movq %rdi, -0x170(%rbp)
movq %rdi, %rax
movq %rax, -0x180(%rbp)
callq 0xa8b0
movq -0x170(%rbp), %rdi
addq $0x18, %rdi
movq %rdi, -0x178(%rbp)
callq 0xa8d0
movq -0x170(%rbp), %rax
addq $0x30, %rax
movq %rax, -0x168(%rbp)
leaq -0x11(%rbp), %rdi
movq %rdi, -0x160(%rbp)
callq 0x3550
movq -0x168(%rbp), %rdi
movq -0x160(%rbp), %rdx
leaq 0x1408e(%rip), %rsi     # 0x1c560
callq 0x4440
jmp 0x84d9
leaq -0x11(%rbp), %rdi
callq 0x3380
leaq -0x58(%rbp), %rdi
xorl %esi, %esi
movl $0x30, %edx
callq 0x31c0
leaq -0x58(%rbp), %rdi
callq 0xa8f0
movq -0x188(%rbp), %rax
movq %rax, -0x60(%rbp)
movq -0x60(%rbp), %rdi
callq 0xa910
movq %rax, -0x68(%rbp)
movq -0x60(%rbp), %rdi
callq 0xa940
movq %rax, -0x70(%rbp)
leaq -0x68(%rbp), %rdi
leaq -0x70(%rbp), %rsi
callq 0xa970
testb $0x1, %al
jne 0x8536
jmp 0x8637
leaq -0x68(%rbp), %rdi
callq 0xa9b0
movq -0x180(%rbp), %rsi
movq %rax, -0x78(%rbp)
leaq -0x90(%rbp), %rdi
callq 0xaa20
jmp 0x8558
movq -0x78(%rbp), %rax
movl (%rax), %esi
leaq -0x90(%rbp), %rdi
callq 0xa9d0
movb %al, -0x189(%rbp)
jmp 0x8572
movb -0x189(%rbp), %al
xorb $-0x1, %al
movb %al, -0x18a(%rbp)
leaq -0x90(%rbp), %rdi
callq 0xab10
movb -0x18a(%rbp), %al
testb $0x1, %al
jne 0x8598
jmp 0x85f4
movq -0x180(%rbp), %rdi
movq -0x78(%rbp), %rsi
callq 0xab70
jmp 0x85aa
jmp 0x85f4
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
leaq -0x11(%rbp), %rdi
callq 0x3380
jmp 0x89cf
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
jmp 0x89ba
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
leaq -0x90(%rbp), %rdi
callq 0xab10
jmp 0x89ba
movq -0x78(%rbp), %rsi
addq $0x8, %rsi
leaq -0x58(%rbp), %rdi
callq 0xabe0
movq %rax, -0x198(%rbp)
jmp 0x860e
movq -0x198(%rbp), %rdi
movq -0x78(%rbp), %rsi
movq %rsi, %rdx
addq $0x28, %rdx
callq 0xacc0
jmp 0x8627
jmp 0x8629
leaq -0x68(%rbp), %rdi
callq 0xad40
jmp 0x8520
leaq -0x58(%rbp), %rax
movq %rax, -0x98(%rbp)
movq -0x98(%rbp), %rdi
callq 0xad60
movq %rax, -0xa0(%rbp)
movq -0x98(%rbp), %rdi
callq 0xad90
movq %rax, -0xa8(%rbp)
leaq -0xa0(%rbp), %rdi
leaq -0xa8(%rbp), %rsi
callq 0xadc0
testb $0x1, %al
jne 0x8684
jmp 0x89a8
leaq -0xa0(%rbp), %rdi
callq 0xadf0
movq %rax, -0xb0(%rbp)
movq -0xb0(%rbp), %rdi
callq 0xae10
movq %rax, -0xb8(%rbp)
movq -0xb0(%rbp), %rdi
callq 0xae30
movq %rax, -0xc0(%rbp)
movl $0x0, -0xc4(%rbp)
leaq -0xe0(%rbp), %rdi
xorl %esi, %esi
movl $0x18, %edx
callq 0x31c0
leaq -0xe0(%rbp), %rdi
callq 0xa8b0
movq -0xc0(%rbp), %rax
movq %rax, -0xe8(%rbp)
movq -0xe8(%rbp), %rdi
callq 0xae50
movq %rax, -0xf0(%rbp)
movq -0xe8(%rbp), %rdi
callq 0xae80
movq %rax, -0xf8(%rbp)
leaq -0xf0(%rbp), %rdi
leaq -0xf8(%rbp), %rsi
callq 0xaeb0
testb $0x1, %al
jne 0x8736
jmp 0x898b
leaq -0xf0(%rbp), %rdi
callq 0xaef0
movq %rax, -0x100(%rbp)
movq -0x100(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x104(%rbp)
movq -0x100(%rbp), %rax
movl 0x4(%rax), %eax
movl %eax, -0x108(%rbp)
cmpl $0x0, -0x108(%rbp)
jne 0x8846
cmpl $0x0, -0xc4(%rbp)
jl 0x87c3
movl -0xc4(%rbp), %eax
incl %eax
movl %eax, -0xc4(%rbp)
leaq -0xe0(%rbp), %rdi
leaq -0x104(%rbp), %rsi
callq 0xab70
jmp 0x87a1
jmp 0x8844
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
leaq -0xe0(%rbp), %rdi
callq 0xab10
jmp 0x89ba
cmpl $-0x1, -0xc4(%rbp)
jne 0x8842
xorl %eax, %eax
movl %eax, %esi
leaq -0xe0(%rbp), %rdi
callq 0xaf10
movq -0x180(%rbp), %rdi
movl (%rax), %esi
movl -0x104(%rbp), %edx
movl $0x1, %ecx
callq 0x89f0
jmp 0x87f7
movl -0x104(%rbp), %eax
movl %eax, -0x11c(%rbp)
leaq -0x11c(%rbp), %rax
movq %rax, -0x118(%rbp)
movq $0x1, -0x110(%rbp)
movq -0x118(%rbp), %rsi
movq -0x110(%rbp), %rdx
leaq -0xe0(%rbp), %rdi
callq 0xaf30
jmp 0x8838
movl $0x1, -0xc4(%rbp)
jmp 0x8844
jmp 0x8846
movq -0x100(%rbp), %rax
cmpl $0x1, 0x4(%rax)
jne 0x8978
cmpl $0x0, -0xc4(%rbp)
jl 0x88f5
leaq -0xe0(%rbp), %rax
movq %rax, -0x128(%rbp)
movq -0x128(%rbp), %rdi
callq 0xaf80
movq %rax, -0x130(%rbp)
movq -0x128(%rbp), %rdi
callq 0xafb0
movq %rax, -0x138(%rbp)
leaq -0x130(%rbp), %rdi
leaq -0x138(%rbp), %rsi
callq 0xafe0
testb $0x1, %al
jne 0x88b1
jmp 0x88f3
leaq -0x130(%rbp), %rdi
callq 0xb020
movq -0x180(%rbp), %rdi
movq %rax, -0x140(%rbp)
movq -0x140(%rbp), %rax
movl (%rax), %esi
movl -0x104(%rbp), %edx
xorl %ecx, %ecx
callq 0x89f0
jmp 0x88e3
jmp 0x88e5
leaq -0x130(%rbp), %rdi
callq 0xb040
jmp 0x8898
jmp 0x892d
cmpl $-0x1, -0xc4(%rbp)
jne 0x892b
xorl %eax, %eax
movl %eax, %esi
leaq -0xe0(%rbp), %rdi
callq 0xaf10
movq -0x180(%rbp), %rdi
movl (%rax), %esi
movl -0x104(%rbp), %edx
movl $0x2, %ecx
callq 0x89f0
jmp 0x8929
jmp 0x892b
jmp 0x892d
movl -0x104(%rbp), %eax
movl %eax, -0x154(%rbp)
leaq -0x154(%rbp), %rax
movq %rax, -0x150(%rbp)
movq $0x1, -0x148(%rbp)
movq -0x150(%rbp), %rsi
movq -0x148(%rbp), %rdx
leaq -0xe0(%rbp), %rdi
callq 0xaf30
jmp 0x896e
movl $0xffffffff, -0xc4(%rbp) # imm = 0xFFFFFFFF
jmp 0x897a
leaq -0xf0(%rbp), %rdi
callq 0xb060
jmp 0x871a
leaq -0xe0(%rbp), %rdi
callq 0xab10
leaq -0xa0(%rbp), %rdi
callq 0xb080
jmp 0x8668
leaq -0x58(%rbp), %rdi
callq 0xb0b0
addq $0x1a0, %rsp            # imm = 0x1A0
popq %rbp
retq
leaq -0x58(%rbp), %rdi
callq 0xb0b0
movq -0x168(%rbp), %rdi
callq 0x35f8
movq -0x178(%rbp), %rdi
callq 0xb0d0
movq -0x170(%rbp), %rdi
callq 0xab10
movq -0x20(%rbp), %rdi
callq 0x3540