// File: jstaursky[P]hutch/src/Sleigh/slghsymbol.cc
// Function: DecisionNode::chooseOptimalField()
void DecisionNode::chooseOptimalField(void)

{
  double score = 0.0;
  
  int4 sbit,size;		// The current field
  bool context;
  double sc;

  int4 maxlength,numfixed,maxfixed;

  maxfixed = 1;
  context = true;
  do {
    maxlength = 8*getMaximumLength(context);
    for(sbit=0;sbit<maxlength;++sbit) {
      numfixed = getNumFixed(sbit,1,context); // How may patterns specify this bit
      if (numfixed < maxfixed) continue; // Skip this bit, if we don't have maximum specification
      sc = getScore(sbit,1,context);

 // if we got more patterns this time than previously, and a positive score, reset
 // the high score (we prefer this bit, because it has a higher numfixed, regardless
 // of the difference in score, as long as the new score is positive).
      if ((numfixed > maxfixed)&&(sc > 0.0)) {
	score = sc;
	maxfixed = numfixed;
	startbit = sbit;
	bitsize = 1;
	contextdecision = context;
	continue;
      }
				// We have maximum patterns
      if (sc > score) {
	score = sc;
	startbit = sbit;
	bitsize = 1;
	contextdecision = context;
      }
    }
    context = !context;
  } while(!context);

  context = true;
  do {
    maxlength = 8*getMaximumLength(context);
    for(size=2;size <= 8;++size) {
      for(sbit=0;sbit<maxlength-size+1;++sbit) {
	if (getNumFixed(sbit,size,context) < maxfixed) continue; // Consider only maximal fields
	sc = getScore(sbit,size,context);
	if (sc > score) {
	  score = sc;
	  startbit = sbit;
	  bitsize = size;
	  contextdecision = context;
	}
      }
    }
    context = !context;
  } while(!context);
  if (score <= 0.0)		// If we failed to get a positive score
    bitsize = 0;		// treat the node as terminal
}

// Assembly:
subq $0x48, %rsp
movq %rdi, 0x40(%rsp)
movq 0x40(%rsp), %rax
movq %rax, 0x8(%rsp)
xorps %xmm0, %xmm0
movsd %xmm0, 0x38(%rsp)
movl $0x1, 0x14(%rsp)
movb $0x1, 0x2f(%rsp)
movq 0x8(%rsp), %rdi
movb 0x2f(%rsp), %al
andb $0x1, %al
movzbl %al, %esi
callq 0xdd050
shll $0x3, %eax
movl %eax, 0x1c(%rsp)
movl $0x0, 0x34(%rsp)
movl 0x34(%rsp), %eax
cmpl 0x1c(%rsp), %eax
jge 0x14b6ef
movq 0x8(%rsp), %rdi
movl 0x34(%rsp), %esi
movb 0x2f(%rsp), %al
movl $0x1, %edx
andb $0x1, %al
movzbl %al, %ecx
callq 0xdd6e0
movl %eax, 0x18(%rsp)
movl 0x18(%rsp), %eax
cmpl 0x14(%rsp), %eax
jge 0x14b638
jmp 0x14b6df
movq 0x8(%rsp), %rdi
movl 0x34(%rsp), %esi
movb 0x2f(%rsp), %al
movl $0x1, %edx
andb $0x1, %al
movzbl %al, %ecx
callq 0xdc710
movsd %xmm0, 0x20(%rsp)
movl 0x18(%rsp), %eax
cmpl 0x14(%rsp), %eax
jle 0x14b6a6
movsd 0x20(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x14b6a6
movq 0x8(%rsp), %rax
movsd 0x20(%rsp), %xmm0
movsd %xmm0, 0x38(%rsp)
movl 0x18(%rsp), %ecx
movl %ecx, 0x14(%rsp)
movl 0x34(%rsp), %ecx
movl %ecx, 0x38(%rax)
movl $0x1, 0x3c(%rax)
movb 0x2f(%rsp), %cl
andb $0x1, %cl
movb %cl, 0x34(%rax)
jmp 0x14b6df
movsd 0x20(%rsp), %xmm0
ucomisd 0x38(%rsp), %xmm0
jbe 0x14b6dd
movq 0x8(%rsp), %rax
movsd 0x20(%rsp), %xmm0
movsd %xmm0, 0x38(%rsp)
movl 0x34(%rsp), %ecx
movl %ecx, 0x38(%rax)
movl $0x1, 0x3c(%rax)
movb 0x2f(%rsp), %cl
andb $0x1, %cl
movb %cl, 0x34(%rax)
jmp 0x14b6df
movl 0x34(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x34(%rsp)
jmp 0x14b5fb
movb 0x2f(%rsp), %al
xorb $-0x1, %al
andb $0x1, %al
movb %al, 0x2f(%rsp)
movb 0x2f(%rsp), %al
xorb $-0x1, %al
testb $0x1, %al
jne 0x14b5d9
movb $0x1, 0x2f(%rsp)
movq 0x8(%rsp), %rdi
movb 0x2f(%rsp), %al
andb $0x1, %al
movzbl %al, %esi
callq 0xdd050
shll $0x3, %eax
movl %eax, 0x1c(%rsp)
movl $0x2, 0x30(%rsp)
cmpl $0x8, 0x30(%rsp)
jg 0x14b7f9
movl $0x0, 0x34(%rsp)
movl 0x34(%rsp), %eax
movl 0x1c(%rsp), %ecx
subl 0x30(%rsp), %ecx
addl $0x1, %ecx
cmpl %ecx, %eax
jge 0x14b7e7
movq 0x8(%rsp), %rdi
movl 0x34(%rsp), %esi
movl 0x30(%rsp), %edx
movb 0x2f(%rsp), %al
andb $0x1, %al
movzbl %al, %ecx
callq 0xdd6e0
cmpl 0x14(%rsp), %eax
jge 0x14b77d
jmp 0x14b7d7
movq 0x8(%rsp), %rdi
movl 0x34(%rsp), %esi
movl 0x30(%rsp), %edx
movb 0x2f(%rsp), %al
andb $0x1, %al
movzbl %al, %ecx
callq 0xdc710
movsd %xmm0, 0x20(%rsp)
movsd 0x20(%rsp), %xmm0
ucomisd 0x38(%rsp), %xmm0
jbe 0x14b7d5
movq 0x8(%rsp), %rax
movsd 0x20(%rsp), %xmm0
movsd %xmm0, 0x38(%rsp)
movl 0x34(%rsp), %ecx
movl %ecx, 0x38(%rax)
movl 0x30(%rsp), %ecx
movl %ecx, 0x3c(%rax)
movb 0x2f(%rsp), %cl
andb $0x1, %cl
movb %cl, 0x34(%rax)
jmp 0x14b7d7
movl 0x34(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x34(%rsp)
jmp 0x14b743
jmp 0x14b7e9
movl 0x30(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x30(%rsp)
jmp 0x14b730
movb 0x2f(%rsp), %al
xorb $-0x1, %al
andb $0x1, %al
movb %al, 0x2f(%rsp)
movb 0x2f(%rsp), %al
xorb $-0x1, %al
testb $0x1, %al
jne 0x14b70e
xorps %xmm0, %xmm0
ucomisd 0x38(%rsp), %xmm0
jb 0x14b82a
movq 0x8(%rsp), %rax
movl $0x0, 0x3c(%rax)
addq $0x48, %rsp
retq
nop