// File: /workspace/github_c_makefile_star2_O1/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, 0x50(%rsp)
movq %rcx, 0x8(%rsp)
movq %rsi, 0x70(%rsp)
movl %edi, %ebp
movl %edx, 0x28(%rsp)
movslq %edx, %rbx
leaq (,%rbx,8), %r13
movq %r13, %rdi
callq 0x21d0
movq %rax, 0x48(%rsp)
movl $0x0, %r15d
testl %ebx, %ebx
jle 0x18511
movl 0x28(%rsp), %eax
xorl %ecx, %ecx
xorl %r15d, %r15d
movq 0x70(%rsp), %rdx
movq (%rdx,%rcx,8), %rdx
movl 0x8(%rdx), %esi
movl $0x0, %edi
testl %esi, %esi
jle 0x184f6
movq 0x50(%rsp), %rdi
movq 0x10(%rdi), %r8
movq (%rdx), %r9
xorl %edi, %edi
movslq (%r9,%rdi,4), %r10
cmpl $0x0, (%r8,%r10,4)
je 0x184f6
incq %rdi
cmpq %rdi, %rsi
jne 0x184e1
movl %esi, %edi
cmpl %esi, %edi
je 0x18509
movslq %r15d, %rsi
incl %r15d
movq 0x48(%rsp), %rdi
movq %rdx, (%rdi,%rsi,8)
incq %rcx
cmpq %rax, %rcx
jne 0x184be
movl $0x1f40, %edi           # imm = 0x1F40
callq 0x21d0
movq %rax, %rbx
movq %r13, %rdi
callq 0x21d0
movq %rax, 0x40(%rsp)
leaq 0x149c0(%rip), %rdi     # 0x2cef2
movl %r15d, %esi
movl 0x28(%rsp), %edx
xorl %eax, %eax
callq 0x2110
movq 0x8(%rsp), %rcx
imull $0xc8, 0x10(%rcx), %eax
cltd
idivl 0x8(%rcx)
movl %eax, 0x60(%rsp)
movl %r15d, %eax
movq %rax, 0x68(%rsp)
movq %rbx, %r15
movl $0x3e8, %r12d           # imm = 0x3E8
xorl %r13d, %r13d
xorl %edi, %edi
movl $0x0, 0x30(%rsp)
movl $0x0, (%rsp)
movl $0x0, 0x2c(%rsp)
xorl %esi, %esi
movq %rbp, 0x10(%rsp)
movl %edi, 0x34(%rsp)
movl %esi, 0x4(%rsp)
cmpl 0x30(%rsp), %esi
movl %r12d, 0x24(%rsp)
movl %ebp, %edi
jge 0x185a7
movq 0x40(%rsp), %rsi
movl (%rsp), %edx
jmp 0x185b1
movq 0x48(%rsp), %rsi
movq 0x68(%rsp), %rdx
movq 0x8(%rsp), %rcx
movq 0x50(%rsp), %r8
callq 0x18350
movq %rax, %rsi
incl 0x2c(%rsp)
xorl %r14d, %r14d
movq %r15, 0x58(%rsp)
cmpl %r13d, %r14d
jge 0x186cc
movslq %r14d, %rbx
movq (%r15,%rbx,8), %rdi
movq %rsi, %r12
movq 0x8(%rsp), %rdx
movl %ebp, %ecx
callq 0x7cbb
testq %rax, %rax
je 0x1861a
movq %rax, %rbp
movl %r14d, 0x18(%rsp)
movl 0x8(%rax), %r14d
movq (%r15,%rbx,8), %rdi
cmpl 0x8(%rdi), %r14d
jne 0x18627
movq %rbp, %rdi
callq 0x7060
xorl %eax, %eax
movq 0x10(%rsp), %rbp
jmp 0x1866b
incl %r14d
movb $0x1, %al
movq %r12, %rsi
jmp 0x186c4
movl 0x8(%r12), %eax
movl %eax, 0x64(%rsp)
leal -0x1(%r13), %eax
movl %eax, 0x38(%rsp)
callq 0x7060
movslq %r13d, %rax
movq -0x8(%r15,%rax,8), %rax
movq %rax, (%r15,%rbx,8)
cmpl 0x64(%rsp), %r14d
jne 0x18675
movq %rbp, %rdi
callq 0x7060
movb $0x1, %al
movl 0x38(%rsp), %ecx
movl %ecx, %r13d
movq 0x10(%rsp), %rbp
movq 0x58(%rsp), %r15
movq %r12, %rsi
movl 0x18(%rsp), %r14d
jmp 0x186c4
movq %r12, %rdi
callq 0x7060
movq %rbp, %rdi
movq 0x8(%rsp), %rsi
movq 0x70(%rsp), %rdx
movl 0x28(%rsp), %ecx
callq 0x817d
movq %rax, %rbx
movq %rbp, %rdi
callq 0x7060
movq %rbx, %rsi
cmpl $0x0, 0x14(%rbx)
movl 0x38(%rsp), %edx
movl %edx, %r14d
movl $0x0, %eax
cmovgl %eax, %r14d
movb $0x1, %al
movl %edx, %r13d
movq 0x10(%rsp), %rbp
movq 0x58(%rsp), %r15
testb %al, %al
jne 0x185cf
cmpl %r13d, %r14d
jne 0x18788
cmpl $0x0, 0x14(%rsi)
movl 0x24(%rsp), %r12d
movl 0x34(%rsp), %ecx
jle 0x1890a
movslq %r13d, %rax
movq %rsi, (%r15,%rax,8)
movl 0x8(%rsi), %eax
cmpl %ecx, %eax
movq 0x40(%rsp), %r15
jle 0x187a4
movq %rax, 0x18(%rsp)
movq %rsi, %rbp
movl (%rsp), %eax
testl %eax, %eax
jle 0x18724
movl %eax, %ebx
xorl %r14d, %r14d
movq (%r15,%r14,8), %rdi
callq 0x7060
incq %r14
cmpq %r14, %rbx
jne 0x18713
movq %r13, 0x38(%rsp)
movq 0x68(%rsp), %r14
testl %r14d, %r14d
movq 0x48(%rsp), %rbx
jle 0x187ab
xorl %r12d, %r12d
movl $0x0, (%rsp)
movq 0x10(%rsp), %r13
movq %rbp, %rcx
movq (%rbx,%r12,8), %rax
movq (%rax), %rdi
movl 0x8(%rax), %esi
movq (%rcx), %rdx
movl 0x8(%rcx), %ecx
callq 0x2ce2
testl %eax, %eax
jne 0x1877b
movq (%rbx,%r12,8), %rdi
callq 0x7054
movl (%rsp), %edx
movslq %edx, %rcx
incl %edx
movl %edx, (%rsp)
movq %rax, (%r15,%rcx,8)
incq %r12
cmpq %r12, %r14
movq %rbp, %rcx
jne 0x1874a
jmp 0x187b7
movq %rsi, %rdi
callq 0x7060
movl 0x4(%rsp), %esi
incl %esi
movl 0x24(%rsp), %r12d
movl 0x34(%rsp), %edi
jmp 0x188e2
movq %rbp, %r14
movl %ecx, %ebp
jmp 0x187d4
movl $0x0, (%rsp)
movq 0x10(%rsp), %r13
movq 0x18(%rsp), %rbp
movl %ebp, %eax
imull 0x60(%rsp), %eax
movl %eax, 0x30(%rsp)
movl 0x24(%rsp), %r12d
movq %r13, %r14
movq 0x38(%rsp), %r13
movq %r13, %rbx
incl %r13d
movl 0x4(%rsp), %esi
cmpl $0xc7, %esi
movq 0x58(%rsp), %r15
jge 0x187ef
incl %esi
jmp 0x18826
movq 0x8(%rsp), %rcx
movl 0x8(%rcx), %eax
cltd
idivl 0x10(%rcx)
leaq 0x1470e(%rip), %rdi     # 0x2cf10
movl 0x2c(%rsp), %esi
movl %r13d, %edx
movl %ebp, %ecx
movl %eax, %r8d
xorl %eax, %eax
callq 0x2110
movq 0x197a4(%rip), %rax     # 0x31fc0
movq (%rax), %rdi
callq 0x21e0
xorl %esi, %esi
cmpl %r12d, %r13d
jne 0x188b1
movl %esi, 0x4(%rsp)
testl %ebx, %ebx
movq %rbp, 0x18(%rsp)
js 0x18876
leal (%r14,%rbp), %eax
movl %eax, %ebp
shrl $0x1f, %ebp
addl %eax, %ebp
sarl %ebp
xorl %r14d, %r14d
movslq %r14d, %rbx
movq (%r15,%rbx,8), %rdi
cmpl %ebp, 0x8(%rdi)
jle 0x1885d
incl %r14d
jmp 0x18871
callq 0x7060
movslq %r13d, %rax
decl %r13d
movq -0x8(%r15,%rax,8), %rax
movq %rax, (%r15,%rbx,8)
cmpl %r13d, %r14d
jl 0x1884c
movl %r12d, %eax
subl %r13d, %eax
cmpl $0x1f3, %eax            # imm = 0x1F3
jg 0x188b8
movslq %r12d, %rax
addl $0x3e8, %r12d           # imm = 0x3E8
leaq 0x1f40(,%rax,8), %rsi
movq %r15, %rdi
callq 0x2200
testq %rax, %rax
movq 0x10(%rsp), %rbp
movq 0x18(%rsp), %rbx
je 0x188c6
movq %rax, %r15
jmp 0x188dc
movl %ebp, %edi
movq %r14, %rbp
jmp 0x188e2
movq 0x18(%rsp), %rax
movl %eax, %edi
movq 0x10(%rsp), %rbp
jmp 0x188de
leaq 0x14407(%rip), %rdi     # 0x2ccd4
xorl %eax, %eax
callq 0x2110
callq 0x2180
xorl %r15d, %r15d
movl %ebx, %edi
movl 0x4(%rsp), %esi
cmpl $0x1, %r13d
jne 0x18588
movq (%r15), %rbx
movl 0x8(%rbx), %ecx
movq 0x8(%rsp), %r8
movl 0x8(%r8), %eax
cltd
idivl 0x10(%r8)
cmpl %eax, %ecx
jne 0x18588
jmp 0x1895d
movq %rsi, 0x8(%rsp)
testl %r13d, %r13d
movq %r15, %r12
movq 0x40(%rsp), %r15
movl (%rsp), %ebp
jle 0x18936
movl %r13d, %ebx
xorl %r14d, %r14d
movq (%r12,%r14,8), %rdi
callq 0x7060
incq %r14
cmpq %r14, %rbx
jne 0x18925
movq %r12, %rdi
callq 0x2030
testl %ebp, %ebp
jle 0x18958
movl %ebp, %ebx
xorl %r14d, %r14d
movq (%r15,%r14,8), %rdi
callq 0x7060
incq %r14
cmpq %r14, %rbx
jne 0x18947
movq 0x8(%rsp), %rbx
movq %r15, %rdi
callq 0x2030
movl $0xa, %edi
callq 0x2040
movq %rbx, %rax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq