// File: /workspace/github_cpp_makefile_star2_O2/canzarlab[P]Trajan/src/match.cpp
// Function: forest_forest(tree const&, tree const&, array2d const&, unsigned long long, unsigned long long)
int forest_forest(const tree& t1, const tree& t2, const array2d& matrix, mask rx, mask ry)
{
    // trivial case: empty forest
    if (rx == 0 || ry == 0)
        return 0;

    // memoization
    auto it = fdp[rx].find(ry);
    if (it != fdp[rx].end())
        return get<0>(it->second);

    // score, transition type, transition destination, bipartite matching
    // (branching) roots of trees in t1 and t2 respectively
    // roots of paths in va and vb respectively
    // bipartite matching transition table
    // branch choice table
    auto& [sol, trs, dst, bdp, va, vb, ra, rb, btr, bst] = fdp[rx][ry];

    // initialize score to negative infinity
    sol = NINF;

    // don't waste time on reallocation
    va.reserve(MAXLB);
    vb.reserve(MAXLB);
    ra.reserve(MAXLB);
    rb.reserve(MAXLB);

    // remove a path in t1
    for (mask mp = rx; mp != 0; mp -= ls(mp))
    {
        // branching node index
        int u = tz(mp);
        // dont disconnect the current root-to-branching path
        if (rx & t1.ch[u])
            continue;

        // for bipartite matching consider only nodes which are leaf/branching in current subforest
        va.push_back(u);

        // path  x..z in t1
        int z = t1.b[u];
        int x = find_root(t1, rx, u);
        ra.push_back(x);

        // don't remove leaves
        if (!t1.ch[u])
            continue;

        // exclude u, include its branching children, exclude its branching ancestors
        mask m = em(rx ^ ls(mp) ^ t1.ch[u], t1.ba[u]);
        int nsol = forest_forest(t1, t2, matrix, m, ry);
        if (nsol > sol)
            sol = nsol, trs = DEL_1, dst = u;
    }

    // remove a path in t2
    for (mask mq = ry; mq != 0; mq -= ls(mq))
    {
        // branching node index
        int v = tz(mq);
        // dont disconnect the current root-to-branching path
        if (ry & t2.ch[v])
            continue;

        // for bipartite matching consider only nodes which are leaf/branching in current subforest
        vb.push_back(v);

        // path y..w in t2
        int w = t2.b[v];
        int y = find_root(t2, ry, v);
        rb.push_back(y);

        // don't remove leaves
        if (!t2.ch[v])
            continue;

        // exclude v, include its branching children, exclude its branching ancestors
        mask m = em(ry ^ ls(mq) ^ t2.ch[v], t2.ba[v]);
        int nsol = forest_forest(t1, t2, matrix, rx, m);
        if (nsol > sol)
            sol = nsol, trs = DEL_2, dst = v;
    }

    // tree to tree distance table
    array2d D(va.size(), vb.size());

    // initialize tree to tree transition tables
    btr = array2d(va.size(), vb.size());
    bst = array2d(va.size(), vb.size());

    // compute tree-to-tree weights for use in bipartite matching
    for (uint i = 0; i < va.size(); ++i)
    {
        for (uint j = 0; j < vb.size(); ++j)
        {
            // branching node index
            int u = va[i], v = vb[j];
            // path x..z in t1
            int z = t1.b[u];
            int x = ra[i];
            // path y..w in t2
            int w = t2.b[v];
            int y = rb[j];
            // corresponding distance table entry
            int& uvs = D(i, j);

            // select a branch at u
            for (mask s = t1.ch[u]; s != 0; s -= ls(s))
            {
                // on t1 side: include u, chosen branch s and keep branching ancestors of u
                // on t2 side: include v and keep branching ancestors of v
                int nuvs = forest_forest(t1, t2, matrix, in(u) | ls(s) | (rx & t1.ba[u]), in(v) | (ry & t2.ba[v]));
                if (nuvs > uvs)
                    uvs = nuvs, btr(i, j) = DEL_1, bst(i, j) = tz(s);
            }

            // select a branch at v
            for (mask s = t2.ch[v]; s != 0; s -= ls(s))
            {
                // on t1 side: include u and keep branching ancestors of u
                // on t2 side: include v, chosen branch s and keep branching ancestors of v
                int nuvs = forest_forest(t1, t2, matrix, in(u) | (rx & t1.ba[u]), in(v) | ls(s) | (ry & t2.ba[v]));
                if (nuvs > uvs)
                    uvs = nuvs, btr(i, j) = DEL_2, bst(i, j) = tz(s);
            }

            // initialize or fetch path-to-path matching dp table
            auto it = pdp.find({x, y});
            if (it == pdp.end())
                pdp[{x, y}] = {array2d(t1.n, t2.n), array2d(t1.n, t2.n)};
            auto& [dp, tdp] = pdp[{x, y}];

            // match the paths
            int nuvs = forest_forest(t1, t2, matrix, t1.ch[u], t2.ch[v]) + path_path(dp, tdp, matrix, t1, t2, x, y, z, w);
            if (nuvs > uvs)
                uvs = nuvs, btr(i, j) = MATCH, bst(i, j) = -1;
        }
    }
    // compute optimal forest-to-forest bipartite matching
    int nsol = bipartite_aux(D, bdp);
    if (nsol > sol)
        sol = nsol, trs = MATCH, dst = -1;

    return sol;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x138, %rsp            # imm = 0x138
movq %rcx, 0x20(%rsp)
movq %r8, 0x10(%rsp)
testq %rcx, %rcx
sete %al
testq %r8, %r8
sete %cl
xorl %ebp, %ebp
orb %al, %cl
jne 0x4491
movq %rdi, 0x8(%rsp)
movq %rsi, 0x28(%rsp)
movq %rdx, 0x38(%rsp)
leaq 0xc5a4(%rip), %r12      # 0x10248
leaq 0x20(%rsp), %r13
movq %r12, %rdi
movq %r13, %rsi
callq 0x6d4a
leaq 0x10(%rsp), %rsi
movq %rax, %rdi
callq 0x70ee
movq %rax, %rbx
movq %r12, %rdi
movq %r13, %rsi
callq 0x6d4a
testq %rbx, %rbx
je 0x3cdf
movl 0xd0(%rbx), %ebp
jmp 0x4491
leaq 0xc562(%rip), %rdi      # 0x10248
leaq 0x20(%rsp), %rbp
movq %rbp, %rsi
callq 0x6d4a
leaq 0x10(%rsp), %rsi
movq %rax, %rdi
callq 0x716a
leaq 0x88(%rax), %rdi
leaq 0x70(%rax), %r13
leaq 0x58(%rax), %r14
leaq 0x40(%rax), %r15
movq %rax, (%rsp)
movl $0x80000000, 0xc0(%rax) # imm = 0x80000000
pushq $0x40
popq %rbx
movq %rdi, 0x70(%rsp)
movq %rbx, %rsi
callq 0x5254
movq %r13, %rdi
movq %rbx, %rsi
callq 0x5254
movq %r14, 0x68(%rsp)
movq %r14, %rdi
movq %rbx, %rsi
callq 0x5254
movq %r15, 0x60(%rsp)
movq %r15, %rdi
movq %rbx, %rsi
callq 0x5254
movq (%rbp), %rbx
leaq 0xc0(%rsp), %rbp
movq 0x38(%rsp), %r14
movq 0x28(%rsp), %r12
movq 0x8(%rsp), %r15
testq %rbx, %rbx
je 0x3e6f
bsfq %rbx, %rax
movl %eax, 0x80(%rsp)
movq 0x8(%rsp), %rcx
movq 0x6eb8(%rcx,%rax,8), %rax
testq %rax, 0x20(%rsp)
je 0x3da8
leaq -0x1(%rbx), %rax
andq %rax, %rbx
jmp 0x3d77
movq 0x70(%rsp), %rdi
leaq 0x80(%rsp), %rsi
callq 0x52ea
movslq 0x80(%rsp), %rax
movq 0x8058(%r15,%rax,8), %rcx
btsq %rax, %rcx
andq 0x20(%rsp), %rcx
bsfq %rcx, %rax
movl 0x8258(%r15,%rax,4), %eax
movl %eax, 0xc0(%rsp)
movq 0x68(%rsp), %rdi
movq %rbp, %rsi
callq 0x52ea
movslq 0x80(%rsp), %rax
movq 0x6eb8(%r15,%rax,8), %rcx
testq %rcx, %rcx
je 0x3d9f
movq %rbx, %rdx
negq %rdx
andq %rbx, %rdx
xorq 0x20(%rsp), %rdx
xorq %rcx, %rdx
movq 0x8(%rsp), %rdi
movq 0x8058(%rdi,%rax,8), %rcx
notq %rcx
andq %rdx, %rcx
movq 0x10(%rsp), %r8
movq %r12, %rsi
movq %r14, %rdx
callq 0x3c5d
movq (%rsp), %rcx
cmpl 0xc0(%rcx), %eax
jle 0x3d9f
movq (%rsp), %rcx
movl %eax, 0xc0(%rcx)
andl $0x0, 0xbc(%rcx)
movl 0x80(%rsp), %eax
movl %eax, 0xb8(%rcx)
jmp 0x3d9f
movq (%rsp), %rax
addq $0xa0, %rax
movq %rax, 0xe0(%rsp)
movq 0x10(%rsp), %rbx
leaq 0x80(%rsp), %r15
leaq 0xc0(%rsp), %rbp
testq %rbx, %rbx
je 0x3f85
bsfq %rbx, %rax
movl %eax, 0x80(%rsp)
movq 0x6eb8(%r12,%rax,8), %rax
testq %rax, 0x10(%rsp)
je 0x3ec2
leaq -0x1(%rbx), %rax
andq %rax, %rbx
jmp 0x3e96
movq %r13, %rdi
movq %r15, %rsi
callq 0x52ea
movslq 0x80(%rsp), %rax
movq 0x8058(%r12,%rax,8), %rcx
btsq %rax, %rcx
andq 0x10(%rsp), %rcx
bsfq %rcx, %rax
movl 0x8258(%r12,%rax,4), %eax
movl %eax, 0xc0(%rsp)
movq 0x60(%rsp), %rdi
movq %rbp, %rsi
callq 0x52ea
movslq 0x80(%rsp), %rax
movq 0x6eb8(%r12,%rax,8), %rcx
testq %rcx, %rcx
je 0x3eb9
movq %rbx, %rdx
negq %rdx
andq %rbx, %rdx
xorq 0x10(%rsp), %rdx
xorq %rcx, %rdx
movq 0x8058(%r12,%rax,8), %r8
notq %r8
andq %rdx, %r8
movq 0x20(%rsp), %rcx
movq 0x8(%rsp), %rdi
movq %r12, %rsi
movq %r14, %rdx
callq 0x3c5d
movq (%rsp), %rcx
cmpl 0xc0(%rcx), %eax
jle 0x3eb9
movq (%rsp), %rcx
movl %eax, 0xc0(%rcx)
movl $0x1, 0xbc(%rcx)
movl 0x80(%rsp), %eax
movl %eax, 0xb8(%rcx)
jmp 0x3eb9
movq (%rsp), %rbx
movq 0x78(%rbx), %rdx
movq 0x90(%rbx), %rsi
subq 0x88(%rbx), %rsi
shrq $0x2, %rsi
subq 0x70(%rbx), %rdx
shrq $0x2, %rdx
leaq 0xc0(%rsp), %rdi
movl $0x80000000, %ecx       # imm = 0x80000000
callq 0x517e
movq 0x78(%rbx), %rdx
movq 0x90(%rbx), %rsi
subq 0x88(%rbx), %rsi
shrq $0x2, %rsi
subq 0x70(%rbx), %rdx
shrq $0x2, %rdx
leaq 0x80(%rsp), %rdi
movl $0x80000000, %ecx       # imm = 0x80000000
callq 0x517e
movq %rbx, %rdi
addq $0x20, %rdi
leaq 0x80(%rsp), %rsi
callq 0x5162
leaq 0x88(%rsp), %rdi
callq 0x65ec
movq 0x78(%rbx), %rdx
movq 0x90(%rbx), %rsi
subq 0x88(%rbx), %rsi
shrq $0x2, %rsi
subq 0x70(%rbx), %rdx
shrq $0x2, %rdx
leaq 0x80(%rsp), %rdi
movl $0x80000000, %ecx       # imm = 0x80000000
callq 0x517e
movq 0x8(%rsp), %rbp
leaq 0x80(%rsp), %rsi
movq %rbx, %rdi
callq 0x5162
leaq 0x88(%rsp), %rdi
callq 0x65ec
xorl %edx, %edx
movl %edx, %edx
movq 0x90(%rbx), %rax
subq 0x88(%rbx), %rax
sarq $0x2, %rax
cmpq %rdx, %rax
jbe 0x444c
xorl %esi, %esi
movq %rdx, 0x48(%rsp)
movl %esi, %esi
movq 0x70(%rbx), %rax
movq 0x78(%rbx), %rcx
subq %rax, %rcx
sarq $0x2, %rcx
cmpq %rsi, %rcx
jbe 0x4445
movq 0x70(%rsp), %rcx
movq (%rcx), %rcx
movslq (%rcx,%rdx,4), %rcx
movslq (%rax,%rsi,4), %rdi
movl 0x5e14(%rbp,%rcx,4), %eax
movq %rax, 0xf0(%rsp)
movq 0x68(%rsp), %rax
movq (%rax), %rax
movl (%rax,%rdx,4), %eax
movl %eax, 0x1c(%rsp)
movl 0x5e14(%r12,%rdi,4), %eax
movq %rax, 0xe8(%rsp)
movq 0x60(%rsp), %rax
movq (%rax), %rax
movl (%rax,%rsi,4), %eax
movq %rax, 0x30(%rsp)
movl 0xc4(%rsp), %eax
imull %edx, %eax
movq %rsi, 0x50(%rsp)
addl %esi, %eax
movslq %eax, %r14
movq 0xc8(%rsp), %r15
movq 0x6eb8(%rbp,%rcx,8), %rbp
pushq $0x1
popq %rbx
movq %rbx, %r12
movq %rcx, 0x58(%rsp)
shlq %cl, %r12
movq %rdi, 0x40(%rsp)
movl %edi, %ecx
shlq %cl, %rbx
testq %rbp, %rbp
je 0x41c4
movq %rbp, %r13
negq %r13
movq 0x8(%rsp), %rdi
movq 0x58(%rsp), %rax
movq 0x8058(%rdi,%rax,8), %rax
andq 0x20(%rsp), %rax
andq %rbp, %r13
movq %r12, %rcx
orq %r13, %rcx
orq %rax, %rcx
movq 0x28(%rsp), %rsi
movq 0x40(%rsp), %rax
movq 0x8058(%rsi,%rax,8), %r8
andq 0x10(%rsp), %r8
orq %rbx, %r8
movq 0x38(%rsp), %rdx
callq 0x3c5d
cmpl (%r15,%r14,4), %eax
jle 0x41bc
movl %eax, (%r15,%r14,4)
movq (%rsp), %rdx
movl 0x24(%rdx), %eax
movq 0x48(%rsp), %rsi
imull %esi, %eax
movq 0x50(%rsp), %rdi
addl %edi, %eax
cltq
movq 0x28(%rdx), %rcx
andl $0x0, (%rcx,%rax,4)
bsfq %rbp, %rax
movl 0x4(%rdx), %ecx
imull %esi, %ecx
addl %edi, %ecx
movslq %ecx, %rcx
movq 0x8(%rdx), %rdx
movl %eax, (%rdx,%rcx,4)
subq %r13, %rbp
jmp 0x4126
movq 0x28(%rsp), %rax
movq 0x40(%rsp), %rcx
movq 0x6eb8(%rax,%rcx,8), %r13
testq %r13, %r13
je 0x4277
movq 0x8(%rsp), %rdi
movq 0x58(%rsp), %rax
movq 0x8058(%rdi,%rax,8), %rcx
andq 0x20(%rsp), %rcx
orq %r12, %rcx
movq %r13, %rbp
negq %rbp
movq 0x28(%rsp), %rsi
movq 0x40(%rsp), %rax
movq 0x8058(%rsi,%rax,8), %rax
andq 0x10(%rsp), %rax
andq %r13, %rbp
movq %rbx, %r8
orq %rbp, %r8
orq %rax, %r8
movq 0x38(%rsp), %rdx
callq 0x3c5d
cmpl (%r15,%r14,4), %eax
jle 0x426f
movl %eax, (%r15,%r14,4)
movq (%rsp), %rdx
movl 0x24(%rdx), %eax
movq 0x48(%rsp), %rsi
imull %esi, %eax
movq 0x50(%rsp), %rdi
addl %edi, %eax
cltq
movq 0x28(%rdx), %rcx
movl $0x1, (%rcx,%rax,4)
bsfq %r13, %rax
movl 0x4(%rdx), %ecx
imull %esi, %ecx
addl %edi, %ecx
movslq %ecx, %rcx
movq 0x8(%rdx), %rdx
movl %eax, (%rdx,%rcx,4)
subq %rbp, %r13
jmp 0x41d6
movq 0x30(%rsp), %rax
movl %eax, 0x80(%rsp)
movl 0x1c(%rsp), %eax
movl %eax, 0x84(%rsp)
leaq 0xbfeb(%rip), %rdi      # 0x10280
leaq 0x80(%rsp), %rbp
movq %rbp, %rsi
callq 0x747a
leaq 0xbfdc(%rip), %rcx      # 0x10288
cmpq %rcx, %rax
movq 0x38(%rsp), %r13
movq 0x28(%rsp), %r12
jne 0x436e
movq 0x8(%rsp), %rbx
movl 0x5e08(%rbx), %esi
movl 0x5e08(%r12), %edx
leaq 0xf8(%rsp), %rdi
movl $0x80000000, %ecx       # imm = 0x80000000
callq 0x517e
movl 0x5e08(%rbx), %esi
movl 0x5e08(%r12), %edx
leaq 0x118(%rsp), %r12
movq %r12, %rdi
movl $0x80000000, %ecx       # imm = 0x80000000
callq 0x517e
movq %rbp, %rdi
leaq 0xf8(%rsp), %rsi
movq %r12, %rdx
callq 0x5308
movq 0x30(%rsp), %rax
movl %eax, 0x78(%rsp)
movl 0x1c(%rsp), %eax
movl %eax, 0x7c(%rsp)
leaq 0xbf4e(%rip), %rdi      # 0x10280
leaq 0x78(%rsp), %rsi
callq 0x532e
movq %rax, %rdi
movq %rbp, %rsi
callq 0x5394
movq %rbp, %rdi
callq 0x53be
leaq 0x120(%rsp), %rdi
callq 0x65ec
leaq 0x100(%rsp), %rdi
callq 0x65ec
movq 0x28(%rsp), %r12
movq 0x30(%rsp), %rax
movl %eax, 0x80(%rsp)
movl 0x1c(%rsp), %eax
movl %eax, 0x84(%rsp)
leaq 0xbef4(%rip), %rdi      # 0x10280
movq %rbp, %rsi
callq 0x532e
movq %rax, %rbx
movq 0x58(%rsp), %rax
movq 0x8(%rsp), %rbp
movq 0x6eb8(%rbp,%rax,8), %rcx
movq 0x40(%rsp), %rax
movq 0x6eb8(%r12,%rax,8), %r8
movq %rbp, %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0x3c5d
movq %rbx, %rsi
addq $0x20, %rsi
subq $0x8, %rsp
movq %rbx, %rdi
movq %r13, %rdx
movq %rbp, %rcx
movq %r12, %r8
movl 0x24(%rsp), %r9d
pushq 0xf0(%rsp)
pushq 0x100(%rsp)
pushq 0x48(%rsp)
movl %eax, %ebx
callq 0x3771
addq $0x20, %rsp
addl %ebx, %eax
cmpl (%r15,%r14,4), %eax
movq (%rsp), %rbx
movq 0x48(%rsp), %rdx
movq 0x50(%rsp), %rsi
jle 0x443e
movl %eax, (%r15,%r14,4)
movl 0x24(%rbx), %eax
imull %edx, %eax
addl %esi, %eax
cltq
movq 0x28(%rbx), %rcx
movl $0x2, (%rcx,%rax,4)
movl 0x4(%rbx), %eax
imull %edx, %eax
addl %esi, %eax
cltq
movq 0x8(%rbx), %rcx
orl $-0x1, (%rcx,%rax,4)
incl %esi
jmp 0x4082
incl %edx
jmp 0x405e
leaq 0xc0(%rsp), %rdi
movq 0xe0(%rsp), %rsi
callq 0x3a88
movl 0xc0(%rbx), %ebp
cmpl %ebp, %eax
jle 0x4484
movl %eax, 0xc0(%rbx)
movabsq $0x2ffffffff, %rcx      # imm = 0x2FFFFFFFF
movq %rcx, 0xb8(%rbx)
movl %eax, %ebp
leaq 0xc8(%rsp), %rdi
callq 0x65ec
movl %ebp, %eax
addq $0x138, %rsp            # imm = 0x138
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x44e6
jmp 0x44e6
jmp 0x44e6
movq %rax, %rbx
jmp 0x44cf
jmp 0x44e6
movq %rax, %rbx
leaq 0x80(%rsp), %rdi
callq 0x53be
leaq 0x120(%rsp), %rdi
callq 0x65ec
leaq 0x100(%rsp), %rdi
callq 0x65ec
jmp 0x44e9
jmp 0x44e6
jmp 0x44e6
jmp 0x44e6
jmp 0x44e6
movq %rax, %rbx
leaq 0xc8(%rsp), %rdi
callq 0x65ec
movq %rbx, %rdi
callq 0x33e0