// File: /workspace/github_cpp_makefile_star2_O1/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 $0x658, %rsp            # imm = 0x658
leaq 0x80(%rsp), %r13
movq %r13, -0x10(%r13)
movq $0x0, -0x8(%r13)
movb $0x0, (%r13)
movl $0x4cbebc20, 0x14(%rsp) # imm = 0x4CBEBC20
xorps %xmm0, %xmm0
leaq 0xc0(%rsp), %rdx
movaps %xmm0, (%rdx)
movaps %xmm0, 0x10(%rdx)
movaps %xmm0, 0x20(%rdx)
movl $0x2, 0x8(%rdx)
leaq 0xbf6(%rip), %rdi       # 0x3008
leaq 0xbf9(%rip), %rsi       # 0x3012
leaq 0x20(%rsp), %rcx
callq 0x2120
testl %eax, %eax
jne 0x2477
movq 0x20(%rsp), %rdi
callq 0x29ec
movl %eax, %ebx
leaq 0x20(%rsp), %r14
movq (%r14), %rdi
callq 0x2130
xorps %xmm0, %xmm0
leaq 0xc0(%rsp), %rdx
movaps %xmm0, (%rdx)
movaps %xmm0, 0x20(%rdx)
movaps %xmm0, 0x10(%rdx)
movl $0x2, 0x8(%rdx)
leaq 0xba4(%rip), %rdi       # 0x3008
leaq 0xbbe(%rip), %rsi       # 0x3029
movq %r14, %rcx
callq 0x2120
testl %eax, %eax
je 0x24cd
movq 0x2b5a(%rip), %rcx      # 0x4fd8
movq (%rcx), %rbx
movl %eax, %edi
callq 0x2140
leaq 0xb89(%rip), %rsi       # 0x3018
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x2160
movq 0x70(%rsp), %rdi
cmpq %r13, %rdi
je 0x24b6
movq 0x80(%rsp), %rsi
incq %rsi
callq 0x20d0
movl $0x1, %eax
addq $0x658, %rsp            # imm = 0x658
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x2afc(%rip), %rdi      # 0x4fd0
leaq 0xb54(%rip), %rsi       # 0x302f
movl $0x31, %edx
callq 0x20f0
movq 0x2ae4(%rip), %rdi      # 0x4fd0
leaq 0xb1f(%rip), %rsi       # 0x3012
movl $0x5, %edx
callq 0x20f0
movq 0x2acc(%rip), %rdi      # 0x4fd0
leaq 0xc28(%rip), %rsi       # 0x3133
movl $0x1, %edx
callq 0x20f0
movq 0x2ab4(%rip), %rdi      # 0x4fd0
callq 0x2080
leaq 0xf0(%rsp), %r15
movq 0x2aa0(%rip), %r14      # 0x4fd0
movl $0x3e7, %edx            # imm = 0x3E7
movl %ebx, %edi
leaq 0x270(%rsp), %rsi
xorl %ecx, %ecx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x2090
cmpl $-0x1, %eax
je 0x2959
movl 0x270(%rsp), %ecx
movl %ecx, 0x10(%rsp)
movl 0x274(%rsp), %ecx
movl %ecx, 0xc(%rsp)
movl 0x278(%rsp), %ebp
movb 0x27c(%rsp), %r12b
movslq %eax, %r8
addq $-0xd, %r8
movq 0x78(%rsp), %rdx
leaq 0x70(%rsp), %rdi
xorl %esi, %esi
leaq 0x27d(%rsp), %rcx
callq 0x2180
movl %ebp, 0x2c(%rsp)
movq %r15, %rdi
leaq 0x70(%rsp), %rsi
movl $0x8, %edx
callq 0x21b0
leaq 0x60(%rsp), %rax
movq %rax, 0x50(%rsp)
xorl %ecx, %ecx
movq %rcx, 0x58(%rsp)
movb %cl, 0x60(%rsp)
leaq 0x40(%rsp), %rax
movq %rax, 0x30(%rsp)
movq %rcx, 0x38(%rsp)
movb %cl, 0x40(%rsp)
movq %r15, %rdi
leaq 0x50(%rsp), %rsi
callq 0x2050
leaq 0xf0(%rsp), %rdi
leaq 0x30(%rsp), %rsi
callq 0x2050
movq 0x50(%rsp), %rdi
callq 0x2110
movsd %xmm0, 0x18(%rsp)
movq 0x30(%rsp), %rdi
callq 0x2190
movl %eax, %ebp
movl $0x30, %edx
movq %r14, %rdi
leaq 0xa48(%rip), %rsi       # 0x306a
callq 0x20f0
movl $0x15, %edx
movq %r14, %rdi
leaq 0xa65(%rip), %rsi       # 0x309b
callq 0x20f0
xorps %xmm0, %xmm0
cvtsd2ss 0x18(%rsp), %xmm0
movss %xmm0, 0x18(%rsp)
cvtss2sd %xmm0, %xmm0
movq %r14, %rdi
callq 0x21a0
movq %rax, %r15
movl $0x1d, %edx
movq %rax, %rdi
leaq 0xa49(%rip), %rsi       # 0x30b1
callq 0x20f0
movq %r15, %rdi
movl %ebp, %esi
callq 0x21c0
movq %rax, %r15
movl $0x11, %edx
movq %rax, %rdi
leaq 0xa46(%rip), %rsi       # 0x30cf
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 0xa2a(%rip), %rsi       # 0x30e1
callq 0x20f0
movq %r15, %rdi
movl 0x10(%rsp), %esi
callq 0x21c0
movq %rax, %r15
movl $0x14, %edx
movq %rax, %rdi
leaq 0xa17(%rip), %rsi       # 0x30f1
callq 0x20f0
movq %r15, %rdi
movl 0xc(%rsp), %esi
callq 0x21c0
movq %rax, %r15
movl $0x2, %edx
movq %rax, %rdi
leaq 0xa09(%rip), %rsi       # 0x3106
callq 0x20f0
movq %r15, %rdi
callq 0x2080
leaq 0xf0(%rsp), %rdi
movl 0x10(%rsp), %esi
movl 0xc(%rsp), %edx
leaq 0x90(%rsp), %rcx
leaq 0x14(%rsp), %r8
callq 0x2aa7
movss 0x14(%rsp), %xmm0
movss %xmm0, 0x10(%rsp)
movl $0x2b, %edx
movq %r14, %rdi
leaq 0x9c2(%rip), %rsi       # 0x3109
callq 0x20f0
movl $0xf, %edx
movq %r14, %rdi
leaq 0x9da(%rip), %rsi       # 0x3135
callq 0x20f0
cvtsi2ssl 0x2c(%rsp), %xmm1
xorps %xmm0, %xmm0
cvtsi2ss %ebp, %xmm0
divss %xmm0, %xmm1
movss %xmm1, 0xc(%rsp)
movss 0x10(%rsp), %xmm0
divss 0x18(%rsp), %xmm0
movss %xmm0, 0x18(%rsp)
xorl %r15d, %r15d
movl 0x90(%rsp,%r15,4), %ebp
cmpl $0x91d, %ebp            # imm = 0x91D
je 0x27c8
testq %r15, %r15
je 0x27b5
movl $0x4, %edx
movq %r14, %rdi
leaq 0x995(%rip), %rsi       # 0x3145
callq 0x20f0
movq %r14, %rdi
movl %ebp, %esi
callq 0x21c0
incq %r15
cmpq $0xb, %r15
jne 0x278c
movl $0x14, %edx
movq %r14, %rdi
leaq 0x973(%rip), %rsi       # 0x314a
callq 0x20f0
xorps %xmm0, %xmm0
cvtss2sd 0x10(%rsp), %xmm0
movq %r14, %rdi
callq 0x21a0
movq %rax, %r15
movl $0x18, %edx
movq %rax, %rdi
leaq 0x960(%rip), %rsi       # 0x315f
callq 0x20f0
xorps %xmm0, %xmm0
cvtss2sd 0xc(%rsp), %xmm0
movq %r15, %rdi
callq 0x21a0
movq %rax, %r15
movl $0x16, %edx
movq %rax, %rdi
leaq 0x951(%rip), %rsi       # 0x3178
callq 0x20f0
xorps %xmm0, %xmm0
cvtss2sd 0x18(%rsp), %xmm0
movq %r15, %rdi
callq 0x21a0
movq %rax, %r15
movl $0x3, %edx
movq %rax, %rdi
leaq 0x940(%rip), %rsi       # 0x318f
callq 0x20f0
movq %r15, %rdi
callq 0x2080
movl 0x14(%rsp), %eax
movl %eax, 0x270(%rsp)
movss 0xc(%rsp), %xmm0
movss %xmm0, 0x274(%rsp)
movss 0x18(%rsp), %xmm0
movss %xmm0, 0x278(%rsp)
movups 0xac(%rsp), %xmm0
leaq 0x27c(%rsp), %rax
movups %xmm0, 0x1c(%rax)
movaps 0x90(%rsp), %xmm0
movaps 0xa0(%rsp), %xmm1
movups %xmm1, 0x10(%rax)
movups %xmm0, (%rax)
movq 0x20(%rsp), %rax
movq 0x18(%rax), %r8
movl 0x10(%rax), %r9d
movl $0x38, %edx
movl %ebx, %edi
leaq 0x270(%rsp), %rsi
xorl %ecx, %ecx
callq 0x2030
cmpl $-0x1, %eax
je 0x295e
movl $0x34, %edx
movq %r14, %rdi
leaq 0x8b7(%rip), %rsi       # 0x31a2
callq 0x20f0
movq %r14, %rdi
callq 0x2080
movq 0x30(%rsp), %rdi
leaq 0x40(%rsp), %rax
cmpq %rax, %rdi
je 0x2914
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x20d0
movq 0x50(%rsp), %rdi
leaq 0x60(%rsp), %rax
cmpq %rax, %rdi
leaq 0xf0(%rsp), %r15
je 0x2938
movq 0x60(%rsp), %rsi
incq %rsi
callq 0x20d0
movq %r15, %rdi
movq 0x2686(%rip), %rsi      # 0x4fc8
callq 0x2150
leaq 0x168(%rsp), %rdi
callq 0x2070
jmp 0x2530
callq 0x2255
callq 0x2240
jmp 0x29c7
jmp 0x29c7
jmp 0x2969
movq %rax, %rbx
movq 0x30(%rsp), %rdi
leaq 0x40(%rsp), %rax
cmpq %rax, %rdi
je 0x2988
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x20d0
movq 0x50(%rsp), %rdi
leaq 0x60(%rsp), %rax
cmpq %rax, %rdi
je 0x29a4
movq 0x60(%rsp), %rsi
incq %rsi
callq 0x20d0
movq 0x261d(%rip), %rsi      # 0x4fc8
leaq 0xf0(%rsp), %rdi
callq 0x2150
leaq 0x168(%rsp), %rdi
callq 0x2070
jmp 0x29ca
movq %rax, %rbx
movq 0x70(%rsp), %rdi
cmpq %r13, %rdi
je 0x29e4
movq 0x80(%rsp), %rsi
incq %rsi
callq 0x20d0
movq %rbx, %rdi
callq 0x21e0