// File: jstaursky[P]hutch/src/Sleigh/slghpattern.cc
// Function: PatternBlock::normalize()
void PatternBlock::normalize(void)

{
  if (nonzerosize<=0) {		// Check if alwaystrue or alwaysfalse
    offset = 0;			// in which case we don't need mask and value
    maskvec.clear();
    valvec.clear();
    return;
  }
  vector<uintm>::iterator iter1,iter2;
  
  iter1 = maskvec.begin();	// Cut zeros from beginning of mask
  iter2 = valvec.begin();
  while((iter1 != maskvec.end())&&((*iter1)==0)) {
    iter1++;
    iter2++;
    offset += sizeof(uintm);
  }
  maskvec.erase(maskvec.begin(),iter1);
  valvec.erase(valvec.begin(),iter2);

  if (!maskvec.empty()) {
    int4 suboff = 0;		// Cut off unaligned zeros from beginning of mask
    uintm tmp = maskvec[0];
    while(tmp != 0) {
      suboff += 1;
      tmp >>= 8;
    }
    suboff = sizeof(uintm)-suboff;
    if (suboff != 0) {
      offset += suboff;		// Slide up maskvec by suboff bytes
      for(int4 i=0;i<maskvec.size()-1;++i) {
	tmp = maskvec[i] << (suboff*8);
	tmp |= (maskvec[i+1] >> ((sizeof(uintm)-suboff)*8));
	maskvec[i] = tmp;
      }
      maskvec.back() <<= suboff*8;
      for(int4 i=0;i<valvec.size()-1;++i) { // Slide up valvec by suboff bytes
	tmp = valvec[i] << (suboff*8);
	tmp |= (valvec[i+1] >> ((sizeof(uintm)-suboff)*8));
	valvec[i] = tmp;
      }
      valvec.back() <<= suboff*8;
    }
    
    iter1 = maskvec.end();	// Cut zeros from end of mask
    iter2 = valvec.end();
    while(iter1 != maskvec.begin()) {
      --iter1;
      --iter2;
      if ((*iter1) != 0) break; // Find last non-zero
    }
    if (iter1 != maskvec.end()) {
      iter1++;			// Find first zero, in last zero chain
      iter2++;
    }
    maskvec.erase(iter1,maskvec.end());
    valvec.erase(iter2,valvec.end());
  }

  if (maskvec.empty()) {
    offset = 0;
    nonzerosize = 0;		// Always true
    return;
  }
  nonzerosize = maskvec.size() * sizeof(uintm);
  uintm tmp = maskvec.back();	// tmp must be nonzero
  while( (tmp&0xff) == 0) {
    nonzerosize -= 1;
    tmp >>= 8;
  }
}

// Assembly:
subq $0x158, %rsp            # imm = 0x158
movq %rdi, 0x150(%rsp)
movq 0x150(%rsp), %rax
movq %rax, 0x48(%rsp)
cmpl $0x0, 0x4(%rax)
jg 0x135ad9
movq 0x48(%rsp), %rdi
movl $0x0, (%rdi)
addq $0x8, %rdi
callq 0xd7010
movq 0x48(%rsp), %rdi
addq $0x20, %rdi
callq 0xd7010
jmp 0x1361c0
leaq 0x148(%rsp), %rdi
callq 0xe2fb0
leaq 0x140(%rsp), %rdi
callq 0xe2fb0
movq 0x48(%rsp), %rdi
addq $0x8, %rdi
callq 0xd5b90
movq 0x48(%rsp), %rdi
movq %rax, 0x138(%rsp)
movq 0x138(%rsp), %rax
movq %rax, 0x148(%rsp)
addq $0x20, %rdi
callq 0xd5b90
movq %rax, 0x130(%rsp)
movq 0x130(%rsp), %rax
movq %rax, 0x140(%rsp)
movq 0x48(%rsp), %rdi
addq $0x8, %rdi
callq 0xcf2b0
movq %rax, 0x128(%rsp)
leaq 0x148(%rsp), %rdi
leaq 0x128(%rsp), %rsi
callq 0xe0b60
movb %al, %cl
xorl %eax, %eax
testb $0x1, %cl
movb %al, 0x47(%rsp)
jne 0x135b79
jmp 0x135b90
leaq 0x148(%rsp), %rdi
callq 0xda520
cmpl $0x0, (%rax)
sete %al
movb %al, 0x47(%rsp)
movb 0x47(%rsp), %al
testb $0x1, %al
jne 0x135b9a
jmp 0x135bde
leaq 0x148(%rsp), %rdi
xorl %esi, %esi
callq 0xd44d0
movq %rax, 0x120(%rsp)
leaq 0x140(%rsp), %rdi
xorl %esi, %esi
callq 0xd44d0
movq %rax, %rcx
movq 0x48(%rsp), %rax
movq %rcx, 0x118(%rsp)
movslq (%rax), %rcx
addq $0x4, %rcx
movl %ecx, (%rax)
jmp 0x135b3f
movq 0x48(%rsp), %rdi
movq %rdi, %rax
addq $0x8, %rax
movq %rax, 0x30(%rsp)
addq $0x8, %rdi
callq 0xd5b90
movq %rax, 0x108(%rsp)
leaq 0x110(%rsp), %rdi
leaq 0x108(%rsp), %rsi
callq 0xce410
leaq 0x100(%rsp), %rdi
leaq 0x148(%rsp), %rsi
callq 0xce410
movq 0x30(%rsp), %rdi
movq 0x110(%rsp), %rsi
movq 0x100(%rsp), %rdx
callq 0xcf230
movq 0x48(%rsp), %rdi
movq %rax, 0xf8(%rsp)
movq %rdi, %rax
addq $0x20, %rax
movq %rax, 0x38(%rsp)
addq $0x20, %rdi
callq 0xd5b90
movq %rax, 0xe8(%rsp)
leaq 0xf0(%rsp), %rdi
leaq 0xe8(%rsp), %rsi
callq 0xce410
leaq 0xe0(%rsp), %rdi
leaq 0x140(%rsp), %rsi
callq 0xce410
movq 0x38(%rsp), %rdi
movq 0xf0(%rsp), %rsi
movq 0xe0(%rsp), %rdx
callq 0xcf230
movq 0x48(%rsp), %rdi
movq %rax, 0xd8(%rsp)
addq $0x8, %rdi
callq 0xd6270
testb $0x1, %al
jne 0x136146
movq 0x48(%rsp), %rdi
movl $0x0, 0xd4(%rsp)
addq $0x8, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0xdd2d0
movl (%rax), %eax
movl %eax, 0xd0(%rsp)
cmpl $0x0, 0xd0(%rsp)
je 0x135d24
movl 0xd4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xd4(%rsp)
movl 0xd0(%rsp), %eax
shrl $0x8, %eax
movl %eax, 0xd0(%rsp)
jmp 0x135cf6
movslq 0xd4(%rsp), %rcx
movl $0x4, %eax
subq %rcx, %rax
movl %eax, 0xd4(%rsp)
cmpl $0x0, 0xd4(%rsp)
je 0x135f71
movq 0x48(%rsp), %rax
movl 0xd4(%rsp), %ecx
addl (%rax), %ecx
movl %ecx, (%rax)
movl $0x0, 0xcc(%rsp)
movq 0x48(%rsp), %rdi
movslq 0xcc(%rsp), %rax
movq %rax, 0x28(%rsp)
addq $0x8, %rdi
callq 0xcec30
movq %rax, %rcx
movq 0x28(%rsp), %rax
subq $0x1, %rcx
cmpq %rcx, %rax
jae 0x135e3d
movq 0x48(%rsp), %rdi
addq $0x8, %rdi
movslq 0xcc(%rsp), %rsi
callq 0xdd2d0
movq 0x48(%rsp), %rdi
movl (%rax), %eax
movl 0xd4(%rsp), %ecx
shll $0x3, %ecx
shll %cl, %eax
movl %eax, 0xd0(%rsp)
addq $0x8, %rdi
movl 0xcc(%rsp), %eax
addl $0x1, %eax
movslq %eax, %rsi
callq 0xdd2d0
movq 0x48(%rsp), %rdi
movl (%rax), %eax
movslq 0xd4(%rsp), %rdx
movl $0x4, %ecx
subq %rdx, %rcx
shlq $0x3, %rcx
shrl %cl, %eax
orl 0xd0(%rsp), %eax
movl %eax, 0xd0(%rsp)
movl 0xd0(%rsp), %eax
movl %eax, 0x24(%rsp)
addq $0x8, %rdi
movslq 0xcc(%rsp), %rsi
callq 0xdd2d0
movl 0x24(%rsp), %ecx
movl %ecx, (%rax)
movl 0xcc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xcc(%rsp)
jmp 0x135d64
movq 0x48(%rsp), %rdi
movl 0xd4(%rsp), %eax
shll $0x3, %eax
movl %eax, 0x20(%rsp)
addq $0x8, %rdi
callq 0xd33d0
movl 0x20(%rsp), %ecx
movl (%rax), %edx
shll %cl, %edx
movl %edx, %ecx
movl %ecx, (%rax)
movl $0x0, 0xc8(%rsp)
movq 0x48(%rsp), %rdi
movslq 0xc8(%rsp), %rax
movq %rax, 0x18(%rsp)
addq $0x20, %rdi
callq 0xcec30
movq %rax, %rcx
movq 0x18(%rsp), %rax
subq $0x1, %rcx
cmpq %rcx, %rax
jae 0x135f49
movq 0x48(%rsp), %rdi
addq $0x20, %rdi
movslq 0xc8(%rsp), %rsi
callq 0xdd2d0
movq 0x48(%rsp), %rdi
movl (%rax), %eax
movl 0xd4(%rsp), %ecx
shll $0x3, %ecx
shll %cl, %eax
movl %eax, 0xd0(%rsp)
addq $0x20, %rdi
movl 0xc8(%rsp), %eax
addl $0x1, %eax
movslq %eax, %rsi
callq 0xdd2d0
movq 0x48(%rsp), %rdi
movl (%rax), %eax
movslq 0xd4(%rsp), %rdx
movl $0x4, %ecx
subq %rdx, %rcx
shlq $0x3, %rcx
shrl %cl, %eax
orl 0xd0(%rsp), %eax
movl %eax, 0xd0(%rsp)
movl 0xd0(%rsp), %eax
movl %eax, 0x14(%rsp)
addq $0x20, %rdi
movslq 0xc8(%rsp), %rsi
callq 0xdd2d0
movl 0x14(%rsp), %ecx
movl %ecx, (%rax)
movl 0xc8(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xc8(%rsp)
jmp 0x135e70
movq 0x48(%rsp), %rdi
movl 0xd4(%rsp), %eax
shll $0x3, %eax
movl %eax, 0x10(%rsp)
addq $0x20, %rdi
callq 0xd33d0
movl 0x10(%rsp), %ecx
movl (%rax), %edx
shll %cl, %edx
movl %edx, %ecx
movl %ecx, (%rax)
movq 0x48(%rsp), %rdi
addq $0x8, %rdi
callq 0xcf2b0
movq 0x48(%rsp), %rdi
movq %rax, 0xc0(%rsp)
movq 0xc0(%rsp), %rax
movq %rax, 0x148(%rsp)
addq $0x20, %rdi
callq 0xcf2b0
movq %rax, 0xb8(%rsp)
movq 0xb8(%rsp), %rax
movq %rax, 0x140(%rsp)
movq 0x48(%rsp), %rdi
addq $0x8, %rdi
callq 0xd5b90
movq %rax, 0xb0(%rsp)
leaq 0x148(%rsp), %rdi
leaq 0xb0(%rsp), %rsi
callq 0xe0b60
testb $0x1, %al
jne 0x135fee
jmp 0x13601e
leaq 0x148(%rsp), %rdi
callq 0xd2e60
leaq 0x140(%rsp), %rdi
callq 0xd2e60
leaq 0x148(%rsp), %rdi
callq 0xda520
cmpl $0x0, (%rax)
je 0x13601c
jmp 0x13601e
jmp 0x135fbd
movq 0x48(%rsp), %rdi
addq $0x8, %rdi
callq 0xcf2b0
movq %rax, 0xa8(%rsp)
leaq 0x148(%rsp), %rdi
leaq 0xa8(%rsp), %rsi
callq 0xe0b60
testb $0x1, %al
jne 0x13604f
jmp 0x13607d
leaq 0x148(%rsp), %rdi
xorl %esi, %esi
callq 0xd44d0
movq %rax, 0xa0(%rsp)
leaq 0x140(%rsp), %rdi
xorl %esi, %esi
callq 0xd44d0
movq %rax, 0x98(%rsp)
movq 0x48(%rsp), %rax
addq $0x8, %rax
movq %rax, (%rsp)
leaq 0x90(%rsp), %rdi
leaq 0x148(%rsp), %rsi
callq 0xce410
movq 0x48(%rsp), %rdi
addq $0x8, %rdi
callq 0xcf2b0
movq %rax, 0x80(%rsp)
leaq 0x88(%rsp), %rdi
leaq 0x80(%rsp), %rsi
callq 0xce410
movq (%rsp), %rdi
movq 0x90(%rsp), %rsi
movq 0x88(%rsp), %rdx
callq 0xcf230
movq %rax, %rcx
movq 0x48(%rsp), %rax
movq %rcx, 0x78(%rsp)
addq $0x20, %rax
movq %rax, 0x8(%rsp)
leaq 0x70(%rsp), %rdi
leaq 0x140(%rsp), %rsi
callq 0xce410
movq 0x48(%rsp), %rdi
addq $0x20, %rdi
callq 0xcf2b0
movq %rax, 0x60(%rsp)
leaq 0x68(%rsp), %rdi
leaq 0x60(%rsp), %rsi
callq 0xce410
movq 0x8(%rsp), %rdi
movq 0x70(%rsp), %rsi
movq 0x68(%rsp), %rdx
callq 0xcf230
movq %rax, 0x58(%rsp)
movq 0x48(%rsp), %rdi
addq $0x8, %rdi
callq 0xd6270
testb $0x1, %al
jne 0x13615a
jmp 0x13616e
movq 0x48(%rsp), %rax
movl $0x0, (%rax)
movl $0x0, 0x4(%rax)
jmp 0x1361c0
movq 0x48(%rsp), %rdi
addq $0x8, %rdi
callq 0xcec30
movq 0x48(%rsp), %rdi
shlq $0x2, %rax
movl %eax, 0x4(%rdi)
addq $0x8, %rdi
callq 0xd33d0
movl (%rax), %eax
movl %eax, 0x54(%rsp)
movl 0x54(%rsp), %eax
andl $0xff, %eax
cmpl $0x0, %eax
jne 0x1361c0
movq 0x48(%rsp), %rax
movl 0x4(%rax), %ecx
subl $0x1, %ecx
movl %ecx, 0x4(%rax)
movl 0x54(%rsp), %eax
shrl $0x8, %eax
movl %eax, 0x54(%rsp)
jmp 0x136197
addq $0x158, %rsp            # imm = 0x158
retq
nopl (%rax,%rax)