// File: /workspace/github_c_makefile_star2_O0/6851-2021[P]euler_tour_tree/euler_tour_tree.c
// Function: link
int link(EulerTourTree* tree, k_t u, k_t v) {
    if (u >= tree->n) {
        extend_euler_tour_tree(tree, u+1);
    }
    if (v >= tree->n) {
        extend_euler_tour_tree(tree, v+1);
    }

    if (!tree->has_parent[u]) {
        tree->has_parent[u] = true;
    }
    else {
        return 1;
    }

    if (find_root(tree, v) == u) {
        return 2;
    }
    

    // split before last visit to v
    Node* left = split_left(tree->visits[v].last);

    // create new visit to v
    int is_first = (tree->visits[v].first == tree->visits[v].last);
    Node* new_v = make_node(v, tree->visits[v].first->value, is_first);

    // concatenate visits in the correct order
    Node* root_node = merge(left, new_v);
    root(tree->visits[u].first);
    root_node = merge(root_node, tree->visits[u].first);
    root_node = merge(root_node, tree->visits[v].last);

    // if v was a leaf, need to update visits
    if (is_first) {
        update_node_start(tree->visits[v].first, 0);
        tree->visits[v].first = new_v;
    }

    return 0;
}

// Assembly:
subq $0x48, %rsp
movq %rdi, 0x38(%rsp)
movq %rsi, 0x30(%rsp)
movq %rdx, 0x28(%rsp)
movq 0x30(%rsp), %rax
movq 0x38(%rsp), %rcx
cmpq 0x10(%rcx), %rax
jb 0x2166
movq 0x38(%rsp), %rdi
movq 0x30(%rsp), %rsi
addq $0x1, %rsi
callq 0x1db0
movq 0x28(%rsp), %rax
movq 0x38(%rsp), %rcx
cmpq 0x10(%rcx), %rax
jb 0x2189
movq 0x38(%rsp), %rdi
movq 0x28(%rsp), %rsi
addq $0x1, %rsi
callq 0x1db0
movq 0x38(%rsp), %rax
movq 0x8(%rax), %rax
movq 0x30(%rsp), %rcx
testb $0x1, (%rax,%rcx)
jne 0x21b1
movq 0x38(%rsp), %rax
movq 0x8(%rax), %rax
movq 0x30(%rsp), %rcx
movb $0x1, (%rax,%rcx)
jmp 0x21be
movl $0x1, 0x44(%rsp)
jmp 0x2332
movq 0x38(%rsp), %rdi
movq 0x28(%rsp), %rsi
callq 0x1f60
cmpq 0x30(%rsp), %rax
jne 0x21e1
movl $0x2, 0x44(%rsp)
jmp 0x2332
movq 0x38(%rsp), %rax
movq (%rax), %rax
movq 0x28(%rsp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rdi
callq 0x1740
movq %rax, 0x20(%rsp)
movq 0x38(%rsp), %rax
movq (%rax), %rax
movq 0x28(%rsp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq (%rax), %rax
movq 0x38(%rsp), %rcx
movq (%rcx), %rcx
movq 0x28(%rsp), %rdx
shlq $0x4, %rdx
addq %rdx, %rcx
cmpq 0x8(%rcx), %rax
sete %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, 0x1c(%rsp)
movq 0x28(%rsp), %rdi
movq 0x38(%rsp), %rax
movq (%rax), %rax
movq 0x28(%rsp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq (%rax), %rax
movq 0x20(%rax), %rsi
movl 0x1c(%rsp), %edx
callq 0x14b0
movq %rax, 0x10(%rsp)
movq 0x20(%rsp), %rdi
movq 0x10(%rsp), %rsi
callq 0x1800
movq %rax, 0x8(%rsp)
movq 0x38(%rsp), %rax
movq (%rax), %rax
movq 0x30(%rsp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq (%rax), %rdi
callq 0x1b70
movq 0x8(%rsp), %rdi
movq 0x38(%rsp), %rax
movq (%rax), %rax
movq 0x30(%rsp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq (%rax), %rsi
callq 0x1800
movq %rax, 0x8(%rsp)
movq 0x8(%rsp), %rdi
movq 0x38(%rsp), %rax
movq (%rax), %rax
movq 0x28(%rsp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rsi
callq 0x1800
movq %rax, 0x8(%rsp)
cmpl $0x0, 0x1c(%rsp)
je 0x232a
movq 0x38(%rsp), %rax
movq (%rax), %rax
movq 0x28(%rsp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq (%rax), %rdi
xorl %esi, %esi
callq 0x1890
movq 0x10(%rsp), %rcx
movq 0x38(%rsp), %rax
movq (%rax), %rax
movq 0x28(%rsp), %rdx
shlq $0x4, %rdx
addq %rdx, %rax
movq %rcx, (%rax)
movl $0x0, 0x44(%rsp)
movl 0x44(%rsp), %eax
addq $0x48, %rsp
retq
nopl (%rax,%rax)