// File: /workspace/github_c_makefile_star2_O2/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, 0x30(%rsp)
movl %esi, %r15d
movq %rdi, %r12
leal 0x1(%r15), %eax
movl %eax, 0x1c(%rsp)
movslq %esi, %r14
leaq 0x8(,%r14,8), %rdi
callq 0x21c0
movq %rax, 0x8(%rsp)
xorl %ebx, %ebx
testl %r14d, %r14d
movl $0x0, %r14d
movq %r15, 0x10(%rsp)
cmovgl %r15d, %r14d
cmpq %rbx, %r14
je 0x5489
movq (%r12,%rbx,8), %rdi
callq 0x4319
movq 0x8(%rsp), %rcx
movq %rax, (%rcx,%rbx,8)
incq %rbx
jmp 0x546d
movl 0x1c(%rsp), %esi
movq 0x10(%rsp), %rax
movq %rax, 0x20(%rsp)
xorl %eax, %eax
xorl %r13d, %r13d
testl %esi, %esi
movl $0x0, %r15d
cmovgl %esi, %r15d
cmpl 0x10(%rsp), %eax
jge 0x55d4
movslq %eax, %r14
movq 0x8(%rsp), %rcx
movq (%rcx,%r14,8), %rbx
movl 0x18(%rbx), %ecx
testl %ecx, %ecx
movl $0x0, %edx
cmovlel %edx, %ecx
xorl %edx, %edx
cmpq %rdx, %rcx
je 0x54ee
movq %rdx, %rbp
movq 0x20(%rbx), %rdx
movq (%rdx,%rbp,8), %rdi
cmpl $0x0, 0x18(%rdi)
jne 0x54f2
leaq 0x1(%rbp), %rdx
cmpl $0x1, (%rdi)
je 0x54cd
jmp 0x54f2
incl %eax
jmp 0x54a8
movl %esi, 0x2c(%rsp)
callq 0x42a5
leaq 0x2adb6(%rip), %rax     # 0x302b8
movl (%rax), %edi
addl %r13d, %edi
callq 0x451b
movq 0x20(%rbx), %rcx
movq %rax, (%rcx,%rbp,8)
leaq 0x2ad9d(%rip), %rax     # 0x302b8
movl (%rax), %esi
addl %r13d, %esi
leaq 0x21c33(%rip), %rdi     # 0x2715a
xorl %eax, %eax
callq 0x2110
movq (%r12,%r14,8), %rax
movq 0x20(%rax), %rax
movq (%rax,%rbp,8), %rdi
callq 0x414c
movq (%r12,%r14,8), %rax
movq 0x20(%rax), %rax
movq (%rax,%rbp,8), %rdi
movq 0x20(%rbx), %rax
movq (%rax,%rbp,8), %rsi
callq 0x4b60
movq 0x10(%rsp), %rcx
addl %r13d, %ecx
movslq %ecx, %rcx
movq 0x8(%rsp), %rdx
movq %rax, (%rdx,%rcx,8)
leaq 0x21bee(%rip), %rdi     # 0x27161
callq 0x2090
leal 0x1(%r13), %eax
movl %eax, 0x28(%rsp)
xorl %ebx, %ebx
cmpq %rbx, %r15
je 0x55b2
movq 0x8(%rsp), %rax
movq (%rax,%rbx,8), %rdi
movq (%r12,%r14,8), %rax
movq 0x20(%rax), %rax
movq (%rax,%rbp,8), %rsi
leaq 0x2ad15(%rip), %rax     # 0x302b8
movl (%rax), %edx
addl %r13d, %edx
callq 0x526f
incq %rbx
jmp 0x5582
movq 0x20(%rsp), %rax
incl %eax
movq %rax, 0x20(%rsp)
movl 0x2c(%rsp), %esi
incl %esi
movl 0x1c(%rsp), %eax
movl 0x28(%rsp), %ecx
movl %ecx, %r13d
jmp 0x549c
testl %r13d, %r13d
je 0x562f
leaq 0x2acd8(%rip), %rax     # 0x302b8
addl %r13d, (%rax)
movq 0x10(%rsp), %rsi
addl %r13d, %esi
movq 0x8(%rsp), %rdi
movq 0x30(%rsp), %rdx
callq 0x5423
movq %rax, %r14
xorl %ebx, %ebx
movq 0x20(%rsp), %r15
testl %r15d, %r15d
cmovlel %ebx, %r15d
cmpq %rbx, %r15
je 0x5623
movq 0x8(%rsp), %rax
movq (%rax,%rbx,8), %rdi
callq 0x42a5
incq %rbx
jmp 0x560b
movq 0x8(%rsp), %rdi
callq 0x2030
jmp 0x5640
movq 0x30(%rsp), %rax
movq 0x10(%rsp), %rcx
movl %ecx, (%rax)
movq 0x8(%rsp), %r14
movq %r14, %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq