// File: /workspace/github_c_makefile_star2_O2/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 0xbf5(%rip), %rdi       # 0x2038
leaq 0xbf8(%rip), %rsi       # 0x2042
leaq -0x38(%rbp), %r14
movq %r14, %rcx
callq 0x1150
testl %eax, %eax
jne 0x14b4
leaq 0xbf8(%rip), %r15       # 0x2059
leaq 0xc00(%rip), %r12       # 0x2068
movq (%r14), %r14
testq %r14, %r14
je 0x14dd
movl 0x4(%r14), %edi
movl 0x8(%r14), %esi
movl 0xc(%r14), %edx
callq 0x1180
movq %r15, %rdi
cmpl $-0x1, %eax
je 0x14a9
movl %eax, %ebx
movq 0x18(%r14), %rsi
movl 0x10(%r14), %edx
movl %eax, %edi
callq 0x10d0
cmpl $-0x1, %eax
jne 0x150e
movl %ebx, %edi
callq 0x1090
movq %r12, %rdi
callq 0x10f0
addq $0x28, %r14
jmp 0x1468
movq 0x2b3d(%rip), %rcx      # 0x3ff8
movq (%rcx), %rbx
movl %eax, %edi
callq 0x1070
leaq 0xb7c(%rip), %rsi       # 0x2048
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x10a0
pushq $0x1
jmp 0x14fb
movq 0x2b14(%rip), %rax      # 0x3ff8
movq (%rax), %rcx
leaq 0xb87(%rip), %rdi       # 0x2075
pushq $0x1e
popq %rsi
pushq $0x1
popq %rdx
callq 0x1140
pushq $0x2
popq %rax
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), %rax
movl $0x80, (%rax)
leaq 0xb6c(%rip), %rdi       # 0x2094
leaq 0xb13(%rip), %rsi       # 0x2042
xorl %eax, %eax
callq 0x1060
leaq -0x10a0(%rbp), %r15
leaq -0x100(%rbp), %r12
leaq -0x2040(%rbp), %r14
movl $0xf9f, %edx            # imm = 0xF9F
movl %ebx, %edi
movq %r15, %rsi
xorl %ecx, %ecx
movq %r12, %r8
leaq -0x2c(%rbp), %r9
callq 0x10b0
cmpl $-0x1, %eax
je 0x1604
cltq
movb $0x0, -0x10a0(%rbp,%rax)
leaq -0x50(%rbp), %rdi
movq %r15, %rsi
callq 0x12a3
movq -0x50(%rbp), %r13
movq %r13, %rdi
movq %r14, %rsi
callq 0x12d4
leaq 0xb3a(%rip), %rdi       # 0x20d2
movq %r13, %rsi
xorl %eax, %eax
callq 0x1060
movq %r14, %rdi
callq 0x1050
movl -0x2c(%rbp), %r9d
movl %ebx, %edi
movq %r14, %rsi
movq %rax, %rdx
xorl %ecx, %ecx
movq %r12, %r8
callq 0x1080
cmpl $-0x1, %eax
je 0x160d
movq %r14, %rdi
leaq 0xa5b(%rip), %rsi       # 0x202a
pushq $0xe
popq %rdx
callq 0x10c0
leaq 0xb77(%rip), %rdi       # 0x2155
testl %eax, %eax
jne 0x15fa
leaq 0xb37(%rip), %rdi       # 0x2120
movq %r13, %rsi
xorl %eax, %eax
callq 0x1060
leaq 0xb7e(%rip), %rdi       # 0x2178
callq 0x1040
jmp 0x154b
leaq 0xabe(%rip), %rdi       # 0x20c9
jmp 0x1614
leaq 0xafb(%rip), %rdi       # 0x210f
callq 0x10f0
pushq $0x1
popq %rdi
callq 0x1130