// File: /workspace/github_cpp_makefile_star2_O3/LiGaCu[P]Distributed_System_to_Compute_the_Shortest_Path/serverC.cpp
// Function: main
int main(void)
{
    int selffd;
    struct addrinfo hints, *servinfo;
    int rv;

    char buf[MAXDATASIZE];int numbytes;
    std::string stringbuff;
    int srcVtxid, dstVtxid, filesize; char mapid;
    float Pspeed; int Tspeed;
    float Tt,Tp;
    int path[11]; float mindistan = inf_dis;

    memset(&hints, 0, sizeof hints);
    hints.ai_family = AF_UNSPEC; // set to AF_INET to force IPv4
    hints.ai_socktype = SOCK_DGRAM;
    if ((rv = getaddrinfo("127.0.0.1", CUDPPORT, &hints, &servinfo)) != 0) 
    {
        fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
        return 1;
    }
    selffd = getUDPsockandbind(servinfo);
    freeaddrinfo(servinfo);

    memset(&hints, 0, sizeof hints);
    hints.ai_family = AF_UNSPEC; // set to AF_INET to force IPv4
    hints.ai_socktype = SOCK_DGRAM;
    if ((rv = getaddrinfo("127.0.0.1", AWSUDPPORT, &hints, &servinfo)) != 0) 
    {
        fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
        return 1;
    }

    std::cout << "The server C is up and running using UDP on port " << CUDPPORT << "\n" << std::endl;

    while(1)
    {
        if ((numbytes = recvfrom(selffd, buf, MAXDATASIZE-1 , 0, (struct sockaddr *) NULL, NULL)) == -1) 
        {
            perror("recvfrom");
            exit(1);
        }
        memcpy(&srcVtxid, buf, sizeof(int));
        memcpy(&dstVtxid, buf+sizeof(int), sizeof(int));
        memcpy(&filesize, buf+2*sizeof(int), sizeof(int));
        memcpy(&mapid, buf+3*sizeof(int), sizeof(char));

        stringbuff.assign(buf + 3*sizeof(int) + sizeof(char), numbytes - 3*sizeof(int) - sizeof(char));

        std::istringstream tokenStream (stringbuff);
        std::string Pspeedbuf, Tspeedbuf;
        std::getline(tokenStream,Pspeedbuf);
        std::getline(tokenStream,Tspeedbuf);
        Pspeed = atof(Pspeedbuf.c_str());
        Tspeed = atoi(Tspeedbuf.c_str());

        std::cout << "The Server C has received data for calculation:\n" << "* Propagation Speed: " << Pspeed << " km/s;\n* Transmission Speed: " << Tspeed 
        << " KB/s;\n* Map ID: " << mapid << ";\n* Source ID: " << srcVtxid << ";\n* Destination ID: " << dstVtxid << ";\n" << std::endl;

        // find the shortest path and its length and store them in path and mindistan respectively.
        searchpath(tokenStream, srcVtxid, dstVtxid, path, &mindistan); 

        Tt = (float) filesize/ (float) Tspeed;
        Tp = mindistan/Pspeed;

        std::cout << "The Server C has finished the calculation:\n" <<  "Shortest path: ";
        for (int i =0 ; i< 11; i++)
        {
            if(path[i] == 2333)
                break;
            
            if (i==0)
                std::cout << path[i];
            else
                std::cout << " -- " << path[i];

        }
        std::cout << "\nShortest Distance: " << mindistan << " km\nTransmission Delay: " << Tt << " s\nPropagation Delay: " << Tp << " s\n" << std::endl;

        memcpy(buf                  , &mindistan, sizeof(float));
        memcpy(buf + sizeof(float)  , &Tt       , sizeof(float));
        memcpy(buf + sizeof(float)*2, &Tp       , sizeof(float));
        memcpy(buf + sizeof(float)*3, path      , sizeof(path));
        if ((numbytes = sendto(selffd, buf, sizeof(float)*3 + sizeof(path), 0, servinfo->ai_addr, servinfo->ai_addrlen)) == -1) 
        {
            perror("talker: sendto");
            exit(1);
        }
        std::cout << "The Server C has finished sending the output to AWS\n" << std::endl ;
    }

    close(selffd);
    freeaddrinfo(servinfo);
    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x678, %rsp            # imm = 0x678
leaq 0xa0(%rsp), %r13
movq %r13, -0x10(%r13)
movq $0x0, -0x8(%r13)
movb $0x0, (%r13)
movl $0x4cbebc20, 0xc(%rsp)  # imm = 0x4CBEBC20
xorps %xmm0, %xmm0
leaq 0xe0(%rsp), %rdx
movaps %xmm0, (%rdx)
movaps %xmm0, 0x10(%rdx)
movaps %xmm0, 0x20(%rdx)
movl $0x2, 0x8(%rdx)
leaq 0x1c42(%rip), %rdi      # 0x4054
leaq 0x1c45(%rip), %rsi      # 0x405e
leaq 0x10(%rsp), %rcx
callq 0x2120
testl %eax, %eax
jne 0x2477
movq 0x10(%rsp), %rdi
callq 0x29f1
movl %eax, %ebx
leaq 0x10(%rsp), %r14
movq (%r14), %rdi
callq 0x2130
xorps %xmm0, %xmm0
leaq 0xe0(%rsp), %rdx
movaps %xmm0, (%rdx)
movaps %xmm0, 0x20(%rdx)
movaps %xmm0, 0x10(%rdx)
movl $0x2, 0x8(%rdx)
leaq 0x1bf0(%rip), %rdi      # 0x4054
leaq 0x1c0a(%rip), %rsi      # 0x4075
movq %r14, %rcx
callq 0x2120
testl %eax, %eax
je 0x24d0
movq 0x3b5a(%rip), %rcx      # 0x5fd8
movq (%rcx), %rbx
movl %eax, %edi
callq 0x2140
leaq 0x1bd5(%rip), %rsi      # 0x4064
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x2160
movq 0x90(%rsp), %rdi
cmpq %r13, %rdi
je 0x24b9
movq 0xa0(%rsp), %rsi
incq %rsi
callq 0x20d0
movl $0x1, %eax
addq $0x678, %rsp            # imm = 0x678
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x3af9(%rip), %rdi      # 0x5fd0
leaq 0x1b9d(%rip), %rsi      # 0x407b
movl $0x31, %edx
callq 0x20f0
movq 0x3ae1(%rip), %rdi      # 0x5fd0
leaq 0x1b68(%rip), %rsi      # 0x405e
movl $0x5, %edx
callq 0x20f0
movq 0x3ac9(%rip), %rdi      # 0x5fd0
leaq 0x1c71(%rip), %rsi      # 0x417f
movl $0x1, %edx
callq 0x20f0
movq 0x3ab1(%rip), %rdi      # 0x5fd0
callq 0x2080
leaq 0x110(%rsp), %r15
movq 0x3a9d(%rip), %r14      # 0x5fd0
movl $0x3e7, %edx            # imm = 0x3E7
movl %ebx, %edi
leaq 0x290(%rsp), %rsi
xorl %ecx, %ecx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x2090
cmpl $-0x1, %eax
je 0x295b
movl 0x290(%rsp), %ecx
movl %ecx, 0x30(%rsp)
movl 0x294(%rsp), %ecx
movl %ecx, 0x20(%rsp)
movl 0x298(%rsp), %ebp
movb 0x29c(%rsp), %r12b
movslq %eax, %r8
addq $-0xd, %r8
movq 0x98(%rsp), %rdx
leaq 0x90(%rsp), %rdi
xorl %esi, %esi
leaq 0x29d(%rsp), %rcx
callq 0x2180
movl %ebp, 0x1c(%rsp)
movq %r15, %rdi
leaq 0x90(%rsp), %rsi
movl $0x8, %edx
callq 0x21b0
leaq 0x70(%rsp), %rax
movq %rax, 0x60(%rsp)
xorl %ecx, %ecx
movq %rcx, 0x68(%rsp)
movb %cl, 0x70(%rsp)
leaq 0x50(%rsp), %rax
movq %rax, 0x40(%rsp)
movq %rcx, 0x48(%rsp)
movb %cl, 0x50(%rsp)
movq %r15, %rdi
leaq 0x60(%rsp), %rsi
callq 0x2050
leaq 0x110(%rsp), %rdi
leaq 0x40(%rsp), %rsi
callq 0x2050
movq 0x60(%rsp), %rdi
callq 0x2110
movsd %xmm0, 0x80(%rsp)
movq 0x40(%rsp), %rdi
callq 0x2190
movl %eax, %ebp
movl $0x30, %edx
movq %r14, %rdi
leaq 0x1a85(%rip), %rsi      # 0x40b6
callq 0x20f0
movl $0x15, %edx
movq %r14, %rdi
leaq 0x1aa2(%rip), %rsi      # 0x40e7
callq 0x20f0
xorps %xmm0, %xmm0
cvtsd2ss 0x80(%rsp), %xmm0
movaps %xmm0, 0x80(%rsp)
cvtss2sd %xmm0, %xmm0
movq %r14, %rdi
callq 0x21a0
movq %rax, %r15
movl $0x1d, %edx
movq %rax, %rdi
leaq 0x1a81(%rip), %rsi      # 0x40fd
callq 0x20f0
movq %r15, %rdi
movl %ebp, %esi
callq 0x21c0
movq %rax, %r15
movl $0x11, %edx
movq %rax, %rdi
leaq 0x1a7e(%rip), %rsi      # 0x411b
callq 0x20f0
movb %r12b, 0xb(%rsp)
movl $0x1, %edx
movq %r15, %rdi
leaq 0xb(%rsp), %rsi
callq 0x20f0
movq %rax, %r15
movl $0xf, %edx
movq %rax, %rdi
leaq 0x1a62(%rip), %rsi      # 0x412d
callq 0x20f0
movq %r15, %rdi
movl 0x30(%rsp), %esi
callq 0x21c0
movq %rax, %r15
movl $0x14, %edx
movq %rax, %rdi
leaq 0x1a4f(%rip), %rsi      # 0x413d
callq 0x20f0
movq %r15, %rdi
movl 0x20(%rsp), %esi
callq 0x21c0
movq %rax, %r15
movl $0x2, %edx
movq %rax, %rdi
leaq 0x1a41(%rip), %rsi      # 0x4152
callq 0x20f0
movq %r15, %rdi
callq 0x2080
leaq 0x110(%rsp), %rdi
movl 0x30(%rsp), %esi
movl 0x20(%rsp), %edx
leaq 0xb0(%rsp), %rcx
leaq 0xc(%rsp), %r8
callq 0x2a9b
movss 0xc(%rsp), %xmm0
movaps %xmm0, 0x30(%rsp)
movl $0x2b, %edx
movq %r14, %rdi
leaq 0x19fb(%rip), %rsi      # 0x4155
callq 0x20f0
movl $0xf, %edx
movq %r14, %rdi
leaq 0x1a13(%rip), %rsi      # 0x4181
callq 0x20f0
cvtsi2ssl 0x1c(%rsp), %xmm1
xorps %xmm0, %xmm0
cvtsi2ss %ebp, %xmm0
unpcklps 0x30(%rsp), %xmm1       # xmm1 = xmm1[0],mem[0],xmm1[1],mem[1]
unpcklps 0x80(%rsp), %xmm0       # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
divps %xmm0, %xmm1
movaps %xmm1, 0x20(%rsp)
xorl %r15d, %r15d
movl 0xb0(%rsp,%r15,4), %ebp
cmpl $0x91d, %ebp            # imm = 0x91D
je 0x27d4
testq %r15, %r15
je 0x27c1
movl $0x4, %edx
movq %r14, %rdi
leaq 0x19d5(%rip), %rsi      # 0x4191
callq 0x20f0
movq %r14, %rdi
movl %ebp, %esi
callq 0x21c0
incq %r15
cmpq $0xb, %r15
jne 0x2798
movl $0x14, %edx
movq %r14, %rdi
leaq 0x19b3(%rip), %rsi      # 0x4196
callq 0x20f0
xorps %xmm0, %xmm0
cvtss2sd 0x30(%rsp), %xmm0
movq %r14, %rdi
callq 0x21a0
movq %rax, %r15
movl $0x18, %edx
movq %rax, %rdi
leaq 0x19a0(%rip), %rsi      # 0x41ab
callq 0x20f0
xorps %xmm0, %xmm0
cvtss2sd 0x20(%rsp), %xmm0
movq %r15, %rdi
callq 0x21a0
movq %rax, %r15
movl $0x16, %edx
movq %rax, %rdi
leaq 0x1991(%rip), %rsi      # 0x41c4
callq 0x20f0
movaps 0x20(%rsp), %xmm0
shufps $0x55, %xmm0, %xmm0     # xmm0 = xmm0[1,1,1,1]
cvtss2sd %xmm0, %xmm0
movq %r15, %rdi
callq 0x21a0
movq %rax, %r15
movl $0x3, %edx
movq %rax, %rdi
leaq 0x197c(%rip), %rsi      # 0x41db
callq 0x20f0
movq %r15, %rdi
callq 0x2080
movaps 0x30(%rsp), %xmm0
movss %xmm0, 0x290(%rsp)
movaps 0x20(%rsp), %xmm0
movlps %xmm0, 0x294(%rsp)
movups 0xcc(%rsp), %xmm0
leaq 0x29c(%rsp), %rax
movups %xmm0, 0x1c(%rax)
movaps 0xb0(%rsp), %xmm0
movaps 0xc0(%rsp), %xmm1
movups %xmm1, 0x10(%rax)
movups %xmm0, (%rax)
movq 0x10(%rsp), %rax
movq 0x18(%rax), %r8
movl 0x10(%rax), %r9d
movl $0x38, %edx
movl %ebx, %edi
leaq 0x290(%rsp), %rsi
xorl %ecx, %ecx
callq 0x2030
cmpl $-0x1, %eax
je 0x2960
movl $0x34, %edx
movq %r14, %rdi
leaq 0x1901(%rip), %rsi      # 0x41ee
callq 0x20f0
movq %r14, %rdi
callq 0x2080
movq 0x40(%rsp), %rdi
leaq 0x50(%rsp), %rax
cmpq %rax, %rdi
je 0x2916
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x20d0
movq 0x60(%rsp), %rdi
leaq 0x70(%rsp), %rax
cmpq %rax, %rdi
leaq 0x110(%rsp), %r15
je 0x293a
movq 0x70(%rsp), %rsi
incq %rsi
callq 0x20d0
movq %r15, %rdi
movq 0x3684(%rip), %rsi      # 0x5fc8
callq 0x2150
leaq 0x188(%rsp), %rdi
callq 0x2070
jmp 0x2533
callq 0x2255
callq 0x2240
jmp 0x29c9
jmp 0x29c9
jmp 0x296b
movq %rax, %rbx
movq 0x40(%rsp), %rdi
leaq 0x50(%rsp), %rax
cmpq %rax, %rdi
je 0x298a
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x20d0
movq 0x60(%rsp), %rdi
leaq 0x70(%rsp), %rax
cmpq %rax, %rdi
je 0x29a6
movq 0x70(%rsp), %rsi
incq %rsi
callq 0x20d0
movq 0x361b(%rip), %rsi      # 0x5fc8
leaq 0x110(%rsp), %rdi
callq 0x2150
leaq 0x188(%rsp), %rdi
callq 0x2070
jmp 0x29cc
movq %rax, %rbx
movq 0x90(%rsp), %rdi
cmpq %r13, %rdi
je 0x29e9
movq 0xa0(%rsp), %rsi
incq %rsi
callq 0x20d0
movq %rbx, %rdi
callq 0x21e0