// File: /workspace/github_c_makefile_star2_O2/tvlabs[P]edge264/edge264_headers.c
// Function: parse_scaling_lists
static void parse_scaling_lists(Edge264Decoder *dec, i8x16 *w4x4, i8x16 *w8x8, int transform_8x8_mode_flag, int chroma_format_idc)
{
	i8x16 fb4x4 = *w4x4; // fall-back
	i8x16 d4x4 = Default_4x4_Intra; // for useDefaultScalingMatrixFlag
	for (int i = 0; i < 6; i++, w4x4++) {
		log_dec(dec, "    - [");
		if (i == 3) {
			fb4x4 = *w4x4;
			d4x4 = Default_4x4_Inter;
		}
		if (!get_u1(&dec->_gb)) { // scaling_list_present_flag
			*w4x4 = fb4x4;
		} else {
			unsigned nextScale = (8 + get_se16(&dec->_gb, -128, 127)) & 255;
			log_dec(dec, "%u", nextScale);
			if (nextScale == 0) {
				*w4x4 = fb4x4 = d4x4;
			} else {
				for (unsigned j = 0, lastScale;;) {
					((uint8_t *)w4x4)[((int8_t *)scan_4x4)[j]] = nextScale ?: lastScale;
					if (++j >= 16)
						break;
					if (nextScale != 0) {
						lastScale = nextScale;
						nextScale = (nextScale + get_se16(&dec->_gb, -128, 127)) & 255;
						log_dec(dec, ",%u", nextScale);
					}
				}
				fb4x4 = *w4x4;
			}
		}
		log_dec(dec, "]\n");
	}
	
	// For 8x8 scaling lists, we really have no better choice than pointers.
	if (!transform_8x8_mode_flag)
		return;
	for (int i = 0; i < (chroma_format_idc == 3 ? 6 : 2); i++, w8x8 += 4) {
		log_dec(dec, "    - [");
		if (!get_u1(&dec->_gb)) {
			if (i >= 2) {
				w8x8[0] = w8x8[-8];
				w8x8[1] = w8x8[-7];
				w8x8[2] = w8x8[-6];
				w8x8[3] = w8x8[-5];
			}
		} else {
			unsigned nextScale = (8 + get_se16(&dec->_gb, -128, 127)) & 255;
			log_dec(dec, "%u", nextScale);
			if (nextScale == 0) {
				const i8x16 *d8x8 = (i % 2 == 0) ? Default_8x8_Intra : Default_8x8_Inter;
				w8x8[0] = d8x8[0];
				w8x8[1] = d8x8[1];
				w8x8[2] = d8x8[2];
				w8x8[3] = d8x8[3];
			} else {
				for (unsigned j = 0, lastScale = 0;;) {
					((uint8_t *)w8x8)[((int8_t *)scan_8x8_cabac)[j]] = nextScale ?: lastScale;
					if (++j >= 64)
						break;
					if (nextScale != 0) {
						lastScale = nextScale;
						nextScale = (nextScale + get_se16(&dec->_gb, -128, 127)) & 255;
						log_dec(dec, ",%u", nextScale);
					}
				}
			}
		}
		log_dec(dec, "]\n");
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movl %r8d, 0x18(%rsp)
movl %ecx, 0x1c(%rsp)
movq %rdx, %r14
movq %rsi, %r12
movq %rdi, %rbx
vmovaps (%rsi), %xmm0
vmovaps %xmm0, 0x20(%rsp)
leaq 0x7cb2(%rdi), %rbp
vmovaps 0xd3bf(%rip), %xmm0     # 0x2d460
vmovaps %xmm0, 0x40(%rsp)
leaq 0xd0b2(%rip), %r15      # 0x2d160
xorl %ecx, %ecx
movq %rdi, 0x8(%rsp)
movq %rbp, 0x10(%rsp)
movq %rdx, 0x38(%rsp)
cmpl $0x6, %ecx
je 0x202a9
movzwl 0x7cb0(%rbx), %eax
cmpq $0x2541, %rax           # imm = 0x2541
ja 0x2010d
movq %rax, %rdi
addq %rbp, %rdi
movq %rbp, %r15
movl %ecx, %ebp
movl $0x2542, %ecx           # imm = 0x2542
subl %eax, %ecx
movzwl %cx, %esi
leaq 0x14a24(%rip), %rdx     # 0x34b17
xorl %eax, %eax
callq 0x1070
movl %ebp, %ecx
movq %r15, %rbp
leaq 0xd05a(%rip), %r15      # 0x2d160
addw %ax, 0x7cb0(%rbx)
movl %ecx, 0x4(%rsp)
cmpl $0x3, %ecx
jne 0x20130
vmovaps (%r12), %xmm0
vmovaps %xmm0, 0x20(%rsp)
vmovaps 0xd346(%rip), %xmm0     # 0x2d470
vmovaps %xmm0, 0x40(%rsp)
movq %rbx, %rdi
callq 0x1f662
testl %eax, %eax
je 0x2023d
movq %rbx, %rdi
pushq $-0x80
popq %rsi
pushq $0x7f
popq %rdx
callq 0x1f6a0
addl $0x8, %eax
movzbl %al, %r14d
movzwl 0x7cb0(%rbx), %eax
cmpq $0x2541, %rax           # imm = 0x2541
ja 0x2018c
movq %rax, %rdi
addq %rbp, %rdi
movl $0x2542, %ecx           # imm = 0x2542
subl %eax, %ecx
movzwl %cx, %esi
leaq 0x13934(%rip), %rdx     # 0x33aaf
movl %r14d, %ecx
xorl %eax, %eax
callq 0x1070
addw %ax, 0x7cb0(%rbx)
testl %r14d, %r14d
je 0x2024b
xorl %ebx, %ebx
testl %r14d, %r14d
movl %r14d, %eax
cmovel %r13d, %eax
movsbq (%rbx,%r15), %rcx
movb %al, (%r12,%rcx)
cmpq $0xf, %rbx
je 0x20220
movl %r14d, %ebp
incq %rbx
xorl %r14d, %r14d
testl %ebp, %ebp
je 0x20197
movq 0x8(%rsp), %r13
movq %r13, %rdi
pushq $-0x80
popq %rsi
pushq $0x7f
popq %rdx
callq 0x1f6a0
addl %ebp, %eax
movzbl %al, %r14d
movzwl 0x7cb0(%r13), %eax
movl %ebp, %r13d
cmpq $0x2542, %rax           # imm = 0x2542
jae 0x20197
movq %rax, %rdi
addq 0x10(%rsp), %rdi
movl $0x2542, %ecx           # imm = 0x2542
subl %eax, %ecx
movzwl %cx, %esi
leaq 0x1491d(%rip), %rdx     # 0x34b1f
movl %r14d, %ecx
xorl %eax, %eax
callq 0x1070
movq 0x8(%rsp), %rcx
addw %ax, 0x7cb0(%rcx)
movl %ebp, %r13d
jmp 0x20197
vmovaps (%r12), %xmm0
vmovaps %xmm0, 0x20(%rsp)
movq 0x38(%rsp), %r14
movq 0x8(%rsp), %rbx
movq 0x10(%rsp), %rbp
jmp 0x20262
vmovaps 0x20(%rsp), %xmm0
vmovaps %xmm0, (%r12)
jmp 0x20262
vmovaps 0x40(%rsp), %xmm0
vmovaps %xmm0, (%r12)
vmovaps %xmm0, 0x20(%rsp)
movq 0x38(%rsp), %r14
movl 0x4(%rsp), %ecx
movzwl 0x7cb0(%rbx), %eax
cmpq $0x2541, %rax           # imm = 0x2541
ja 0x2029e
movq %rax, %rdi
addq %rbp, %rdi
movl $0x2542, %ecx           # imm = 0x2542
subl %eax, %ecx
movzwl %cx, %esi
leaq 0x145f4(%rip), %rdx     # 0x34880
xorl %eax, %eax
callq 0x1070
movl 0x4(%rsp), %ecx
addw %ax, 0x7cb0(%rbx)
incl %ecx
addq $0x10, %r12
jmp 0x200bf
cmpl $0x0, 0x1c(%rsp)
je 0x204ee
xorl %eax, %eax
cmpl $0x3, 0x18(%rsp)
sete %al
leal 0x2(,%rax,4), %eax
movl %eax, 0x20(%rsp)
leaq 0x12450(%rip), %r12     # 0x32720
xorl %r13d, %r13d
cmpl 0x20(%rsp), %r13d
je 0x204ee
movzwl 0x7cb0(%rbx), %eax
cmpq $0x2541, %rax           # imm = 0x2541
ja 0x20315
movq %rax, %rdi
addq %rbp, %rdi
movl $0x2542, %ecx           # imm = 0x2542
subl %eax, %ecx
movzwl %cx, %esi
leaq 0x14813(%rip), %rdx     # 0x34b17
xorl %eax, %eax
vzeroupper
callq 0x1070
addw %ax, 0x7cb0(%rbx)
movq %rbx, %rdi
vzeroupper
callq 0x1f662
testl %eax, %eax
movl %r13d, 0x4(%rsp)
je 0x20415
movq %rbx, %rdi
pushq $-0x80
popq %rsi
pushq $0x7f
popq %rdx
callq 0x1f6a0
addl $0x8, %eax
movzbl %al, %r15d
movzwl 0x7cb0(%rbx), %eax
cmpq $0x2541, %rax           # imm = 0x2541
ja 0x20379
movq %rax, %rdi
addq %rbp, %rdi
movl $0x2542, %ecx           # imm = 0x2542
subl %eax, %ecx
movzwl %cx, %esi
leaq 0x13747(%rip), %rdx     # 0x33aaf
movl %r15d, %ecx
xorl %eax, %eax
callq 0x1070
addw %ax, 0x7cb0(%rbx)
testl %r15d, %r15d
je 0x2043e
xorl %r13d, %r13d
xorl %eax, %eax
testl %r15d, %r15d
movl %r15d, %ecx
cmovel %eax, %ecx
movsbq (%r13,%r12), %rdx
movb %cl, (%r14,%rdx)
cmpq $0x3f, %r13
je 0x204a1
movl %r15d, %ebx
incq %r13
xorl %r15d, %r15d
testl %ebx, %ebx
je 0x20387
movq 0x8(%rsp), %rbp
movq %rbp, %rdi
pushq $-0x80
popq %rsi
pushq $0x7f
popq %rdx
callq 0x1f6a0
addl %ebx, %eax
movzbl %al, %r15d
movzwl 0x7cb0(%rbp), %ecx
movq 0x10(%rsp), %rbp
movl %ebx, %eax
cmpq $0x2542, %rcx           # imm = 0x2542
jae 0x20387
movq %rcx, %rdi
addq %rbp, %rdi
movl $0x2542, %eax           # imm = 0x2542
subl %ecx, %eax
movzwl %ax, %esi
leaq 0x14727(%rip), %rdx     # 0x34b1f
movl %r15d, %ecx
xorl %eax, %eax
callq 0x1070
movq 0x8(%rsp), %rcx
addw %ax, 0x7cb0(%rcx)
movl %ebx, %eax
jmp 0x20387
cmpl $0x2, %r13d
jb 0x204a1
vmovaps -0x80(%r14), %xmm0
vmovaps %xmm0, (%r14)
vmovups -0x70(%r14), %ymm0
vmovups %ymm0, 0x10(%r14)
vmovaps -0x50(%r14), %xmm0
jmp 0x2049b
vmovaps 0xd03a(%rip), %xmm0     # 0x2d480
testb $0x1, %r13b
je 0x20454
vmovaps 0xd03c(%rip), %xmm0     # 0x2d490
vmovaps %xmm0, (%r14)
vmovaps 0xd03f(%rip), %xmm0     # 0x2d4a0
je 0x2046b
vmovaps 0xd045(%rip), %xmm0     # 0x2d4b0
vmovaps %xmm0, 0x10(%r14)
vmovaps 0xd047(%rip), %xmm0     # 0x2d4c0
je 0x20483
vmovaps 0xd04d(%rip), %xmm0     # 0x2d4d0
vmovaps %xmm0, 0x20(%r14)
vmovaps 0xd04f(%rip), %xmm0     # 0x2d4e0
je 0x2049b
vmovaps 0xd055(%rip), %xmm0     # 0x2d4f0
vmovaps %xmm0, 0x30(%r14)
movq 0x8(%rsp), %rbx
movzwl 0x7cb0(%rbx), %eax
cmpq $0x2541, %rax           # imm = 0x2541
ja 0x204dd
movq %rax, %rdi
addq %rbp, %rdi
movl $0x2542, %ecx           # imm = 0x2542
subl %eax, %ecx
movzwl %cx, %esi
leaq 0x143b4(%rip), %rdx     # 0x34880
xorl %eax, %eax
vzeroupper
callq 0x1070
addw %ax, 0x7cb0(%rbx)
movl 0x4(%rsp), %r13d
incl %r13d
addq $0x40, %r14
jmp 0x202d3
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
vzeroupper
retq