// File: /workspace/github_cpp_makefile_star2_O3/kiudee[P]VAHD-Projekt/Node.cpp
// Function: Node::FinishSearch(SearchJob*)
Action Node::FinishSearch(SearchJob *sj)
{
    //TO DO dont insert data on leaving nodes! num > MAX! => fixed in Search
    if (sj->type == LOOKUP || sj->type == INSERT || sj->type == DELETE) {
        std::cout << "(" << sj->_debugID << ") " << num << "<-FinishSearch(" << sj->sid << ")\n";
    }

    double hashedkey = sj->sid;
    if (sj->type == JOIN // SearchJob is a Join
            || (isReal && right == NULL && num <= hashedkey) // There is no right node and this node is responsible
            || (isReal && num <= hashedkey)// There is a right node but not responsible
            || (isReal && sj->sid == MAX)) { //The searchjob was delegated to the last node and now we reached the last node
        if (sj->type == LOOKUP || sj->type == INSERT || sj->type == DELETE) {
            std::cout << "(" << sj->_debugID << ") " << num << ": Search terminated!(" << sj->sid << ") after " << sj->hopcount << " hops  \n";
        }
        switch (sj->type) {
        case DATAINSERT:
            data[sj->dob->num] = sj->dob->date;
            break;
        case INSERT:
            data[sj->dob->num] = sj->dob->date;
            //Save hopcount to csv file:
            *csvFile << "Insert," << sj->hopcount << "," << std::abs(sj->startID - num) << "," << sj->startID << "," << num << "," << sj->bound << "," << "\n";
            csvFile->flush();
            break;
        case DELETE:
            data.erase(sj->key);
            //Save hopcount to csv file:
            *csvFile << "Delete," << sj->hopcount << "," << std::abs(sj->startID - num) << "," << sj->startID << "," << num << "," << sj->bound << "," << "\n";
            csvFile->flush();
            break;
        case LOOKUP: {
            DATATYPE obj = data[sj->key]; //TODO replace by unordered_map.at() and catch the exception. than pack an empty string or so.
            //std::cout << "Lookup reached key:" << sj->key << " data: " << obj << "\n";
            Relay *temprelay = new Relay(sj->ido->id);
            DateObj *dob = new DateObj(sj->key, obj);

            //Save hopcount to csv file:
            *csvFile << "Lookup," << sj->hopcount << "," << std::abs(sj->startID - num) << "," << sj->startID << "," << num << "," << sj->bound << "," << "\n";
            csvFile->flush();

            temprelay->call(Node::ReceiveLookUp, dob);
            //delete temprelay;
            break;
        }
        case JOIN: {
            IdObj *ido = sj->ido;
            //std::cout << ido->num << " joins at " << num << "\n";
            BuildList(ido); // just connect to given reference;

            if (ido->num > num && right != NULL) {
                //std::cout << num << " triggers a data transfer for " << ido->num << " (to right) \n";
                IdObj *tempido =
                    new IdObj(right->num, new Identity(right->out), right->debugID);
                TriggerDataTransfer(tempido);

            } else if (ido->num <= num && left != NULL) {//this case matches iff we are joining the new node in front of the first node.
                //std::cout << num << " triggers a data transfer for " << ido->num << " (to left)\n";
                if (!(right == NULL && !isReal)) { //we know that the joined node is the first from now on. if the actual node has no right neighbor and is not real, there is no data to transfer (because FinishSearch will determine that the joined node is responsible)
                    IdObj *tempido = new IdObj(left->num, new Identity(left->out), left->debugID);
                    SearchJob *newsj = new SearchJob(MAX, DATATRANSFER,
                                                     Node::calcRoutingBound(), tempido);
                    Search(newsj);
                }
            }

            break;
        }
        case DATATRANSFER: {
            IdObj *tempido = sj->ido;
            TriggerDataTransfer(tempido);
            break;
        }
        }
        //std::cout << "(" << sj->_debugID << ") " << "delete searchjob\n";
        delete sj;
        return;
    } else {
        //the actual node is the end of the list. send searchjob to the other end.
        if (left == NULL) {
            if (sj->sid < MAX) { //break recursion
                delegateSearchJobToLastNode(sj);
            }
        } else if (leftstable) {
            sj->round++;
            sj->hopcount++;
            left->out->call(Node::FinishSearch, sj);
            return;
        } else {
            call(Node::FinishSearch, sj);
            return;
        }
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %rsi, %rbx
movq %rdi, %r14
movl 0x38(%rsi), %eax
cmpl $0x2, %eax
ja 0x8a6b
movq 0x85e7(%rip), %r15      # 0x10fd0
leaq 0x3a6d(%rip), %rsi      # 0xc45d
movl $0x1, %edx
movq %r15, %rdi
callq 0x21a0
movq (%rbx), %rsi
movq %r15, %rdi
callq 0x20e0
movq %rax, %r15
leaq 0x3a24(%rip), %rsi      # 0xc436
movl $0x2, %edx
movq %rax, %rdi
callq 0x21a0
movsd 0xb0(%r14), %xmm0
movq %r15, %rdi
callq 0x2230
movq %rax, %r15
leaq 0x39ff(%rip), %rsi      # 0xc439
movl $0xf, %edx
movq %rax, %rdi
callq 0x21a0
movsd 0x20(%rbx), %xmm0
movq %r15, %rdi
callq 0x2230
leaq 0x39c5(%rip), %rsi      # 0xc420
movl $0x2, %edx
movq %rax, %rdi
callq 0x21a0
movl 0x38(%rbx), %eax
cmpl $0x3, %eax
jne 0x8b9c
movq 0x30(%rbx), %r15
movq %r14, %rdi
movq %r15, %rsi
callq 0x7364
movsd 0x20(%r15), %xmm0
movsd 0xb0(%r14), %xmm1
ucomisd %xmm1, %xmm0
jbe 0x8cdd
movq 0xd0(%r14), %r13
testq %r13, %r13
je 0x8cdd
movq %r14, 0x20(%rsp)
movq %rbx, 0x8(%rsp)
movl $0x38, %edi
callq 0x2160
movq %rax, %r15
movsd 0x20(%r13), %xmm0
movsd %xmm0, 0x18(%rsp)
movl $0x28, %edi
callq 0x2160
movq %rax, %r12
movq 0x28(%r13), %rbx
leaq 0x86a1(%rip), %rcx      # 0x11188
movq (%rcx), %rbp
movq %rbp, (%rax)
leaq 0x1(%rbp), %rdx
movq %rdx, (%rcx)
xorpd %xmm0, %xmm0
movupd %xmm0, 0x8(%rax)
movq $0x0, 0x18(%rax)
leaq 0x8694(%rip), %rax      # 0x111a0
movq (%rax), %r14
testq %r14, %r14
je 0x8f15
movq %r14, 0x8(%r12)
movupd %xmm0, 0x10(%r12)
movq $0x0, 0x20(%r12)
testq %rbx, %rbx
je 0x8f28
cmpq %r14, 0x18(%rbx)
jne 0x8f28
movq %rdx, 0x10(%rsp)
movq %rbx, 0x10(%r12)
movq 0x30(%rbx), %rax
movq %rax, 0x20(%r12)
movl $0x18, %edi
callq 0x2160
movq %r12, 0x10(%rax)
movq 0x40(%rbx), %rcx
movq 0x8(%rcx), %rdx
testq %rdx, %rdx
je 0x8b7b
movq %rax, (%rdx)
leaq 0x8628(%rip), %rsi      # 0x111a0
movq (%rsi), %r14
movq %rdx, 0x8(%rax)
movq %rcx, (%rax)
movq %rax, 0x8(%rcx)
movq 0x8(%rsp), %rbx
leaq 0x85f6(%rip), %rcx      # 0x11188
movq 0x10(%rsp), %rdx
jmp 0x8f2d
movsd 0x20(%rbx), %xmm0
cmpb $0x1, 0xb8(%r14)
jne 0x8dfb
movsd 0xb0(%r14), %xmm1
cmplepd %xmm0, %xmm1
movsd 0x37fb(%rip), %xmm2     # 0xc3c0
cmpeqpd %xmm0, %xmm2
orpd %xmm1, %xmm2
movd %xmm2, %ecx
testb $0x1, %cl
je 0x8dfb
cmpl $0x2, %eax
ja 0x8c8f
movq 0x83e5(%rip), %r15      # 0x10fd0
leaq 0x386b(%rip), %rsi      # 0xc45d
movl $0x1, %edx
movq %r15, %rdi
callq 0x21a0
movq (%rbx), %rsi
movq %r15, %rdi
callq 0x20e0
movq %rax, %r15
leaq 0x3822(%rip), %rsi      # 0xc436
movl $0x2, %edx
movq %rax, %rdi
callq 0x21a0
movsd 0xb0(%r14), %xmm0
movq %r15, %rdi
callq 0x2230
movq %rax, %r15
leaq 0x380d(%rip), %rsi      # 0xc449
movl $0x15, %edx
movq %rax, %rdi
callq 0x21a0
movsd 0x20(%rbx), %xmm0
movq %r15, %rdi
callq 0x2230
movq %rax, %r15
leaq 0x37ff(%rip), %rsi      # 0xc45f
movl $0x8, %edx
movq %rax, %rdi
callq 0x21a0
movl 0x4c(%rbx), %esi
movq %r15, %rdi
callq 0x2270
leaq 0x37e9(%rip), %rsi      # 0xc468
movl $0x8, %edx
movq %rax, %rdi
callq 0x21a0
movl 0x38(%rbx), %eax
cmpl $0x5, %eax
ja 0x93d3
movl %eax, %eax
leaq 0x374f(%rip), %rcx      # 0xc3f0
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq 0x28(%rbx), %rsi
leaq 0x28(%rsi), %r15
leaq 0x68(%r14), %rdi
addq $0x20, %rsi
callq 0xb9c6
movq %rax, %rdi
movq %r15, %rsi
callq 0x20c0
movq 0xa0(%r14), %r15
leaq 0x3799(%rip), %rsi      # 0xc471
jmp 0x8fa6
ucomisd %xmm0, %xmm1
jb 0x93d3
movq 0xc8(%r14), %rbp
testq %rbp, %rbp
je 0x93d3
cmpq $0x0, 0xd0(%r14)
jne 0x8d0f
cmpb $0x1, 0xb8(%r14)
jne 0x93d3
movq %r14, 0x20(%rsp)
movq %rbx, 0x8(%rsp)
movl $0x38, %edi
callq 0x2160
movq %rax, %r15
movsd 0x20(%rbp), %xmm0
movsd %xmm0, 0x18(%rsp)
movl $0x28, %edi
callq 0x2160
movq %rax, %r12
movq 0x28(%rbp), %rbx
leaq 0x843f(%rip), %rax      # 0x11188
movq (%rax), %r13
movq %r13, (%r12)
leaq 0x1(%r13), %rcx
movq %rcx, (%rax)
xorpd %xmm0, %xmm0
movupd %xmm0, 0x8(%r12)
movq $0x0, 0x18(%r12)
leaq 0x842e(%rip), %rax      # 0x111a0
movq (%rax), %r14
testq %r14, %r14
je 0x92d0
movq %r14, 0x8(%r12)
movupd %xmm0, 0x10(%r12)
movq $0x0, 0x20(%r12)
testq %rbx, %rbx
je 0x92e3
cmpq %r14, 0x18(%rbx)
jne 0x92e3
movq %rcx, 0x10(%rsp)
movq %rbx, 0x10(%r12)
movq 0x30(%rbx), %rax
movq %rax, 0x20(%r12)
movl $0x18, %edi
callq 0x2160
movq %r12, 0x10(%rax)
movq 0x40(%rbx), %rcx
movq 0x8(%rcx), %rdx
testq %rdx, %rdx
je 0x8de1
movq %rax, (%rdx)
leaq 0x83c2(%rip), %rsi      # 0x111a0
movq (%rsi), %r14
movq %rdx, 0x8(%rax)
movq %rcx, (%rax)
movq %rax, 0x8(%rcx)
movq 0x8(%rsp), %rbx
movq 0x10(%rsp), %rcx
jmp 0x92e8
movq 0xc8(%r14), %rax
testq %rax, %rax
je 0x8e76
cmpb $0x1, 0x60(%r14)
jne 0x8eb6
movq 0x48(%rbx), %xmm0
pcmpeqd %xmm1, %xmm1
psubd %xmm1, %xmm0
movq %xmm0, 0x48(%rbx)
movq 0x28(%rax), %r15
movl $0x38, %edi
callq 0x2160
leaq 0x836f(%rip), %rcx      # 0x111a8
movq (%rcx), %rdx
movq %rdx, (%rax)
incq %rdx
movq %rdx, (%rcx)
pxor %xmm0, %xmm0
movdqu %xmm0, 0x8(%rax)
leaq -0x493(%rip), %rcx      # 0x89c2
movq %rcx, 0x18(%rax)
xorl %ecx, %ecx
movq %rcx, 0x20(%rax)
movq %rbx, 0x28(%rax)
movq %rcx, 0x30(%rax)
movq (%r15), %rcx
movq 0x8(%rcx), %rcx
movq %r15, %rdi
jmp 0x8eff
movsd 0x3542(%rip), %xmm1     # 0xc3c0
ucomisd %xmm0, %xmm1
jbe 0x93e0
movabsq $0x3ff0000000000000, %rax # imm = 0x3FF0000000000000
movq %rax, 0x20(%rbx)
movl $0x0, 0x48(%rbx)
movq %r14, %rdi
movq %rbx, %rsi
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x8774
movl $0x38, %edi
callq 0x2160
leaq 0x82e1(%rip), %rcx      # 0x111a8
movq (%rcx), %rdx
movq %rdx, (%rax)
incq %rdx
movq %rdx, (%rcx)
xorpd %xmm0, %xmm0
movupd %xmm0, 0x8(%rax)
leaq -0x521(%rip), %rcx      # 0x89c2
movq %rcx, 0x18(%rax)
xorl %ecx, %ecx
movq %rcx, 0x20(%rax)
movq %rbx, 0x28(%rax)
movq %rcx, 0x30(%rax)
movq (%r14), %rcx
movq 0x48(%rcx), %rcx
movq %r14, %rdi
movq %r14, %rsi
movq %rax, %rdx
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmpq *%rcx
movupd %xmm0, 0x10(%r12)
movq $0x0, 0x20(%r12)
xorl %r14d, %r14d
movq 0x8(%rsp), %rbx
movl 0x30(%r13), %eax
movq %rdx, (%r15)
addq $0x2, %rbp
movq %rbp, (%rcx)
xorpd %xmm0, %xmm0
movupd %xmm0, 0x8(%r15)
movq $0x0, 0x18(%r15)
testq %r14, %r14
je 0x8f5c
movq %r15, %rcx
addq $0x8, %rcx
movq %r14, (%rcx)
movsd 0x18(%rsp), %xmm0
movsd %xmm0, 0x20(%r15)
movq %r12, 0x28(%r15)
movl %eax, 0x30(%r15)
movq 0x20(%rsp), %rdi
movq %r15, %rsi
callq 0x9562
jmp 0x93d3
movq 0x30(%rbx), %rsi
movq %r14, %rdi
jmp 0x8f78
leaq 0x68(%r14), %rdi
leaq 0x50(%rbx), %rsi
callq 0xbc98
movq 0xa0(%r14), %r15
leaq 0x34d3(%rip), %rsi      # 0xc479
movl $0x7, %edx
movq %r15, %rdi
callq 0x21a0
movl 0x4c(%rbx), %esi
movq %r15, %rdi
callq 0x2270
movq %rax, %r12
leaq 0x34b7(%rip), %r15      # 0xc47f
movl $0x1, %edx
movq %rax, %rdi
movq %r15, %rsi
callq 0x21a0
movsd 0x58(%rbx), %xmm0
subsd 0xb0(%r14), %xmm0
andpd 0x33f2(%rip), %xmm0     # 0xc3e0
movq %r12, %rdi
callq 0x2230
movq %rax, %r12
movl $0x1, %edx
movq %rax, %rdi
movq %r15, %rsi
callq 0x21a0
movsd 0x58(%rbx), %xmm0
movq %r12, %rdi
callq 0x2230
movq %rax, %r12
movl $0x1, %edx
movq %rax, %rdi
movq %r15, %rsi
callq 0x21a0
movsd 0xb0(%r14), %xmm0
movq %r12, %rdi
callq 0x2230
movq %rax, %r12
movl $0x1, %edx
movq %rax, %rdi
movq %r15, %rsi
callq 0x21a0
movsd 0x40(%rbx), %xmm0
movq %r12, %rdi
callq 0x2230
movq %rax, %r12
movl $0x1, %edx
movq %rax, %rdi
movq %r15, %rsi
callq 0x21a0
leaq 0x33fb(%rip), %rsi      # 0xc46f
movl $0x1, %edx
movq %r12, %rdi
callq 0x21a0
movq 0xa0(%r14), %rdi
callq 0x20f0
jmp 0x93d3
leaq 0x68(%r14), %rdi
leaq 0x50(%rbx), %r13
movq %r13, %rsi
callq 0xb9c6
leaq 0x38(%rsp), %rcx
movq %rcx, -0x10(%rcx)
movq (%rax), %rsi
movq 0x8(%rax), %rdx
addq %rsi, %rdx
leaq 0x28(%rsp), %rdi
callq 0xb930
movl $0x48, %edi
callq 0x2160
movq %rax, %r15
movq 0x30(%rbx), %rsi
addq $0x28, %rsi
movq %rax, %rdi
callq 0x3dfc
movl $0x48, %edi
callq 0x2160
movq %rax, %r12
movl (%r13), %ebp
leaq 0x58(%rsp), %r13
movq %r13, -0x10(%r13)
movq 0x28(%rsp), %rsi
movq 0x30(%rsp), %rdx
addq %rsi, %rdx
leaq 0x48(%rsp), %rdi
callq 0xb930
leaq 0x48(%rsp), %rdx
movq %r12, %rdi
movl %ebp, %esi
callq 0x3f6a
movq 0x48(%rsp), %rdi
cmpq %r13, %rdi
je 0x9133
movq 0x58(%rsp), %rsi
incq %rsi
callq 0x2170
movq 0xa0(%r14), %r13
leaq 0x3340(%rip), %rsi      # 0xc481
movl $0x7, %edx
movq %r13, %rdi
callq 0x21a0
movl 0x4c(%rbx), %esi
movq %r13, %rdi
callq 0x2270
movq %rax, %r13
leaq 0x331c(%rip), %rsi      # 0xc47f
movl $0x1, %edx
movq %rax, %rdi
callq 0x21a0
movsd 0x58(%rbx), %xmm0
subsd 0xb0(%r14), %xmm0
andpd 0x325a(%rip), %xmm0     # 0xc3e0
movq %r13, %rdi
callq 0x2230
movq %rax, %r13
leaq 0x32e7(%rip), %rsi      # 0xc47f
movl $0x1, %edx
movq %rax, %rdi
callq 0x21a0
movsd 0x58(%rbx), %xmm0
movq %r13, %rdi
callq 0x2230
movq %rax, %r13
leaq 0x32c3(%rip), %rsi      # 0xc47f
movl $0x1, %edx
movq %rax, %rdi
callq 0x21a0
movsd 0xb0(%r14), %xmm0
movq %r13, %rdi
callq 0x2230
movq %rax, %r13
leaq 0x329b(%rip), %rsi      # 0xc47f
movl $0x1, %edx
movq %rax, %rdi
callq 0x21a0
movsd 0x40(%rbx), %xmm0
movq %r13, %rdi
callq 0x2230
movq %rax, %r13
leaq 0x3277(%rip), %rsi      # 0xc47f
movl $0x1, %edx
movq %rax, %rdi
callq 0x21a0
leaq 0x3253(%rip), %rsi      # 0xc46f
movl $0x1, %edx
movq %r13, %rdi
callq 0x21a0
movq 0xa0(%r14), %rdi
callq 0x20f0
movl $0x38, %edi
callq 0x2160
leaq 0x7f62(%rip), %rcx      # 0x111a8
movq (%rcx), %rdx
movq %rdx, (%rax)
incq %rdx
movq %rdx, (%rcx)
xorpd %xmm0, %xmm0
movupd %xmm0, 0x8(%rax)
leaq 0x214(%rip), %rcx       # 0x9476
movq %rcx, 0x18(%rax)
xorl %ecx, %ecx
movq %rcx, 0x20(%rax)
movq %r12, 0x28(%rax)
movq %rcx, 0x30(%rax)
movq (%r15), %rcx
movq %r15, %rdi
movq %r14, %rsi
movq %rax, %rdx
callq *0x8(%rcx)
movq 0x28(%rsp), %rdi
leaq 0x38(%rsp), %rax
cmpq %rax, %rdi
je 0x93d3
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x2170
jmp 0x93d3
movq 0x28(%rbx), %rsi
leaq 0x28(%rsi), %r15
addq $0x68, %r14
addq $0x20, %rsi
movq %r14, %rdi
callq 0xb9c6
movq %rax, %rdi
movq %r15, %rsi
callq 0x20c0
jmp 0x93d3
movupd %xmm0, 0x10(%r12)
movq $0x0, 0x20(%r12)
xorl %r14d, %r14d
movq 0x8(%rsp), %rbx
movl 0x30(%rbp), %eax
movq %rcx, (%r15)
addq $0x2, %r13
leaq 0x7e8f(%rip), %rcx      # 0x11188
movq %r13, (%rcx)
movq %rcx, %r13
xorpd %xmm0, %xmm0
movupd %xmm0, 0x8(%r15)
movq $0x0, 0x18(%r15)
testq %r14, %r14
je 0x9320
movq %r15, %rcx
addq $0x8, %rcx
movq %r14, (%rcx)
movsd 0x18(%rsp), %xmm0
movsd %xmm0, 0x20(%r15)
movq %r12, 0x28(%r15)
movl %eax, 0x30(%r15)
movl $0x60, %edi
callq 0x2160
movq %rax, %r12
movq 0x20(%rsp), %r14
movq %r14, %rdi
callq 0x7a80
movq (%r13), %rax
movq %rax, (%r12)
incq %rax
movq %rax, (%r13)
xorpd %xmm1, %xmm1
movupd %xmm1, 0x8(%r12)
movq $0x0, 0x18(%r12)
leaq 0x7e28(%rip), %rax      # 0x111a0
movq (%rax), %rax
testq %rax, %rax
je 0x938a
movq %r12, %rcx
addq $0x8, %rcx
movq %rax, (%rcx)
movabsq $0x3ff0000000000000, %rax # imm = 0x3FF0000000000000
movq %rax, 0x20(%r12)
xorl %eax, %eax
movq %rax, 0x28(%r12)
movq %r15, 0x30(%r12)
movl $0x4, 0x38(%r12)
movsd %xmm0, 0x40(%r12)
movq %rax, 0x48(%r12)
movl $0x0, 0x50(%r12)
movq %rax, 0x58(%r12)
movq %r14, %rdi
movq %r12, %rsi
callq 0x8774
movl $0x60, %esi
movq %rbx, %rdi
callq 0x2170
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x93f1
movq %rax, %rbx
movl $0x28, %esi
movq %r12, %rdi
callq 0x2170
jmp 0x9445
movq %rax, %rbx
movq 0x48(%rsp), %rdi
cmpq %r13, %rdi
je 0x9422
movq 0x58(%rsp), %rsi
incq %rsi
callq 0x2170
jmp 0x9422
movq %rax, %rbx
movl $0x48, %esi
movq %r12, %rdi
jmp 0x9437
movq %rax, %rbx
movl $0x48, %esi
movq %r15, %rdi
callq 0x2170
jmp 0x9452
jmp 0x944f
jmp 0x9442
movq %rax, %rbx
movl $0x38, %esi
movq %r15, %rdi
jmp 0x9469
movq %rax, %rbx
movq 0x28(%rsp), %rdi
leaq 0x38(%rsp), %rax
cmpq %rax, %rdi
je 0x946e
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x2170
movq %rbx, %rdi
callq 0x2280