// File: /workspace/github_c_makefile_star2_O3/cbouilla[P]AES-attacks-finder/main.c
// Function: main
int main(int argc, char** argv) {
  
  int		c;
  /*
	int 	Nk;
  int 	Tours;
  int 	Final;
  int 	Messages;
   */
  int 	N;
  int 	*Known;
  int 	NKnown;
  int 	i;
  SysEqLin E;
  Equation *Eq;
  string_list_t *variable_names;
  int **var_by_message;
  int n_message;
  int n_var_by_message;


  
  if(argc<5) {
    display_usage(argv[0]);
    exit(1);
  }
  
  /*
   * Default values
   */
  memoire=-1;
  r=DEFAULT_TYPE;
  xml=DEFAULT_XML;
  code=DEFAULT_CODE;
  reducemem=DEFAULT_REDUCEMEM;
  
  /*
   * Go over all the elements of the command line
   */
  while (1) {
    
    int option_index = 0;    
    c = getopt_long (argc, argv, "e:t:m:r:x::X::c::C::h::", long_options, &option_index);
    
    /*
     * Detect the end of the options
     */
    if(c==-1) break;
    
    /*
     * Parse the command line
     */
    switch (c) {
      	/* Required parameters */
      case 'e': eq_file=optarg; break;
      case 't': temps=atoi(optarg); break;
        
        /* Optional parameters */
      case 'm': memoire=atoi(optarg); break;
      case 'r': r=atoi(optarg); break;
      case 'X': xml=1; break;
      case 'x': xml=0; break;
      case 'C': code=1; break;
      case 'c': code=0; break;
      /*case 'M': reducemem=1; break;*/
        
      case 'h':
      case '?':
        display_usage(argv[0]);
        exit(1);
        
      default:
        display_usage(argv[0]);
        exit(1);
    }
  }

  /*
   * Default values
   */
  if (memoire<=0) memoire=temps;
  
  
  
  
  
	/*  
   * Lancement du programme  
   */	   
  Eq=NULL;
  parse(eq_file, &Eq, &N, &VARIABLES, &Known, &NKnown, &variable_names, &n_message, &n_var_by_message, &var_by_message);
	printf("[+] Parsed %d equations in %d variables (%d are known)\n", N, VARIABLES, NKnown);  
  
  
	/*
   * Initialize PRNG
   */
	init_PRNG();
  
	
	E=Transform(Eq,N,2);
	for (i = 0; i < N; i++) freeEq(Eq[i]);
	free(Eq);
		
		
  
#if 1
	Symetric_structure S = InitialiseSymetricStrucure(var_by_message,n_var_by_message,n_message);
	Solver B = SearchAlgo(E, temps, memoire, Known, NKnown, r, variable_names,S);
#else
	FILE *b;
	b=fopen("xml/4r_4CP_4-4_3.xml","r");
	Solver B = charge_solver(b, variable_names);
	PrintListe(B->var,B->Nvar);
	fclose(b);
	printf("Solver charged\n");
#endif

  /*
   * If found
   */
	if(B) {
		FILE *output;
    
    printf("[>] Result found : \n");
    printf("   - Time complexity : 2^%d\n", B->temps*8);
    printf("   - Memory complexity : 2^%d\n", B->memoire*8);
    printf("   - Number of solution : 2^%d\n", B->sortie*8);
  	
    /*
     * XML Tree generation
     */
    if(xml==-1) {
      char c;
      printf("[?] Do you want to save the result (XML) ? (y/n) ");fflush(stdout);
			scanf(" %c", &c);
			getchar();
      xml=(c=='y')?1:0;
    }
    
		if(xml) {
      char *xml_file; 
      xml_file=getXML(B->temps, B->memoire, r);
      
			output = fopen(xml_file, "w");
			print_solver(output, B, variable_names);
			fclose(output);
      
      printf("[+] Output XML file: \n");
      printf("\t%s\n", xml_file);
    } else {
      printf("[-] Skip XML generation\n");
    }
    
    /*
     * C Code generation
     */
    if(code==-1) {
      char c;
      printf("[?] Do you want to generate C code ? (y/n) ");fflush(stdout);
      scanf(" %c",&c);
      getchar();
      code=(c=='y')?1:0;
    }
    
    if(code) {
      char *code_file; 
      char *bin_file; 
      code_file=getCFile(B->temps, B->memoire, r);
      bin_file=strdup(code_file);
			      		
      printf("[+] Source file: %s.c\n", code_file);
      printf("[+] Compilation : \n\tgcc -Wall Programs/%s.c -o Programs/%s -O3\n\t./Programs/%s\n\n", code_file, bin_file, bin_file);
      EcrireAlgo(code_file, B, Known, NKnown, E, variable_names);
      
		} else {
      printf("[-] Skip code generation\n");
    }
    
		freeSolver(B);
	}
  
  /*
   * Free memory
   */
	freeSysEq(E);
	free(Known);
  
	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rsi, %rbx
cmpl $0x4, %edi
jle 0xd1ef
movl %edi, %ebp
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movl %eax, 0x255f1(%rip)     # 0x32388
movl $0x0, 0x255eb(%rip)     # 0x3238c
movl %eax, 0x255e9(%rip)     # 0x32390
movl %eax, 0x255e7(%rip)     # 0x32394
movl %eax, 0x255e5(%rip)     # 0x32398
leaq 0x1c7ce(%rip), %r14     # 0x29588
leaq 0x2539f(%rip), %r15     # 0x32160
leaq 0x10(%rsp), %r12
movq 0x25203(%rip), %r13     # 0x31fd0
movl $0x0, 0x10(%rsp)
movl %ebp, %edi
movq %rbx, %rsi
movq %r14, %rdx
movq %r15, %rcx
movq %r12, %r8
callq 0x2100
cmpl $0x67, %eax
jg 0xce07
cmpl $0x57, %eax
jg 0xce45
cmpl $0x43, %eax
jne 0xceae
movl $0x1, 0x2558f(%rip)     # 0x32394
jmp 0xcdcd
cmpl $0x73, %eax
jg 0xce2b
cmpl $0x6d, %eax
je 0xce7c
cmpl $0x72, %eax
jne 0xd1ec
movq (%r13), %rdi
callq 0x2220
movl %eax, 0x25563(%rip)     # 0x3238c
jmp 0xcdcd
cmpl $0x74, %eax
je 0xce68
cmpl $0x78, %eax
jne 0xd1ef
movl $0x0, 0x2554d(%rip)     # 0x32390
jmp 0xcdcd
cmpl $0x58, %eax
je 0xce9f
cmpl $0x63, %eax
je 0xce90
cmpl $0x65, %eax
jne 0xd1ef
movq (%r13), %rax
movq %rax, 0x2551d(%rip)     # 0x32380
jmp 0xcdcd
movq (%r13), %rdi
callq 0x2220
movl %eax, 0x25525(%rip)     # 0x3239c
jmp 0xcdcd
movq (%r13), %rdi
callq 0x2220
movl %eax, 0x254fd(%rip)     # 0x32388
jmp 0xcdcd
movl $0x0, 0x254fa(%rip)     # 0x32394
jmp 0xcdcd
movl $0x1, 0x254e7(%rip)     # 0x32390
jmp 0xcdcd
cmpl $-0x1, %eax
jne 0xd1e7
cmpl $0x0, 0x254ca(%rip)     # 0x32388
jg 0xcecc
movl 0x254d6(%rip), %eax     # 0x3239c
movl %eax, 0x254bc(%rip)     # 0x32388
leaq 0x38(%rsp), %rbx
movq $0x0, (%rbx)
movq 0x254a1(%rip), %rdi     # 0x32380
leaq 0x40(%rsp), %rax
leaq 0x2c(%rsp), %r10
leaq 0x30(%rsp), %r11
leaq 0x20(%rsp), %r13
leaq 0x253be(%rip), %r15     # 0x322b8
leaq 0x34(%rsp), %r14
leaq 0x10(%rsp), %r8
leaq 0x1c(%rsp), %r12
movq %rbx, %rsi
movq %r14, %rdx
movq %r15, %rcx
movq %r12, %r9
pushq %rax
pushq %r10
pushq %r11
pushq %r13
callq 0xd2bf
addq $0x20, %rsp
movl (%r14), %esi
movl (%r15), %edx
movl (%r12), %ecx
leaq 0x1c66a(%rip), %rdi     # 0x295a0
xorl %eax, %eax
callq 0x2110
movl $0x7e86d5ce, %edi       # imm = 0x7E86D5CE
callq 0x2150
leaq 0x1c5fa(%rip), %rdi     # 0x29548
movl $0x7e86d5ce, %esi       # imm = 0x7E86D5CE
xorl %eax, %eax
callq 0x2110
movq (%rbx), %rdi
movl (%r14), %esi
movl $0x2, %edx
callq 0xb0d0
movq %rax, %rbx
cmpl $0x0, (%r14)
jle 0xcf91
xorl %r14d, %r14d
movq 0x38(%rsp), %rax
movq (%rax,%r14,8), %rdi
callq 0x4ad9
incq %r14
movslq 0x34(%rsp), %rax
cmpq %rax, %r14
jl 0xcf76
movq 0x38(%rsp), %rdi
callq 0x2030
movq 0x40(%rsp), %rdi
movl 0x2c(%rsp), %esi
movl 0x30(%rsp), %edx
callq 0x18190
movl 0x253e9(%rip), %esi     # 0x3239c
movl 0x253cf(%rip), %edx     # 0x32388
movq 0x10(%rsp), %rcx
movl 0x1c(%rsp), %r8d
movl 0x253c2(%rip), %r9d     # 0x3238c
movq %rbx, %rdi
pushq %rax
pushq 0x28(%rsp)
callq 0x17eff
addq $0x10, %rsp
testq %rax, %rax
je 0xd1c4
movq %rax, %r14
leaq 0x1c9de(%rip), %rdi     # 0x299cc
callq 0x2090
movl 0xc(%r14), %esi
shll $0x3, %esi
leaq 0x1c5d7(%rip), %rdi     # 0x295d8
xorl %eax, %eax
callq 0x2110
movl 0x10(%r14), %esi
shll $0x3, %esi
leaq 0x1c5df(%rip), %rdi     # 0x295f5
xorl %eax, %eax
callq 0x2110
movl 0x14(%r14), %esi
shll $0x3, %esi
leaq 0x1c5e9(%rip), %rdi     # 0x29614
xorl %eax, %eax
callq 0x2110
movl 0x25358(%rip), %eax     # 0x32390
cmpl $-0x1, %eax
jne 0xd084
leaq 0x1c5f0(%rip), %rdi     # 0x29634
xorl %eax, %eax
callq 0x2110
movq 0x24f6e(%rip), %rax     # 0x31fc0
movq (%rax), %rdi
callq 0x21e0
leaq 0x1c605(%rip), %rdi     # 0x29666
leaq 0xf(%rsp), %r15
movq %r15, %rsi
xorl %eax, %eax
callq 0x2230
callq 0x2180
xorl %eax, %eax
cmpb $0x79, (%r15)
sete %al
movl %eax, 0x2530c(%rip)     # 0x32390
testl %eax, %eax
je 0xd0e7
movl 0xc(%r14), %edi
movl 0x10(%r14), %esi
movl 0x252f6(%rip), %edx     # 0x3238c
callq 0xcc20
movq %rax, %r15
leaq 0x1d1e8(%rip), %rsi     # 0x2a28d
movq %rax, %rdi
callq 0x2210
movq %rax, %r12
movq 0x20(%rsp), %rdx
movq %rax, %rdi
movq %r14, %rsi
callq 0x7734
movq %r12, %rdi
callq 0x20e0
leaq 0x1c929(%rip), %rdi     # 0x299f8
callq 0x2090
leaq 0x1c58f(%rip), %rdi     # 0x2966a
movq %r15, %rsi
xorl %eax, %eax
callq 0x2110
jmp 0xd0f3
leaq 0x1c8f2(%rip), %rdi     # 0x299e0
callq 0x2090
movl 0x2529b(%rip), %eax     # 0x32394
cmpl $-0x1, %eax
jne 0xd145
leaq 0x1c56a(%rip), %rdi     # 0x2966f
xorl %eax, %eax
callq 0x2110
movq 0x24ead(%rip), %rax     # 0x31fc0
movq (%rax), %rdi
callq 0x21e0
leaq 0x1c544(%rip), %rdi     # 0x29666
leaq 0xf(%rsp), %r15
movq %r15, %rsi
xorl %eax, %eax
callq 0x2230
callq 0x2180
xorl %eax, %eax
cmpb $0x79, (%r15)
sete %al
movl %eax, 0x2524f(%rip)     # 0x32394
testl %eax, %eax
je 0xd1b0
movl 0xc(%r14), %edi
movl 0x10(%r14), %esi
movl 0x25235(%rip), %edx     # 0x3238c
callq 0xccc8
movq %rax, %r15
movq %rax, %rdi
callq 0x2270
movq %rax, %r12
leaq 0x1c52a(%rip), %rdi     # 0x2969b
movq %r15, %rsi
xorl %eax, %eax
callq 0x2110
leaq 0x1c530(%rip), %rdi     # 0x296b2
movq %r15, %rsi
movq %r12, %rdx
movq %r12, %rcx
xorl %eax, %eax
callq 0x2110
movq 0x10(%rsp), %rdx
movl 0x1c(%rsp), %ecx
movq 0x20(%rsp), %r9
movq %r15, %rdi
movq %r14, %rsi
movq %rbx, %r8
callq 0x12b80
jmp 0xd1bc
leaq 0x1c857(%rip), %rdi     # 0x29a0e
callq 0x2090
movq %r14, %rdi
callq 0x7447
movq %rbx, %rdi
callq 0xc82d
movq 0x10(%rsp), %rdi
callq 0x2030
xorl %eax, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpl $0x3f, %eax
jmp 0xd1ef
cmpl $0x68, %eax
movq (%rbx), %rdi
callq 0xcb60
movl $0x1, %edi
callq 0x2250
nopl (%rax)