// File: /workspace/github_c_makefile_star2_O1/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, %ebx
movq %rdi, %r14
movslq (%rdi), %rax
testq %rax, %rax
jle 0x57ef
movq 0x8(%r14), %rcx
cmpl %ebx, (%rcx)
jne 0x5731
movb $0x1, %bpl
xorl %r12d, %r12d
jmp 0x5755
movl $0x1, %edx
cmpq %rdx, %rax
je 0x57e6
leaq 0x1(%rdx), %r12
cmpl %ebx, (%rcx,%rdx,4)
movq %r12, %rdx
jne 0x5736
decq %r12
cmpq %rax, %r12
setb %bpl
movq %r14, %rdi
callq 0x48c5
movq %rax, %r15
movslq (%rax), %rax
leal -0x1(%rax), %ecx
movl %ecx, (%r15)
cmpl $0x1, (%r14)
jne 0x578b
movq 0x8(%r15), %rdi
callq 0x2030
movq 0x10(%r15), %rdi
callq 0x2030
movq $0x0, 0x8(%r15)
jmp 0x57ad
movq 0x8(%r14), %rcx
movl -0x4(%rcx,%rax,4), %eax
movq 0x8(%r15), %rcx
movl %eax, (%rcx,%r12,4)
movq 0x10(%r14), %rax
movslq (%r15), %rcx
movb (%rax,%rcx), %al
movq 0x10(%r15), %rcx
movb %al, (%rcx,%r12)
movq 0x10(%r14), %rax
movzbl (%rax,%r12), %edi
callq 0x2537
movzbl %al, %edi
movq %r15, %rsi
callq 0x4bad
movq %rax, %r12
movq %r15, %rdi
callq 0x484f
movq %r12, %rdi
callq 0x4d1b
movq %rax, %r15
movq %r12, %rdi
callq 0x484f
jmp 0x57ea
seta %bpl
testb %bpl, %bpl
jne 0x581f
movslq 0x18(%r14), %r12
testq %r12, %r12
jle 0x581c
movq 0x20(%r14), %rbp
xorl %r15d, %r15d
xorl %r13d, %r13d
movq (%rbp,%r13,8), %rdi
movl %ebx, %esi
callq 0x54d8
testl %eax, %eax
jne 0x5827
incq %r13
cmpq %r13, %r12
jne 0x5802
jmp 0x581f
xorl %r15d, %r15d
movq %r15, %rax
jmp 0x592f
movq %r14, %rdi
callq 0x48c5
movq %rax, %r15
movq 0x20(%rax), %rax
movq (%rax,%r13,8), %rdi
callq 0x484f
movslq 0x18(%r15), %rax
movl %eax, %ecx
decl %ecx
movl %ecx, 0x18(%r15)
je 0x589e
movl %r13d, %ecx
subl %eax, %ecx
cmpl $-0x1, %ecx
je 0x58b9
movq 0x20(%r15), %rcx
movq -0x8(%rcx,%rax,8), %rdi
callq 0x48c5
movq 0x20(%r15), %rcx
movq %rax, (%rcx,%r13,8)
movq 0x20(%r15), %rax
movslq 0x18(%r15), %rcx
movq (%rax,%rcx,8), %rdi
callq 0x484f
movq 0x30(%r15), %rax
movslq 0x18(%r15), %rcx
movb (%rax,%rcx), %cl
movb %cl, (%rax,%r13)
movq 0x28(%r15), %rax
movslq 0x18(%r15), %rcx
movb (%rax,%rcx), %cl
movb %cl, (%rax,%r13)
jmp 0x58b9
movq 0x20(%r15), %rdi
callq 0x2030
movq 0x30(%r15), %rdi
callq 0x2030
movq 0x28(%r15), %rdi
callq 0x2030
movq 0x28(%r14), %rax
movzbl (%rax,%r13), %edi
callq 0x2537
movzbl %al, %edi
movq %r15, %rsi
callq 0x4bad
movq %rax, %r12
movq %r15, %rdi
callq 0x484f
movq 0x30(%r14), %rax
movzbl (%rax,%r13), %esi
xorl $0x1, %esi
movq %r12, %rdi
callq 0x544b
movq %rax, %r15
movq %r12, %rdi
callq 0x484f
movq 0x20(%r14), %rax
movq (%rax,%r13,8), %rdi
movq %r15, %rsi
callq 0x5221
movq %rax, %r14
movq %r15, %rdi
callq 0x484f
movq %r14, %rdi
movl %ebx, %esi
callq 0x5705
movq %rax, %rbx
movq %r14, %rdi
callq 0x484f
movq %rbx, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq