// File: /workspace/github_c_makefile_star2_O3/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, %rbp
movq %r8, 0x60(%rsp)
movl %ecx, %r13d
movl %esi, %ebx
cmpl %edx, %ecx
movl %edx, 0x38(%rsp)
cmovgel %edx, %r13d
movq %rdi, %r12
leal (%rbx,%rbx), %eax
movl %eax, 0x4c(%rsp)
movslq %eax, %rdi
shlq $0x3, %rdi
callq 0x21d0
movq %rax, %r15
movl %ebx, %r14d
movq %rbx, 0x58(%rsp)
testl %ebx, %ebx
movq %rbp, 0x40(%rsp)
movq %r14, 0x50(%rsp)
movq %r12, 0x68(%rsp)
jle 0x163e0
xorl %ebx, %ebx
movq (%r12,%rbx,8), %rdi
callq 0x743b
movq %rax, (%r15,%rbx,8)
incq %rbx
cmpq %rbx, %r14
jne 0x15f36
leaq 0x16f7f(%rip), %rdi     # 0x2ced1
callq 0x2090
leal 0x1(%r13), %eax
movl %eax, 0x2c(%rsp)
xorl %ebx, %ebx
movl 0x38(%rsp), %eax
movl %eax, 0x18(%rsp)
movq $0x0, 0x10(%rsp)
movq (%r15,%rbx,8), %rax
cmpl $0x0, 0x14(%rax)
jg 0x15fdf
movl 0x18(%rsp), %edx
cmpl %edx, 0xc(%rax)
jl 0x15f9f
testl %edx, %edx
je 0x15fd0
xorl %ecx, %ecx
movl 0x2c(%rsp), %edx
cmpl %edx, 0x10(%rax)
setl %cl
movl %ecx, 0xc(%rax)
movq 0x50(%rsp), %r14
jge 0x15fdf
movq 0x10(%rsp), %rdi
callq 0x7447
movq (%r15,%rbx,8), %rdi
callq 0x743b
movq 0x50(%rsp), %r14
movq %rax, 0x10(%rsp)
movq (%r15,%rbx,8), %rax
movl 0xc(%rax), %edx
movl %edx, 0x18(%rsp)
movl 0x10(%rax), %eax
movl %eax, 0x2c(%rsp)
jmp 0x15fdf
movl $0x0, 0xc(%rax)
movl $0x0, 0x18(%rsp)
incq %rbx
cmpq %rbx, %r14
jne 0x15f72
movq 0x58(%rsp), %rax
movl %eax, %ecx
xorl %ebx, %ebx
xorl %r14d, %r14d
movq %r13, 0x30(%rsp)
movslq %ebx, %rax
leaq (%r15,%rax,8), %rdi
movl %ecx, %ebx
movl %ecx, %esi
subl %eax, %esi
callq 0x7ba5
cmpl %ebx, %r14d
jge 0x163fc
movl %ebx, 0x8(%rsp)
movq %rbx, 0x20(%rsp)
testl %r14d, %r14d
jle 0x1638f
movl %r14d, %ebx
decl %r14d
movslq %ebx, %r12
movq (%r15,%r12,8), %rax
movq (%r15,%r14,8), %rcx
cmpl %r13d, 0x14(%rax)
jle 0x16042
cmpl %r13d, 0x14(%rcx)
jg 0x160a7
movq (%rax), %rdi
movl 0x8(%rax), %esi
movq (%rcx), %rdx
movl 0x8(%rcx), %ecx
callq 0x2e80
testl %eax, %eax
jne 0x160a7
movq (%r15,%r14,8), %rax
movq (%rax), %rdi
movl 0x8(%rax), %esi
movq (%r15,%r12,8), %rax
movq (%rax), %rdx
movl 0x8(%rax), %ecx
callq 0x2e80
testl %eax, %eax
jne 0x160a7
movq (%r15,%r12,8), %rdi
movq (%r15,%r14,8), %rsi
movq %rbp, %rdx
movl 0x38(%rsp), %ecx
callq 0x7ff0
movq %rax, %r13
testq %rax, %rax
je 0x1609a
movl 0xc(%r13), %eax
cmpl 0x38(%rsp), %eax
jle 0x160b5
movq %r13, %rdi
callq 0x7447
movq 0x30(%rsp), %r13
testl %r14d, %r14d
jg 0x16028
jmp 0x16392
cmpl 0x18(%rsp), %eax
jge 0x16113
movl 0x8(%rsp), %eax
testl %eax, %eax
jle 0x16125
movl %eax, %ebp
xorl %r12d, %r12d
movq (%r15,%r12,8), %rax
movl 0x8(%rax), %ecx
movl 0x8(%r13), %esi
cmpl %esi, %ecx
jl 0x16109
movl 0x14(%rax), %edx
cmpl 0x14(%r13), %edx
jg 0x16109
movl 0xc(%r13), %edx
cmpl %edx, 0xc(%rax)
jge 0x160fe
movq (%r13), %rdi
movq (%rax), %rdx
callq 0x2e80
testl %eax, %eax
je 0x16109
jmp 0x16386
jne 0x16109
movl 0x10(%rax), %edx
cmpl 0x10(%r13), %edx
jle 0x160e9
incq %r12
cmpq %r12, %rbp
jne 0x160c8
jmp 0x16131
jne 0x1609a
movl 0x2c(%rsp), %eax
cmpl %eax, 0x10(%r13)
jge 0x1609a
jmp 0x160bb
xorl %r12d, %r12d
cmpl %eax, %r12d
jne 0x16374
movq %r13, %rdi
movq 0x40(%rsp), %rsi
movq 0x68(%rsp), %rdx
movq 0x58(%rsp), %rcx
callq 0x83ec
movq %rax, %rbp
movq %r13, %rdi
callq 0x7447
cmpl $0x0, 0x20(%rsp)
jle 0x162a0
xorl %r13d, %r13d
movl 0x8(%rbp), %ecx
movslq %r13d, %r12
movq (%r15,%r12,8), %rax
movl 0x8(%rax), %esi
cmpl %esi, %ecx
jl 0x1617a
movl 0x14(%rbp), %edx
cmpl 0x14(%rax), %edx
jle 0x16189
incl %r13d
cmpl 0x20(%rsp), %r13d
jl 0x16161
jmp 0x162a3
movl 0xc(%rax), %edx
cmpl %edx, 0xc(%rbp)
jge 0x161f6
movq (%rax), %rdi
movq (%rbp), %rdx
callq 0x2e80
testl %eax, %eax
je 0x1617a
movq (%r15,%r12,8), %rdi
callq 0x7447
movslq 0x20(%rsp), %rax
leal -0x1(%rax), %ecx
cmpl %r14d, %r12d
jge 0x16206
movq %rcx, 0x20(%rsp)
movslq %r14d, %rcx
decl %r14d
movq -0x8(%r15,%rcx,8), %rdx
movq %rdx, (%r15,%r12,8)
movq (%r15,%rcx,8), %rdx
movq %rdx, -0x8(%r15,%rcx,8)
movslq %ebx, %rdx
decl %ebx
movq -0x8(%r15,%rdx,8), %rsi
movq %rsi, (%r15,%rcx,8)
movq (%r15,%rdx,8), %rcx
movq %rcx, -0x8(%r15,%rdx,8)
movq -0x8(%r15,%rax,8), %rcx
movq %rcx, (%r15,%rdx,8)
jmp 0x16230
jne 0x1617a
movl 0x10(%rbp), %edx
cmpl 0x10(%rax), %edx
jg 0x1617a
jmp 0x16191
cmpl %ebx, %r13d
jge 0x1624c
movq %rcx, 0x20(%rsp)
movslq %ebx, %rcx
decl %ebx
movq -0x8(%r15,%rcx,8), %rdx
movq %rdx, (%r15,%r12,8)
movq (%r15,%rcx,8), %rdx
movq %rdx, -0x8(%r15,%rcx,8)
movq -0x8(%r15,%rax,8), %rdx
movq %rdx, (%r15,%rcx,8)
movl 0x8(%rsp), %edx
movslq %edx, %rcx
decl %edx
movl %edx, 0x8(%rsp)
movq -0x8(%r15,%rcx,8), %rcx
movq %rcx, -0x8(%r15,%rax,8)
jmp 0x1617d
movq %rcx, 0x20(%rsp)
leaq -0x1(%rax), %rcx
movq -0x8(%r15,%rax,8), %rax
movq %rax, (%r15,%r12,8)
cmpl %ebx, %r13d
jne 0x16285
leal -0x1(%r13), %ebx
movl 0x8(%rsp), %edx
movslq %edx, %rax
decl %edx
movl %edx, 0x8(%rsp)
movq -0x8(%r15,%rax,8), %rax
movq %rax, (%r15,%rcx,8)
xorl %r14d, %r14d
jmp 0x1617d
movl 0x8(%rsp), %edx
movslq %edx, %rax
decl %edx
movl %edx, 0x8(%rsp)
movq -0x8(%r15,%rax,8), %rax
movq %rax, (%r15,%rcx,8)
jmp 0x1617d
xorl %r13d, %r13d
cmpl 0x8(%rsp), %r13d
jge 0x1630d
movl 0x8(%rbp), %ecx
movslq %r13d, %r12
movq (%r15,%r12,8), %rax
movl 0x8(%rax), %esi
cmpl %esi, %ecx
jl 0x162c3
movl 0x14(%rbp), %edx
cmpl 0x14(%rax), %edx
jle 0x162c8
incl %r13d
jmp 0x162a3
movl 0xc(%rax), %edx
cmpl %edx, 0xc(%rbp)
jge 0x16301
movq (%rax), %rdi
movq (%rbp), %rdx
callq 0x2e80
testl %eax, %eax
je 0x162c3
movq (%r15,%r12,8), %rdi
callq 0x7447
movl 0x8(%rsp), %ecx
movslq %ecx, %rax
decl %ecx
movl %ecx, 0x8(%rsp)
movq -0x8(%r15,%rax,8), %rax
movq %rax, (%r15,%r12,8)
jmp 0x162a3
jne 0x162c3
movl 0x10(%rbp), %edx
cmpl 0x10(%rax), %edx
jg 0x162c3
jmp 0x162d0
cmpl $0x0, 0x14(%rbp)
jle 0x16355
movl 0x4c(%rsp), %eax
cmpl %eax, 0x8(%rsp)
movq 0x30(%rsp), %r13
jne 0x1633a
addl %eax, %eax
movl %eax, 0x4c(%rsp)
movslq %eax, %rsi
shlq $0x3, %rsi
movq %r15, %rdi
callq 0x2200
movq %rax, %r15
movl 0x8(%rsp), %ecx
movslq %ecx, %rax
movq %rbp, (%r15,%rax,8)
incl %ecx
movl %ecx, 0x8(%rsp)
movq 0x40(%rsp), %rbp
jmp 0x160a7
movq 0x10(%rsp), %rdi
callq 0x7447
movl 0xc(%rbp), %eax
movl %eax, 0x18(%rsp)
movl 0x10(%rbp), %eax
movl %eax, 0x2c(%rsp)
movq %rbp, 0x10(%rsp)
jmp 0x1637c
movq %r13, %rdi
callq 0x7447
movq 0x40(%rsp), %rbp
jmp 0x160a2
movl 0x8(%rsp), %eax
jmp 0x16128
movl %r14d, %ebx
movl %ebx, %r14d
incl %r14d
testb $0xf, %r14b
movq 0x20(%rsp), %rbx
jne 0x163c9
leaq 0x167f4(%rip), %rdi     # 0x2cb9e
movl %r14d, %esi
movl %ebx, %edx
movl 0x8(%rsp), %ecx
xorl %eax, %eax
callq 0x2110
movq 0x1bbff(%rip), %rax     # 0x31fc0
movq (%rax), %rdi
callq 0x21e0
cmpl %ebx, %r14d
jl 0x16017
movl 0x8(%rsp), %ecx
cmpl %ebx, %ecx
jne 0x15ff8
jmp 0x163fc
leaq 0x16aea(%rip), %rdi     # 0x2ced1
callq 0x2090
xorl %r14d, %r14d
xorl %ebx, %ebx
movq $0x0, 0x10(%rsp)
jmp 0x163fc
leaq 0x167af(%rip), %rdi     # 0x2cbb2
movl %r14d, %esi
movl %ebx, %edx
movl %ebx, %ecx
xorl %eax, %eax
callq 0x2110
movq 0x1bba8(%rip), %rax     # 0x31fc0
movq (%rax), %rdi
callq 0x21e0
movq 0x10(%rsp), %r14
testq %r14, %r14
je 0x16575
movq %rbx, 0x20(%rsp)
movq 0x58(%rsp), %rbx
movslq %ebx, %rdi
shlq $0x3, %rdi
callq 0x21d0
movq %rax, 0x18(%rsp)
movq (%r14), %rdi
movq %r14, 0x10(%rsp)
movl 0x8(%r14), %esi
movq %rbp, %rdx
callq 0xc29a
movq %rax, %r14
testl %ebx, %ebx
jle 0x16540
xorl %r12d, %r12d
xorl %ebp, %ebp
movq 0x18(%rsp), %rbx
movq 0x68(%rsp), %rax
movq (%rax,%r12,8), %rdi
movq %r14, %rsi
callq 0x9401
movl %ebp, %ecx
movq %rax, (%rbx,%rcx,8)
cmpq $0x1, %rax
sbbl $-0x1, %ebp
incq %r12
cmpq %r12, 0x50(%rsp)
jne 0x16472
testl %ebp, %ebp
je 0x16540
movq %r13, %rcx
leaq 0x74(%rsp), %r13
movq 0x18(%rsp), %rdi
movl %ebp, %esi
movl 0x38(%rsp), %edx
movq %r13, %r8
movq %r14, %r9
callq 0x15ed7
movq %rax, %r12
movslq (%r13), %r13
movq 0x20(%rsp), %rbx
leal (%rbx,%r13), %eax
movslq %eax, %rsi
shlq $0x3, %rsi
movq %r15, %rdi
callq 0x2200
testq %r13, %r13
movq %rax, 0x38(%rsp)
jle 0x16589
movq %r14, 0x30(%rsp)
movslq %ebx, %rcx
leaq (%rax,%rcx,8), %rax
movq %rax, 0x8(%rsp)
xorl %r15d, %r15d
movq 0x40(%rsp), %r14
movq (%r12,%r15,8), %rdi
movq 0x10(%rsp), %rsi
movq %r14, %rdx
callq 0x81b0
movq 0x8(%rsp), %rcx
movq %rax, (%rcx,%r15,8)
movq (%r12,%r15,8), %rdi
callq 0x7447
incq %r15
cmpq %r15, %r13
jne 0x16506
addl %r15d, %ebx
movq 0x10(%rsp), %r13
movq 0x30(%rsp), %r14
jmp 0x1658e
movq 0x20(%rsp), %rbx
movslq %ebx, %r12
incl %ebx
leaq 0x8(,%r12,8), %rsi
movq %r15, %rdi
callq 0x2200
movq %rax, %r15
movq 0x10(%rsp), %rax
movq %rax, (%r15,%r12,8)
movq %r14, %rdi
callq 0xc82d
movq 0x18(%rsp), %r12
jmp 0x165c7
movslq %ebx, %rsi
shlq $0x3, %rsi
movq %r15, %rdi
callq 0x2200
movq %rax, %r15
jmp 0x165cf
movq 0x10(%rsp), %r13
movq %r12, %rdi
callq 0x2030
movq %r13, %rdi
callq 0x7447
movq %r14, %rdi
callq 0xc82d
movl %ebp, %r14d
xorl %r15d, %r15d
movq 0x18(%rsp), %r12
movq (%r12,%r15,8), %rdi
callq 0x7447
incq %r15
cmpq %r15, %r14
jne 0x165b1
movq 0x38(%rsp), %r15
movq %r12, %rdi
callq 0x2030
movq 0x60(%rsp), %rax
movl %ebx, (%rax)
movq %r15, %rax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq