// File: grame-cncm[P]digraph/./dglib/digraphop.hh
// Function: std::vector<std::vector<digraph<char, int>, std::allocator<digraph<char, int>>>, std::allocator<std::vector<digraph<char, int>, std::allocator<digraph<char, int>>>>> parallelize<digraph<char, int>, int>(digraph<digraph<char, int>, int> const&)
inline std::vector<std::vector<N>> parallelize(const digraph<N, A>& g)
{
    //-----------------------------------------------------------
    // Find the level of a node n -> {m1,m2,...} such that
    //		level(n -> {})			= 0
    //		level(n -> {m1,m2,...})	= 1 + std::max(level(mi))
    //-----------------------------------------------------------
    using Levelfun = std::function<int(const N&, std::map<N, int>&)>;

    Levelfun level = [&g, &level](const N& n1, std::map<N, int>& levelcache) -> int {
        auto p = levelcache.find(n1);
        if (p != levelcache.end()) {
            return p->second;
        } else {
            int l = -1;
            for (const auto& e : g.connections(n1)) {
                l = std::max(l, level(e.first, levelcache));
            }
            return levelcache[n1] = l + 1;
        }
    };

    std::map<N, int> levelcache;
    // compute the level of each node in the graph
    int l = -1;
    for (const N& n : g.nodes()) {
        l = std::max(l, level(n, levelcache));
    }
    // create a graph for each level and place
    // each node in the appropriate level
    std::vector<std::vector<N>> v;
    v.resize(l + 1);
    for (const N& n : g.nodes()) {
        v[levelcache[n]].push_back(n);
    }

    return v;
}

// Assembly:
subq $0x108, %rsp            # imm = 0x108
movq %rdi, 0x20(%rsp)
movq %rdi, %rax
movq %rax, 0x28(%rsp)
movq %rdi, 0x100(%rsp)
movq %rsi, 0xf8(%rsp)
movq 0xf8(%rsp), %rax
movq %rax, 0xc8(%rsp)
leaq 0xd8(%rsp), %rdi
movq %rdi, 0xd0(%rsp)
leaq 0xc8(%rsp), %rsi
callq 0x34b30
leaq 0x98(%rsp), %rdi
callq 0x305c0
movl $0xffffffff, 0x94(%rsp) # imm = 0xFFFFFFFF
movq 0xf8(%rsp), %rdi
callq 0x32690
movq %rax, 0x30(%rsp)
jmp 0x253fd
movq 0x30(%rsp), %rax
movq %rax, 0x88(%rsp)
movq 0x88(%rsp), %rdi
callq 0x326b0
movq %rax, 0x70(%rsp)
movq 0x88(%rsp), %rdi
callq 0x326e0
movq %rax, 0x68(%rsp)
leaq 0x70(%rsp), %rdi
leaq 0x68(%rsp), %rsi
callq 0x32710
testb $0x1, %al
jne 0x25446
jmp 0x254c9
leaq 0x70(%rsp), %rdi
callq 0x32730
movq %rax, 0x60(%rsp)
movq 0x60(%rsp), %rsi
leaq 0xd8(%rsp), %rdi
leaq 0x98(%rsp), %rdx
callq 0x34c00
movl %eax, 0x1c(%rsp)
jmp 0x25475
movl 0x1c(%rsp), %eax
movl %eax, 0x5c(%rsp)
leaq 0x94(%rsp), %rdi
leaq 0x5c(%rsp), %rsi
callq 0x34bc0
movq %rax, 0x10(%rsp)
jmp 0x25496
movq 0x10(%rsp), %rax
movl (%rax), %eax
movl %eax, 0x94(%rsp)
leaq 0x70(%rsp), %rdi
callq 0x32850
jmp 0x2542e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x80(%rsp)
movl %eax, 0x7c(%rsp)
jmp 0x255f0
movq 0x20(%rsp), %rdi
movb $0x0, 0x5b(%rsp)
callq 0x34c50
movq 0x20(%rsp), %rdi
movl 0x94(%rsp), %eax
incl %eax
movslq %eax, %rsi
callq 0x34c60
jmp 0x254f0
movq 0xf8(%rsp), %rdi
callq 0x32690
movq %rax, 0x8(%rsp)
jmp 0x25504
movq 0x8(%rsp), %rax
movq %rax, 0x50(%rsp)
movq 0x50(%rsp), %rdi
callq 0x326b0
movq %rax, 0x48(%rsp)
movq 0x50(%rsp), %rdi
callq 0x326e0
movq %rax, 0x40(%rsp)
leaq 0x48(%rsp), %rdi
leaq 0x40(%rsp), %rsi
callq 0x32710
testb $0x1, %al
jne 0x25541
jmp 0x255b3
leaq 0x48(%rsp), %rdi
callq 0x32730
movq %rax, 0x38(%rsp)
movq 0x38(%rsp), %rsi
leaq 0x98(%rsp), %rdi
callq 0x31600
movq %rax, (%rsp)
jmp 0x25568
movq 0x20(%rsp), %rdi
movq (%rsp), %rax
movslq (%rax), %rsi
callq 0x34d00
movq %rax, %rdi
movq 0x38(%rsp), %rsi
callq 0x34d20
jmp 0x25588
jmp 0x2558a
leaq 0x48(%rsp), %rdi
callq 0x32850
jmp 0x2552c
movq 0x20(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x80(%rsp)
movl %eax, 0x7c(%rsp)
callq 0x26410
jmp 0x255f0
movb $0x1, 0x5b(%rsp)
testb $0x1, 0x5b(%rsp)
jne 0x255c9
movq 0x20(%rsp), %rdi
callq 0x26410
leaq 0x98(%rsp), %rdi
callq 0x2de40
leaq 0xd8(%rsp), %rdi
callq 0x34d90
movq 0x28(%rsp), %rax
addq $0x108, %rsp            # imm = 0x108
retq
leaq 0x98(%rsp), %rdi
callq 0x2de40
leaq 0xd8(%rsp), %rdi
callq 0x34d90
movq 0x80(%rsp), %rdi
callq 0x2290
nopw (%rax,%rax)