// File: /workspace/github_c_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xfe8, %rsp            # imm = 0xFE8
movq %rcx, %rbx
movq %rdx, %r15
movq %rsi, %r12
movl %edi, %r14d
xorps %xmm0, %xmm0
leaq -0x70(%rbp), %rdx
movaps %xmm0, (%rdx)
movaps %xmm0, 0x10(%rdx)
movaps %xmm0, 0x20(%rdx)
leaq 0x941(%rip), %rdi       # 0x2004
leaq -0x38(%rbp), %r13
movq %r15, %rsi
movq %r13, %rcx
callq 0x1200
testl %eax, %eax
jne 0x17dc
movq %rbx, -0x30(%rbp)
leaq 0x98a(%rip), %rbx       # 0x206f
movq (%r13), %r13
testq %r13, %r13
je 0x1803
movl 0x4(%r13), %edi
movl 0x8(%r13), %esi
movl 0xc(%r13), %edx
callq 0x1230
cmpl $-0x1, %eax
jne 0x1716
movq %rbx, %rdi
callq 0x11a0
addq $0x28, %r13
jmp 0x16e5
movq %r12, %rdi
callq 0x10a0
movq 0x18(%r13), %r8
movl 0x10(%r13), %r9d
movl %r14d, %edi
movq %r12, %rsi
movq %rax, %rdx
xorl %ecx, %ecx
callq 0x10f0
cmpl $-0x1, %eax
je 0x1821
leaq 0x968(%rip), %rsi       # 0x20ae
movq %r15, %rdi
callq 0x1120
testl %eax, %eax
movq -0x30(%rbp), %rbx
je 0x177b
leaq 0x999(%rip), %rsi       # 0x20f6
movq %r15, %rdi
callq 0x1120
testl %eax, %eax
leaq 0x98e(%rip), %rax       # 0x20fc
leaq 0x9c3(%rip), %rdi       # 0x2138
cmoveq %rax, %rdi
jmp 0x1782
leaq 0x932(%rip), %rdi       # 0x20b4
leaq 0x967(%rip), %rsi       # 0x20f0
xorl %eax, %eax
callq 0x10c0
leaq -0x1010(%rbp), %rsi
movl $0xfa0, %edx            # imm = 0xFA0
movl %r14d, %edi
xorl %ecx, %ecx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x1160
cmpl $-0x1, %eax
je 0x182a
cltq
movb $0x0, -0x1010(%rbp,%rax)
leaq -0x1010(%rbp), %rsi
movq %rbx, %rdi
callq 0x1060
addq $0xfe8, %rsp            # imm = 0xFE8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x2815(%rip), %rcx      # 0x3ff8
movq (%rcx), %rbx
movl %eax, %edi
callq 0x10d0
leaq 0x81a(%rip), %rsi       # 0x200e
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x1130
jmp 0x17ca
movq 0x27ee(%rip), %rax      # 0x3ff8
movq (%rax), %rcx
leaq 0x86a(%rip), %rdi       # 0x207e
pushq $0x20
popq %rsi
pushq $0x1
popq %rdx
callq 0x11f0
jmp 0x17ca
leaq 0x877(%rip), %rdi       # 0x209f
jmp 0x1831
leaq 0x985(%rip), %rdi       # 0x21b6
callq 0x11a0
pushq $0x1
popq %rdi
callq 0x11e0