// File: /workspace/github_c_makefile_star2_O1/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:
testl %ecx, %ecx
jle 0x34ad
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movl %ecx, %ebp
movq %rsi, %r14
movq %rdi, 0x48(%rsp)
movslq %edx, %r15
leaq (,%r15,8), %rdi
movl %edx, 0xc(%rsp)
callq 0x21d0
movq %rax, %rbx
movl %ebp, %eax
imull 0xc(%rsp), %eax
movslq %eax, %rdi
callq 0x21d0
movq %rbx, %r8
movl 0xc(%rsp), %r11d
movl %ebp, %ebx
movl %r11d, %r10d
movq %r15, 0x60(%rsp)
testl %r15d, %r15d
jle 0x32fc
xorl %r9d, %r9d
movq %rax, %rcx
movq %r9, %rdx
imulq %rbx, %rdx
addq %rax, %rdx
movq %rdx, (%r8,%r9,8)
movq (%r14,%r9,8), %rdx
xorl %esi, %esi
movb (%rdx,%rsi), %dil
movb %dil, (%rcx,%rsi)
incq %rsi
cmpq %rsi, %rbx
jne 0x32e1
incq %r9
addq %rbx, %rcx
cmpq %r10, %r9
jne 0x32cd
movq %r10, 0x40(%rsp)
movq %rax, 0x20(%rsp)
movq %r14, 0x50(%rsp)
cmpl $0x2, %ebp
movl $0x1, %esi
cmovgel %ebp, %esi
xorl %r15d, %r15d
movl $0x1, %eax
movq %rax, 0x38(%rsp)
leaq 0x15ce6(%rip), %r14     # 0x19010
xorl %edi, %edi
movq %r8, 0x10(%rsp)
movq %rsi, 0x28(%rsp)
movb $0x1, %r13b
movl %r11d, %ecx
subl %edi, %ecx
jle 0x347a
movslq %edi, %r12
leaq (%r8,%r12,8), %rax
xorl %ebp, %ebp
movq (%rax,%rbp,8), %rdx
cmpb $0x0, (%rdx,%r15)
jne 0x3364
incq %rbp
cmpl %ebp, %ecx
jne 0x334d
jmp 0x347a
testl %ebp, %ebp
je 0x3374
movq (%r8,%r12,8), %rcx
movq %rdx, (%r8,%r12,8)
movq %rcx, (%rax,%rbp,8)
leal 0x1(%rdi), %eax
movq %rax, 0x30(%rsp)
movq (%r8,%r12,8), %r13
movq %rdi, 0x58(%rsp)
movzbl (%r13,%r15), %edi
callq 0x2537
movq 0x58(%rsp), %rcx
addl %ebp, %ecx
incl %ecx
movl 0xc(%rsp), %r11d
cmpl %r11d, %ecx
jge 0x340b
addq 0x30(%rsp), %rbp
movzbl %al, %eax
leaq 0x1(%r15), %rcx
movslq %ebp, %rdx
shll $0x8, %eax
cmpq %rbx, %rcx
setae %r8b
movq 0x10(%rsp), %rsi
movq (%rsi,%rdx,8), %rsi
movzbl (%rsi,%r15), %edi
testq %rdi, %rdi
sete %r9b
orb %r8b, %r9b
jne 0x3401
leaq (%rax,%r14), %r8
movzbl (%rdi,%r8), %edi
shll $0x8, %edi
movq 0x38(%rsp), %r8
movzbl (%r13,%r8), %r9d
leaq (%rdi,%r14), %r10
movb (%r9,%r10), %r9b
xorb %r9b, (%rsi,%r8)
incq %r8
cmpq %r8, %rbx
jne 0x33e7
incq %rdx
cmpq 0x60(%rsp), %rdx
jl 0x33b5
testl %r11d, %r11d
movq 0x50(%rsp), %rsi
movq 0x48(%rsp), %rdi
movq 0x40(%rsp), %r8
jle 0x3439
xorl %eax, %eax
movq (%rsi,%rax,8), %rcx
movb (%rcx,%r15), %cl
movq (%rdi,%rax,8), %rdx
movb %cl, (%rdx,%r12)
incq %rax
cmpq %rax, %r8
jne 0x3421
movq 0x30(%rsp), %rbp
cmpl %r11d, %ebp
setne %r13b
je 0x3450
movl %ebp, %edi
movq 0x10(%rsp), %r8
jmp 0x3475
movq 0x20(%rsp), %rdi
callq 0x2030
movq 0x10(%rsp), %r12
movq %r12, %rdi
callq 0x2030
movq %r12, %r8
movl 0xc(%rsp), %r11d
movl %ebp, %edi
movl %ebp, 0x1c(%rsp)
movq 0x28(%rsp), %rsi
testb %r13b, %r13b
je 0x34b0
incq %r15
incq 0x38(%rsp)
cmpq %rsi, %r15
jne 0x3336
movq %rdi, %r14
movq 0x20(%rsp), %rdi
movq %r8, %rbx
callq 0x2030
movq %rbx, %rdi
callq 0x2030
movq %r14, %rax
jmp 0x34b4
xorl %eax, %eax
retq
movl 0x1c(%rsp), %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq