// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
pushq %rbx
subq $0xc8, %rsp
movl $0x0, -0xc(%rbp)
movl %edi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
cmpl $0x5, -0x10(%rbp)
jge 0x129b6
movq -0x18(%rbp), %rax
movq (%rax), %rdi
callq 0x12630
movl $0x1, %edi
callq 0x2240
movl $0xffffffff, 0x2a9c8(%rip) # imm = 0xFFFFFFFF
movl $0x0, 0x2a9c2(%rip)     # 0x3d38c
movl $0xffffffff, 0x2a9bc(%rip) # imm = 0xFFFFFFFF
movl $0xffffffff, 0x2a9b6(%rip) # imm = 0xFFFFFFFF
movl $0xffffffff, 0x2a9b0(%rip) # imm = 0xFFFFFFFF
movl $0x0, -0x5c(%rbp)
movl -0x10(%rbp), %edi
movq -0x18(%rbp), %rsi
leaq 0x22eb2(%rip), %rdx     # 0x358af
leaq 0x2a76c(%rip), %rcx     # 0x3d170
leaq -0x5c(%rbp), %r8
callq 0x20f0
movl %eax, -0x1c(%rbp)
cmpl $-0x1, -0x1c(%rbp)
jne 0x12a1b
jmp 0x12b09
movl -0x1c(%rbp), %eax
addl $-0x3f, %eax
movl %eax, %ecx
movq %rcx, -0xa8(%rbp)
subl $0x39, %eax
ja 0x12aee
movq -0xa8(%rbp), %rax
leaq 0x22a4b(%rip), %rcx     # 0x3548c
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq 0x2a587(%rip), %rax     # 0x3cfd8
movq (%rax), %rax
movq %rax, 0x2a925(%rip)     # 0x3d380
jmp 0x12b04
movq 0x2a571(%rip), %rax     # 0x3cfd8
movq (%rax), %rdi
callq 0x2210
movl %eax, 0x2a927(%rip)     # 0x3d39c
jmp 0x12b04
movq 0x2a557(%rip), %rax     # 0x3cfd8
movq (%rax), %rdi
callq 0x2210
movl %eax, 0x2a8f9(%rip)     # 0x3d388
jmp 0x12b04
movq 0x2a540(%rip), %rax     # 0x3cfd8
movq (%rax), %rdi
callq 0x2210
movl %eax, 0x2a8e6(%rip)     # 0x3d38c
jmp 0x12b04
movl $0x1, 0x2a8de(%rip)     # 0x3d390
jmp 0x12b04
movl $0x0, 0x2a8d2(%rip)     # 0x3d390
jmp 0x12b04
movl $0x1, 0x2a8ca(%rip)     # 0x3d394
jmp 0x12b04
movl $0x0, 0x2a8be(%rip)     # 0x3d394
jmp 0x12b04
movq -0x18(%rbp), %rax
movq (%rax), %rdi
callq 0x12630
movl $0x1, %edi
callq 0x2240
movq -0x18(%rbp), %rax
movq (%rax), %rdi
callq 0x12630
movl $0x1, %edi
callq 0x2240
jmp 0x129e8
cmpl $0x0, 0x2a878(%rip)     # 0x3d388
jg 0x12b1e
movl 0x2a884(%rip), %eax     # 0x3d39c
movl %eax, 0x2a86a(%rip)     # 0x3d388
movq $0x0, -0x40(%rbp)
movq 0x2a853(%rip), %rdi     # 0x3d380
leaq -0x40(%rbp), %rsi
leaq -0x20(%rbp), %rdx
leaq 0x2a78c(%rip), %rcx     # 0x3d2c8
leaq -0x28(%rbp), %r8
leaq -0x2c(%rbp), %r9
leaq -0x48(%rbp), %rbx
leaq -0x54(%rbp), %r11
leaq -0x58(%rbp), %r10
leaq -0x50(%rbp), %rax
movq %rbx, (%rsp)
movq %r11, 0x8(%rsp)
movq %r10, 0x10(%rsp)
movq %rax, 0x18(%rsp)
callq 0x13010
movl -0x20(%rbp), %esi
leaq 0x2a752(%rip), %rax     # 0x3d2c8
movl (%rax), %edx
movl -0x2c(%rbp), %ecx
leaq 0x22d45(%rip), %rdi     # 0x358c7
movb $0x0, %al
callq 0x2100
callq 0x12700
movq -0x40(%rbp), %rdi
movl -0x20(%rbp), %esi
movl $0x2, %edx
callq 0x100a0
movq %rax, -0x38(%rbp)
movl $0x0, -0x30(%rbp)
movl -0x30(%rbp), %eax
cmpl -0x20(%rbp), %eax
jge 0x12bce
movq -0x40(%rbp), %rax
movslq -0x30(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
callq 0x5900
movl -0x30(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
jmp 0x12baa
movq -0x40(%rbp), %rdi
callq 0x2030
movq -0x50(%rbp), %rdi
movl -0x58(%rbp), %esi
movl -0x54(%rbp), %edx
callq 0x23ba0
movq %rax, -0x68(%rbp)
movq -0x38(%rbp), %rdi
movl 0x2a7a8(%rip), %esi     # 0x3d39c
movl 0x2a78e(%rip), %edx     # 0x3d388
movq -0x28(%rbp), %rcx
movl -0x2c(%rbp), %r8d
movl 0x2a783(%rip), %r9d     # 0x3d38c
movq -0x48(%rbp), %r10
movq -0x68(%rbp), %rax
movq %r10, (%rsp)
movq %rax, 0x8(%rsp)
callq 0x23800
movq %rax, -0x70(%rbp)
cmpq $0x0, -0x70(%rbp)
je 0x12e6f
leaq 0x22cca(%rip), %rdi     # 0x358ff
movb $0x0, %al
callq 0x2100
movq -0x70(%rbp), %rax
movl 0xc(%rax), %esi
shll $0x3, %esi
leaq 0x22cc7(%rip), %rdi     # 0x35914
movb $0x0, %al
callq 0x2100
movq -0x70(%rbp), %rax
movl 0x10(%rax), %esi
shll $0x3, %esi
leaq 0x22ccc(%rip), %rdi     # 0x35931
movb $0x0, %al
callq 0x2100
movq -0x70(%rbp), %rax
movl 0x14(%rax), %esi
shll $0x3, %esi
leaq 0x22cd3(%rip), %rdi     # 0x35950
movb $0x0, %al
callq 0x2100
cmpl $-0x1, 0x2a705(%rip)    # 0x3d390
jne 0x12cd8
leaq 0x22cdc(%rip), %rdi     # 0x35970
movb $0x0, %al
callq 0x2100
movq 0x2a326(%rip), %rax     # 0x3cfc8
movq (%rax), %rdi
callq 0x21d0
leaq 0x22cf1(%rip), %rdi     # 0x359a2
leaq -0x79(%rbp), %rsi
movb $0x0, %al
callq 0x2220
callq 0x2170
movsbl -0x79(%rbp), %edx
xorl %eax, %eax
movl $0x1, %ecx
cmpl $0x79, %edx
cmovel %ecx, %eax
movl %eax, 0x2a6b8(%rip)     # 0x3d390
cmpl $0x0, 0x2a6b1(%rip)     # 0x3d390
je 0x12d57
movq -0x70(%rbp), %rax
movl 0xc(%rax), %edi
movq -0x70(%rbp), %rax
movl 0x10(%rax), %esi
movl 0x2a697(%rip), %edx     # 0x3d38c
callq 0x12730
movq %rax, -0x88(%rbp)
movq -0x88(%rbp), %rdi
leaq 0x236be(%rip), %rsi     # 0x363cd
callq 0x2200
movq %rax, -0x78(%rbp)
movq -0x78(%rbp), %rdi
movq -0x70(%rbp), %rsi
movq -0x48(%rbp), %rdx
callq 0x9810
movq -0x78(%rbp), %rdi
callq 0x20d0
leaq 0x22c6d(%rip), %rdi     # 0x359a6
movb $0x0, %al
callq 0x2100
movq -0x88(%rbp), %rsi
leaq 0x22c6f(%rip), %rdi     # 0x359bd
movb $0x0, %al
callq 0x2100
jmp 0x12d65
leaq 0x22c64(%rip), %rdi     # 0x359c2
movb $0x0, %al
callq 0x2100
cmpl $-0x1, 0x2a628(%rip)    # 0x3d394
jne 0x12dbf
leaq 0x22c66(%rip), %rdi     # 0x359db
movb $0x0, %al
callq 0x2100
movq 0x2a245(%rip), %rax     # 0x3cfc8
movq (%rax), %rdi
callq 0x21d0
leaq 0x22c10(%rip), %rdi     # 0x359a2
leaq -0x89(%rbp), %rsi
movb $0x0, %al
callq 0x2220
callq 0x2170
movsbl -0x89(%rbp), %edx
xorl %eax, %eax
movl $0x1, %ecx
cmpl $0x79, %edx
cmovel %ecx, %eax
movl %eax, 0x2a5d5(%rip)     # 0x3d394
cmpl $0x0, 0x2a5ce(%rip)     # 0x3d394
je 0x12e58
movq -0x70(%rbp), %rax
movl 0xc(%rax), %edi
movq -0x70(%rbp), %rax
movl 0x10(%rax), %esi
movl 0x2a5ac(%rip), %edx     # 0x3d38c
callq 0x12860
movq %rax, -0x98(%rbp)
movq -0x98(%rbp), %rdi
callq 0x2260
movq %rax, -0xa0(%rbp)
movq -0x98(%rbp), %rsi
leaq 0x22bfa(%rip), %rdi     # 0x35a07
movb $0x0, %al
callq 0x2100
movq -0x98(%rbp), %rsi
movq -0xa0(%rbp), %rdx
movq -0xa0(%rbp), %rcx
leaq 0x22bee(%rip), %rdi     # 0x35a1e
movb $0x0, %al
callq 0x2100
movq -0x98(%rbp), %rdi
movq -0x70(%rbp), %rsi
movq -0x28(%rbp), %rdx
movl -0x2c(%rbp), %ecx
movq -0x38(%rbp), %r8
movq -0x48(%rbp), %r9
callq 0x1b780
jmp 0x12e66
leaq 0x22c0f(%rip), %rdi     # 0x35a6e
movb $0x0, %al
callq 0x2100
movq -0x70(%rbp), %rdi
callq 0x93b0
movq -0x38(%rbp), %rdi
callq 0x12220
movq -0x28(%rbp), %rdi
callq 0x2030
xorl %eax, %eax
addq $0xc8, %rsp
popq %rbx
popq %rbp
retq
nopl (%rax)