// File: /workspace/github_c_makefile_star2_O2/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 0xbe75
movl %edi, %ebp
orl $-0x1, 0x2491b(%rip)    # 0x30388
andl $0x0, 0x24918(%rip)     # 0x3038c
orl $-0x1, 0x24915(%rip)    # 0x30390
orl $-0x1, 0x24912(%rip)    # 0x30394
orl $-0x1, 0x2490f(%rip)    # 0x30398
leaq 0x1ba98(%rip), %r14     # 0x27528
leaq 0x246c9(%rip), %r15     # 0x30160
leaq 0x10(%rsp), %r12
movq 0x2452d(%rip), %r13     # 0x2ffd0
andl $0x0, 0x10(%rsp)
movl %ebp, %edi
movq %rbx, %rsi
movq %r14, %rdx
movq %r15, %rcx
movq %r12, %r8
callq 0x2100
cmpl $0x43, %eax
je 0xbb17
cmpl $0x58, %eax
je 0xbb4b
cmpl $0x63, %eax
je 0xbafd
cmpl $0x65, %eax
je 0xbaf0
cmpl $0x6d, %eax
je 0xbb23
cmpl $0x72, %eax
je 0xbb37
cmpl $0x74, %eax
je 0xbb06
cmpl $0x78, %eax
jne 0xbb5a
andl $0x0, 0x248a2(%rip)     # 0x30390
jmp 0xbaa3
movq (%r13), %rax
movq %rax, 0x24885(%rip)     # 0x30380
jmp 0xbaa3
andl $0x0, 0x24890(%rip)     # 0x30394
jmp 0xbaa3
movq (%r13), %rdi
callq 0x2210
movl %eax, 0x24887(%rip)     # 0x3039c
jmp 0xbaa3
movl $0x1, 0x24873(%rip)     # 0x30394
jmp 0xbaa3
movq (%r13), %rdi
callq 0x2210
movl %eax, 0x24856(%rip)     # 0x30388
jmp 0xbaa3
movq (%r13), %rdi
callq 0x2210
movl %eax, 0x24846(%rip)     # 0x3038c
jmp 0xbaa3
movl $0x1, 0x2483b(%rip)     # 0x30390
jmp 0xbaa3
cmpl $-0x1, %eax
jne 0xbe6d
cmpl $0x0, 0x2481e(%rip)     # 0x30388
jg 0xbb78
movl 0x2482a(%rip), %eax     # 0x3039c
movl %eax, 0x24810(%rip)     # 0x30388
leaq 0x40(%rsp), %rbx
andq $0x0, (%rbx)
movq 0x247f8(%rip), %rdi     # 0x30380
leaq 0x38(%rsp), %rax
leaq 0x2c(%rsp), %r10
leaq 0x30(%rsp), %r11
leaq 0x20(%rsp), %r13
leaq 0x24715(%rip), %r15     # 0x302b8
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 0xbf39
addq $0x20, %rsp
movl (%r14), %esi
movl (%r15), %edx
movl (%r12), %ecx
leaq 0x1b961(%rip), %rdi     # 0x27540
xorl %r15d, %r15d
xorl %eax, %eax
callq 0x2110
callq 0xb8d3
movq (%rbx), %rdi
movl (%r14), %esi
pushq $0x2
popq %rdx
callq 0xa12e
movq %rax, %rbx
movslq 0x34(%rsp), %rax
movq 0x40(%rsp), %rdi
cmpq %rax, %r15
jge 0xbc1c
movq (%rdi,%r15,8), %rdi
callq 0x42a5
incq %r15
jmp 0xbbff
callq 0x2030
movq 0x38(%rsp), %rdi
movl 0x2c(%rsp), %esi
movl 0x30(%rsp), %edx
callq 0x1629c
movl 0x24763(%rip), %esi     # 0x3039c
movl 0x24749(%rip), %edx     # 0x30388
movq 0x10(%rsp), %rcx
movl 0x1c(%rsp), %r8d
movl 0x2473c(%rip), %r9d     # 0x3038c
movq %rbx, %rdi
pushq %rax
pushq 0x28(%rsp)
callq 0x16021
addq $0x10, %rsp
testq %rax, %rax
je 0xbe4a
movq %rax, %r14
leaq 0x1bcf8(%rip), %rdi     # 0x2796c
callq 0x2090
movl 0xc(%r14), %esi
shll $0x3, %esi
leaq 0x1b8f1(%rip), %rdi     # 0x27578
xorl %eax, %eax
callq 0x2110
movl 0x10(%r14), %esi
shll $0x3, %esi
leaq 0x1b8f9(%rip), %rdi     # 0x27595
xorl %eax, %eax
callq 0x2110
movl 0x14(%r14), %esi
shll $0x3, %esi
leaq 0x1b903(%rip), %rdi     # 0x275b4
xorl %eax, %eax
callq 0x2110
movl 0x246d2(%rip), %eax     # 0x30390
cmpl $-0x1, %eax
jne 0xbd0a
leaq 0x1b90a(%rip), %rdi     # 0x275d4
xorl %eax, %eax
callq 0x2110
movq 0x242e8(%rip), %rax     # 0x2ffc0
movq (%rax), %rdi
callq 0x21d0
leaq 0x1b91f(%rip), %rdi     # 0x27606
leaq 0xf(%rsp), %r15
movq %r15, %rsi
xorl %eax, %eax
callq 0x2220
callq 0x2170
xorl %eax, %eax
cmpb $0x79, (%r15)
sete %al
movl %eax, 0x24686(%rip)     # 0x30390
testl %eax, %eax
je 0xbd6d
movl 0xc(%r14), %edi
movl 0x10(%r14), %esi
movl 0x24670(%rip), %edx     # 0x3038c
callq 0xb8f2
movq %rax, %r15
leaq 0x1c502(%rip), %rsi     # 0x2822d
movq %rax, %rdi
callq 0x2200
movq %rax, %r12
movq 0x20(%rsp), %rdx
movq %rax, %rdi
movq %r14, %rsi
callq 0x6b64
movq %r12, %rdi
callq 0x20e0
leaq 0x1bc43(%rip), %rdi     # 0x27998
callq 0x2090
leaq 0x1b8a9(%rip), %rdi     # 0x2760a
movq %r15, %rsi
xorl %eax, %eax
callq 0x2110
jmp 0xbd79
leaq 0x1bc0c(%rip), %rdi     # 0x27980
callq 0x2090
movl 0x24615(%rip), %eax     # 0x30394
cmpl $-0x1, %eax
jne 0xbdcb
leaq 0x1b884(%rip), %rdi     # 0x2760f
xorl %eax, %eax
callq 0x2110
movq 0x24227(%rip), %rax     # 0x2ffc0
movq (%rax), %rdi
callq 0x21d0
leaq 0x1b85e(%rip), %rdi     # 0x27606
leaq 0xf(%rsp), %r15
movq %r15, %rsi
xorl %eax, %eax
callq 0x2220
callq 0x2170
xorl %eax, %eax
cmpb $0x79, (%r15)
sete %al
movl %eax, 0x245c9(%rip)     # 0x30394
testl %eax, %eax
je 0xbe36
movl 0xc(%r14), %edi
movl 0x10(%r14), %esi
movl 0x245af(%rip), %edx     # 0x3038c
callq 0xb99e
movq %rax, %r15
movq %rax, %rdi
callq 0x2260
movq %rax, %r12
leaq 0x1b844(%rip), %rdi     # 0x2763b
movq %r15, %rsi
xorl %eax, %eax
callq 0x2110
leaq 0x1b84a(%rip), %rdi     # 0x27652
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 0x111ad
jmp 0xbe42
leaq 0x1bb71(%rip), %rdi     # 0x279ae
callq 0x2090
movq %r14, %rdi
callq 0x6852
movq %rbx, %rdi
callq 0xb562
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
je 0xbe75
cmpl $0x68, %eax
movq (%rbx), %rdi
callq 0xb834
pushq $0x1
popq %rdi
callq 0x2240
nopl (%rax)