// File: /workspace/github_c_makefile_star2_O3/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 $0x88, %rsp
movq %rdi, %rbx
movl $0x38, %edi
callq 0x21d0
movq %rax, %r12
xorl %r15d, %r15d
movl %r15d, (%rax)
xorps %xmm0, %xmm0
movups %xmm0, 0x8(%rax)
movl %r15d, 0x18(%rax)
movups %xmm0, 0x20(%rax)
movq $0x0, 0x30(%rax)
movslq (%rbx), %r13
testq %r13, %r13
movq %rbx, 0x18(%rsp)
jle 0x50f2
movq %r13, %rdi
callq 0x21d0
movq %rax, %rbp
leaq (,%r13,4), %rdi
callq 0x21d0
movq %rax, 0x28(%rsp)
movq %rax, 0x8(%r12)
movq %r13, %rdi
callq 0x21d0
movq %rax, 0x20(%rsp)
movq %rax, 0x10(%r12)
movq %rbp, %rdi
movl $0x1, %esi
movq %r13, %rdx
callq 0x2120
movq %rbp, %rax
incq %rax
leaq -0x1(%r13), %rcx
movl $0x1, %edx
xorl %r15d, %r15d
movl $0x4, %esi
xorl %r8d, %r8d
cmpb $0x0, (%rbp,%r8)
je 0x50a6
movq 0x10(%rbx), %r10
movb (%r10,%r8), %r9b
leaq 0x1(%r8), %rdi
cmpq %r13, %rdi
jge 0x5070
movq 0x8(%rbx), %r11
movl (%r11,%r8,4), %ebx
addq %rdx, %r10
addq %rsi, %r11
xorl %r14d, %r14d
cmpl %ebx, (%r11,%r14,4)
jne 0x5068
movb $0x0, (%rax,%r14)
xorb (%r10,%r14), %r9b
incq %r14
cmpq %r14, %rcx
jne 0x5059
testb %r9b, %r9b
je 0x50ae
movq 0x18(%rsp), %rbx
movq 0x8(%rbx), %r10
movl (%r10,%r8,4), %r8d
movq %r15, %r10
movslq %r10d, %r10
movq 0x28(%rsp), %r11
movl %r8d, (%r11,%r10,4)
movq 0x20(%rsp), %r8
movb %r9b, (%r8,%r10)
incl %r10d
movq %r10, %r15
movl %r10d, (%r12)
jmp 0x50b3
incq %r8
movq %r8, %rdi
jmp 0x50b3
movq 0x18(%rsp), %rbx
incq %rdx
incq %rax
addq $0x4, %rsi
decq %rcx
movq %rdi, %r8
cmpq %r13, %rdi
jl 0x502f
movq %rbp, %rdi
callq 0x2030
testl %r15d, %r15d
jne 0x5104
movq 0x28(%rsp), %rdi
callq 0x2030
movq 0x20(%rsp), %rdi
callq 0x2030
xorl %r15d, %r15d
jmp 0x5104
movq $0x0, 0x20(%rsp)
movq $0x0, 0x28(%rsp)
movslq 0x18(%rbx), %rbp
testq %rbp, %rbp
jle 0x53b7
movq %r15, 0x70(%rsp)
movq %r12, %rax
addq $0x20, %rax
movq %rax, 0x48(%rsp)
movq %rbp, %rdi
callq 0x21d0
movq %rax, %rbx
leaq (,%rbp,8), %r14
movq %r14, %rdi
callq 0x21d0
movq %rax, 0x58(%rsp)
movq %rax, 0x20(%r12)
movq %rbp, %rdi
callq 0x21d0
movq %rax, 0x60(%rsp)
movq %rax, 0x28(%r12)
movq %rbp, %rdi
movq %rbx, %rbp
callq 0x21d0
movq %rax, 0x50(%rsp)
movq %rax, 0x30(%r12)
movq %r14, %rdi
movq 0x18(%rsp), %r14
callq 0x21d0
movq %rax, %r13
xorl %ebx, %ebx
movq 0x20(%r14), %rax
movq (%rax,%rbx,8), %rdi
callq 0x4f82
movq %rax, (%r13,%rbx,8)
movb $0x1, (%rbp,%rbx)
incq %rbx
movslq 0x18(%r14), %rax
cmpq %rax, %rbx
jl 0x5180
testl %eax, %eax
jle 0x5389
movq %rax, %rbx
movq %r12, 0x10(%rsp)
movl $0x1, %ecx
movq $0x0, 0x8(%rsp)
xorl %r15d, %r15d
movq %r13, 0x40(%rsp)
movq %rbp, 0x38(%rsp)
cmpb $0x0, (%rbp,%r15)
je 0x52b3
movq %rcx, 0x30(%rsp)
movq 0x28(%r14), %rax
movq %rax, 0x80(%rsp)
movb (%rax,%r15), %r13b
leaq 0x1(%r15), %rcx
movslq %ebx, %rax
movq %rcx, 0x68(%rsp)
cmpq %rax, %rcx
movq %rbx, 0x78(%rsp)
jge 0x5251
movq %rbx, %rax
movq 0x30(%r14), %rbx
movb (%rbx,%r15), %r12b
movl %eax, %r14d
movq 0x30(%rsp), %rbp
cmpb %r12b, (%rbx,%rbp)
jne 0x5249
movq 0x40(%rsp), %rax
movq (%rax,%r15,8), %rdi
movq (%rax,%rbp,8), %rsi
callq 0x4c61
testl %eax, %eax
je 0x5249
movq 0x38(%rsp), %rax
movb $0x0, (%rax,%rbp)
movq 0x80(%rsp), %rax
xorb (%rax,%rbp), %r13b
incq %rbp
cmpq %rbp, %r14
jne 0x5218
testb %r13b, %r13b
je 0x52bb
movq 0x8(%rsp), %rbx
movslq %ebx, %rbx
movq 0x60(%rsp), %rax
movb %r13b, (%rax,%rbx)
movq 0x18(%rsp), %r14
movq 0x30(%r14), %rax
movb (%rax,%r15), %al
movq 0x50(%rsp), %rcx
movb %al, (%rcx,%rbx)
movq 0x40(%rsp), %r13
movq (%r13,%r15,8), %rdi
callq 0x4b4b
movq 0x58(%rsp), %rcx
movq %rax, (%rcx,%rbx,8)
incl %ebx
movq 0x10(%rsp), %rax
movq %rbx, 0x8(%rsp)
movl %ebx, 0x18(%rax)
movl 0x18(%r14), %ebx
movq 0x38(%rsp), %rbp
movq 0x30(%rsp), %rcx
jmp 0x52d4
incq %r15
movq %r15, %rdx
jmp 0x52d9
movq 0x18(%rsp), %r14
movq 0x40(%rsp), %r13
movq 0x38(%rsp), %rbp
movq 0x30(%rsp), %rcx
movq 0x78(%rsp), %rbx
movq 0x68(%rsp), %rdx
movslq %ebx, %rax
incq %rcx
movq %rdx, %r15
cmpq %rax, %rdx
jl 0x51ce
movq %rbp, %rdi
callq 0x2030
testl %ebx, %ebx
movq %r14, %r15
movq 0x28(%rsp), %r14
jle 0x5317
xorl %ebx, %ebx
movq (%r13,%rbx,8), %rdi
callq 0x4ad9
incq %rbx
movslq 0x18(%r15), %rax
cmpq %rax, %rbx
jl 0x5301
movq %r13, %rdi
callq 0x2030
cmpl $0x0, 0x8(%rsp)
movq 0x10(%rsp), %r12
movq 0x20(%rsp), %rbx
je 0x5399
movl $0x38, %edi
callq 0x21d0
movq %rax, %r12
movq 0x70(%rsp), %rax
movl %eax, (%r12)
testl %eax, %eax
jle 0x53bf
movl %eax, %r13d
leaq (,%r13,4), %rdi
callq 0x21d0
movq %rax, %rbp
movq %rax, 0x8(%r12)
movq %r13, %rdi
callq 0x21d0
movq %rax, 0x10(%r12)
xorl %ecx, %ecx
movl (%r14,%rcx,4), %edx
movl %edx, (%rbp,%rcx,4)
movb (%rbx,%rcx), %dl
movb %dl, (%rax,%rcx)
incq %rcx
cmpq %r13, %rcx
jb 0x5371
jmp 0x53c8
movq %rbp, %rdi
callq 0x2030
movq %r13, %rdi
callq 0x2030
movq 0x58(%rsp), %rdi
callq 0x2030
movq 0x50(%rsp), %rdi
callq 0x2030
movq 0x60(%rsp), %rdi
callq 0x2030
movq %r12, %rax
jmp 0x5560
xorps %xmm0, %xmm0
movups %xmm0, 0x8(%r12)
xorl %ebx, %ebx
movl %ebx, 0x18(%r12)
movq 0x8(%rsp), %r14
movslq %r14d, %r15
leaq (,%r15,8), %rdi
callq 0x21d0
movq %rax, 0x20(%r12)
movq %r15, %rdi
callq 0x21d0
movq %rax, 0x30(%r12)
movq %r15, %rdi
callq 0x21d0
movq %rax, 0x28(%r12)
movl $0x38, %edi
callq 0x21d0
movq %rax, %rbp
movl %ebx, (%rax)
xorps %xmm0, %xmm0
movups %xmm0, 0x8(%rax)
movl %ebx, 0x18(%rax)
movups %xmm0, 0x20(%rax)
movq $0x0, 0x30(%rax)
testl %r14d, %r14d
jle 0x5508
xorl %ebx, %ebx
movq 0x10(%rsp), %r14
movq 0x48(%rsp), %r15
movq (%r15), %rax
movq (%rax,%rbx,8), %rdi
cmpl $0x1, 0x18(%rdi)
jne 0x5467
cmpl $0x0, (%rdi)
jne 0x5467
movq 0x28(%rdi), %rax
cmpb $0x1, (%rax)
jne 0x5467
movq 0x30(%rdi), %rax
movb (%rax), %al
movq 0x30(%r14), %rcx
cmpb (%rcx,%rbx), %al
jne 0x54af
callq 0x4b4b
movq 0x20(%r12), %rcx
movslq 0x18(%r12), %rdx
movq %rax, (%rcx,%rdx,8)
movq 0x28(%r14), %rax
movb (%rax,%rbx), %al
movq 0x28(%r12), %rcx
movb %al, (%rcx,%rdx)
movq 0x30(%r14), %rax
movb (%rax,%rbx), %al
movq 0x30(%r12), %rcx
movb %al, (%rcx,%rdx)
leal 0x1(%rdx), %eax
movl %eax, 0x18(%r12)
incq %rbx
movslq 0x8(%rsp), %rax
cmpq %rax, %rbx
jl 0x543d
jmp 0x54fe
movq 0x28(%r14), %rax
movzbl (%rax,%rbx), %eax
movq 0x20(%rdi), %rcx
movq (%rcx), %rsi
movl %eax, %edi
callq 0x4e14
movq %rax, %r15
movq %rax, %rdi
movq %rbp, %rsi
callq 0x557f
movq %rax, %r13
movq %rbp, %rdi
callq 0x4ad9
movq %r15, %rdi
movq 0x48(%rsp), %r15
movq 0x10(%rsp), %r14
callq 0x4ad9
movl 0x18(%r14), %eax
movq %rax, 0x8(%rsp)
movq %r13, %rbp
jmp 0x54a0
cmpl $0x0, 0x18(%r12)
jne 0x552b
jmp 0x550d
movq 0x10(%rsp), %r14
movq 0x20(%r12), %rdi
callq 0x2030
movq 0x30(%r12), %rdi
callq 0x2030
movq 0x28(%r12), %rdi
callq 0x2030
movq %r14, %rdi
callq 0x4ad9
cmpl $0x0, 0x18(%rbp)
jne 0x553f
cmpl $0x0, (%rbp)
je 0x5572
movq %rbp, %rdi
movq %r12, %rsi
callq 0x557f
movq %rax, %rbx
movq %rbp, %rdi
callq 0x4ad9
movq %r12, %rdi
callq 0x4ad9
movq %rbx, %rax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rbp, %rdi
callq 0x4ad9
jmp 0x53b7