// File: /workspace/github_c_makefile_star2_O2/cbouilla[P]AES-attacks-finder/Equation.c
// Function: reduitEq
Equation reduitEq(Equation a)
{	
	Equation p=NulEq();
	if (a->nV>0)
	{
		unsigned char *ajout=malloc(a->nV*sizeof(unsigned char));
		p->var=malloc(a->nV*sizeof(int));
		p->coefV=malloc(a->nV*sizeof(unsigned char));
		int i;
		for (i = 0; i < a->nV; i++) ajout[i]=1;
		for (i = 0; i < a->nV; i++)
		{
			if (ajout[i])
			{
				unsigned char coeff=a->coefV[i];
				int j;
				for (j = i+1; j < a->nV; j++)
				{
					if (a->var[j]==a->var[i])
					{
						ajout[j]=0;
						coeff^=a->coefV[j];
					}
				}
				if (coeff!=0)
				{
					p->var[p->nV]=a->var[i];
					p->coefV[p->nV]=coeff;
					p->nV++;
				}
			}
		}
		free(ajout);
		if (p->nV==0)
		{
			free(p->var);
			free(p->coefV);
		}
	}
	
	if (a->nS>0)
	{
		unsigned char *ajout=malloc(a->nS*sizeof(unsigned char));
		p->S=malloc(a->nS*sizeof(Equation));
		p->coefS=malloc(a->nS*sizeof(unsigned char));
		p->sbox=malloc(a->nS*sizeof(unsigned char));
		Equation *red=malloc(a->nS*sizeof(Equation));
		int i;
		for (i = 0; i < a->nS; i++)
		{
			red[i]=reduitEq(a->S[i]);
			ajout[i]=1;
		}
		for (i = 0; i < a->nS; i++)
		{
			if (ajout[i])
			{
				unsigned char coeff=a->coefS[i];
				int j;
				for (j = i+1; j < a->nS; j++)
				{
					if (a->sbox[j]==a->sbox[i] && EgaleEq(red[i],red[j]))
					{
						ajout[j]=0;
						coeff^=a->coefS[j];
					}
				}
				if (coeff!=0)
				{
					p->coefS[p->nS]=coeff;
					p->sbox[p->nS]=a->sbox[i];
					p->S[p->nS]=copyEq(red[i]);
					p->nS++;
				}
			}
		}
		free(ajout);
		for (i = 0; i < a->nS; i++) freeEq(red[i]);
		free(red);
		if (p->nS==0)
		{
			free(p->S);
			free(p->sbox);
			free(p->coefS);
		}
	}
	
	if (p->nS==0) return p;
	else
	{
		Equation pp=malloc(sizeof(EQUATION));
		pp->nV=p->nV;
		if (p->nV>0)
		{
			pp->var=malloc(p->nV*sizeof(int));
			pp->coefV=malloc(p->nV*sizeof(unsigned char));
			int i;
			for (i = 0; i < p->nV; i++)
			{
				pp->var[i]=p->var[i];
				pp->coefV[i]=p->coefV[i];
			}
		}
		else
		{
			pp->var=NULL;
			pp->coefV=NULL;
		}
		pp->nS=0;
		pp->S=malloc(p->nS*sizeof(Equation));
		pp->sbox=malloc(p->nS*sizeof(unsigned char));
		pp->coefS=malloc(p->nS*sizeof(unsigned char));
		Equation tmp=NulEq();
		int i;
		for (i = 0; i < p->nS; i++)
		{
			int res=(p->S[i]->nS==1 && p->S[i]->nV==0);
			if (res) res=(p->S[i]->coefS[0]==0x01 && p->S[i]->sbox[0]!=p->sbox[i]);
			if (res)
			{
				Equation tmp1=mulEq(p->coefS[i],p->S[i]->S[0]);
				Equation tmp2=addEq(tmp1,tmp);
				freeEq(tmp);
				freeEq(tmp1);
				tmp=tmp2;
			}
			else
			{
				pp->S[pp->nS]=copyEq(p->S[i]);
				pp->coefS[pp->nS]=p->coefS[i];
				pp->sbox[pp->nS]=p->sbox[i];
				pp->nS++;
			}
		}
		if (pp->nS==0)
		{
			free(pp->S);
			free(pp->sbox);
			free(pp->coefS);
		}
		freeEq(p);
		if (EstNulEq(tmp))
		{
			freeEq(tmp);
			return pp;
		}
		else
		{
			p=addEq(tmp,pp);
			freeEq(tmp);
			freeEq(pp);
			return p;
		}
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rdi, %r14
callq 0x4271
movq %rax, %rbx
movslq (%r14), %r15
testq %r15, %r15
jle 0x47f4
movq %r15, %rdi
callq 0x21c0
movq %rax, %r13
leaq (,%r15,4), %rdi
callq 0x21c0
movq %rax, %r12
movq %rax, 0x8(%rbx)
movq %r15, %rdi
callq 0x21c0
movq %rax, 0x10(%rbx)
xorl %ecx, %ecx
cmpq %rcx, %r15
je 0x4778
movb $0x1, (%r13,%rcx)
incq %rcx
jmp 0x4768
xorl %ecx, %ecx
cmpq %r15, %rcx
jae 0x47d6
cmpb $0x0, (%r13,%rcx)
je 0x47d1
movq 0x10(%r14), %rsi
movb (%rsi,%rcx), %dl
movq %rcx, %rdi
leal 0x1(%rdi), %r8d
cmpl %r8d, %r15d
jle 0x47b7
movq 0x8(%r14), %r8
movl 0x4(%r8,%rdi,4), %r9d
incq %rdi
cmpl (%r8,%rcx,4), %r9d
jne 0x4791
movb $0x0, (%r13,%rdi)
xorb (%rsi,%rdi), %dl
jmp 0x4791
testb %dl, %dl
je 0x47d1
movq 0x8(%r14), %rsi
movl (%rsi,%rcx,4), %esi
movslq (%rbx), %rdi
movl %esi, (%r12,%rdi,4)
movb %dl, (%rax,%rdi)
leal 0x1(%rdi), %edx
movl %edx, (%rbx)
incq %rcx
jmp 0x477a
movq %r13, %rdi
callq 0x2030
cmpl $0x0, (%rbx)
jne 0x47f4
movq %r12, %rdi
callq 0x2030
movq 0x10(%rbx), %rdi
callq 0x2030
movslq 0x18(%r14), %rbp
testq %rbp, %rbp
jle 0x4997
movq %rbp, %rdi
callq 0x21c0
movq %rax, 0x8(%rsp)
leaq (,%rbp,8), %r13
movq %r13, %rdi
callq 0x21c0
movq %rax, 0x10(%rsp)
movq %rax, 0x20(%rbx)
movq %rbp, %rdi
callq 0x21c0
movq %rax, 0x28(%rsp)
movq %rax, 0x28(%rbx)
movq %rbp, %rdi
callq 0x21c0
movq %rbx, 0x18(%rsp)
movq %rax, 0x20(%rsp)
movq %rax, 0x30(%rbx)
movq %r13, %rdi
callq 0x21c0
movq %rax, %r13
xorl %ebx, %ebx
movslq %ebp, %rax
cmpq %rax, %rbx
jge 0x4887
movq 0x20(%r14), %rax
movq (%rax,%rbx,8), %rdi
callq 0x4716
movq %rax, (%r13,%rbx,8)
movq 0x8(%rsp), %rax
movb $0x1, (%rax,%rbx)
incq %rbx
movl 0x18(%r14), %ebp
jmp 0x485b
xorl %r12d, %r12d
movslq %ebp, %rax
cmpq %rax, %r12
jge 0x4941
movq 0x8(%rsp), %rax
cmpb $0x0, (%rax,%r12)
je 0x4939
movq 0x28(%r14), %rax
movq %rax, 0x30(%rsp)
movb (%rax,%r12), %bl
movq %r12, %r15
incq %r15
cmpl %r15d, %ebp
jle 0x48f4
movq 0x30(%r14), %rax
movb (%rax,%r15), %cl
cmpb (%rax,%r12), %cl
jne 0x48b6
movq (%r13,%r12,8), %rdi
movq (%r13,%r15,8), %rsi
callq 0x4441
testl %eax, %eax
je 0x48b6
movq 0x8(%rsp), %rax
movb $0x0, (%rax,%r15)
movq 0x30(%rsp), %rax
xorb (%rax,%r15), %bl
jmp 0x48b6
testb %bl, %bl
je 0x4939
movq 0x18(%rsp), %rbp
movslq 0x18(%rbp), %r15
movq 0x28(%rsp), %rax
movb %bl, (%rax,%r15)
movq 0x30(%r14), %rax
movb (%rax,%r12), %al
movq 0x20(%rsp), %rcx
movb %al, (%rcx,%r15)
movq (%r13,%r12,8), %rdi
callq 0x4319
movq 0x10(%rsp), %rcx
movq %rax, (%rcx,%r15,8)
leal 0x1(%r15), %eax
movl %eax, 0x18(%rbp)
movl 0x18(%r14), %ebp
incq %r12
jmp 0x488a
movq 0x8(%rsp), %rdi
callq 0x2030
xorl %ebx, %ebx
movslq %ebp, %rax
cmpq %rax, %rbx
jge 0x4968
movq (%r13,%rbx,8), %rdi
callq 0x42a5
incq %rbx
movl 0x18(%r14), %ebp
jmp 0x494d
movq %r13, %rdi
callq 0x2030
movq 0x18(%rsp), %rbx
cmpl $0x0, 0x18(%rbx)
jne 0x4997
movq 0x10(%rsp), %rdi
callq 0x2030
movq 0x30(%rbx), %rdi
callq 0x2030
movq 0x28(%rbx), %rdi
callq 0x2030
movslq 0x18(%rbx), %r15
testq %r15, %r15
je 0x4b4e
pushq $0x38
popq %rdi
callq 0x21c0
movq %rax, %r14
movslq (%rbx), %r12
movl %r12d, (%rax)
testq %r12, %r12
jle 0x49fe
leaq (,%r12,4), %rdi
callq 0x21c0
movq %rax, %r13
movq %rax, 0x8(%r14)
movq %r12, %rdi
callq 0x21c0
movq %rax, 0x10(%r14)
movq 0x8(%rbx), %rcx
movq 0x10(%rbx), %rdx
xorl %esi, %esi
cmpq %r12, %rsi
jae 0x4a06
movl (%rcx,%rsi,4), %edi
movl %edi, (%r13,%rsi,4)
movb (%rdx,%rsi), %dil
movb %dil, (%rax,%rsi)
incq %rsi
jmp 0x49e4
xorps %xmm0, %xmm0
movups %xmm0, 0x8(%r14)
andl $0x0, 0x18(%r14)
leaq (,%r15,8), %rdi
callq 0x21c0
movq %rax, 0x20(%r14)
movq %r15, %rdi
callq 0x21c0
movq %rax, 0x30(%r14)
movq %r15, %rdi
callq 0x21c0
movq %rax, 0x28(%r14)
xorl %ebp, %ebp
callq 0x4271
movq %rax, %r12
movslq %r15d, %rax
cmpq %rax, %rbp
jge 0x4aee
movq 0x20(%rbx), %rax
movq (%rax,%rbp,8), %rdi
cmpl $0x1, 0x18(%rdi)
jne 0x4a75
cmpl $0x0, (%rdi)
jne 0x4a75
movq 0x28(%rdi), %rax
cmpb $0x1, (%rax)
jne 0x4a75
movq 0x30(%rdi), %rax
movb (%rax), %al
movq 0x30(%rbx), %rcx
cmpb (%rcx,%rbp), %al
jne 0x4aae
callq 0x4319
movq 0x20(%r14), %rcx
movslq 0x18(%r14), %rdx
movq %rax, (%rcx,%rdx,8)
movq 0x28(%rbx), %rax
movb (%rax,%rbp), %al
movq 0x28(%r14), %rcx
movb %al, (%rcx,%rdx)
movq 0x30(%rbx), %rax
movb (%rax,%rbp), %al
movq 0x30(%r14), %rcx
movb %al, (%rcx,%rdx)
leal 0x1(%rdx), %eax
movl %eax, 0x18(%r14)
incq %rbp
jmp 0x4a3e
movq 0x28(%rbx), %rax
movzbl (%rax,%rbp), %eax
movq 0x20(%rdi), %rcx
movq (%rcx), %rsi
movl %eax, %edi
callq 0x45c6
movq %rax, %r15
movq %rax, %rdi
movq %r12, %rsi
callq 0x4b60
movq %rax, %r13
movq %r12, %rdi
callq 0x42a5
movq %r15, %rdi
callq 0x42a5
movl 0x18(%rbx), %r15d
movq %r13, %r12
jmp 0x4aa9
cmpl $0x0, 0x18(%r14)
jne 0x4b10
movq 0x20(%r14), %rdi
callq 0x2030
movq 0x30(%r14), %rdi
callq 0x2030
movq 0x28(%r14), %rdi
callq 0x2030
movq %rbx, %rdi
callq 0x42a5
cmpl $0x0, 0x18(%r12)
jne 0x4b27
cmpl $0x0, (%r12)
je 0x4b43
movq %r12, %rdi
movq %r14, %rsi
callq 0x4b60
movq %rax, %rbx
movq %r12, %rdi
callq 0x42a5
movq %r14, %r12
movq %rbx, %r14
movq %r12, %rdi
callq 0x42a5
movq %r14, %rbx
movq %rbx, %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq