// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movb (%rax), %dl
xorl %eax, %eax
movl $0x1, %ecx
cmpb $0x0, %dl
cmovnel %ecx, %eax
movslq %eax, %rsi
movl $0x1, %edx
callq 0x1f020
movl $0x3, -0x1c(%rbp)
movl $0x0, -0x20(%rbp)
movl $0x0, -0x18(%rbp)
movl $0x0, -0x24(%rbp)
movl -0x24(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0x4c32
movq -0x10(%rbp), %rax
movslq -0x24(%rbp), %rcx
cmpb $0x0, (%rax,%rcx)
je 0x4c19
cmpl $0x0, -0x18(%rbp)
je 0x4a53
movq -0x8(%rbp), %rdi
movl -0x18(%rbp), %esi
callq 0x1f080
movl $0x0, -0x18(%rbp)
movl -0x24(%rbp), %eax
movl %eax, -0x28(%rbp)
movl -0x28(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0x4a90
movq -0x10(%rbp), %rax
movslq -0x28(%rbp), %rcx
cmpb $0x0, (%rax,%rcx)
je 0x4a81
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
jmp 0x4a83
jmp 0x4a90
jmp 0x4a85
movl -0x28(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x28(%rbp)
jmp 0x4a60
movq -0x8(%rbp), %rdi
movl -0x18(%rbp), %esi
callq 0x1f080
movl -0x24(%rbp), %eax
cmpl -0x28(%rbp), %eax
jge 0x4c07
movq -0x10(%rbp), %rax
movslq -0x24(%rbp), %rcx
movsbl (%rax,%rcx), %eax
movl %eax, -0x2c(%rbp)
movslq -0x20(%rbp), %rcx
leaq 0x2188e(%rip), %rax     # 0x26350
shlq $0x2, %rcx
addq %rcx, %rax
movslq -0x1c(%rbp), %rcx
movsbl (%rax,%rcx), %eax
movl %eax, -0x30(%rbp)
cmpl $0x0, -0x2c(%rbp)
jl 0x4ae4
movl -0x2c(%rbp), %eax
shll %eax
movl %eax, -0x44(%rbp)
jmp 0x4af1
imull $0xfffffffe, -0x2c(%rbp), %eax # imm = 0xFFFFFFFE
subl $0x1, %eax
movl %eax, -0x44(%rbp)
movl -0x44(%rbp), %eax
subl $0x1, %eax
movl %eax, -0x34(%rbp)
movq -0x8(%rbp), %rdi
callq 0x1f120
addq $0x4, %rax
movl %eax, -0x38(%rbp)
movb $0x1, -0x39(%rbp)
movl -0x34(%rbp), %eax
movl -0x30(%rbp), %ecx
sarl %cl, %eax
movl %eax, -0x40(%rbp)
cmpl $0x0, -0x40(%rbp)
jle 0x4b71
movslq -0x38(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0x8(%rbp), %rdi
callq 0x1f120
movq %rax, %rcx
movq -0x50(%rbp), %rax
cmpq %rcx, %rax
jne 0x4b5b
movq -0x8(%rbp), %rdi
movl -0x34(%rbp), %eax
movl -0x30(%rbp), %ecx
sarl %cl, %eax
movslq %eax, %rsi
movl $0x8, %edx
callq 0x1f020
movb $0x0, -0x39(%rbp)
jmp 0x4b71
movq -0x8(%rbp), %rdi
xorl %esi, %esi
callq 0x1f140
movl -0x40(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x40(%rbp)
jmp 0x4b19
movslq -0x38(%rbp), %rax
movq %rax, -0x58(%rbp)
movq -0x8(%rbp), %rdi
callq 0x1f120
movq %rax, %rcx
movq -0x58(%rbp), %rax
cmpq %rcx, %rax
jne 0x4bab
movq -0x8(%rbp), %rdi
movl -0x34(%rbp), %eax
movl -0x30(%rbp), %ecx
sarl %cl, %eax
movslq %eax, %rsi
movl $0x8, %edx
callq 0x1f020
movb $0x0, -0x39(%rbp)
testb $0x1, -0x39(%rbp)
je 0x4bbf
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x1f140
movq -0x8(%rbp), %rdi
movslq -0x34(%rbp), %rsi
movl -0x30(%rbp), %edx
callq 0x1f020
movl -0x34(%rbp), %eax
sarl %eax
addl -0x20(%rbp), %eax
movl %eax, -0x20(%rbp)
movl -0x1c(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x1c(%rbp)
cmpl $0x0, %eax
jge 0x4bf7
movl -0x20(%rbp), %eax
sarl %eax
movl %eax, -0x20(%rbp)
movl $0x3, -0x1c(%rbp)
jmp 0x4bf9
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x4a9c
movl -0x28(%rbp), %eax
subl $0x1, %eax
movl %eax, -0x24(%rbp)
movl $0x0, -0x18(%rbp)
jmp 0x4c22
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
jmp 0x4c24
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x4a23
cmpl $0x0, -0x18(%rbp)
je 0x4c44
movq -0x8(%rbp), %rdi
movl -0x18(%rbp), %esi
callq 0x1f080
addq $0x60, %rsp
popq %rbp
retq
nopw (%rax,%rax)