// File: /workspace/github_c_makefile_star2_O1/cbouilla[P]AES-attacks-finder/SysEqLin.c
// Function: TailleE_Borne
int TailleE_Borne(int * __restrict const X, const int N, SysEqLin E, const int borne) // Renvoi N - dim(E(X)),  N = |X|
{
	//return TailleE(X,N,E);
	/* Beaucoup de variables */
	static int AlloueN = 0;
	static LigneCreuse * __restrict L = NULL;
	static int * __restrict nombre_nnz_colonne = NULL;
	static int AlloueCOL = 0;
	static int AlloueNCOL = 0;
	static int AlloueLIGNE_mat = 0;
	static unsigned char ** __restrict m = NULL;
	static int * __restrict nouvelle_colonne = NULL;
	static int * __restrict colonne_changer = NULL;
	//static int compteur1 = 0, compteur2 = 0, compteur3 = 0, compteur4 = 0;

	if (!E->initialise) InitialiseSousMat(E);
	
	if (N*E->gen>AlloueN)
	{
		free(L);
		AlloueN=N*E->gen;
		//printf("AlloueN : %d\n",AlloueN);
		L=malloc(AlloueN*sizeof(LigneCreuse));
	}
	
	if (E->col_max>AlloueCOL)
	{
		nouvelle_colonne=realloc(nouvelle_colonne,E->col_max*sizeof(int));
		int i;
		for (i = AlloueCOL; i < E->col_max; i++) nouvelle_colonne[i]=-1;
		free(nombre_nnz_colonne);
		AlloueCOL=E->col_max;
		//printf("AlloueCOL : %d\n",AlloueCOL);
		nombre_nnz_colonne=calloc(AlloueCOL,sizeof(int));
	}

	int NombreDeLignes =  0;
	int taille=0,i;
	for (i = 0; i < N; i++)
	{
		if (E->lignevar[X[i]]*E->gen<E->a)
		{
			int j;
			for (j = 0; j < E->sousmat[X[i]]->nombre_de_lignes; j++) L[NombreDeLignes++]=E->sousmat[X[i]]->ligne[j];
			taille++;
		}
	}
	TriLignesCreusesByNNZ(L,NombreDeLignes);
	
	/* 
	 * On compte le nombre d'éléments non-nuls sur chaque colonne
	 */
	int dim = (1-E->gen)*taille;
	int NombreDeColonnes = 0;
	for (i = 0; i < NombreDeLignes; i++)
	{
		unsigned char pivot_present = 0;
		int j;
		for (j = 0; j < L[i]->nnz; j++)
		{
			if (!nombre_nnz_colonne[L[i]->elements[j].colonne])
			{
				pivot_present = 1;
				NombreDeColonnes++;
			}
			nombre_nnz_colonne[L[i]->elements[j].colonne]++;
		}
		if (pivot_present)
		{
			dim++;
			if (dim>borne) // on est sur que la sortie sera plus grande que borne
			{
				for (j = 0; j <= i ; j++) // on remet le tableau à zero
				{
					int k;
					for (k = 0; k < L[j]->nnz; k++) nombre_nnz_colonne[L[j]->elements[k].colonne]=0;
				}
				//compteur1++;
				//printf("cpt1 = %d, cpt2 = %d, cpt3 = %d, cpt4 = %d\n",compteur1,compteur2,compteur3,compteur4);
				return dim;
			}
		}
	}
	
	dim = (1-E->gen)*taille;
	int ligne = 0;
	while (ligne < NombreDeLignes)
	{
		int nombre_element_ligne = 0;
		int colonne_a_enlever;
		int j;
		for (j = 0; j < L[ligne]->nnz; j++)
		{
			if (nombre_nnz_colonne[L[ligne]->elements[j].colonne])
			{
				if (nombre_nnz_colonne[L[ligne]->elements[j].colonne]==1) // 1 seul élément non nul sur la colonne -> très bon pivot pour matrice creuse
				{
					int l;
					for (l = 0; l < L[ligne]->nnz; l++)
					{
						if (nombre_nnz_colonne[L[ligne]->elements[l].colonne])
						{
							if (nombre_nnz_colonne[L[ligne]->elements[l].colonne] == 1) NombreDeColonnes--;
							nombre_nnz_colonne[L[ligne]->elements[l].colonne]--; // On met à jour le nombre d'éléments dans les colonnes
						}
					}
					goto suppr_ligne;
				}
				else
				{
					nombre_element_ligne++;
					colonne_a_enlever = j;
				}
			}
		}
		if (nombre_element_ligne<=1)
		{
			if (nombre_element_ligne==1) // 1 seul élément sur la ligne
			{
				nombre_nnz_colonne[L[ligne]->elements[colonne_a_enlever].colonne] = 0; // on supprime la colonne
				NombreDeColonnes--;
				suppr_ligne: // on met à jour la dim et on supprime la ligne
				dim++;
				if (dim>borne) goto remise_a_zero;
			}
			NombreDeLignes--;
			L[ligne]=L[NombreDeLignes];
		}
		else ligne++;
	}

	{

		if (NombreDeColonnes>AlloueNCOL)
		{
			AlloueNCOL=NombreDeColonnes;
			for (i = 0; i < AlloueLIGNE_mat; i++)
			{
				free(m[i]);
				m[i]=malloc(AlloueNCOL*sizeof(unsigned char));
			}
			free(colonne_changer);
			colonne_changer=malloc((AlloueNCOL+1)*sizeof(int));
		}
		
		if (NombreDeLignes>AlloueLIGNE_mat)
		{
			m = realloc(m,NombreDeLignes*sizeof(unsigned char *));
			for (i = AlloueLIGNE_mat; i < NombreDeLignes; i++) m[i] = malloc(AlloueNCOL*sizeof(unsigned char));
			AlloueLIGNE_mat = NombreDeLignes;
			//printf("AlloueLIGNE_mat : %d\n",AlloueLIGNE_mat);
		}
		
		int pos=0;
		int DerniereColonne = NombreDeColonnes-1;
		int Ncol = 0;
		const int new_borne = borne - dim + 1;
		for (i = 0; i < NombreDeLignes; i++)
		{
			int k;
			unsigned char * __restrict ligne_matrice = m[i];
			for (k = 0; k < NombreDeColonnes; k++) ligne_matrice[k]=0;
			int premier = 1;
			for (k = 0; k < L[i]->nnz; k++)
			{
				if (nombre_nnz_colonne[L[i]->elements[k].colonne])
				{
					nombre_nnz_colonne[L[i]->elements[k].colonne]--;
					int colonne;
					colonne_changer[pos]=L[i]->elements[k].colonne;
					if (nouvelle_colonne[colonne_changer[pos]]>=0) colonne=nouvelle_colonne[colonne_changer[pos]];
					else
					{
						if (!premier)
						{
							nouvelle_colonne[colonne_changer[pos++]]=DerniereColonne;
							colonne=DerniereColonne--;
						}
						else
						{
							nouvelle_colonne[colonne_changer[pos++]]=Ncol;
							colonne=Ncol++;
							if (Ncol != new_borne) premier=0;
							else // on est sur que la sortie sera plus grande que borne
							{
								for (i = i; i < NombreDeLignes; i++)
								{
									for (k = 0; k < L[i]->nnz; k++)	nombre_nnz_colonne[L[i]->elements[k].colonne]=0;								
								}
								for (i = 0; i < pos; i++) nouvelle_colonne[colonne_changer[i]]=-1;
								return borne+1;
							}
						}
					}
					ligne_matrice[colonne]=L[i]->elements[k].coeff;								
				}
			}
		}
				
		for (i = 0; i < pos; i++) nouvelle_colonne[colonne_changer[i]]=-1; // on remet le tableau à zero pour la prochaine execution

		//compteur4++;
		//printf("cpt1 = %d, cpt2 = %d, cpt3 = %d, cpt4 = %d\n",compteur1,compteur2,compteur3,compteur4);
																
		return dim+DimensionBorne_tmp(m,NombreDeLignes,NombreDeColonnes,borne-dim);

	}

	remise_a_zero :
		
	for (ligne = 0; ligne < NombreDeLignes; ligne++)
	{
		int k;
		for (k = 0; k < L[ligne]->nnz; k++) nombre_nnz_colonne[L[ligne]->elements[k].colonne]=0;
	}
	return dim;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movl %ecx, 0x4(%rsp)
movq %rdx, %r12
movl %esi, %r13d
movq %rdi, %r15
cmpb $0x0, 0x28(%rdx)
jne 0xa785
movq %r12, %rdi
callq 0x9bd9
movb $0x1, 0x28(%r12)
movl 0x10(%r12), %eax
imull %r13d, %eax
cmpl 0x27bac(%rip), %eax     # 0x32340
jle 0xa7c4
movq 0x27bab(%rip), %rdi     # 0x32348
callq 0x2030
movslq 0x10(%r12), %rax
movslq %r13d, %rdi
imulq %rax, %rdi
movl %edi, 0x27b8c(%rip)     # 0x32340
shlq $0x3, %rdi
callq 0x21d0
movq %rax, 0x27b84(%rip)     # 0x32348
movslq 0x38(%r12), %rsi
movslq 0x27b88(%rip), %rbx     # 0x32358
cmpl %ebx, %esi
jle 0xa836
movq 0x27b95(%rip), %rdi     # 0x32370
shlq $0x2, %rsi
callq 0x2200
movq %rax, 0x27b85(%rip)     # 0x32370
movl 0x38(%r12), %ecx
cmpl %ecx, %ebx
jge 0xa80e
leaq (%rax,%rbx,4), %rdi
notl %ebx
addl %ebx, %ecx
leaq 0x4(,%rcx,4), %rdx
movl $0xff, %esi
callq 0x2120
movq 0x27b3b(%rip), %rdi     # 0x32350
callq 0x2030
movslq 0x38(%r12), %rdi
movl %edi, 0x27b33(%rip)     # 0x32358
movl $0x4, %esi
callq 0x2160
movq %rax, 0x27b1a(%rip)     # 0x32350
xorl %r14d, %r14d
movl $0x0, (%rsp)
movl $0x0, %ebp
testl %r13d, %r13d
jle 0xa8c7
movl 0x8(%r12), %eax
movl 0x10(%r12), %ecx
movq 0x27aed(%rip), %rdx     # 0x32348
movl %r13d, %esi
xorl %edi, %edi
xorl %ebp, %ebp
movl $0x0, (%rsp)
movq 0x18(%r12), %r9
movslq (%r15,%rdi,4), %r8
movl (%r9,%r8,4), %r9d
imull %ecx, %r9d
cmpl %eax, %r9d
jge 0xa8bf
movq 0x30(%r12), %r9
movq (%r9,%r8,8), %r11
cmpl $0x0, (%r11)
jle 0xa8bd
movslq (%rsp), %r9
leaq (%rdx,%r9,8), %r10
xorl %r9d, %r9d
movq 0x8(%r11), %r11
movq (%r11,%r9,8), %r11
movq %r11, (%r10,%r9,8)
incq %r9
movq 0x30(%r12), %r11
movq (%r11,%r8,8), %r11
movslq (%r11), %r13
cmpq %r13, %r9
jl 0xa899
addl %r9d, (%rsp)
incl %ebp
incq %rdi
cmpq %rsi, %rdi
jne 0xa869
movq 0x27a7a(%rip), %r13     # 0x32348
movq %r13, %rdi
movl (%rsp), %ebx
movl %ebx, %esi
callq 0xa1e8
testl %ebx, %ebx
movq %r13, 0x10(%rsp)
jle 0xa9f5
movl $0x1, %eax
movl $0x1, %ecx
subl 0x10(%r12), %ecx
imull %ebp, %ecx
movq %rcx, 0x18(%rsp)
movq 0x27a4a(%rip), %rcx     # 0x32350
movl %ebx, %edx
movq %rdx, 0x28(%rsp)
xorl %esi, %esi
movl $0x1, %edi
xorl %r14d, %r14d
movq (%r13,%rsi,8), %r10
movb $0x1, %r9b
cmpl $0x0, (%r10)
jle 0xa968
movl %ebp, %r8d
movq 0x8(%r10), %r11
xorl %r15d, %r15d
xorl %ebp, %ebp
movslq 0x4(%r11,%r15,8), %r13
movl (%rcx,%r13,4), %edx
xorl %ebx, %ebx
testl %edx, %edx
sete %bl
movzbl %bpl, %ebp
cmovel %eax, %ebp
addl %ebx, %r14d
incl %edx
movl %edx, (%rcx,%r13,4)
incq %r15
movslq (%r10), %rdx
cmpq %rdx, %r15
jl 0xa931
testb %bpl, %bpl
sete %r10b
movl %r8d, %ebp
jmp 0xa96b
movb $0x1, %r10b
testb %r10b, %r10b
je 0xa977
movq 0x10(%rsp), %r13
jmp 0xa9d9
movq 0x18(%rsp), %rdx
leal 0x1(%rdx), %r10d
cmpl 0x4(%rsp), %edx
movq 0x10(%rsp), %r13
jge 0xa995
movl %r10d, %edx
movq %rdx, 0x18(%rsp)
jmp 0xa9d9
xorl %r8d, %r8d
movq (%r13,%r8,8), %r9
cmpl $0x0, (%r9)
jle 0xa9c1
movq 0x8(%r9), %r11
xorl %r15d, %r15d
movslq 0x4(%r11,%r15,8), %rdx
movl $0x0, (%rcx,%rdx,4)
incq %r15
movslq (%r9), %rdx
cmpq %rdx, %r15
jl 0xa9aa
incq %r8
cmpq %rdi, %r8
jne 0xa998
xorl %r9d, %r9d
movl %r10d, %edx
movq %rdx, 0x18(%rsp)
movl %r10d, 0xc(%rsp)
testb %r9b, %r9b
je 0xae42
incq %rsi
incq %rdi
cmpq 0x28(%rsp), %rsi
jne 0xa917
jmp 0xa9f5
movl $0x1, %r13d
subl 0x10(%r12), %r13d
imull %ebp, %r13d
xorl %eax, %eax
movq 0x27943(%rip), %rcx     # 0x32350
movl 0x4(%rsp), %ebp
movl (%rsp), %r12d
cmpl %r12d, %eax
jge 0xab47
movslq %eax, %rdi
movq 0x10(%rsp), %rsi
movq (%rsi,%rdi,8), %rsi
movslq (%rsi), %r9
testq %r9, %r9
jle 0xaa5d
movq 0x8(%rsi), %r8
xorl %r11d, %r11d
xorl %r10d, %r10d
movslq 0x4(%r8,%r11,8), %rbx
movl (%rcx,%rbx,4), %ebx
testl %ebx, %ebx
je 0xaa53
cmpl $0x1, %ebx
je 0xaa8d
incl %r10d
movl %r11d, %edx
incq %r11
cmpq %r11, %r9
jne 0xaa3c
jmp 0xaa60
xorl %r10d, %r10d
cmpl $0x1, %r10d
jg 0xaa89
movq 0x10(%rsp), %r8
leaq (%r8,%rdi,8), %rdi
jne 0xaad3
movq 0x8(%rsi), %rsi
movslq %edx, %r8
movslq 0x4(%rsi,%r8,8), %rsi
movl $0x0, (%rcx,%rsi,4)
decl %r14d
jmp 0xaac7
incl %eax
jmp 0xaae6
movq 0x10(%rsp), %r9
leaq (%r9,%rdi,8), %rdi
cmpl $0x0, (%rsi)
jle 0xaac7
xorl %r9d, %r9d
movslq 0x4(%r8,%r9,8), %r10
movl (%rcx,%r10,4), %r11d
testl %r11d, %r11d
je 0xaabc
cmpl $0x1, %r11d
jne 0xaab5
decl %r14d
decl %r11d
movl %r11d, (%rcx,%r10,4)
incq %r9
movslq (%rsi), %r10
cmpq %r10, %r9
jl 0xaa9e
leal 0x1(%r13), %r8d
cmpl %r13d, %ebp
jle 0xaaeb
movl %r8d, %r13d
movslq %r12d, %rsi
decl %r12d
movq 0x10(%rsp), %r8
movq -0x8(%r8,%rsi,8), %rsi
movq %rsi, (%rdi)
movb $0x1, %sil
jmp 0xaaf0
xorl %esi, %esi
movl %r8d, %r13d
testb %sil, %sil
jne 0xaa15
testl %r12d, %r12d
jle 0xab3f
movq 0x2784b(%rip), %rax     # 0x32350
movl %r12d, %ecx
xorl %edx, %edx
movq 0x10(%rsp), %r10
movq (%r10,%rdx,8), %rsi
cmpl $0x0, (%rsi)
jle 0xab37
movq 0x8(%rsi), %rdi
xorl %r8d, %r8d
movslq 0x4(%rdi,%r8,8), %r9
movl $0x0, (%rax,%r9,4)
incq %r8
movslq (%rsi), %r9
cmpq %r9, %r8
jl 0xab1f
incq %rdx
cmpq %rcx, %rdx
jne 0xab0f
movl %r13d, %eax
jmp 0xae90
movl %r14d, %eax
movq %rax, 0x38(%rsp)
cmpl 0x27806(%rip), %r14d    # 0x3235c
movl %r12d, (%rsp)
jle 0xabc3
movl %r14d, 0x277f9(%rip)    # 0x3235c
movslq 0x277f6(%rip), %rbx     # 0x32360
testq %rbx, %rbx
movq 0x38(%rsp), %rbp
jle 0xab9b
movq 0x277ed(%rip), %r15     # 0x32368
xorl %r12d, %r12d
movq (%r15,%r12,8), %rdi
callq 0x2030
movq %rbp, %rdi
callq 0x21d0
movq %rax, (%r15,%r12,8)
incq %r12
cmpq %r12, %rbx
jne 0xab7e
movq 0x277d6(%rip), %rdi     # 0x32378
callq 0x2030
leaq 0x4(,%rbp,4), %rdi
callq 0x21d0
movq %rax, 0x277bd(%rip)     # 0x32378
movl 0x4(%rsp), %ebp
movl (%rsp), %r12d
movslq 0x27796(%rip), %r15     # 0x32360
movl %r12d, %eax
cmpl %r15d, %r12d
movq %rax, 0x48(%rsp)
movq %r13, 0x30(%rsp)
jle 0xac2e
movq 0x27785(%rip), %rdi     # 0x32368
leaq (,%rax,8), %rsi
callq 0x2200
movl %r12d, %ecx
movq %rax, %r12
movq %rax, 0x2776b(%rip)     # 0x32368
movl 0x27759(%rip), %ebx     # 0x3235c
movslq %ecx, %r13
movq %rbx, %rdi
callq 0x21d0
movq %rax, (%r12,%r15,8)
incq %r15
cmpq %r15, %r13
jne 0xac06
movl (%rsp), %r12d
movl %r12d, 0x2773b(%rip)    # 0x32360
movl 0x4(%rsp), %ebp
movq 0x30(%rsp), %r13
movl %ebp, %eax
subl %r13d, %eax
movl %eax, 0x24(%rsp)
testl %r12d, %r12d
jle 0xae48
leal -0x1(%r14), %r12d
movq 0x2771d(%rip), %rax     # 0x32368
movq %rax, 0x50(%rsp)
movq 0x276f9(%rip), %rbx     # 0x32350
movq 0x2771a(%rip), %rax     # 0x32378
movq %rax, 0x40(%rsp)
movq 0x27706(%rip), %rax     # 0x32370
movq %rax, 0x18(%rsp)
incl %ebp
movl %ebp, 0x4(%rsp)
xorl %r13d, %r13d
xorl %r10d, %r10d
xorl %r15d, %r15d
movslq %r15d, %rbp
movq 0x50(%rsp), %rax
movq (%rax,%rbp,8), %rcx
testl %r14d, %r14d
movq %rcx, 0x28(%rsp)
jle 0xacaf
movq 0x28(%rsp), %rdi
xorl %esi, %esi
movq 0x38(%rsp), %rdx
movl %r10d, 0x8(%rsp)
callq 0x2120
movl 0x8(%rsp), %r10d
movq 0x10(%rsp), %rax
movq (%rax,%rbp,8), %r8
movq %rax, %rbp
cmpl $0x0, (%r8)
setg %al
jle 0xae2f
xorl %ecx, %ecx
movl $0x1, %edx
movl %r13d, %edi
movl %r10d, %esi
movq 0x8(%r8), %r8
movslq %ecx, %r9
movslq 0x4(%r8,%r9,8), %r10
movl (%rbx,%r10,4), %r11d
testl %r11d, %r11d
je 0xad1f
decl %r11d
movl %r11d, (%rbx,%r10,4)
movslq 0x4(%r8,%r9,8), %r10
movslq %esi, %r11
movq 0x40(%rsp), %r13
movl %r10d, (%r13,%r11,4)
movq 0x18(%rsp), %r11
movl (%r11,%r10,4), %r11d
testl %r11d, %r11d
js 0xad27
movl %esi, %r10d
movl %r12d, %esi
movl %edi, %r13d
movl %r11d, %r12d
jmp 0xad4c
movl %esi, %r10d
movl %edi, %r13d
jmp 0xad71
leal 0x1(%rsi), %r11d
movl %r11d, 0x8(%rsp)
testl %edx, %edx
jne 0xad8c
movq 0x18(%rsp), %rdx
movl %r12d, (%rdx,%r10,4)
leal -0x1(%r12), %esi
xorl %edx, %edx
movl %edi, %r13d
movl 0x8(%rsp), %r10d
movb (%r8,%r9,8), %dil
movslq %r12d, %r8
movq 0x28(%rsp), %r9
movb %dil, (%r9,%r8)
movb $0x1, %r8b
movl %esi, %r12d
movl %r10d, %esi
movl %r13d, %edi
testb %r8b, %r8b
je 0xae2f
incl %ecx
movslq %r15d, %rax
movq (%rbp,%rax,8), %r8
cmpl (%r8), %ecx
setl %al
jl 0xacd5
jmp 0xae2f
movq 0x18(%rsp), %r11
movl %edi, (%r11,%r10,4)
leal 0x1(%rdi), %r13d
xorl %edx, %edx
cmpl 0x24(%rsp), %edi
jne 0xae11
cmpl (%rsp), %r15d
movq 0x48(%rsp), %r10
jge 0xade3
movslq %r15d, %rdx
movq (%rbp,%rdx,8), %rdi
cmpl $0x0, (%rdi)
jle 0xadd9
movq 0x8(%rdi), %r8
xorl %ecx, %ecx
movslq 0x4(%r8,%rcx,8), %r9
movl $0x0, (%rbx,%r9,4)
incq %rcx
movslq (%rdi), %r9
cmpq %r9, %rcx
jl 0xadbf
jmp 0xaddb
xorl %ecx, %ecx
incq %rdx
cmpq %r10, %rdx
jne 0xadaf
movl $0x1, %edx
testl %esi, %esi
movl 0x8(%rsp), %r10d
js 0xae1c
incl %esi
xorl %r15d, %r15d
movq 0x40(%rsp), %r8
movslq (%r8,%r15,4), %rdi
movl $0xffffffff, (%r11,%rdi,4) # imm = 0xFFFFFFFF
incq %r15
cmpq %r15, %rsi
jne 0xadfb
jmp 0xae1f
movl %r12d, %esi
movl %edi, %r12d
jmp 0xad47
xorl %r15d, %r15d
xorl %r8d, %r8d
movl 0x4(%rsp), %esi
movl %esi, 0xc(%rsp)
jmp 0xad62
testb $0x1, %al
jne 0xae42
incl %r15d
cmpl (%rsp), %r15d
jl 0xac7e
jmp 0xae4b
movl 0xc(%rsp), %eax
jmp 0xae90
xorl %r10d, %r10d
testl %r10d, %r10d
jle 0xae76
movq 0x27519(%rip), %rax     # 0x32370
movq 0x2751a(%rip), %rcx     # 0x32378
movl %r10d, %edx
xorl %esi, %esi
movslq (%rcx,%rsi,4), %rdi
movl $0xffffffff, (%rax,%rdi,4) # imm = 0xFFFFFFFF
incq %rsi
cmpq %rsi, %rdx
jne 0xae63
movq 0x274eb(%rip), %rdi     # 0x32368
movl (%rsp), %esi
movl %r14d, %edx
movl 0x24(%rsp), %ecx
callq 0x2ede
addl 0x30(%rsp), %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq