// File: /workspace/github_cpp_makefile_star2_O2/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, -0x48(%rbp)
movl %esi, %r14d
movq %rdi, %r15
leaq 0x10(%rbp), %rax
movl (%rax), %ebx
movl 0x48(%rax), %eax
movl %eax, -0x50(%rbp)
addl %ebx, %ebx
leal 0x2(,%r14,2), %eax
cltq
imulq $0x18, %rax, %rdi
callq 0x1130
movq %rax, -0x38(%rbp)
xorl %eax, %eax
testl %r14d, %r14d
cmovlel %eax, %r14d
incl %r14d
movq %r14, -0x68(%rbp)
pushq $0x1
popq %rcx
pushq $-0x1
popq %rax
movl %eax, -0x2c(%rbp)
pushq $0x1
popq %rdx
xorl %r13d, %r13d
movl %ecx, -0x4c(%rbp)
xorl %r12d, %r12d
movq %r15, -0x60(%rbp)
movslq -0x2c(%rbp), %rax
movq %rax, -0x40(%rbp)
cmpq -0x68(%rbp), %rdx
je 0xfebd
imulq $0xa8, %rdx, %r14
cmpl $0x0, (%r15,%r14)
jle 0xfe9e
movq %rdx, -0x70(%rbp)
addq %r15, %r14
incl %r13d
movl 0x94(%r14), %ecx
xorl %eax, %eax
movl %ecx, -0x54(%rbp)
cmpl %r12d, %ecx
setne %al
movl -0x4c(%rbp), %esi
addl %eax, %esi
testl %r12d, %r12d
pushq $0x1
popq %rax
cmovel %eax, %esi
movl %r13d, -0x58(%rbp)
movslq %r13d, %rax
imulq $0xa8, %rax, %rcx
movq -0x48(%rbp), %rdx
leaq (%rdx,%rcx), %rax
movl %esi, -0x4c(%rbp)
movl %esi, 0x94(%rdx,%rcx)
pushq $0x15
popq %rcx
movq %rsp, %rdi
movq %r14, %rsi
rep  movsq (%rsi), %es:(%rdi)
movq %rax, %rdi
callq 0x9c4d
movq 0x10(%r14), %r12
movslq (%r12), %r13
imulq $0x18, -0x40(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0x38(%rbp), %r15
movl %r13d, 0x18(%r15,%rax)
movq %r13, %rdi
shlq $0x2, %rdi
callq 0x1130
movq -0x40(%rbp), %rcx
movq %rax, 0x28(%r15,%rcx)
testl %r13d, %r13d
movl $0x0, %ecx
cmovgl %r13d, %ecx
xorl %edx, %edx
cmpq %rdx, %rcx
je 0xfe3c
movl 0x4(%r12,%rdx,4), %esi
movl %esi, (%rax,%rdx,4)
incq %rdx
jmp 0xfe2a
movl -0x2c(%rbp), %eax
addl $0x2, %eax
movq 0x18(%r14), %r14
movslq (%r14), %r15
movl %eax, -0x2c(%rbp)
cltq
imulq $0x18, %rax, %r12
movq -0x38(%rbp), %r13
movl %r15d, (%r13,%r12)
movq %r15, %rdi
shlq $0x2, %rdi
callq 0x1130
movq %rax, 0x10(%r13,%r12)
testl %r15d, %r15d
movl $0x0, %ecx
cmovgl %r15d, %ecx
xorl %edx, %edx
movl -0x58(%rbp), %r13d
movq -0x60(%rbp), %r15
cmpq %rdx, %rcx
je 0xfe94
movl 0x4(%r14,%rdx,4), %esi
movl %esi, (%rax,%rdx,4)
incq %rdx
jmp 0xfe82
movl -0x54(%rbp), %r12d
movq -0x70(%rbp), %r14
jmp 0xfeb2
leaq 0x5b1d(%rip), %rdi      # 0x159c2
movq %rdx, %r14
movl %r14d, %esi
xorl %eax, %eax
callq 0x1030
incq %r14
movq %r14, %rdx
jmp 0xfd6b
movslq %r13d, %rax
imulq $0xa8, %rax, %rax
movq -0x48(%rbp), %rcx
andl $0x0, 0xa8(%rcx,%rax)
addl $-0x3, %ebx
xorl %eax, %eax
testl %r13d, %r13d
movl $0x0, %ecx
cmovgl %r13d, %ecx
imulq $0xa8, %rcx, %rcx
movq -0x48(%rbp), %rdx
cmpq %rax, %rcx
je 0xff0c
movups 0x128(%rdx,%rax), %xmm0
movups %xmm0, 0x140(%rdx,%rax)
addq $0xa8, %rax
jmp 0xfeef
subl -0x50(%rbp), %ebx
movq -0x38(%rbp), %rdi
movl -0x2c(%rbp), %esi
callq 0xfcbc
leaq 0x10(%rbp), %rsi
movslq 0x20(%rsi), %rax
movq 0x8(%rsi), %rcx
xorl %edx, %edx
testl %ebx, %ebx
cmovlel %edx, %ebx
movq 0x30(%rsi), %rsi
shlq $0x4, %rbx
pushq $0x1
popq %rdi
movsd 0x4107(%rip), %xmm0     # 0x14048
pushq $0x1
popq %r8
movapd 0x4133(%rip), %xmm1     # 0x14080
movsd 0x40d3(%rip), %xmm2     # 0x14028
movsd 0x40ab(%rip), %xmm3     # 0x14008
cmpq -0x40(%rbp), %rdx
jg 0x1011d
imulq $0x18, %rdx, %r11
movq -0x38(%rbp), %r14
movq 0x10(%r14,%r11), %r10
movslq (%r10), %r9
movslq (%r14,%r11), %r11
cmpq $0x1, %r11
jle 0xffc0
movq %r8, %r14
movapd %xmm0, %xmm4
cmpq %r14, %r11
je 0x1001d
movq (%rcx,%rax,8), %r15
movsd (%r15,%r9,8), %xmm5
movslq (%r10,%r14,4), %r12
addsd (%r15,%r12,8), %xmm5
movq (%rcx,%r9,8), %r15
subsd (%r15,%r12,8), %xmm5
mulsd %xmm3, %xmm5
minsd %xmm4, %xmm5
incq %r14
movapd %xmm5, %xmm4
jmp 0xff88
xorl %r10d, %r10d
cmpq %r10, %rbx
je 0x10115
movq 0x8(%rsi,%r10), %r11
cmpq %r9, %r11
je 0xffdc
cmpq %r9, (%rsi,%r10)
jne 0x10017
movslq %edi, %r14
imulq $0xa8, %r14, %r14
addq -0x48(%rbp), %r14
testb $0x1, %dl
jne 0x10003
movl %r11d, 0x80(%r14)
movl (%rsi,%r10), %r11d
movl %r11d, 0x84(%r14)
jmp 0x10017
movl %r11d, 0x88(%r14)
movl (%rsi,%r10), %r11d
movl %r11d, 0x8c(%r14)
incl %edi
addq $0x10, %r10
jmp 0xffc3
xorl %r10d, %r10d
cmpq %r10, %rbx
je 0x10115
movq (%rcx,%rax,8), %r12
movq (%rsi,%r10), %r11
movq 0x8(%rsi,%r10), %r14
movsd (%r12,%r14,8), %xmm5
movapd %xmm4, %xmm6
subsd %xmm5, %xmm6
andpd %xmm1, %xmm6
ucomisd %xmm6, %xmm2
movsd (%r12,%r11,8), %xmm6
jbe 0x1007f
ucomisd %xmm6, %xmm5
jbe 0x1007f
movsd (%r12,%r9,8), %xmm7
subsd %xmm5, %xmm7
movq (%rcx,%r9,8), %r15
movapd %xmm7, %xmm8
subsd (%r15,%r14,8), %xmm8
andpd %xmm1, %xmm8
ucomisd %xmm8, %xmm2
ja 0x100c1
movapd %xmm4, %xmm7
subsd %xmm6, %xmm7
andpd %xmm1, %xmm7
ucomisd %xmm7, %xmm2
jbe 0x1010c
ucomisd %xmm5, %xmm6
jbe 0x1010c
movsd (%r12,%r9,8), %xmm7
movapd %xmm7, %xmm8
subsd %xmm6, %xmm8
movq (%rcx,%r9,8), %r15
subsd (%r15,%r11,8), %xmm8
andpd %xmm1, %xmm8
ucomisd %xmm8, %xmm2
jbe 0x1010c
subsd %xmm5, %xmm7
movq (%rcx,%r14,8), %r15
subsd (%r15,%r9,8), %xmm7
andpd %xmm1, %xmm7
ucomisd %xmm7, %xmm2
jbe 0x1010c
movslq %edi, %r15
imulq $0xa8, %r15, %r12
addq -0x48(%rbp), %r12
testb $0x1, %dl
jne 0x100fa
movl %r14d, 0x80(%r12)
movl %r11d, 0x84(%r12)
jmp 0x1010c
movl %r14d, 0x88(%r12)
movl %r11d, 0x8c(%r12)
incl %edi
addq $0x10, %r10
jmp 0x10020
incq %rdx
jmp 0xff5d
movq -0x38(%rbp), %rbx
addq $0x10, %rbx
xorl %r14d, %r14d
cmpq -0x40(%rbp), %r14
jg 0x1013f
movq (%rbx), %rdi
callq 0x10f0
incq %r14
addq $0x18, %rbx
jmp 0x10128
movq -0x38(%rbp), %rdi
callq 0x10f0
movl %r13d, %eax
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq