// File: /workspace/github_c_makefile_star2_O1/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, %r15
movl %edx, %ebx
movq %rsi, %r13
movl %edi, %r14d
movl $0x1f40, %edi           # imm = 0x1F40
callq 0x21d0
movq %rax, %r12
movl %ebx, 0x2c(%rsp)
movslq %ebx, %rbx
leaq (,%rbx,8), %rdi
callq 0x21d0
movq %rax, 0x50(%rsp)
leaq 0x160e9(%rip), %rdi     # 0x2ce86
callq 0x2090
imull $0xc8, 0x10(%r15), %eax
cltd
idivl 0x8(%r15)
movq %r12, %rdi
movq %r13, %r12
movl %eax, 0x60(%rsp)
movl %ebx, %eax
movq %rax, 0x70(%rsp)
movq %r14, %rcx
movq %r15, %r14
movl $0x3e8, 0x28(%rsp)      # imm = 0x3E8
movl $0x0, 0x8(%rsp)
movl $0x0, 0x48(%rsp)
movl $0x0, 0x4(%rsp)
xorl %r8d, %r8d
movl $0x0, 0x44(%rsp)
movl $0x0, 0xc(%rsp)
movq %r15, 0x20(%rsp)
movq %r13, 0x30(%rsp)
movq %rcx, %rbx
movq %rcx, 0x10(%rsp)
movq %r8, %r13
movq %rdi, 0x18(%rsp)
movl 0x48(%rsp), %eax
cmpl %eax, 0xc(%rsp)
jge 0x16e2a
movl %ebx, %edi
movq 0x50(%rsp), %rsi
movl 0x4(%rsp), %edx
jmp 0x16e33
movl %ebx, %edi
movq %r12, %rsi
movl 0x2c(%rsp), %edx
movq %r14, %rcx
callq 0x16842
movq %rax, %r12
incl 0x44(%rsp)
xorl %esi, %esi
movq 0x18(%rsp), %rdx
movq %rbx, %rcx
cmpl %r13d, %esi
jge 0x17062
movslq %esi, %rbp
movq (%rdx,%rbp,8), %rdi
movq %r12, 0x38(%rsp)
movl %esi, %ebx
movq %r12, %rsi
movq %rdx, %r12
movq %r14, %rdx
movq %rcx, %r15
callq 0x7cbb
testq %rax, %rax
je 0x16ead
movq %rax, %r14
movl %ebx, 0x4c(%rsp)
movl 0x8(%rax), %ebx
movq (%r12,%rbp,8), %rdi
cmpl 0x8(%rdi), %ebx
jne 0x16ecb
movq %r14, %rdi
callq 0x7060
xorl %eax, %eax
movl %r13d, %ebp
movq 0x10(%rsp), %rcx
movq %r12, %rdx
movq 0x38(%rsp), %r12
movq 0x20(%rsp), %r14
jmp 0x16f19
incl %ebx
movb $0x1, %al
movl %r13d, %ebp
movq %r12, %rdx
movq 0x38(%rsp), %r12
movq 0x20(%rsp), %r14
movl %ebx, %esi
movq %r15, %rcx
jmp 0x17055
movq 0x38(%rsp), %r15
movl 0x8(%r15), %eax
movl %eax, 0x64(%rsp)
leal -0x1(%r13), %eax
movq %rax, 0x58(%rsp)
callq 0x7060
movslq %r13d, %rax
movq -0x8(%r12,%rax,8), %rax
movq %rax, (%r12,%rbp,8)
cmpl 0x64(%rsp), %ebx
jne 0x16f22
movq %r14, %rdi
callq 0x7060
movb $0x1, %al
movq 0x20(%rsp), %r14
movq 0x10(%rsp), %rcx
movq 0x18(%rsp), %rdx
movq 0x58(%rsp), %rbp
movq %r15, %r12
movl 0x4c(%rsp), %esi
jmp 0x17055
movq %r15, %rdi
callq 0x7060
movq %r14, %rdi
movq 0x20(%rsp), %r15
movq %r15, %rsi
movq 0x30(%rsp), %rdx
movl 0x2c(%rsp), %ecx
callq 0x817d
movq %rax, %r12
movq %r14, %rdi
callq 0x7060
movl 0x14(%r12), %esi
leaq 0x15d1b(%rip), %rdi     # 0x2cc75
xorl %eax, %eax
callq 0x2110
movl 0x14(%r12), %eax
movq 0x10(%rsp), %rcx
cmpl %ecx, %eax
setge %dl
testl %eax, %eax
setle %al
orb %dl, %al
jne 0x17021
movq (%r12), %rdi
movl 0x8(%r12), %esi
movq %r15, %rdx
movq %rcx, %r15
callq 0xbed5
movq %rax, %r14
movq %r15, %rbx
movl %r15d, %esi
subl 0x14(%r12), %esi
movq %rax, %rdi
callq 0x13ba8
movq %r12, %r15
testq %rax, %rax
movq 0x58(%rsp), %rbp
je 0x1702b
movq %rax, %r12
callq 0x6fb4
movq %rax, %r13
movq (%r15), %rdi
movl 0x8(%r15), %esi
movq (%r12), %rdx
movl 0x8(%r12), %ecx
leaq 0x8(%rax), %r8
callq 0x2544
movq %rax, (%r13)
movl 0xc(%r15), %eax
movl 0x14(%r15), %ecx
movl 0xc(%r12), %edx
addl %ecx, %edx
cmpl %edx, %eax
cmovgl %eax, %edx
movl %edx, 0xc(%r13)
movl 0x10(%r15), %eax
movl 0x10(%r12), %edx
cmpl %edx, %eax
cmovgl %eax, %edx
movl %edx, 0x10(%r13)
addl 0x14(%r12), %ecx
movl %ecx, 0x14(%r13)
movb $0x2, 0x18(%r13)
movq %r15, 0x28(%r13)
movq %r12, 0x20(%r13)
movq %r13, %r12
jmp 0x1702e
movq 0x58(%rsp), %rbp
movq %r15, %r14
jmp 0x1703e
movq %r15, %r12
movq %r14, %rdi
callq 0xc453
movq 0x20(%rsp), %r14
movq %rbx, %rcx
cmpl $0x0, 0x14(%r12)
movl %ebp, %esi
movl $0x0, %eax
cmovgl %eax, %esi
movb $0x1, %al
movq 0x18(%rsp), %rdx
movl %ebp, %r13d
testb %al, %al
jne 0x16e4c
jmp 0x17065
movl %r13d, %ebp
cmpl %ebp, %esi
jne 0x1709d
cmpl $0x0, 0x14(%r12)
movq %r12, %r15
jle 0x170cc
movslq %ebp, %rax
movq %r12, (%rdx,%rax,8)
movl 0x8(%r12), %r13d
movl 0x8(%rsp), %eax
cmpl %eax, %r13d
movq 0x50(%rsp), %r12
jge 0x17134
movq %rcx, %rbx
movl %eax, %r13d
jmp 0x171dd
movq %rcx, %rbx
movq %r12, %rdi
movq %r14, %r15
movq %rdx, %r14
callq 0x7060
incl 0xc(%rsp)
movl 0x8(%rsp), %eax
movl %eax, %r13d
movl %ebp, %r8d
movq %r14, %rdi
movq %r15, %r14
movq 0x30(%rsp), %r12
jmp 0x172ff
testl %ebp, %ebp
movq 0x50(%rsp), %r12
jle 0x170f0
movl %ebp, %ebx
xorl %r14d, %r14d
movq (%rdx,%r14,8), %rdi
callq 0x7060
movq 0x18(%rsp), %rdx
incq %r14
cmpq %r14, %rbx
jne 0x170da
movq %rdx, %rdi
callq 0x2030
movl 0x4(%rsp), %eax
testl %eax, %eax
jle 0x17116
movl %eax, %ebx
xorl %r14d, %r14d
movq (%r12,%r14,8), %rdi
callq 0x7060
incq %r14
cmpq %r14, %rbx
jne 0x17105
movq %r12, %rdi
movl 0x8(%rsp), %eax
movl %eax, %r13d
movq 0x10(%rsp), %rbx
movq 0x18(%rsp), %r14
movq 0x30(%rsp), %r12
jmp 0x17323
movl 0x4(%rsp), %eax
testl %eax, %eax
jle 0x17152
movl %eax, %r14d
xorl %ebx, %ebx
movq (%r12,%rbx,8), %rdi
callq 0x7060
incq %rbx
cmpq %rbx, %r14
jne 0x17141
movq %r13, 0x38(%rsp)
cmpl $0x0, 0x2c(%rsp)
jle 0x171b5
xorl %r14d, %r14d
movl $0x0, 0x4(%rsp)
movq 0x10(%rsp), %rbx
movq 0x30(%rsp), %r13
movq (%r13,%r14,8), %rax
movq (%rax), %rdi
movl 0x8(%rax), %esi
movq (%r15), %rdx
movl 0x8(%r15), %ecx
callq 0x2ce2
testl %eax, %eax
jne 0x171a9
movq (%r13,%r14,8), %rdi
callq 0x7054
movl 0x4(%rsp), %edx
movslq %edx, %rcx
incl %edx
movl %edx, 0x4(%rsp)
movq %rax, (%r12,%rcx,8)
incq %r14
cmpq %r14, 0x70(%rsp)
jne 0x1716e
jmp 0x171c2
movl $0x0, 0x4(%rsp)
movq 0x10(%rsp), %rbx
movq 0x38(%rsp), %r13
movl %r13d, %eax
imull 0x60(%rsp), %eax
movl %eax, 0x48(%rsp)
movq 0x18(%rsp), %rdx
movq 0x20(%rsp), %r14
movl 0xc(%rsp), %eax
cmpl $0xc7, %eax
movq 0x30(%rsp), %r12
jge 0x171f5
incl %eax
movl %eax, 0xc(%rsp)
jmp 0x17231
movl 0x8(%r14), %eax
cltd
idivl 0x10(%r14)
movl $0x0, 0xc(%rsp)
leaq 0x15a88(%rip), %rdi     # 0x2cc95
movl 0x44(%rsp), %esi
movl %r13d, %edx
movl %eax, %ecx
xorl %eax, %eax
callq 0x2110
movq 0x1ad9c(%rip), %rax     # 0x31fc0
movq (%rax), %rdi
callq 0x21e0
movq 0x18(%rsp), %rdx
leal 0x1(%rbp), %r8d
cmpl 0x28(%rsp), %r8d
jne 0x172d3
testl %ebp, %ebp
js 0x1728f
leal (%rbx,%r13), %eax
movl %eax, %ebx
shrl $0x1f, %ebx
addl %eax, %ebx
sarl %ebx
xorl %r14d, %r14d
movslq %r14d, %r15
movq (%rdx,%r15,8), %rdi
cmpl %ebx, 0x8(%rdi)
jg 0x17287
cmpl $0x1, 0x10(%rdi)
jg 0x17287
movq %r8, %rbp
callq 0x7060
movq %rbp, %r8
movq 0x18(%rsp), %rdx
movslq %r8d, %rax
decl %r8d
movq -0x8(%rdx,%rax,8), %rax
movq %rax, (%rdx,%r15,8)
jmp 0x1728a
incl %r14d
cmpl %r8d, %r14d
jl 0x17254
movl 0x28(%rsp), %ecx
movl %ecx, %eax
subl %r8d, %eax
cmpl $0x1f3, %eax            # imm = 0x1F3
jg 0x172d8
movq %r8, %r15
movslq %ecx, %rax
addl $0x3e8, %ecx            # imm = 0x3E8
movl %ecx, 0x28(%rsp)
leaq 0x1f40(,%rax,8), %rsi
movq %rdx, %rdi
callq 0x2200
testq %rax, %rax
movq 0x20(%rsp), %r14
movq 0x10(%rsp), %rbx
je 0x172e7
movq %rax, %rdi
jmp 0x172fc
movq %rdx, %rdi
jmp 0x172ff
movq %rdx, %rdi
movq 0x20(%rsp), %r14
movq 0x10(%rsp), %rbx
jmp 0x172ff
leaq 0x159e6(%rip), %rdi     # 0x2ccd4
xorl %eax, %eax
callq 0x2110
callq 0x2180
xorl %edi, %edi
movq %r15, %r8
movb $0x1, %cl
cmpl $0x1, %r8d
jne 0x1733f
movq (%rdi), %r15
movl 0x8(%r15), %esi
movl 0x8(%r14), %eax
cltd
idivl 0x10(%r14)
movl $0x1, %ebp
movq %rdi, %r14
cmpl %eax, %esi
jne 0x1733f
callq 0x2030
movl $0xa, %edi
callq 0x2040
xorl %ecx, %ecx
movl %ebp, %r8d
movq %r14, %rdi
movq %r15, 0x68(%rsp)
movl %r13d, 0x8(%rsp)
testb %cl, %cl
movq 0x20(%rsp), %r14
jne 0x16e0b
movq 0x68(%rsp), %rax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq