// File: /workspace/github_c_makefile_star2_O1/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 0xce1d
movl %edi, %ebp
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movl %eax, 0x259c9(%rip)     # 0x32388
movl $0x0, 0x259c3(%rip)     # 0x3238c
movl %eax, 0x259c1(%rip)     # 0x32390
movl %eax, 0x259bf(%rip)     # 0x32394
movl %eax, 0x259bd(%rip)     # 0x32398
leaq 0x1cb46(%rip), %r14     # 0x29528
leaq 0x25777(%rip), %r15     # 0x32160
leaq 0x10(%rsp), %r12
movq 0x255db(%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 0xca2f
cmpl $0x57, %eax
jg 0xca6d
cmpl $0x43, %eax
jne 0xcad6
movl $0x1, 0x25967(%rip)     # 0x32394
jmp 0xc9f5
cmpl $0x73, %eax
jg 0xca53
cmpl $0x6d, %eax
je 0xcaa4
cmpl $0x72, %eax
jne 0xce1a
movq (%r13), %rdi
callq 0x2220
movl %eax, 0x2593b(%rip)     # 0x3238c
jmp 0xc9f5
cmpl $0x74, %eax
je 0xca90
cmpl $0x78, %eax
jne 0xce1d
movl $0x0, 0x25925(%rip)     # 0x32390
jmp 0xc9f5
cmpl $0x58, %eax
je 0xcac7
cmpl $0x63, %eax
je 0xcab8
cmpl $0x65, %eax
jne 0xce1d
movq (%r13), %rax
movq %rax, 0x258f5(%rip)     # 0x32380
jmp 0xc9f5
movq (%r13), %rdi
callq 0x2220
movl %eax, 0x258fd(%rip)     # 0x3239c
jmp 0xc9f5
movq (%r13), %rdi
callq 0x2220
movl %eax, 0x258d5(%rip)     # 0x32388
jmp 0xc9f5
movl $0x0, 0x258d2(%rip)     # 0x32394
jmp 0xc9f5
movl $0x1, 0x258bf(%rip)     # 0x32390
jmp 0xc9f5
cmpl $-0x1, %eax
jne 0xce15
cmpl $0x0, 0x258a2(%rip)     # 0x32388
jg 0xcaf4
movl 0x258ae(%rip), %eax     # 0x3239c
movl %eax, 0x25894(%rip)     # 0x32388
leaq 0x38(%rsp), %rbx
movq $0x0, (%rbx)
movq 0x25879(%rip), %rdi     # 0x32380
leaq 0x40(%rsp), %rax
leaq 0x2c(%rsp), %r10
leaq 0x30(%rsp), %r11
leaq 0x20(%rsp), %r13
leaq 0x25796(%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 0xcee7
addq $0x20, %rsp
movl (%r14), %esi
movl (%r15), %edx
movl (%r12), %ecx
leaq 0x1c9e2(%rip), %rdi     # 0x29540
xorl %eax, %eax
callq 0x2110
movl $0x7e86d5ce, %edi       # imm = 0x7E86D5CE
callq 0x2150
leaq 0x1c972(%rip), %rdi     # 0x294e8
movl $0x7e86d5ce, %esi       # imm = 0x7E86D5CE
xorl %eax, %eax
callq 0x2110
movq (%rbx), %rdi
movl (%r14), %esi
movl $0x2, %edx
callq 0xae9f
movq %rax, %rbx
cmpl $0x0, (%r14)
jle 0xcbb9
xorl %r14d, %r14d
movq 0x38(%rsp), %rax
movq (%rax,%r14,8), %rdi
callq 0x484f
incq %r14
movslq 0x34(%rsp), %rax
cmpq %rax, %r14
jl 0xcb9e
movq 0x38(%rsp), %rdi
callq 0x2030
movq 0x40(%rsp), %rdi
movl 0x2c(%rsp), %esi
movl 0x30(%rsp), %edx
callq 0x17f58
movl 0x257c1(%rip), %esi     # 0x3239c
movl 0x257a7(%rip), %edx     # 0x32388
movq 0x10(%rsp), %rcx
movl 0x1c(%rsp), %r8d
movl 0x2579a(%rip), %r9d     # 0x3238c
movq %rbx, %rdi
pushq %rax
pushq 0x28(%rsp)
callq 0x17c7e
addq $0x10, %rsp
testq %rax, %rax
je 0xcdf2
movq %rax, %r14
leaq 0x1cd56(%rip), %rdi     # 0x2996c
callq 0x2090
movl 0xc(%r14), %esi
shll $0x3, %esi
leaq 0x1c94f(%rip), %rdi     # 0x29578
xorl %eax, %eax
callq 0x2110
movl 0x10(%r14), %esi
shll $0x3, %esi
leaq 0x1c957(%rip), %rdi     # 0x29595
xorl %eax, %eax
callq 0x2110
movl 0x14(%r14), %esi
shll $0x3, %esi
leaq 0x1c961(%rip), %rdi     # 0x295b4
xorl %eax, %eax
callq 0x2110
cmpl $-0x1, 0x2572f(%rip)    # 0x32390
jne 0xccaa
leaq 0x1c96a(%rip), %rdi     # 0x295d4
xorl %eax, %eax
callq 0x2110
movq 0x25348(%rip), %rax     # 0x31fc0
movq (%rax), %rdi
callq 0x21e0
leaq 0x1c97f(%rip), %rdi     # 0x29606
leaq 0xf(%rsp), %r15
movq %r15, %rsi
xorl %eax, %eax
callq 0x2230
callq 0x2180
xorl %eax, %eax
cmpb $0x79, (%r15)
sete %al
movl %eax, 0x256e6(%rip)     # 0x32390
cmpl $0x0, 0x256df(%rip)     # 0x32390
je 0xcd12
movl 0xc(%r14), %edi
movl 0x10(%r14), %esi
movl 0x256cb(%rip), %edx     # 0x3238c
callq 0xc848
movq %rax, %r15
leaq 0x1d55d(%rip), %rsi     # 0x2a22d
movq %rax, %rdi
callq 0x2210
movq %rax, %r12
movq 0x20(%rsp), %rdx
movq %rax, %rdi
movq %r14, %rsi
callq 0x7375
movq %r12, %rdi
callq 0x20e0
leaq 0x1cc9e(%rip), %rdi     # 0x29998
callq 0x2090
leaq 0x1c904(%rip), %rdi     # 0x2960a
movq %r15, %rsi
xorl %eax, %eax
callq 0x2110
jmp 0xcd1e
leaq 0x1cc67(%rip), %rdi     # 0x29980
callq 0x2090
cmpl $-0x1, 0x2566f(%rip)    # 0x32394
jne 0xcd6e
leaq 0x1c8e1(%rip), %rdi     # 0x2960f
xorl %eax, %eax
callq 0x2110
movq 0x25284(%rip), %rax     # 0x31fc0
movq (%rax), %rdi
callq 0x21e0
leaq 0x1c8bb(%rip), %rdi     # 0x29606
leaq 0xf(%rsp), %r15
movq %r15, %rsi
xorl %eax, %eax
callq 0x2230
callq 0x2180
xorl %eax, %eax
cmpb $0x79, (%r15)
sete %al
movl %eax, 0x25626(%rip)     # 0x32394
cmpl $0x0, 0x2561f(%rip)     # 0x32394
je 0xcdde
movl 0xc(%r14), %edi
movl 0x10(%r14), %esi
movl 0x25607(%rip), %edx     # 0x3238c
callq 0xc8f0
movq %rax, %r15
movq %rax, %rdi
callq 0x2270
movq %rax, %r12
leaq 0x1c89c(%rip), %rdi     # 0x2963b
movq %r15, %rsi
xorl %eax, %eax
callq 0x2110
leaq 0x1c8a2(%rip), %rdi     # 0x29652
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 0x12822
jmp 0xcdea
leaq 0x1cbc9(%rip), %rdi     # 0x299ae
callq 0x2090
movq %r14, %rdi
callq 0x7060
movq %rbx, %rdi
callq 0xc453
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 0xce1d
cmpl $0x68, %eax
movq (%rbx), %rdi
callq 0xc788
movl $0x1, %edi
callq 0x2250
nop