// File: /workspace/github_c_makefile_star2_O0/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
subq $0x2090, %rsp           # imm = 0x2090
movl $0x0, -0x4(%rbp)
leaq -0x38(%rbp), %rdi
xorl %esi, %esi
movl $0x30, %edx
callq 0x1070
movl $0x0, -0x34(%rbp)
movl $0x2, -0x30(%rbp)
movl $0x1, -0x38(%rbp)
leaq 0xb6a(%rip), %rdi       # 0x2038
leaq 0xb6d(%rip), %rsi       # 0x2042
leaq -0x38(%rbp), %rdx
leaq -0x40(%rbp), %rcx
callq 0x1150
movl %eax, -0x4c(%rbp)
cmpl $0x0, %eax
je 0x1527
movq 0x2b07(%rip), %rax      # 0x3ff8
movq (%rax), %rax
movq %rax, -0x2078(%rbp)
movl -0x4c(%rbp), %edi
callq 0x1060
movq -0x2078(%rbp), %rdi
movq %rax, %rdx
leaq 0xb34(%rip), %rsi       # 0x2048
movb $0x0, %al
callq 0x10b0
movl $0x1, -0x4(%rbp)
jmp 0x174a
movq -0x40(%rbp), %rax
movq %rax, -0x48(%rbp)
cmpq $0x0, -0x48(%rbp)
je 0x15a8
movq -0x48(%rbp), %rax
movl 0x4(%rax), %edi
movq -0x48(%rbp), %rax
movl 0x8(%rax), %esi
movq -0x48(%rbp), %rax
movl 0xc(%rax), %edx
callq 0x1180
movl %eax, -0x8(%rbp)
cmpl $-0x1, %eax
jne 0x1566
leaq 0xafa(%rip), %rdi       # 0x2059
callq 0x1100
jmp 0x159a
movl -0x8(%rbp), %edi
movq -0x48(%rbp), %rax
movq 0x18(%rax), %rsi
movq -0x48(%rbp), %rax
movl 0x10(%rax), %edx
callq 0x10e0
cmpl $-0x1, %eax
jne 0x1598
movl -0x8(%rbp), %edi
callq 0x1090
leaq 0xad7(%rip), %rdi       # 0x2068
callq 0x1100
jmp 0x159a
jmp 0x15a8
movq -0x48(%rbp), %rax
movq 0x28(%rax), %rax
movq %rax, -0x48(%rbp)
jmp 0x152f
cmpq $0x0, -0x48(%rbp)
jne 0x15d3
movq 0x2a42(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0xab5(%rip), %rsi       # 0x2075
movb $0x0, %al
callq 0x10b0
movl $0x2, -0x4(%rbp)
jmp 0x174a
movq -0x40(%rbp), %rdi
callq 0x1170
movl $0x80, -0xd4(%rbp)
leaq 0xaa7(%rip), %rdi       # 0x2094
leaq 0xa4e(%rip), %rsi       # 0x2042
movb $0x0, %al
callq 0x1050
movq 0x29d6(%rip), %rax      # 0x3fd8
movq (%rax), %rdi
callq 0x10c0
movl -0x8(%rbp), %edi
leaq -0x10b0(%rbp), %rsi
movl $0xf9f, %edx            # imm = 0xF9F
xorl %ecx, %ecx
leaq -0xd0(%rbp), %r8
leaq -0xd4(%rbp), %r9
callq 0x10d0
movl %eax, -0x50(%rbp)
cmpl $-0x1, %eax
jne 0x164c
leaq 0xa8c(%rip), %rdi       # 0x20c9
callq 0x1100
movl $0x1, %edi
callq 0x1140
movslq -0x50(%rbp), %rax
movb $0x0, -0x10b0(%rbp,%rax)
leaq -0x2070(%rbp), %rdi
leaq -0x10b0(%rbp), %rsi
callq 0x12d0
movq -0x2070(%rbp), %rdi
leaq -0x2050(%rbp), %rsi
callq 0x1340
movq -0x2070(%rbp), %rsi
leaq 0xa46(%rip), %rdi       # 0x20d2
movb $0x0, %al
callq 0x1050
movl -0x8(%rbp), %eax
movl %eax, -0x2084(%rbp)
leaq -0x2050(%rbp), %rax
movq %rax, -0x2080(%rbp)
leaq -0x2050(%rbp), %rdi
callq 0x1040
movl -0x2084(%rbp), %edi
movq -0x2080(%rbp), %rsi
movq %rax, %rdx
movl -0xd4(%rbp), %r9d
xorl %ecx, %ecx
leaq -0xd0(%rbp), %r8
callq 0x1080
movl %eax, -0x50(%rbp)
cmpl $-0x1, -0x50(%rbp)
jne 0x16fa
leaq 0xa24(%rip), %rdi       # 0x210f
callq 0x1100
movl $0x1, %edi
callq 0x1140
leaq -0x2050(%rbp), %rdi
leaq 0x922(%rip), %rsi       # 0x202a
callq 0x10a0
cmpl $0x0, %eax
jne 0x1737
movq -0x2070(%rbp), %rsi
leaq 0xa00(%rip), %rdi       # 0x2120
movb $0x0, %al
callq 0x1050
leaq 0xa27(%rip), %rdi       # 0x2155
movb $0x0, %al
callq 0x1050
jmp 0x1745
leaq 0xa47(%rip), %rdi       # 0x2185
movb $0x0, %al
callq 0x1050
jmp 0x160a
movl -0x4(%rbp), %eax
addq $0x2090, %rsp           # imm = 0x2090
popq %rbp
retq