// File: /workspace/github_c_makefile_star2_O2/tvlabs[P]edge264/edge264.c
// Function: edge264_get_frame
int edge264_get_frame(Edge264Decoder *dec, Edge264Frame *out, int borrow) {
	if (dec == NULL || out == NULL)
		return EINVAL;
	if (dec->n_threads)
		pthread_mutex_lock(&dec->lock);
	int pic[2] = {-1, -1};
	unsigned used = dec->reference_flags | dec->output_flags;
	int best = (__builtin_popcount(dec->output_flags & ~dec->non_base_views) > dec->sps.max_num_reorder_frames ||
		(dec->non_base_views && __builtin_popcount(dec->output_flags & dec->non_base_views) > dec->ssps.max_num_reorder_frames) ||
		__builtin_popcount(used & ~dec->non_base_views) >= dec->sps.num_frame_buffers ||
		(dec->non_base_views && __builtin_popcount(used & dec->non_base_views) >= dec->ssps.num_frame_buffers)) ? INT_MAX : dec->dispTopFieldOrderCnt;
	for (int o = dec->output_flags; o != 0; o &= o - 1) {
		int i = __builtin_ctz(o);
		if (dec->FieldOrderCnt[0][i] <= best) {
			int non_base = dec->non_base_views >> i & 1;
			if (dec->FieldOrderCnt[0][i] < best) {
				best = dec->FieldOrderCnt[0][i];
				pic[non_base ^ 1] = -1;
			}
			pic[non_base] = i;
		}
	}
	
	int res = ENOMSG;
	if (pic[0] >= 0 && dec->next_deblock_addr[pic[0]] == INT_MAX && (!dec->non_base_views || (pic[1] >= 0 && dec->next_deblock_addr[pic[1]] == INT_MAX))) {
		*out = dec->out;
		int top = dec->out.frame_crop_offsets[0];
		int left = dec->out.frame_crop_offsets[3];
		int offY = top * dec->out.stride_Y + (left << dec->out.pixel_depth_Y);
		int topC = dec->sps.chroma_format_idc == 3 ? top : top >> 1;
		int leftC = dec->sps.chroma_format_idc == 1 ? left >> 1 : left;
		int offC = dec->plane_size_Y + topC * dec->out.stride_C + (leftC << dec->out.pixel_depth_C);
		dec->output_flags ^= 1 << pic[0];
		const uint8_t *samples = dec->frame_buffers[pic[0]];
		out->samples[0] = samples + offY;
		out->samples[1] = samples + offC;
		out->samples[2] = samples + (dec->out.stride_C >> 1) + offC;
		out->FrameId = dec->FrameIds[pic[0]];
		out->return_arg = (void *)((size_t)1 << pic[0]);
		if (pic[1] >= 0) {
			dec->output_flags ^= 1 << pic[1];
			samples = dec->frame_buffers[pic[1]];
			out->samples_mvc[0] = samples + offY;
			out->samples_mvc[1] = samples + offC;
			out->samples_mvc[2] = samples + (dec->out.stride_C >> 1) + offC;
			out->FrameId_mvc = dec->FrameIds[pic[1]];
			out->return_arg = (void *)((size_t)1 << pic[0] | (size_t)1 << pic[1]);
		}
		res = 0;
		if (borrow)
			dec->borrow_flags |= (size_t)out->return_arg;
	}
	if (dec->n_threads)
		pthread_mutex_unlock(&dec->lock);
	return res;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %rbx
pushq %rax
testq %rdi, %rdi
sete %al
testq %rsi, %rsi
sete %cl
orb %al, %cl
je 0xb89f
pushq $0x16
popq %r15
jmp 0xbade
movl %edx, %ebp
movq %rsi, %r14
movq %rdi, %rbx
cmpb $0x0, 0x20(%rdi)
je 0xb8b9
leaq 0x1590(%rbx), %rdi
callq 0x1160
orq $-0x1, (%rsp)
movl 0x64(%rbx), %ecx
movl 0x78(%rbx), %eax
andnl %ecx, %eax, %edx
popcntl %edx, %esi
movsbl 0x544(%rbx), %edi
movl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
cmpl %edi, %esi
jg 0xb963
movl 0x5c(%rbx), %esi
orl %ecx, %esi
testl %eax, %eax
je 0xb8ff
movl %eax, %edi
andl %ecx, %edi
popcntl %edi, %edi
movsbl 0x954(%rbx), %r8d
cmpl %r8d, %edi
jg 0xb963
movl %eax, %edi
notl %edi
andl %esi, %edi
popcntl %edi, %edi
movsbl 0x543(%rbx), %r8d
cmpl %r8d, %edi
jge 0xb963
testl %eax, %eax
je 0xb92b
andl %eax, %esi
popcntl %esi, %esi
movsbl 0x953(%rbx), %edi
cmpl %edi, %esi
jge 0xb963
movl 0x58(%rbx), %edx
jmp 0xb963
tzcntl %ecx, %esi
movl 0x3d0(%rbx,%rsi,4), %edi
cmpl %edx, %edi
jg 0xb95e
shrxl %esi, %eax, %r8d
andl $0x1, %r8d
cmpl %edx, %edi
jge 0xb958
movl %r8d, %edx
xorl $0x1, %edx
orl $-0x1, (%rsp,%rdx,4)
movl %edi, %edx
movl %r8d, %edi
movl %esi, (%rsp,%rdi,4)
blsrl %ecx, %ecx
testl %ecx, %ecx
jne 0xb930
movslq (%rsp), %rdx
pushq $0x2a
popq %r15
testq %rdx, %rdx
js 0xbac6
cmpl $0x7fffffff, 0x16d0(%rbx,%rdx,4) # imm = 0x7FFFFFFF
jne 0xbac6
movl 0x4(%rsp), %ecx
testl %eax, %eax
je 0xb9aa
testl %ecx, %ecx
js 0xbac6
cmpl $0x7fffffff, 0x16d0(%rbx,%rcx,4) # imm = 0x7FFFFFFF
jne 0xbac6
vmovups 0x4d0(%rbx), %ymm0
vmovups 0x4f0(%rbx), %ymm1
vmovups 0x510(%rbx), %ymm2
vmovups %ymm2, 0x40(%r14)
vmovups %ymm1, 0x20(%r14)
vmovups %ymm0, (%r14)
movswl 0x520(%rbx), %eax
movswl 0x526(%rbx), %esi
movswl 0x512(%rbx), %edi
imull %eax, %edi
movb 0x508(%rbx), %r8b
shlxl %r8d, %esi, %r8d
addl %edi, %r8d
movb 0x530(%rbx), %dil
cmpb $0x3, %dil
setne %r9b
sarxl %r9d, %eax, %eax
cmpb $0x1, %dil
sete %dil
movswl 0x514(%rbx), %r9d
imull %r9d, %eax
addl 0x2c(%rbx), %eax
sarxl %edi, %esi, %esi
movb 0x509(%rbx), %dil
shlxl %edi, %esi, %esi
addl %eax, %esi
movl 0x64(%rbx), %edi
btcl %edx, %edi
movl %edi, 0x64(%rbx)
movq 0x188(%rbx,%rdx,8), %rax
movslq %r8d, %r8
leaq (%rax,%r8), %r10
movq %r10, (%r14)
movslq %esi, %rsi
addq %rsi, %rax
movq %rax, 0x8(%r14)
shrl %r9d
movswq %r9w, %r9
addq %r9, %rax
movq %rax, 0x10(%r14)
movl 0xfc(%rbx,%rdx,4), %eax
movl %eax, 0x48(%r14)
pushq $0x1
popq %rax
shlxq %rdx, %rax, %rax
movq %rax, 0x58(%r14)
testl %ecx, %ecx
js 0xbabc
btcl %ecx, %edi
movl %edi, 0x64(%rbx)
movq 0x188(%rbx,%rcx,8), %rdx
addq %rdx, %r8
movq %r8, 0x18(%r14)
addq %rdx, %rsi
movq %rsi, 0x20(%r14)
addq %r9, %rsi
movq %rsi, 0x28(%r14)
movl 0xfc(%rbx,%rcx,4), %edx
movl %edx, 0x4c(%r14)
btsq %rcx, %rax
movq %rax, 0x58(%r14)
xorl %r15d, %r15d
testl %ebp, %ebp
je 0xbac6
orl %eax, 0x68(%rbx)
cmpb $0x0, 0x20(%rbx)
je 0xbade
addq $0x1590, %rbx           # imm = 0x1590
movq %rbx, %rdi
vzeroupper
callq 0x10e0
movl %r15d, %eax
addq $0x8, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
vzeroupper
retq