// File: /workspace/github_c_makefile_star2_O3/thegenemyers[P]DASCRUBBER/QV.c
// Function: Encode
static void Encode(HScheme *scheme, FILE *out, uint8 *read, int rlen)
{ uint32  x, c, ocode;
  int     n, k, olen, llen;
  int    *nlens;
  uint32 *nbits;
  uint32  nspec;
  int     nslen;

  nlens = scheme->codelens;
  nbits = scheme->codebits;

  if (scheme->type == 2)
    { nspec = nbits[255];
      nslen = nlens[255];
    }
  else
    nspec = nslen = 0x7fffffff;

#define OCODE(L,C)				\
{ int    len  = olen + (L);			\
  uint32 code = (C);				\
						\
  llen = olen;					\
  if (len >= 32)				\
    { olen   = len-32;				\
      ocode |= (code >> olen);			\
      fwrite(&ocode,sizeof(uint32),1,out);	\
      if (olen > 0)				\
        ocode = (code << (32-olen));		\
      else					\
        ocode = 0;				\
    } 						\
  else						\
    { olen   = len;				\
      ocode |= (code << (32-olen));;		\
    }						\
}

  llen  = 0;
  olen  = 0;
  ocode = 0;
  for (k = 0; k < rlen; k++)
    { x = read[k];
      n = nlens[x];
      c = nbits[x];
      OCODE(n,c);
      if (c == nspec && n == nslen)
        OCODE(8,x);
    }

  if (olen > 0)                              //  Tricky: must pad so decoder does not read past
    { fwrite(&ocode,sizeof(uint32),1,out);   //    last integer int the coded output.
      if (llen > 16 && olen > llen)
        fwrite(&ocode,sizeof(uint32),1,out);
    }
  else if (llen > 16)
    fwrite(&ocode,sizeof(uint32),1,out);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rdx, 0x30(%rsp)
movq %rsi, 0x18(%rsp)
movl $0x7fffffff, 0x14(%rsp) # imm = 0x7FFFFFFF
movq %rdi, 0x20(%rsp)
cmpl $0x2, (%rdi)
movl $0x7fffffff, 0x10(%rsp) # imm = 0x7FFFFFFF
jne 0x9283
movq 0x20(%rsp), %rax
movl 0x400(%rax), %edx
movl %edx, 0x10(%rsp)
movl 0x800(%rax), %eax
movl %eax, 0x14(%rsp)
testl %ecx, %ecx
jle 0x940f
movl %ecx, %eax
movq %rax, 0x28(%rsp)
xorl %eax, %eax
xorl %r12d, %r12d
xorl %r13d, %r13d
movq 0x30(%rsp), %rcx
movzbl (%rcx,%r12), %ebx
movq 0x20(%rsp), %rcx
movl 0x404(%rcx,%rbx,4), %r14d
movl 0x4(%rcx,%rbx,4), %r15d
leal (%r14,%r13), %ebp
cmpl $0x20, %ebp
jl 0x931a
movl %r15d, %edx
movl %ebp, %ecx
shrl %cl, %edx
orl %edx, %eax
movl %eax, 0xc(%rsp)
movl $0x4, %esi
movl $0x1, %edx
leaq 0xc(%rsp), %rdi
movq 0x18(%rsp), %rcx
callq 0x1280
cmpl $0x20, %ebp
jne 0x9327
movl $0x0, 0xc(%rsp)
xorl 0x10(%rsp), %r15d
xorl 0x14(%rsp), %r14d
xorl %eax, %eax
orl %r15d, %r14d
movl $0x0, %ebp
movl %r13d, %r14d
movl $0x0, %r13d
je 0x939a
jmp 0x93b9
movl %ebp, %ecx
negb %cl
movl %r15d, %edx
shll %cl, %edx
orl %edx, %eax
jmp 0x9336
leal -0x20(%rbp), %edx
negb %bpl
movl %r15d, %eax
movl %ebp, %ecx
shll %cl, %eax
movl %edx, %ebp
movl %eax, 0xc(%rsp)
xorl 0x10(%rsp), %r15d
xorl 0x14(%rsp), %r14d
orl %r15d, %r14d
jne 0x9392
cmpl $0x18, %ebp
jl 0x939a
leal -0x18(%rbp), %r13d
movl %ebx, %edx
movl %r13d, %ecx
shrl %cl, %edx
orl %edx, %eax
movl %eax, 0xc(%rsp)
movl $0x4, %esi
movl $0x1, %edx
leaq 0xc(%rsp), %rdi
movq 0x18(%rsp), %rcx
callq 0x1280
cmpl $0x18, %ebp
jne 0x93a9
movl $0x0, 0xc(%rsp)
movl $0x18, %r14d
xorl %eax, %eax
xorl %r13d, %r13d
jmp 0x93b9
movl %r13d, %r14d
movl %ebp, %r13d
jmp 0x93b9
leal 0x8(%rbp), %r13d
movb $0x18, %cl
subb %bpl, %cl
shll %cl, %ebx
orl %eax, %ebx
jmp 0x93b0
movb $0x38, %cl
subb %bpl, %cl
shll %cl, %ebx
movl %ebx, 0xc(%rsp)
movl %ebx, %eax
movl %ebp, %r14d
incq %r12
cmpq %r12, 0x28(%rsp)
jne 0x929a
testl %r13d, %r13d
jle 0x941e
leaq 0xc(%rsp), %rdi
movl $0x4, %esi
movl $0x1, %edx
movq 0x18(%rsp), %rcx
callq 0x1280
cmpl $0x11, %r14d
setge %al
cmpl %r14d, %r13d
setg %cl
testb %cl, %al
je 0x940f
leaq 0xc(%rsp), %rdi
movl $0x4, %esi
movl $0x1, %edx
movq 0x18(%rsp), %rcx
callq 0x1280
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpl $0x11, %r14d
jge 0x93f6
jmp 0x940f