// File: /workspace/github_c_makefile_star2_O2/cbouilla[P]AES-attacks-finder/Symetric.c
// Function: SearchUneSolverN3_SymetricVersion
Solver SearchUneSolverN3_SymetricVersion(int T, Solver *B1, int N1, SysEqLin E, Symetric_structure S)
{

	Solver * __restrict Solver_du_premier_message = malloc(N1*sizeof(Solver));
	int Nombre_de_solver_a_guesser = 0;
	int i;
	for (i = 0; i < N1; i++)
	{
		int j = 0;
		while (j<B1[i]->Nvar && S->message_by_var[B1[i]->var[j]]!=0) j++;
		if (j!=B1[i]->Nvar)	Solver_du_premier_message[Nombre_de_solver_a_guesser++]=B1[i];
	}

	int Alloue=1000; // espace alloué pour stocker les solvers
	Solver * __restrict Candidat=malloc(Alloue*sizeof(Solver));
	int N=0;
	int v_max=0;
	int proba = 0;
	int pos=0;
	Solver * __restrict guess_probable = malloc(N1*sizeof(Solver));
	printf("Randomized Search : (%d/%d) \n",Nombre_de_solver_a_guesser,N1);
	unsigned int cpt=0;
	int cpt2=0;
	Solver tmp;
	const int coeff_proba=200*E->gen/E->a;
	while (1)
	{
		cpt++;
		// On génère une solver
		if (cpt2 < proba) tmp=SearchUneSolver3_SymetricVersion(T,guess_probable,pos,E,S);
		else tmp=SearchUneSolver3_SymetricVersion(T,Solver_du_premier_message,Nombre_de_solver_a_guesser,E,S);
		i=0;
		// On la teste vis à vis des autres
		while (i < N)
		{
			Solver tmp2=UnionSolverBorne(Candidat[i],tmp,E,T);
			if (tmp2!=NULL)
			{
				if (tmp2->Nvar == Candidat[i]->Nvar)
				{
					freeSolver(tmp2);
					break;
				}
				if (tmp2->Nvar == tmp->Nvar)
				{
					N--;
					freeSolver(Candidat[i]);
					Candidat[i]=Candidat[N];
					freeSolver(tmp2);
				}
				else
				{
					N--;
					freeSolver(Candidat[i]);
					Candidat[i]=Candidat[N];
					freeSolver(tmp);
					tmp=PropagateSolver2(tmp2,E,B1,N1);
					freeSolver(tmp2);
					if (tmp->sortie>0) i=0;
					else i=N;
				}
			}
			else
			{
				//freeSolver(tmp2);
				i++;
			}
		}
		if (i==N) // c'est une nouvelle solver
		{
			if (tmp->sortie<=0) // On a probablement gagné
			{
				for (i = 0; i < N; i++) freeSolver(Candidat[i]);
				free(Candidat);
				for (i = 0; i < pos; i++) freeSolver(guess_probable[i]);
				free(guess_probable);
				printf("\n");
				return tmp;
			}
			Candidat[N++]=tmp;
			if (tmp->Nvar>v_max) // Elle a beaucoup de variables -> elle parait interessante
			{
				/* On cherchera une partie des solvers générées dans son complémentaire */
				v_max=tmp->Nvar;
				for (i = 0; i < pos; i++) freeSolver(guess_probable[i]);
				pos=0;
				for (i = 0; i < Nombre_de_solver_a_guesser; i++) if (!ContenuListesTriees(Solver_du_premier_message[i]->var,Solver_du_premier_message[i]->Nvar,tmp->var,tmp->Nvar)) guess_probable[pos++]=copySolver(Solver_du_premier_message[i]);
				proba=v_max*coeff_proba;
			}
			//v_max=max(v_max,tmp->Nvar);
			if (cpt2 >= 199)
			{
				printf("\r - Candidats générés : %u (%d stockés), Nombre var max : %d / %d ",cpt,N,v_max,E->a/E->gen);
				fflush(stdout);
				cpt2=0;
			}
			else cpt2++;
			if (N==Alloue) // On a atteint la limite d'espace alloué
			{
				i=0;
				while (i<N) // On supprime celles qui semblent peu intéressantes
				{
					if (Candidat[i]->Nvar<=(v_max+T)/2)
					{
						freeSolver(Candidat[i]);
						N--;
						Candidat[i]=Candidat[N];
					}
					else i++;
				}
				if (Alloue-N<500) // On realloue
				{
					Alloue+=1000;
					Candidat=realloc(Candidat,Alloue*sizeof(Solver));
					if (Candidat==NULL)
					{
						printf("Erreur allocation");
						getchar();
					}
				}
			}		
		}
		else // elle est moins interessante que celles que l'on a déjà
		{
			freeSolver(tmp);
			cpt2++;
		}
		if (N==1 && Candidat[0]->Nvar==E->a/E->gen) // On a gagné
		{
			tmp=Candidat[0];
			free(Candidat);
			printf("\n");
			return tmp;
		}
	}
	for (i = 0; i < N; i++) freeSolver(Candidat[i]);
	free(Candidat);
	printf("\n");
	return NULL;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movq %r8, %r15
movq %rcx, 0x28(%rsp)
movl %edx, %r12d
movq %rsi, %r13
movq %rdi, 0x40(%rsp)
movslq %edx, %r14
leaq (,%r14,8), %rbx
movq %rbx, %rdi
callq 0x21c0
xorl %r11d, %r11d
testl %r14d, %r14d
movl $0x0, %ecx
cmovgl %r12d, %ecx
xorl %r14d, %r14d
cmpq %rcx, %r11
je 0x167f9
movq (%r13,%r11,8), %rdx
movl 0x8(%rdx), %esi
testl %esi, %esi
movl $0x0, %edi
cmovgl %esi, %edi
xorl %r8d, %r8d
cmpq %r8, %rdi
je 0x167e6
movq 0x10(%r15), %r9
movq (%rdx), %r10
movslq (%r10,%r8,4), %r10
cmpl $0x0, (%r9,%r10,4)
je 0x167e3
incq %r8
jmp 0x167c7
movl %r8d, %edi
cmpl %esi, %edi
je 0x167f4
movslq %r14d, %rsi
incl %r14d
movq %rdx, (%rax,%rsi,8)
incq %r11
jmp 0x167ad
movq %rax, 0x60(%rsp)
movq %r13, 0x68(%rsp)
movq %r15, 0x58(%rsp)
movl $0x1f40, %edi           # imm = 0x1F40
callq 0x21c0
movq %rax, %rbp
movq %rbx, %rdi
callq 0x21c0
movq %rax, %r15
leaq 0x146cb(%rip), %rdi     # 0x2aef2
movl %r14d, %esi
movl %r12d, 0x4c(%rsp)
movl %r12d, %edx
xorl %eax, %eax
callq 0x2110
movl %r14d, %ecx
movq 0x28(%rsp), %r14
imull $0xc8, 0x10(%r14), %eax
cltd
idivl 0x8(%r14)
movl %eax, 0x50(%rsp)
testl %ecx, %ecx
movl $0x0, %eax
movl %ecx, 0x54(%rsp)
cmovgl %ecx, %eax
movq %rax, 0x70(%rsp)
movl $0x3e8, 0x1c(%rsp)      # imm = 0x3E8
xorl %r12d, %r12d
xorl %esi, %esi
movl $0x0, 0x38(%rsp)
movq $0x0, 0x20(%rsp)
movl $0x0, 0x34(%rsp)
xorl %r8d, %r8d
movq %r15, 0x10(%rsp)
movl %esi, 0x3c(%rsp)
movl %r8d, 0xc(%rsp)
cmpl 0x38(%rsp), %r8d
jge 0x168b2
movq 0x40(%rsp), %rdi
movq %r15, %rsi
movq 0x20(%rsp), %rdx
jmp 0x168c0
movq 0x40(%rsp), %rdi
movq 0x60(%rsp), %rsi
movl 0x54(%rsp), %edx
movq %r14, %rcx
movq 0x58(%rsp), %r8
callq 0x16654
movq %rax, %rbx
incl 0x34(%rsp)
xorl %r15d, %r15d
movslq %r12d, %r12
movslq %r15d, %r15
cmpl %r12d, %r15d
jge 0x16995
movq (%rbp,%r15,8), %rdi
movq %rbx, %rsi
movq %r14, %rdx
movq 0x40(%rsp), %rcx
callq 0x73be
testq %rax, %rax
jne 0x16905
incq %r15
jmp 0x168dd
movq %rax, %r13
movl 0x8(%rax), %r14d
movq (%rbp,%r15,8), %rdi
cmpl 0x8(%rdi), %r14d
je 0x16988
movl 0x8(%rbx), %eax
movl %eax, (%rsp)
decq %r12
callq 0x6852
movslq %r12d, %rax
movq (%rbp,%rax,8), %rax
movq %rax, (%rbp,%r15,8)
cmpl (%rsp), %r14d
jne 0x16947
movq %r13, %rdi
callq 0x6852
movq 0x28(%rsp), %r14
jmp 0x168da
movq %rbx, %rdi
callq 0x6852
movq %r13, %rdi
movq 0x28(%rsp), %r14
movq %r14, %rsi
movq 0x68(%rsp), %rdx
movl 0x4c(%rsp), %ecx
callq 0x788d
movq %rax, %rbx
movq %r13, %rdi
callq 0x6852
cmpl $0x0, 0x14(%rbx)
movl %r12d, %r15d
movl $0x0, %eax
cmovgl %eax, %r15d
jmp 0x168d7
movq %r13, %rdi
callq 0x6852
movq 0x28(%rsp), %r14
cmpl %r15d, %r12d
jne 0x169e7
cmpl $0x0, 0x14(%rbx)
movq 0x10(%rsp), %r15
movl 0x3c(%rsp), %ecx
jle 0x16bc0
movslq %r12d, %rax
movq %rbx, (%rbp,%rax,8)
movl 0x8(%rbx), %eax
movq %rax, (%rsp)
cmpl %ecx, %eax
jle 0x16a05
movq 0x20(%rsp), %r13
testl %r13d, %r13d
movl $0x0, %eax
cmovlel %eax, %r13d
xorl %r14d, %r14d
cmpq %r14, %r13
je 0x16a12
movq (%r15,%r14,8), %rdi
callq 0x6852
incq %r14
jmp 0x169d4
movq %rbx, %rdi
callq 0x6852
movl 0xc(%rsp), %r8d
incl %r8d
movq 0x10(%rsp), %r15
movl 0x3c(%rsp), %esi
jmp 0x16b99
movl %ecx, %eax
movq %rax, (%rsp)
movl 0xc(%rsp), %r8d
jmp 0x16a87
xorl %r15d, %r15d
movq $0x0, 0x20(%rsp)
movq 0x28(%rsp), %r14
movl 0xc(%rsp), %r8d
cmpq %r15, 0x70(%rsp)
je 0x16a75
movq 0x60(%rsp), %r13
movq (%r13,%r15,8), %rax
movq (%rax), %rdi
movl 0x8(%rax), %esi
movq (%rbx), %rdx
movl 0x8(%rbx), %ecx
callq 0x2a3d
testl %eax, %eax
jne 0x16a70
movq (%r13,%r15,8), %rdi
callq 0x6846
movq 0x20(%rsp), %rdx
movslq %edx, %rcx
incl %edx
movq %rdx, 0x20(%rsp)
movq 0x10(%rsp), %rdx
movq %rax, (%rdx,%rcx,8)
incq %r15
jmp 0x16a23
movq (%rsp), %rax
imull 0x50(%rsp), %eax
movl %eax, 0x38(%rsp)
movq 0x10(%rsp), %r15
incl %r12d
cmpl $0xc7, %r8d
movl 0x1c(%rsp), %ebx
jge 0x16a9c
incl %r8d
jmp 0x16ad3
movl 0x8(%r14), %eax
cltd
idivl 0x10(%r14)
leaq 0x14464(%rip), %rdi     # 0x2af10
movl 0x34(%rsp), %esi
movl %r12d, %edx
movq (%rsp), %rcx
movl %eax, %r8d
xorl %eax, %eax
callq 0x2110
movq 0x194f8(%rip), %rax     # 0x2ffc0
movq (%rax), %rdi
callq 0x21d0
xorl %r8d, %r8d
cmpl %ebx, %r12d
jne 0x16b27
movl %ebx, %ecx
movq 0x40(%rsp), %rax
movq (%rsp), %rdx
addl %edx, %eax
cltd
pushq $0x2
popq %rsi
idivl %esi
movl %eax, %ebx
xorl %r15d, %r15d
movl %ecx, %r12d
movslq %r15d, %r15
cmpl %r12d, %r15d
jge 0x16b2f
movq (%rbp,%r15,8), %rdi
cmpl %ebx, 0x8(%rdi)
jle 0x16b0a
incq %r15
jmp 0x16af6
movl %r8d, %r13d
callq 0x6852
movl %r13d, %r8d
movslq %r12d, %rax
decl %r12d
movq -0x8(%rbp,%rax,8), %rax
movq %rax, (%rbp,%r15,8)
jmp 0x16af3
movq (%rsp), %rax
movl %eax, %esi
jmp 0x16b99
movl 0x1c(%rsp), %ecx
movl %ecx, %eax
subl %r12d, %eax
cmpl $0x1f3, %eax            # imm = 0x1F3
jg 0x16b6e
movl %r8d, %ebx
movslq %ecx, %rax
addl $0x3e8, %ecx            # imm = 0x3E8
movl %ecx, 0x1c(%rsp)
leaq 0x1f40(,%rax,8), %rsi
movq %rbp, %rdi
callq 0x21f0
testq %rax, %rax
movq 0x10(%rsp), %r15
je 0x16b7b
movq %rax, %rbp
jmp 0x16b90
movq (%rsp), %rax
movl %eax, %esi
movq 0x10(%rsp), %r15
jmp 0x16b99
leaq 0x14152(%rip), %rdi     # 0x2acd4
xorl %eax, %eax
callq 0x2110
callq 0x2170
xorl %ebp, %ebp
movq (%rsp), %rax
movl %eax, %esi
movl %ebx, %r8d
cmpl $0x1, %r12d
jne 0x16893
movq (%rbp), %rbx
movl 0x8(%rbx), %ecx
movl 0x8(%r14), %eax
cltd
idivl 0x10(%r14)
cmpl %eax, %ecx
jne 0x16893
movq %rbp, %rdi
jmp 0x16c16
xorl %r14d, %r14d
testl %r12d, %r12d
movl $0x0, %r15d
cmovgl %r12d, %r15d
cmpq %r14, %r15
je 0x16be4
movq (%rbp,%r14,8), %rdi
callq 0x6852
incq %r14
jmp 0x16bd0
movq %rbp, %rdi
callq 0x2030
xorl %r14d, %r14d
movq 0x20(%rsp), %r12
testl %r12d, %r12d
cmovlel %r14d, %r12d
movq 0x10(%rsp), %r15
cmpq %r14, %r12
je 0x16c13
movq (%r15,%r14,8), %rdi
callq 0x6852
incq %r14
jmp 0x16c00
movq %r15, %rdi
callq 0x2030
pushq $0xa
popq %rdi
callq 0x2040
movq %rbx, %rax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq