// File: /workspace/github_c_makefile_star2_O1/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, %r8
movl $0x7fffffff, 0x18(%rsp) # imm = 0x7FFFFFFF
movq %rdi, 0x20(%rsp)
cmpl $0x2, (%rdi)
movl $0x7fffffff, 0x1c(%rsp) # imm = 0x7FFFFFFF
jne 0x8ea9
movq 0x20(%rsp), %rax
movl 0x400(%rax), %edx
movl %edx, 0x1c(%rsp)
movl 0x800(%rax), %eax
movl %eax, 0x18(%rsp)
movl $0x0, 0xc(%rsp)
testl %ecx, %ecx
movq %r8, 0x10(%rsp)
jle 0x8fe5
movl %ecx, %eax
movq %rax, 0x28(%rsp)
xorl %r14d, %r14d
xorl %r12d, %r12d
movq 0x30(%rsp), %rax
movzbl (%rax,%r14), %ebx
movq 0x20(%rsp), %rax
movl 0x404(%rax,%rbx,4), %ebp
movl 0x4(%rax,%rbx,4), %r15d
leal (%r12,%rbp), %r13d
movl 0xc(%rsp), %eax
cmpl $0x20, %r13d
jl 0x8f2c
movl %r15d, %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 %r8, %rcx
callq 0x1280
cmpl $0x20, %r13d
jne 0x8f3e
movl $0x0, 0xc(%rsp)
xorl %r13d, %r13d
jmp 0x8f54
movl %r13d, %ecx
negb %cl
movl %r15d, %edx
shll %cl, %edx
orl %edx, %eax
movl %eax, 0xc(%rsp)
jmp 0x8f59
leal -0x20(%r13), %eax
negb %r13b
movl %r15d, %edx
movl %r13d, %ecx
shll %cl, %edx
movl %edx, 0xc(%rsp)
movl %eax, %r13d
movq 0x10(%rsp), %r8
cmpl 0x1c(%rsp), %r15d
jne 0x8fa3
cmpl 0x18(%rsp), %ebp
jne 0x8fa3
cmpl $0x18, %r13d
jl 0x8fa8
leal -0x18(%r13), %ebp
movl %ebx, %eax
movl %ebp, %ecx
shrl %cl, %eax
orl %eax, 0xc(%rsp)
movl $0x4, %esi
movl $0x1, %edx
leaq 0xc(%rsp), %rdi
movq %r8, %rcx
callq 0x1280
cmpl $0x18, %r13d
jne 0x8fbc
movl $0x0, 0xc(%rsp)
xorl %ebp, %ebp
jmp 0x8fc7
movl %r13d, %ebp
jmp 0x8fcf
leal 0x8(%r13), %ebp
movb $0x18, %cl
subb %r13b, %cl
shll %cl, %ebx
orl %ebx, 0xc(%rsp)
movl %r13d, %r12d
jmp 0x8fcf
movb $0x38, %cl
subb %r13b, %cl
shll %cl, %ebx
movl %ebx, 0xc(%rsp)
movl %r13d, %r12d
movq 0x10(%rsp), %r8
movl %r12d, %ebx
incq %r14
movl %ebp, %r12d
cmpq %r14, 0x28(%rsp)
jne 0x8ecb
jmp 0x8fe9
xorl %ebx, %ebx
xorl %ebp, %ebp
testl %ebp, %ebp
jle 0x903e
leaq 0xc(%rsp), %rdi
movl $0x4, %esi
movl $0x1, %edx
movq %r8, %rcx
callq 0x1280
movq 0x10(%rsp), %r8
cmpl $0x11, %ebx
setge %al
cmpl %ebx, %ebp
setg %cl
testb %cl, %al
je 0x902f
leaq 0xc(%rsp), %rdi
movl $0x4, %esi
movl $0x1, %edx
movq %r8, %rcx
callq 0x1280
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpl $0x11, %ebx
jge 0x9018
jmp 0x902f