// File: /workspace/github_c_makefile_star2_O2/cbouilla[P]AES-attacks-finder/parser_machinery.c
// Function: parse
void parse(char *filename, 
	   Equation **equations, 
	   int *n_eqs, 
	   int *n_vars, 
	   int **known_vars, 
	   int *known_array_size, 
	   string_list_t **dictionnary,
	   int *n_msg,
	   int *n_var_by_msg,
	   int ***message_groups) {
  int i,j;

  message_variables = malloc(32*sizeof(string_list_t*));
  for(i=0; i<32; i++)
    message_variables[i] = NULL;

  yyin = fopen(filename, "r");
  // TODO : detect errors

  yyparse();
  fclose(yyin);

  register_string(&all_variables, "1"); // register the constant variable



  // allocate the equations
  Equation *eqs = malloc(n_equations*sizeof(Equation));

  equation_t *equation = all_equations;
  i=0;
  while (equation != NULL) {
    
    // allocate the equation
    eqs[i] = malloc(sizeof(EQUATION));
    
    // scan the terms
    eqs[i]->nV = 0;
    eqs[i]->nS = 0;
    term_t *term = equation->head_term;
    while (term != NULL) {
      if (term->sbox == NULL) eqs[i]->nV++; else eqs[i]->nS++;
      term = term->next;
    }
    
    // allocate the coefficients/stuff
    eqs[i]->var = malloc(eqs[i]->nV*sizeof(int));
    eqs[i]->coefV = malloc(eqs[i]->nV*sizeof(unsigned char));
    eqs[i]->sbox = malloc(eqs[i]->nS*sizeof(unsigned char));
    eqs[i]->coefS = malloc(eqs[i]->nS*sizeof(unsigned char));
    eqs[i]->S = malloc(eqs[i]->nS*sizeof(EQUATION*));
    for(j=0; j<eqs[i]->nS; j++)
      eqs[i]->S[j] = malloc(sizeof(EQUATION));
    
    // now rescan the terms and populate the datastructure
    term = equation->head_term;
    int lin_i = 0;
    int box_i = 0;
    while (term != NULL) {
      if (term->variable == NULL) { /* constant term */
	eqs[i]->coefV[lin_i] = term->coeff;
	//printf("read coeff : %02x\n", term->coeff);
	eqs[i]->var[lin_i] = 0; // variable zero denotes constant terms

	lin_i++;
      } else if (term->sbox == NULL) { /* linear term */
	eqs[i]->coefV[lin_i] = term->coeff;
	eqs[i]->var[lin_i] = register_string(&all_variables, term->variable); 

	lin_i++;	
      } else { /* Sbox */
	eqs[i]->coefS[box_i] = term->coeff;
	eqs[i]->sbox[box_i] = register_string(&all_boxes, term->sbox); 
	eqs[i]->S[box_i]->nV = 1;
	eqs[i]->S[box_i]->nS = 0;
	eqs[i]->S[box_i]->S = NULL;
	eqs[i]->S[box_i]->coefS = NULL;
	eqs[i]->S[box_i]->sbox = NULL;
	eqs[i]->S[box_i]->coefV = malloc(sizeof(unsigned char));
	eqs[i]->S[box_i]->var = malloc(sizeof(int));
	eqs[i]->S[box_i]->coefV[0] = 1;
	eqs[i]->S[box_i]->var[0] = register_string(&all_variables, term->variable);

	box_i++;	
      }
      term = term->next;
    }

    i++;
    equation = equation->next;
  }

  // now dealing with the known vars

  n_known_variables++;   // we know "variable 0", ie the constant terms
  int * known_vars_array = malloc(n_known_variables*sizeof(int));
  string_list_t *curr_var = known_variables;
  known_vars_array[0] = 0;
  j = 1;
  while (curr_var != NULL) {
    known_vars_array[j] = find_string(all_variables, curr_var->name);
    if (known_vars_array[j] < 0) {
      fprintf(stderr, "[Parser] a known variable (%s) does not occur in the equations !\n", curr_var->name);
      exit(1);
    }
    curr_var = curr_var->next;
    j++;
  }

  // dealing with the "message" grouping, if relevant
  int all_msg_nvar = -1;
  int **msg_grouping = NULL;

  if (n_messages == 0 && message_variables[0] == NULL) 
    printf("[+] No (explicit) message information available\n");
  else {
    printf("[+] %d distinct messages appear in the equations\n", n_messages+1);
    n_messages++;

    msg_grouping = malloc(n_messages*sizeof(int*));
    for(i=0; i<n_messages; i++) {
      
      // count the number of variables in current message
      int nvars = 0;
      curr_var = message_variables[i];
      while (curr_var != NULL) { curr_var = curr_var->next; nvars++; }
      printf("  [+] %d variables found in message %d\n", nvars, i+1);
      
      if (all_msg_nvar < 0) all_msg_nvar = nvars;
      if (all_msg_nvar > 0 && nvars != all_msg_nvar) {
	printf("\nHmmm, all your message do not have the same number of variables... This is quite suspect !\n");
	exit(1);
      }
      
      // allocate and fill array 
      msg_grouping[i] = malloc(nvars*sizeof(int));
      curr_var = message_variables[i];
      j = 0;
      while (curr_var != NULL) {
	msg_grouping[i][j] = find_string(all_variables, curr_var->name);
	if (msg_grouping[i][j] < 0) {
	  fprintf(stderr, "[Parser] in the ''message'' section, a variable (%s) does not occur in the equations !\n", curr_var->name);
	  exit(1);
	}
	curr_var = curr_var->next;
	j++;
      }
      
      // moving on to the next message
    }
  }
    
  // storing the results and leaving
  
  *equations = eqs;
  *known_vars = known_vars_array;
  
  *n_eqs = n_equations;
  *n_vars = all_variables->number + 1;  // numbered from 0 to all_variables->number
  *n_msg = n_messages;
  *n_var_by_msg = all_msg_nvar;
  
  *known_array_size = n_known_variables;
  *dictionnary = all_variables;
  *message_groups = msg_grouping;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %r9, 0x40(%rsp)
movq %r8, 0x28(%rsp)
movq %rcx, 0x38(%rsp)
movq %rdx, 0x30(%rsp)
movq %rsi, 0x20(%rsp)
movq %rdi, %rbx
movl $0x100, %edi            # imm = 0x100
callq 0x21c0
movq %rax, 0x2445c(%rip)     # 0x303d0
xorl %eax, %eax
cmpq $0x20, %rax
je 0xbf8d
movq 0x2444d(%rip), %rcx     # 0x303d0
andq $0x0, (%rcx,%rax,8)
incq %rax
jmp 0xbf76
leaq 0x1eb5e(%rip), %rsi     # 0x2aaf2
movq %rbx, %rdi
callq 0x2200
leaq 0x2444d(%rip), %rbx     # 0x303f0
movq %rax, (%rbx)
callq 0xc43c
movq (%rbx), %rdi
callq 0x20e0
leaq 0x243ee(%rip), %rdi     # 0x303a8
leaq 0x1b47f(%rip), %rsi     # 0x27440
callq 0xbe88
movslq 0x243f3(%rip), %rdi     # 0x303c0
shlq $0x3, %rdi
callq 0x21c0
movq %rax, 0x18(%rsp)
leaq 0x243be(%rip), %r12     # 0x303a0
xorl %ebx, %ebx
pushq $0x38
popq %r14
movq (%r12), %r12
testq %r12, %r12
je 0xc1d2
movq %r14, %rdi
callq 0x21c0
movq %rax, %rbp
andl $0x0, (%rax)
andl $0x0, 0x18(%rax)
movq 0x18(%rsp), %rax
movq %rbx, 0x10(%rsp)
movq %rbp, (%rax,%rbx,8)
xorl %eax, %eax
xorl %ebx, %ebx
movq %r12, %rcx
movq (%rcx), %rcx
testq %rcx, %rcx
je 0xc03d
cmpq $0x0, 0x8(%rcx)
je 0xc032
incl %ebx
movl %ebx, 0x18(%rbp)
jmp 0xc037
incl %eax
movl %eax, (%rbp)
addq $0x18, %rcx
jmp 0xc01c
movslq %eax, %r15
leaq (,%r15,4), %rdi
callq 0x21c0
movq %rax, 0x8(%rbp)
movq %r15, %rdi
callq 0x21c0
movq %rax, 0x10(%rbp)
movslq %ebx, %r15
movq %r15, %rdi
callq 0x21c0
movq %rax, 0x30(%rbp)
movq %r15, %rdi
callq 0x21c0
movq %rax, 0x28(%rbp)
leaq (,%r15,8), %rdi
callq 0x21c0
movq %rax, 0x20(%rbp)
xorl %ebx, %ebx
cmpq %r15, %rbx
jge 0xc0a5
movq %r14, %rdi
callq 0x21c0
movq 0x20(%rbp), %rcx
movq %rax, (%rcx,%rbx,8)
incq %rbx
jmp 0xc08b
xorl %ebx, %ebx
movq %r12, 0x8(%rsp)
movq %r12, %r15
xorl %r13d, %r13d
movq (%r15), %r15
testq %r15, %r15
je 0xc1bc
cmpq $0x0, (%r15)
movslq %ebx, %r12
je 0xc177
cmpq $0x0, 0x8(%r15)
movb 0x10(%r15), %al
je 0xc18e
movq 0x28(%rbp), %rcx
movslq %r13d, %r13
movb %al, (%rcx,%r13)
movq 0x8(%r15), %rsi
leaq 0x242c0(%rip), %rdi     # 0x303b0
callq 0xbe88
movq 0x30(%rbp), %rcx
movb %al, (%rcx,%r13)
movq 0x20(%rbp), %rax
movq (%rax,%r13,8), %rax
movl $0x1, (%rax)
andl $0x0, 0x18(%rax)
andq $0x0, 0x20(%rax)
movq 0x20(%rbp), %r12
movq (%r12,%r13,8), %rax
andq $0x0, 0x28(%rax)
movq (%r12,%r13,8), %rax
andq $0x0, 0x30(%rax)
pushq $0x1
popq %rdi
callq 0x21c0
movq (%r12,%r13,8), %rcx
movq %rax, 0x10(%rcx)
pushq $0x4
popq %rdi
callq 0x21c0
movq (%r12,%r13,8), %rcx
movq %rax, 0x8(%rcx)
movq (%r12,%r13,8), %rax
movq 0x10(%rax), %rax
movb $0x1, (%rax)
movq (%r15), %rsi
leaq 0x24249(%rip), %rdi     # 0x303a8
callq 0xbe88
movq 0x20(%rbp), %rcx
movq (%rcx,%r13,8), %rcx
movq 0x8(%rcx), %rcx
movl %eax, (%rcx)
incl %r13d
jmp 0xc1b3
movb 0x10(%r15), %al
movq 0x10(%rbp), %rcx
movb %al, (%rcx,%r12)
movq 0x8(%rbp), %rax
andl $0x0, (%rax,%r12,4)
jmp 0xc1ad
movq 0x10(%rbp), %rcx
movb %al, (%rcx,%r12)
movq (%r15), %rsi
leaq 0x24208(%rip), %rdi     # 0x303a8
callq 0xbe88
movq 0x8(%rbp), %rcx
movl %eax, (%rcx,%r12,4)
incl %r12d
movl %r12d, %ebx
addq $0x18, %r15
jmp 0xc0b2
movq 0x10(%rsp), %rbx
incq %rbx
movq 0x8(%rsp), %r12
addq $0x8, %r12
jmp 0xbfe8
movslq 0x241eb(%rip), %rax     # 0x303c4
leaq 0x1(%rax), %rcx
movl %ecx, 0x241e1(%rip)     # 0x303c4
leaq 0x4(,%rax,4), %rdi
callq 0x21c0
andl $0x0, (%rax)
movq 0x241be(%rip), %rbx     # 0x303b8
movq 0x241a7(%rip), %r14     # 0x303a8
movq %rax, 0x10(%rsp)
movq %rax, %r12
addq $0x4, %r12
testq %rbx, %rbx
je 0xc232
movq (%rbx), %r15
movq %r14, %rdi
movq %r15, %rsi
callq 0xbeef
movl %eax, (%r12)
testl %eax, %eax
js 0xc415
movq 0x10(%rbx), %rbx
jmp 0xc209
movl 0x24194(%rip), %esi     # 0x303cc
testl %esi, %esi
jne 0xc24d
movq 0x2418d(%rip), %rax     # 0x303d0
cmpq $0x0, (%rax)
je 0xc34d
incl %esi
leaq 0x1b7b3(%rip), %rdi     # 0x27a09
xorl %eax, %eax
callq 0x2110
movslq 0x24168(%rip), %rax     # 0x303cc
leaq 0x1(%rax), %r14
movl %r14d, 0x2415d(%rip)    # 0x303cc
leaq 0x8(,%rax,8), %rdi
callq 0x21c0
movq %rax, 0x8(%rsp)
pushq $-0x1
popq %r15
xorl %ebx, %ebx
movslq %r14d, %rax
cmpq %rax, %rbx
jge 0xc366
leaq (,%rbx,8), %r14
movq 0x2412e(%rip), %rax     # 0x303d0
addq %r14, %rax
xorl %r13d, %r13d
xorl %ebp, %ebp
movq (%rax), %rax
testq %rax, %rax
je 0xc2be
addq $0x10, %rax
incl %ebp
addq $0x4, %r13
jmp 0xc2aa
leaq 0x1(%rbx), %r12
leaq 0x1b772(%rip), %rdi     # 0x27a3b
movl %ebp, %esi
movl %r12d, %edx
xorl %eax, %eax
callq 0x2110
testl %r15d, %r15d
cmovsl %ebp, %r15d
testl %r15d, %r15d
sete %al
cmpl %ebp, %r15d
sete %cl
orb %al, %cl
je 0xc42b
movq %r13, %rdi
callq 0x21c0
movq %rax, %r13
movq 0x8(%rsp), %rax
movq %r13, (%rax,%rbx,8)
addq 0x240c5(%rip), %r14     # 0x303d0
movq 0x24096(%rip), %rbp     # 0x303a8
movq (%r14), %r14
testq %r14, %r14
je 0xc33e
movq (%r14), %rbx
movq %rbp, %rdi
movq %rbx, %rsi
callq 0xbeef
movl %eax, (%r13)
testl %eax, %eax
js 0xc3f2
addq $0x10, %r14
addq $0x4, %r13
jmp 0xc312
movl 0x24087(%rip), %r14d    # 0x303cc
movq %r12, %rbx
jmp 0xc287
leaq 0x1b7c3(%rip), %rdi     # 0x27b17
callq 0x2090
pushq $-0x1
popq %r15
movq $0x0, 0x8(%rsp)
movq 0x20(%rsp), %rax
movq 0x18(%rsp), %rcx
movq %rcx, (%rax)
movq 0x28(%rsp), %rax
movq 0x10(%rsp), %rcx
movq %rcx, (%rax)
movl 0x2403a(%rip), %eax     # 0x303c0
movq 0x30(%rsp), %rcx
movl %eax, (%rcx)
movq 0x24014(%rip), %rax     # 0x303a8
movl 0x8(%rax), %ecx
incl %ecx
movq 0x38(%rsp), %rdx
movl %ecx, (%rdx)
movl 0x24026(%rip), %ecx     # 0x303cc
movq 0x88(%rsp), %rdx
movl %ecx, (%rdx)
movq 0x90(%rsp), %rcx
movl %r15d, (%rcx)
movl 0x24003(%rip), %ecx     # 0x303c4
movq 0x40(%rsp), %rdx
movl %ecx, (%rdx)
movq 0x80(%rsp), %rcx
movq %rax, (%rcx)
movq 0x98(%rsp), %rax
movq 0x8(%rsp), %rcx
movq %rcx, (%rax)
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x23bff(%rip), %rax     # 0x2fff8
movq (%rax), %rdi
leaq 0x1b660(%rip), %rsi     # 0x27a63
movq %rbx, %rdx
xorl %eax, %eax
callq 0x2180
pushq $0x1
popq %rdi
callq 0x2240
movq 0x23bdc(%rip), %rax     # 0x2fff8
movq (%rax), %rdi
leaq 0x1b5a1(%rip), %rsi     # 0x279c7
movq %r15, %rdx
jmp 0xc406
leaq 0x1b689(%rip), %rdi     # 0x27abb
callq 0x2090
jmp 0xc40d
nopl (%rax)