// File: /workspace/github_c_makefile_star2_O1/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
movl $0x16, %r15d
jne 0xef51
movl %edx, %ebp
movq %rsi, %r14
movq %rdi, %rbx
cmpb $0x0, 0x20(%rdi)
je 0xed14
leaq 0x1590(%rbx), %rdi
callq 0x1160
movq $-0x1, (%rsp)
movl 0x64(%rbx), %ecx
movl 0x78(%rbx), %eax
andnl %ecx, %eax, %edx
xorl %esi, %esi
popcntl %edx, %esi
movsbl 0x544(%rbx), %edi
movl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
cmpl %edi, %esi
jg 0xed8a
movl 0x5c(%rbx), %esi
orl %ecx, %esi
testl %eax, %eax
je 0xed5b
movl %eax, %edi
andl %ecx, %edi
popcntl %edi, %edi
movsbl 0x954(%rbx), %r8d
cmpl %r8d, %edi
jg 0xed8a
movl %eax, %edi
notl %edi
andl %esi, %edi
popcntl %edi, %edi
movsbl 0x543(%rbx), %r8d
cmpl %r8d, %edi
jge 0xed8a
testl %eax, %eax
je 0xed87
andl %eax, %esi
popcntl %esi, %esi
movsbl 0x953(%rbx), %edi
cmpl %edi, %esi
jge 0xed8a
movl 0x58(%rbx), %edx
testl %ecx, %ecx
je 0xedc6
tzcntl %ecx, %esi
movl 0x3d0(%rbx,%rsi,4), %edi
cmpl %edx, %edi
jg 0xedbf
shrxl %esi, %eax, %r8d
andl $0x1, %r8d
cmpl %edx, %edi
jge 0xedb9
movl %r8d, %edx
xorl $0x1, %edx
movl $0xffffffff, (%rsp,%rdx,4) # imm = 0xFFFFFFFF
movl %edi, %edx
movl %r8d, %edi
movl %esi, (%rsp,%rdi,4)
blsrl %ecx, %ecx
jne 0xed8e
movslq (%rsp), %rcx
movl $0x2a, %r15d
testq %rcx, %rcx
js 0xef39
cmpl $0x7fffffff, 0x16d0(%rbx,%rcx,4) # imm = 0x7FFFFFFF
jne 0xef39
testl %eax, %eax
je 0xee0d
movslq 0x4(%rsp), %rax
testq %rax, %rax
js 0xef39
cmpl $0x7fffffff, 0x16d0(%rbx,%rax,4) # imm = 0x7FFFFFFF
jne 0xef39
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), %edx
movswl 0x512(%rbx), %esi
imull %eax, %esi
movb 0x508(%rbx), %dil
shlxl %edi, %edx, %edi
addl %esi, %edi
movb 0x530(%rbx), %sil
cmpb $0x3, %sil
setne %r8b
sarxl %r8d, %eax, %eax
cmpb $0x1, %sil
sete %sil
sarxl %esi, %edx, %edx
movswl 0x514(%rbx), %r8d
imull %r8d, %eax
addl 0x2c(%rbx), %eax
movb 0x509(%rbx), %sil
shlxl %esi, %edx, %r9d
addl %eax, %r9d
movl 0x64(%rbx), %edx
btcl %ecx, %edx
movl %edx, 0x64(%rbx)
movq 0x188(%rbx,%rcx,8), %r10
movslq %edi, %rsi
leaq (%r10,%rsi), %rax
movq %rax, (%r14)
movslq %r9d, %rax
addq %rax, %r10
movq %r10, 0x8(%r14)
shrl %r8d
movswq %r8w, %rdi
addq %rdi, %r10
movq %r10, 0x10(%r14)
movl 0xfc(%rbx,%rcx,4), %r8d
movl %r8d, 0x48(%r14)
movl $0x1, %r8d
shlxq %rcx, %r8, %rcx
movq %rcx, 0x58(%r14)
movslq 0x4(%rsp), %r8
testq %r8, %r8
js 0xef2b
btcl %r8d, %edx
movl %edx, 0x64(%rbx)
movq 0x188(%rbx,%r8,8), %rdx
addq %rdx, %rsi
movq %rsi, 0x18(%r14)
addq %rdx, %rax
movq %rax, 0x20(%r14)
addq %rdi, %rax
movq %rax, 0x28(%r14)
movl 0xfc(%rbx,%r8,4), %eax
movl %eax, 0x4c(%r14)
btsq %r8, %rcx
movq %rcx, 0x58(%r14)
xorl %r15d, %r15d
testl %ebp, %ebp
je 0xef39
movl 0x58(%r14), %eax
orl %eax, 0x68(%rbx)
cmpb $0x0, 0x20(%rbx)
je 0xef51
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