// File: /workspace/github_cpp_makefile_star2_O2/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 $0x48, %rsp
movq %rsi, %rbx
movq %rdi, %r14
movl 0x38(%rsi), %eax
cmpl $0x2, %eax
ja 0x7488
movq 0x8bad(%rip), %rdi      # 0xffd0
leaq 0x3033(%rip), %rsi      # 0xa45d
callq 0x2160
movq (%rbx), %rsi
movq %rax, %rdi
callq 0x20e0
leaq 0x2ff5(%rip), %rsi      # 0xa436
movq %rax, %rdi
callq 0x2160
movsd 0xb0(%r14), %xmm0
movq %rax, %rdi
callq 0x2240
leaq 0x2fd8(%rip), %rsi      # 0xa439
movq %rax, %rdi
callq 0x2160
movsd 0x20(%rbx), %xmm0
movq %rax, %rdi
callq 0x2240
leaq 0x2fa3(%rip), %rsi      # 0xa420
movq %rax, %rdi
callq 0x2160
movl 0x38(%rbx), %eax
cmpl $0x3, %eax
jne 0x752b
movq 0x30(%rbx), %r15
movq %r14, %rdi
movq %r15, %rsi
callq 0x65f2
movsd 0x20(%r15), %xmm0
movsd 0xb0(%r14), %xmm1
ucomisd %xmm1, %xmm0
jbe 0x7647
movq 0xd0(%r14), %r13
testq %r13, %r13
je 0x7647
pushq $0x38
popq %rdi
callq 0x2170
movq %rax, %r15
movsd 0x20(%r13), %xmm0
movsd %xmm0, (%rsp)
pushq $0x28
popq %rdi
callq 0x2170
movq %rax, %r12
movq 0x28(%r13), %rsi
movq %rax, %rdi
callq 0x92c2
movq 0xd0(%r14), %rax
movl 0x30(%rax), %ebp
movq %r15, %rdi
callq 0x3a32
movsd (%rsp), %xmm0
movsd %xmm0, 0x20(%r15)
movq %r12, 0x28(%r15)
movl %ebp, 0x30(%r15)
movq %r14, %rdi
movq %r15, %rsi
callq 0x7bce
jmp 0x7a9b
movsd 0x20(%rbx), %xmm0
cmpb $0x1, 0xb8(%r14)
jne 0x7708
movsd 0xb0(%r14), %xmm1
cmplepd %xmm0, %xmm1
movsd 0x2e74(%rip), %xmm2     # 0xa3c8
cmpeqpd %xmm0, %xmm2
orpd %xmm1, %xmm2
movd %xmm2, %ecx
testb $0x1, %cl
je 0x7708
cmpl $0x2, %eax
ja 0x75f9
movq 0x8a56(%rip), %rdi      # 0xffd0
leaq 0x2edc(%rip), %rsi      # 0xa45d
callq 0x2160
movq (%rbx), %rsi
movq %rax, %rdi
callq 0x20e0
leaq 0x2e9e(%rip), %rsi      # 0xa436
movq %rax, %rdi
callq 0x2160
movsd 0xb0(%r14), %xmm0
movq %rax, %rdi
callq 0x2240
leaq 0x2e91(%rip), %rsi      # 0xa449
movq %rax, %rdi
callq 0x2160
movsd 0x20(%rbx), %xmm0
movq %rax, %rdi
callq 0x2240
leaq 0x2e8b(%rip), %rsi      # 0xa45f
movq %rax, %rdi
callq 0x2160
movl 0x4c(%rbx), %esi
movq %rax, %rdi
callq 0x2280
leaq 0x2e7a(%rip), %rsi      # 0xa468
movq %rax, %rdi
callq 0x2160
movl 0x38(%rbx), %eax
cmpl $0x5, %eax
ja 0x7a9b
movl %eax, %eax
leaq 0x2de5(%rip), %rcx      # 0xa3f0
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 0x9636
movq %rax, %rdi
movq %r15, %rsi
callq 0x20c0
movq 0xa0(%r14), %rdi
leaq 0x2e2f(%rip), %rsi      # 0xa471
jmp 0x782d
ucomisd %xmm0, %xmm1
jb 0x7a9b
movq 0xc8(%r14), %r13
testq %r13, %r13
je 0x7a9b
cmpq $0x0, 0xd0(%r14)
jne 0x7679
cmpb $0x1, 0xb8(%r14)
jne 0x7a9b
pushq $0x38
popq %rdi
callq 0x2170
movq %rax, %r15
movsd 0x20(%r13), %xmm0
movsd %xmm0, (%rsp)
pushq $0x28
popq %rdi
callq 0x2170
movq %rax, %r12
movq 0x28(%r13), %rsi
movq %rax, %rdi
callq 0x92c2
movq 0xc8(%r14), %rax
movl 0x30(%rax), %ebp
movq %r15, %rdi
callq 0x3a32
movsd (%rsp), %xmm0
movsd %xmm0, 0x20(%r15)
movq %r12, 0x28(%r15)
movl %ebp, 0x30(%r15)
pushq $0x60
popq %rdi
callq 0x2170
movq %rax, %r12
movq %r14, %rdi
callq 0x6a96
movapd %xmm0, %xmm1
movsd 0x2cde(%rip), %xmm0     # 0xa3c8
pushq $0x4
popq %rsi
movq %r12, %rdi
movq %r15, %rdx
callq 0x9472
movq %r14, %rdi
movq %r12, %rsi
callq 0x7228
jmp 0x7a9b
movq 0xc8(%r14), %rax
testq %rax, %rax
je 0x777e
cmpb $0x1, 0x60(%r14)
jne 0x77a9
movq 0x48(%rbx), %xmm0
pcmpeqd %xmm1, %xmm1
psubd %xmm1, %xmm0
movq %xmm0, 0x48(%rbx)
movq 0x28(%rax), %r15
pushq $0x38
popq %rdi
callq 0x2170
leaq 0x8a74(%rip), %rcx      # 0x101b8
movq (%rcx), %rdx
movq %rdx, (%rax)
incq %rdx
movq %rdx, (%rcx)
pxor %xmm0, %xmm0
movdqu %xmm0, 0x8(%rax)
leaq -0x360(%rip), %rcx      # 0x7400
movq %rcx, 0x18(%rax)
andq $0x0, 0x20(%rax)
movq %rbx, 0x28(%rax)
andq $0x0, 0x30(%rax)
movq (%r15), %rcx
movq 0x8(%rcx), %rcx
movq %r15, %rdi
jmp 0x77f0
movsd 0x2c42(%rip), %xmm1     # 0xa3c8
ucomisd %xmm0, %xmm1
jbe 0x7aa6
movq %r14, %rdi
movq %rbx, %rsi
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x7e0a
pushq $0x38
popq %rdi
callq 0x2170
leaq 0x8a00(%rip), %rcx      # 0x101b8
movq (%rcx), %rdx
movq %rdx, (%rax)
incq %rdx
movq %rdx, (%rcx)
xorpd %xmm0, %xmm0
movupd %xmm0, 0x8(%rax)
leaq -0x3d4(%rip), %rcx      # 0x7400
movq %rcx, 0x18(%rax)
andq $0x0, 0x20(%rax)
movq %rbx, 0x28(%rax)
andq $0x0, 0x30(%rax)
movq (%r14), %rcx
movq 0x48(%rcx), %rcx
movq %r14, %rdi
movq %r14, %rsi
movq %rax, %rdx
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmpq *%rcx
movq 0x30(%rbx), %rsi
movq %r14, %rdi
jmp 0x7521
leaq 0x68(%r14), %rdi
leaq 0x50(%rbx), %rsi
callq 0x9992
movq 0xa0(%r14), %rdi
leaq 0x2c4c(%rip), %rsi      # 0xa479
callq 0x2160
movl 0x4c(%rbx), %esi
movq %rax, %rdi
callq 0x2280
leaq 0x2c3b(%rip), %r15      # 0xa47f
movq %rax, %rdi
movq %r15, %rsi
callq 0x2160
movsd 0x58(%rbx), %xmm0
subsd 0xb0(%r14), %xmm0
andpd 0x2b7b(%rip), %xmm0     # 0xa3e0
movq %rax, %rdi
callq 0x2240
movq %rax, %rdi
movq %r15, %rsi
callq 0x2160
movsd 0x58(%rbx), %xmm0
movq %rax, %rdi
callq 0x2240
movq %rax, %rdi
movq %r15, %rsi
callq 0x2160
movsd 0xb0(%r14), %xmm0
movq %rax, %rdi
callq 0x2240
movq %rax, %rdi
movq %r15, %rsi
callq 0x2160
movsd 0x40(%rbx), %xmm0
movq %rax, %rdi
callq 0x2240
movq %rax, %rdi
movq %r15, %rsi
callq 0x2160
leaq 0x2ba4(%rip), %rsi      # 0xa46f
movq %rax, %rdi
callq 0x2160
movq 0xa0(%r14), %rdi
callq 0x20f0
jmp 0x7a9b
leaq 0x68(%r14), %rdi
leaq 0x50(%rbx), %r13
movq %r13, %rsi
callq 0x9636
leaq 0x28(%rsp), %rdi
movq %rax, %rsi
callq 0x21d0
pushq $0x48
popq %rdi
callq 0x2170
movq %rax, %r15
movq 0x30(%rbx), %rsi
addq $0x28, %rsi
movq %rax, %rdi
callq 0x37c0
pushq $0x48
popq %rdi
callq 0x2170
movq %rax, %r12
movl (%r13), %ebp
leaq 0x8(%rsp), %rdi
leaq 0x28(%rsp), %rsi
callq 0x21d0
leaq 0x8(%rsp), %rdx
movq %r12, %rdi
movl %ebp, %esi
callq 0x391e
leaq 0x8(%rsp), %rdi
callq 0x20a0
movq 0xa0(%r14), %rdi
leaq 0x2b20(%rip), %rsi      # 0xa481
callq 0x2160
movl 0x4c(%rbx), %esi
movq %rax, %rdi
callq 0x2280
leaq 0x2b07(%rip), %rsi      # 0xa47f
movq %rax, %rdi
callq 0x2160
movsd 0x58(%rbx), %xmm0
subsd 0xb0(%r14), %xmm0
andpd 0x2a4a(%rip), %xmm0     # 0xa3e0
movq %rax, %rdi
callq 0x2240
leaq 0x2ada(%rip), %rsi      # 0xa47f
movq %rax, %rdi
callq 0x2160
movsd 0x58(%rbx), %xmm0
movq %rax, %rdi
callq 0x2240
leaq 0x2abe(%rip), %rsi      # 0xa47f
movq %rax, %rdi
callq 0x2160
movsd 0xb0(%r14), %xmm0
movq %rax, %rdi
callq 0x2240
leaq 0x2a9e(%rip), %rsi      # 0xa47f
movq %rax, %rdi
callq 0x2160
movsd 0x40(%rbx), %xmm0
movq %rax, %rdi
callq 0x2240
leaq 0x2a82(%rip), %rsi      # 0xa47f
movq %rax, %rdi
callq 0x2160
leaq 0x2a63(%rip), %rsi      # 0xa46f
movq %rax, %rdi
callq 0x2160
movq 0xa0(%r14), %rdi
callq 0x20f0
pushq $0x38
popq %rdi
callq 0x2170
leaq 0x8789(%rip), %rcx      # 0x101b8
movq (%rcx), %rdx
movq %rdx, (%rax)
incq %rdx
movq %rdx, (%rcx)
xorpd %xmm0, %xmm0
movupd %xmm0, 0x8(%rax)
leaq 0xcf(%rip), %rcx        # 0x7b1a
movq %rcx, 0x18(%rax)
andq $0x0, 0x20(%rax)
movq %r12, 0x28(%rax)
andq $0x0, 0x30(%rax)
movq (%r15), %rcx
movq %r15, %rdi
movq %r14, %rsi
movq %rax, %rdx
callq *0x8(%rcx)
leaq 0x28(%rsp), %rdi
callq 0x20a0
jmp 0x7a9b
movq 0x28(%rbx), %rsi
leaq 0x28(%rsi), %r15
addq $0x68, %r14
addq $0x20, %rsi
movq %r14, %rdi
callq 0x9636
movq %rax, %rdi
movq %r15, %rsi
callq 0x20c0
pushq $0x60
popq %rsi
movq %rbx, %rdi
callq 0x2180
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq 0x8(%rsp), %rdi
callq 0x20a0
jmp 0x7ac7
movq %rax, %rbx
pushq $0x48
popq %rsi
movq %r12, %rdi
jmp 0x7ad8
movq %rax, %rbx
pushq $0x48
popq %rsi
movq %r15, %rdi
callq 0x2180
jmp 0x7b08
jmp 0x7b05
jmp 0x7ae5
jmp 0x7af5
movq %rax, %rbx
pushq $0x28
popq %rsi
movq %r12, %rdi
callq 0x2180
jmp 0x7af8
movq %rax, %rbx
pushq $0x38
popq %rsi
movq %r15, %rdi
callq 0x2180
jmp 0x7b12
movq %rax, %rbx
leaq 0x28(%rsp), %rdi
callq 0x20a0
movq %rbx, %rdi
callq 0x2290