// File: kiudee[P]VAHD-Projekt/Supervisor.cpp
// Function: Supervisor::SetLink(IdPair*)
Action Supervisor::SetLink(IdPair *idop)
{
    NumObj *numo;

    Nodes.push_back(new NodeRelay(idop->ido1));
    Relay *temprelay = new Relay(idop->ido1->id);//TODO please review
    subjects[idop->ido1->num] = temprelay;
    delete idop->ido1;
    StartID.push_back(idop->ido2);
    delete idop;
    count++;

    // If the last Node has registered we can link them with relays
    if (count == total) {
        for (int i = 0; i < total; i++) {
            // make sure that node i periodically wakes up
            numo = new NumObj(5);
            Nodes[i]->out->call(Node::Wakeup, numo);

            // initially connect node i to nodes 2i and 2i+1
            if (2 * i + 1 < total) {
                Nodes[i]->out->call(Node::Join, StartID[2 * i + 1]);
            }
            if (2 * i + 2 < total) {
                Nodes[i]->out->call(Node::Join, StartID[2 * i + 2]);
            }
        }

        numo = new NumObj(14000);
        call(Supervisor::Wakeup, numo);
    }
    if (count > total) {
        numo = new NumObj(5);
        Nodes[count - 1]->out->call(Node::Wakeup, numo);
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x140, %rsp            # imm = 0x140
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0xa8(%rbp)
addq $0x68, %rax
movq %rax, -0xa0(%rbp)
movl $0x38, %edi
callq 0x31c0
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x98(%rbp)
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rsi
callq 0x57f0
jmp 0x387f
movq -0xa0(%rbp), %rdi
movq -0x98(%rbp), %rax
movq %rax, -0x20(%rbp)
leaq -0x20(%rbp), %rsi
callq 0x57c0
movl $0x48, %edi
callq 0x31c0
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0xb0(%rbp)
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rsi
addq $0x28, %rsi
callq 0x5890
jmp 0x38c4
movq -0xa8(%rbp), %rdi
movq -0xb0(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rax
movq %rax, -0xc0(%rbp)
addq $0xa8, %rdi
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rsi
addq $0x20, %rsi
callq 0x59f0
movq -0xc0(%rbp), %rcx
movq %rcx, (%rax)
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movq %rax, -0xb8(%rbp)
cmpq $0x0, %rax
je 0x3929
movq -0xb8(%rbp), %rdi
movl $0x38, %esi
callq 0x31d0
movq -0xa8(%rbp), %rdi
addq $0x80, %rdi
movq -0x10(%rbp), %rsi
addq $0x28, %rsi
callq 0x5a20
movq -0x10(%rbp), %rax
movq %rax, -0xc8(%rbp)
cmpq $0x0, %rax
je 0x3966
movq -0xc8(%rbp), %rdi
movl $0x30, %esi
callq 0x31d0
movq -0xa8(%rbp), %rcx
movl 0x60(%rcx), %eax
addl $0x1, %eax
movl %eax, 0x60(%rcx)
movl 0x60(%rcx), %eax
cmpl 0x64(%rcx), %eax
jne 0x3d59
movl $0x0, -0x3c(%rbp)
movq -0xa8(%rbp), %rcx
movl -0x3c(%rbp), %eax
cmpl 0x64(%rcx), %eax
jge 0x3c91
movl $0x28, %edi
callq 0x31c0
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0xd0(%rbp)
movl $0x5, %esi
callq 0x5a90
jmp 0x39bf
movq -0xa8(%rbp), %rdi
movq -0xd0(%rbp), %rax
movq %rax, -0x18(%rbp)
addq $0x68, %rdi
movslq -0x3c(%rbp), %rsi
callq 0x5ac0
movq (%rax), %rax
movq 0x28(%rax), %rax
movq %rax, -0xe0(%rbp)
movl $0x38, %edi
callq 0x31c0
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0xd8(%rbp)
leaq 0xbb76(%rip), %rax      # 0xf580
movq %rax, -0x50(%rbp)
movq $0x0, -0x48(%rbp)
movq -0x50(%rbp), %rsi
movq -0x48(%rbp), %rdx
leaq -0x18(%rbp), %rcx
callq 0x5ae0
jmp 0x3a29
movq -0xa8(%rbp), %rsi
movq -0xd8(%rbp), %rdx
movq -0xe0(%rbp), %rdi
movq (%rdi), %rax
callq *0x8(%rax)
movq -0xa8(%rbp), %rcx
movl -0x3c(%rbp), %eax
shll %eax
addl $0x1, %eax
cmpl 0x64(%rcx), %eax
jge 0x3ba8
movq -0xa8(%rbp), %rdi
addq $0x68, %rdi
movslq -0x3c(%rbp), %rsi
callq 0x5ac0
movq (%rax), %rax
movq 0x28(%rax), %rax
movq %rax, -0xf8(%rbp)
movl $0x38, %edi
callq 0x31c0
movq -0xa8(%rbp), %rdi
movq %rax, -0xe8(%rbp)
movq %rax, -0xf0(%rbp)
subq $-0x80, %rdi
movl -0x3c(%rbp), %ecx
movl %ecx, %eax
leal 0x1(%rax,%rax), %eax
movslq %eax, %rsi
callq 0x5bd0
movq -0xe8(%rbp), %rdi
movq %rax, %rcx
leaq 0xfefd(%rip), %rax      # 0x139c0
movq %rax, -0x60(%rbp)
movq $0x0, -0x58(%rbp)
movq -0x60(%rbp), %rsi
movq -0x58(%rbp), %rdx
callq 0x5ae0
jmp 0x3ade
movq -0xf0(%rbp), %rdx
movq -0xa8(%rbp), %rsi
movq -0xf8(%rbp), %rdi
movq (%rdi), %rax
callq *0x8(%rax)
jmp 0x3ba8
movq -0x98(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x28(%rbp)
movl %eax, -0x2c(%rbp)
movl $0x38, %esi
callq 0x31d0
jmp 0x3e70
movq -0xb0(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x28(%rbp)
movl %eax, -0x2c(%rbp)
movl $0x48, %esi
callq 0x31d0
jmp 0x3e70
movq -0xd0(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x28(%rbp)
movl %eax, -0x2c(%rbp)
movl $0x28, %esi
callq 0x31d0
jmp 0x3e70
movq -0xd8(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x28(%rbp)
movl %eax, -0x2c(%rbp)
movl $0x38, %esi
callq 0x31d0
jmp 0x3e70
movq -0xf0(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x28(%rbp)
movl %eax, -0x2c(%rbp)
movl $0x38, %esi
callq 0x31d0
jmp 0x3e70
movq -0xa8(%rbp), %rcx
movl -0x3c(%rbp), %eax
shll %eax
addl $0x2, %eax
cmpl 0x64(%rcx), %eax
jge 0x3c81
movq -0xa8(%rbp), %rdi
addq $0x68, %rdi
movslq -0x3c(%rbp), %rsi
callq 0x5ac0
movq (%rax), %rax
movq 0x28(%rax), %rax
movq %rax, -0x110(%rbp)
movl $0x38, %edi
callq 0x31c0
movq -0xa8(%rbp), %rdi
movq %rax, -0x100(%rbp)
movq %rax, -0x108(%rbp)
subq $-0x80, %rdi
movl -0x3c(%rbp), %ecx
movl %ecx, %eax
leal 0x2(%rax,%rax), %eax
movslq %eax, %rsi
callq 0x5bd0
movq -0x100(%rbp), %rdi
movq %rax, %rcx
leaq 0xfd99(%rip), %rax      # 0x139c0
movq %rax, -0x70(%rbp)
movq $0x0, -0x68(%rbp)
movq -0x70(%rbp), %rsi
movq -0x68(%rbp), %rdx
callq 0x5ae0
jmp 0x3c42
movq -0x108(%rbp), %rdx
movq -0xa8(%rbp), %rsi
movq -0x110(%rbp), %rdi
movq (%rdi), %rax
callq *0x8(%rax)
jmp 0x3c81
movq -0x108(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x28(%rbp)
movl %eax, -0x2c(%rbp)
movl $0x38, %esi
callq 0x31d0
jmp 0x3e70
jmp 0x3c83
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0x3989
movl $0x28, %edi
callq 0x31c0
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x118(%rbp)
movl $0x36b0, %esi           # imm = 0x36B0
callq 0x5a90
jmp 0x3cb4
movq -0x118(%rbp), %rax
movq %rax, -0x18(%rbp)
movl $0x38, %edi
callq 0x31c0
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x120(%rbp)
leaq 0x1a3(%rip), %rax       # 0x3e80
movq %rax, -0x80(%rbp)
movq $0x0, -0x78(%rbp)
movq -0x80(%rbp), %rsi
movq -0x78(%rbp), %rdx
leaq -0x18(%rbp), %rcx
callq 0x5ae0
jmp 0x3cfc
movq -0x120(%rbp), %rdx
movq -0xa8(%rbp), %rsi
movq (%rsi), %rax
movq %rsi, %rdi
callq *0x48(%rax)
jmp 0x3d59
movq -0x118(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x28(%rbp)
movl %eax, -0x2c(%rbp)
movl $0x28, %esi
callq 0x31d0
jmp 0x3e70
movq -0x120(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x28(%rbp)
movl %eax, -0x2c(%rbp)
movl $0x38, %esi
callq 0x31d0
jmp 0x3e70
movq -0xa8(%rbp), %rcx
movl 0x60(%rcx), %eax
cmpl 0x64(%rcx), %eax
jle 0x3e67
movl $0x28, %edi
callq 0x31c0
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x128(%rbp)
movl $0x5, %esi
callq 0x5a90
jmp 0x3d8f
movq -0xa8(%rbp), %rax
movq -0x128(%rbp), %rcx
movq %rcx, -0x18(%rbp)
movq %rax, %rdi
addq $0x68, %rdi
movl 0x60(%rax), %eax
decl %eax
movslq %eax, %rsi
callq 0x5ac0
movq (%rax), %rax
movq 0x28(%rax), %rax
movq %rax, -0x138(%rbp)
movl $0x38, %edi
callq 0x31c0
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x130(%rbp)
leaq 0xb79f(%rip), %rax      # 0xf580
movq %rax, -0x90(%rbp)
movq $0x0, -0x88(%rbp)
movq -0x90(%rbp), %rsi
movq -0x88(%rbp), %rdx
leaq -0x18(%rbp), %rcx
callq 0x5ae0
jmp 0x3e0c
movq -0x130(%rbp), %rdx
movq -0xa8(%rbp), %rsi
movq -0x138(%rbp), %rdi
movq (%rdi), %rax
callq *0x8(%rax)
jmp 0x3e67
movq -0x128(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x28(%rbp)
movl %eax, -0x2c(%rbp)
movl $0x28, %esi
callq 0x31d0
jmp 0x3e70
movq -0x130(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x28(%rbp)
movl %eax, -0x2c(%rbp)
movl $0x38, %esi
callq 0x31d0
jmp 0x3e70
addq $0x140, %rsp            # imm = 0x140
popq %rbp
retq
movq -0x28(%rbp), %rdi
callq 0x3330
nopl (%rax)