// File: /workspace/github_c_makefile_star2_O1/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
movl %r8d, %ebp
movl %ecx, %r8d
movq %rdx, %rbx
movq %rdi, 0x10(%rsp)
incl %esi
movl %esi, %edx
andl $-0x2, %edx
addq %rdi, %rdx
movq %rdx, 0x18(%rsp)
movq %rbx, 0x20(%rsp)
movl 0x8(%rsp), %eax
shrl $0x10, %eax
movl %eax, 0x8(%rsp)
movl $0x10, 0xc(%rsp)
cmpl $0x2, %esi
jb 0x2a69
leaq 0x2(%rdi), %rcx
movq %rcx, 0x10(%rsp)
movzwl (%rdi), %ecx
shll $0x10, %ecx
orl %eax, %ecx
movl %ecx, 0x8(%rsp)
movl 0x8(%rsp), %esi
movl 0xc(%rsp), %ecx
movl %esi, %eax
shrl %cl, %eax
leal 0x10(%rcx), %edi
movl %edi, 0xc(%rsp)
movzwl %ax, %edi
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpl $0x5344, %edi           # imm = 0x5344
jne 0x2ca8
movslq %r8d, %r14
testl %ecx, %ecx
js 0x2ac1
shrl $0x10, %esi
movl %esi, 0x8(%rsp)
movl %ecx, 0xc(%rsp)
movq 0x10(%rsp), %rax
cmpq %rdx, %rax
jae 0x2ac1
leaq 0x2(%rax), %rcx
movq %rcx, 0x10(%rsp)
movzwl (%rax), %eax
shll $0x10, %eax
orl %esi, %eax
movl %eax, 0x8(%rsp)
leaq 0x8(%rsp), %r15
addl $0x10, 0x4(%r15)
addq %rbx, %r14
leaq 0x453b(%rip), %r13      # 0x7010
leaq 0x20(%rsp), %r12
movl 0xc(%rsp), %ecx
cmpl $0x10, %ecx
jl 0x2b16
movl 0x8(%rsp), %eax
shrl $0x10, %eax
movl %eax, 0x8(%rsp)
addl $-0x10, %ecx
movl %ecx, 0xc(%rsp)
movq 0x10(%rsp), %rcx
cmpq 0x18(%rsp), %rcx
jae 0x2b16
leaq 0x2(%rcx), %rdx
movq %rdx, 0x10(%rsp)
movzwl (%rcx), %ecx
shll $0x10, %ecx
orl %eax, %ecx
movl %ecx, 0x8(%rsp)
movl 0x8(%rsp), %r8d
movl 0xc(%rsp), %ecx
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 0x2b84
addl $0x9, %ecx
movl %ecx, 0xc(%rsp)
shrl $0x2, %r8d
andb $0x7f, %r8b
jmp 0x2ba9
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
movl %ecx, 0xc(%rsp)
andl %r8d, %edx
leal (%rdx,%rax), %r8d
addl $0x40, %r8d
movq %r15, %rdi
movq %r12, %rsi
movq %rbx, %rdx
movq %r14, %rcx
movl %ebp, %r9d
callq 0x2d01
jmp 0x2bbc
addl $0x9, %ecx
movl %ecx, 0xc(%rsp)
shrl $0x2, %r8d
orb $-0x80, %r8b
movq 0x20(%rsp), %rax
leaq 0x1(%rax), %rcx
movq %rcx, 0x20(%rsp)
movb %r8b, (%rax)
xorl %eax, %eax
movq 0x20(%rsp), %rdx
testl %eax, %eax
jne 0x2be7
cmpq %r14, %rdx
jae 0x2be7
movq 0x18(%rsp), %rcx
cmpq %rcx, 0x10(%rsp)
jb 0x2ada
jne 0x2be7
cmpl $0x10, 0xc(%rsp)
jl 0x2ada
testl %eax, %eax
js 0x2ca8
btl $0xe, %ebp
jb 0x2ca4
movl 0xc(%rsp), %ecx
cmpl $0x10, %ecx
jl 0x2c35
movl 0x8(%rsp), %eax
shrl $0x10, %eax
movl %eax, 0x8(%rsp)
addl $-0x10, %ecx
movl %ecx, 0xc(%rsp)
movq 0x10(%rsp), %rcx
cmpq 0x18(%rsp), %rcx
jae 0x2c35
leaq 0x2(%rcx), %rsi
movq %rsi, 0x10(%rsp)
movzwl (%rcx), %ecx
shll $0x10, %ecx
orl %eax, %ecx
movl %ecx, 0x8(%rsp)
movl 0x8(%rsp), %esi
movl 0xc(%rsp), %ecx
movl %esi, %edi
shrl %cl, %edi
leal 0x3(%rcx), %eax
movl %eax, 0xc(%rsp)
notl %edi
movl $0xfffffffe, %eax       # imm = 0xFFFFFFFE
testb $0x7, %dil
jne 0x2ca8
cmpl $0xd, %ecx
jl 0x2c89
shrl $0x10, %esi
movl %esi, 0x8(%rsp)
addl $-0xd, %ecx
movl %ecx, 0xc(%rsp)
movq 0x10(%rsp), %rcx
cmpq 0x18(%rsp), %rcx
jae 0x2c89
leaq 0x2(%rcx), %rdi
movq %rdi, 0x10(%rsp)
movzwl (%rcx), %ecx
shll $0x10, %ecx
orl %esi, %ecx
movl %ecx, 0x8(%rsp)
movl 0x8(%rsp), %esi
movl 0xc(%rsp), %ecx
shrl %cl, %esi
addl $0xc, %ecx
movl %ecx, 0xc(%rsp)
notl %esi
testl $0xfff, %esi            # imm = 0xFFF
jne 0x2ca8
subl %ebx, %edx
movl %edx, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq