// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x80, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movq %rdx, -0x20(%rbp)
movl %ecx, -0x24(%rbp)
movl %r8d, -0x28(%rbp)
movq -0x10(%rbp), %rsi
movl -0x14(%rbp), %edx
leaq -0x60(%rbp), %rdi
callq 0x1590
movq -0x20(%rbp), %rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
movslq -0x24(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0x38(%rbp)
leaq -0x60(%rbp), %rdi
movl $0x10, %esi
callq 0x15e0
cmpl $0x5344, %eax           # imm = 0x5344
je 0x1315
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x1581
leaq -0x60(%rbp), %rdi
movl $0x10, %esi
callq 0x15e0
movl %eax, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
andl $0xff, %eax
shll $0x8, %eax
movl -0x3c(%rbp), %ecx
shrl $0x8, %ecx
andl $0xff, %ecx
orl %ecx, %eax
movl %eax, -0x3c(%rbp)
movl $0x0, -0x44(%rbp)
cmpl $0x10, -0x5c(%rbp)
jl 0x1388
movl -0x60(%rbp), %eax
shrl $0x10, %eax
movl %eax, -0x60(%rbp)
movl -0x5c(%rbp), %eax
subl $0x10, %eax
movl %eax, -0x5c(%rbp)
movq -0x58(%rbp), %rax
cmpq -0x50(%rbp), %rax
jae 0x1386
movq -0x58(%rbp), %rax
movq %rax, %rcx
addq $0x2, %rcx
movq %rcx, -0x58(%rbp)
movzwl (%rax), %eax
shll $0x10, %eax
orl -0x60(%rbp), %eax
movl %eax, -0x60(%rbp)
jmp 0x1388
movl -0x60(%rbp), %eax
movb -0x5c(%rbp), %cl
shrl %cl, %eax
movl %eax, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
andl $0x3, %eax
movl %eax, %ecx
movq %rcx, -0x68(%rbp)
subl $0x3, %eax
ja 0x14bc
movq -0x68(%rbp), %rax
leaq 0x6c5d(%rip), %rcx      # 0x8010
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movl -0x5c(%rbp), %eax
addl $0x8, %eax
movl %eax, -0x5c(%rbp)
movl -0x3c(%rbp), %eax
shrl $0x2, %eax
andl $0x3f, %eax
movl %eax, -0x40(%rbp)
movq -0x20(%rbp), %rdx
movq -0x38(%rbp), %rcx
movl -0x40(%rbp), %r8d
movl -0x28(%rbp), %eax
leaq -0x60(%rbp), %rdi
leaq -0x30(%rbp), %rsi
movl $0xffffffff, %r9d       # imm = 0xFFFFFFFF
movl %eax, (%rsp)
callq 0x1680
movl %eax, -0x44(%rbp)
jmp 0x14bc
movl -0x5c(%rbp), %eax
addl $0x9, %eax
movl %eax, -0x5c(%rbp)
movl -0x3c(%rbp), %eax
shrl $0x2, %eax
orl $0x80, %eax
movb %al, %cl
movq -0x30(%rbp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, -0x30(%rbp)
movb %cl, (%rax)
jmp 0x14bc
movl -0x5c(%rbp), %eax
addl $0x9, %eax
movl %eax, -0x5c(%rbp)
movl -0x3c(%rbp), %eax
shrl $0x2, %eax
andl $0x7f, %eax
movb %al, %cl
movq -0x30(%rbp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, -0x30(%rbp)
movb %cl, (%rax)
jmp 0x14bc
movl -0x3c(%rbp), %eax
andl $0x4, %eax
cmpl $0x0, %eax
je 0x147a
movl -0x5c(%rbp), %eax
addl $0xf, %eax
movl %eax, -0x5c(%rbp)
movl -0x3c(%rbp), %eax
shrl $0x3, %eax
andl $0xfff, %eax            # imm = 0xFFF
addl $0x140, %eax            # imm = 0x140
movl %eax, -0x40(%rbp)
jmp 0x1494
movl -0x5c(%rbp), %eax
addl $0xb, %eax
movl %eax, -0x5c(%rbp)
movl -0x3c(%rbp), %eax
shrl $0x3, %eax
andl $0xff, %eax
addl $0x40, %eax
movl %eax, -0x40(%rbp)
movq -0x20(%rbp), %rdx
movq -0x38(%rbp), %rcx
movl -0x40(%rbp), %r8d
movl -0x28(%rbp), %eax
leaq -0x60(%rbp), %rdi
leaq -0x30(%rbp), %rsi
movl $0xffffffff, %r9d       # imm = 0xFFFFFFFF
movl %eax, (%rsp)
callq 0x1680
movl %eax, -0x44(%rbp)
jmp 0x14be
xorl %eax, %eax
cmpl $0x0, -0x44(%rbp)
movb %al, -0x69(%rbp)
jne 0x150c
movq -0x30(%rbp), %rcx
xorl %eax, %eax
cmpq -0x38(%rbp), %rcx
movb %al, -0x69(%rbp)
jae 0x150c
movq -0x58(%rbp), %rcx
movb $0x1, %al
cmpq -0x50(%rbp), %rcx
movb %al, -0x6a(%rbp)
jb 0x1506
movq -0x58(%rbp), %rcx
xorl %eax, %eax
cmpq -0x50(%rbp), %rcx
movb %al, -0x6b(%rbp)
jne 0x1500
cmpl $0x10, -0x5c(%rbp)
setl %al
movb %al, -0x6b(%rbp)
movb -0x6b(%rbp), %al
movb %al, -0x6a(%rbp)
movb -0x6a(%rbp), %al
movb %al, -0x69(%rbp)
movb -0x69(%rbp), %al
testb $0x1, %al
jne 0x1342
cmpl $0x0, -0x44(%rbp)
jge 0x1525
movl -0x44(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x1581
movl -0x28(%rbp), %eax
andl $0x4000, %eax           # imm = 0x4000
cmpl $0x0, %eax
jne 0x1573
leaq -0x60(%rbp), %rdi
movl $0x3, %esi
callq 0x15e0
movl %eax, -0x3c(%rbp)
cmpl $0x7, -0x3c(%rbp)
jne 0x155f
leaq -0x60(%rbp), %rdi
movl $0xc, %esi
callq 0x15e0
addl $0x140, %eax            # imm = 0x140
movl %eax, -0x3c(%rbp)
cmpl $0x113f, -0x3c(%rbp)    # imm = 0x113F
je 0x1571
movl $0xfffffffe, -0x4(%rbp) # imm = 0xFFFFFFFE
jmp 0x1581
jmp 0x1573
movq -0x30(%rbp), %rax
movq -0x20(%rbp), %rcx
subq %rcx, %rax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x80, %rsp
popq %rbp
retq
nopl (%rax)