// File: /workspace/github_cpp_makefile_star2_O3/dcarrey[P]hgt-detection/./fonctions.cpp
// Function: formatResult(HGT*, int, HGT*, InputTree)
int formatResult(struct HGT *tabHGT,int nbHGT, struct HGT *outHGT,struct InputTree aTree){

	int nbSommet,i,j,nbTrans,sommet,tmp=1,cpt=-1,nbHGTRet=0;
	int racine;
	double max;
	int nbArete = 2*(aTree.size)-3-aTree.kt,sequence=1,sequence_old=0;

	struct DescTree *tabDetect  = (struct DescTree*)malloc((2*(nbHGT+1))*sizeof(struct DescTree));

	//res = fopen(result,"r");

	//==lecture des transferts

	//printf("\nHGT-DETECTION : nombre de HGT avant formatRes %d",nbHGT);
	
	for(i=1;i<=nbHGT;i++){
		
		if(tabHGT[i].valide >= 1){
			nbHGTRet++;
			/*sequence_old = tabHGT[i].sequence;
			
			outHGT[nbHGTRet].valide = 1;
			if(tabHGT[i].sequence != tabHGT[i-1].sequence)
					sequence ++;
			}*/
			
			if(sequence_old == 0){
				sequence = 1;
			}else{
				if(tabHGT[i].sequence != sequence_old){
					sequence ++;
				}
			}
			sequence_old = tabHGT[i].sequence;
			outHGT[nbHGTRet].sequence = sequence;
			
			copyHGT(tabHGT[i],&outHGT[nbHGTRet]);
			cpt++;
			nbSommet = tabDetect[cpt].nbSommet = tabHGT[i].listSource[0];
			
			//printf("\ni=%d",i);
			tabDetect[cpt].Tableau = (int*)malloc(nbSommet*sizeof(int));
			//printf("\n");
			for(j=0;j<nbSommet;j++){
				tabDetect[cpt].Tableau[j] = tabHGT[i].listSource[j+1];
			//	printf("%d ",tabHGT[i].listSource[j+1]);
			}
			//printf("\ni=%d",i);
			//if(i==73) {printf("\nOn sort nbSommet=%d",nbSommet); exit(1);}
			cpt++;
			nbSommet = tabDetect[cpt].nbSommet = tabHGT[i].listDestination[0];
			tabDetect[cpt].Tableau = (int*)malloc(nbSommet*sizeof(int));
			//printf("\n");
			for(j=0;j<nbSommet;j++){
				tabDetect[cpt].Tableau[j] = tabHGT[i].listDestination[j+1];
				//printf("%d ",tabHGT[i].listDestination[j+1]);
			}
		}
		else{
			printf("\nhgt : Transfer %d was found to be idle",i);
		}
	}

	outHGT[nbHGTRet+1].valide=0;
	for(int i=1;i<=nbHGTRet;i++){
		outHGT[i].source_Ar = outHGT[i].source_A;
		outHGT[i].source_Br = outHGT[i].source_B;
		outHGT[i].dest_Ar = outHGT[i].dest_A;
		outHGT[i].dest_Br = outHGT[i].dest_B;
	}
/*
	nbTrans=-1;
	do{
		val=fscanf(res,"%d",&nbSommet);
		if(val != -1){
			printf("<br>");
			nbTrans++;
			tabDetect[nbTrans].nbSommet=nbSommet;
			tabDetect[nbTrans].Tableau = (int*)malloc(nbSommet*sizeof(int));
			for(j=0;j<nbSommet;j++){
				fscanf(res,"%d",&val);
				printf("%d ",val);
				tabDetect[nbTrans].Tableau[j] = val;
			}
		}
	}while(val!=-1);*/
	
	nbTrans = cpt;

//	printf("\n");
	supprimerSousEnsemble(tabDetect,nbTrans);

/*	for(i=0;i<=nbTrans;i++){
		printf("\n");
		for(j=0;j<tabDetect[i].nbSommet;j++){
			printf("%d ",tabDetect[i].Tableau[j]);
		}
	}*/

//	fclose(res);

//	if(addRoot) printf("<br>Racine : %d",n);
//	else  printf("<br>Racine : %d--%d",R1,R2);

	//recherche des aretes au dessus des sous-arbres

//	if(addRoot) racine = n; else racine = R1;

	racine = aTree.Root;
	

	for(i=0;i<=nbTrans;i++){
		max=INFINI;
		sommet=tabDetect[i].Tableau[0];
//		printf("<br> ARETE :"); 
		if(tabDetect[i].nbSommet > 1){
			// calcul de la distance entre la racine et le point d'intersection de tous les sommets
			for(j=1;j<tabDetect[i].nbSommet;j++){
			
				if(max > ( (aTree.ADD[racine][sommet] + aTree.ADD[racine][tabDetect[i].Tableau[j]] - aTree.ADD[sommet][tabDetect[i].Tableau[j]]) / 2.0 ) )
					max = (aTree.ADD[racine][sommet] + aTree.ADD[racine][tabDetect[i].Tableau[j]] - aTree.ADD[sommet][tabDetect[i].Tableau[j]]) / 2.0 ;  
			}
		
			// recherche du sommet
			for(j=1;j<=nbArete;j++){
				if( ((fabs(max - aTree.ADD[racine][aTree.ARETE[2*j-1]]) < 0.00001) && (aTree.ADD[racine][aTree.ARETE[2*j-2]] < aTree.ADD[racine][aTree.ARETE[2*j-1]]) && fabs(aTree.ADD[racine][sommet] - aTree.ADD[racine][aTree.ARETE[2*j-1]] - aTree.ADD[sommet][aTree.ARETE[2*j-1]])< 0.00001) ||
					((fabs(max - aTree.ADD[racine][aTree.ARETE[2*j-2]]) < 0.00001) && (aTree.ADD[racine][aTree.ARETE[2*j-1]] < aTree.ADD[racine][aTree.ARETE[2*j-2]]) && fabs(aTree.ADD[racine][sommet] - aTree.ADD[racine][aTree.ARETE[2*j-2]] - aTree.ADD[sommet][aTree.ARETE[2*j-2]])< 0.00001)  ) {
				
					if( fabs(aTree.ADD[racine][sommet] - aTree.ADD[racine][aTree.ARETE[2*j-1]] - aTree.ADD[aTree.ARETE[2*j-1]][sommet]) < 0.00001 ){		
						if(i % 2 == 0){
							outHGT[tmp].source_A = aTree.ARETE[2*j-1];
							outHGT[tmp].source_B = aTree.ARETE[2*j-2];
						}
						else{
							outHGT[tmp].dest_A = aTree.ARETE[2*j-1];
							outHGT[tmp].dest_B = aTree.ARETE[2*j-2];
							tmp++;
						}
	//					printf(" %d--%d (%d)",ARETE[2*j-1],ARETE[2*j-2],nbTrans);
					}
				}
			}
		}
		else{
			for(j=1;j<=nbArete;j++){
				if((aTree.ARETE[2*j-1] == sommet) || (aTree.ARETE[2*j-2] == sommet)){
					if(i % 2 == 0){
						outHGT[tmp].source_A = aTree.ARETE[2*j-1];
						outHGT[tmp].source_B = aTree.ARETE[2*j-2];
					}
					else{
						outHGT[tmp].dest_A = aTree.ARETE[2*j-1];
						outHGT[tmp].dest_B = aTree.ARETE[2*j-2];
						tmp++;
					}
	//				printf(" %d--%d (%d)",ARETE[2*j-1],ARETE[2*j-2],nbTrans);
				}
					
			}
		}
	}

	for(i=0;i<=cpt;i++){
		free(tabDetect[i].Tableau);
	}
	free(tabDetect);
	
	return nbHGTRet;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xf8, %rsp
movq %rdx, %r12
movl %esi, %r13d
movq %rdi, -0x48(%rbp)
movl 0x10(%rbp), %r14d
movl 0x58(%rbp), %ebx
leal 0x2(,%r13,2), %eax
cltq
shlq $0x3, %rax
leaq (%rax,%rax,2), %rdi
callq 0x1140
movq %rax, %r15
testl %r13d, %r13d
jle 0x11780
movq %r15, -0x60(%rbp)
addl %r14d, %r14d
subl %ebx, %r14d
movq %r14, -0x78(%rbp)
leal -0x3(%r14), %eax
movl %eax, -0x3c(%rbp)
incl %r13d
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movq %rax, -0x38(%rbp)
movl $0x1, %esi
xorl %ebx, %ebx
movl $0x1, -0x58(%rbp)
xorl %r14d, %r14d
movq %r12, -0x68(%rbp)
movq %r13, %r15
movq -0x48(%rbp), %rdi
movq %r13, -0x70(%rbp)
imulq $0xa8, %rsi, %r13
cmpl $0x0, (%rdi,%r13)
jle 0x11494
movq %rsi, -0x50(%rbp)
addq %rdi, %r13
incl %r14d
movl 0x94(%r13), %ecx
xorl %eax, %eax
movl %ecx, -0x40(%rbp)
cmpl %ebx, %ecx
setne %al
movl -0x58(%rbp), %edx
addl %eax, %edx
testl %ebx, %ebx
movl $0x1, %eax
cmovel %eax, %edx
movq %r14, -0x30(%rbp)
movslq %r14d, %rax
imulq $0xa8, %rax, %rcx
leaq (%r12,%rcx), %rax
movl %edx, -0x58(%rbp)
movl %edx, 0x94(%r12,%rcx)
movl $0x15, %ecx
movq %rsp, %rdi
movq %r13, %rsi
rep  movsq (%rsi), %es:(%rdi)
movq %rax, %rdi
callq 0xab1a
movq 0x10(%r13), %rbx
movslq (%rbx), %r15
movslq -0x38(%rbp), %rax
leaq (%rax,%rax,2), %r14
movq -0x60(%rbp), %r12
movl %r15d, 0x18(%r12,%r14,8)
leaq (,%r15,4), %rdi
callq 0x1140
movq %rax, 0x28(%r12,%r14,8)
testq %r15, %r15
jle 0x11433
xorl %ecx, %ecx
movl 0x4(%rbx,%rcx,4), %edx
movl %edx, (%rax,%rcx,4)
leaq 0x1(%rcx), %rdx
movq %rdx, %rcx
cmpq %rdx, %r15
jne 0x11420
movq -0x38(%rbp), %rax
addl $0x2, %eax
movq 0x18(%r13), %rbx
movslq (%rbx), %r15
movq %rax, -0x38(%rbp)
cltq
leaq (%rax,%rax,2), %r14
movq -0x60(%rbp), %r12
movl %r15d, (%r12,%r14,8)
leaq (,%r15,4), %rdi
callq 0x1140
movq %rax, 0x10(%r12,%r14,8)
testq %r15, %r15
jle 0x114ab
xorl %ecx, %ecx
movq -0x30(%rbp), %r14
movq -0x48(%rbp), %rdi
movl 0x4(%rbx,%rcx,4), %edx
movl %edx, (%rax,%rcx,4)
leaq 0x1(%rcx), %rdx
movq %rdx, %rcx
cmpq %rdx, %r15
jne 0x11474
movl -0x40(%rbp), %ebx
movq -0x68(%rbp), %r12
movq -0x70(%rbp), %r15
jmp 0x114be
leaq 0x5527(%rip), %rdi      # 0x169c2
movq %rsi, %r13
xorl %eax, %eax
callq 0x1030
movq -0x48(%rbp), %rdi
jmp 0x114c2
movl -0x40(%rbp), %ebx
movq -0x30(%rbp), %r14
movq -0x68(%rbp), %r12
movq -0x70(%rbp), %r15
movq -0x48(%rbp), %rdi
movq -0x50(%rbp), %r13
incq %r13
cmpq %r15, %r13
movq %r13, %rsi
jne 0x11380
movslq %r14d, %rax
imulq $0xa8, %rax, %rcx
movl $0x0, 0xa8(%r12,%rcx)
testl %eax, %eax
jle 0x1151e
leal 0x1(%r14), %eax
imulq $0xa8, %rax, %rax
addq $-0xa8, %rax
xorl %ecx, %ecx
movupd 0x128(%r12,%rcx), %xmm0
movupd %xmm0, 0x140(%r12,%rcx)
addq $0xa8, %rcx
cmpq %rcx, %rax
jne 0x114fe
movq -0x60(%rbp), %r15
movq %r15, %rdi
movq -0x38(%rbp), %rbx
movl %ebx, %esi
callq 0x11287
testl %ebx, %ebx
movq -0x78(%rbp), %rdi
js 0x1179c
movq %r14, -0x30(%rbp)
leaq 0x10(%rbp), %rdx
movslq 0x20(%rdx), %rax
movq %rax, -0x58(%rbp)
movq 0x8(%rdx), %rcx
movq 0x30(%rdx), %rdx
addl $-0x2, %edi
leal 0x1(%rbx), %esi
decq %rdi
leaq 0x8(%rdx), %rax
movq %rax, -0x50(%rbp)
movl $0x1, %r8d
xorl %r9d, %r9d
movsd 0x3ad2(%rip), %xmm0     # 0x15048
movsd 0x3a8a(%rip), %xmm1     # 0x15008
movapd 0x3afa(%rip), %xmm2     # 0x15080
movsd 0x3a9a(%rip), %xmm3     # 0x15028
leaq (%r9,%r9,2), %r11
movq 0x10(%r15,%r11,8), %rax
movslq (%rax), %r10
movslq (%r15,%r11,8), %rbx
cmpq $0x1, %rbx
jle 0x116dd
movq -0x58(%rbp), %r11
movq (%rcx,%r11,8), %r11
movsd (%r11,%r10,8), %xmm4
movq (%rcx,%r10,8), %r14
movl $0x1, %r15d
movapd %xmm0, %xmm5
movapd %xmm5, %xmm6
movslq (%rax,%r15,4), %r12
movsd (%r11,%r12,8), %xmm5
addsd %xmm4, %xmm5
subsd (%r14,%r12,8), %xmm5
mulsd %xmm1, %xmm5
minsd %xmm6, %xmm5
incq %r15
cmpq %r15, %rbx
jne 0x115c4
movq %rdi, %r12
movq -0x50(%rbp), %r13
cmpl $0x0, -0x3c(%rbp)
jle 0x1173e
movq -0x8(%r13), %rbx
movq (%r13), %rax
movsd (%r11,%rax,8), %xmm6
movapd %xmm5, %xmm7
subsd %xmm6, %xmm7
andpd %xmm2, %xmm7
ucomisd %xmm7, %xmm3
movsd (%r11,%rbx,8), %xmm7
jbe 0x11645
ucomisd %xmm7, %xmm6
jbe 0x11645
movapd %xmm4, %xmm8
subsd %xmm6, %xmm8
subsd (%r14,%rax,8), %xmm8
andpd %xmm2, %xmm8
ucomisd %xmm8, %xmm3
ja 0x1167d
movapd %xmm5, %xmm8
subsd %xmm7, %xmm8
andpd %xmm2, %xmm8
ucomisd %xmm8, %xmm3
jbe 0x116ce
ucomisd %xmm6, %xmm7
jbe 0x116ce
movapd %xmm4, %xmm8
subsd %xmm7, %xmm8
subsd (%r14,%rbx,8), %xmm8
andpd %xmm2, %xmm8
ucomisd %xmm8, %xmm3
jbe 0x116ce
movapd %xmm4, %xmm7
subsd %xmm6, %xmm7
movq (%rcx,%rax,8), %r15
subsd (%r15,%r10,8), %xmm7
andpd %xmm2, %xmm7
ucomisd %xmm7, %xmm3
jbe 0x116ce
movslq %r8d, %r15
imulq $0xa8, %r15, %r15
addq -0x68(%rbp), %r15
testb $0x1, %r9b
jne 0x116bd
movl %eax, 0x80(%r15)
movl %ebx, 0x84(%r15)
jmp 0x116ce
movl %eax, 0x88(%r15)
movl %ebx, 0x8c(%r15)
incl %r8d
addq $0x10, %r13
decq %r12
jne 0x115fd
jmp 0x1173e
movq %rdi, %rax
movq %rdx, %r11
cmpl $0x0, -0x3c(%rbp)
movq -0x68(%rbp), %r15
jle 0x1173e
movq 0x8(%r11), %rbx
cmpq %r10, %rbx
je 0x116fb
cmpq %r10, (%r11)
jne 0x11735
movslq %r8d, %r14
imulq $0xa8, %r14, %r14
addq %r15, %r14
testb $0x1, %r9b
jne 0x11721
movl %ebx, 0x80(%r14)
movl (%r11), %ebx
movl %ebx, 0x84(%r14)
jmp 0x11735
movl %ebx, 0x88(%r14)
movl (%r11), %ebx
movl %ebx, 0x8c(%r14)
incl %r8d
addq $0x10, %r11
decq %rax
jne 0x116ed
incq %r9
cmpq %rsi, %r9
movq -0x60(%rbp), %r15
jne 0x1158e
movq -0x38(%rbp), %rax
testl %eax, %eax
js 0x1177a
movl %eax, %eax
shlq $0x3, %rax
leaq (%rax,%rax,2), %rbx
addq $0x18, %rbx
xorl %r14d, %r14d
movq 0x10(%r15,%r14), %rdi
callq 0x1100
addq $0x18, %r14
cmpq %r14, %rbx
jne 0x11767
movq -0x30(%rbp), %r14
jmp 0x1179c
movl $0x0, 0xa8(%r12)
movq %r15, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x11287
xorl %r14d, %r14d
movq %r15, %rdi
callq 0x1100
movl %r14d, %eax
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq