// File: /workspace/github_c_makefile_star2_O3/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 0xf2b6
movl %edx, %ebp
movq %rsi, %r14
movq %rdi, %rbx
cmpb $0x0, 0x20(%rdi)
je 0xf07c
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 0xf0fb
movl 0x5c(%rbx), %esi
orl %ecx, %esi
testl %eax, %eax
je 0xf0e9
movl %eax, %edi
andl %ecx, %edi
popcntl %edi, %edi
movsbl 0x954(%rbx), %r8d
cmpl %r8d, %edi
jg 0xf0fb
movl %eax, %edi
notl %edi
andl %esi, %edi
popcntl %edi, %edi
movsbl 0x543(%rbx), %r8d
cmpl %r8d, %edi
jge 0xf0fb
andl %eax, %esi
popcntl %esi, %esi
movsbl 0x953(%rbx), %edi
jmp 0xf0f4
popcntl %esi, %esi
movsbl 0x543(%rbx), %edi
cmpl %edi, %esi
jge 0xf0fb
movl 0x58(%rbx), %edx
movl $0x2a, %r15d
testl %ecx, %ecx
je 0xf29e
tzcntl %ecx, %esi
movl 0x3d0(%rbx,%rsi,4), %edi
cmpl %edx, %edi
jg 0xf13a
shrxl %esi, %eax, %r8d
andl $0x1, %r8d
cmpl %edx, %edi
jge 0xf134
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 0xf109
movslq (%rsp), %rdx
testq %rdx, %rdx
js 0xf29e
cmpl $0x7fffffff, 0x16d0(%rbx,%rdx,4) # imm = 0x7FFFFFFF
jne 0xf29e
movl 0x4(%rsp), %ecx
testl %eax, %eax
je 0xf180
testl %ecx, %ecx
js 0xf29e
cmpl $0x7fffffff, 0x16d0(%rbx,%rcx,4) # imm = 0x7FFFFFFF
jne 0xf29e
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)
movl $0x1, %eax
shlxq %rdx, %rax, %rax
movq %rax, 0x58(%r14)
testl %ecx, %ecx
js 0xf294
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 0xf29e
orl %eax, 0x68(%rbx)
cmpb $0x0, 0x20(%rbx)
je 0xf2b6
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