// File: /workspace/github_cpp_makefile_star2_O1/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), %rbp
movq %rbp, -0x10(%rbp)
movq $0x0, -0x8(%rbp)
movb $0x0, (%rbp)
xorps %xmm0, %xmm0
leaq 0x30(%rsp), %rdx
movaps %xmm0, (%rdx)
movaps %xmm0, 0x10(%rdx)
movaps %xmm0, 0x20(%rdx)
movl $0x2, 0x8(%rdx)
leaq 0xbd1(%rip), %rdi       # 0x3004
leaq 0xbd4(%rip), %rsi       # 0x300e
leaq 0x8(%rsp), %rcx
callq 0x2180
testl %eax, %eax
jne 0x2495
movq 0x8(%rsp), %rdi
callq 0x2750
movl %eax, %ebx
leaq 0x8(%rsp), %r14
movq (%r14), %rdi
callq 0x2190
xorps %xmm0, %xmm0
leaq 0x30(%rsp), %rdx
movaps %xmm0, (%rdx)
movaps %xmm0, 0x20(%rdx)
movaps %xmm0, 0x10(%rdx)
movl $0x2, 0x8(%rdx)
leaq 0xb82(%rip), %rdi       # 0x3004
leaq 0xb9c(%rip), %rsi       # 0x3025
movq %r14, %rcx
callq 0x2180
testl %eax, %eax
je 0x24e8
movq 0x2b3c(%rip), %rcx      # 0x4fd8
movq (%rcx), %rbx
movl %eax, %edi
callq 0x21a0
leaq 0xb67(%rip), %rsi       # 0x3014
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x21b0
movq 0x10(%rsp), %rdi
cmpq %rbp, %rdi
je 0x24d1
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x2130
movl $0x1, %eax
addq $0x448, %rsp            # imm = 0x448
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x2ae1(%rip), %rdi      # 0x4fd0
leaq 0xb35(%rip), %rsi       # 0x302b
movl $0x31, %edx
callq 0x2160
movq 0x2ac9(%rip), %rdi      # 0x4fd0
leaq 0xb00(%rip), %rsi       # 0x300e
movl $0x5, %edx
callq 0x2160
movq 0x2ab1(%rip), %rdi      # 0x4fd0
leaq 0xc1f(%rip), %rsi       # 0x3145
movl $0x1, %edx
callq 0x2160
movq 0x2a99(%rip), %rdi      # 0x4fd0
callq 0x20c0
movl $0x3e7, %edx            # imm = 0x3E7
movl %ebx, %edi
leaq 0x60(%rsp), %rsi
xorl %ecx, %ecx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x20f0
cmpl $-0x1, %eax
je 0x2722
movb 0x60(%rsp), %r14b
movl 0x61(%rsp), %r12d
movl 0x65(%rsp), %r15d
movl $0x39, %edx
movq 0x2a57(%rip), %rdi      # 0x4fd0
leaq 0xae6(%rip), %rsi       # 0x3066
callq 0x2160
movb %r14b, 0x7(%rsp)
movl $0x1, %edx
movq 0x2a3a(%rip), %rdi      # 0x4fd0
leaq 0x7(%rsp), %rsi
callq 0x2160
movq %rax, %r13
movl $0x1, %edx
movq %rax, %rdi
leaq 0xb93(%rip), %rsi       # 0x3145
callq 0x2160
movq (%r13), %rax
movq -0x18(%rax), %rdi
addq %r13, %rdi
movl $0xa, %esi
callq 0x2120
movsbl %al, %esi
movq %r13, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x20e0
movsbl %r14b, %esi
leaq 0x10(%rsp), %rdi
movl %r12d, %edx
movl %r15d, %ecx
callq 0x280b
movl %eax, %r15d
testl %eax, %eax
jne 0x264c
movl $0x31, %edx
movq 0x29ca(%rip), %rdi      # 0x4fd0
leaq 0xa93(%rip), %rsi       # 0x30a0
callq 0x2160
movb %r14b, 0x7(%rsp)
movl $0x1, %edx
movq 0x29ad(%rip), %rdi      # 0x4fd0
leaq 0x7(%rsp), %rsi
callq 0x2160
movq %rax, %r12
movl $0x1, %edx
movq %rax, %rdi
leaq 0xb06(%rip), %rsi       # 0x3145
callq 0x2160
movq %r12, %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 0x2040
cmpl $-0x1, %eax
je 0x2727
testl %r15d, %r15d
je 0x26e1
movl $0x22, %edx
movq 0x294a(%rip), %rdi      # 0x4fd0
leaq 0xa54(%rip), %rsi       # 0x30e1
callq 0x2160
movl $0x1, %edx
movq 0x2932(%rip), %rdi      # 0x4fd0
leaq 0xaa0(%rip), %rsi       # 0x3145
callq 0x2160
movq 0x291f(%rip), %rcx      # 0x4fd0
movq (%rcx), %rax
movq -0x18(%rax), %rdi
addq %rcx, %rdi
movl $0xa, %esi
callq 0x2120
movsbl %al, %esi
movq 0x2901(%rip), %rdi      # 0x4fd0
callq 0x2030
movq %rax, %rdi
callq 0x20e0
jmp 0x253c
movl $0x2e, %edx
movq 0x28e3(%rip), %rdi      # 0x4fd0
leaq 0xa10(%rip), %rsi       # 0x3104
callq 0x2160
movl $0x1, %edx
movq 0x28cb(%rip), %rdi      # 0x4fd0
leaq 0xa39(%rip), %rsi       # 0x3145
callq 0x2160
movq 0x28b8(%rip), %rdi      # 0x4fd0
callq 0x20c0
jmp 0x253c
callq 0x2285
callq 0x2270
jmp 0x272e
movq %rax, %rbx
movq 0x10(%rsp), %rdi
cmpq %rbp, %rdi
je 0x2748
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x2130
movq %rbx, %rdi
callq 0x2200