// File: /workspace/github_c_makefile_star2_O1/zhiyb[P]png2tlg/png2tlg.cpp
// Function: CompressValuesGolomb(TLG6BitStream&, char*, int)
void CompressValuesGolomb(TLG6BitStream &bs, char *buf, int size)
{
	// golomb encoding, -- http://oku.edu.mie-u.ac.jp/~okumura/compression/golomb/

	// run-length golomb method
	bs.PutValue(buf[0]?1:0, 1); // initial value state

	int count;

	int n = TVP_TLG6_GOLOMB_N_COUNT - 1; // 個数のカウンタ
	int a = 0; // 予測誤差の絶対値の和

	count = 0;
	for(int i = 0; i < size; i++)
	{
		if(buf[i])
		{
			// write zero count
			if(count) bs.PutGamma(count);

			// count non-zero values
			count = 0;
			int ii;
			for(ii = i; ii < size; ii++)
			{
				if(buf[ii]) count++; else break;
			}

			// write non-zero count
			bs.PutGamma(count);

			// write non-zero values
			for(; i < ii; i++)
			{
				int e = buf[i];
#ifdef WRITE_VSTXT
				fprintf(vstxt, "%d ", e);
#endif
				int k = TVPTLG6GolombBitLengthTable[a][n];
				int m = ((e >= 0) ? 2*e : -2*e-1) - 1;
				int store_limit = bs.GetBytePos() + GOLOMB_GIVE_UP_BYTES;
				bool put1 = true;
				for(int c = (m >> k); c > 0; c--)
				{
					if(store_limit == bs.GetBytePos())
					{
						bs.PutValue(m >> k, 8);
#ifdef WRITE_VSTXT
						fprintf(vstxt, "[ %d ] ", m>>k);
#endif
						put1 = false;
						break;
					}
					bs.Put1Bit(0);
				}
				if(store_limit == bs.GetBytePos())
				{
					bs.PutValue(m >> k, 8);
#ifdef WRITE_VSTXT
					fprintf(vstxt, "[ %d ] ", m>>k);
#endif
					put1 = false;
				}
				if(put1) bs.Put1Bit(1);
				bs.PutValue(m, k);
				a += (m>>1);
				if (--n < 0) {
					a >>= 1; n = TVP_TLG6_GOLOMB_N_COUNT - 1;
				}
			}

#ifdef WRITE_VSTXT
			fprintf(vstxt, "\n");
#endif

			i = ii - 1;
			count = 0;
		}
		else
		{
			// zero
			count ++;
		}
	}

	if(count) bs.PutGamma(count);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movl %edx, %ebp
movq %rsi, %rax
movq %rdi, %rbx
xorl %esi, %esi
movq %rax, 0x18(%rsp)
cmpb $0x0, (%rax)
setne %sil
callq 0x163ec
testl %ebp, %ebp
jle 0x4d7f
movl %ebp, %eax
movq %rax, 0x38(%rsp)
xorl %r15d, %r15d
movl $0x3, %r12d
xorl %r13d, %r13d
xorl %r14d, %r14d
movl %ebp, 0x34(%rsp)
movslq %r14d, %rcx
movq 0x18(%rsp), %rax
cmpb $0x0, (%rax,%rcx)
je 0x4bf5
movq %rcx, 0x20(%rsp)
testl %r15d, %r15d
je 0x4bba
movq %rbx, %rdi
movl %r15d, %esi
callq 0x16390
xorl %r15d, %r15d
movl %ebp, %eax
movl %r14d, %ecx
movl $0x0, %esi
subl %r14d, %eax
jle 0x4c07
movslq %r14d, %rsi
movq 0x18(%rsp), %rcx
addq %rsi, %rcx
movq 0x38(%rsp), %rdx
subq %rsi, %rdx
xorl %esi, %esi
cmpb $0x0, (%rcx,%rsi)
je 0x4c03
incq %rsi
cmpq %rsi, %rdx
jne 0x4be1
movl %ebp, %ecx
movl %eax, %esi
jmp 0x4c07
incl %r15d
incl %r14d
movl %r14d, %eax
jmp 0x4d72
leal (%r14,%rsi), %ecx
movl %ecx, 0xc(%rsp)
movq %rbx, %rdi
callq 0x16390
movl 0xc(%rsp), %eax
cmpl %eax, %r14d
jge 0x4d72
movl $0x3, %r14d
movq 0x20(%rsp), %rcx
movq 0x18(%rsp), %rax
movq %rcx, 0x20(%rsp)
movsbl (%rax,%rcx), %eax
movl %r13d, 0x10(%rsp)
movslq %r13d, %rcx
movq %r12, 0x40(%rsp)
movslq %r12d, %rdx
leaq 0x175f0(%rip), %rsi     # 0x1c240
leaq (%rsi,%rcx,4), %rcx
movsbl (%rdx,%rcx), %r12d
movl %eax, %ecx
addl %eax, %ecx
sarb $0x7, %al
movsbl %al, %edx
xorl %ecx, %edx
decl %edx
movl 0x8(%rbx), %eax
movl %edx, 0x14(%rsp)
movl %r12d, %ecx
sarl %cl, %edx
addl $0x4, %eax
movslq %eax, %r13
movq %rdx, 0x28(%rsp)
testl %edx, %edx
setle %r15b
jle 0x4cd4
cmpq %r13, 0x8(%rbx)
je 0x4caf
movq 0x28(%rsp), %rax
leal 0x1(%rax), %ebp
movq %rbx, %rdi
xorl %esi, %esi
callq 0x163ec
decl %ebp
cmpl $0x2, %ebp
setl %r15b
jl 0x4cd4
cmpq %r13, 0x8(%rbx)
jne 0x4c94
movl 0x28(%rsp), %ebp
movl $0xfffffff8, %r14d      # imm = 0xFFFFFFF8
movl %ebp, %esi
andl $0x1, %esi
movq %rbx, %rdi
callq 0x163ec
shrq %rbp
incl %r14d
jne 0x4cb9
movl $0x3, %r14d
cmpq %r13, 0x8(%rbx)
jne 0x4d00
movslq 0x28(%rsp), %r15
movl $0xfffffff8, %ebp       # imm = 0xFFFFFFF8
movl 0x10(%rsp), %r13d
movl %r15d, %esi
andl $0x1, %esi
movq %rbx, %rdi
callq 0x163ec
sarq %r15
incl %ebp
jne 0x4ce9
jmp 0x4d17
testb %r15b, %r15b
movl 0x10(%rsp), %r13d
je 0x4d17
movq %rbx, %rdi
movl $0x1, %esi
callq 0x163ec
testb %r12b, %r12b
je 0x4d37
movslq 0x14(%rsp), %r15
movl %r15d, %esi
andl $0x1, %esi
movq %rbx, %rdi
callq 0x163ec
sarq %r15
decl %r12d
jne 0x4d21
movl 0x14(%rsp), %eax
sarl %eax
addl %eax, %r13d
movq 0x40(%rsp), %rax
testl %eax, %eax
leal -0x1(%rax), %eax
setle %cl
movl %eax, %r12d
cmovlel %r14d, %r12d
sarl %cl, %r13d
movq 0x20(%rsp), %rcx
incq %rcx
movl 0xc(%rsp), %eax
cmpl %ecx, %eax
jne 0x4c2b
movl 0x34(%rsp), %ebp
xorl %r15d, %r15d
movl %eax, %r14d
cmpl %ebp, %eax
jl 0x4b97
jmp 0x4d82
xorl %r15d, %r15d
testl %r15d, %r15d
je 0x4da0
movq %rbx, %rdi
movl %r15d, %esi
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x16390
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq