// File: /workspace/github_c_makefile_star2_O3/pali[P]bmfdec/./bmfdec.c
// Function: ds_dec
int ds_dec(void* pin,int lin, void* pout, int lout, int flg)
{ 
  __u8 *p, *pend;
  unsigned u, repoffs;
  int r;
  bits_t bits;

  dblb_rdi(&bits,pin,lin);
  p=(__u8*)pout;pend=p+lout;
  if((dblb_rdn(&bits,16))!=0x5344) return -1;
    
  u=dblb_rdn(&bits,16);
  u=((u&0xff)<<8)|((u>>8)&0xff);
  LOG_DECOMP("DMSDOS: DS decompression version %d\n",u);
  
  do
  { r=0;
    RDN_PR(bits,u);
    switch(u&3)
    {
      case 0:
	bits.pb+=2+6;
	repoffs=(u>>2)&63;
	r=dblb_decrep(&bits,&p,pout,pend,repoffs,-1,flg);
	break;
      case 1:
	bits.pb+=2+7;
	*(p++)=(u>>2)|128;
	break;
      case 2:
	bits.pb+=2+7;
	*(p++)=(u>>2)&127;
	break;
      case 3:
	if(u&4) {  bits.pb+=3+12; repoffs=((u>>3)&4095)+320; }
	else  {  bits.pb+=3+8;  repoffs=((u>>3)&255)+64; };
	r=dblb_decrep(&bits,&p,pout,pend,repoffs,-1,flg);
	break;
    }
  }while((r==0)&&(p<pend)&&(bits.pd<bits.pe||(bits.pd==bits.pe&&bits.pb<16)));
  
  if(r<0) return r;

  if(!(flg&0x4000))
  { 
    u=dblb_rdn(&bits,3);if(u==7) u=dblb_rdn(&bits,12)+320;
    if(u!=0x113f)
    { LOG_DECOMP("DMSDOS: decrb: final sync not found?\n");
      return -2;
    }
  }

  return p-(__u8*)pout;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rdx, %rbx
incl %esi
movl %esi, %edx
andl $-0x2, %edx
leaq (%rdx,%rdi), %r9
movq %r9, 0x18(%rsp)
movq %rbx, 0x20(%rsp)
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpl $0x2, %esi
jb 0x2be3
leaq 0x2(%rdi), %rsi
movq %rsi, 0x10(%rsp)
cmpw $0x5344, (%rdi)         # imm = 0x5344
jne 0x2be3
movl %r8d, %ebp
movslq %ecx, %r14
movl $0x5344, 0x8(%rsp)      # imm = 0x5344
movl $0x5344, %r8d           # imm = 0x5344
cmpl $0x3, %edx
jb 0x2a4b
leaq 0x4(%rdi), %rsi
movq %rsi, 0x10(%rsp)
movzwl 0x2(%rdi), %r8d
shll $0x10, %r8d
orl $0x5344, %r8d           # imm = 0x5344
movl %r8d, 0x8(%rsp)
addq %rbx, %r14
movl $0x20, %ecx
leaq 0x45b6(%rip), %r13      # 0x7010
leaq 0x8(%rsp), %r15
leaq 0x20(%rsp), %r12
movq %rbx, %rdx
cmpl $0x10, %ecx
jl 0x2a94
shrl $0x10, %r8d
movl %r8d, 0x8(%rsp)
addl $-0x10, %ecx
cmpq %r9, %rsi
jae 0x2a94
leaq 0x2(%rsi), %rax
movq %rax, 0x10(%rsp)
movzwl (%rsi), %eax
shll $0x10, %eax
orl %eax, %r8d
movl %r8d, 0x8(%rsp)
shrl %cl, %r8d
movl %r8d, %eax
andl $0x3, %eax
movslq (%r13,%rax,4), %rax
addq %r13, %rax
jmpq *%rax
addl $0x8, %ecx
movl %ecx, 0xc(%rsp)
shrl $0x2, %r8d
andl $0x3f, %r8d
jmp 0x2af9
addl $0x9, %ecx
movl %ecx, 0xc(%rsp)
shrl $0x2, %r8d
andb $0x7f, %r8b
jmp 0x2b1e
movl %r8d, %eax
andl $0x4, %eax
shrl $0x3, %r8d
testl %eax, %eax
movl $0xfff, %edx            # imm = 0xFFF
movl $0xff, %esi
cmovel %esi, %edx
addl %eax, %ecx
addl $0xb, %ecx
shll $0x6, %eax
andl %r8d, %edx
leal (%rdx,%rax), %r8d
addl $0x40, %r8d
movl %ecx, 0xc(%rsp)
movq %r15, %rdi
movq %r12, %rsi
movq %rbx, %rdx
movq %r14, %rcx
movl %ebp, %r9d
callq 0x2c34
jmp 0x2b2c
addl $0x9, %ecx
movl %ecx, 0xc(%rsp)
shrl $0x2, %r8d
orb $-0x80, %r8b
leaq 0x1(%rdx), %rax
movq %rax, 0x20(%rsp)
movb %r8b, (%rdx)
xorl %eax, %eax
movq 0x20(%rsp), %rdx
testl %eax, %eax
jne 0x2b64
cmpq %r14, %rdx
jae 0x2b64
movq 0x10(%rsp), %rsi
movq 0x18(%rsp), %r9
cmpq %r9, %rsi
jae 0x2b4f
movl 0xc(%rsp), %ecx
jmp 0x2b5a
jne 0x2b68
movl 0xc(%rsp), %ecx
cmpl $0xf, %ecx
jg 0x2b68
movl 0x8(%rsp), %r8d
jmp 0x2a67
testl %eax, %eax
js 0x2be3
btl $0xe, %ebp
jb 0x2bdf
movl 0x8(%rsp), %esi
movl 0xc(%rsp), %ecx
cmpl $0x10, %ecx
jl 0x2b9e
shrl $0x10, %esi
addl $-0x10, %ecx
movq 0x10(%rsp), %rax
cmpq 0x18(%rsp), %rax
jae 0x2b9e
leaq 0x2(%rax), %rdi
movq %rdi, 0x10(%rsp)
movzwl (%rax), %eax
shll $0x10, %eax
orl %eax, %esi
movl %esi, %edi
shrl %cl, %edi
notl %edi
movl $0xfffffffe, %eax       # imm = 0xFFFFFFFE
testb $0x7, %dil
jne 0x2be3
cmpl $0xd, %ecx
jl 0x2bd0
shrl $0x10, %esi
addl $-0xd, %ecx
movq 0x10(%rsp), %rdi
cmpq 0x18(%rsp), %rdi
jae 0x2bd3
movzwl (%rdi), %edi
shll $0x10, %edi
orl %edi, %esi
jmp 0x2bd3
addl $0x3, %ecx
shrl %cl, %esi
notl %esi
testl $0xfff, %esi            # imm = 0xFFF
jne 0x2be3
subl %ebx, %edx
movl %edx, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq