// File: /workspace/github_c_makefile_star2_O3/MAGI003769[P]EE450-Socket-Project/serverB.c
// Function: main
int main(void) {
    int sockfd;
    struct addrinfo hints, *servinfo, *p;
    int rv;
    int numbytes;
    struct sockaddr_storage their_addr;
    socklen_t addr_len;
    char s[INET6_ADDRSTRLEN];

    char buf[MAXBUFLEN];
    char data[MAXBUFLEN];
    char *args[3];

    memset(&hints, 0, sizeof hints);
    hints.ai_family = AF_UNSPEC;
    hints.ai_socktype = SOCK_DGRAM;
    hints.ai_flags = AI_PASSIVE;

    // get information of the backend server itself with MYPORT
    if((rv = getaddrinfo(IPADDRESS, MYPORT, &hints, &servinfo)) != 0) {
        fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
        return 1;
    }

    // loop through all the results and make a socket
    for(p = servinfo; p != NULL; p = p->ai_next) {
        if((sockfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) {
            perror("server: socket");
            continue;
        }

        if(bind(sockfd, p->ai_addr, p->ai_addrlen) == -1) {
            close(sockfd);
            perror("server: bind");
            continue;
        }
        break;
    }

    if(p == NULL) {
        fprintf(stderr, "talker: failed to bind socket\n");
        return 2;
    }

    freeaddrinfo(servinfo);

    addr_len = sizeof their_addr;
    
    printf("The Server B is up and running using UDP on port %s\n", MYPORT);
    fflush(stdout);

    while(1) {
        if((numbytes = recvfrom(sockfd, buf, MAXBUFLEN-1, 0,
            (struct sockaddr *)&their_addr, &addr_len)) == -1) {
            perror("recvfrom");
            exit(1);
        }
        buf[numbytes] = '\0';

        // split messages into map_id, source_vtx and target_vtx 
        split_args2(args, buf);

        // read file and search the map_id
        //memset(&buf, 0, sizeof buf);
        search_map(args[0], data);

        printf("The Server B has received input for finding graph of map %s\n", args[0]);

        numbytes = sendto(sockfd, data, strlen(data), 0,
            (struct sockaddr *)&their_addr, addr_len);
        
        if(numbytes == -1) {
            perror("listener: sendto");
            exit(1);
        }

        if(strcmp(data, MAPNOTFOUND) == 0) {
            printf("The Server B does not have the required graph id %s\n", args[0]);
            printf("The Server B has sent \"Graph not Found\" to AWS\n");
        } else {
            printf("The Server B has sent Graph to AWS\n");
        }
        
        //close(sockfd);
    }

    return 0;

}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x2018, %rsp           # imm = 0x2018
xorps %xmm0, %xmm0
leaq -0x80(%rbp), %rdx
movaps %xmm0, (%rdx)
movaps %xmm0, 0x10(%rdx)
movaps %xmm0, 0x20(%rdx)
movl $0x2, 0x8(%rdx)
movl $0x1, (%rdx)
leaq 0xb33(%rip), %rdi       # 0x2038
leaq 0xb36(%rip), %rsi       # 0x2042
leaq -0x38(%rbp), %rcx
callq 0x1150
testl %eax, %eax
jne 0x1583
movq -0x38(%rbp), %r14
testq %r14, %r14
je 0x1577
leaq 0xb30(%rip), %r15       # 0x2059
leaq 0xb38(%rip), %r12       # 0x2068
movl 0x4(%r14), %edi
movl 0x8(%r14), %esi
movl 0xc(%r14), %edx
callq 0x1180
movq %r15, %rdi
cmpl $-0x1, %eax
je 0x1569
movl %eax, %ebx
movq 0x18(%r14), %rsi
movl 0x10(%r14), %edx
movl %eax, %edi
callq 0x10d0
cmpl $-0x1, %eax
jne 0x15a1
movl %ebx, %edi
callq 0x1090
movq %r12, %rdi
callq 0x10f0
movq 0x28(%r14), %r14
testq %r14, %r14
jne 0x1530
callq 0x1200
movl $0x2, %eax
jmp 0x158f
movl %eax, %edi
callq 0x11a0
movl $0x1, %eax
addq $0x2018, %rsp           # imm = 0x2018
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq -0x38(%rbp), %rdi
callq 0x1170
leaq -0x2c(%rbp), %r14
movl $0x80, (%r14)
leaq 0xad8(%rip), %rdi       # 0x2094
leaq 0xa7f(%rip), %rsi       # 0x2042
xorl %eax, %eax
callq 0x1060
movq 0x2a07(%rip), %rax      # 0x3fd8
movq (%rax), %rdi
callq 0x10b0
leaq -0x2040(%rbp), %rsi
leaq -0x100(%rbp), %r8
movl $0xf9f, %edx            # imm = 0xF9F
movl %ebx, %edi
xorl %ecx, %ecx
movq %r14, %r9
callq 0x10c0
cmpl $-0x1, %eax
je 0x16e1
leaq -0x2040(%rbp), %r15
leaq -0x10a0(%rbp), %r12
leaq -0x100(%rbp), %r14
cltq
movb $0x0, -0x2040(%rbp,%rax)
leaq -0x50(%rbp), %rdi
movq %r15, %rsi
callq 0x1333
movq -0x50(%rbp), %r13
movq %r13, %rdi
movq %r12, %rsi
callq 0x1376
leaq 0xa90(%rip), %rdi       # 0x20d2
movq %r13, %rsi
xorl %eax, %eax
callq 0x1060
movq %r12, %rdi
callq 0x1050
movl -0x2c(%rbp), %r9d
movl %ebx, %edi
movq %r12, %rsi
movq %rax, %rdx
xorl %ecx, %ecx
movq %r14, %r8
callq 0x1080
cmpl $-0x1, %eax
je 0x16e6
movq -0x10a0(%rbp), %rax
movabsq $0x20746f6e2070614d, %rcx # imm = 0x20746F6E2070614D
xorq %rcx, %rax
movq -0x109a(%rbp), %rcx
movabsq $0x646e756f662074, %rdx # imm = 0x646E756F662074
xorq %rdx, %rcx
orq %rax, %rcx
leaq 0xab4(%rip), %rdi       # 0x2155
jne 0x16bb
leaq 0xa76(%rip), %rdi       # 0x2120
movq %r13, %rsi
xorl %eax, %eax
callq 0x1060
leaq 0xabd(%rip), %rdi       # 0x2178
callq 0x1040
movl $0xf9f, %edx            # imm = 0xF9F
movl %ebx, %edi
movq %r15, %rsi
xorl %ecx, %ecx
movq %r14, %r8
leaq -0x2c(%rbp), %r9
callq 0x10c0
cmpl $-0x1, %eax
jne 0x1616
callq 0x11e8
callq 0x11d0