// File: /workspace/github_c_makefile_star2_O1/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 $0x68, %rsp
movq %r9, %rbp
movq %r8, %rbx
movl %esi, %r12d
cmpl %edx, %ecx
movl %edx, 0x14(%rsp)
cmovgel %edx, %ecx
movq %rcx, 0x28(%rsp)
movq %rdi, %r14
leal (%r12,%r12), %eax
movl %eax, 0x40(%rsp)
movslq %eax, %rdi
shlq $0x3, %rdi
callq 0x21d0
movq %rax, %r15
movl %r12d, %eax
movq %r12, 0x50(%rsp)
testl %r12d, %r12d
movq %rbx, 0x60(%rsp)
movq %r14, 0x58(%rsp)
movq %rax, 0x48(%rsp)
jle 0x15c3c
xorl %r12d, %r12d
movq (%r14,%r12,8), %rdi
callq 0x7054
movq %rax, (%r15,%r12,8)
movq 0x48(%rsp), %rax
incq %r12
cmpq %r12, %rax
jne 0x15c1d
movq %r12, %rbx
jmp 0x15c3e
xorl %ebx, %ebx
leaq 0x1722c(%rip), %rdi     # 0x2ce71
callq 0x2090
movq 0x28(%rsp), %rax
incl %eax
movl %eax, 0x24(%rsp)
xorl %r14d, %r14d
testl %ebx, %ebx
je 0x15cd8
movq %rbx, %r12
movl %ebx, %ebx
xorl %r13d, %r13d
movl 0x14(%rsp), %eax
movq $0x0, 0x18(%rsp)
movl %eax, %esi
movq (%r15,%r13,8), %rax
cmpl $0x0, 0x14(%rax)
jle 0x15c81
movl %esi, %eax
jmp 0x15cce
cmpl %esi, 0xc(%rax)
jl 0x15c9b
testl %esi, %esi
je 0x15cc5
xorl %ecx, %ecx
movl 0x24(%rsp), %edx
cmpl %edx, 0x10(%rax)
setl %cl
movl %ecx, 0xc(%rax)
jge 0x15c7d
movq 0x18(%rsp), %rdi
callq 0x7060
movq (%r15,%r13,8), %rdi
callq 0x7054
movq %rax, 0x18(%rsp)
movq (%r15,%r13,8), %rax
movl 0xc(%rax), %ecx
movl 0x10(%rax), %eax
movl %eax, 0x24(%rsp)
movl %ecx, %eax
jmp 0x15cce
movl $0x0, 0xc(%rax)
xorl %eax, %eax
incq %r13
cmpq %r13, %rbx
jne 0x15c71
jmp 0x15ce8
movq %rbx, %r12
movq $0x0, 0x18(%rsp)
movl 0x14(%rsp), %eax
testl %r12d, %r12d
movq %rbp, 0x30(%rsp)
je 0x160f2
movl %eax, 0x38(%rsp)
xorl %ebx, %ebx
xorl %r14d, %r14d
movslq %ebx, %rax
leaq (%r15,%rax,8), %rdi
movl %r12d, %esi
subl %eax, %esi
callq 0x77f5
cmpl %r12d, %r14d
movq %r12, 0x8(%rsp)
movl %r12d, %ebx
jge 0x160e2
movl %ebx, 0x4(%rsp)
testl %r14d, %r14d
jle 0x1609f
movl %r14d, %ebx
decl %r14d
movslq %ebx, %r12
movq (%r15,%r12,8), %rax
movl %r14d, %r13d
movq 0x28(%rsp), %rcx
cmpl %ecx, 0x14(%rax)
jle 0x15d5a
movq (%r15,%r13,8), %rcx
movq 0x28(%rsp), %rdx
cmpl %edx, 0x14(%rcx)
jg 0x15fbe
movq (%rax), %rdi
movl 0x8(%rax), %esi
movq (%r15,%r13,8), %rax
movq (%rax), %rdx
movl 0x8(%rax), %ecx
callq 0x2ce2
testl %eax, %eax
jne 0x15fbe
movq (%r15,%r13,8), %rax
movq (%rax), %rdi
movl 0x8(%rax), %esi
movq (%r15,%r12,8), %rax
movq (%rax), %rdx
movl 0x8(%rax), %ecx
callq 0x2ce2
testl %eax, %eax
jne 0x15fbe
movq (%r15,%r12,8), %rdi
movq (%r15,%r13,8), %rsi
movq %rbp, %rdx
movl 0x14(%rsp), %ecx
callq 0x7cbb
movq %rax, %r13
testq %rax, %rax
je 0x15fb6
movl 0xc(%r13), %eax
cmpl 0x14(%rsp), %eax
jg 0x15fb6
cmpl 0x38(%rsp), %eax
jge 0x15e2a
movq 0x8(%rsp), %rax
testl %eax, %eax
jle 0x15e3f
movl %eax, %ebp
xorl %r12d, %r12d
movq (%r15,%r12,8), %rax
movl 0x8(%rax), %ecx
movl 0x8(%r13), %esi
cmpl %esi, %ecx
jl 0x15e18
movl 0x14(%rax), %edx
cmpl 0x14(%r13), %edx
jg 0x15e18
movl 0xc(%r13), %edx
cmpl %edx, 0xc(%rax)
jge 0x15e0d
movq (%r13), %rdi
movq (%rax), %rdx
callq 0x2ce2
testl %eax, %eax
je 0x15e18
jmp 0x15e23
jne 0x15e18
movl 0x10(%rax), %edx
cmpl 0x10(%r13), %edx
jle 0x15dfb
incq %r12
cmpq %r12, %rbp
jne 0x15dda
movl %ebp, %r12d
movq 0x30(%rsp), %rbp
jmp 0x15e42
jne 0x15fb6
movl 0x24(%rsp), %eax
cmpl %eax, 0x10(%r13)
jl 0x15dcc
jmp 0x15fb6
xorl %r12d, %r12d
cmpl 0x8(%rsp), %r12d
jne 0x15fb6
movq %r13, %rdi
movq %rbp, %rsi
movq 0x58(%rsp), %rdx
movq 0x50(%rsp), %rcx
callq 0x80b0
movq %rax, %rbp
movq %r13, %rdi
callq 0x7060
cmpl $0x0, 0x4(%rsp)
jle 0x15fcc
xorl %r13d, %r13d
movl 0x8(%rbp), %ecx
movslq %r13d, %r12
movq (%r15,%r12,8), %rax
movl 0x8(%rax), %esi
cmpl %esi, %ecx
jl 0x15e94
movl 0x14(%rbp), %edx
cmpl 0x14(%rax), %edx
jle 0x15ea3
incl %r13d
cmpl 0x4(%rsp), %r13d
jl 0x15e7b
jmp 0x15fcf
movl 0xc(%rax), %edx
cmpl %edx, 0xc(%rbp)
jge 0x15f14
movq (%rax), %rdi
movq (%rbp), %rdx
callq 0x2ce2
testl %eax, %eax
je 0x15e94
movq (%r15,%r12,8), %rdi
callq 0x7060
movslq 0x4(%rsp), %rcx
movslq 0x8(%rsp), %rax
leal -0x1(%rcx), %edx
cmpl %r14d, %r12d
jge 0x15f28
movl %edx, 0x4(%rsp)
movslq %r14d, %rdx
decl %r14d
movq -0x8(%r15,%rdx,8), %rsi
movq %rsi, (%r15,%r12,8)
movq (%r15,%rdx,8), %rsi
movq %rsi, -0x8(%r15,%rdx,8)
movslq %ebx, %rsi
decl %ebx
movq -0x8(%r15,%rsi,8), %rdi
movq %rdi, (%r15,%rdx,8)
movq (%r15,%rsi,8), %rdx
movq %rdx, -0x8(%r15,%rsi,8)
movq -0x8(%r15,%rcx,8), %rdx
movq %rdx, (%r15,%rsi,8)
jmp 0x15f51
jne 0x15e94
movl 0x10(%rbp), %edx
cmpl 0x10(%rax), %edx
jg 0x15e94
jmp 0x15eab
cmpl %ebx, %r13d
jge 0x15f68
movl %edx, 0x4(%rsp)
movslq %ebx, %rdx
decl %ebx
movq -0x8(%r15,%rdx,8), %rsi
movq %rsi, (%r15,%r12,8)
movq (%r15,%rdx,8), %rsi
movq %rsi, -0x8(%r15,%rdx,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)
jmp 0x15e97
movl %edx, 0x4(%rsp)
leaq -0x1(%rcx), %rdx
movq -0x8(%r15,%rcx,8), %rcx
cmpl %ebx, %r13d
jne 0x15f9c
movslq %ebx, %rsi
movq %rcx, (%r15,%rsi,8)
decl %ebx
leal -0x1(%rax), %ecx
movq %rcx, 0x8(%rsp)
movq -0x8(%r15,%rax,8), %rax
movq %rax, (%r15,%rdx,8)
xorl %r14d, %r14d
jmp 0x15e97
movq %rcx, (%r15,%r12,8)
leal -0x1(%rax), %ecx
movq %rcx, 0x8(%rsp)
movq -0x8(%r15,%rax,8), %rax
movq %rax, (%r15,%rdx,8)
jmp 0x15e97
movq %r13, %rdi
callq 0x7060
testl %r14d, %r14d
jg 0x15d31
jmp 0x160a2
xorl %r13d, %r13d
cmpl 0x8(%rsp), %r13d
jge 0x1603b
movl 0x8(%rbp), %ecx
movslq %r13d, %r12
movq (%r15,%r12,8), %rax
movl 0x8(%rax), %esi
cmpl %esi, %ecx
jl 0x15fef
movl 0x14(%rbp), %edx
cmpl 0x14(%rax), %edx
jle 0x15ff4
incl %r13d
jmp 0x15fcf
movl 0xc(%rax), %edx
cmpl %edx, 0xc(%rbp)
jge 0x1602f
movq (%rax), %rdi
movq (%rbp), %rdx
callq 0x2ce2
testl %eax, %eax
je 0x15fef
movq (%r15,%r12,8), %rdi
callq 0x7060
movq 0x8(%rsp), %rcx
movslq %ecx, %rax
decl %ecx
movq %rcx, 0x8(%rsp)
movq -0x8(%r15,%rax,8), %rax
movq %rax, (%r15,%r12,8)
jmp 0x15fcf
jne 0x15fef
movl 0x10(%rbp), %edx
cmpl 0x10(%rax), %edx
jg 0x15fef
jmp 0x15ffc
cmpl $0x0, 0x14(%rbp)
jle 0x16078
movl 0x40(%rsp), %eax
cmpl %eax, 0x8(%rsp)
jne 0x16063
addl %eax, %eax
movl %eax, 0x40(%rsp)
movslq %eax, %rsi
shlq $0x3, %rsi
movq %r15, %rdi
callq 0x2200
movq %rax, %r15
movq 0x8(%rsp), %rcx
movslq %ecx, %rax
movq %rbp, (%r15,%rax,8)
incl %ecx
movq %rcx, 0x8(%rsp)
jmp 0x16095
movq 0x18(%rsp), %rdi
callq 0x7060
movl 0xc(%rbp), %eax
movl %eax, 0x38(%rsp)
movl 0x10(%rbp), %eax
movl %eax, 0x24(%rsp)
movq %rbp, 0x18(%rsp)
movq 0x30(%rsp), %rbp
jmp 0x15fbe
movl %r14d, %ebx
movl %ebx, %r14d
incl %r14d
testb $0xf, %r14b
movl 0x4(%rsp), %ebx
jne 0x160d9
leaq 0x16a85(%rip), %rdi     # 0x2cb3e
movl %r14d, %esi
movl %ebx, %edx
movq 0x8(%rsp), %rcx
xorl %eax, %eax
callq 0x2110
movq 0x1beef(%rip), %rax     # 0x31fc0
movq (%rax), %rdi
callq 0x21e0
cmpl %ebx, %r14d
jl 0x15d21
movq 0x8(%rsp), %r12
cmpl %ebx, %r12d
jne 0x15cff
jmp 0x160f4
xorl %ebx, %ebx
leaq 0x16a57(%rip), %rdi     # 0x2cb52
xorl %ebp, %ebp
movl %r14d, %esi
movl %ebx, %edx
movl %r12d, %ecx
xorl %eax, %eax
callq 0x2110
movq 0x1bead(%rip), %rax     # 0x31fc0
movq (%rax), %rdi
callq 0x21e0
movq 0x18(%rsp), %r12
testq %r12, %r12
je 0x16237
movl %ebx, 0x4(%rsp)
movq 0x50(%rsp), %rbx
movslq %ebx, %rdi
shlq $0x3, %rdi
callq 0x21d0
movq %rax, %r14
movq (%r12), %rdi
movq %r12, 0x18(%rsp)
movl 0x8(%r12), %esi
movq 0x30(%rsp), %rdx
callq 0xbed5
testl %ebx, %ebx
movq 0x58(%rsp), %rbx
movq 0x48(%rsp), %r13
movq %rax, 0x8(%rsp)
jle 0x16197
xorl %r12d, %r12d
xorl %ebp, %ebp
movq (%rbx,%r12,8), %rdi
movq %rax, %rsi
callq 0x912d
movl %ebp, %ecx
movq %rax, (%r14,%rcx,8)
cmpq $0x1, %rax
movq 0x8(%rsp), %rax
sbbl $-0x1, %ebp
incq %r12
cmpq %r12, %r13
jne 0x16171
movq %r14, 0x38(%rsp)
testl %ebp, %ebp
je 0x1624b
leaq 0x44(%rsp), %r14
movq 0x38(%rsp), %rdi
movl %ebp, %esi
movl 0x14(%rsp), %edx
movq 0x28(%rsp), %rcx
movq %r14, %r8
movq %rax, %r9
callq 0x15bbf
movq %rax, %r12
movslq (%r14), %rsi
movl 0x4(%rsp), %ebx
movslq %ebx, %r13
addq %r13, %rsi
shlq $0x3, %rsi
movq %r15, %rdi
callq 0x2200
cmpl $0x0, (%r14)
movq %rax, 0x28(%rsp)
jle 0x16277
leaq (%rax,%r13,8), %r15
xorl %r13d, %r13d
movq 0x18(%rsp), %rbx
movq 0x30(%rsp), %r14
movq (%r12,%r13,8), %rdi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x7e74
movq %rax, (%r15,%r13,8)
movq (%r12,%r13,8), %rdi
callq 0x7060
incq %r13
movslq 0x44(%rsp), %rax
cmpq %rax, %r13
jl 0x16200
movl 0x4(%rsp), %eax
addl %r13d, %eax
movq %rbx, %r14
movl %eax, %ebx
jmp 0x1627c
movslq %ebx, %rsi
shlq $0x3, %rsi
movq %r15, %rdi
callq 0x2200
movq %rax, %rcx
jmp 0x162c3
movl 0x4(%rsp), %ebx
movslq %ebx, %r12
incl %ebx
leaq 0x8(,%r12,8), %rsi
movq %r15, %rdi
callq 0x2200
movq %rax, %rcx
movq 0x18(%rsp), %rax
movq %rcx, 0x28(%rsp)
movq %rax, (%rcx,%r12,8)
jmp 0x1628c
movq 0x18(%rsp), %r14
movq %r12, %rdi
callq 0x2030
movq %r14, %rdi
callq 0x7060
movq 0x8(%rsp), %rdi
callq 0xc453
testl %ebp, %ebp
movq 0x38(%rsp), %r12
jle 0x162b6
movl %ebp, %r14d
xorl %r15d, %r15d
movq (%r12,%r15,8), %rdi
callq 0x7060
incq %r15
cmpq %r15, %r14
jne 0x162a5
movq %r12, %rdi
callq 0x2030
movq 0x28(%rsp), %rcx
movq 0x60(%rsp), %rax
movl %ebx, (%rax)
movq %rcx, %rax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq