// File: /workspace/github_cpp_makefile_star2_O1/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 $0x58, %rsp
movq %rsi, %rbx
movq %rdi, %r14
cmpl $0x2, 0x38(%rsi)
ja 0x9a32
movq 0x861d(%rip), %r15      # 0x11fd0
leaq 0x3aa3(%rip), %rsi      # 0xd45d
movl $0x1, %edx
movq %r15, %rdi
callq 0x31a0
movq (%rbx), %rsi
movq %r15, %rdi
callq 0x30e0
movq %rax, %r15
leaq 0x3a5a(%rip), %rsi      # 0xd436
movl $0x2, %edx
movq %rax, %rdi
callq 0x31a0
movsd 0xb0(%r14), %xmm0
movq %r15, %rdi
callq 0x3230
movq %rax, %r15
leaq 0x3a35(%rip), %rsi      # 0xd439
movl $0xf, %edx
movq %rax, %rdi
callq 0x31a0
movsd 0x20(%rbx), %xmm0
movq %r15, %rdi
callq 0x3230
leaq 0x39fb(%rip), %rsi      # 0xd420
movl $0x2, %edx
movq %rax, %rdi
callq 0x31a0
movl 0x38(%rbx), %eax
cmpl $0x3, %eax
je 0x9a79
movsd 0x20(%rbx), %xmm0
cmpb $0x1, 0xb8(%r14)
jne 0x9b7a
movsd 0xb0(%r14), %xmm1
cmplepd %xmm0, %xmm1
movsd 0x395d(%rip), %xmm2     # 0xd3c0
cmpeqpd %xmm0, %xmm2
orpd %xmm1, %xmm2
movd %xmm2, %ecx
testb $0x1, %cl
je 0x9b7a
cmpl $0x2, %eax
ja 0x9b2a
movq 0x8547(%rip), %r15      # 0x11fd0
leaq 0x39cd(%rip), %rsi      # 0xd45d
movl $0x1, %edx
movq %r15, %rdi
callq 0x31a0
movq (%rbx), %rsi
movq %r15, %rdi
callq 0x30e0
movq %rax, %r15
leaq 0x3984(%rip), %rsi      # 0xd436
movl $0x2, %edx
movq %rax, %rdi
callq 0x31a0
movsd 0xb0(%r14), %xmm0
movq %r15, %rdi
callq 0x3230
movq %rax, %r15
leaq 0x396f(%rip), %rsi      # 0xd449
movl $0x15, %edx
movq %rax, %rdi
callq 0x31a0
movsd 0x20(%rbx), %xmm0
movq %r15, %rdi
callq 0x3230
movq %rax, %r15
leaq 0x3961(%rip), %rsi      # 0xd45f
movl $0x8, %edx
movq %rax, %rdi
callq 0x31a0
movl 0x4c(%rbx), %esi
movq %r15, %rdi
callq 0x3270
leaq 0x394b(%rip), %rsi      # 0xd468
movl $0x8, %edx
movq %rax, %rdi
callq 0x31a0
movl 0x38(%rbx), %eax
cmpq $0x5, %rax
ja 0xa34f
leaq 0x38b2(%rip), %rcx      # 0xd3f0
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 0xc98a
movq %rax, %rdi
movq %r15, %rsi
callq 0x30c0
movq 0xa0(%r14), %r15
leaq 0x38fc(%rip), %rsi      # 0xd471
jmp 0x9fa1
movq 0xc8(%r14), %rax
testq %rax, %rax
je 0xa0b5
cmpb $0x1, 0x60(%r14)
jne 0xa0f5
incl 0x48(%rbx)
incl 0x4c(%rbx)
movq 0x28(%rax), %r15
movl $0x38, %edi
callq 0x3160
leaq 0x85f8(%rip), %rcx      # 0x121a8
movq (%rcx), %rdx
movq %rdx, (%rax)
incq %rdx
movq %rdx, (%rcx)
xorpd %xmm0, %xmm0
movupd %xmm0, 0x8(%rax)
leaq -0x23e(%rip), %rcx      # 0x998e
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 0xa13e
movq 0x30(%rbx), %rsi
movq %r14, %rdi
jmp 0x9f7c
leaq 0x68(%r14), %rdi
leaq 0x50(%rbx), %r13
movq %r13, %rsi
callq 0xc98a
leaq 0x28(%rsp), %rcx
movq %rcx, -0x10(%rcx)
movq (%rax), %rsi
movq 0x8(%rax), %rdx
addq %rsi, %rdx
leaq 0x18(%rsp), %rdi
callq 0xc8fa
movl $0x48, %edi
callq 0x3160
movq %rax, %r15
movq 0x30(%rbx), %rsi
addq $0x28, %rsi
movq %rax, %rdi
callq 0x4e3c
movl $0x48, %edi
callq 0x3160
movq %rax, %r12
movl (%r13), %ebp
leaq 0x48(%rsp), %r13
movq %r13, -0x10(%r13)
movq 0x18(%rsp), %rsi
movq 0x20(%rsp), %rdx
addq %rsi, %rdx
leaq 0x38(%rsp), %rdi
callq 0xc8fa
leaq 0x38(%rsp), %rdx
movq %r12, %rdi
movl %ebp, %esi
callq 0x4faa
movq 0x38(%rsp), %rdi
cmpq %r13, %rdi
je 0x9c9a
movq 0x48(%rsp), %rsi
incq %rsi
callq 0x3170
movq 0xa0(%r14), %r13
leaq 0x37d9(%rip), %rsi      # 0xd481
movl $0x7, %edx
movq %r13, %rdi
callq 0x31a0
movl 0x4c(%rbx), %esi
movq %r13, %rdi
callq 0x3270
movq %rax, %r13
leaq 0x37b5(%rip), %rsi      # 0xd47f
movl $0x1, %edx
movq %rax, %rdi
callq 0x31a0
movsd 0x58(%rbx), %xmm0
subsd 0xb0(%r14), %xmm0
andpd 0x36f3(%rip), %xmm0     # 0xd3e0
movq %r13, %rdi
callq 0x3230
movq %rax, %r13
leaq 0x3780(%rip), %rsi      # 0xd47f
movl $0x1, %edx
movq %rax, %rdi
callq 0x31a0
movsd 0x58(%rbx), %xmm0
movq %r13, %rdi
callq 0x3230
movq %rax, %r13
leaq 0x375c(%rip), %rsi      # 0xd47f
movl $0x1, %edx
movq %rax, %rdi
callq 0x31a0
movsd 0xb0(%r14), %xmm0
movq %r13, %rdi
callq 0x3230
movq %rax, %r13
leaq 0x3734(%rip), %rsi      # 0xd47f
movl $0x1, %edx
movq %rax, %rdi
callq 0x31a0
movsd 0x40(%rbx), %xmm0
movq %r13, %rdi
callq 0x3230
movq %rax, %r13
leaq 0x3710(%rip), %rsi      # 0xd47f
movl $0x1, %edx
movq %rax, %rdi
callq 0x31a0
leaq 0x36ec(%rip), %rsi      # 0xd46f
movl $0x1, %edx
movq %r13, %rdi
callq 0x31a0
movq 0xa0(%r14), %rdi
callq 0x30f0
movl $0x38, %edi
callq 0x3160
leaq 0x83fb(%rip), %rcx      # 0x121a8
movq (%rcx), %rdx
movq %rdx, (%rax)
incq %rdx
movq %rdx, (%rcx)
xorpd %xmm0, %xmm0
movupd %xmm0, 0x8(%rax)
leaq 0x62f(%rip), %rcx       # 0xa3f8
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 0x18(%rsp), %rdi
leaq 0x28(%rsp), %rax
cmpq %rax, %rdi
je 0xa34f
movq 0x28(%rsp), %rsi
incq %rsi
callq 0x3170
jmp 0xa34f
movq 0x30(%rbx), %r15
movq %r14, %rdi
movq %r15, %rsi
callq 0x8410
movsd 0x20(%r15), %xmm0
movsd 0xb0(%r14), %xmm1
ucomisd %xmm1, %xmm0
jbe 0xa154
movq 0xd0(%r14), %r13
testq %r13, %r13
je 0xa154
movl $0x38, %edi
callq 0x3160
movq %rax, %r15
movsd 0x20(%r13), %xmm0
movsd %xmm0, 0x10(%rsp)
movl $0x28, %edi
callq 0x3160
movq %rax, %r12
movq 0x28(%r13), %r9
leaq 0x8310(%rip), %rsi      # 0x12188
movq (%rsi), %rbp
movq %rbp, (%rax)
leaq 0x1(%rbp), %r8
movq %r8, (%rsi)
xorpd %xmm0, %xmm0
movupd %xmm0, 0x8(%rax)
movq $0x0, 0x18(%rax)
leaq 0x8303(%rip), %rdi      # 0x121a0
movq (%rdi), %rax
testq %rax, %rax
je 0x9eaf
movq %r12, %rcx
addq $0x8, %rcx
movq %rax, (%rcx)
testq %rax, %rax
sete %cl
testq %r9, %r9
sete %dl
orb %cl, %dl
movupd %xmm0, 0x10(%r12)
movq $0x0, 0x20(%r12)
jne 0x9f34
cmpq %rax, 0x18(%r9)
jne 0x9f34
movq %r8, 0x8(%rsp)
movq %r9, 0x10(%r12)
movq 0x30(%r9), %rax
movq %rax, 0x20(%r12)
movl $0x18, %edi
movq %r9, (%rsp)
callq 0x3160
xorpd %xmm0, %xmm0
movupd %xmm0, (%rax)
movq %r12, 0x10(%rax)
movq (%rsp), %rcx
movq 0x40(%rcx), %rcx
movq 0x8(%rcx), %rdx
testq %rdx, %rdx
je 0x9f16
movq %rax, (%rdx)
movq %rdx, 0x8(%rax)
movq %rcx, (%rax)
movq %rax, 0x8(%rcx)
leaq 0x8260(%rip), %rsi      # 0x12188
leaq 0x8271(%rip), %rdi      # 0x121a0
movq 0x8(%rsp), %r8
movl 0x30(%r13), %eax
movq %r8, (%r15)
addq $0x2, %rbp
movq %rbp, (%rsi)
movupd %xmm0, 0x8(%r15)
movq $0x0, 0x18(%r15)
movq (%rdi), %rcx
testq %rcx, %rcx
je 0x9f62
movq %r15, %rdx
addq $0x8, %rdx
movq %rcx, (%rdx)
movsd 0x10(%rsp), %xmm0
movsd %xmm0, 0x20(%r15)
movq %r12, 0x28(%r15)
movl %eax, 0x30(%r15)
movq %r14, %rdi
movq %r15, %rsi
callq 0xa4f4
jmp 0xa34f
leaq 0x68(%r14), %rdi
leaq 0x50(%rbx), %rsi
callq 0xcc8e
movq 0xa0(%r14), %r15
leaq 0x34d8(%rip), %rsi      # 0xd479
movl $0x7, %edx
movq %r15, %rdi
callq 0x31a0
movl 0x4c(%rbx), %esi
movq %r15, %rdi
callq 0x3270
movq %rax, %r12
leaq 0x34bc(%rip), %r15      # 0xd47f
movl $0x1, %edx
movq %rax, %rdi
movq %r15, %rsi
callq 0x31a0
movsd 0x58(%rbx), %xmm0
subsd 0xb0(%r14), %xmm0
andpd 0x33f7(%rip), %xmm0     # 0xd3e0
movq %r12, %rdi
callq 0x3230
movq %rax, %r12
movl $0x1, %edx
movq %rax, %rdi
movq %r15, %rsi
callq 0x31a0
movsd 0x58(%rbx), %xmm0
movq %r12, %rdi
callq 0x3230
movq %rax, %r12
movl $0x1, %edx
movq %rax, %rdi
movq %r15, %rsi
callq 0x31a0
movsd 0xb0(%r14), %xmm0
movq %r12, %rdi
callq 0x3230
movq %rax, %r12
movl $0x1, %edx
movq %rax, %rdi
movq %r15, %rsi
callq 0x31a0
movsd 0x40(%rbx), %xmm0
movq %r12, %rdi
callq 0x3230
movq %rax, %r12
movl $0x1, %edx
movq %rax, %rdi
movq %r15, %rsi
callq 0x31a0
leaq 0x3400(%rip), %rsi      # 0xd46f
movl $0x1, %edx
movq %r12, %rdi
callq 0x31a0
movq 0xa0(%r14), %rdi
callq 0x30f0
jmp 0xa34f
movq 0x28(%rbx), %rsi
leaq 0x28(%rsi), %r15
addq $0x68, %r14
addq $0x20, %rsi
movq %r14, %rdi
callq 0xc98a
movq %rax, %rdi
movq %r15, %rsi
callq 0x30c0
jmp 0xa34f
movsd 0x3303(%rip), %xmm1     # 0xd3c0
ucomisd %xmm0, %xmm1
jbe 0xa361
movabsq $0x3ff0000000000000, %rax # imm = 0x3FF0000000000000
movq %rax, 0x20(%rbx)
movl $0x0, 0x48(%rbx)
movq %r14, %rdi
movq %rbx, %rsi
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x972a
movl $0x38, %edi
callq 0x3160
leaq 0x80a2(%rip), %rcx      # 0x121a8
movq (%rcx), %rdx
movq %rdx, (%rax)
incq %rdx
movq %rdx, (%rcx)
xorpd %xmm0, %xmm0
movupd %xmm0, 0x8(%rax)
leaq -0x794(%rip), %rcx      # 0x998e
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 $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmpq *%rcx
ucomisd %xmm0, %xmm1
jb 0xa34f
movq 0xc8(%r14), %rbp
testq %rbp, %rbp
je 0xa34f
cmpq $0x0, 0xd0(%r14)
jne 0xa186
cmpb $0x1, 0xb8(%r14)
jne 0xa34f
movl $0x38, %edi
callq 0x3160
movq %rax, %r15
movsd 0x20(%rbp), %xmm0
movsd %xmm0, 0x10(%rsp)
movl $0x28, %edi
callq 0x3160
movq %rax, %r12
movq 0x28(%rbp), %rdi
leaq 0x7fd2(%rip), %rax      # 0x12188
movq (%rax), %r13
movq %r13, (%r12)
leaq 0x1(%r13), %rsi
movq %rsi, (%rax)
xorpd %xmm0, %xmm0
movupd %xmm0, 0x8(%r12)
movq $0x0, 0x18(%r12)
leaq 0x7fc1(%rip), %rax      # 0x121a0
movq (%rax), %rax
testq %rax, %rax
je 0xa1f1
movq %r12, %rcx
addq $0x8, %rcx
movq %rax, (%rcx)
testq %rax, %rax
sete %cl
testq %rdi, %rdi
sete %dl
orb %cl, %dl
movupd %xmm0, 0x10(%r12)
movq $0x0, 0x20(%r12)
jne 0xa268
cmpq %rax, 0x18(%rdi)
jne 0xa268
movq %rsi, 0x8(%rsp)
movq %rdi, 0x10(%r12)
movq 0x30(%rdi), %rax
movq %rax, 0x20(%r12)
movq %rdi, (%rsp)
movl $0x18, %edi
callq 0x3160
xorpd %xmm0, %xmm0
movupd %xmm0, (%rax)
movq %r12, 0x10(%rax)
movq (%rsp), %rcx
movq 0x40(%rcx), %rcx
movq 0x8(%rcx), %rdx
testq %rdx, %rdx
je 0xa258
movq %rax, (%rdx)
movq %rdx, 0x8(%rax)
movq %rcx, (%rax)
movq %rax, 0x8(%rcx)
movq 0x8(%rsp), %rsi
movl 0x30(%rbp), %eax
movq %rsi, (%r15)
addq $0x2, %r13
leaq 0x7f0f(%rip), %rcx      # 0x12188
movq %r13, (%rcx)
movq %rcx, %r13
movupd %xmm0, 0x8(%r15)
movq $0x0, 0x18(%r15)
leaq 0x7f0c(%rip), %rbp      # 0x121a0
movq (%rbp), %rcx
testq %rcx, %rcx
je 0xa2a7
movq %r15, %rdx
addq $0x8, %rdx
movq %rcx, (%rdx)
movsd 0x10(%rsp), %xmm0
movsd %xmm0, 0x20(%r15)
movq %r12, 0x28(%r15)
movl %eax, 0x30(%r15)
movl $0x60, %edi
callq 0x3160
movq %rax, %r12
movq %r14, %rdi
callq 0x8b0a
movq (%r13), %rax
movq %rax, (%r12)
incq %rax
movq %rax, (%r13)
xorpd %xmm1, %xmm1
movupd %xmm1, 0x8(%r12)
movq $0x0, 0x18(%r12)
movq (%rbp), %rax
testq %rax, %rax
je 0xa306
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 0x972a
testq %rbx, %rbx
je 0xa361
movl $0x60, %esi
movq %rbx, %rdi
callq 0x3170
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0xa372
movq %rax, %rbx
movl $0x28, %esi
movq %r12, %rdi
callq 0x3170
jmp 0xa389
jmp 0xa386
movq %rax, %rbx
movl $0x38, %esi
movq %r15, %rdi
jmp 0xa3ea
movq %rax, %rbx
movq 0x38(%rsp), %rdi
cmpq %r13, %rdi
je 0xa3b2
movq 0x48(%rsp), %rsi
incq %rsi
callq 0x3170
jmp 0xa3b2
movq %rax, %rbx
movl $0x48, %esi
movq %r12, %rdi
jmp 0xa3c7
movq %rax, %rbx
movl $0x48, %esi
movq %r15, %rdi
callq 0x3170
jmp 0xa3d3
jmp 0xa3d0
movq %rax, %rbx
movq 0x18(%rsp), %rdi
leaq 0x28(%rsp), %rax
cmpq %rax, %rdi
je 0xa3ef
movq 0x28(%rsp), %rsi
incq %rsi
callq 0x3170
movq %rbx, %rdi
callq 0x3280
nop