// File: /workspace/github_c_makefile_star2_O2/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 0x7b90
movq %rdi, %rbx
cmpb $0x0, 0x18(%rdi)
je 0x7c06
movq %rsi, %rdx
movq (%rbx), %rdi
movl 0x8(%rbx), %esi
callq 0xace0
movq %rax, (%rsp)
movslq 0x8(%rbx), %r12
movq %r12, %rdi
shlq $0x2, %rdi
callq 0x21c0
andl $0x0, 0xc(%rsp)
movq %rax, %r15
xorl %r13d, %r13d
xorl %ebp, %ebp
movslq %r12d, %rax
cmpq %rax, %r13
jge 0x7b54
movq (%rbx), %rax
movl (%rax,%r13,4), %edi
movq (%rsp), %rsi
callq 0xb528
testl %eax, %eax
je 0x7b4b
movq (%rbx), %rax
movl (%rax,%r13,4), %eax
movslq %ebp, %rcx
leal 0x1(%rbp), %edx
movl %edx, 0xc(%rsp)
movl %eax, (%r15,%rcx,4)
movl %edx, %ebp
incq %r13
movl 0x8(%rbx), %r12d
jmp 0x7b18
testl %ebp, %ebp
je 0x7c0e
incl 0x1c(%rbx)
xorl %r14d, %r14d
testl %ebp, %ebp
cmovlel %r14d, %ebp
cmpq %r14, %rbp
je 0x7b97
movl (%r15,%r14,4), %edi
movq %rbx, %rsi
movq (%rsp), %rdx
callq 0x7963
movq %rax, %r12
movq %rbx, %rdi
callq 0x6852
incq %r14
movq %r12, %rbx
jmp 0x7b68
xorl %ebx, %ebx
jmp 0x7e96
movq %rbx, %rdi
movq (%rsp), %rsi
callq 0x7ac5
movq %rax, %r12
movq %rbx, %rdi
callq 0x6852
xorl %r14d, %r14d
movq %r12, %rbx
cmpq %r14, %rbp
je 0x7bf5
movl (%r15,%r14,4), %edi
movq (%rsp), %r13
movq %r13, %rsi
callq 0x6d1e
movq %rax, %r12
movq %rbx, %rdi
movq %rax, %rsi
movq %r13, %rdx
callq 0x72e2
movq %rax, %r13
movq %rbx, %rdi
callq 0x6852
movq %r12, %rdi
callq 0x6852
incq %r14
movq %r13, %rbx
jmp 0x7bb4
movq %r15, %rdi
callq 0x2030
movq (%rsp), %rdi
jmp 0x7e91
incl 0x1c(%rbx)
jmp 0x7e96
movq %r15, %rdi
callq 0x2030
cmpb $0x1, 0x18(%rbx)
jne 0x7cd4
movq 0x20(%rbx), %rdi
movq 0x28(%rbx), %rax
movl 0x8(%rdi), %esi
movl 0x8(%rax), %ecx
leal (%rcx,%rsi), %edx
cmpl %r12d, %edx
je 0x7e4f
movq (%rdi), %rdi
movq (%rax), %rdx
leaq 0xc(%rsp), %r8
callq 0x25c6
movq %rax, %r13
movq 0x20(%rbx), %rax
testq %rax, %rax
je 0x7c59
incl 0x1c(%rax)
movq 0x28(%rbx), %r15
testq %r15, %r15
je 0x7c66
incl 0x1c(%r15)
movq (%rax), %rdi
movq %rax, 0x10(%rsp)
movl 0x8(%rax), %esi
movq (%rsp), %r14
movq %r14, %rdx
callq 0xace0
movq (%r15), %rdi
movq %r15, 0x18(%rsp)
movl 0x8(%r15), %esi
movq %r14, %rdx
movq %rax, %r14
callq 0xace0
movq %rax, %r15
xorl %ebp, %ebp
movslq 0xc(%rsp), %rax
cmpq %rax, %rbp
jge 0x7dd7
movl (%r13,%rbp,4), %edi
movq %r14, %rsi
callq 0xb528
movl %eax, %r12d
movl (%r13,%rbp,4), %edi
movq %r15, %rsi
callq 0xb528
movl %r12d, %ecx
orl %eax, %ecx
jne 0x7d8d
incq %rbp
jmp 0x7c99
movq 0x28(%rbx), %rdi
movq (%rsp), %r14
movq %r14, %rsi
callq 0x7ac5
movq %rax, %r15
movq (%rax), %rdi
movl 0x8(%rax), %esi
movq %r14, %rdx
callq 0xb021
movq %rax, %r13
movq 0x20(%rbx), %rdi
movq %rax, %rsi
callq 0x7ac5
movq %rax, %r12
movq %r13, %rdi
callq 0xb562
movq %r14, %rdi
callq 0xb562
callq 0x67a4
movq %rax, %rbx
movq (%r12), %rdi
movl 0x8(%r12), %esi
movq (%r15), %rdx
movl 0x8(%r15), %ecx
leaq 0x8(%rax), %r8
callq 0x2440
movq %rax, (%rbx)
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(%rbx)
movl 0x10(%r12), %edx
movl 0x10(%r15), %esi
cmpl %esi, %edx
cmovgl %edx, %esi
movl %esi, 0x10(%rbx)
movl 0x14(%r12), %edx
testl %edx, %edx
cmovgl %edx, %ecx
addl %eax, %ecx
movl %ecx, 0x14(%rbx)
movb $0x2, 0x18(%rbx)
movq %r12, 0x20(%rbx)
movq %r15, 0x28(%rbx)
jmp 0x7e96
movq %r14, 0x20(%rsp)
movl (%r13,%rbp,4), %edi
cmpl %eax, %r12d
jle 0x7db5
movq 0x10(%rsp), %r14
movq %r14, %rsi
movq 0x20(%rsp), %rdx
callq 0x7963
movq %rax, 0x10(%rsp)
jmp 0x7dca
movq 0x18(%rsp), %r14
movq %r14, %rsi
movq %r15, %rdx
callq 0x7963
movq %rax, 0x18(%rsp)
movq %r14, %rdi
callq 0x6852
movq 0x20(%rsp), %r14
movq %r13, %rdi
callq 0x2030
movq %r14, %rdi
callq 0xb562
movq %r15, %rdi
callq 0xb562
cmpl %ebp, 0xc(%rsp)
jle 0x7e37
movq 0x10(%rsp), %r12
movq %r12, %rdi
movq 0x18(%rsp), %rbx
movq %rbx, %rsi
movq (%rsp), %r14
movq %r14, %rdx
callq 0x72e2
movq %rax, %r15
movq %r12, %rdi
callq 0x6852
movq %rbx, %rdi
callq 0x6852
movq %r15, %rdi
movq %r14, %rsi
callq 0x7ac5
movq %rax, %rbx
movq %r15, %rdi
jmp 0x7e89
movq 0x10(%rsp), %rdi
callq 0x6852
movq 0x18(%rsp), %rdi
callq 0x6852
movq 0x20(%rbx), %rdi
movq (%rsp), %r14
movq %r14, %rsi
callq 0x7ac5
movq %rax, %r15
movq 0x28(%rbx), %rdi
movq %r14, %rsi
callq 0x7ac5
movq %rax, %r12
movq %r15, %rdi
movq %rax, %rsi
movq %r14, %rdx
callq 0x72e2
movq %rax, %rbx
movq %r15, %rdi
callq 0x6852
movq %r12, %rdi
callq 0x6852
movq %r14, %rdi
callq 0xb562
movq %rbx, %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq