	
	.text

	.align		4
	
	.global		lzss_dec_th
	.type		lzss_dec_th,function
	.thumb

;*********************************************************************************
;*
;* lzss_dec_th (lzss Decoder)
;*
;* r0 = input 
;* r1 = output 
;* r2 = inputmax (input+len)
;* r3 = tbuffer (muss davor mit ' ' initalisiert werden)
;*

lzss_dec_th

	push	{r4, r5, r6, r7, lr}
	mov	r7, sl
	mov	r6, r8
	push	{r6, r7}
	mov	r4, r0	;  in
	mov	r8, r2	;  inmax
	;ldr	r0, .L18	;  r 4078
	mov	r0,#$fe
	lsl     r0,r0,#4
	add	r0,r0,#$e
	
	mov	r2, #0
	mov	r5, r1	;  out
	mov	ip, r3	;  tbuffer
	mov	lr, r2	;  flags
.L17:
	cmp	r4, r8	;  in,  inmax
	beq	.L1
	mov	r3, lr	;  flags
	mov	r2, #128
	lsr	r3, r3, #1
	lsl	r1, r2, #1
	and	r1, r1, r3
	mov	lr, r3	;  flags
	cmp	r1, #0
	bne	.L6
	ldrb	r2, [r4]	; * in
	mov	r6, #255
	lsl	r1, r6, #8
	orr	r2, r2, r1
	add	r4, r4, #1	;  in
	mov	lr, r2	;  flags
.L6:
	mov	r2, #1
	mov	r6, lr	;  flags
	and	r2, r2, r6
	cmp	r2, #0
	beq	.L7
	ldrb	r1, [r4]	;  c, * in
	mov	r2, ip	;  tbuffer
	strb	r1, [r5]	;  c, * out
	strb	r1, [r2, r0]	;  c, * r
	
	;ldr	r1, .L18+4	; optimized by goldmomo 4095
	mov	r1,#$ff
	lsl     r1,r1,#4
	add	r1,r1,#$f

	add	r0, r0, #1	;  r
	add	r4, r4, #1	;  in
	add	r5, r5, #1	;  out
	and	r0, r0, r1	;  r
	b	.L17
.L7:
	ldrb	r3, [r4]	; * in
	add	r4, r4, #1	;  in
	mov	sl, r3	;  i
	cmp	r4, r8	;  in,  inmax
	bcs	.L1
	ldrb	r1, [r4]	;  j, * in
	mov	r6, #240
	and	r6, r6, r1	;  j
	lsl	r2, r6, #4
	mov	r6, r3	;  i
	orr	r6, r6, r2
	mov	r2, #15
	and	r2, r2, r1	;  j
	add	r1, r2, #2	;  j
	mov	r2, #0	;  k
	add	r4, r4, #1	;  in
	mov	sl, r6	;  i
	cmp	r2, r1	;  k,  j
	bgt	.L17
	;ldr	r7, .L18+4	; optimized by goldmomo 4095
	mov	r7,#$ff
	lsl     r7,r7,#4
	add	r7,r7,#$f
.L14:
	mov	r6, sl		;  i
	add	r3, r6, r2	;  k
	and	r3, r3, r7
	mov	r6, ip	;  tbuffer
	ldrb	r3, [r6, r3]	;  c
	add	r2, r2, #1	;  k
	strb	r3, [r5]	;  c, * out
	add	r5, r5, #1	;  out
	strb	r3, [r6, r0]	;  c, * r
	add	r0, r0, #1	;  r
	and	r0, r0, r7	;  r
	cmp	r2, r1	;  k,  j
	ble	.L14
	b	.L17
.L1:
	; sp needed for prologue
	pop	{r3, r4}
	mov	r8, r3
	mov	sl, r4
	pop	{r4, r5, r6, r7}
	pop	{r0}
	bx	r0
;.L19:
	
;.L18:
;	.word	4078
;	.word	4095

	
	.end