// File: /workspace/github_c_makefile_star2_O2/cbouilla[P]AES-attacks-finder/Algebre.c
// Function: Base
int Base(unsigned char ** __restrict dst, unsigned char ** __restrict const mat, const int a, const int b) // Met dans dst une famille libre contenue dans la liste de vecteurs mat. Renvoi le nombre de colone de dst.
{
	if (b>0)
	{
		unsigned char ** __restrict m=malloc(a*sizeof(unsigned char *));
		unsigned char * __restrict espace=malloc(a*b*sizeof(unsigned char));
		
		{
			int i;
			for (i = 0; i < a; i++)
			{
				m[i]=espace+i*b;
				int j;
				for (j = 0; j < b; j++) m[i][j]=mat[i][j];
			}
		}
		
		int dim = 0;
		int pivot_c;
		for (pivot_c = 0; pivot_c < b; pivot_c++)
		{
			int new_pivot_l;
			for (new_pivot_l = dim; new_pivot_l < a; new_pivot_l++)
			{
				if (m[new_pivot_l][pivot_c])
				{
					if (new_pivot_l!=dim)
					{
						unsigned char *tmp = m[dim];
						m[dim]=m[new_pivot_l];
						m[new_pivot_l]=tmp;
					}

					int ligne;
					unsigned char * __restrict ligne_pivot = m[dim];
					const unsigned char invp = Inverse(ligne_pivot[pivot_c]);
					for (ligne = new_pivot_l+1; ligne < a; ligne++) // on fait le pivot de Gauss
					{
						if (m[ligne][pivot_c])
						{
							int colonne;
							unsigned char * __restrict ligne_need_update = m[ligne];
							const unsigned char coeff = Multiply(invp,ligne_need_update[pivot_c]);
							for (colonne = pivot_c+1; colonne < b; colonne++)
							{
								//m[ligne][colonne]=Multiply(m[dim][pivot_c],m[ligne][colonne]) ^ Multiply(m[ligne][pivot_c],m[dim][colonne]);
								ligne_need_update[colonne]^=Multiply(coeff,ligne_pivot[colonne]);
							}
						}
					}
					for (ligne = 0; ligne < a; ligne++) dst[ligne][dim]=mat[ligne][pivot_c];
					dim++;
					if (dim != a) break;
					else
					{
						free(espace);
						free(m);
						return dim;
					}
				}
			}
		}
		free(espace);
		free(m);
		return dim;	
	}
	else return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rdi, 0x40(%rsp)
testl %ecx, %ecx
jle 0x30f2
movl %ecx, %ebp
movl %edx, %r13d
movslq %edx, %r14
leaq (,%r14,8), %rdi
movq %rsi, 0x18(%rsp)
callq 0x21c0
movq %rax, %rbx
movl %ebp, %eax
imull %r13d, %eax
movslq %eax, %rdi
callq 0x21c0
movq 0x18(%rsp), %r8
movq %rbx, %r10
movl %ebp, %r11d
xorl %r9d, %r9d
movq %r14, 0x8(%rsp)
testl %r14d, %r14d
movl $0x0, %ebx
cmovgl %r13d, %ebx
movq %rax, %rcx
cmpq %rbx, %r9
je 0x2fb3
movq %r9, %rdx
imulq %r11, %rdx
addq %rax, %rdx
movq %rdx, (%r10,%r9,8)
movq (%r8,%r9,8), %rdx
xorl %esi, %esi
cmpq %rsi, %r11
je 0x2fab
movb (%rdx,%rsi), %dil
movb %dil, (%rcx,%rsi)
incq %rsi
jmp 0x2f99
incq %r9
addq %r11, %rcx
jmp 0x2f80
movq %rax, 0x28(%rsp)
xorl %r15d, %r15d
leaq 0x1404e(%rip), %r14     # 0x17010
xorl %esi, %esi
movq 0x8(%rsp), %rdi
movl %r13d, 0x10(%rsp)
movq %r10, 0x20(%rsp)
movq %r11, 0x38(%rsp)
movq %rbx, 0x30(%rsp)
cmpq %r11, %r15
je 0x30d6
movslq %esi, %r12
leaq (%r10,%r12,8), %rax
xorl %ecx, %ecx
leaq (%r12,%rcx), %rbx
cmpq %rdi, %rbx
jge 0x30ce
movq (%rax,%rcx,8), %rdx
cmpb $0x0, (%rdx,%r15)
jne 0x300c
incq %rcx
jmp 0x2fef
movl %esi, 0x14(%rsp)
testl %ecx, %ecx
je 0x3020
movq (%r10,%r12,8), %rsi
movq %rdx, (%r10,%r12,8)
movq %rsi, (%rax,%rcx,8)
movq (%r10,%r12,8), %r13
movzbl (%r13,%r15), %edi
callq 0x2433
movq 0x20(%rsp), %r10
movzbl %al, %eax
shll $0x8, %eax
movq 0x18(%rsp), %r11
movq 0x8(%rsp), %rdi
leaq 0x1(%rbx), %rcx
cmpq %rdi, %rcx
jge 0x3093
movq 0x8(%r10,%rbx,8), %rdx
movzbl (%rdx,%r15), %esi
movq %rcx, %rbx
testq %rsi, %rsi
je 0x3044
leaq (%rax,%r14), %rdi
movzbl (%rsi,%rdi), %esi
shll $0x8, %esi
movq %r15, %rdi
leal 0x1(%rdi), %r8d
cmpl %ebp, %r8d
jge 0x308e
movzbl 0x1(%r13,%rdi), %r8d
leaq (%rsi,%r14), %r9
movb (%r8,%r9), %r8b
xorb %r8b, 0x1(%rdx,%rdi)
incq %rdi
jmp 0x306d
movq %rcx, %rbx
jmp 0x303f
xorl %eax, %eax
movl 0x10(%rsp), %r13d
movq 0x40(%rsp), %rsi
movq 0x30(%rsp), %rbx
cmpq %rax, %rbx
je 0x30be
movq (%r11,%rax,8), %rcx
movb (%rcx,%r15), %cl
movq (%rsi,%rax,8), %rdx
movb %cl, (%rdx,%r12)
incq %rax
jmp 0x30a4
movl 0x14(%rsp), %esi
incl %esi
cmpl %r13d, %esi
movq 0x38(%rsp), %r11
je 0x30d9
incq %r15
jmp 0x2fdd
movl %esi, %r13d
movq 0x28(%rsp), %rdi
callq 0x2030
movq 0x20(%rsp), %rdi
callq 0x2030
movl %r13d, %eax
jmp 0x30f4
xorl %eax, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq