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

    char buf[MAXDATASIZE];int numbytes;
    char mapid; int srcVtxid,dstVtxid;
    std::string mapbuff; int foundmap=0;

    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", BUDPPORT, &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 B is up and running using UDP on port " << BUDPPORT << "\n" << std::endl;

    while(1)
    {
        if ((numbytes = recvfrom(selffd, buf, MAXDATASIZE-1 , 0, (struct sockaddr *) NULL, NULL)) == -1) 
        {
            perror("recvfrom");
            exit(1);
        }

        mapid = buf[0];
        memcpy(&srcVtxid,buf+1,sizeof(int));
        memcpy(&dstVtxid,buf+1+sizeof(int),sizeof(int));

        std::cout << "The server B has received input for finding graph of map " << mapid << "\n" << std::endl;

        foundmap = searchmap(mapbuff, mapid, srcVtxid, dstVtxid);

        if(!foundmap)
            std::cout << "The server B does not have the required graph id " << mapid << "\n" << std::endl;

        if ((numbytes = sendto(selffd, mapbuff.data(), mapbuff.length(), 0, servinfo->ai_addr, servinfo->ai_addrlen)) == -1) 
        {
            perror("talker: sendto");
            exit(1);
        }
        
        if(foundmap)
            std::cout << "The server B has sent Graph to AWS" << "\n" << std::endl;
        else
            std::cout << "The server B has sent \"Graph not found\" 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 $0x448, %rsp            # imm = 0x448
leaq 0x20(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
xorps %xmm0, %xmm0
leaq 0x30(%rsp), %rdx
movaps %xmm0, (%rdx)
movaps %xmm0, 0x10(%rdx)
movaps %xmm0, 0x20(%rdx)
movl $0x2, 0x8(%rdx)
leaq 0xc55(%rip), %rdi       # 0x3004
leaq 0xc58(%rip), %rsi       # 0x300e
leaq 0x8(%rsp), %rcx
callq 0x2160
testl %eax, %eax
jne 0x2411
movq 0x8(%rsp), %rdi
callq 0x25d1
movl %eax, %ebx
leaq 0x8(%rsp), %r14
movq (%r14), %rdi
callq 0x2170
xorps %xmm0, %xmm0
leaq 0x30(%rsp), %rdx
movaps %xmm0, (%rdx)
movaps %xmm0, 0x20(%rdx)
movaps %xmm0, 0x10(%rdx)
movl $0x2, 0x8(%rdx)
leaq 0xc06(%rip), %rdi       # 0x3004
leaq 0xc20(%rip), %rsi       # 0x3025
movq %r14, %rcx
callq 0x2160
testl %eax, %eax
je 0x2455
movq 0x2bc0(%rip), %rcx      # 0x4fd8
movq (%rcx), %rbx
movl %eax, %edi
callq 0x2180
leaq 0xbeb(%rip), %rsi       # 0x3014
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x2190
leaq 0x10(%rsp), %rdi
callq 0x20b0
pushq $0x1
popq %rax
addq $0x448, %rsp            # imm = 0x448
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x2b74(%rip), %rdi      # 0x4fd0
leaq 0xbc8(%rip), %rsi       # 0x302b
callq 0x2110
leaq 0xb9f(%rip), %rsi       # 0x300e
movq %rax, %rdi
callq 0x2110
leaq 0xcc7(%rip), %rsi       # 0x3145
movq %rax, %rdi
callq 0x2110
movq %rax, %rdi
callq 0x20c0
movq 0x2b3b(%rip), %r15      # 0x4fd0
leaq 0xca9(%rip), %r13       # 0x3145
movl $0x3e7, %edx            # imm = 0x3E7
movl %ebx, %edi
leaq 0x60(%rsp), %rsi
xorl %ecx, %ecx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x20e0
cmpl $-0x1, %eax
je 0x259d
movb 0x60(%rsp), %r12b
movl 0x61(%rsp), %r14d
movl 0x65(%rsp), %ebp
movq %r15, %rdi
leaq 0xb90(%rip), %rsi       # 0x3066
callq 0x2110
movsbl %r12b, %r12d
movq %rax, %rdi
movl %r12d, %esi
callq 0x2140
movq %rax, %rdi
movq %r13, %rsi
callq 0x2110
movq %rax, %rdi
callq 0x20c0
leaq 0x10(%rsp), %rdi
movl %r12d, %esi
movl %r14d, %edx
movl %ebp, %ecx
callq 0x26a4
movl %eax, %r14d
testl %eax, %eax
jne 0x2543
movq %r15, %rdi
leaq 0xb80(%rip), %rsi       # 0x30a0
callq 0x2110
movq %rax, %rdi
movl %r12d, %esi
callq 0x2140
movq %rax, %rdi
movq %r13, %rsi
callq 0x2110
movq %rax, %rdi
callq 0x20c0
movq 0x10(%rsp), %rsi
movq 0x18(%rsp), %rdx
movq 0x8(%rsp), %rax
movq 0x18(%rax), %r8
movl 0x10(%rax), %r9d
movl %ebx, %edi
xorl %ecx, %ecx
callq 0x2030
cmpl $-0x1, %eax
je 0x25a6
testl %r14d, %r14d
leaq 0xb6f(%rip), %rsi       # 0x30e1
leaq 0xb8b(%rip), %rax       # 0x3104
cmoveq %rax, %rsi
movq %r15, %rdi
callq 0x2110
movq %rax, %rdi
movq %r13, %rsi
callq 0x2110
movq %rax, %rdi
callq 0x20c0
jmp 0x249c
leaq 0xab9(%rip), %rdi       # 0x305d
jmp 0x25ad
leaq 0xb25(%rip), %rdi       # 0x30d2
callq 0x20f0
pushq $0x1
popq %rdi
callq 0x2150
jmp 0x25bc
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x20b0
movq %rbx, %rdi
callq 0x21d0