// File: /workspace/github_c_makefile_star2_O2/cbouilla[P]AES-attacks-finder/Search.c
// Function: ExhaustiveSearch
Solver * ExhaustiveSearch(Solver * __restrict const B1, const int N1, const int t, int m, int * __restrict taille, SysEqLin E) // Construit toutes les boites de temps <= t et de memoire <= m, ne garde que les plus interessante.
{
	if (m>t) m=t;
	int Alloue=2*N1;
	Solver * __restrict B=malloc(Alloue*sizeof(Solver));
	int Max=0,i;
	for (i = 0; i < N1; i++) /*if (B1[i]->temps<=t && B1[i]->memoire<=m)*/ B[Max++]=copySolver(B1[i]);
	int N=0;
	printf("Search exhaustive : \n");

	/* Partie en test pour les cas surdéterminés */
	Solver Sol = NULL;
	int temps_sol = t, memoire_sol = m+1;
	for (i = 0; i < Max; i++)
	{
		if (B[i]->sortie<=0 && (B[i]->temps<temps_sol || (B[i]->temps=temps_sol && B[i]->memoire<memoire_sol)))
		{
			freeSolver(Sol);
			Sol=copySolver(B[i]);
			temps_sol=B[i]->temps;
			memoire_sol=B[i]->memoire;
		}
	}
	i=0;
	/* La recherche */
	while (Max!=N)
	{
		TriFusion1(B+N,Max-N); // On favorise les boites les plus interessantes
		N=Max;
		while (i<N)
		{
			int j=i-1;
			while (j>=0) // On fait l'union de B[i] avec B[j], 0 <= j < i
			{
				// Un première passe pour voir si ça vaut le coup		
				if ((B[i]->sortie<=m || B[j]->sortie<=m) && !ContenuListesTriees(B[i]->var,B[i]->Nvar,B[j]->var,B[j]->Nvar) && !ContenuListesTriees(B[j]->var,B[j]->Nvar,B[i]->var,B[i]->Nvar))
				{
					//Solver tmp=UnionSolver(B[i],B[j],E);
					Solver tmp=UnionSolverBorne(B[i],B[j],E,t);
					// Une seconde passe pour voir si ça vaut le coup
					if (tmp!=NULL && tmp->temps<=t && (tmp->temps<temps_sol || (tmp->temps==temps_sol && tmp->memoire<memoire_sol)))
					{
						int k;
						for (k = 0; k < Max; k++) if (PlusInteressant(B[k],tmp) && ContenuListesTriees(tmp->var,tmp->Nvar,B[k]->var,B[k]->Nvar)) break;
						if (k==Max) // ça vaut le coup
						{
							Solver tmp2=PropagateSolver(tmp,E,B1,N1); // On l'augmente gratuitement avec les boites de "base"
							freeSolver(tmp);
							tmp=tmp2;
							k=0;
							
							/* On supprime celle qui sont moins interessante
							 * Le but est de toujours avoir j < i < N <= Max
							 * Les unions entre les i premières boites ont déjà été faite
							 */
							while (k<N)
							{
								if (PlusInteressant(tmp,B[k]) && ContenuListesTriees(B[k]->var,B[k]->Nvar,tmp->var,tmp->Nvar))
								{
									freeSolver(B[k]);
									if (k<j)
									{
										N--;
										B[k]=B[j-1];
										B[j-1]=B[j];
										B[j]=B[i-1];
										B[i-1]=B[i];
										B[i]=B[N];
										j--;
										i--;
										Max--;
										B[N]=B[Max];
									}
									else
									{
										if (k<i)
										{
											N--;
											B[k]=B[i-1];
											B[i-1]=B[i];
											B[i]=B[N];
											i--;
											Max--;
											B[N]=B[Max];
										}
										else
										{
											if (k==i)
											{
												N--;
												B[i]=B[N];
												i--;
												j=0;
												Max--;
												B[N]=B[Max];
											}
											else
											{
												N--;
												B[k]=B[N];
												Max--;
												B[N]=B[Max];
											}
										}
									}					
								}
								else k++;
							}
							while (k<Max)
							{
								if (PlusInteressant(tmp,B[k]) && ContenuListesTriees(B[k]->var,B[k]->Nvar,tmp->var,tmp->Nvar))
								{
									freeSolver(B[k]);
									Max--;
									B[k]=B[Max];
								}
								else k++;
							}
							if (tmp->sortie<=0) // En test
							{
								freeSolver(Sol);
								Sol=tmp;
								temps_sol=tmp->temps;
								memoire_sol=tmp->memoire;
							}
							else
							{
								/* On ajoute la boite à la liste et on continue */
								if (Max==Alloue)
								{
									Alloue=2*Alloue;
									B=realloc(B,Alloue*sizeof(Solver));					
								}
								/*printf(" Solver union :");
								PrintVarSolver(tmp);
								getchar();*/
								B[Max]=tmp;
								Max++;
							}
						}
						else freeSolver(tmp);
					}
					else freeSolver(tmp);
				}
				j--;
			}
			i++;
			if (i%0x10==0)
			{
				printf("\ri=%d, N=%d, Max=%d",i,N,Max);
				fflush(stdout);
			}
		}
	}
	printf("\ri=%d, N=%d, Max=%d\n",i,N,Max);
	fflush(stdout);
	if (Sol!=NULL) // En test
	{
		Solver * __restrict B1_tmp=malloc(N1*sizeof(Solver));
		int N1_tmp = 0;
		SysEqLin F = EsachantX(Sol->var,Sol->Nvar,E);
		for (i = 0; i < N1; i++)
		{
			B1_tmp[N1_tmp]=AjusteSolver(B1[i],F);
			if (B1_tmp[N1_tmp]!=NULL) N1_tmp++;
		}
		if (N1_tmp!=0)
		{
			int N_tmp;
			Solver * __restrict B_tmp = ExhaustiveSearch(B1_tmp,N1_tmp,t,m,&N_tmp,F);
			B=realloc(B,(N+N_tmp)*sizeof(Solver));
			for (i = 0; i < N_tmp; i++)
			{
				B[N++]=UnionSolver2(B_tmp[i],Sol,E);
				freeSolver(B_tmp[i]);
			}
			free(B_tmp);
			freeSolver(Sol);
		}
		else
		{
			B=realloc(B,(N+1)*sizeof(Solver));
			B[N++]=Sol;
		}
		freeSysEq(F);
		for (i = 0; i < N1_tmp; i++) freeSolver(B1_tmp[i]);
		free(B1_tmp);
	}
	else B=realloc(B,N*sizeof(Solver));
	*taille=N;
	return B;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movq %r9, 0x48(%rsp)
movq %r8, %r14
movl %ecx, %ebp
movl %edx, 0x28(%rsp)
movl %esi, %r12d
movq %rdi, 0x50(%rsp)
leal (%r12,%r12), %eax
movl %eax, 0x14(%rsp)
movslq %eax, %rdi
shlq $0x3, %rdi
callq 0x21c0
movq %rax, %r15
xorl %ebx, %ebx
testl %r12d, %r12d
movl $0x0, %r13d
movq %r12, 0x60(%rsp)
cmovgl %r12d, %r13d
movl %r13d, %r12d
cmpq %rbx, %r12
je 0x1430e
movq 0x50(%rsp), %rax
movq (%rax,%rbx,8), %rdi
callq 0x6846
movq %rax, (%r15,%rbx,8)
incq %rbx
jmp 0x142f2
movq %r12, 0x68(%rsp)
movl 0x28(%rsp), %r12d
cmpl %r12d, %ebp
cmovgel %r12d, %ebp
leaq 0x16b4b(%rip), %rdi     # 0x2ae71
callq 0x2090
leal 0x1(%rbp), %eax
movl %eax, 0x20(%rsp)
xorl %ebx, %ebx
xorl %edi, %edi
movl %r12d, %esi
movq 0x68(%rsp), %r12
cmpq %rbx, %r12
je 0x14393
movq (%r15,%rbx,8), %rax
cmpl $0x0, 0x14(%rax)
jg 0x1438e
cmpl %esi, 0xc(%rax)
jl 0x14367
testl %esi, %esi
je 0x14388
xorl %ecx, %ecx
movl 0x20(%rsp), %edx
cmpl %edx, 0x10(%rax)
setl %cl
movl %ecx, 0xc(%rax)
jge 0x1438e
callq 0x6852
movq (%r15,%rbx,8), %rdi
callq 0x6846
movq %rax, %rdi
movq (%r15,%rbx,8), %rax
movl 0xc(%rax), %esi
movl 0x10(%rax), %eax
movl %eax, 0x20(%rsp)
jmp 0x1438e
andl $0x0, 0xc(%rax)
xorl %esi, %esi
incq %rbx
jmp 0x1433e
movl %esi, 0x44(%rsp)
movq %rdi, 0x30(%rsp)
movq %r14, 0x58(%rsp)
xorl %ebx, %ebx
xorl %r14d, %r14d
movq %rbp, 0x38(%rsp)
movl %r13d, %esi
movslq %r14d, %r12
subl %r14d, %esi
je 0x147cb
leaq (%r15,%r12,8), %rdi
callq 0x6f82
movl %r13d, %ecx
movq %rbx, %r12
cmpl %r13d, %r12d
jge 0x147bd
movl %r13d, 0x24(%rsp)
movl %r12d, %ebx
movq %r12, 0x18(%rsp)
movslq %r12d, %r14
testl %ecx, %ecx
movl $0x0, %r13d
movq %rcx, 0x8(%rsp)
cmovgl %ecx, %r13d
testl %ebx, %ebx
jle 0x14774
decl %ebx
movq (%r15,%r14,8), %rax
movl %ebx, %r12d
movq (%r15,%r12,8), %rcx
cmpl %ebp, 0x14(%rax)
jle 0x14412
cmpl %ebp, 0x14(%rcx)
jg 0x143f3
movq (%rax), %rdi
movl 0x8(%rax), %esi
movq (%rcx), %rdx
movl 0x8(%rcx), %ecx
callq 0x2a3d
testl %eax, %eax
jne 0x143f3
movq (%r15,%r12,8), %rax
movq (%rax), %rdi
movl 0x8(%rax), %esi
movq (%r15,%r14,8), %rax
movq (%rax), %rdx
movl 0x8(%rax), %ecx
callq 0x2a3d
testl %eax, %eax
jne 0x143f3
movq (%r15,%r14,8), %rdi
movq (%r15,%r12,8), %rsi
movq 0x48(%rsp), %rdx
movl 0x28(%rsp), %ecx
callq 0x73be
movq %rax, %rbp
testq %rax, %rax
je 0x144cf
movl 0xc(%rbp), %eax
cmpl 0x28(%rsp), %eax
jg 0x144cf
cmpl 0x44(%rsp), %eax
jge 0x144c4
xorl %r12d, %r12d
cmpq %r12, %r13
je 0x144b8
movq (%r15,%r12,8), %rax
movl 0x8(%rax), %ecx
movl 0x8(%rbp), %esi
cmpl %esi, %ecx
jl 0x144b3
movl 0x14(%rax), %edx
cmpl 0x14(%rbp), %edx
jg 0x144b3
movl 0xc(%rbp), %edx
cmpl %edx, 0xc(%rax)
jge 0x144a9
movq (%rbp), %rdi
movq (%rax), %rdx
callq 0x2a3d
testl %eax, %eax
je 0x144b3
jmp 0x144bb
jne 0x144b3
movl 0x10(%rax), %edx
cmpl 0x10(%rbp), %edx
jle 0x14497
incq %r12
jmp 0x14474
movl %r13d, %r12d
cmpl 0x8(%rsp), %r12d
jne 0x144cf
jmp 0x144e1
jne 0x144cf
movl 0x20(%rsp), %eax
cmpl %eax, 0x10(%rbp)
jl 0x14471
movq %rbp, %rdi
callq 0x6852
movq 0x38(%rsp), %rbp
jmp 0x143f3
movq %rbp, %rdi
movq 0x48(%rsp), %rsi
movq 0x50(%rsp), %rdx
movq 0x60(%rsp), %rcx
callq 0x77bb
movq %rax, %r14
movq %rbp, %rdi
callq 0x6852
movq 0x18(%rsp), %rax
movl %eax, %esi
xorl %r12d, %r12d
movl 0x14(%rsp), %r8d
movl 0x24(%rsp), %r13d
movl %r12d, %ebp
movslq %esi, %r12
movslq %ebp, %rbp
cmpl %r13d, %ebp
jge 0x14673
movl 0x8(%r14), %ecx
movq (%r15,%rbp,8), %rax
movl 0x8(%rax), %esi
cmpl %esi, %ecx
jl 0x1456b
movl 0x14(%r14), %edx
cmpl 0x14(%rax), %edx
jg 0x1456b
movl 0xc(%rax), %edx
cmpl %edx, 0xc(%r14)
jge 0x14560
movq (%rax), %rdi
movq (%r14), %rdx
callq 0x2a3d
movl 0x14(%rsp), %r8d
testl %eax, %eax
je 0x1456b
jmp 0x14570
jne 0x1456b
movl 0x10(%r14), %edx
cmpl 0x10(%rax), %edx
jle 0x1454a
incq %rbp
jmp 0x14520
movq (%r15,%rbp,8), %rdi
callq 0x6852
movslq %r12d, %rdx
movslq %r13d, %rcx
movslq 0x8(%rsp), %rax
decl %r13d
cmpl %ebx, %ebp
jl 0x145fc
cmpl %r12d, %ebp
jl 0x145c1
leaq -0x1(%rcx), %rdi
movq -0x8(%r15,%rcx,8), %rcx
cmpl %ebp, %r12d
movl 0x14(%rsp), %r8d
je 0x14652
movq %rcx, (%r15,%rbp,8)
leal -0x1(%rax), %ecx
movq %rcx, 0x8(%rsp)
movq -0x8(%r15,%rax,8), %rax
movq %rax, (%r15,%rdi,8)
jmp 0x1451d
decq %r12
movslq %r12d, %rsi
movq (%r15,%rsi,8), %rdi
movq %rdi, (%r15,%rbp,8)
movq (%r15,%rdx,8), %rdi
movq %rdi, (%r15,%rsi,8)
movq -0x8(%r15,%rcx,8), %rsi
movq %rsi, (%r15,%rdx,8)
leal -0x1(%rax), %edx
movq %rdx, 0x8(%rsp)
movq -0x8(%r15,%rax,8), %rax
movq %rax, -0x8(%r15,%rcx,8)
movl 0x14(%rsp), %r8d
jmp 0x1451d
movslq %ebx, %rdi
decl %ebx
movq -0x8(%r15,%rdi,8), %rsi
movq %rsi, (%r15,%rbp,8)
movq (%r15,%rdi,8), %rsi
movq %rsi, -0x8(%r15,%rdi,8)
leal -0x1(%r12), %esi
movq -0x8(%r15,%rdx,8), %r8
movq %r8, (%r15,%rdi,8)
movq (%r15,%rdx,8), %rdi
movq %rdi, -0x8(%r15,%rdx,8)
movq -0x8(%r15,%rcx,8), %rdi
movq %rdi, (%r15,%rdx,8)
leal -0x1(%rax), %edx
movq %rdx, 0x8(%rsp)
movq -0x8(%r15,%rax,8), %rax
movq %rax, -0x8(%r15,%rcx,8)
movl %ebp, %r12d
movl 0x14(%rsp), %r8d
jmp 0x14517
movq %rcx, (%r15,%rdx,8)
leal -0x1(%r12), %esi
leal -0x1(%rax), %ecx
movq %rcx, 0x8(%rsp)
movq -0x8(%r15,%rax,8), %rax
movq %rax, (%r15,%rdi,8)
xorl %ebx, %ebx
jmp 0x14517
movq %r12, 0x18(%rsp)
movslq %ebp, %r12
movq 0x8(%rsp), %rax
cltq
cmpl %eax, %ebp
movq %rax, 0x8(%rsp)
jge 0x146f5
movl 0x8(%r14), %ecx
movq (%r15,%r12,8), %rax
movl 0x8(%rax), %esi
cmpl %esi, %ecx
jl 0x146ec
movl 0x14(%r14), %edx
cmpl 0x14(%rax), %edx
jg 0x146ec
movl 0xc(%rax), %edx
cmpl %edx, 0xc(%r14)
jl 0x146b7
jne 0x146ec
movl 0x10(%r14), %edx
cmpl 0x10(%rax), %edx
jg 0x146ec
movq (%rax), %rdi
movq (%r14), %rdx
callq 0x2a3d
testl %eax, %eax
je 0x146ec
movq (%r15,%r12,8), %rdi
callq 0x6852
movq 0x8(%rsp), %rcx
decq %rcx
movslq %ecx, %rax
movq (%r15,%rax,8), %rax
movq %rax, (%r15,%r12,8)
movq %rcx, %rax
movl 0x14(%rsp), %r8d
jmp 0x14682
incl %ebp
movl 0x14(%rsp), %r8d
jmp 0x14678
cmpl $0x0, 0x14(%r14)
movl %r13d, 0x24(%rsp)
jle 0x14741
movl %r8d, %eax
movq 0x8(%rsp), %rcx
cmpl %ecx, %eax
movq 0x38(%rsp), %rbp
movq 0x18(%rsp), %r12
jne 0x14736
addl %r8d, %r8d
movl %r8d, 0x14(%rsp)
movslq %r8d, %rsi
shlq $0x3, %rsi
movq %r15, %rdi
callq 0x21f0
movq %rax, %r15
movq 0x8(%rsp), %rcx
movq %r14, (%r15,%rcx,8)
incl %ecx
jmp 0x143da
movq 0x30(%rsp), %rdi
callq 0x6852
movl 0xc(%r14), %eax
movl %eax, 0x44(%rsp)
movl 0x10(%r14), %eax
movl %eax, 0x20(%rsp)
movq %r14, 0x30(%rsp)
movq 0x38(%rsp), %rbp
movq 0x18(%rsp), %r12
movq 0x8(%rsp), %rcx
jmp 0x143da
movq 0x18(%rsp), %r12
incl %r12d
testb $0xf, %r12b
movl 0x24(%rsp), %r13d
movq 0x8(%rsp), %rcx
jne 0x143c9
leaq 0x163a7(%rip), %rdi     # 0x2ab3e
movl %r12d, %esi
movl %r13d, %edx
xorl %eax, %eax
callq 0x2110
movq 0x1b815(%rip), %rax     # 0x2ffc0
movq (%rax), %rdi
callq 0x21d0
movq 0x8(%rsp), %rcx
jmp 0x143c9
movq %r12, %rbx
movl %r13d, %r14d
movl %ecx, %r13d
jmp 0x143ab
leaq 0x16380(%rip), %rdi     # 0x2ab52
xorl %ebp, %ebp
movl %ebx, %esi
movl %r14d, %edx
movl %r14d, %ecx
xorl %eax, %eax
callq 0x2110
movq 0x1b7d6(%rip), %rax     # 0x2ffc0
movq (%rax), %rdi
callq 0x21d0
movq 0x30(%rsp), %rax
testq %rax, %rax
je 0x148fd
movslq 0x60(%rsp), %rdi
shlq $0x3, %rdi
movq %rax, %rbx
callq 0x21c0
movq %rax, 0x8(%rsp)
movq (%rbx), %rdi
movl 0x8(%rbx), %esi
movq 0x48(%rsp), %rdx
callq 0xb021
movq %rax, 0x18(%rsp)
xorl %r13d, %r13d
movl %ebp, %ebx
cmpq %r13, 0x68(%rsp)
je 0x1485f
movq 0x50(%rsp), %rax
movq (%rax,%r13,8), %rdi
movq 0x18(%rsp), %rsi
callq 0x8723
movq 0x8(%rsp), %rcx
movq %rax, (%rcx,%rbx,8)
cmpq $0x1, %rax
sbbl $-0x1, %ebp
incq %r13
jmp 0x1482e
testl %ebp, %ebp
je 0x14916
leaq 0x74(%rsp), %r8
movq 0x8(%rsp), %rdi
movl %ebp, %esi
movl 0x28(%rsp), %edx
movq 0x38(%rsp), %rcx
movq %r8, %rbp
movq 0x18(%rsp), %r9
callq 0x142a0
movq %rax, %r13
movl (%rbp), %ebp
leal (%r14,%rbp), %eax
movslq %eax, %rsi
shlq $0x3, %rsi
movq %r15, %rdi
callq 0x21f0
xorl %r15d, %r15d
testl %ebp, %ebp
cmovlel %r15d, %ebp
movq %rax, 0x28(%rsp)
leaq (%rax,%r12,8), %r12
cmpq %r15, %rbp
movq 0x30(%rsp), %rsi
je 0x148e0
movq (%r13,%r15,8), %rdi
movq 0x48(%rsp), %rdx
callq 0x757e
movq %rax, (%r12,%r15,8)
movq (%r13,%r15,8), %rdi
callq 0x6852
incq %r15
jmp 0x148b4
addl %r15d, %r14d
movq %r13, %rdi
movq %rsi, %r15
callq 0x2030
movq %r15, %rdi
callq 0x6852
movq 0x28(%rsp), %r13
jmp 0x14936
shlq $0x3, %r12
movq %r15, %rdi
movq %r12, %rsi
callq 0x21f0
movq %rax, %r13
movq 0x58(%rsp), %r12
jmp 0x1496a
incl %r14d
leaq 0x8(,%r12,8), %rsi
movq %r15, %rdi
callq 0x21f0
movq %rax, %r13
movq 0x30(%rsp), %rax
movq %rax, (%r13,%r12,8)
movq 0x18(%rsp), %rdi
callq 0xb562
xorl %r15d, %r15d
movq 0x58(%rsp), %r12
cmpq %r15, %rbx
je 0x14960
movq 0x8(%rsp), %rax
movq (%rax,%r15,8), %rdi
callq 0x6852
incq %r15
jmp 0x14948
movq 0x8(%rsp), %rdi
callq 0x2030
movl %r14d, (%r12)
movq %r13, %rax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq