// File: /workspace/github_c_makefile_star2_O3/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 $0x58, %rsp
movq %r9, 0x48(%rsp)
movq %r8, 0x30(%rsp)
movq %rcx, 0x40(%rsp)
movq %rdx, 0x38(%rsp)
movq %rsi, 0x28(%rsp)
movq %rdi, %rbx
movl $0x100, %edi            # imm = 0x100
callq 0x21d0
movq %rax, 0x250d6(%rip)     # 0x323d0
xorl %eax, %eax
movq 0x250cd(%rip), %rcx     # 0x323d0
movq $0x0, (%rcx,%rax,8)
incq %rax
cmpq $0x20, %rax
jne 0xd2fc
leaq 0x1f837(%rip), %rsi     # 0x2cb52
movq %rbx, %rdi
callq 0x2210
leaq 0x250c6(%rip), %rbx     # 0x323f0
movq %rax, (%rbx)
callq 0xd84c
movq (%rbx), %rdi
callq 0x20e0
leaq 0x25067(%rip), %rdi     # 0x323a8
leaq 0x1c158(%rip), %rsi     # 0x294a0
callq 0xd204
movslq 0x2506c(%rip), %rdi     # 0x323c0
shlq $0x3, %rdi
callq 0x21d0
movq %rax, 0x20(%rsp)
movq 0x25037(%rip), %r14     # 0x323a0
testq %r14, %r14
je 0xd576
xorl %ebp, %ebp
xorl %ebx, %ebx
movl $0x38, %edi
callq 0x21d0
movq %rax, %r15
movq 0x20(%rsp), %rax
movq %rbx, 0x10(%rsp)
movq %r15, (%rax,%rbx,8)
movl %ebp, (%r15)
movl %ebp, 0x18(%r15)
movq (%r14), %rax
testq %rax, %rax
je 0xd3c6
xorl %ecx, %ecx
xorl %ebx, %ebx
cmpq $0x0, 0x8(%rax)
je 0xd3b3
incl %ebx
movl %ebx, 0x18(%r15)
jmp 0xd3b8
incl %ecx
movl %ecx, (%r15)
movq 0x18(%rax), %rax
testq %rax, %rax
jne 0xd3a4
movslq %ecx, %r12
jmp 0xd3cb
xorl %ebx, %ebx
xorl %r12d, %r12d
leaq (,%r12,4), %rdi
callq 0x21d0
movq %rax, 0x8(%r15)
movq %r12, %rdi
callq 0x21d0
movq %rax, 0x10(%r15)
movslq %ebx, %r12
movq %r12, %rdi
callq 0x21d0
movq %rax, 0x30(%r15)
movq %r12, %rdi
callq 0x21d0
movq %rax, 0x28(%r15)
leaq (,%r12,8), %rdi
callq 0x21d0
movq %rax, 0x20(%r15)
testl %r12d, %r12d
jle 0xd435
xorl %ebx, %ebx
movl $0x38, %edi
callq 0x21d0
movq 0x20(%r15), %rcx
movq %rax, (%rcx,%rbx,8)
incq %rbx
cmpq %r12, %rbx
jl 0xd41b
movq %r14, 0x18(%rsp)
movq (%r14), %r12
testq %r12, %r12
je 0xd55c
xorl %ebx, %ebx
xorl %r14d, %r14d
cmpq $0x0, (%r12)
je 0xd50c
cmpq $0x0, 0x8(%r12)
movb 0x10(%r12), %al
je 0xd528
movq 0x28(%r15), %rcx
movslq %ebx, %rbx
movb %al, (%rcx,%rbx)
movq 0x8(%r12), %rsi
leaq 0x24f33(%rip), %rdi     # 0x323b0
callq 0xd204
movq 0x30(%r15), %rcx
movb %al, (%rcx,%rbx)
movq 0x20(%r15), %rax
movq (%rax,%rbx,8), %rax
movl $0x1, (%rax)
movl $0x0, 0x18(%rax)
movq %rbp, 0x20(%rax)
movq 0x20(%r15), %r13
movq (%r13,%rbx,8), %rax
movq %rbp, 0x28(%rax)
movq (%r13,%rbx,8), %rax
movq %rbp, 0x30(%rax)
movl $0x1, %edi
callq 0x21d0
movq (%r13,%rbx,8), %rcx
movq %rax, 0x10(%rcx)
movl $0x4, %edi
callq 0x21d0
movq (%r13,%rbx,8), %rcx
movq %rax, 0x8(%rcx)
movq (%r13,%rbx,8), %rax
movq 0x10(%rax), %rax
movb $0x1, (%rax)
movq (%r12), %rsi
leaq 0x24eb3(%rip), %rdi     # 0x323a8
callq 0xd204
movq 0x20(%r15), %rcx
movq (%rcx,%rbx,8), %rcx
movq 0x8(%rcx), %rcx
movl %eax, (%rcx)
incl %ebx
jmp 0xd54e
movb 0x10(%r12), %al
movq 0x10(%r15), %rcx
movslq %r14d, %rdx
movb %al, (%rcx,%rdx)
movq 0x8(%r15), %rax
movl $0x0, (%rax,%rdx,4)
jmp 0xd54b
movq 0x10(%r15), %rcx
movslq %r14d, %r14
movb %al, (%rcx,%r14)
movq (%r12), %rsi
leaq 0x24e6a(%rip), %rdi     # 0x323a8
callq 0xd204
movq 0x8(%r15), %rcx
movl %eax, (%rcx,%r14,4)
incl %r14d
movq 0x18(%r12), %r12
testq %r12, %r12
jne 0xd44b
movq 0x10(%rsp), %rbx
incq %rbx
movq 0x18(%rsp), %r14
movq 0x8(%r14), %r14
testq %r14, %r14
jne 0xd376
movslq 0x24e47(%rip), %rax     # 0x323c4
leaq 0x1(%rax), %rcx
movl %ecx, 0x24e3d(%rip)     # 0x323c4
leaq 0x4(,%rax,4), %rdi
callq 0x21d0
movq 0x24e1d(%rip), %r15     # 0x323b8
movq %rax, 0x8(%rsp)
movl $0x0, (%rax)
testq %r15, %r15
je 0xd60a
movl $0x1, %ebx
movq 0x24df1(%rip), %r13     # 0x323a8
movl $0x1, %r12d
movq (%r15), %r14
testq %r13, %r13
je 0xd81e
movq %r13, %rbp
movq (%rbp), %rdi
movq %r14, %rsi
callq 0x2170
testl %eax, %eax
je 0xd5ea
movq 0x10(%rbp), %rbp
testq %rbp, %rbp
jne 0xd5cc
jmp 0xd81b
movl 0x8(%rbp), %eax
movq 0x8(%rsp), %rcx
movl %eax, (%rcx,%r12,4)
testl %eax, %eax
js 0xd82a
movq 0x10(%r15), %r15
incq %r12
testq %r15, %r15
jne 0xd5bd
movl 0x24dbc(%rip), %esi     # 0x323cc
testl %esi, %esi
jne 0xd625
movq 0x24db5(%rip), %rax     # 0x323d0
cmpq $0x0, (%rax)
je 0xd765
incl %esi
leaq 0x1c43b(%rip), %rdi     # 0x29a69
xorl %eax, %eax
callq 0x2110
movslq 0x24d90(%rip), %rbx     # 0x323cc
leaq 0x1(%rbx), %rax
movl %eax, 0x24d86(%rip)     # 0x323cc
leaq 0x8(,%rbx,8), %rdi
callq 0x21d0
movq %rax, (%rsp)
testq %rbx, %rbx
js 0xd75e
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
movq 0x24d64(%rip), %rax     # 0x323d0
xorl %r12d, %r12d
movq (%rax,%r12,8), %rax
movl $0x0, %ebp
testq %rax, %rax
je 0xd68a
xorl %ebp, %ebp
incl %ebp
movq 0x10(%rax), %rax
testq %rax, %rax
jne 0xd67f
leaq 0x1(%r12), %rdx
leaq 0x1c405(%rip), %rdi     # 0x29a9b
movl %ebp, %esi
movq %rdx, 0x10(%rsp)
xorl %eax, %eax
callq 0x2110
testl %ebx, %ebx
cmovsl %ebp, %ebx
testl %ebx, %ebx
sete %al
movl %ebx, 0x18(%rsp)
cmpl %ebx, %ebp
sete %cl
orb %al, %cl
je 0xd836
movl %ebp, %edi
shlq $0x2, %rdi
callq 0x21d0
movq %rax, %r13
movq (%rsp), %rax
movq %r13, (%rax,%r12,8)
movq 0x24cf4(%rip), %rax     # 0x323d0
movq %rax, 0x50(%rsp)
movq (%rax,%r12,8), %r12
testq %r12, %r12
je 0xd73e
movq 0x24cb7(%rip), %r14     # 0x323a8
xorl %ebx, %ebx
movq (%r12), %rbp
testq %r14, %r14
je 0xd832
movq %r14, %r15
movq (%r15), %rdi
movq %rbp, %rsi
callq 0x2170
testl %eax, %eax
je 0xd720
movq 0x10(%r15), %r15
testq %r15, %r15
jne 0xd703
jmp 0xd808
movl 0x8(%r15), %eax
movl %eax, (%r13,%rbx,4)
testl %eax, %eax
js 0xd813
incq %rbx
movq 0x10(%r12), %r12
testq %r12, %r12
jne 0xd6f3
movslq 0x24c87(%rip), %rax     # 0x323cc
movq 0x10(%rsp), %r12
cmpq %rax, %r12
movl 0x18(%rsp), %ebx
movq 0x50(%rsp), %rax
jl 0xd66f
jmp 0xd77e
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
jmp 0xd77e
leaq 0x1c40b(%rip), %rdi     # 0x29b77
callq 0x2090
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
movq $0x0, (%rsp)
movq 0x28(%rsp), %rax
movq 0x20(%rsp), %rcx
movq %rcx, (%rax)
movq 0x30(%rsp), %rax
movq 0x8(%rsp), %rcx
movq %rcx, (%rax)
movl 0x24c22(%rip), %eax     # 0x323c0
movq 0x38(%rsp), %rcx
movl %eax, (%rcx)
movq 0x24bfc(%rip), %rax     # 0x323a8
movl 0x8(%rax), %ecx
incl %ecx
movq 0x40(%rsp), %rdx
movl %ecx, (%rdx)
movl 0x24c0e(%rip), %ecx     # 0x323cc
movq 0x98(%rsp), %rdx
movl %ecx, (%rdx)
movq 0xa0(%rsp), %rcx
movl %ebx, (%rcx)
movl 0x24bec(%rip), %ecx     # 0x323c4
movq 0x48(%rsp), %rdx
movl %ecx, (%rdx)
movq 0x90(%rsp), %rcx
movq %rax, (%rcx)
movq 0xa8(%rsp), %rax
movq (%rsp), %rcx
movq %rcx, (%rax)
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl %ebx, %eax
movl $0xffffffff, (%r13,%rax,4) # imm = 0xFFFFFFFF
movq %rbp, %rdi
callq 0x22d4
movl %r12d, %ebx
movq 0x8(%rsp), %rax
movl $0xffffffff, (%rax,%rbx,4) # imm = 0xFFFFFFFF
movq %r14, %rdi
callq 0x22b0
xorl %eax, %eax
jmp 0xd80a
leaq 0x1c2de(%rip), %rdi     # 0x29b1b
callq 0x2090
movl $0x1, %edi
callq 0x2250