// File: /workspace/github_c_makefile_star2_O1/cbouilla[P]AES-attacks-finder/Solver.c
// Function: Refine
Solver Refine(Solver B, SysEqLin E)
{
	if (B==NULL || B->type==0) return copySolver(B); // cas simple : rien à faire
	
	SysEqLin F = ExtraireEX2(B->var,B->Nvar,E);
	int * __restrict X = malloc(B->Nvar*sizeof(int));
	int n=0;
	int i;
	for (i = 0; i < B->Nvar; i++) if (ApparaitLineairement(B->var[i],F)) X[n++]=B->var[i];
	if (n)
	{
		Solver result = copySolver(B);
		for (i = 0; i < n; i++)
		{
			Solver tmp = DeleteVariable(X[i],result,F);
			freeSolver(result);
			result = tmp;
		}
		
		{
			Solver tmp = Refine(result,F);
			freeSolver(result);
			result = tmp;
		}
		
		for (i = 0; i < n; i++)
		{
			Solver bs = BaseSolver(X[i],F);
			Solver tmp = UnionSolver(result,bs,F);
			freeSolver(result);
			freeSolver(bs);
			result = tmp;
		}
		free(X);
		freeSysEq(F);
		return result;
	}
	else
	{
		free(X);
		if (B->type==1)
		{
			if (B->fils1->Nvar + B->fils2->Nvar != B->Nvar)
			{
				X=IntersectionListesTriees(B->fils1->var,B->fils1->Nvar,B->fils2->var,B->fils2->Nvar,&n);
				Solver f1 = copySolver(B->fils1);
				Solver f2 = copySolver(B->fils2);
				SysEqLin F1 = ExtraireEX2(f1->var,f1->Nvar,F);
				SysEqLin F2 = ExtraireEX2(f2->var,f2->Nvar,F);
				for (i = 0; i < n; i++)
				{
					int l1 = ApparaitLineairement(X[i],F1);
					int l2 = ApparaitLineairement(X[i],F2);
					if (l1 || l2)
					{
						if (l1>l2)
						{
							Solver tmp = DeleteVariable(X[i],f1,F1);
							freeSolver(f1);
							f1 = tmp;
						}
						else
						{
							Solver tmp = DeleteVariable(X[i],f2,F2);
							freeSolver(f2);
							f2 = tmp;
						}
						break;
					}
				}
				free(X);
				freeSysEq(F1);
				freeSysEq(F2);
				if (i<n)
				{
					Solver tmp = UnionSolver(f1,f2,F);
					freeSolver(f1);
					freeSolver(f2);
					Solver result = Refine(tmp,F);
					freeSolver(tmp);
					freeSysEq(F);
					return result;
				}
				else
				{
					freeSolver(f1);
					freeSolver(f2);
				}		
			}
			Solver f1 = Refine(B->fils1,F);
			Solver f2 = Refine(B->fils2,F);
			Solver result = UnionSolver(f1,f2,F);
			freeSolver(f1);
			freeSolver(f2);
			freeSysEq(F);
			return result;
		}
		else
		{
			Solver f2 = Refine(B->fils2,F);
			SysEqLin G = EsachantX(f2->var,f2->Nvar,F);
			Solver f1 = Refine(B->fils1,G);
			freeSysEq(G);
			freeSysEq(F);
			Solver result = NewSolver();
			result->var=UnionListesTriees(f1->var,f1->Nvar,f2->var,f2->Nvar,&result->Nvar);
			result->temps=max(f2->temps,max(0,f2->sortie)+f1->temps);
			result->memoire=max(f1->memoire,f2->memoire);
			result->sortie=max(0,f1->sortie)+f2->sortie;
			result->type=2;
			result->fils1 = f1;
			result->fils2 = f2;
			return result;
		}
	}
	
		
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rdi, %r14
testq %rdi, %rdi
je 0x8632
cmpb $0x0, 0x18(%r14)
je 0x847e
movq %rsi, %rdx
movq (%r14), %rdi
movl 0x8(%r14), %esi
callq 0xbb8a
movq %rax, %rbx
movslq 0x8(%r14), %r12
leaq (,%r12,4), %rdi
callq 0x21d0
movq %rax, %r15
movl $0x0, 0x4(%rsp)
testq %r12, %r12
jle 0x8436
xorl %r13d, %r13d
movq (%r14), %rax
movl (%rax,%r13,4), %edi
movq %rbx, %rsi
callq 0xc415
testl %eax, %eax
je 0x842a
movq (%r14), %rax
movl (%rax,%r13,4), %eax
movslq 0x4(%rsp), %rcx
leal 0x1(%rcx), %edx
movl %edx, 0x4(%rsp)
movl %eax, (%r15,%rcx,4)
incq %r13
movslq 0x8(%r14), %r12
cmpq %r12, %r13
jl 0x8400
cmpl $0x0, 0x4(%rsp)
je 0x8490
incl 0x1c(%r14)
cmpl $0x0, 0x4(%rsp)
jle 0x85b8
xorl %r13d, %r13d
movl (%r15,%r13,4), %edi
movq %r14, %rsi
movq %rbx, %rdx
callq 0x823a
movq %rax, %r12
movq %r14, %rdi
callq 0x7060
incq %r13
movslq 0x4(%rsp), %rax
movq %r12, %r14
cmpq %rax, %r13
jl 0x844f
jmp 0x85bb
testq %r14, %r14
je 0x8632
incl 0x1c(%r14)
jmp 0x8632
movq %r15, %rdi
callq 0x2030
cmpb $0x1, 0x18(%r14)
jne 0x8644
movq 0x20(%r14), %rax
movq 0x28(%r14), %rdx
movl 0x8(%rax), %esi
movl 0x8(%rdx), %ecx
leal (%rcx,%rsi), %edi
cmpl %r12d, %edi
je 0x878f
movq (%rax), %rdi
movq (%rdx), %rdx
leaq 0x4(%rsp), %r8
callq 0x27bd
movq %rax, %r13
movq 0x20(%r14), %rax
testq %rax, %rax
je 0x84dc
incl 0x1c(%rax)
movq 0x28(%r14), %r15
testq %r15, %r15
je 0x84e9
incl 0x1c(%r15)
movq (%rax), %rdi
movq %rax, 0x10(%rsp)
movl 0x8(%rax), %esi
movq %rbx, %rdx
callq 0xbb8a
movq %rax, 0x20(%rsp)
movq (%r15), %rdi
movl 0x8(%r15), %esi
movq %rbx, %rdx
callq 0xbb8a
movq %rax, 0x18(%rsp)
cmpl $0x0, 0x4(%rsp)
jle 0x86ff
movq %r15, 0x8(%rsp)
xorl %ebp, %ebp
movl (%r13,%rbp,4), %edi
movq 0x20(%rsp), %rsi
callq 0xc415
movl %eax, %r12d
movl (%r13,%rbp,4), %edi
movq 0x18(%rsp), %rsi
callq 0xc415
movl %r12d, %ecx
orl %eax, %ecx
sete %r15b
je 0x8598
movl (%r13,%rbp,4), %edi
cmpl %eax, %r12d
jle 0x8579
movq 0x10(%rsp), %r12
movq %r12, %rsi
movq 0x20(%rsp), %rdx
callq 0x823a
movq %r12, %rdi
movq %rax, 0x10(%rsp)
jmp 0x8593
movq 0x8(%rsp), %r12
movq %r12, %rsi
movq 0x18(%rsp), %rdx
callq 0x823a
movq %r12, %rdi
movq %rax, 0x8(%rsp)
callq 0x7060
testb %r15b, %r15b
je 0x85ae
incq %rbp
movslq 0x4(%rsp), %rax
cmpq %rax, %rbp
jl 0x8527
movq 0x8(%rsp), %r15
jmp 0x8701
movq %r14, %r12
movq %r12, %rdi
movq %rbx, %rsi
callq 0x83a5
movq %rax, %r14
movq %r12, %rdi
callq 0x7060
cmpl $0x0, 0x4(%rsp)
jle 0x861c
xorl %ebp, %ebp
movl (%r15,%rbp,4), %edi
movq %rbx, %rsi
callq 0x754c
movq %rax, %r13
movq %r14, %rdi
movq %rax, %rsi
movq %rbx, %rdx
callq 0x7be3
movq %rax, %r12
movq %r14, %rdi
callq 0x7060
movq %r13, %rdi
callq 0x7060
incq %rbp
movslq 0x4(%rsp), %rax
movq %r12, %r14
cmpq %rax, %rbp
jl 0x85da
jmp 0x861f
movq %r14, %r12
movq %r15, %rdi
callq 0x2030
movq %rbx, %rdi
callq 0xc453
movq %r12, %r14
movq %r14, %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x28(%r14), %rdi
movq %rbx, %rsi
callq 0x83a5
movq %rax, %r15
movq (%rax), %rdi
movl 0x8(%rax), %esi
movq %rbx, %rdx
callq 0xbed5
movq %rax, %r12
movq 0x20(%r14), %rdi
movq %rax, %rsi
callq 0x83a5
movq %rax, %r14
movq %r12, %rdi
callq 0xc453
movq %rbx, %rdi
callq 0xc453
callq 0x6fb4
movq %rax, %r12
movq (%r14), %rdi
movl 0x8(%r14), %esi
movq (%r15), %rdx
movl 0x8(%r15), %ecx
leaq 0x8(%rax), %r8
callq 0x2544
movq %rax, (%r12)
movl 0x14(%r15), %eax
xorl %ecx, %ecx
testl %eax, %eax
movl $0x0, %edx
cmovgl %eax, %edx
addl 0xc(%r14), %edx
movl 0xc(%r15), %esi
cmpl %edx, %esi
cmovgl %esi, %edx
movl %edx, 0xc(%r12)
movl 0x10(%r14), %edx
movl 0x10(%r15), %esi
cmpl %esi, %edx
cmovgl %edx, %esi
movl %esi, 0x10(%r12)
movl 0x14(%r14), %edx
testl %edx, %edx
cmovgl %edx, %ecx
addl %eax, %ecx
movl %ecx, 0x14(%r12)
movb $0x2, 0x18(%r12)
movq %r14, 0x20(%r12)
movq %r15, 0x28(%r12)
jmp 0x862f
xorl %ebp, %ebp
movq %r13, %rdi
callq 0x2030
movq 0x20(%rsp), %rdi
callq 0xc453
movq 0x18(%rsp), %rdi
callq 0xc453
movl 0x4(%rsp), %r13d
cmpl %r13d, %ebp
jge 0x8774
movq 0x10(%rsp), %r12
movq %r12, %rdi
movq %r15, %rsi
movq %rbx, %rdx
callq 0x7be3
movq %r15, 0x8(%rsp)
movq %rax, %r15
movq %r12, %rdi
callq 0x7060
movq 0x8(%rsp), %rdi
callq 0x7060
movq %r15, %rdi
movq %rbx, %rsi
callq 0x83a5
movq %rax, %r12
movq %r15, %rdi
callq 0x7060
movq %rbx, %rdi
callq 0xc453
jmp 0x8786
movq 0x10(%rsp), %rdi
callq 0x7060
movq %r15, %rdi
callq 0x7060
cmpl %r13d, %ebp
jl 0x862f
movq 0x20(%r14), %rdi
movq %rbx, %rsi
callq 0x83a5
movq %rax, %r15
movq 0x28(%r14), %rdi
movq %rbx, %rsi
callq 0x83a5
movq %rax, %r14
movq %r15, %rdi
movq %rax, %rsi
movq %rbx, %rdx
callq 0x7be3
movq %rax, %r12
movq %r15, %rdi
callq 0x7060
movq %r14, %rdi
callq 0x7060
jmp 0x8627