// File: /workspace/github_c_makefile_star2_O1/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 0x815(%rip), %rdi       # 0x2004
leaq -0x38(%rbp), %rcx
movq %r15, %rsi
callq 0x11f0
testl %eax, %eax
jne 0x190b
movq %rbx, -0x30(%rbp)
movq -0x38(%rbp), %rbx
testq %rbx, %rbx
je 0x183b
leaq 0x858(%rip), %r13       # 0x206f
movl 0x4(%rbx), %edi
movl 0x8(%rbx), %esi
movl 0xc(%rbx), %edx
callq 0x1220
cmpl $-0x1, %eax
jne 0x1845
movq %r13, %rdi
callq 0x1190
movq 0x28(%rbx), %rbx
testq %rbx, %rbx
jne 0x1817
callq 0x1392
jmp 0x18f9
movq %r12, %rdi
callq 0x10a0
movq 0x18(%rbx), %r8
movl 0x10(%rbx), %r9d
movl %r14d, %edi
movq %r12, %rsi
movq %rax, %rdx
xorl %ecx, %ecx
callq 0x10f0
cmpl $-0x1, %eax
je 0x1914
leaq 0x839(%rip), %rsi       # 0x20ae
movq %r15, %rdi
callq 0x1120
testl %eax, %eax
movq -0x30(%rbp), %rbx
je 0x18aa
leaq 0x86a(%rip), %rsi       # 0x20f6
movq %r15, %rdi
callq 0x1120
testl %eax, %eax
leaq 0x85f(%rip), %rax       # 0x20fc
leaq 0x894(%rip), %rdi       # 0x2138
cmoveq %rax, %rdi
jmp 0x18b1
leaq 0x803(%rip), %rdi       # 0x20b4
leaq 0x838(%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 0x1919
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
movl %eax, %edi
callq 0x1332
jmp 0x18f9
callq 0x137a
callq 0x1362