// File: /workspace/github_c_makefile_star2_O2/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 0x50dc
movq 0x8(%rbx), %rcx
xorl %edx, %edx
cmpq %rdx, %rax
je 0x50dc
leaq 0x1(%rdx), %r15
cmpl %ebp, (%rcx,%rdx,4)
movq %r15, %rdx
jne 0x5092
movq %rbx, %rdi
callq 0x4319
movq %rax, %r14
movslq (%rax), %rax
leal -0x1(%rax), %ecx
movl %ecx, (%r14)
cmpl $0x1, (%rbx)
jne 0x517c
movq 0x8(%r14), %rdi
callq 0x2030
movq 0x10(%r14), %rdi
callq 0x2030
andq $0x0, 0x8(%r14)
jmp 0x519e
movl 0x18(%rbx), %r15d
xorl %r14d, %r14d
testl %r15d, %r15d
cmovlel %r14d, %r15d
xorl %r12d, %r12d
cmpq %r12, %r15
je 0x525d
movq 0x20(%rbx), %rax
movq (%rax,%r12,8), %rdi
movl %ebp, %esi
callq 0x4e21
testl %eax, %eax
jne 0x510e
incq %r12
jmp 0x50ed
movq %rbx, %rdi
callq 0x4319
movq %rax, %r14
movq 0x20(%rax), %r15
movq (%r15,%r12,8), %rdi
callq 0x42a5
movslq 0x18(%r14), %r13
movl %r13d, %eax
decl %eax
movl %eax, 0x18(%r14)
je 0x51d0
movl %r12d, %eax
subl %r13d, %eax
cmpl $-0x1, %eax
je 0x51ea
movq -0x8(%r15,%r13,8), %rdi
callq 0x4319
movq %rax, (%r15,%r12,8)
movq -0x8(%r15,%r13,8), %rdi
callq 0x42a5
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 0x51ea
movq 0x8(%rbx), %rcx
movl -0x4(%rcx,%rax,4), %ecx
movq 0x8(%r14), %rdx
movl %ecx, -0x4(%rdx,%r15,4)
movq 0x10(%rbx), %rcx
movb -0x1(%rcx,%rax), %al
movq 0x10(%r14), %rcx
movb %al, -0x1(%rcx,%r15)
movq 0x10(%rbx), %rax
movzbl -0x1(%rax,%r15), %edi
callq 0x2433
movzbl %al, %edi
movq %r14, %rsi
callq 0x45c6
movq %rax, %rbx
movq %r14, %rdi
callq 0x42a5
movq %rbx, %rdi
callq 0x4716
jmp 0x5252
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 0x2433
movzbl %al, %edi
movq %r14, %rsi
callq 0x45c6
movq %rax, %r15
movq %r14, %rdi
callq 0x42a5
movq 0x30(%rbx), %rax
movzbl (%rax,%r12), %esi
xorl $0x1, %esi
movq %r15, %rdi
callq 0x4d8b
movq %rax, %r14
movq %r15, %rdi
callq 0x42a5
movq 0x20(%rbx), %rax
movq (%rax,%r12,8), %rdi
movq %r14, %rsi
callq 0x4b60
movq %rax, %rbx
movq %r14, %rdi
callq 0x42a5
movq %rbx, %rdi
movl %ebp, %esi
callq 0x5074
movq %rax, %r14
movq %rbx, %rdi
callq 0x42a5
movq %r14, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq