// File: /workspace/github_c_makefile_star2_O1/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
movl $0x38, %edi
callq 0x21d0
movq %rax, %rbx
xorl %eax, %eax
movl %eax, (%rbx)
xorps %xmm0, %xmm0
movups %xmm0, 0x8(%rbx)
movl %eax, 0x18(%rbx)
movups %xmm0, 0x20(%rbx)
movq $0x0, 0x30(%rbx)
movslq (%r14), %r12
testq %r12, %r12
jle 0x4e32
movq %r12, %rdi
callq 0x21d0
movq %rax, %r15
leaq (,%r12,4), %rdi
callq 0x21d0
movq %rax, 0x8(%rbx)
movq %r12, %rdi
callq 0x21d0
movq %rax, 0x10(%rbx)
movq %r15, %rdi
movl $0x1, %esi
movq %r12, %rdx
callq 0x2120
movl (%r14), %esi
testl %esi, %esi
jle 0x4e13
movl $0x1, %eax
xorl %ecx, %ecx
cmpb $0x0, (%r15,%rcx)
je 0x4e05
movq 0x10(%r14), %rdi
movb (%rdi,%rcx), %dl
leaq 0x1(%rcx), %r8
movslq %esi, %r9
cmpq %r9, %r8
jge 0x4de3
movq 0x8(%r14), %r8
movl (%r8,%rcx,4), %r9d
movl %esi, %esi
movq %rax, %r10
cmpl %r9d, (%r8,%r10,4)
jne 0x4ddb
movb $0x0, (%r15,%r10)
xorb (%rdi,%r10), %dl
incq %r10
cmpq %r10, %rsi
jne 0x4dcc
testb %dl, %dl
je 0x4e05
movq 0x8(%r14), %rsi
movl (%rsi,%rcx,4), %esi
movq 0x8(%rbx), %rdi
movslq (%rbx), %r8
movl %esi, (%rdi,%r8,4)
movq 0x10(%rbx), %rsi
movslq (%rbx), %rdi
movb %dl, (%rsi,%rdi)
incl (%rbx)
incq %rcx
movslq (%r14), %rsi
incq %rax
cmpq %rsi, %rcx
jl 0x4da5
movq %r15, %rdi
callq 0x2030
cmpl $0x0, (%rbx)
jne 0x4e32
movq 0x8(%rbx), %rdi
callq 0x2030
movq 0x10(%rbx), %rdi
callq 0x2030
movslq 0x18(%r14), %r15
testq %r15, %r15
movq %rbx, 0x8(%rsp)
jle 0x500a
movq %r15, %rdi
callq 0x21d0
movq %rax, %r12
movq %r14, 0x18(%rsp)
leaq (,%r15,8), %r13
movq %r13, %rdi
callq 0x21d0
movq %rax, 0x20(%rbx)
movq %r15, %rdi
callq 0x21d0
movq %rax, 0x28(%rbx)
movq %r15, %rdi
callq 0x21d0
movq %rax, 0x30(%rbx)
movq %r13, %rdi
movq 0x18(%rsp), %r15
callq 0x21d0
movq %rax, %r14
cmpl $0x0, 0x18(%r15)
jle 0x4ebb
xorl %ebx, %ebx
movq 0x20(%r15), %rax
movq (%rax,%rbx,8), %rdi
callq 0x4d1b
movq %rax, (%r14,%rbx,8)
movb $0x1, (%r12,%rbx)
incq %rbx
movslq 0x18(%r15), %rax
cmpq %rax, %rbx
jl 0x4e99
movq %r14, 0x10(%rsp)
movl 0x18(%r15), %eax
testl %eax, %eax
jle 0x4fb1
movl $0x1, %r13d
xorl %ebp, %ebp
movq %r12, 0x28(%rsp)
cmpb $0x0, (%r12,%rbp)
je 0x4f9e
movq %r13, 0x20(%rsp)
movq 0x28(%r15), %rcx
movq %rcx, 0x30(%rsp)
movb (%rcx,%rbp), %bl
leaq 0x1(%rbp), %rcx
movslq %eax, %rdx
cmpq %rdx, %rcx
jge 0x4f48
movq 0x30(%r15), %r12
movb (%r12,%rbp), %r13b
movl %eax, %r14d
movq 0x20(%rsp), %r15
cmpb %r13b, (%r12,%r15)
jne 0x4f40
movq 0x10(%rsp), %rax
movq (%rax,%rbp,8), %rdi
movq (%rax,%r15,8), %rsi
callq 0x49db
testl %eax, %eax
je 0x4f40
movq 0x28(%rsp), %rax
movb $0x0, (%rax,%r15)
movq 0x30(%rsp), %rax
xorb (%rax,%r15), %bl
incq %r15
cmpq %r15, %r14
jne 0x4f11
testb %bl, %bl
movq 0x18(%rsp), %r15
movq 0x28(%rsp), %r12
movq 0x20(%rsp), %r13
je 0x4f9e
movq 0x8(%rsp), %r14
movq 0x28(%r14), %rax
movslq 0x18(%r14), %rcx
movb %bl, (%rax,%rcx)
movq 0x30(%r15), %rax
movb (%rax,%rbp), %al
movq 0x30(%r14), %rcx
movslq 0x18(%r14), %rdx
movb %al, (%rcx,%rdx)
movq 0x10(%rsp), %rax
movq (%rax,%rbp,8), %rdi
callq 0x48c5
movq 0x20(%r14), %rcx
movslq 0x18(%r14), %rdx
movq %rax, (%rcx,%rdx,8)
leal 0x1(%rdx), %eax
movl %eax, 0x18(%r14)
incq %rbp
movslq 0x18(%r15), %rax
incq %r13
cmpq %rax, %rbp
jl 0x4ed9
movq %r12, %rdi
callq 0x2030
cmpl $0x0, 0x18(%r15)
movq 0x10(%rsp), %r14
jle 0x4fdc
xorl %ebx, %ebx
movq (%r14,%rbx,8), %rdi
callq 0x484f
incq %rbx
movslq 0x18(%r15), %rax
cmpq %rax, %rbx
jl 0x4fc7
movq %r14, %rdi
callq 0x2030
movq 0x8(%rsp), %rbx
cmpl $0x0, 0x18(%rbx)
jne 0x500a
movq 0x20(%rbx), %rdi
callq 0x2030
movq 0x30(%rbx), %rdi
callq 0x2030
movq 0x28(%rbx), %rdi
callq 0x2030
cmpl $0x0, 0x18(%rbx)
je 0x520f
movl $0x38, %edi
callq 0x21d0
movq %rax, %r14
movslq (%rbx), %r15
movl %r15d, (%rax)
testq %r15, %r15
jle 0x5079
leaq (,%r15,4), %rdi
callq 0x21d0
movq %rax, 0x8(%r14)
movq %r15, %rdi
callq 0x21d0
movq %rax, 0x10(%r14)
cmpl $0x0, (%rbx)
jle 0x5081
xorl %eax, %eax
movq 0x8(%rbx), %rcx
movl (%rcx,%rax,4), %ecx
movq 0x8(%r14), %rdx
movl %ecx, (%rdx,%rax,4)
movq 0x10(%rbx), %rcx
movb (%rcx,%rax), %cl
movq 0x10(%r14), %rdx
movb %cl, (%rdx,%rax)
incq %rax
movslq (%rbx), %rcx
cmpq %rcx, %rax
jl 0x5050
jmp 0x5081
xorps %xmm0, %xmm0
movups %xmm0, 0x8(%r14)
xorl %ebx, %ebx
movl %ebx, 0x18(%r14)
movq 0x8(%rsp), %rax
movslq 0x18(%rax), %r15
leaq (,%r15,8), %rdi
callq 0x21d0
movq %rax, 0x20(%r14)
movq %r15, %rdi
callq 0x21d0
movq %rax, 0x30(%r14)
movq %r15, %rdi
callq 0x21d0
movq %rax, 0x28(%r14)
movl $0x38, %edi
callq 0x21d0
movq %rax, %r13
movl %ebx, (%rax)
xorps %xmm0, %xmm0
movups %xmm0, 0x8(%rax)
movl %ebx, 0x18(%rax)
movups %xmm0, 0x20(%rax)
movq $0x0, 0x30(%rax)
movq 0x8(%rsp), %rax
cmpl %ebx, 0x18(%rax)
movq 0x8(%rsp), %rbx
jle 0x51b0
movq %rbx, %r15
addq $0x20, %r15
xorl %ebp, %ebp
movq (%r15), %rax
movq (%rax,%rbp,8), %rdi
cmpl $0x1, 0x18(%rdi)
jne 0x510f
cmpl $0x0, (%rdi)
sete %al
jmp 0x5111
xorl %eax, %eax
testb %al, %al
je 0x512e
movq 0x28(%rdi), %rax
cmpb $0x1, (%rax)
jne 0x516b
movq 0x30(%rdi), %rax
movb (%rax), %al
movq 0x30(%rbx), %rcx
cmpb (%rcx,%rbp), %al
setne %al
testb %al, %al
je 0x516b
movq 0x28(%rbx), %rax
movzbl (%rax,%rbp), %eax
movq 0x20(%rdi), %rcx
movq (%rcx), %rsi
movl %eax, %edi
callq 0x4bad
movq %rax, %r12
movq %rax, %rdi
movq %r13, %rsi
callq 0x5221
movq %r13, %rdi
movq %rax, %r13
callq 0x484f
movq %r12, %rdi
callq 0x484f
jmp 0x51a0
callq 0x48c5
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
movslq 0x18(%r14), %rdx
movb %al, (%rcx,%rdx)
incl 0x18(%r14)
incq %rbp
movslq 0x18(%rbx), %rax
cmpq %rax, %rbp
jl 0x50fa
cmpl $0x0, 0x18(%r14)
jne 0x51d2
movq 0x20(%r14), %rdi
callq 0x2030
movq 0x30(%r14), %rdi
callq 0x2030
movq 0x28(%r14), %rdi
callq 0x2030
movq %rbx, %rdi
callq 0x484f
cmpl $0x0, 0x18(%r13)
jne 0x51e8
cmpl $0x0, (%r13)
je 0x5204
movq %r13, %rdi
movq %r14, %rsi
callq 0x5221
movq %rax, %rbx
movq %r13, %rdi
callq 0x484f
movq %r14, %r13
movq %rbx, %r14
movq %r13, %rdi
callq 0x484f
movq %r14, %rbx
movq %rbx, %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq