// File: /workspace/github_c_makefile_star2_O1/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, 0x254ae(%rip)     # 0x323d0
xorl %eax, %eax
movq 0x254a5(%rip), %rcx     # 0x323d0
movq $0x0, (%rcx,%rax,8)
incq %rax
cmpq $0x20, %rax
jne 0xcf24
leaq 0x1fbaf(%rip), %rsi     # 0x2caf2
movq %rbx, %rdi
callq 0x2210
leaq 0x2549e(%rip), %rbx     # 0x323f0
movq %rax, (%rbx)
callq 0xd4a0
movq (%rbx), %rdi
callq 0x20e0
leaq 0x2543f(%rip), %rdi     # 0x323a8
leaq 0x1c4d0(%rip), %rsi     # 0x29440
callq 0xce30
movslq 0x25444(%rip), %rdi     # 0x323c0
shlq $0x3, %rdi
callq 0x21d0
movq %rax, %rbp
movq 0x25411(%rip), %rbx     # 0x323a0
testq %rbx, %rbx
je 0xd1da
xorl %r14d, %r14d
movl $0x18, %r12d
xorl %r13d, %r13d
movl $0x38, %edi
callq 0x21d0
movq %rax, %r15
movq %rax, (%rbp,%r13,8)
movl %r14d, (%rax)
movl %r14d, 0x18(%rax)
movq %rbx, (%rsp)
movq (%rbx), %rax
testq %rax, %rax
je 0xcfde
movq 0x8(%rax), %rcx
testq %rcx, %rcx
cmovneq %r12, %rcx
incl (%r15,%rcx)
movq 0x18(%rax), %rax
jmp 0xcfc4
movslq (%r15), %r12
leaq (,%r12,4), %rdi
callq 0x21d0
movq %rax, 0x8(%r15)
movq %r12, %rdi
callq 0x21d0
movq %rax, 0x10(%r15)
movslq 0x18(%r15), %r12
movq %r12, %rdi
callq 0x21d0
movq %rax, 0x30(%r15)
movq %r12, %rdi
callq 0x21d0
movq %rax, 0x28(%r15)
shlq $0x3, %r12
movq %r12, %rdi
callq 0x21d0
movq %rax, 0x20(%r15)
movq (%rbp,%r13,8), %rbx
cmpl $0x0, 0x18(%rbx)
jle 0xd05b
xorl %r15d, %r15d
movl $0x38, %edi
callq 0x21d0
movq 0x20(%rbx), %rcx
movq %rax, (%rcx,%r15,8)
incq %r15
movq (%rbp,%r13,8), %rbx
movslq 0x18(%rbx), %rax
cmpq %rax, %r15
jl 0xd038
movq (%rsp), %rax
movq (%rax), %r15
testq %r15, %r15
je 0xd1c0
xorl %r12d, %r12d
xorl %ebx, %ebx
cmpq $0x0, (%r15)
je 0xd167
cmpq $0x0, 0x8(%r15)
movb 0x10(%r15), %al
movq (%rbp,%r13,8), %rcx
je 0xd18c
movq 0x28(%rcx), %rcx
movslq %r12d, %r12
movb %al, (%rcx,%r12)
movq 0x8(%r15), %rsi
leaq 0x2530c(%rip), %rdi     # 0x323b0
callq 0xce30
movq (%rbp,%r13,8), %rcx
movq 0x30(%rcx), %rcx
movb %al, (%rcx,%r12)
movq (%rbp,%r13,8), %rax
movq 0x20(%rax), %rax
movq (%rax,%r12,8), %rax
movl $0x1, (%rax)
movl $0x0, 0x18(%rax)
movq %r14, 0x20(%rax)
movq (%rbp,%r13,8), %rax
movq 0x20(%rax), %rax
movq (%rax,%r12,8), %rax
movq %r14, 0x28(%rax)
movq (%rbp,%r13,8), %rax
movq 0x20(%rax), %rax
movq (%rax,%r12,8), %rax
movq %r14, 0x30(%rax)
movl $0x1, %edi
callq 0x21d0
movq (%rbp,%r13,8), %rcx
movq 0x20(%rcx), %rcx
movq (%rcx,%r12,8), %rcx
movq %rax, 0x10(%rcx)
movl $0x4, %edi
callq 0x21d0
movq (%rbp,%r13,8), %rcx
movq 0x20(%rcx), %rcx
movq (%rcx,%r12,8), %rcx
movq %rax, 0x8(%rcx)
movq (%rbp,%r13,8), %rax
movq 0x20(%rax), %rax
movq (%rax,%r12,8), %rax
movq 0x10(%rax), %rax
movb $0x1, (%rax)
movq (%r15), %rsi
leaq 0x2525e(%rip), %rdi     # 0x323a8
callq 0xce30
movq (%rbp,%r13,8), %rcx
movq 0x20(%rcx), %rcx
movq (%rcx,%r12,8), %rcx
movq 0x8(%rcx), %rcx
movl %eax, (%rcx)
incl %r12d
jmp 0xd1b3
movb 0x10(%r15), %al
movq (%rbp,%r13,8), %rcx
movq 0x10(%rcx), %rcx
movslq %ebx, %rdx
movb %al, (%rcx,%rdx)
movq (%rbp,%r13,8), %rax
movq 0x8(%rax), %rax
movl $0x0, (%rax,%rdx,4)
jmp 0xd1b1
movq 0x10(%rcx), %rcx
movslq %ebx, %rbx
movb %al, (%rcx,%rbx)
movq (%r15), %rsi
leaq 0x25208(%rip), %rdi     # 0x323a8
callq 0xce30
movq (%rbp,%r13,8), %rcx
movq 0x8(%rcx), %rcx
movl %eax, (%rcx,%rbx,4)
incl %ebx
movq 0x18(%r15), %r15
testq %r15, %r15
jne 0xd070
incq %r13
movq (%rsp), %rbx
movq 0x8(%rbx), %rbx
testq %rbx, %rbx
movl $0x18, %r12d
jne 0xcfa4
movslq 0x251e3(%rip), %rax     # 0x323c4
leaq 0x1(%rax), %rcx
movl %ecx, 0x251d9(%rip)     # 0x323c4
leaq 0x4(,%rax,4), %rdi
callq 0x21d0
movq 0x251b9(%rip), %rbx     # 0x323b8
movq %rax, 0x20(%rsp)
movl $0x0, (%rax)
testq %rbx, %rbx
movq %rbp, 0x10(%rsp)
je 0xd274
movl $0x1, %r15d
movq 0x25187(%rip), %r12     # 0x323a8
movq (%rbx), %r14
movl $0xffffffff, %r13d      # imm = 0xFFFFFFFF
testq %r12, %r12
je 0xd256
movq %r12, %rbp
movq (%rbp), %rdi
movq %r14, %rsi
callq 0x2170
testl %eax, %eax
je 0xd24d
movq 0x10(%rbp), %rbp
testq %rbp, %rbp
jne 0xd232
jmp 0xd251
movl 0x8(%rbp), %r13d
movq 0x10(%rsp), %rbp
movq 0x20(%rsp), %rax
movl %r13d, (%rax,%r15,4)
testl %r13d, %r13d
js 0xd47f
movq 0x10(%rbx), %rbx
incq %r15
testq %rbx, %rbx
jne 0xd221
movl 0x25152(%rip), %esi     # 0x323cc
testl %esi, %esi
jne 0xd28f
movq 0x2514b(%rip), %rax     # 0x323d0
cmpq $0x0, (%rax)
je 0xd3d7
incl %esi
leaq 0x1c771(%rip), %rdi     # 0x29a09
xorl %eax, %eax
callq 0x2110
movslq 0x25126(%rip), %rbx     # 0x323cc
leaq 0x1(%rbx), %rax
movl %eax, 0x2511c(%rip)     # 0x323cc
leaq 0x8(,%rbx,8), %rdi
callq 0x21d0
movq %rax, 0x8(%rsp)
testq %rbx, %rbx
js 0xd3d0
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
xorl %r14d, %r14d
movq 0x250f6(%rip), %rax     # 0x323d0
movq (%rax,%r14,8), %rax
movl $0x0, %r13d
testq %rax, %rax
je 0xd2f8
xorl %r13d, %r13d
incl %r13d
movq 0x10(%rax), %rax
testq %rax, %rax
jne 0xd2ec
leaq 0x1(%r14), %rdx
leaq 0x1c738(%rip), %rdi     # 0x29a3b
movl %r13d, %esi
movq %rdx, 0x50(%rsp)
xorl %eax, %eax
callq 0x2110
testl %ebx, %ebx
cmovsl %r13d, %ebx
testl %ebx, %ebx
sete %al
movl %ebx, 0x1c(%rsp)
cmpl %ebx, %r13d
sete %cl
orb %al, %cl
je 0xd487
movl %r13d, %edi
shlq $0x2, %rdi
callq 0x21d0
movq %rax, %rcx
movq 0x8(%rsp), %rax
movq %rcx, (%rsp)
movq %rcx, (%rax,%r14,8)
movq 0x2507e(%rip), %rax     # 0x323d0
movq (%rax,%r14,8), %r14
testq %r14, %r14
je 0xd3b0
movq 0x25046(%rip), %r13     # 0x323a8
xorl %ebx, %ebx
movq (%r14), %r15
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
testq %r13, %r13
je 0xd395
movq %r13, %r12
movq (%r12), %rdi
movq %r15, %rsi
callq 0x2170
testl %eax, %eax
je 0xd390
movq 0x10(%r12), %r12
testq %r12, %r12
jne 0xd374
jmp 0xd395
movl 0x8(%r12), %ebp
movq (%rsp), %rax
movl %ebp, (%rax,%rbx,4)
testl %ebp, %ebp
js 0xd477
incq %rbx
movq 0x10(%r14), %r14
testq %r14, %r14
jne 0xd364
movslq 0x25015(%rip), %rax     # 0x323cc
movq 0x50(%rsp), %r14
cmpq %rax, %r14
movq 0x10(%rsp), %rbp
movl 0x1c(%rsp), %ebx
jl 0xd2d3
jmp 0xd3f1
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
jmp 0xd3f1
leaq 0x1c739(%rip), %rdi     # 0x29b17
callq 0x2090
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
movq $0x0, 0x8(%rsp)
movq 0x28(%rsp), %rax
movq %rbp, (%rax)
movq 0x30(%rsp), %rax
movq 0x20(%rsp), %rcx
movq %rcx, (%rax)
movl 0x24fb4(%rip), %eax     # 0x323c0
movq 0x38(%rsp), %rcx
movl %eax, (%rcx)
movq 0x24f8e(%rip), %rax     # 0x323a8
movl 0x8(%rax), %ecx
incl %ecx
movq 0x40(%rsp), %rdx
movl %ecx, (%rdx)
movl 0x24fa0(%rip), %ecx     # 0x323cc
movq 0x98(%rsp), %rdx
movl %ecx, (%rdx)
movq 0xa0(%rsp), %rcx
movl %ebx, (%rcx)
movl 0x24f7e(%rip), %ecx     # 0x323c4
movq 0x48(%rsp), %rdx
movl %ecx, (%rdx)
movq 0x90(%rsp), %rcx
movq %rax, (%rcx)
movq 0xa8(%rsp), %rax
movq 0x8(%rsp), %rcx
movq %rcx, (%rax)
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r15, %rdi
callq 0x22d4
movq %r14, %rdi
callq 0x22b0
leaq 0x1c62d(%rip), %rdi     # 0x29abb
callq 0x2090
movl $0x1, %edi
callq 0x2250
nopl (%rax)