// File: /workspace/github_c_makefile_star2_O3/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, 0x48(%rsp)
movq %rcx, 0x28(%rsp)
movq %rsi, 0x70(%rsp)
movq %rdi, 0x40(%rsp)
movl %edx, 0x30(%rsp)
movslq %edx, %r14
leaq (,%r14,8), %rbx
movq %rbx, %rdi
callq 0x21d0
movq %rax, %r12
movl $0x0, %r15d
testl %r14d, %r14d
jle 0x1876f
movl 0x30(%rsp), %eax
xorl %ecx, %ecx
xorl %r15d, %r15d
movq 0x70(%rsp), %rdx
movq (%rdx,%rcx,8), %rdx
movslq 0x8(%rdx), %rsi
movl $0x0, %edi
testq %rsi, %rsi
jle 0x18759
movq 0x48(%rsp), %rdi
movq 0x10(%rdi), %r8
movq (%rdx), %r9
xorl %edi, %edi
movslq (%r9,%rdi,4), %r10
cmpl $0x0, (%r8,%r10,4)
je 0x18759
incq %rdi
cmpq %rdi, %rsi
jne 0x18744
jmp 0x18767
cmpl %esi, %edi
je 0x18767
movslq %r15d, %rsi
incl %r15d
movq %rdx, (%r12,%rsi,8)
incq %rcx
cmpq %rax, %rcx
jne 0x1871f
movl $0x1f40, %edi           # imm = 0x1F40
callq 0x21d0
movq %rax, %r14
movq %rbx, %rdi
callq 0x21d0
movq %rax, 0x60(%rsp)
leaq 0x147c2(%rip), %rdi     # 0x2cf52
movl %r15d, %esi
movl 0x30(%rsp), %edx
xorl %eax, %eax
callq 0x2110
movq 0x28(%rsp), %rcx
imull $0xc8, 0x10(%rcx), %eax
cltd
idivl 0x8(%rcx)
movl %eax, 0x58(%rsp)
movl %r15d, %eax
movq %rax, 0x50(%rsp)
movl $0x3e8, 0x1c(%rsp)      # imm = 0x3E8
xorl %esi, %esi
movl $0x0, 0x14(%rsp)
movl $0x0, 0x38(%rsp)
movl $0x0, 0x4(%rsp)
movl $0x0, 0x34(%rsp)
xorl %ebp, %ebp
movq %r12, 0x68(%rsp)
movq %rsi, %rbx
cmpl 0x38(%rsp), %ebp
jge 0x18804
movq 0x40(%rsp), %rdi
movq 0x60(%rsp), %rsi
movl 0x4(%rsp), %edx
jmp 0x18811
movq 0x40(%rsp), %rdi
movq %r12, %rsi
movq 0x50(%rsp), %rdx
movq 0x28(%rsp), %rcx
movq 0x48(%rsp), %r8
callq 0x185a1
movq %rax, %r13
movl %ebp, 0x18(%rsp)
movq %rbx, %rcx
testl %ecx, %ecx
movq %r14, 0x20(%rsp)
jle 0x18911
xorl %r12d, %r12d
movl %ecx, %r15d
movslq %r12d, %rbp
movq (%r14,%rbp,8), %rdi
movq %r13, %rsi
movq 0x28(%rsp), %rdx
movq 0x40(%rsp), %rcx
callq 0x7ff0
testq %rax, %rax
je 0x188b7
movq %rax, %rbx
movl %r12d, 0x3c(%rsp)
movl 0x8(%rax), %r12d
movq (%r14,%rbp,8), %rdi
cmpl 0x8(%rdi), %r12d
je 0x18916
movl 0x8(%r13), %eax
movl %eax, 0x5c(%rsp)
leal -0x1(%r15), %eax
movq %rax, 0x8(%rsp)
callq 0x7447
movslq %r15d, %rax
movq -0x8(%r14,%rax,8), %rax
movq %rax, (%r14,%rbp,8)
cmpl 0x5c(%rsp), %r12d
jne 0x188bf
movq %rbx, %rdi
callq 0x7447
movq 0x20(%rsp), %r14
movq 0x8(%rsp), %rcx
movl 0x3c(%rsp), %r12d
jmp 0x18903
incl %r12d
movl %r15d, %ecx
jmp 0x18903
movq %r13, %rdi
callq 0x7447
movq %rbx, %rdi
movq 0x28(%rsp), %rsi
movq 0x70(%rsp), %rdx
movl 0x30(%rsp), %ecx
callq 0x84b9
movq %rax, %r13
movq %rbx, %rdi
callq 0x7447
cmpl $0x0, 0x14(%r13)
movq 0x8(%rsp), %rcx
movl %ecx, %r12d
movl $0x0, %eax
cmovgl %eax, %r12d
movq 0x20(%rsp), %r14
movl %ecx, %r15d
cmpl %ecx, %r12d
jl 0x1883d
jmp 0x18926
xorl %r12d, %r12d
jmp 0x18926
movq %rbx, %rdi
callq 0x7447
movl %r15d, %ecx
movl 0x3c(%rsp), %r12d
incl 0x34(%rsp)
cmpl %ecx, %r12d
jne 0x189dc
cmpl $0x0, 0x14(%r13)
movq 0x68(%rsp), %r12
movq 0x60(%rsp), %r15
jle 0x18b8c
movslq %ecx, %rax
movq %r13, (%r14,%rax,8)
movl 0x8(%r13), %edx
movl 0x14(%rsp), %eax
movq %rdx, 0x8(%rsp)
cmpl %eax, %edx
jle 0x189fa
movq %rcx, %rbp
movl 0x4(%rsp), %eax
testl %eax, %eax
jle 0x18985
movl %eax, %ebx
xorl %r14d, %r14d
movq (%r15,%r14,8), %rdi
callq 0x7447
incq %r14
cmpq %r14, %rbx
jne 0x18974
cmpl $0x0, 0x50(%rsp)
jle 0x18a01
xorl %ebx, %ebx
movl $0x0, 0x4(%rsp)
movq 0x20(%rsp), %r14
movq (%r12,%rbx,8), %rax
movq (%rax), %rdi
movl 0x8(%rax), %esi
movq (%r13), %rdx
movl 0x8(%r13), %ecx
callq 0x2e80
testl %eax, %eax
jne 0x189d0
movq (%r12,%rbx,8), %rdi
callq 0x743b
movl 0x4(%rsp), %edx
movslq %edx, %rcx
incl %edx
movl %edx, 0x4(%rsp)
movq %rax, (%r15,%rcx,8)
incq %rbx
cmpq %rbx, 0x50(%rsp)
jne 0x1899b
jmp 0x18a0e
movq %r13, %rdi
movq %rcx, %rbx
callq 0x7447
movq %rbx, %rsi
movl 0x18(%rsp), %ebp
incl %ebp
movq 0x68(%rsp), %r12
jmp 0x18b63
movq %rax, 0x8(%rsp)
jmp 0x18a1f
movl $0x0, 0x4(%rsp)
movq 0x20(%rsp), %r14
movq 0x8(%rsp), %rax
imull 0x58(%rsp), %eax
movl %eax, 0x38(%rsp)
movq %rbp, %rcx
leal 0x1(%rcx), %ebx
movl 0x18(%rsp), %ebp
cmpl $0xc7, %ebp
jge 0x18a32
incl %ebp
jmp 0x18a71
movq 0x28(%rsp), %rsi
movl 0x8(%rsi), %eax
cltd
idivl 0x10(%rsi)
xorl %ebp, %ebp
leaq 0x14529(%rip), %rdi     # 0x2cf70
movq %rcx, %r15
movl 0x34(%rsp), %esi
movl %ebx, %edx
movq 0x8(%rsp), %rcx
movl %eax, %r8d
xorl %eax, %eax
callq 0x2110
movq 0x1955a(%rip), %rax     # 0x31fc0
movq (%rax), %rdi
callq 0x21e0
movq %r15, %rcx
movl 0x1c(%rsp), %eax
cmpl %eax, %ebx
jne 0x18b20
movl %ebp, 0x18(%rsp)
testl %ecx, %ecx
movl %eax, %esi
js 0x18ae1
movq 0x40(%rsp), %rax
movq 0x8(%rsp), %rcx
addl %ecx, %eax
movl %eax, %ebx
shrl $0x1f, %ebx
addl %eax, %ebx
sarl %ebx
xorl %r15d, %r15d
movl 0x1c(%rsp), %eax
movl %eax, %esi
movslq %r15d, %r14
movq 0x20(%rsp), %rbp
movq (%rbp,%r14,8), %rdi
cmpl %ebx, 0x8(%rdi)
jle 0x18abc
incl %r15d
jmp 0x18adc
movq %r12, %r13
movq %rsi, %r12
callq 0x7447
movq %r12, %rsi
movq %r13, %r12
movslq %esi, %rax
decl %esi
movq -0x8(%rbp,%rax,8), %rax
movq %rax, (%rbp,%r14,8)
cmpl %esi, %r15d
jl 0x18aa5
movl 0x1c(%rsp), %ecx
movl %ecx, %eax
subl %esi, %eax
cmpl $0x1f3, %eax            # imm = 0x1F3
jg 0x18b2d
movq %rsi, %rbx
movslq %ecx, %rax
addl $0x3e8, %ecx            # imm = 0x3E8
movl %ecx, 0x1c(%rsp)
leaq 0x1f40(,%rax,8), %rsi
movq 0x20(%rsp), %rdi
callq 0x2200
testq %rax, %rax
movl 0x18(%rsp), %ebp
je 0x18b41
movq %rax, %r14
jmp 0x18b57
movl %ebx, %esi
movq 0x8(%rsp), %rax
movl %eax, 0x14(%rsp)
jmp 0x18b63
movq 0x8(%rsp), %rax
movl %eax, 0x14(%rsp)
movq 0x20(%rsp), %r14
movl 0x18(%rsp), %ebp
jmp 0x18b63
leaq 0x141ec(%rip), %rdi     # 0x2cd34
xorl %eax, %eax
callq 0x2110
callq 0x2180
xorl %r14d, %r14d
movq 0x8(%rsp), %rax
movl %eax, 0x14(%rsp)
movq %rbx, %rsi
cmpl $0x1, %esi
jne 0x187eb
movq (%r14), %r13
movl 0x8(%r13), %ecx
movq 0x28(%rsp), %rdi
movl 0x8(%rdi), %eax
cltd
idivl 0x10(%rdi)
cmpl %eax, %ecx
jne 0x187eb
movq %r14, %rdi
jmp 0x18bd2
testl %ecx, %ecx
movq %r14, %r12
jle 0x18ba9
movl %ecx, %ebx
xorl %r14d, %r14d
movq (%r12,%r14,8), %rdi
callq 0x7447
incq %r14
cmpq %r14, %rbx
jne 0x18b98
movq %r12, %rdi
callq 0x2030
movl 0x4(%rsp), %eax
testl %eax, %eax
jle 0x18bcf
movl %eax, %ebx
xorl %r14d, %r14d
movq (%r15,%r14,8), %rdi
callq 0x7447
incq %r14
cmpq %r14, %rbx
jne 0x18bbe
movq %r15, %rdi
callq 0x2030
movl $0xa, %edi
callq 0x2040
movq %r13, %rax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq