// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movl %ecx, -0x1c(%rbp)
movq -0x8(%rbp), %rax
addq $0x404, %rax            # imm = 0x404
movq %rax, -0x40(%rbp)
movq -0x8(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x48(%rbp)
movq -0x8(%rbp), %rax
cmpl $0x2, (%rax)
jne 0xbb56
movq -0x48(%rbp), %rax
movl 0x3fc(%rax), %eax
movl %eax, -0x4c(%rbp)
movq -0x40(%rbp), %rax
movl 0x3fc(%rax), %eax
movl %eax, -0x50(%rbp)
jmp 0xbb64
movl $0x7fffffff, -0x50(%rbp) # imm = 0x7FFFFFFF
movl $0x7fffffff, -0x4c(%rbp) # imm = 0x7FFFFFFF
movl $0x0, -0x38(%rbp)
movl $0x0, -0x34(%rbp)
movl $0x0, -0x28(%rbp)
movl $0x0, -0x30(%rbp)
movl -0x30(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jge 0xbce5
movq -0x18(%rbp), %rax
movslq -0x30(%rbp), %rcx
movzbl (%rax,%rcx), %eax
movl %eax, -0x20(%rbp)
movq -0x40(%rbp), %rax
movl -0x20(%rbp), %ecx
movl (%rax,%rcx,4), %eax
movl %eax, -0x2c(%rbp)
movq -0x48(%rbp), %rax
movl -0x20(%rbp), %ecx
movl (%rax,%rcx,4), %eax
movl %eax, -0x24(%rbp)
movl -0x34(%rbp), %eax
addl -0x2c(%rbp), %eax
movl %eax, -0x54(%rbp)
movl -0x24(%rbp), %eax
movl %eax, -0x58(%rbp)
movl -0x34(%rbp), %eax
movl %eax, -0x38(%rbp)
cmpl $0x20, -0x54(%rbp)
jl 0xbc1f
movl -0x54(%rbp), %eax
subl $0x20, %eax
movl %eax, -0x34(%rbp)
movl -0x58(%rbp), %eax
movl -0x34(%rbp), %ecx
shrl %cl, %eax
orl -0x28(%rbp), %eax
movl %eax, -0x28(%rbp)
movq -0x10(%rbp), %rcx
leaq -0x28(%rbp), %rdi
movl $0x4, %esi
movl $0x1, %edx
callq 0x1270
cmpl $0x0, -0x34(%rbp)
jle 0xbc16
movl -0x58(%rbp), %eax
movl $0x20, %ecx
subl -0x34(%rbp), %ecx
shll %cl, %eax
movl %eax, -0x28(%rbp)
jmp 0xbc1d
movl $0x0, -0x28(%rbp)
jmp 0xbc38
movl -0x54(%rbp), %eax
movl %eax, -0x34(%rbp)
movl -0x58(%rbp), %eax
movl $0x20, %ecx
subl -0x34(%rbp), %ecx
shll %cl, %eax
orl -0x28(%rbp), %eax
movl %eax, -0x28(%rbp)
movl -0x24(%rbp), %eax
cmpl -0x4c(%rbp), %eax
jne 0xbcd5
movl -0x2c(%rbp), %eax
cmpl -0x50(%rbp), %eax
jne 0xbcd5
movl -0x34(%rbp), %eax
addl $0x8, %eax
movl %eax, -0x5c(%rbp)
movl -0x20(%rbp), %eax
movl %eax, -0x60(%rbp)
movl -0x34(%rbp), %eax
movl %eax, -0x38(%rbp)
cmpl $0x20, -0x5c(%rbp)
jl 0xbcba
movl -0x5c(%rbp), %eax
subl $0x20, %eax
movl %eax, -0x34(%rbp)
movl -0x60(%rbp), %eax
movl -0x34(%rbp), %ecx
shrl %cl, %eax
orl -0x28(%rbp), %eax
movl %eax, -0x28(%rbp)
movq -0x10(%rbp), %rcx
leaq -0x28(%rbp), %rdi
movl $0x4, %esi
movl $0x1, %edx
callq 0x1270
cmpl $0x0, -0x34(%rbp)
jle 0xbcb1
movl -0x60(%rbp), %eax
movl $0x20, %ecx
subl -0x34(%rbp), %ecx
shll %cl, %eax
movl %eax, -0x28(%rbp)
jmp 0xbcb8
movl $0x0, -0x28(%rbp)
jmp 0xbcd3
movl -0x5c(%rbp), %eax
movl %eax, -0x34(%rbp)
movl -0x60(%rbp), %eax
movl $0x20, %ecx
subl -0x34(%rbp), %ecx
shll %cl, %eax
orl -0x28(%rbp), %eax
movl %eax, -0x28(%rbp)
jmp 0xbcd5
jmp 0xbcd7
movl -0x30(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
jmp 0xbb80
cmpl $0x0, -0x34(%rbp)
jle 0xbd29
movq -0x10(%rbp), %rcx
leaq -0x28(%rbp), %rdi
movl $0x4, %esi
movl $0x1, %edx
callq 0x1270
cmpl $0x10, -0x38(%rbp)
jle 0xbd27
movl -0x34(%rbp), %eax
cmpl -0x38(%rbp), %eax
jle 0xbd27
movq -0x10(%rbp), %rcx
leaq -0x28(%rbp), %rdi
movl $0x4, %esi
movl $0x1, %edx
callq 0x1270
jmp 0xbd48
cmpl $0x10, -0x38(%rbp)
jle 0xbd46
movq -0x10(%rbp), %rcx
leaq -0x28(%rbp), %rdi
movl $0x4, %esi
movl $0x1, %edx
callq 0x1270
jmp 0xbd48
addq $0x60, %rsp
popq %rbp
retq
nop