// File: /workspace/github_cpp_makefile_star2_O1/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 $0x108, %rsp            # imm = 0x108
movq %rdx, %rbx
movl %esi, %r15d
movq %rdi, -0x40(%rbp)
movl 0x10(%rbp), %eax
movq %rax, -0x80(%rbp)
movl 0x58(%rbp), %eax
movl %eax, -0x64(%rbp)
leal 0x2(,%r15,2), %eax
cltq
shlq $0x3, %rax
leaq (%rax,%rax,2), %rdi
callq 0x1140
movq %rax, -0x58(%rbp)
testl %r15d, %r15d
movq %rbx, -0x60(%rbp)
jle 0x114a1
incl %r15d
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
movl $0x1, -0x34(%rbp)
movl $0x1, %esi
movl $0x0, -0x30(%rbp)
xorl %r14d, %r14d
movq %r15, -0x70(%rbp)
movq -0x40(%rbp), %rdi
imulq $0xa8, %rsi, %r13
cmpl $0x0, (%rdi,%r13)
jle 0x11373
movq %rsi, -0x78(%rbp)
addq %rdi, %r13
movl -0x30(%rbp), %ecx
testl %ecx, %ecx
je 0x1138d
xorl %eax, %eax
cmpl %ecx, 0x94(%r13)
setne %al
movl -0x34(%rbp), %edx
addl %eax, %edx
jmp 0x11392
leaq 0x5648(%rip), %rdi      # 0x169c2
movq %rsi, %r13
xorl %eax, %eax
callq 0x1030
movq -0x40(%rbp), %rdi
jmp 0x11490
movl $0x1, %edx
incl %r14d
movl 0x94(%r13), %eax
movl %eax, -0x30(%rbp)
movq %r14, -0x48(%rbp)
movslq %r14d, %rax
imulq $0xa8, %rax, %rcx
leaq (%rbx,%rcx), %rax
movl %edx, -0x34(%rbp)
movl %edx, 0x94(%rbx,%rcx)
movl $0x15, %ecx
movq %rsp, %rdi
movq %r13, %rsi
rep  movsq (%rsi), %es:(%rdi)
movq %rax, %rdi
callq 0xab98
movq 0x10(%r13), %r15
movslq (%r15), %rbx
movq %r12, -0x50(%rbp)
movslq %r12d, %rax
leaq (%rax,%rax,2), %r14
movq -0x58(%rbp), %r12
movl %ebx, 0x18(%r12,%r14,8)
leaq (,%rbx,4), %rdi
callq 0x1140
movq %rax, 0x28(%r12,%r14,8)
testq %rbx, %rbx
jle 0x11419
xorl %ecx, %ecx
movl 0x4(%r15,%rcx,4), %edx
movl %edx, (%rax,%rcx,4)
leaq 0x1(%rcx), %rdx
movq %rdx, %rcx
cmpq %rdx, %rbx
jne 0x11405
movq -0x50(%rbp), %rax
addl $0x2, %eax
movq 0x18(%r13), %rbx
movslq (%rbx), %r15
movq %rax, %r13
cltq
leaq (%rax,%rax,2), %r14
movq -0x58(%rbp), %r12
movl %r15d, (%r12,%r14,8)
leaq (,%r15,4), %rdi
callq 0x1140
movq %rax, 0x10(%r12,%r14,8)
testq %r15, %r15
jle 0x11479
xorl %ecx, %ecx
movq -0x48(%rbp), %r14
movq -0x40(%rbp), %rdi
movl 0x4(%rbx,%rcx,4), %edx
movl %edx, (%rax,%rcx,4)
leaq 0x1(%rcx), %rdx
movq %rdx, %rcx
cmpq %rdx, %r15
jne 0x11459
movq -0x60(%rbp), %rbx
movq %r13, %r12
movq -0x70(%rbp), %r15
jmp 0x1148c
movq -0x60(%rbp), %rbx
movq -0x48(%rbp), %r14
movq %r13, %r12
movq -0x70(%rbp), %r15
movq -0x40(%rbp), %rdi
movq -0x78(%rbp), %r13
incq %r13
cmpq %r15, %r13
movq %r13, %rsi
jne 0x11344
jmp 0x114aa
xorl %r14d, %r14d
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
movslq %r14d, %rax
imulq $0xa8, %rax, %rcx
movl $0x0, 0xa8(%rbx,%rcx)
testl %eax, %eax
jle 0x114f4
leal 0x1(%r14), %eax
imulq $0xa8, %rax, %rax
addq $-0xa8, %rax
xorl %ecx, %ecx
movupd 0x128(%rbx,%rcx), %xmm0
movupd %xmm0, 0x140(%rbx,%rcx)
addq $0xa8, %rcx
cmpq %rcx, %rax
jne 0x114d6
movq %r14, -0x48(%rbp)
movq -0x58(%rbp), %r15
movq %r15, %rdi
movl %r12d, %esi
callq 0x1125a
movq %r12, -0x50(%rbp)
testl %r12d, %r12d
js 0x1173e
movq -0x80(%rbp), %rcx
addl %ecx, %ecx
subl -0x64(%rbp), %ecx
leaq 0x10(%rbp), %rsi
leal -0x3(%rcx), %eax
movslq 0x20(%rsi), %rdx
movq %rdx, -0x30(%rbp)
movq 0x8(%rsi), %rdx
movq 0x30(%rsi), %rsi
addl $-0x2, %ecx
movq -0x50(%rbp), %rdi
incl %edi
decq %rcx
movl $0x1, %r8d
xorl %r9d, %r9d
movsd 0x3af7(%rip), %xmm0     # 0x15048
movsd 0x3aaf(%rip), %xmm1     # 0x15008
movapd 0x3b1f(%rip), %xmm2     # 0x15080
movsd 0x3abf(%rip), %xmm3     # 0x15028
leaq (%r9,%r9,2), %rbx
movq 0x10(%r15,%rbx,8), %r11
movslq (%r11), %r10
movslq (%r15,%rbx,8), %r15
cmpq $0x1, %r15
jle 0x116ce
movq -0x30(%rbp), %rbx
movq (%rdx,%rbx,8), %rbx
movsd (%rbx,%r10,8), %xmm5
movq (%rdx,%r10,8), %r12
movl $0x1, %r13d
movapd %xmm0, %xmm4
movapd %xmm4, %xmm6
movslq (%r11,%r13,4), %r14
movsd (%rbx,%r14,8), %xmm4
addsd %xmm5, %xmm4
subsd (%r12,%r14,8), %xmm4
mulsd %xmm1, %xmm4
minsd %xmm6, %xmm4
incq %r13
cmpq %r13, %r15
jne 0x1159f
testl %eax, %eax
jle 0x1172e
movq -0x30(%rbp), %r11
movq (%rdx,%r11,8), %r11
movq %rcx, %r15
movq %rsi, %r12
movq 0x8(%r12), %r13
movsd (%r11,%r13,8), %xmm5
movapd %xmm4, %xmm6
subsd %xmm5, %xmm6
andpd %xmm2, %xmm6
ucomisd %xmm6, %xmm3
jbe 0x11624
movq (%r12), %rbx
ucomisd (%r11,%rbx,8), %xmm5
jbe 0x11624
movsd (%r11,%r10,8), %xmm6
subsd %xmm5, %xmm6
movq (%rdx,%r10,8), %rbx
subsd (%rbx,%r13,8), %xmm6
andpd %xmm2, %xmm6
ucomisd %xmm6, %xmm3
ja 0x11664
movq (%r12), %rbx
movsd (%r11,%rbx,8), %xmm6
movapd %xmm4, %xmm7
subsd %xmm6, %xmm7
andpd %xmm2, %xmm7
ucomisd %xmm7, %xmm3
jbe 0x116bf
ucomisd %xmm5, %xmm6
jbe 0x116bf
movsd (%r11,%r10,8), %xmm7
subsd %xmm6, %xmm7
movq (%rdx,%r10,8), %r14
subsd (%r14,%rbx,8), %xmm7
andpd %xmm2, %xmm7
ucomisd %xmm7, %xmm3
jbe 0x116bf
movsd (%r11,%r10,8), %xmm6
subsd %xmm5, %xmm6
movq (%rdx,%r13,8), %rbx
subsd (%rbx,%r10,8), %xmm6
andpd %xmm2, %xmm6
ucomisd %xmm6, %xmm3
jbe 0x116bf
movslq %r8d, %rbx
imulq $0xa8, %rbx, %rbx
addq -0x60(%rbp), %rbx
testb $0x1, %r9b
jne 0x116aa
movl %r13d, 0x80(%rbx)
movl (%r12), %r14d
movl %r14d, 0x84(%rbx)
jmp 0x116bf
movl %r13d, 0x88(%rbx)
movl (%r12), %r14d
movl %r14d, 0x8c(%rbx)
incl %r8d
addq $0x10, %r12
decq %r15
jne 0x115dd
jmp 0x1172e
movq %rcx, %r11
movq %rsi, %r15
testl %eax, %eax
jle 0x1172e
movq 0x8(%r15), %rbx
cmpq %r10, %rbx
je 0x116e6
cmpq %r10, (%r15)
jne 0x11725
movslq %r8d, %r14
imulq $0xa8, %r14, %r12
addq -0x60(%rbp), %r12
testb $0x1, %r9b
jne 0x1170f
movl %ebx, 0x80(%r12)
movl (%r15), %ebx
movl %ebx, 0x84(%r12)
jmp 0x11725
movl %ebx, 0x88(%r12)
movl (%r15), %ebx
movl %ebx, 0x8c(%r12)
incl %r8d
addq $0x10, %r15
decq %r11
jne 0x116d8
incq %r9
cmpq %rdi, %r9
movq -0x58(%rbp), %r15
jne 0x11569
movq -0x50(%rbp), %rax
testl %eax, %eax
js 0x1176a
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 0x11757
movq %r15, %rdi
callq 0x1100
movq -0x48(%rbp), %rax
addq $0x108, %rsp            # imm = 0x108
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq