// File: /workspace/github_c_makefile_star2_O3/MAGI003769[P]EE450-Socket-Project/serverA.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 backend server itself
    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 A 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 A 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 A does not have the required graph id %s\n", args[0]);
            printf("The Server A has sent \"Graph not Found\" to AWS\n");
        } else {
            printf("The Server A 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 0xb43(%rip), %rdi       # 0x2038
leaq 0xb46(%rip), %rsi       # 0x2042
leaq -0x38(%rbp), %rcx
callq 0x1140
testl %eax, %eax
jne 0x1573
movq -0x38(%rbp), %r14
testq %r14, %r14
je 0x1567
leaq 0xb40(%rip), %r15       # 0x2059
leaq 0xb48(%rip), %r12       # 0x2068
movl 0x4(%r14), %edi
movl 0x8(%r14), %esi
movl 0xc(%r14), %edx
callq 0x1170
movq %r15, %rdi
cmpl $-0x1, %eax
je 0x1559
movl %eax, %ebx
movq 0x18(%r14), %rsi
movl 0x10(%r14), %edx
movl %eax, %edi
callq 0x10c0
cmpl $-0x1, %eax
jne 0x1591
movl %ebx, %edi
callq 0x1090
movq %r12, %rdi
callq 0x10e0
movq 0x28(%r14), %r14
testq %r14, %r14
jne 0x1520
callq 0x11f0
movl $0x2, %eax
jmp 0x157f
movl %eax, %edi
callq 0x1190
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 0x1160
leaq -0x2c(%rbp), %r14
movl $0x80, (%r14)
leaq 0xae8(%rip), %rdi       # 0x2094
leaq 0xa8f(%rip), %rsi       # 0x2042
xorl %eax, %eax
callq 0x1060
leaq -0x2040(%rbp), %rsi
leaq -0x100(%rbp), %r8
movl $0xf9f, %edx            # imm = 0xF9F
movl %ebx, %edi
xorl %ecx, %ecx
movq %r14, %r9
callq 0x10b0
cmpl $-0x1, %eax
je 0x16c2
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 0x1323
movq -0x50(%rbp), %r13
movq %r13, %rdi
movq %r12, %rsi
callq 0x1366
leaq 0xaaf(%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 0x16c7
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 0xad3(%rip), %rdi       # 0x2155
jne 0x169c
leaq 0xa95(%rip), %rdi       # 0x2120
movq %r13, %rsi
xorl %eax, %eax
callq 0x1060
leaq 0xadc(%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 0x10b0
cmpl $-0x1, %eax
jne 0x15f7
callq 0x11d8
callq 0x11c0