// File: /workspace/github_c_makefile_star2_O0/tvlabs[P]edge264/edge264_headers.c
// Function: parse_dec_ref_pic_marking
static void parse_dec_ref_pic_marking(Edge264Decoder *dec, Edge264SeqParameterSet *sps)
{
	static const char * const mmco_names[6] = {
		"    - {idc: 1, sref: %u} # dereference\n",
		"    - {idc: 2, lref: %2$u} # dereference\n",
		"    - {idc: 3, sref: %u, lref: %u} # convert\n",
		"    - {idc: 4, lref: %2$d} # dereference on and above\n",
		"    - {idc: 5} # dereference all\n",
		"    - {idc: 6, lref: %2$u} # convert current\n"};
	
	// while the exact release time of non-ref frames in C.4.5.2 is ambiguous, we ignore no_output_of_prior_pics_flag
	if (dec->IdrPicFlag) {
		int no_output_of_prior_pics_flag = get_u1(&dec->_gb);
		dec->pic_reference_flags = 1 << dec->currPic;
		dec->pic_long_term_flags = get_u1(&dec->_gb) << dec->currPic;
		dec->pic_LongTermFrameIdx_v[0] = dec->pic_LongTermFrameIdx_v[1] = (i8x16){};
		log_dec(dec, "  no_output_of_prior_pics_flag: %d\n"
			"  long_term_reference_flag: %d\n",
			no_output_of_prior_pics_flag,
			dec->pic_long_term_flags >> dec->currPic);
		return;
	}
	
	// 8.2.5.4 - Adaptive memory control marking process.
	int memory_management_control_operation;
	int i = 32;
	if (get_u1(&dec->_gb)) {
		log_dec(dec, "  memory_management_control_operations:\n");
		while ((memory_management_control_operation = get_ue16(&dec->_gb, 6)) != 0 && i-- > 0) {
			// FIXME should do nothing if target does not exist
			int target = dec->currPic, FrameNum = 0, long_term_frame_idx = 0;
			if (10 & 1 << memory_management_control_operation) { // 1 or 3
				FrameNum = dec->FrameNum - 1 - get_ue32(&dec->_gb, 4294967294);
				for (unsigned r = dec->pic_reference_flags & ~dec->pic_long_term_flags; r; r &= r - 1) {
					int j = __builtin_ctz(r);
					if (dec->FrameNums[j] == FrameNum) {
						target = j;
						if (memory_management_control_operation == 1) {
							dec->pic_reference_flags ^= 1 << j;
							dec->pic_long_term_flags &= ~(1 << j);
						}
					}
				}
			}
			if (92 & 1 << memory_management_control_operation) { // 2 or 3 or 4 or 6
				long_term_frame_idx = get_ue16(&dec->_gb, sps->max_num_ref_frames - (memory_management_control_operation != 4));
				for (unsigned r = dec->pic_long_term_flags; r; r &= r - 1) {
					int j = __builtin_ctz(r);
					if (dec->pic_LongTermFrameIdx[j] == long_term_frame_idx ||
						(dec->pic_LongTermFrameIdx[j] > long_term_frame_idx &&
						memory_management_control_operation == 4)) {
						dec->pic_reference_flags ^= 1 << j;
						dec->pic_long_term_flags ^= 1 << j;
					}
				}
				if (72 & 1 << memory_management_control_operation) { // 3 or 6
					dec->pic_LongTermFrameIdx[target] = long_term_frame_idx;
					dec->pic_long_term_flags |= 1 << target;
				}
			}
			if (memory_management_control_operation == 5) {
				dec->IdrPicFlag = 1;
				dec->pic_reference_flags = 0;
				dec->FrameNums[dec->currPic] = 0;
				dec->pic_long_term_flags = 0;
				dec->pic_LongTermFrameIdx_v[0] = dec->pic_LongTermFrameIdx_v[1] = (i8x16){};
				int tempPicOrderCnt = minw(dec->TopFieldOrderCnt, dec->BottomFieldOrderCnt);
				dec->FieldOrderCnt[0][dec->currPic] = dec->TopFieldOrderCnt - tempPicOrderCnt;
				dec->FieldOrderCnt[1][dec->currPic] = dec->BottomFieldOrderCnt - tempPicOrderCnt;
			}
			log_dec(dec, mmco_names[memory_management_control_operation - 1],
				FrameNum, long_term_frame_idx);
		}
	}
	
	// 8.2.5.3 - Sliding window marking process
	if (__builtin_popcount(dec->pic_reference_flags) >= sps->max_num_ref_frames) {
		int best = INT_MAX;
		int next = 0;
		for (unsigned r = dec->pic_reference_flags ^ dec->pic_long_term_flags; r != 0; r &= r - 1) {
			int i = __builtin_ctz(r);
			if (best > dec->FrameNums[i])
				best = dec->FrameNums[next = i];
		}
		dec->pic_reference_flags ^= 1 << next;
	}
	dec->pic_reference_flags |= 1 << dec->currPic;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xa0, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq -0x10(%rbp), %rax
cmpb $0x0, 0x23(%rax)
je 0x780a5
movq -0x10(%rbp), %rdi
callq 0x77060
movl %eax, -0x1c(%rbp)
movq -0x10(%rbp), %rax
movb 0x24(%rax), %cl
movb %cl, %dl
movl $0x1, %ecx
shlxl %edx, %ecx, %ecx
movl %ecx, 0x60(%rax)
movq -0x10(%rbp), %rdi
callq 0x77060
movl %eax, %ecx
movq -0x10(%rbp), %rax
movb 0x24(%rax), %sil
movb %sil, %dl
shlxl %edx, %ecx, %ecx
movl %ecx, 0x64(%rax)
vpxor %xmm0, %xmm0, %xmm0
vmovdqa %xmm0, -0x30(%rbp)
vmovdqa -0x30(%rbp), %xmm0
movq -0x10(%rbp), %rax
vmovdqa %xmm0, 0x3b0(%rax)
movq -0x10(%rbp), %rax
vmovdqa %xmm0, 0x3a0(%rax)
movq -0x10(%rbp), %rax
movswq 0x7ca0(%rax), %rax
cmpq $0x2542, %rax           # imm = 0x2542
jae 0x780a0
movq -0x10(%rbp), %rdi
addq $0x7ca2, %rdi           # imm = 0x7CA2
movq -0x10(%rbp), %rax
movswl 0x7ca0(%rax), %eax
cltq
addq %rax, %rdi
movq -0x10(%rbp), %rax
movswq 0x7ca0(%rax), %rax
movl $0x2542, %esi           # imm = 0x2542
subq %rax, %rsi
movl -0x1c(%rbp), %eax
movl %eax, -0x88(%rbp)
movq -0x10(%rbp), %rax
movl 0x64(%rax), %r8d
movq -0x10(%rbp), %rax
movsbl 0x24(%rax), %ecx
shrl %cl, %r8d
movl -0x88(%rbp), %ecx
leaq 0x58c4c(%rip), %rdx     # 0xd0ccf
movb $0x0, %al
callq 0x7070
movl %eax, %edx
movq -0x10(%rbp), %rax
movswl 0x7ca0(%rax), %ecx
addl %edx, %ecx
movw %cx, 0x7ca0(%rax)
jmp 0x7855b
movl $0x20, -0x38(%rbp)
movq -0x10(%rbp), %rdi
callq 0x77060
cmpl $0x0, %eax
je 0x784a2
movq -0x10(%rbp), %rax
movswq 0x7ca0(%rax), %rax
cmpq $0x2542, %rax           # imm = 0x2542
jae 0x78125
movq -0x10(%rbp), %rdi
addq $0x7ca2, %rdi           # imm = 0x7CA2
movq -0x10(%rbp), %rax
movswl 0x7ca0(%rax), %eax
cltq
addq %rax, %rdi
movq -0x10(%rbp), %rax
movswq 0x7ca0(%rax), %rax
movl $0x2542, %esi           # imm = 0x2542
subq %rax, %rsi
leaq 0x58c0a(%rip), %rdx     # 0xd0d12
movb $0x0, %al
callq 0x7070
movl %eax, %edx
movq -0x10(%rbp), %rax
movswl 0x7ca0(%rax), %ecx
addl %edx, %ecx
movw %cx, 0x7ca0(%rax)
jmp 0x78127
movq -0x10(%rbp), %rdi
movl $0x6, %esi
callq 0x76d10
movl %eax, %ecx
movl %ecx, -0x34(%rbp)
xorl %eax, %eax
cmpl $0x0, %ecx
movb %al, -0x89(%rbp)
je 0x7815e
movl -0x38(%rbp), %eax
movl %eax, %ecx
addl $-0x1, %ecx
movl %ecx, -0x38(%rbp)
cmpl $0x0, %eax
setg %al
movb %al, -0x89(%rbp)
movb -0x89(%rbp), %al
testb $0x1, %al
jne 0x7816d
jmp 0x784a0
movq -0x10(%rbp), %rax
movsbl 0x24(%rax), %eax
movl %eax, -0x3c(%rbp)
movl $0x0, -0x40(%rbp)
movl $0x0, -0x44(%rbp)
movl -0x34(%rbp), %ecx
movl $0x1, %eax
shll %cl, %eax
andl $0xa, %eax
cmpl $0x0, %eax
je 0x7824d
movq -0x10(%rbp), %rax
movl 0x38(%rax), %eax
subl $0x1, %eax
movl %eax, -0x90(%rbp)
movq -0x10(%rbp), %rdi
movl $0xfffffffe, %esi       # imm = 0xFFFFFFFE
callq 0x76d10
movl %eax, %ecx
movl -0x90(%rbp), %eax
subl %ecx, %eax
movl %eax, -0x40(%rbp)
movq -0x10(%rbp), %rax
movl 0x60(%rax), %eax
movq -0x10(%rbp), %rcx
movl 0x64(%rcx), %ecx
xorl $-0x1, %ecx
andl %ecx, %eax
movl %eax, -0x48(%rbp)
cmpl $0x0, -0x48(%rbp)
je 0x7824b
movl -0x48(%rbp), %eax
tzcntl %eax, %eax
movl %eax, -0x4c(%rbp)
movq -0x10(%rbp), %rax
movslq -0x4c(%rbp), %rcx
movl 0x6c(%rax,%rcx,4), %eax
cmpl -0x40(%rbp), %eax
jne 0x7823b
movl -0x4c(%rbp), %eax
movl %eax, -0x3c(%rbp)
cmpl $0x1, -0x34(%rbp)
jne 0x78239
movl -0x4c(%rbp), %ecx
movl $0x1, %eax
shll %cl, %eax
movl %eax, %ecx
movq -0x10(%rbp), %rax
xorl 0x60(%rax), %ecx
movl %ecx, 0x60(%rax)
movl -0x4c(%rbp), %ecx
movl $0x1, %eax
shll %cl, %eax
movl %eax, %ecx
xorl $-0x1, %ecx
movq -0x10(%rbp), %rax
andl 0x64(%rax), %ecx
movl %ecx, 0x64(%rax)
jmp 0x7823b
jmp 0x7823d
movl -0x48(%rbp), %eax
subl $0x1, %eax
andl -0x48(%rbp), %eax
movl %eax, -0x48(%rbp)
jmp 0x781dd
jmp 0x7824d
movl -0x34(%rbp), %ecx
movl $0x1, %eax
shll %cl, %eax
andl $0x5c, %eax
cmpl $0x0, %eax
je 0x78349
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rax
movsbl 0xf(%rax), %esi
cmpl $0x4, -0x34(%rbp)
setne %al
andb $0x1, %al
movzbl %al, %eax
subl %eax, %esi
callq 0x76d10
movl %eax, -0x44(%rbp)
movq -0x10(%rbp), %rax
movl 0x64(%rax), %eax
movl %eax, -0x50(%rbp)
cmpl $0x0, -0x50(%rbp)
je 0x7830b
movl -0x50(%rbp), %eax
tzcntl %eax, %eax
movl %eax, -0x54(%rbp)
movq -0x10(%rbp), %rax
movslq -0x54(%rbp), %rcx
movsbl 0x3a0(%rax,%rcx), %eax
cmpl -0x44(%rbp), %eax
je 0x782cf
movq -0x10(%rbp), %rax
movslq -0x54(%rbp), %rcx
movsbl 0x3a0(%rax,%rcx), %eax
cmpl -0x44(%rbp), %eax
jle 0x782fb
cmpl $0x4, -0x34(%rbp)
jne 0x782fb
movl -0x54(%rbp), %ecx
movl $0x1, %eax
shll %cl, %eax
movl %eax, %ecx
movq -0x10(%rbp), %rax
xorl 0x60(%rax), %ecx
movl %ecx, 0x60(%rax)
movl -0x54(%rbp), %ecx
movl $0x1, %eax
shll %cl, %eax
movl %eax, %ecx
movq -0x10(%rbp), %rax
xorl 0x64(%rax), %ecx
movl %ecx, 0x64(%rax)
jmp 0x782fd
movl -0x50(%rbp), %eax
subl $0x1, %eax
andl -0x50(%rbp), %eax
movl %eax, -0x50(%rbp)
jmp 0x7828f
movl -0x34(%rbp), %ecx
movl $0x1, %eax
shll %cl, %eax
andl $0x48, %eax
cmpl $0x0, %eax
je 0x78347
movl -0x44(%rbp), %eax
movb %al, %dl
movq -0x10(%rbp), %rax
movslq -0x3c(%rbp), %rcx
movb %dl, 0x3a0(%rax,%rcx)
movl -0x3c(%rbp), %ecx
movl $0x1, %eax
shll %cl, %eax
movl %eax, %ecx
movq -0x10(%rbp), %rax
orl 0x64(%rax), %ecx
movl %ecx, 0x64(%rax)
jmp 0x78349
cmpl $0x5, -0x34(%rbp)
jne 0x78420
movq -0x10(%rbp), %rax
movb $0x1, 0x23(%rax)
movq -0x10(%rbp), %rax
movl $0x0, 0x60(%rax)
movq -0x10(%rbp), %rax
movsbq 0x24(%rax), %rcx
movl $0x0, 0x6c(%rax,%rcx,4)
movq -0x10(%rbp), %rax
movl $0x0, 0x64(%rax)
vpxor %xmm0, %xmm0, %xmm0
vmovdqa %xmm0, -0x70(%rbp)
vmovdqa -0x70(%rbp), %xmm0
movq -0x10(%rbp), %rax
vmovdqa %xmm0, 0x3b0(%rax)
movq -0x10(%rbp), %rax
vmovdqa %xmm0, 0x3a0(%rax)
movq -0x10(%rbp), %rax
movl 0x3c(%rax), %ecx
movq -0x10(%rbp), %rax
movl 0x40(%rax), %eax
movl %ecx, -0x4(%rbp)
movl %eax, -0x8(%rbp)
movl -0x4(%rbp), %eax
subl -0x8(%rbp), %eax
cmpl $0x0, %eax
jge 0x783d2
movl -0x4(%rbp), %eax
movl %eax, -0x94(%rbp)
jmp 0x783db
movl -0x8(%rbp), %eax
movl %eax, -0x94(%rbp)
movl -0x94(%rbp), %eax
movl %eax, -0x74(%rbp)
movq -0x10(%rbp), %rax
movl 0x3c(%rax), %edx
subl -0x74(%rbp), %edx
movq -0x10(%rbp), %rax
movq -0x10(%rbp), %rcx
movsbq 0x24(%rcx), %rcx
movl %edx, 0x3c0(%rax,%rcx,4)
movq -0x10(%rbp), %rax
movl 0x40(%rax), %edx
subl -0x74(%rbp), %edx
movq -0x10(%rbp), %rax
movq -0x10(%rbp), %rcx
movsbq 0x24(%rcx), %rcx
movl %edx, 0x440(%rax,%rcx,4)
movq -0x10(%rbp), %rax
movswq 0x7ca0(%rax), %rax
cmpq $0x2542, %rax           # imm = 0x2542
jae 0x7849b
movq -0x10(%rbp), %rdi
addq $0x7ca2, %rdi           # imm = 0x7CA2
movq -0x10(%rbp), %rax
movswl 0x7ca0(%rax), %eax
cltq
addq %rax, %rdi
movq -0x10(%rbp), %rax
movswq 0x7ca0(%rax), %rax
movl $0x2542, %esi           # imm = 0x2542
subq %rax, %rsi
movl -0x34(%rbp), %eax
subl $0x1, %eax
movslq %eax, %rcx
leaq 0x5e6dd(%rip), %rax     # 0xd6b50
movq (%rax,%rcx,8), %rdx
movl -0x40(%rbp), %ecx
movl -0x44(%rbp), %r8d
movb $0x0, %al
callq 0x7070
movl %eax, %edx
movq -0x10(%rbp), %rax
movswl 0x7ca0(%rax), %ecx
addl %edx, %ecx
movw %cx, 0x7ca0(%rax)
jmp 0x78127
jmp 0x784a2
movq -0x10(%rbp), %rax
movl 0x60(%rax), %eax
popcntl %eax, %eax
movq -0x18(%rbp), %rcx
movsbl 0xf(%rcx), %ecx
cmpl %ecx, %eax
jl 0x78540
movl $0x7fffffff, -0x78(%rbp) # imm = 0x7FFFFFFF
movl $0x0, -0x7c(%rbp)
movq -0x10(%rbp), %rax
movl 0x60(%rax), %eax
movq -0x10(%rbp), %rcx
xorl 0x64(%rcx), %eax
movl %eax, -0x80(%rbp)
cmpl $0x0, -0x80(%rbp)
je 0x7852a
movl -0x80(%rbp), %eax
tzcntl %eax, %eax
movl %eax, -0x84(%rbp)
movl -0x78(%rbp), %eax
movq -0x10(%rbp), %rcx
movslq -0x84(%rbp), %rdx
cmpl 0x6c(%rcx,%rdx,4), %eax
jle 0x7851a
movq -0x10(%rbp), %rax
movl -0x84(%rbp), %ecx
movl %ecx, -0x7c(%rbp)
movslq %ecx, %rcx
movl 0x6c(%rax,%rcx,4), %eax
movl %eax, -0x78(%rbp)
jmp 0x7851c
movl -0x80(%rbp), %eax
subl $0x1, %eax
andl -0x80(%rbp), %eax
movl %eax, -0x80(%rbp)
jmp 0x784dc
movl -0x7c(%rbp), %ecx
movl $0x1, %eax
shll %cl, %eax
movl %eax, %ecx
movq -0x10(%rbp), %rax
xorl 0x60(%rax), %ecx
movl %ecx, 0x60(%rax)
movq -0x10(%rbp), %rax
movsbl 0x24(%rax), %ecx
movl $0x1, %eax
shll %cl, %eax
movl %eax, %ecx
movq -0x10(%rbp), %rax
orl 0x60(%rax), %ecx
movl %ecx, 0x60(%rax)
addq $0xa0, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)