// File: /workspace/github_c_makefile_star2_O3/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
testq %rdi, %rdi
je 0x87c4
movq %rdi, %r14
cmpb $0x0, 0x18(%rdi)
je 0x88a3
movq %rsi, %rdx
movq (%r14), %rdi
movl 0x8(%r14), %esi
callq 0xbf32
movq %rax, %rbp
movslq 0x8(%r14), %r12
leaq (,%r12,4), %rdi
callq 0x21d0
movq %rax, %r15
movl $0x0, 0x4(%rsp)
testq %r12, %r12
jle 0x87cc
xorl %r13d, %r13d
xorl %ebx, %ebx
movq (%r14), %rax
movl (%rax,%r13,4), %edi
movq %rbp, %rsi
callq 0xc7ef
testl %eax, %eax
je 0x8772
movq (%r14), %rax
movl (%rax,%r13,4), %eax
movslq %ebx, %rcx
leal 0x1(%rbx), %edx
movl %edx, 0x4(%rsp)
movl %eax, (%r15,%rcx,4)
movl %edx, %ebx
incq %r13
movslq 0x8(%r14), %r12
cmpq %r12, %r13
jl 0x8748
testl %ebx, %ebx
je 0x87cc
movq %rbp, 0x8(%rsp)
incl 0x1c(%r14)
movl %ebx, %ebp
testl %ebx, %ebx
jle 0x8965
xorl %r12d, %r12d
movl (%r15,%r12,4), %edi
movq %r14, %rsi
movq 0x8(%rsp), %rdx
callq 0x8576
movq %rax, %r13
movq %r14, %rdi
callq 0x7447
incq %r12
movq %r13, %r14
cmpq %r12, %rbp
jne 0x8798
jmp 0x8968
xorl %r14d, %r14d
jmp 0x8b02
movq %r15, %rdi
callq 0x2030
cmpb $0x1, 0x18(%r14)
jne 0x88ac
movq 0x20(%r14), %rdi
movq 0x28(%r14), %rax
movl 0x8(%rdi), %esi
movl 0x8(%rax), %ecx
leal (%rcx,%rsi), %edx
cmpl %r12d, %edx
je 0x8abf
movq (%rdi), %rdi
movq (%rax), %rdx
leaq 0x4(%rsp), %r8
callq 0x2900
movq %rax, %r13
movq 0x20(%r14), %rbx
testq %rbx, %rbx
je 0x8818
incl 0x1c(%rbx)
movq 0x28(%r14), %r12
testq %r12, %r12
je 0x8826
incl 0x1c(%r12)
movq (%rbx), %rdi
movl 0x8(%rbx), %esi
movq %rbp, %rdx
callq 0xbf32
movq %rax, 0x10(%rsp)
movq (%r12), %rdi
movl 0x8(%r12), %esi
movq %rbp, 0x8(%rsp)
movq %rbp, %rdx
callq 0xbf32
movq %rax, %r15
cmpl $0x0, 0x4(%rsp)
jle 0x89e7
movq %rbx, 0x18(%rsp)
movq %r12, %rbx
xorl %ebp, %ebp
movl (%r13,%rbp,4), %edi
movq 0x10(%rsp), %rsi
callq 0xc7ef
movl %eax, %r12d
movl (%r13,%rbp,4), %edi
movq %r15, %rsi
callq 0xc7ef
movl %r12d, %ecx
orl %eax, %ecx
jne 0x89eb
incq %rbp
movslq 0x4(%rsp), %rax
cmpq %rax, %rbp
jl 0x8867
jmp 0x8a3a
incl 0x1c(%r14)
jmp 0x8b02
movq 0x28(%r14), %rdi
movq %rbp, %rsi
callq 0x86e8
movq %rax, %r15
movq (%rax), %rdi
movl 0x8(%rax), %esi
movq %rbp, %rdx
callq 0xc29a
movq %rax, %r13
movq 0x20(%r14), %rdi
movq %rax, %rsi
callq 0x86e8
movq %rax, %r12
movq %r13, %rdi
callq 0xc82d
movq %rbp, %rdi
callq 0xc82d
callq 0x7394
movq %rax, %r14
movq (%r12), %rdi
movl 0x8(%r12), %esi
movq (%r15), %rdx
movl 0x8(%r15), %ecx
leaq 0x8(%rax), %r8
callq 0x2544
movq %rax, (%r14)
movl 0x14(%r15), %eax
xorl %ecx, %ecx
testl %eax, %eax
movl $0x0, %edx
cmovgl %eax, %edx
addl 0xc(%r12), %edx
movl 0xc(%r15), %esi
cmpl %edx, %esi
cmovgl %esi, %edx
movl %edx, 0xc(%r14)
movl 0x10(%r12), %edx
movl 0x10(%r15), %esi
cmpl %esi, %edx
cmovgl %edx, %esi
movl %esi, 0x10(%r14)
movl 0x14(%r12), %edx
testl %edx, %edx
cmovgl %edx, %ecx
addl %eax, %ecx
movl %ecx, 0x14(%r14)
movb $0x2, 0x18(%r14)
movq %r12, 0x20(%r14)
movq %r15, 0x28(%r14)
jmp 0x8b02
movq %r14, %r13
movq %rbp, %r14
movq %r13, %rdi
movq 0x8(%rsp), %rsi
callq 0x86e8
movq %rax, %r12
movq %r13, %rdi
callq 0x7447
testl %ebx, %ebx
jle 0x89d2
xorl %ebx, %ebx
movq 0x8(%rsp), %rbp
movq %r14, 0x10(%rsp)
movl (%r15,%rbx,4), %edi
movq %rbp, %rsi
callq 0x790b
movq %rax, %r13
movq %r12, %rdi
movq %rax, %rsi
movq %rbp, %rdx
callq 0x7f14
movq %rax, %r14
movq %r12, %rdi
callq 0x7447
movq %r13, %rdi
callq 0x7447
incq %rbx
movq %r14, %r12
cmpq %rbx, 0x10(%rsp)
jne 0x8993
jmp 0x89da
movq %r12, %r14
movq 0x8(%rsp), %rbp
movq %r15, %rdi
callq 0x2030
jmp 0x8afa
xorl %ebp, %ebp
jmp 0x8a42
movl (%r13,%rbp,4), %edi
cmpl %eax, %r12d
jle 0x8a21
movq 0x18(%rsp), %r12
movq %r12, %rsi
movq 0x10(%rsp), %rdx
callq 0x8576
movq %rax, 0x20(%rsp)
movq %r12, %rdi
callq 0x7447
movq 0x20(%rsp), %rax
movq %rbx, %r12
movq %rax, %rbx
jmp 0x8a42
movq %rbx, %r12
movq %rbx, %rsi
movq %r15, %rdx
callq 0x8576
movq %rax, %rbx
movq %r12, %rdi
callq 0x7447
movq %rbx, %r12
movq 0x18(%rsp), %rbx
movq %r13, %rdi
callq 0x2030
movq 0x10(%rsp), %rdi
callq 0xc82d
movq %r15, %rdi
callq 0xc82d
cmpl 0x4(%rsp), %ebp
jge 0x8aa6
movq %rbx, %rdi
movq %r12, %rsi
movq %rbx, %r14
movq 0x8(%rsp), %rbx
movq %rbx, %rdx
callq 0x7f14
movq %rax, %r15
movq %r14, %rdi
callq 0x7447
movq %r12, %rdi
callq 0x7447
movq %r15, %rdi
movq %rbx, %rsi
callq 0x86e8
movq %rax, %r14
movq %r15, %rdi
callq 0x7447
movq %rbx, %rdi
jmp 0x8afd
movq %rbx, %rdi
callq 0x7447
movq %r12, %rdi
callq 0x7447
movq 0x20(%r14), %rdi
movq 0x8(%rsp), %rbp
movq %rbp, %rsi
callq 0x86e8
movq %rax, %r15
movq 0x28(%r14), %rdi
movq %rbp, %rsi
callq 0x86e8
movq %rax, %r12
movq %r15, %rdi
movq %rax, %rsi
movq %rbp, %rdx
callq 0x7f14
movq %rax, %r14
movq %r15, %rdi
callq 0x7447
movq %r12, %rdi
callq 0x7447
movq %rbp, %rdi
callq 0xc82d
movq %r14, %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq