// File: /workspace/github_c_makefile_star2_O3/cbouilla[P]AES-attacks-finder/Search.c
// Function: RandomizedSearch
Solver RandomizedSearch(const int T, Solver *B1, const int N1, SysEqLin E)
{
	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 : \n");
	unsigned int cpt=0;
	int cpt2=0;
	const int coeff_proba=200*E->gen/E->a;
	Solver tmp;
	while (1)
	{
		cpt++;
		// On génère une solver
		if (cpt2 < proba) tmp=GenerateSolver(T,guess_probable,pos,E);
		else tmp=GenerateSolver(T,B1,N1,E);
		int 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);
					printf("\ncollision found!! Output : %d\n",tmp->sortie);
					if (tmp->sortie<T && tmp->sortie>0)
					{
						SysEqLin F = EsachantX(tmp->var,tmp->Nvar,E);
						tmp2 = SearchAlgoLowMem(F,T-tmp->sortie);
						if (tmp2)
						{
							Solver tmp3 = NewSolver();
							tmp3->var = UnionListesTriees(tmp->var,tmp->Nvar,tmp2->var,tmp2->Nvar,&tmp3->Nvar);
							tmp3->temps = max(tmp->temps,tmp->sortie+tmp2->temps);
							tmp3->memoire = max(tmp->memoire,tmp2->memoire);
							tmp3->sortie = tmp->sortie + tmp2->sortie;
							tmp3->type = 2;
							tmp3->fils2 = tmp;
							tmp3->fils1 = tmp2;
							tmp = tmp3;
						}
						freeSysEq(F);
					}
					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 < N1; i++) if (!ContenuListesTriees(B1[i]->var,B1[i]->Nvar,tmp->var,tmp->Nvar)) guess_probable[pos++]=copySolver(B1[i]);
				proba=v_max*coeff_proba;
			}
			if (cpt2 >= 199)
			{
				printf("\r - generated solvers : %u, number of variables max : %d / %d ",cpt,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 && Candidat[i]->memoire<=1)
					{
						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;
		}
	}
	int i;
	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 %rcx, %r13
movl %edx, %ebx
movq %rsi, %rbp
movl %edi, %r15d
movl $0x1f40, %edi           # imm = 0x1F40
callq 0x21d0
movq %rax, %r14
movl %ebx, 0x3c(%rsp)
movslq %ebx, %rbx
leaq (,%rbx,8), %rdi
callq 0x21d0
movq %rax, %r12
leaq 0x15e78(%rip), %rdi     # 0x2cee6
callq 0x2090
imull $0xc8, 0x10(%r13), %eax
cltd
movq %r13, 0x30(%rsp)
idivl 0x8(%r13)
movl %eax, 0x64(%rsp)
movl %ebx, %eax
movq %rax, 0x70(%rsp)
movq %r15, %rbx
movl $0x3e8, 0x2c(%rsp)      # imm = 0x3E8
movl $0x0, 0x14(%rsp)
movl $0x0, 0x50(%rsp)
movl $0x0, 0x4(%rsp)
xorl %r8d, %r8d
movl $0x0, 0x4c(%rsp)
xorl %esi, %esi
movq %rbp, 0x40(%rsp)
movq %r15, 0x18(%rsp)
movq %r12, 0x68(%rsp)
movq %r8, %r15
movl %esi, 0x10(%rsp)
cmpl 0x50(%rsp), %esi
jge 0x170e7
movl %ebx, %edi
movq %r12, %rsi
movl 0x4(%rsp), %edx
jmp 0x170f0
movl %ebx, %edi
movq %rbp, %rsi
movl 0x3c(%rsp), %edx
movq 0x30(%rsp), %rcx
callq 0x16adf
movq %rax, 0x8(%rsp)
movq %r15, %r8
xorl %ecx, %ecx
testl %r8d, %r8d
jle 0x172fd
movl %r8d, %r12d
movq %rbp, %r13
movslq %ecx, %r15
movq (%r14,%r15,8), %rdi
movq 0x8(%rsp), %rsi
movq 0x30(%rsp), %rdx
movl %ecx, %ebp
movl %ebx, %ecx
callq 0x7ff0
testq %rax, %rax
je 0x1719d
movq %rax, %rbx
movl %ebp, 0x54(%rsp)
movl 0x8(%rax), %ebp
movq (%r14,%r15,8), %rdi
cmpl 0x8(%rdi), %ebp
je 0x172e4
movq 0x8(%rsp), %r13
movl 0x8(%r13), %eax
movl %eax, 0x20(%rsp)
leal -0x1(%r12), %eax
movq %rax, 0x58(%rsp)
callq 0x7447
movslq %r12d, %rax
movq -0x8(%r14,%rax,8), %rax
movq %r14, %r12
movq %rax, (%r14,%r15,8)
cmpl 0x20(%rsp), %ebp
jne 0x171b1
movq %rbx, %rdi
callq 0x7447
movq %r12, %r14
movq 0x40(%rsp), %rbp
movq 0x18(%rsp), %rbx
movq 0x58(%rsp), %r8
movl 0x54(%rsp), %ecx
jmp 0x172d6
incl %ebp
movl %r12d, %r8d
movq 0x18(%rsp), %rbx
movl %ebp, %ecx
movq %r13, %rbp
jmp 0x172d6
movq %r13, %rdi
callq 0x7447
movq %rbx, %rdi
movq 0x30(%rsp), %rsi
movq 0x40(%rsp), %rbp
movq %rbp, %rdx
movl 0x3c(%rsp), %ecx
callq 0x84b9
movq %rax, %r14
movq %rbx, %rdi
callq 0x7447
movl 0x14(%r14), %esi
leaq 0x15aed(%rip), %rdi     # 0x2ccd5
xorl %eax, %eax
callq 0x2110
movq %r14, 0x8(%rsp)
movl 0x14(%r14), %eax
movq 0x18(%rsp), %rbx
cmpl %ebx, %eax
setge %cl
testl %eax, %eax
setle %dl
orb %cl, %dl
jne 0x172c1
movq 0x8(%rsp), %r14
movq (%r14), %rdi
movl 0x8(%r14), %esi
movq 0x30(%rsp), %rdx
callq 0xc29a
movq %rbx, %rcx
movq %rax, %rbx
movl %ecx, %esi
subl 0x14(%r14), %esi
movq %rax, %rdi
callq 0x13f10
testq %rax, %rax
je 0x172ac
movq %rax, %r15
callq 0x7394
movq %rax, %rbp
movq 0x8(%rsp), %r14
movq (%r14), %rdi
movl 0x8(%r14), %esi
movq (%r15), %rdx
movl 0x8(%r15), %ecx
leaq 0x8(%rax), %r8
callq 0x2544
movq %rax, (%rbp)
movl 0xc(%r14), %eax
movl 0x14(%r14), %ecx
movl 0xc(%r15), %edx
addl %ecx, %edx
cmpl %edx, %eax
cmovgl %eax, %edx
movl %edx, 0xc(%rbp)
movl 0x10(%r14), %eax
movl 0x10(%r15), %edx
cmpl %edx, %eax
cmovgl %eax, %edx
movl %edx, 0x10(%rbp)
addl 0x14(%r15), %ecx
movl %ecx, 0x14(%rbp)
movb $0x2, 0x18(%rbp)
movq %r14, 0x28(%rbp)
movq %r15, 0x20(%rbp)
movq %rbp, 0x8(%rsp)
movq 0x40(%rsp), %rbp
movq %rbx, %rdi
callq 0xc82d
movq 0x8(%rsp), %rax
movl 0x14(%rax), %eax
movq 0x18(%rsp), %rbx
movq %r12, %r14
testl %eax, %eax
movq 0x58(%rsp), %r8
movl %r8d, %ecx
movl $0x0, %eax
cmovgl %eax, %ecx
movl %r8d, %r12d
cmpl %r8d, %ecx
jl 0x17110
jmp 0x172fd
movq %rbx, %rdi
callq 0x7447
movl %r12d, %r8d
movq 0x40(%rsp), %rbp
movq 0x18(%rsp), %rbx
movl 0x54(%rsp), %ecx
incl 0x4c(%rsp)
cmpl %r8d, %ecx
jne 0x17339
movq 0x8(%rsp), %rax
cmpl $0x0, 0x14(%rax)
movq 0x68(%rsp), %r12
jle 0x1759d
movslq %r8d, %rax
movq 0x8(%rsp), %rcx
movq %rcx, (%r14,%rax,8)
movl 0x8(%rcx), %ecx
movl 0x14(%rsp), %eax
cmpl %eax, %ecx
jge 0x17359
movl %eax, %r15d
jmp 0x17409
movq 0x8(%rsp), %rdi
movq %r8, %r15
callq 0x7447
movq %r15, %r8
movl 0x10(%rsp), %esi
incl %esi
movq 0x68(%rsp), %r12
jmp 0x1756f
movq %rcx, 0x20(%rsp)
movq %r8, 0x58(%rsp)
movq %r14, %r15
movl 0x4(%rsp), %eax
testl %eax, %eax
jle 0x17384
movl %eax, %ebx
xorl %r14d, %r14d
movq (%r12,%r14,8), %rdi
callq 0x7447
incq %r14
cmpq %r14, %rbx
jne 0x17373
cmpl $0x0, 0x3c(%rsp)
jle 0x173e3
xorl %ebx, %ebx
movl $0x0, 0x4(%rsp)
movq %r15, %r14
movq 0x20(%rsp), %r15
movq (%rbp,%rbx,8), %rax
movq (%rax), %rdi
movl 0x8(%rax), %esi
movq 0x8(%rsp), %rax
movq (%rax), %rdx
movl 0x8(%rax), %ecx
callq 0x2e80
testl %eax, %eax
jne 0x173d7
movq (%rbp,%rbx,8), %rdi
callq 0x743b
movl 0x4(%rsp), %edx
movslq %edx, %rcx
incl %edx
movl %edx, 0x4(%rsp)
movq %rax, (%r12,%rcx,8)
incq %rbx
cmpq %rbx, 0x70(%rsp)
jne 0x1739d
jmp 0x173f3
movl $0x0, 0x4(%rsp)
movq %r15, %r14
movq 0x20(%rsp), %r15
movl %r15d, %eax
imull 0x64(%rsp), %eax
movl %eax, 0x50(%rsp)
movq 0x18(%rsp), %rbx
movq 0x58(%rsp), %r8
movl 0x10(%rsp), %esi
cmpl $0xc7, %esi
jge 0x17419
incl %esi
jmp 0x17459
movq 0x30(%rsp), %rcx
movl 0x8(%rcx), %eax
cltd
idivl 0x10(%rcx)
leaq 0x158c9(%rip), %rdi     # 0x2ccf5
movl 0x4c(%rsp), %esi
movl %r15d, %edx
movl %eax, %ecx
xorl %eax, %eax
movq %r15, %r13
movq %r8, %r15
callq 0x2110
movq 0x1ab77(%rip), %rax     # 0x31fc0
movq (%rax), %rdi
callq 0x21e0
movq %r15, %r8
movq %r13, %r15
xorl %esi, %esi
leal 0x1(%r8), %eax
movl 0x2c(%rsp), %ecx
cmpl %ecx, %eax
jne 0x17526
movq %r15, 0x20(%rsp)
movl %esi, 0x10(%rsp)
testl %r8d, %r8d
movl %ecx, %r8d
js 0x174d9
movq %r14, %rdx
movq 0x18(%rsp), %rax
movq 0x20(%rsp), %rcx
addl %ecx, %eax
movl %eax, %ebx
shrl $0x1f, %ebx
addl %eax, %ebx
sarl %ebx
xorl %ebp, %ebp
movl 0x2c(%rsp), %eax
movl %eax, %r8d
movslq %ebp, %r15
movq (%rdx,%r15,8), %rdi
cmpl %ebx, 0x8(%rdi)
jg 0x174d2
cmpl $0x1, 0x10(%rdi)
jg 0x174d2
movq %r12, %r13
movq %r8, %r12
callq 0x7447
movq %r14, %rdx
movq %r12, %r8
movq %r13, %r12
movslq %r8d, %rax
decl %r8d
movq -0x8(%r14,%rax,8), %rax
movq %rax, (%r14,%r15,8)
jmp 0x174d4
incl %ebp
cmpl %r8d, %ebp
jl 0x1749b
movl 0x2c(%rsp), %ecx
movl %ecx, %eax
subl %r8d, %eax
cmpl $0x1f3, %eax            # imm = 0x1F3
jg 0x17530
movq %r8, %r15
movslq %ecx, %rax
addl $0x3e8, %ecx            # imm = 0x3E8
movl %ecx, 0x2c(%rsp)
leaq 0x1f40(,%rax,8), %rsi
movq %r14, %rdi
callq 0x2200
testq %rax, %rax
movq 0x40(%rsp), %rbp
movq 0x18(%rsp), %rbx
je 0x17549
movq %rax, %r14
movq 0x20(%rsp), %rax
movl %eax, 0x14(%rsp)
jmp 0x17568
movl %r15d, 0x14(%rsp)
movl %eax, %r8d
jmp 0x1756f
movq 0x20(%rsp), %rax
movl %eax, 0x14(%rsp)
movq 0x40(%rsp), %rbp
movq 0x18(%rsp), %rbx
movl 0x10(%rsp), %esi
jmp 0x1756f
leaq 0x157e4(%rip), %rdi     # 0x2cd34
xorl %eax, %eax
callq 0x2110
callq 0x2180
movq 0x20(%rsp), %rax
movl %eax, 0x14(%rsp)
xorl %r14d, %r14d
movl 0x10(%rsp), %esi
movq %r15, %r8
cmpl $0x1, %r8d
jne 0x170cf
movq (%r14), %rax
movq %rax, 0x8(%rsp)
movl 0x8(%rax), %ecx
movq 0x30(%rsp), %rdi
movl 0x8(%rdi), %eax
cltd
idivl 0x10(%rdi)
cmpl %eax, %ecx
jne 0x170cf
movq %r14, %rdi
jmp 0x175e5
testl %r8d, %r8d
movq %r14, %r15
jle 0x175bc
movl %r8d, %ebx
xorl %r14d, %r14d
movq (%r15,%r14,8), %rdi
callq 0x7447
incq %r14
cmpq %r14, %rbx
jne 0x175ab
movq %r15, %rdi
callq 0x2030
movl 0x4(%rsp), %eax
testl %eax, %eax
jle 0x175e2
movl %eax, %ebx
xorl %r14d, %r14d
movq (%r12,%r14,8), %rdi
callq 0x7447
incq %r14
cmpq %r14, %rbx
jne 0x175d1
movq %r12, %rdi
callq 0x2030
movl $0xa, %edi
callq 0x2040
movq 0x8(%rsp), %rax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq