// File: /workspace/github_cpp_makefile_star2_O1/twekkel[P]mzpq/libzpaq.cpp
// Function: libzpaq::Predictor::predict0()
int Predictor::predict0() {
  assert(initTables);
  assert(c8>=1 && c8<=255);

  // Predict next bit
  int n=z.header[6];
  assert(n>0 && n<=255);
  const U8* cp=&z.header[7];
  assert(cp[-1]==n);
  for (int i=0; i<n; ++i) {
    assert(cp>&z.header[0] && cp<&z.header[z.header.isize()-8]);
    Component& cr=comp[i];
    switch(cp[0]) {
      case CONS:  // c
        break;
      case CM:  // sizebits limit
        cr.cxt=h[i]^hmap4;
        p[i]=stretch(cr.cm(cr.cxt)>>17);
        break;
      case ICM: // sizebits
        assert((hmap4&15)>0);
        if (c8==1 || (c8&0xf0)==16) cr.c=find(cr.ht, cp[1]+2, h[i]+16*c8);
        cr.cxt=cr.ht[cr.c+(hmap4&15)];
        p[i]=stretch(cr.cm(cr.cxt)>>8);
        break;
      case MATCH: // sizebits bufbits: a=len, b=offset, c=bit, cxt=bitpos,
                  //                   ht=buf, limit=pos
        assert(cr.cm.size()==(size_t(1)<<cp[1]));
        assert(cr.ht.size()==(size_t(1)<<cp[2]));
        assert(cr.a<=255);
        assert(cr.c==0 || cr.c==1);
        assert(cr.cxt<8);
        assert(cr.limit<cr.ht.size());
        if (cr.a==0) p[i]=0;
        else {
          cr.c=(cr.ht(cr.limit-cr.b)>>(7-cr.cxt))&1; // predicted bit
          p[i]=stretch(dt2k[cr.a]*(cr.c*-2+1)&32767);
        }
        break;
      case AVG: // j k wt
        p[i]=(p[cp[1]]*cp[3]+p[cp[2]]*(256-cp[3]))>>8;
        break;
      case MIX2: { // sizebits j k rate mask
                   // c=size cm=wt[size] cxt=input
        cr.cxt=((h[i]+(c8&cp[5]))&(cr.c-1));
        assert(cr.cxt<cr.a16.size());
        int w=cr.a16[cr.cxt];
        assert(w>=0 && w<65536);
        p[i]=(w*p[cp[2]]+(65536-w)*p[cp[3]])>>16;
        assert(p[i]>=-2048 && p[i]<2048);
      }
        break;
      case MIX: {  // sizebits j m rate mask
                   // c=size cm=wt[size][m] cxt=index of wt in cm
        int m=cp[3];
        assert(m>=1 && m<=i);
        cr.cxt=h[i]+(c8&cp[5]);
        cr.cxt=(cr.cxt&(cr.c-1))*m; // pointer to row of weights
        assert(cr.cxt<=cr.cm.size()-m);
        int* wt=(int*)&cr.cm[cr.cxt];
        p[i]=0;
        for (int j=0; j<m; ++j)
          p[i]+=(wt[j]>>8)*p[cp[2]+j];
        p[i]=clamp2k(p[i]>>8);
      }
        break;
      case ISSE: { // sizebits j -- c=hi, cxt=bh
        assert((hmap4&15)>0);
        if (c8==1 || (c8&0xf0)==16)
          cr.c=find(cr.ht, cp[1]+2, h[i]+16*c8);
        cr.cxt=cr.ht[cr.c+(hmap4&15)];  // bit history
        int *wt=(int*)&cr.cm[cr.cxt*2];
        p[i]=clamp2k((wt[0]*p[cp[2]]+wt[1]*64)>>16);
      }
        break;
      case SSE: { // sizebits j start limit
        cr.cxt=(h[i]+c8)*32;
        int pq=p[cp[2]]+992;
        if (pq<0) pq=0;
        if (pq>1983) pq=1983;
        int wt=pq&63;
        pq>>=6;
        assert(pq>=0 && pq<=30);
        cr.cxt+=pq;
        p[i]=stretch(((cr.cm(cr.cxt)>>10)*(64-wt)+(cr.cm(cr.cxt+1)>>10)*wt)>>13);
        cr.cxt+=wt>>5;
      }
        break;
      default:
        error("component predict not implemented");
    }
    cp+=compsize[cp[0]];
    assert(cp<&z.header[z.cend]);
    assert(p[i]>=-2048 && p[i]<2048);
  }
  assert(cp[0]==NONE);
  return squash(p[n-1]);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rdi, %rbx
movq 0x808(%rdi), %rax
movq 0x10(%rax), %r12
movzbl 0x6(%r12), %r15d
testq %r15, %r15
je 0xbc59
addq $0x7, %r12
leaq 0x810(%rbx), %rbp
xorl %r14d, %r14d
leaq 0x4022b(%rip), %r10     # 0x4bac0
leaq 0x3fee8(%rip), %r11     # 0x4b784
movzbl (%r12), %eax
decl %eax
cmpl $0x8, %eax
ja 0xbc2c
imulq $0x70, %r14, %r13
addq %rbp, %r13
movslq (%r11,%rax,4), %rax
addq %r11, %rax
jmpq *%rax
movl 0x4(%rbx), %eax
xorl 0x408(%rbx,%r14,4), %eax
movq %rax, 0x8(%r13)
movq 0x28(%r13), %rcx
movl 0x30(%r13), %edx
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
addl %esi, %edx
andl %eax, %edx
movzwl 0x2(%rcx,%rdx,4), %eax
andl $-0x2, %eax
movswl 0xac14(%rbx,%rax), %eax
jmp 0xbb64
movzbl 0x1(%r12), %eax
movzbl 0x3(%r12), %ecx
movl 0x8(%rbx,%rax,4), %eax
movzbl 0x2(%r12), %edx
movl $0x100, %esi            # imm = 0x100
subl %ecx, %esi
imull 0x8(%rbx,%rdx,4), %esi
imull %ecx, %eax
addl %eax, %esi
sarl $0x8, %esi
movl %esi, 0x8(%rbx,%r14,4)
jmp 0xbb69
movl (%rbx), %ecx
cmpl $0x1, %ecx
setne %al
movl %ecx, %edx
andl $0xf0, %edx
cmpl $0x10, %edx
setne %dl
testb %dl, %al
jne 0xb96e
leaq 0x40(%r13), %rsi
movzbl 0x1(%r12), %edx
addl $0x2, %edx
shll $0x4, %ecx
addl 0x408(%rbx,%r14,4), %ecx
callq 0xbc76
leaq 0x3fe21(%rip), %r11     # 0x4b784
leaq 0x40156(%rip), %r10     # 0x4bac0
movq %rax, 0x20(%r13)
movl 0x4(%rbx), %eax
andl $0xf, %eax
movq 0x40(%r13), %rcx
addq 0x20(%r13), %rcx
movzbl (%rax,%rcx), %eax
movq %rax, 0x8(%r13)
movq 0x28(%r13), %rcx
movl 0x30(%r13), %edx
decl %edx
andl %eax, %edx
movl (%rcx,%rdx,4), %eax
shrl $0x8, %eax
movswl 0xac14(%rbx,%rax,2), %eax
jmp 0xbb64
movq 0x10(%r13), %rax
testq %rax, %rax
je 0xbc4b
movq (%r13), %rcx
movq 0x40(%r13), %rdx
subq 0x18(%r13), %rcx
movq 0x48(%r13), %rsi
decq %rsi
andq %rcx, %rsi
movb $0x7, %cl
subb 0x8(%r13), %cl
movzbl (%rdx,%rsi), %edx
movzbl %cl, %ecx
xorl %esi, %esi
btl %ecx, %edx
setb %sil
movq %rsi, 0x20(%r13)
leal (%rsi,%rsi), %ecx
shll $0xf, %esi
subl %ecx, %esi
orl $0x1, %esi
imull 0x7814(%rbx,%rax,4), %esi
andl $0x7fff, %esi           # imm = 0x7FFF
movswl 0xac14(%rbx,%rsi,2), %eax
jmp 0xbb64
movl (%rbx), %ecx
cmpl $0x1, %ecx
setne %al
movl %ecx, %edx
andl $0xf0, %edx
cmpl $0x10, %edx
setne %dl
testb %dl, %al
jne 0xba4f
leaq 0x40(%r13), %rsi
movzbl 0x1(%r12), %edx
addl $0x2, %edx
shll $0x4, %ecx
addl 0x408(%rbx,%r14,4), %ecx
callq 0xbc76
leaq 0x3fd40(%rip), %r11     # 0x4b784
leaq 0x40075(%rip), %r10     # 0x4bac0
movq %rax, 0x20(%r13)
movl 0x4(%rbx), %eax
andl $0xf, %eax
movq 0x28(%r13), %rcx
movq 0x40(%r13), %rdx
addq 0x20(%r13), %rdx
movzbl (%rax,%rdx), %eax
movq %rax, 0x8(%r13)
movzbl 0x2(%r12), %edx
movl 0x8(%rbx,%rdx,4), %edx
imull (%rcx,%rax,8), %edx
movl 0x4(%rcx,%rax,8), %eax
shll $0x6, %eax
addl %edx, %eax
sarl $0x10, %eax
jmp 0xbb4d
movzbl 0x5(%r12), %eax
andl (%rbx), %eax
addl 0x408(%rbx,%r14,4), %eax
movl 0x20(%r13), %ecx
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
addl %edx, %ecx
andl %eax, %ecx
movq %rcx, 0x8(%r13)
movq 0x58(%r13), %rax
movzwl (%rax,%rcx,2), %eax
movzbl 0x2(%r12), %ecx
movl $0x10000, %edx          # imm = 0x10000
subl %eax, %edx
imull 0x8(%rbx,%rcx,4), %eax
movzbl 0x3(%r12), %ecx
imull 0x8(%rbx,%rcx,4), %edx
addl %eax, %edx
sarl $0x10, %edx
movl %edx, 0x8(%rbx,%r14,4)
jmp 0xbb69
movzbl 0x5(%r12), %ecx
andl (%rbx), %ecx
addl 0x408(%rbx,%r14,4), %ecx
movzbl 0x3(%r12), %eax
movl 0x20(%r13), %edx
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
addl %esi, %edx
andl %ecx, %edx
imulq %rax, %rdx
leaq (,%rdx,4), %rcx
addq 0x28(%r13), %rcx
movq %rdx, 0x8(%r13)
movl $0x0, 0x8(%rbx,%r14,4)
testq %rax, %rax
je 0xbb45
xorl %edx, %edx
movl (%rcx,%rdx,4), %esi
sarl $0x8, %esi
movzbl 0x2(%r12), %edi
addq %rdx, %rdi
imull 0x8(%rbx,%rdi,4), %esi
addl %esi, 0x8(%rbx,%r14,4)
incq %rdx
cmpq %rdx, %rax
jne 0xbb24
movl 0x8(%rbx,%r14,4), %eax
sarl $0x8, %eax
movl $0x7ff, %ecx            # imm = 0x7FF
cmpl %ecx, %eax
cmovgel %ecx, %eax
cmpl $0xfffff801, %eax       # imm = 0xFFFFF801
movl $0xfffff800, %ecx       # imm = 0xFFFFF800
cmovll %ecx, %eax
movl %eax, 0x8(%rbx,%r14,4)
movzbl (%r12), %eax
movslq (%r10,%rax,4), %rax
addq %rax, %r12
incq %r14
cmpq %r15, %r14
jne 0xb89c
jmp 0xbc59
movl (%rbx), %ecx
addl 0x408(%rbx,%r14,4), %ecx
shll $0x5, %ecx
movq %rcx, 0x8(%r13)
movzbl 0x2(%r12), %eax
movl 0x8(%rbx,%rax,4), %r8d
cmpl $0xfffffc21, %r8d       # imm = 0xFFFFFC21
movl $0xfffffc20, %eax       # imm = 0xFFFFFC20
cmovll %eax, %r8d
movl $0x3df, %eax            # imm = 0x3DF
cmpl %eax, %r8d
cmovgel %eax, %r8d
addl $0x3e0, %r8d            # imm = 0x3E0
movl %r8d, %eax
andl $0x3f, %eax
shrl $0x6, %r8d
leaq (%r8,%rcx), %rdx
movq %rdx, 0x8(%r13)
movq 0x28(%r13), %rsi
movq 0x30(%r13), %rdi
decq %rdi
movl $0x40, %r9d
subl %eax, %r9d
addq %r8, %rcx
incq %rcx
andq %rdi, %rcx
movl (%rsi,%rcx,4), %ecx
shrl $0xa, %ecx
imull %eax, %ecx
shrl $0x5, %eax
addq %rdx, %rax
andq %rdi, %rdx
movl (%rsi,%rdx,4), %edx
shrl $0xa, %edx
imull %edx, %r9d
addl %r9d, %ecx
shrl $0xd, %ecx
movswl 0xac14(%rbx,%rcx,2), %ecx
movl %ecx, 0x8(%rbx,%r14,4)
movq %rax, 0x8(%r13)
jmp 0xbb69
leaq 0x4335b(%rip), %rdi     # 0x4ef8e
callq 0x455c
leaq 0x3fb45(%rip), %r11     # 0x4b784
leaq 0x3fe7a(%rip), %r10     # 0x4bac0
jmp 0xbb69
movl $0x0, 0x8(%rbx,%r14,4)
jmp 0xbb69
movslq 0x4(%rbx,%r15,4), %rax
movzwl 0x9c14(%rbx,%rax,2), %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop