// File: /workspace/github_c_makefile_star2_O3/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 $0x38, %rsp
movl %edx, %ebp
movq %rsi, %rax
movq %rdi, %rbx
xorl %esi, %esi
movq %rax, 0x10(%rsp)
cmpb $0x0, (%rax)
setne %sil
callq 0x16bf2
testl %ebp, %ebp
jle 0x4eec
movl %ebp, %eax
movq %rax, 0x20(%rsp)
xorl %r15d, %r15d
movl $0x3, %r13d
xorl %r12d, %r12d
xorl %r14d, %r14d
movl %ebp, 0x18(%rsp)
movslq %r14d, %rcx
movq 0x10(%rsp), %rax
cmpb $0x0, (%rax,%rcx)
je 0x4d5d
movq %rcx, 0x8(%rsp)
testl %r15d, %r15d
je 0x4d20
movq %rbx, %rdi
movl %r15d, %esi
callq 0x16b82
xorl %r15d, %r15d
movl %ebp, %eax
movl %r14d, %ecx
movl $0x0, %esi
subl %r14d, %eax
jle 0x4d6f
movq 0x10(%rsp), %rcx
movq 0x8(%rsp), %rsi
addq %rsi, %rcx
movq 0x20(%rsp), %rdx
subq %rsi, %rdx
xorl %esi, %esi
cmpb $0x0, (%rcx,%rsi)
je 0x4d6b
incq %rsi
cmpq %rsi, %rdx
jne 0x4d49
movl %ebp, %ecx
movl %eax, %esi
jmp 0x4d6f
incl %r15d
incl %r14d
movl %r14d, %eax
jmp 0x4ec3
leal (%r14,%rsi), %ecx
movl %ecx, 0x4(%rsp)
movq %rbx, %rdi
callq 0x16b82
movl 0x4(%rsp), %eax
cmpl %eax, %r14d
jge 0x4ec3
movq 0x8(%rsp), %rcx
movq 0x10(%rsp), %rax
movq %rcx, 0x8(%rsp)
movsbl (%rax,%rcx), %eax
movl %r12d, 0x1c(%rsp)
movslq %r12d, %rcx
movq %r13, 0x30(%rsp)
movslq %r13d, %rdx
leaq 0x1845e(%rip), %rsi     # 0x1d210
leaq (%rsi,%rcx,4), %rcx
movsbl (%rdx,%rcx), %r12d
movl %eax, %ecx
addl %eax, %ecx
sarb $0x7, %al
movsbl %al, %r14d
xorl %ecx, %r14d
decl %r14d
movl %r14d, %r15d
movl %r12d, %ecx
sarl %cl, %r15d
movq 0x8(%rbx), %rax
leal 0x4(%rax), %ecx
movslq %ecx, %r13
testl %r15d, %r15d
jle 0x4e03
leal 0x1(%r15), %ebp
cmpq %r13, 0x8(%rbx)
je 0x4e17
movq %rbx, %rdi
xorl %esi, %esi
callq 0x16bf2
decl %ebp
cmpl $0x2, %ebp
jge 0x4de8
movq 0x8(%rbx), %rax
cmpq %r13, %rax
je 0x4e44
movq %rbx, %rdi
movl $0x1, %esi
callq 0x16bf2
jmp 0x4e61
movq %r15, 0x28(%rsp)
movl %r15d, %r15d
movl $0xfffffff8, %ebp       # imm = 0xFFFFFFF8
movl %r15d, %esi
andl $0x1, %esi
movq %rbx, %rdi
callq 0x16bf2
shrq %r15
incl %ebp
jne 0x4e24
cmpq %r13, 0x8(%rbx)
movq 0x28(%rsp), %r15
jne 0x4e61
movslq %r15d, %r15
movl $0xfffffff8, %ebp       # imm = 0xFFFFFFF8
movl %r15d, %esi
andl $0x1, %esi
movq %rbx, %rdi
callq 0x16bf2
sarq %r15
incl %ebp
jne 0x4e4c
testb %r12b, %r12b
movq 0x30(%rsp), %r13
je 0x4e84
movslq %r14d, %r15
movl %r15d, %esi
andl $0x1, %esi
movq %rbx, %rdi
callq 0x16bf2
sarq %r15
decl %r12d
jne 0x4e6e
sarl %r14d
movl 0x1c(%rsp), %r12d
addl %r14d, %r12d
testl %r13d, %r13d
leal -0x1(%r13), %eax
setle %cl
movl %eax, %r13d
movl $0x3, %eax
cmovlel %eax, %r13d
sarl %cl, %r12d
movq 0x8(%rsp), %rcx
incq %rcx
movl 0x4(%rsp), %eax
cmpl %ecx, %eax
jne 0x4d8d
movl 0x18(%rsp), %ebp
xorl %r15d, %r15d
movl %eax, %r14d
cmpl %ebp, %eax
jl 0x4cfd
testl %r15d, %r15d
je 0x4eec
movq %rbx, %rdi
movl %r15d, %esi
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x16b82
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq