// File: /workspace/github_c_makefile_star2_O1/cbouilla[P]AES-attacks-finder/Equation.c
// Function: Linearise
Equation * Linearise(Equation *E, int taille, int *t)
{
	int cpt,i,j,newvar,k;
	Equation *tmp,*new;
	
	cpt=taille;	
	for (i = 0; i < taille; i++) cpt+=E[i]->nS;
	new=malloc((taille+1)*sizeof(Equation));
	for (i = 0; i < taille; i++) new[i]=copyEq(E[i]);
	newvar=0;
	for (i = 0; i < taille; i++)
	{
		for (j = 0; j < new[i]->nS; j++)
		{
			if (new[i]->S[j]->nS!=0 || new[i]->S[j]->nV!=1)
			{
				freeEq(new[i]->S[j]);
				new[i]->S[j]=varEq(VARIABLES+newvar);
				printf("x[%d]=",VARIABLES+newvar);
				PrintEq(E[i]->S[j]);
				new[taille+newvar]=addEq(E[i]->S[j],new[i]->S[j]);
				printf(";\n");
				newvar++;
				for (k = 0; k < taille+newvar; k++)
				{
					/*printf("Eq avant :");
					PrintEq(new[k]);
					getchar();
					printf("A remplacer par %d :",VARIABLES+newvar-1);
					PrintEq(E[i]->S[j]);
					getchar();*/
					RemplaceLinearise(new[k],E[i]->S[j],VARIABLES+newvar-1);
					/*printf("Eq apres :");
					PrintEq(new[k]);
					getchar();*/
				}
				i=taille;
				break;
			}
		}
	}
	if (newvar>0)
	{
		VARIABLES+=newvar;
		tmp=new;
		new=Linearise(tmp,taille+newvar,t);
		for (i = 0; i < taille+newvar; i++) freeEq(tmp[i]);
		free(tmp);
		return new;
	}
	else
	{
		*t=taille;
		return new;
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rdx, 0x10(%rsp)
movq %rdi, %r15
movq %rsi, (%rsp)
movslq %esi, %rbx
leaq 0x8(,%rbx,8), %rdi
callq 0x21d0
movq %rax, %r13
testl %ebx, %ebx
jle 0x5b4b
movl (%rsp), %r14d
xorl %ebx, %ebx
movq (%r15,%rbx,8), %rdi
callq 0x48c5
movq %rax, (%r13,%rbx,8)
incq %rbx
cmpq %rbx, %r14
jne 0x5b35
cmpl $0x0, (%rsp)
jle 0x5cac
movq %r15, 0x18(%rsp)
leaq 0x2c757(%rip), %r12     # 0x322b8
xorl %r15d, %r15d
xorl %eax, %eax
movq %r13, 0x28(%rsp)
movslq %eax, %rdx
movq %r13, %rbx
movq (%r13,%rdx,8), %r13
movslq 0x18(%r13), %rcx
testq %rcx, %rcx
jle 0x5ba2
movq %rdx, 0x20(%rsp)
movq 0x20(%r13), %rdx
xorl %r14d, %r14d
movq (%rdx,%r14,8), %rdi
cmpl $0x0, 0x18(%rdi)
jne 0x5baa
cmpl $0x1, (%rdi)
jne 0x5baa
incq %r14
cmpq %r14, %rcx
jne 0x5b8b
movq %rbx, %r13
jmp 0x5c9f
callq 0x484f
movl (%r12), %edi
addl %r15d, %edi
callq 0x4afc
movq 0x20(%r13), %rcx
movq %rax, (%rcx,%r14,8)
movl (%r12), %esi
addl %r15d, %esi
leaq 0x23589(%rip), %rdi     # 0x2915a
xorl %eax, %eax
callq 0x2110
movq %r15, %rbp
movq 0x18(%rsp), %r15
movq 0x20(%rsp), %r12
movq (%r15,%r12,8), %rax
movq 0x20(%rax), %rax
movq (%rax,%r14,8), %rdi
callq 0x46e3
movq (%r15,%r12,8), %rax
movq 0x20(%rax), %rax
movq (%rax,%r14,8), %rdi
movq 0x20(%r13), %rax
movq (%rax,%r14,8), %rsi
callq 0x5221
movq (%rsp), %r12
leal (%r12,%rbp), %ecx
movslq %ecx, %rcx
movq %rbx, %r13
movq %rax, (%rbx,%rcx,8)
leaq 0x23539(%rip), %rdi     # 0x29161
callq 0x2090
leal 0x1(%rbp), %ecx
movl %ecx, %eax
addl %r12d, %eax
jle 0x5c91
movl %ecx, 0xc(%rsp)
movl %eax, %eax
movq %rax, 0x30(%rsp)
xorl %ebx, %ebx
movq 0x18(%rsp), %r15
leaq 0x2c668(%rip), %r12     # 0x322b8
movq 0x20(%rsp), %r13
movq 0x28(%rsp), %rax
movq (%rax,%rbx,8), %rdi
movq (%r15,%r13,8), %rax
movq 0x20(%rax), %rax
movq (%rax,%r14,8), %rsi
movl (%r12), %edx
addl %ebp, %edx
callq 0x593e
incq %rbx
cmpq %rbx, 0x30(%rsp)
jne 0x5c55
movq (%rsp), %rax
movl 0xc(%rsp), %ecx
movl %ecx, %r15d
movq 0x28(%rsp), %r13
jmp 0x5c9f
movq (%rsp), %rax
movl %ecx, %r15d
leaq 0x2c619(%rip), %r12     # 0x322b8
incl %eax
cmpl (%rsp), %eax
jl 0x5b6b
jmp 0x5caf
xorl %r15d, %r15d
testl %r15d, %r15d
jle 0x5d03
leaq 0x2c5fd(%rip), %rax     # 0x322b8
addl %r15d, (%rax)
movq (%rsp), %rbx
leal (%r15,%rbx), %esi
movq %r13, %rdi
movq 0x10(%rsp), %rdx
callq 0x5afe
movq %rbx, %rcx
movq %rax, %r12
addl %ecx, %r15d
jle 0x5cf6
movl %r15d, %ebx
xorl %r14d, %r14d
movq (%r13,%r14,8), %rdi
callq 0x484f
incq %r14
cmpq %r14, %rbx
jne 0x5ce4
movq %r13, %rdi
callq 0x2030
movq %r12, %r13
jmp 0x5d0e
movq 0x10(%rsp), %rax
movq (%rsp), %rcx
movl %ecx, (%rax)
movq %r13, %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq