// File: /workspace/github_c_makefile_star2_O3/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, %r15
movl %esi, %r14d
movq %rdi, %r12
movslq %esi, %rbx
leaq 0x8(,%rbx,8), %rdi
callq 0x21d0
movq %rax, %rbp
testl %ebx, %ebx
jle 0x6065
movq %r14, 0x18(%rsp)
movl %r14d, %ebx
xorl %r14d, %r14d
movq (%r12,%r14,8), %rdi
callq 0x4b4b
movq %rax, (%rbp,%r14,8)
incq %r14
cmpq %r14, %rbx
jne 0x5eb1
movq 0x18(%rsp), %r14
testl %r14d, %r14d
jle 0x6065
movq %r15, 0x28(%rsp)
movq %rbp, 0x8(%rsp)
leaq 0x2c3d2(%rip), %r13     # 0x322b8
xorl %ebp, %ebp
xorl %eax, %eax
movslq %eax, %rdx
movq 0x8(%rsp), %rcx
movq (%rcx,%rdx,8), %r15
movslq 0x18(%r15), %rcx
testq %rcx, %rcx
jle 0x6010
movq %rdx, 0x20(%rsp)
movq 0x20(%r15), %rdx
xorl %ebx, %ebx
movq (%rdx,%rbx,8), %rdi
cmpl $0x0, 0x18(%rdi)
jne 0x5f2a
cmpl $0x1, (%rdi)
jne 0x5f2a
incq %rbx
cmpq %rbx, %rcx
jne 0x5f0e
jmp 0x6010
callq 0x4ad9
movl (%r13), %edi
addl %ebp, %edi
callq 0x4d63
movq 0x20(%r15), %rcx
movq %rax, (%rcx,%rbx,8)
movl (%r13), %esi
addl %ebp, %esi
leaq 0x2326b(%rip), %rdi     # 0x291ba
xorl %eax, %eax
callq 0x2110
movq 0x20(%rsp), %r13
movq (%r12,%r13,8), %rax
movq 0x20(%rax), %rax
movq (%rax,%rbx,8), %rdi
callq 0x496d
movq (%r12,%r13,8), %rax
movq 0x20(%rax), %rax
movq (%rax,%rbx,8), %rdi
movq 0x20(%r15), %rax
movq (%rax,%rbx,8), %rsi
callq 0x557f
leal (%r14,%rbp), %ecx
movslq %ecx, %rcx
movq 0x8(%rsp), %rdx
movq %rax, (%rdx,%rcx,8)
leaq 0x23225(%rip), %rdi     # 0x291c1
callq 0x2090
leal 0x1(%rbp), %ecx
movl %ecx, %eax
addl %r14d, %eax
jle 0x6004
movl %ecx, 0x14(%rsp)
movl %eax, %eax
movq %rax, 0x30(%rsp)
xorl %r14d, %r14d
movq %r12, %r13
movq 0x8(%rsp), %r12
movq 0x20(%rsp), %r15
movq (%r12,%r14,8), %rdi
movq (%r13,%r15,8), %rax
movq 0x20(%rax), %rax
movq (%rax,%rbx,8), %rsi
leaq 0x2c2da(%rip), %rax     # 0x322b8
movl (%rax), %edx
addl %ebp, %edx
callq 0x5cb0
incq %r14
cmpq %r14, 0x30(%rsp)
jne 0x5fc6
movq 0x18(%rsp), %r14
movl %r14d, %eax
movl 0x14(%rsp), %ecx
movl %ecx, %ebp
movq %r13, %r12
jmp 0x6009
movl %r14d, %eax
movl %ecx, %ebp
leaq 0x2c2a8(%rip), %r13     # 0x322b8
incl %eax
cmpl %r14d, %eax
jl 0x5eea
testl %ebp, %ebp
movq %rbp, %rax
movq 0x8(%rsp), %rbp
movq 0x28(%rsp), %r15
jle 0x6065
addl %eax, (%r13)
addl %eax, %r14d
movq %rbp, %rdi
movl %r14d, %esi
movq %r15, %rdx
callq 0x5e74
movq %rax, %r15
xorl %ebx, %ebx
movq (%rbp,%rbx,8), %rdi
callq 0x4ad9
incq %rbx
cmpq %rbx, %r14
jne 0x6046
movq %rbp, %rdi
callq 0x2030
movq %r15, %rbp
jmp 0x6068
movl %r14d, (%r15)
movq %rbp, %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq