// File: /workspace/github_c_makefile_star2_O2/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, 0x28(%rsp)
movq %rsi, %r8
movl $0x7fffffff, 0x18(%rsp) # imm = 0x7FFFFFFF
movq %rdi, 0x20(%rsp)
cmpl $0x2, (%rdi)
movl $0x7fffffff, 0x1c(%rsp) # imm = 0x7FFFFFFF
jne 0x7d0d
movq 0x20(%rsp), %rax
movl 0x400(%rax), %edx
movl %edx, 0x1c(%rsp)
movl 0x800(%rax), %eax
movl %eax, 0x18(%rsp)
leaq 0xc(%rsp), %rax
andl $0x0, (%rax)
xorl %eax, %eax
testl %ecx, %ecx
cmovlel %eax, %ecx
movq %rcx, 0x30(%rsp)
xorl %r15d, %r15d
xorl %ebp, %ebp
xorl %ebx, %ebx
movq %r8, 0x10(%rsp)
cmpq %r15, 0x30(%rsp)
je 0x7e44
movq 0x28(%rsp), %rcx
movzbl (%rcx,%r15), %r14d
movq 0x20(%rsp), %rcx
movl 0x404(%rcx,%r14,4), %r13d
movl 0x4(%rcx,%r14,4), %r12d
leal (%rbx,%r13), %ebp
cmpl $0x20, %ebp
jl 0x7d8d
movl %r12d, %edx
movl %ebp, %ecx
shrl %cl, %edx
orl %edx, %eax
movl %eax, 0xc(%rsp)
leaq 0xc(%rsp), %rdi
pushq $0x4
popq %rsi
pushq $0x1
popq %rdx
movq %r8, %rcx
callq 0x1280
cmpl $0x20, %ebp
jne 0x7d9e
andl $0x0, 0xc(%rsp)
xorl %eax, %eax
xorl %ebp, %ebp
jmp 0x7db1
movl %ebp, %ecx
negb %cl
movl %r12d, %edx
shll %cl, %edx
orl %edx, %eax
movl %eax, 0xc(%rsp)
jmp 0x7db6
leal -0x20(%rbp), %edx
negb %bpl
movl %r12d, %eax
movl %ebp, %ecx
shll %cl, %eax
movl %eax, 0xc(%rsp)
movl %edx, %ebp
movq 0x10(%rsp), %r8
cmpl 0x1c(%rsp), %r12d
jne 0x7e04
cmpl 0x18(%rsp), %r13d
jne 0x7e04
cmpl $0x18, %ebp
jl 0x7e09
leal -0x18(%rbp), %r13d
movl %r14d, %edx
movl %r13d, %ecx
shrl %cl, %edx
orl %edx, %eax
movl %eax, 0xc(%rsp)
leaq 0xc(%rsp), %rdi
pushq $0x4
popq %rsi
pushq $0x1
popq %rdx
movq %r8, %rcx
callq 0x1280
cmpl $0x18, %ebp
jne 0x7e20
andl $0x0, 0xc(%rsp)
xorl %eax, %eax
pushq $0x18
popq %rcx
movl %ecx, %ebx
xorl %r13d, %r13d
jmp 0x7e32
movl %ebp, %r13d
jmp 0x7e37
movb $0x18, %cl
subb %bpl, %cl
shll %cl, %r14d
leal 0x8(%rbp), %r13d
orl %r14d, %eax
movl %eax, 0xc(%rsp)
movl %ebp, %ebx
jmp 0x7e37
movb $0x38, %cl
subb %bpl, %cl
shll %cl, %r14d
movl %r14d, 0xc(%rsp)
movl %r14d, %eax
movl %ebp, %ebx
movq 0x10(%rsp), %r8
incq %r15
movl %ebx, %ebp
movl %r13d, %ebx
jmp 0x7d2d
testl %ebx, %ebx
jle 0x7e91
leaq 0xc(%rsp), %rdi
pushq $0x4
popq %rsi
pushq $0x1
popq %rdx
movq %r8, %rcx
callq 0x1280
movq 0x10(%rsp), %r8
cmpl $0x11, %ebp
setge %al
cmpl %ebp, %ebx
setg %cl
testb %cl, %al
je 0x7e82
leaq 0xc(%rsp), %rdi
pushq $0x4
popq %rsi
pushq $0x1
popq %rdx
movq %r8, %rcx
callq 0x1280
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpl $0x11, %ebp
jge 0x7e6f
jmp 0x7e82