// File: /workspace/github_c_makefile_star2_O3/cbouilla[P]AES-attacks-finder/Equation.c
// Function: inverseEq
Equation inverseEq(Equation a, int var)
{
	if (a->nV>0)
	{
		int i;
		for (i = 0; i < a->nV; i++)
		{
			if (a->var[i]==var)
			{
				Equation p=copyEq(a);
				p->nV--;
				if (a->nV==1)
				{
					free(p->var);
					free(p->coefV);
					p->var=NULL;
				}
				else
				{
					p->var[i]=a->var[p->nV];
					p->coefV[i]=a->coefV[p->nV];
				}
				Equation tmp=mulEq(Inverse(a->coefV[i]),p);
				freeEq(p);
				p=reduitEq(tmp);
				freeEq(tmp);
				return p;
			}
		}
	}

	int i;
	for (i = 0; i < a->nS; i++)
	{
		if (dependEq(a->S[i],var))
		{
			Equation p=copyEq(a);
			freeEq(p->S[i]);
			p->nS--;
			if (p->nS==0)
			{
				free(p->S);
				free(p->sbox);
				free(p->coefS);
			}
			else
			{
				if (i!=p->nS)
				{
					p->S[i]=copyEq(p->S[p->nS]);
					freeEq(p->S[p->nS]);
					p->sbox[i]=p->sbox[p->nS];
					p->coefS[i]=p->coefS[p->nS];
				}
			}
			Equation tmp1=mulEq(Inverse(a->coefS[i]),p);
			freeEq(p);
			p=appliqueS(tmp1,0x01^a->sbox[i]);
			freeEq(tmp1);
			tmp1=addEq(a->S[i],p);
			freeEq(p);
			p=inverseEq(tmp1,var);
			freeEq(tmp1);
			return p;
		}
	}
	return NULL;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movl %esi, %ebp
movq %rdi, %rbx
movslq (%rdi), %rax
testq %rax, %rax
jle 0x5ade
movq 0x8(%rbx), %rcx
xorl %r15d, %r15d
cmpl %ebp, (%rcx,%r15,4)
je 0x5b0e
incq %r15
cmpq %r15, %rax
jne 0x5ad0
movslq 0x18(%rbx), %r15
testq %r15, %r15
jle 0x5b4a
movq 0x20(%rbx), %r13
xorl %r14d, %r14d
xorl %r12d, %r12d
movq (%r13,%r12,8), %rdi
movl %ebp, %esi
callq 0x5874
testl %eax, %eax
jne 0x5b52
incq %r12
cmpq %r12, %r15
jne 0x5af1
jmp 0x5c9e
movq %rbx, %rdi
callq 0x4b4b
movq %rax, %r14
movslq (%rax), %rax
leal -0x1(%rax), %ecx
movl %ecx, (%r14)
cmpl $0x1, (%rbx)
jne 0x5bc0
movq 0x8(%r14), %rdi
callq 0x2030
movq 0x10(%r14), %rdi
callq 0x2030
movq $0x0, 0x8(%r14)
jmp 0x5be0
xorl %r14d, %r14d
jmp 0x5c9e
movq %rbx, %rdi
callq 0x4b4b
movq %rax, %r14
movq 0x20(%rax), %r15
movq (%r15,%r12,8), %rdi
callq 0x4ad9
movslq 0x18(%r14), %r13
movl %r13d, %eax
decl %eax
movl %eax, 0x18(%r14)
je 0x5c11
movl %r12d, %eax
subl %r13d, %eax
cmpl $-0x1, %eax
je 0x5c2b
movq -0x8(%r15,%r13,8), %rdi
callq 0x4b4b
movq %rax, (%r15,%r12,8)
movq -0x8(%r15,%r13,8), %rdi
callq 0x4ad9
movq 0x30(%r14), %rax
movb -0x1(%rax,%r13), %cl
movb %cl, (%rax,%r12)
movq 0x28(%r14), %rax
movb -0x1(%rax,%r13), %cl
movb %cl, (%rax,%r12)
jmp 0x5c2b
movq 0x8(%rbx), %rcx
movl -0x4(%rcx,%rax,4), %ecx
movq 0x8(%r14), %rdx
movl %ecx, (%rdx,%r15,4)
movq 0x10(%rbx), %rcx
movb -0x1(%rcx,%rax), %al
movq 0x10(%r14), %rcx
movb %al, (%rcx,%r15)
movq 0x10(%rbx), %rax
movzbl (%rax,%r15), %edi
callq 0x2534
movzbl %al, %edi
movq %r14, %rsi
callq 0x4e14
movq %rax, %rbx
movq %r14, %rdi
callq 0x4ad9
movq %rbx, %rdi
callq 0x4f82
jmp 0x5c93
movq %r15, %rdi
callq 0x2030
movq 0x30(%r14), %rdi
callq 0x2030
movq 0x28(%r14), %rdi
callq 0x2030
movq 0x28(%rbx), %rax
movzbl (%rax,%r12), %edi
callq 0x2534
movzbl %al, %edi
movq %r14, %rsi
callq 0x4e14
movq %rax, %r15
movq %r14, %rdi
callq 0x4ad9
movq 0x30(%rbx), %rax
movzbl (%rax,%r12), %esi
xorl $0x1, %esi
movq %r15, %rdi
callq 0x57d7
movq %rax, %r14
movq %r15, %rdi
callq 0x4ad9
movq 0x20(%rbx), %rax
movq (%rax,%r12,8), %rdi
movq %r14, %rsi
callq 0x557f
movq %rax, %rbx
movq %r14, %rdi
callq 0x4ad9
movq %rbx, %rdi
movl %ebp, %esi
callq 0x5ab1
movq %rax, %r14
movq %rbx, %rdi
callq 0x4ad9
movq %r14, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq