// File: 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
movq %rsp, %rbp
subq $0x540, %rsp            # imm = 0x540
movl $0x0, -0x4(%rbp)
leaq -0x470(%rbp), %rdi
callq 0x21b0
movl $0x0, -0x474(%rbp)
xorps %xmm0, %xmm0
movaps %xmm0, -0x20(%rbp)
movaps %xmm0, -0x30(%rbp)
movaps %xmm0, -0x40(%rbp)
movl $0x0, -0x3c(%rbp)
movl $0x2, -0x38(%rbp)
leaq 0xbe8(%rip), %rdi       # 0x3004
leaq 0xbeb(%rip), %rsi       # 0x300e
leaq -0x40(%rbp), %rdx
leaq -0x48(%rbp), %rcx
callq 0x2170
movl %eax, -0x48c(%rbp)
jmp 0x2438
movl -0x48c(%rbp), %eax
movl %eax, -0x4c(%rbp)
cmpl $0x0, %eax
je 0x24b0
movq 0x2b8b(%rip), %rax      # 0x4fd8
movq (%rax), %rax
movq %rax, -0x498(%rbp)
movl -0x4c(%rbp), %edi
callq 0x21c0
movq -0x498(%rbp), %rdi
movq %rax, %rdx
leaq 0xba4(%rip), %rsi       # 0x3014
movb $0x0, %al
callq 0x21d0
movl $0x1, -0x4(%rbp)
movl $0x1, -0x488(%rbp)
jmp 0x2889
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x480(%rbp)
movl %eax, -0x484(%rbp)
leaq -0x470(%rbp), %rdi
callq 0x20c0
jmp 0x28a1
movq -0x48(%rbp), %rdi
callq 0x28b0
movl %eax, -0x49c(%rbp)
jmp 0x24c1
movl -0x49c(%rbp), %eax
movl %eax, -0x8(%rbp)
movq -0x48(%rbp), %rdi
callq 0x2190
xorps %xmm0, %xmm0
movaps %xmm0, -0x20(%rbp)
movaps %xmm0, -0x30(%rbp)
movaps %xmm0, -0x40(%rbp)
movl $0x0, -0x3c(%rbp)
movl $0x2, -0x38(%rbp)
leaq 0xb0d(%rip), %rdi       # 0x3004
leaq 0xb27(%rip), %rsi       # 0x3025
leaq -0x40(%rbp), %rdx
leaq -0x48(%rbp), %rcx
callq 0x2170
movl %eax, -0x4a0(%rbp)
jmp 0x2513
movl -0x4a0(%rbp), %eax
movl %eax, -0x4c(%rbp)
cmpl $0x0, %eax
je 0x2568
movq 0x2ab0(%rip), %rax      # 0x4fd8
movq (%rax), %rax
movq %rax, -0x4a8(%rbp)
movl -0x4c(%rbp), %edi
callq 0x21c0
movq -0x4a8(%rbp), %rdi
movq %rax, %rdx
leaq 0xac9(%rip), %rsi       # 0x3014
movb $0x0, %al
callq 0x21d0
movl $0x1, -0x4(%rbp)
movl $0x1, -0x488(%rbp)
jmp 0x2889
movq 0x2a61(%rip), %rdi      # 0x4fd0
leaq 0xab5(%rip), %rsi       # 0x302b
callq 0x2110
movq %rax, -0x4b0(%rbp)
jmp 0x2584
movq -0x4b0(%rbp), %rdi
leaq 0xa7c(%rip), %rsi       # 0x300e
callq 0x2110
movq %rax, -0x4b8(%rbp)
jmp 0x25a0
movq -0x4b8(%rbp), %rdi
leaq 0xb97(%rip), %rsi       # 0x3145
callq 0x2110
movq %rax, -0x4c0(%rbp)
jmp 0x25bc
movq -0x4c0(%rbp), %rdi
movq 0x29f6(%rip), %rsi      # 0x4fc0
callq 0x2130
jmp 0x25d1
jmp 0x25d3
jmp 0x25d5
movl -0x8(%rbp), %edi
xorl %ecx, %ecx
movl %ecx, %r9d
leaq -0x440(%rbp), %rsi
movl $0x3e7, %edx            # imm = 0x3E7
movq %r9, %r8
callq 0x20e0
movq %rax, -0x4c8(%rbp)
jmp 0x25fa
movq -0x4c8(%rbp), %rax
movl %eax, -0x444(%rbp)
cmpl $-0x1, %eax
jne 0x2624
leaq 0xa4a(%rip), %rdi       # 0x305d
callq 0x20f0
jmp 0x261a
movl $0x1, %edi
callq 0x2160
movb -0x440(%rbp), %al
movb %al, -0x445(%rbp)
movl -0x43f(%rbp), %eax
movl %eax, -0x44c(%rbp)
movl -0x43b(%rbp), %eax
movl %eax, -0x450(%rbp)
movq 0x2981(%rip), %rdi      # 0x4fd0
leaq 0xa10(%rip), %rsi       # 0x3066
callq 0x2110
movq %rax, -0x4d0(%rbp)
jmp 0x2664
movq -0x4d0(%rbp), %rdi
movsbl -0x445(%rbp), %esi
callq 0x2150
movq %rax, -0x4d8(%rbp)
jmp 0x2680
movq -0x4d8(%rbp), %rdi
leaq 0xab7(%rip), %rsi       # 0x3145
callq 0x2110
movq %rax, -0x4e0(%rbp)
jmp 0x269c
movq -0x4e0(%rbp), %rdi
movq 0x2916(%rip), %rsi      # 0x4fc0
callq 0x2130
jmp 0x26b1
movsbl -0x445(%rbp), %esi
movl -0x44c(%rbp), %edx
movl -0x450(%rbp), %ecx
leaq -0x470(%rbp), %rdi
callq 0x29c0
movl %eax, -0x4e4(%rbp)
jmp 0x26d8
movl -0x4e4(%rbp), %eax
movl %eax, -0x474(%rbp)
cmpl $0x0, -0x474(%rbp)
jne 0x2758
movq 0x28dc(%rip), %rdi      # 0x4fd0
leaq 0x9a5(%rip), %rsi       # 0x30a0
callq 0x2110
movq %rax, -0x4f0(%rbp)
jmp 0x2709
movq -0x4f0(%rbp), %rdi
movsbl -0x445(%rbp), %esi
callq 0x2150
movq %rax, -0x4f8(%rbp)
jmp 0x2725
movq -0x4f8(%rbp), %rdi
leaq 0xa12(%rip), %rsi       # 0x3145
callq 0x2110
movq %rax, -0x500(%rbp)
jmp 0x2741
movq -0x500(%rbp), %rdi
movq 0x2871(%rip), %rsi      # 0x4fc0
callq 0x2130
jmp 0x2756
jmp 0x2758
movl -0x8(%rbp), %eax
movl %eax, -0x514(%rbp)
leaq -0x470(%rbp), %rdi
movq %rdi, -0x520(%rbp)
callq 0x20b0
movq -0x520(%rbp), %rdi
movq %rax, -0x510(%rbp)
callq 0x21e0
movl -0x514(%rbp), %edi
movq -0x510(%rbp), %rsi
movq %rax, %rdx
movq -0x48(%rbp), %rax
movq 0x18(%rax), %r8
movl 0x10(%rax), %r9d
xorl %ecx, %ecx
callq 0x2030
movq %rax, -0x508(%rbp)
jmp 0x27b3
movq -0x508(%rbp), %rax
movl %eax, -0x444(%rbp)
cmpl $-0x1, %eax
jne 0x27dd
leaq 0x906(%rip), %rdi       # 0x30d2
callq 0x20f0
jmp 0x27d3
movl $0x1, %edi
callq 0x2160
cmpl $0x0, -0x474(%rbp)
je 0x2835
movq 0x27e3(%rip), %rdi      # 0x4fd0
leaq 0x8ed(%rip), %rsi       # 0x30e1
callq 0x2110
movq %rax, -0x528(%rbp)
jmp 0x2802
movq -0x528(%rbp), %rdi
leaq 0x935(%rip), %rsi       # 0x3145
callq 0x2110
movq %rax, -0x530(%rbp)
jmp 0x281e
movq -0x530(%rbp), %rdi
movq 0x2794(%rip), %rsi      # 0x4fc0
callq 0x2130
jmp 0x2833
jmp 0x2884
movq 0x2794(%rip), %rdi      # 0x4fd0
leaq 0x8c1(%rip), %rsi       # 0x3104
callq 0x2110
movq %rax, -0x538(%rbp)
jmp 0x2851
movq -0x538(%rbp), %rdi
leaq 0x8e6(%rip), %rsi       # 0x3145
callq 0x2110
movq %rax, -0x540(%rbp)
jmp 0x286d
movq -0x540(%rbp), %rdi
movq 0x2745(%rip), %rsi      # 0x4fc0
callq 0x2130
jmp 0x2882
jmp 0x2884
jmp 0x25d3
leaq -0x470(%rbp), %rdi
callq 0x20c0
movl -0x4(%rbp), %eax
addq $0x540, %rsp            # imm = 0x540
popq %rbp
retq
movq -0x480(%rbp), %rdi
callq 0x2230
nopl (%rax)