// File: CDISC2020[P]Projet_satellite_agile/src/ARINC/ARINC_Com.cpp
// Function: SamplingPort::ReadSamplingMsg(char*)
int SamplingPort::ReadSamplingMsg(char* M){

		// Setting bit masks
  		FD_ZERO(&readfds);
		FD_SET(s_d, &readfds);
  		FD_ZERO(&writefds);
  		FD_ZERO(&exceptfds);

        int lc_a = sizeof(c_a);
	char e_t[LNGMAX]; // input buffer for message
        int le_t; // number of characters received

	int replys=0;

	// Reading loop until last message
	int last =0;

	// Socket desc id
	printf("Socket desc id = %d\n", s_d);

	// Table size
	printf("desc table size = %d\n", nds);

	while (last==0) {
		
    		 if ((nfds = select(nds,&readfds, &writefds, &exceptfds ,&timeout)) == SOCK_ERR) {
			printf("TRACE : errno = %d\n", errno);
    			perror("S-> select");
    			bye(s_d);
		}
    		else
			if (nfds==0) { // No more message in input queue, timeout elapsed
                       		printf("TRACE : no more message in the input queue \n");
                        	last=1;
                        }
			else { // Yes a message was read! nfds != 0)
                			//printf("TRACE : nfds = %d\n",nfds);
                			//printf("TRACE : reception sur PORT1\n");
                			lc_a = sizeof(c_a); le_t=0;
                			if ((le_t = recvfrom(s_d,e_t,sizeof(e_t),0,(struct sockaddr *)&c_a,(socklen_t *)&lc_a)) == SOCK_ERR) {
                			perror("S-> recvfrom");
                			bye(s_d);
                			}
                			else {
						//printf("TRACE : S-> %d ! Longueur tampon = %d\n", replys, le_t);
                        			//printf("TRACE : S-> tampon = %s\n", e_t);
                        			h_emetteur = gethostbyaddr(&c_a.sin_addr,sizeof(&c_a.sin_addr),c_a.sin_family); 
                        			//printf("TRACE : S-> Emetteur = %s \n",h_emetteur->h_name);
                        			//printf("TRACE : S-> Adresse IP: "); DisplaySocketAddress(&c_a);
                        			replys++;
                     			}
      				}

	} // end of while

	// Now , it's time to return last message and its size
	strcpy(M,e_t); // return the content of the message

	return(le_t); // return size of message

}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x470, %rsp            # imm = 0x470
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x468(%rbp)
movq -0x468(%rbp), %rax
addq $0x4e0, %rax            # imm = 0x4E0
movq %rax, -0x20(%rbp)
movl $0x0, -0x14(%rbp)
movl -0x14(%rbp), %eax
cmpq $0x10, %rax
jae 0x3f99
movq -0x20(%rbp), %rax
movl -0x14(%rbp), %ecx
movq $0x0, (%rax,%rcx,8)
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
jmp 0x3f76
jmp 0x3f9b
movq -0x468(%rbp), %rax
movl 0x404(%rax), %eax
movl $0x40, %ecx
cltd
idivl %ecx
movq -0x468(%rbp), %rax
movl %edx, %ecx
movl $0x1, %edx
shlq %cl, %rdx
movq %rdx, -0x470(%rbp)
movl 0x404(%rax), %eax
movl $0x40, %ecx
cltd
idivl %ecx
movq -0x470(%rbp), %rdx
movl %eax, %ecx
movq -0x468(%rbp), %rax
movslq %ecx, %rcx
orq 0x4e0(%rax,%rcx,8), %rdx
movq %rdx, 0x4e0(%rax,%rcx,8)
movq -0x468(%rbp), %rax
addq $0x560, %rax            # imm = 0x560
movq %rax, -0x30(%rbp)
movl $0x0, -0x24(%rbp)
movl -0x24(%rbp), %eax
cmpq $0x10, %rax
jae 0x4034
movq -0x30(%rbp), %rax
movl -0x24(%rbp), %ecx
movq $0x0, (%rax,%rcx,8)
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x4011
jmp 0x4036
jmp 0x4038
movq -0x468(%rbp), %rax
addq $0x5e0, %rax            # imm = 0x5E0
movq %rax, -0x40(%rbp)
movl $0x0, -0x34(%rbp)
movl -0x34(%rbp), %eax
cmpq $0x10, %rax
jae 0x4073
movq -0x40(%rbp), %rax
movl -0x34(%rbp), %ecx
movq $0x0, (%rax,%rcx,8)
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x4050
jmp 0x4075
movq -0x468(%rbp), %rax
movl $0x10, -0x44(%rbp)
movl $0x0, -0x458(%rbp)
movl $0x0, -0x45c(%rbp)
movl 0x404(%rax), %esi
leaq 0x40d0(%rip), %rdi      # 0x8174
movb $0x0, %al
callq 0x3050
movq -0x468(%rbp), %rax
movl 0x4d8(%rax), %esi
leaq 0x40ca(%rip), %rdi      # 0x8189
movb $0x0, %al
callq 0x3050
cmpl $0x0, -0x45c(%rbp)
jne 0x4239
movq -0x468(%rbp), %r8
movl 0x4d8(%r8), %edi
movq %r8, %rsi
addq $0x4e0, %rsi            # imm = 0x4E0
movq %r8, %rdx
addq $0x560, %rdx            # imm = 0x560
movq %r8, %rcx
addq $0x5e0, %rcx            # imm = 0x5E0
addq $0x4c8, %r8             # imm = 0x4C8
callq 0x3270
movq -0x468(%rbp), %rcx
movl %eax, 0x4c4(%rcx)
cmpl $-0x1, %eax
jne 0x4155
callq 0x3040
movl (%rax), %esi
leaq 0x4074(%rip), %rdi      # 0x819f
movb $0x0, %al
callq 0x3050
leaq 0x407a(%rip), %rdi      # 0x81b3
callq 0x3200
movq -0x468(%rbp), %rax
movl 0x404(%rax), %edi
callq 0x38e0
jmp 0x4234
movq -0x468(%rbp), %rax
cmpl $0x0, 0x4c4(%rax)
jne 0x4182
leaq 0x4052(%rip), %rdi      # 0x81be
movb $0x0, %al
callq 0x3050
movl $0x1, -0x45c(%rbp)
jmp 0x4232
movq -0x468(%rbp), %r8
movl $0x10, -0x44(%rbp)
movl $0x0, -0x454(%rbp)
movl 0x404(%r8), %edi
leaq -0x450(%rbp), %rsi
addq $0x418, %r8             # imm = 0x418
movl $0x400, %edx            # imm = 0x400
xorl %ecx, %ecx
leaq -0x44(%rbp), %r9
callq 0x31d0
movl %eax, -0x454(%rbp)
cmpl $-0x1, %eax
jne 0x41ea
leaq 0x3f3f(%rip), %rdi      # 0x8110
callq 0x3200
movq -0x468(%rbp), %rax
movl 0x404(%rax), %edi
callq 0x38e0
jmp 0x4230
movq -0x468(%rbp), %rax
movq %rax, %rdi
addq $0x418, %rdi            # imm = 0x418
addq $0x4, %rdi
movzwl 0x418(%rax), %edx
movl $0x8, %esi
callq 0x3360
movq %rax, %rcx
movq -0x468(%rbp), %rax
movq %rcx, 0x4b8(%rax)
movl -0x458(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x458(%rbp)
jmp 0x4232
jmp 0x4234
jmp 0x40c6
movq -0x10(%rbp), %rdi
leaq -0x450(%rbp), %rsi
callq 0x3250
movl -0x454(%rbp), %eax
addq $0x470, %rsp            # imm = 0x470
popq %rbp
retq
nopl (%rax,%rax)