// File: /workspace/github_c_makefile_star2_O0/MAGI003769[P]EE450-Socket-Project/aws.c
// Function: udpQuery2
void udpQuery2(int sockfd, char *query, char *port, char *data) {
    //int server_sock;
    int numbytes;
    int rv;
    struct addrinfo hints, *servinfo, *p;
	socklen_t addr_len;
	memset(&hints, 0, sizeof hints);
    char recv_data[MAXBUFLEN]; // data received from backend server
    
    //sockfd = setupUDP(query, port);
    if ((rv = getaddrinfo(IPADDRESS, port, &hints, &servinfo)) != 0) {
		fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
		return;
	}

    for(p = servinfo; p != NULL; p = p->ai_next) {
		if ((socket(p->ai_family, p->ai_socktype,
				p->ai_protocol)) == -1) {
			perror("talker: socket");
			continue;
		}
        break;
    }

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

    //printf("debug: find backend info...\n");
    
    // send map_id, source_vtx and target_vtx to server
	if ((numbytes = sendto(sockfd, query, strlen(query), 0,
			 p->ai_addr, p->ai_addrlen)) == -1) {
		perror("talker: sendto");
		exit(1);
	}

    //printf("debug: aws has sent query...\n");

	if (strcmp(port, SERVERPORTA)==0) {
		printf("The AWS has sent map ID to server A using UDP over port %s\n", UDPPORT);
	} else if (strcmp(port, SERVERPORTB)==0) {
		printf("The AWS has sent map ID to server B using UDP over port %s\n", UDPPORT);
    } else {
        printf("The AWS has sent map, source ID, destination ID, propagation speed and transmission speed to server C using UDP over port %s\n", UDPPORT);
    }

    int recv_bytes;

    recv_bytes = recvfrom(sockfd, recv_data, sizeof recv_data, 0, NULL, NULL);
    if(recv_bytes == -1) {
        perror("recvfrom");
        exit(1);
    }
    recv_data[recv_bytes] = '\0';
    //printf("debug: AWS receives UDP: %s\n", recv_data);
    //printf("debug: AWS receives UDP data length: %d\n", recv_bytes);
    //printf("debug: AWS receives UDP bytes: %d\n", recv_bytes);
    //close(sockfd);
    strcpy(data, recv_data);
    //printf("debug: %s\n", data);
    //return return_recv_data;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x1030, %rsp           # imm = 0x1030
movl %edi, -0x4(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
leaq -0x58(%rbp), %rdi
xorl %esi, %esi
movl $0x30, %edx
callq 0x10d0
movq -0x18(%rbp), %rsi
leaq 0x17f1(%rip), %rdi      # 0x3006
leaq -0x58(%rbp), %rdx
leaq -0x60(%rbp), %rcx
callq 0x11d0
movl %eax, -0x28(%rbp)
cmpl $0x0, %eax
je 0x1860
movq 0x37c7(%rip), %rax      # 0x4ff8
movq (%rax), %rax
movq %rax, -0x1020(%rbp)
movl -0x28(%rbp), %edi
callq 0x10c0
movq -0x1020(%rbp), %rdi
movq %rax, %rdx
leaq 0x17bc(%rip), %rsi      # 0x3010
movb $0x0, %al
callq 0x1120
jmp 0x1a03
movq -0x60(%rbp), %rax
movq %rax, -0x68(%rbp)
cmpq $0x0, -0x68(%rbp)
je 0x18ac
movq -0x68(%rbp), %rax
movl 0x4(%rax), %edi
movq -0x68(%rbp), %rax
movl 0x8(%rax), %esi
movq -0x68(%rbp), %rax
movl 0xc(%rax), %edx
callq 0x1200
cmpl $-0x1, %eax
jne 0x189c
leaq 0x17dc(%rip), %rdi      # 0x3071
callq 0x1180
jmp 0x189e
jmp 0x18ac
movq -0x68(%rbp), %rax
movq 0x28(%rax), %rax
movq %rax, -0x68(%rbp)
jmp 0x1868
cmpq $0x0, -0x68(%rbp)
jne 0x18d0
movq 0x373e(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
leaq 0x17bc(%rip), %rsi      # 0x3080
movb $0x0, %al
callq 0x1120
jmp 0x1a03
movl -0x4(%rbp), %eax
movl %eax, -0x102c(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x1028(%rbp)
movq -0x10(%rbp), %rdi
callq 0x1090
movl -0x102c(%rbp), %edi
movq -0x1028(%rbp), %rsi
movq %rax, %rdx
movq -0x68(%rbp), %rax
movq 0x18(%rax), %r8
movq -0x68(%rbp), %rax
movl 0x10(%rax), %r9d
xorl %ecx, %ecx
callq 0x10e0
movl %eax, -0x24(%rbp)
cmpl $-0x1, %eax
jne 0x1932
leaq 0x177e(%rip), %rdi      # 0x30a1
callq 0x1180
movl $0x1, %edi
callq 0x11c0
movq -0x18(%rbp), %rdi
leaq 0x1773(%rip), %rsi      # 0x30b0
callq 0x1110
cmpl $0x0, %eax
jne 0x195e
leaq 0x1768(%rip), %rdi      # 0x30b6
leaq 0x179d(%rip), %rsi      # 0x30f2
movb $0x0, %al
callq 0x10b0
jmp 0x19a1
movq -0x18(%rbp), %rdi
leaq 0x178f(%rip), %rsi      # 0x30f8
callq 0x1110
cmpl $0x0, %eax
jne 0x198a
leaq 0x1784(%rip), %rdi      # 0x30fe
leaq 0x1771(%rip), %rsi      # 0x30f2
movb $0x0, %al
callq 0x10b0
jmp 0x199f
leaq 0x17a9(%rip), %rdi      # 0x313a
leaq 0x175a(%rip), %rsi      # 0x30f2
movb $0x0, %al
callq 0x10b0
jmp 0x19a1
movl -0x4(%rbp), %edi
leaq -0x1010(%rbp), %rsi
movl $0xfa0, %edx            # imm = 0xFA0
xorl %ecx, %ecx
xorl %eax, %eax
movl %eax, %r9d
movq %r9, %r8
callq 0x1150
movl %eax, -0x1014(%rbp)
cmpl $-0x1, -0x1014(%rbp)
jne 0x19e4
leaq 0x17e3(%rip), %rdi      # 0x31b8
callq 0x1180
movl $0x1, %edi
callq 0x11c0
movslq -0x1014(%rbp), %rax
movb $0x0, -0x1010(%rbp,%rax)
movq -0x20(%rbp), %rdi
leaq -0x1010(%rbp), %rsi
callq 0x1060
addq $0x1030, %rsp           # imm = 0x1030
popq %rbp
retq
nopl (%rax)