// File: /workspace/github_c_makefile_star2_O0/tvlabs[P]edge264/./edge264_bitstream.c
// Function: cabac_start
static int cabac_start(Edge264Context *ctx) {
	// reclaim bits from cache while realigning with CPB on a byte boundary
	int extra_bits = SIZE_BIT - 1 - ctz(ctx->t._gb.lsb_cache);
	int shift = extra_bits & 7;
	int ret = shift > 0 && (ssize_t)ctx->t._gb.msb_cache >> (SIZE_BIT - shift) != -1; // return 1 if not all alignment bits are ones
	ctx->t._gb.codIOffset = shld(ctx->t._gb.lsb_cache, ctx->t._gb.msb_cache, shift); // codIOffset and msb_cache are the same memory slot
	ctx->t._gb.lsb_cache >>= (SIZE_BIT - extra_bits) & (SIZE_BIT - 1);
	while (extra_bits >= 8) {
		int32_t i = 0;
		if ((intptr_t)(ctx->t._gb.end - ctx->t._gb.CPB) >= 0)
			memcpy(&i, ctx->t._gb.CPB - 4, 4);
		ctx->t._gb.CPB -= 1 + ((big_endian32(i) & 0xffffff) == 3);
		ctx->t._gb.lsb_cache >>= 8;
		extra_bits -= 8;
	}
	ctx->t._gb.codIRange = (size_t)510 << (SIZE_BIT - 9);
	ctx->t._gb.codIOffset = (ctx->t._gb.codIOffset < ctx->t._gb.codIRange) ? ctx->t._gb.codIOffset : ctx->t._gb.codIRange - 1; // protection against invalid bitstream
	return ret;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
movq %rdi, -0x20(%rbp)
movq -0x20(%rbp), %rax
movq 0x10(%rax), %rax
tzcntq %rax, %rax
movl %eax, %ecx
movl $0x3f, %eax
subl %ecx, %eax
movl %eax, -0x24(%rbp)
movl -0x24(%rbp), %eax
andl $0x7, %eax
movl %eax, -0x28(%rbp)
xorl %eax, %eax
cmpl $0x0, -0x28(%rbp)
movb %al, -0x31(%rbp)
jle 0xaca4
movq -0x20(%rbp), %rax
movq 0x18(%rax), %rax
movl $0x40, %ecx
subl -0x28(%rbp), %ecx
movl %ecx, %ecx
sarq %cl, %rax
cmpq $-0x1, %rax
setne %al
movb %al, -0x31(%rbp)
movb -0x31(%rbp), %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, -0x2c(%rbp)
movq -0x20(%rbp), %rax
movq 0x10(%rax), %rdx
movq -0x20(%rbp), %rax
movq 0x18(%rax), %rcx
movl -0x28(%rbp), %eax
movq %rdx, -0x8(%rbp)
movq %rcx, -0x10(%rbp)
movl %eax, -0x14(%rbp)
movq -0x10(%rbp), %rax
movq -0x8(%rbp), %rdx
movl -0x14(%rbp), %ecx
shldq %cl, %rdx, %rax
movq %rax, -0x10(%rbp)
movq -0x10(%rbp), %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x18(%rax)
movl $0x40, %ecx
subl -0x24(%rbp), %ecx
andl $0x3f, %ecx
movq -0x20(%rbp), %rax
movq 0x10(%rax), %rdx
movl %ecx, %ecx
shrq %cl, %rdx
movq %rdx, %rcx
movq %rcx, 0x10(%rax)
cmpl $0x8, -0x24(%rbp)
jl 0xad85
movl $0x0, -0x30(%rbp)
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x20(%rbp), %rcx
movq (%rcx), %rcx
subq %rcx, %rax
cmpq $0x0, %rax
jl 0xad3d
movq -0x20(%rbp), %rax
movq (%rax), %rax
movl -0x4(%rax), %eax
movl %eax, -0x30(%rbp)
movl -0x30(%rbp), %eax
bswapl %eax
andl $0xffffff, %eax         # imm = 0xFFFFFF
cmpl $0x3, %eax
sete %al
andb $0x1, %al
movzbl %al, %edx
addl $0x1, %edx
movq -0x20(%rbp), %rax
movq (%rax), %rcx
movslq %edx, %rsi
xorl %edx, %edx
subq %rsi, %rdx
addq %rdx, %rcx
movq %rcx, (%rax)
movq -0x20(%rbp), %rax
movq 0x10(%rax), %rcx
shrq $0x8, %rcx
movq %rcx, 0x10(%rax)
movl -0x24(%rbp), %eax
subl $0x8, %eax
movl %eax, -0x24(%rbp)
jmp 0xad0b
movq -0x20(%rbp), %rax
movabsq $-0x100000000000000, %rcx # imm = 0xFF00000000000000
movq %rcx, 0x10(%rax)
movq -0x20(%rbp), %rax
movq 0x18(%rax), %rax
movq -0x20(%rbp), %rcx
cmpq 0x10(%rcx), %rax
jae 0xadb7
movq -0x20(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0x40(%rbp)
jmp 0xadc7
movq -0x20(%rbp), %rax
movq 0x10(%rax), %rax
subq $0x1, %rax
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x18(%rax)
movl -0x2c(%rbp), %eax
popq %rbp
retq
nopl (%rax,%rax)