// File: /workspace/github_c_makefile_star2_O2/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 0xbe5(%rip), %rdi       # 0x2038
leaq 0xbe8(%rip), %rsi       # 0x2042
leaq -0x38(%rbp), %r14
movq %r14, %rcx
callq 0x1160
testl %eax, %eax
jne 0x14c4
leaq 0xbe8(%rip), %r15       # 0x2059
leaq 0xbf0(%rip), %r12       # 0x2068
movq (%r14), %r14
testq %r14, %r14
je 0x14ed
movl 0x4(%r14), %edi
movl 0x8(%r14), %esi
movl 0xc(%r14), %edx
callq 0x1190
movq %r15, %rdi
cmpl $-0x1, %eax
je 0x14b9
movl %eax, %ebx
movq 0x18(%r14), %rsi
movl 0x10(%r14), %edx
movl %eax, %edi
callq 0x10e0
cmpl $-0x1, %eax
jne 0x151e
movl %ebx, %edi
callq 0x1090
movq %r12, %rdi
callq 0x1100
addq $0x28, %r14
jmp 0x1478
movq 0x2b2d(%rip), %rcx      # 0x3ff8
movq (%rcx), %rbx
movl %eax, %edi
callq 0x1070
leaq 0xb6c(%rip), %rsi       # 0x2048
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x10a0
pushq $0x1
jmp 0x150b
movq 0x2b04(%rip), %rax      # 0x3ff8
movq (%rax), %rcx
leaq 0xb77(%rip), %rdi       # 0x2075
pushq $0x1e
popq %rsi
pushq $0x1
popq %rdx
callq 0x1150
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 0x1180
leaq -0x2c(%rbp), %rax
movl $0x80, (%rax)
leaq 0xb5c(%rip), %rdi       # 0x2094
leaq 0xb03(%rip), %rsi       # 0x2042
xorl %eax, %eax
callq 0x1060
movq 0x2a8b(%rip), %rax      # 0x3fd8
movq (%rax), %rdi
callq 0x10b0
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 0x10c0
cmpl $-0x1, %eax
je 0x1623
cltq
movb $0x0, -0x10a0(%rbp,%rax)
leaq -0x50(%rbp), %rdi
movq %r15, %rsi
callq 0x12b3
movq -0x50(%rbp), %r13
movq %r13, %rdi
movq %r14, %rsi
callq 0x12e4
leaq 0xb1b(%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 0x162c
movq %r14, %rdi
leaq 0xa3c(%rip), %rsi       # 0x202a
pushq $0xe
popq %rdx
callq 0x10d0
leaq 0xb58(%rip), %rdi       # 0x2155
testl %eax, %eax
jne 0x1619
leaq 0xb18(%rip), %rdi       # 0x2120
movq %r13, %rsi
xorl %eax, %eax
callq 0x1060
leaq 0xb5f(%rip), %rdi       # 0x2178
callq 0x1040
jmp 0x156a
leaq 0xa9f(%rip), %rdi       # 0x20c9
jmp 0x1633
leaq 0xadc(%rip), %rdi       # 0x210f
callq 0x1100
pushq $0x1
popq %rdi
callq 0x1140