// File: /workspace/github_c_makefile_star2_O2/tvlabs[P]edge264/edge264.c
// Function: edge264_decode_NAL
int edge264_decode_NAL(Edge264Decoder *dec, const uint8_t *buf, const uint8_t *end, int non_blocking, void(*free_cb)(void*,int), void *free_arg, const uint8_t **next_NAL)
{
	static const char * const nal_unit_type_names[32] = {
		[0 ... 31] = "Unknown",
		[1] = "Coded slice of a non-IDR picture",
		[2] = "Coded slice data partition A",
		[3] = "Coded slice data partition B",
		[4] = "Coded slice data partition C",
		[5] = "Coded slice of an IDR picture",
		[6] = "Supplemental enhancement information (SEI)",
		[7] = "Sequence parameter set",
		[8] = "Picture parameter set",
		[9] = "Access unit delimiter",
		[10] = "End of sequence",
		[11] = "End of stream",
		[12] = "Filler data",
		[13] = "Sequence parameter set extension",
		[14] = "Prefix NAL unit",
		[15] = "Subset sequence parameter set",
		[16] = "Depth parameter set",
		[19] = "Coded slice of an auxiliary coded picture without partitioning",
		[20] = "Coded slice extension",
		[21] = "Coded slice extension for a depth view component or a 3D-AVC texture view component",
	};
	
	// initial checks before parsing
	if (dec == NULL || (buf == NULL && end != NULL))
		return EINVAL;
	if (dec->n_threads)
		pthread_mutex_lock(&dec->lock);
	if (__builtin_expect((intptr_t)(end - buf) <= 0, 0)) {
		for (unsigned o = dec->output_flags; o; o &= o - 1)
			dec->dispTopFieldOrderCnt = max(dec->dispTopFieldOrderCnt, dec->FieldOrderCnt[0][__builtin_ctz(o)]);
		unsigned busy;
		while ((busy = dec->busy_tasks) && !non_blocking)
			pthread_cond_wait(&dec->task_complete, &dec->lock);
		if (dec->n_threads)
			pthread_mutex_unlock(&dec->lock);
		return busy ? EWOULDBLOCK : ENODATA;
	}
	dec->nal_ref_idc = buf[0] >> 5;
	dec->nal_unit_type = buf[0] & 0x1f;
	Parser parser = dec->parse_nal_unit[dec->nal_unit_type];
	if (dec->log_arg) { // no need to test log_cb since harmless otherwise
		dec->log_pos = snprintf(dec->log_buf, sizeof(dec->log_buf),
			"\n- nal_ref_idc: %u\n"
			"  nal_unit_type: %u # %s%s\n",
			dec->nal_ref_idc,
			dec->nal_unit_type, nal_unit_type_names[dec->nal_unit_type], unsup_if(!parser));
	}
	
	// initialize the parsing context if we can parse the current NAL
	int ret = ENOTSUP;
	if (parser != NULL) {
		// prefill the bitstream cache
		if ((intptr_t)(end - buf) >= 2) {
			dec->_gb.msb_cache = (size_t)buf[1] << (SIZE_BIT - 8) | (size_t)1 << (SIZE_BIT - 9);
			dec->_gb.CPB = buf + 2;
		} else {
			dec->_gb.msb_cache = (size_t)1 << (SIZE_BIT - 1);
			dec->_gb.CPB = buf + 1;
		}
		dec->_gb.lsb_cache = 0;
		dec->_gb.end = end;
		refill(&dec->_gb, 0);
		ret = parser(dec, non_blocking, free_cb, free_arg);
		// end may have been set to the next start code thanks to escape code detection in get_bytes
		buf = minp(dec->_gb.CPB - 2, dec->_gb.end);
	} else if (dec->log_arg) {
		dec->log_pos = 0;
		dec->log_cb(dec->log_buf, dec->log_arg);
	}
	// printf("nal_unit_type=%d, ret=%d\n\n", dec->nal_unit_type, ret);
	
	// for 0, ENOTSUP and EBADMSG we may free or advance the buffer pointer
	if (ret == 0 || ret == ENOTSUP || ret == EBADMSG) {
		if (free_cb && !(ret == 0 && 1048610 & 1 << dec->nal_unit_type)) // 1, 5 or 20
			free_cb(free_arg, ret);
		if (next_NAL)
			*next_NAL = edge264_find_start_code(buf, end, 0) + 3;
	}
	if (dec->n_threads)
		pthread_mutex_unlock(&dec->lock);
	return ret;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
pushq $0x16
popq %r12
testq %rdi, %rdi
je 0xb7ad
movq %rdx, %rbp
movq %rsi, %r13
testq %rsi, %rsi
sete %al
testq %rdx, %rdx
setne %dl
testb %dl, %al
jne 0xb7ad
movq %r8, %r15
movq %rdi, %rbx
movl %ecx, 0xc(%rsp)
movq %r9, 0x10(%rsp)
cmpb $0x0, 0x20(%rdi)
je 0xb632
leaq 0x1590(%rbx), %rdi
callq 0x1160
movq %rbp, %r14
subq %r13, %r14
testq %r14, %r14
jle 0xb7d2
movzbl (%r13), %ecx
shrl $0x5, %ecx
movb %cl, 0x21(%rbx)
movzbl (%r13), %eax
andl $0x1f, %eax
movb %al, 0x22(%rbx)
movl %eax, %edx
movq 0x288(%rbx,%rdx,8), %r12
cmpq $0x0, 0x7ca8(%rbx)
je 0xb6b7
leaq 0x7cb2(%rbx), %rdi
movzbl %al, %r8d
leaq 0x22b03(%rip), %rdx     # 0x2e180
movslq (%rdx,%rax,4), %r9
addq %rdx, %r9
testq %r12, %r12
leaq 0x25f24(%rip), %rax     # 0x315b2
leaq 0x291fa(%rip), %rdx     # 0x3488f
cmoveq %rax, %rdx
movq %rdx, (%rsp)
leaq 0x25e46(%rip), %rdx     # 0x314ea
movl $0x2542, %esi           # imm = 0x2542
xorl %eax, %eax
callq 0x1070
movw %ax, 0x7cb0(%rbx)
testq %r12, %r12
je 0xb6d1
cmpq $0x1, %r14
jne 0xb702
incq %r13
movabsq $-0x8000000000000000, %rax # imm = 0x8000000000000000
jmp 0xb714
movq 0x7ca8(%rbx), %rsi
testq %rsi, %rsi
movq 0x10(%rsp), %r14
je 0xb6f7
andw $0x0, 0x7cb0(%rbx)
leaq 0x7cb2(%rbx), %rdi
callq *0x7ca0(%rbx)
pushq $0x5f
popq %r12
testq %r15, %r15
jne 0xb771
jmp 0xb77a
movzbl 0x1(%r13), %eax
shlq $0x38, %rax
btsq $0x37, %rax
addq $0x2, %r13
movq 0x10(%rsp), %r14
movq %rax, 0x18(%rbx)
movq %r13, (%rbx)
andq $0x0, 0x10(%rbx)
movq %rbp, 0x8(%rbx)
movq %rbx, %rdi
callq 0xb845
movq %rbx, %rdi
movl 0xc(%rsp), %esi
movq %r15, %rdx
movq %r14, %rcx
callq *%r12
movl %eax, %r12d
movq (%rbx), %r13
movq 0x8(%rbx), %rax
addq $-0x2, %r13
cmpq %rax, %r13
cmovnsq %rax, %r13
testl %r12d, %r12d
je 0xb767
cmpl $0x5f, %r12d
je 0xb767
cmpl $0x4a, %r12d
jne 0xb798
testq %r15, %r15
je 0xb77a
testl %r12d, %r12d
je 0xb7bf
movq %r14, %rdi
movl %r12d, %esi
callq *%r15
movq 0x50(%rsp), %r14
testq %r14, %r14
je 0xb798
movq %r13, %rdi
movq %rbp, %rsi
xorl %edx, %edx
callq 0x1150
addq $0x3, %rax
movq %rax, (%r14)
cmpb $0x0, 0x20(%rbx)
je 0xb7ad
addq $0x1590, %rbx           # imm = 0x1590
movq %rbx, %rdi
callq 0x10e0
movl %r12d, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movzbl 0x22(%rbx), %eax
xorl %r12d, %r12d
movl $0x100022, %ecx         # imm = 0x100022
btl %eax, %ecx
jb 0xb77a
jmp 0xb771
movl 0x64(%rbx), %eax
testl %eax, %eax
je 0xb7f6
movl 0x58(%rbx), %ecx
tzcntl %eax, %edx
movl 0x3d0(%rbx,%rdx,4), %edx
cmpl %edx, %ecx
cmovgl %ecx, %edx
movl %edx, 0x58(%rbx)
blsrl %eax, %eax
jmp 0xb7d5
leaq 0x1618(%rbx), %r15
leaq 0x1590(%rbx), %r14
movzwl 0x1648(%rbx), %ebp
cmpl $0x0, 0xc(%rsp)
jne 0xb824
testw %bp, %bp
je 0xb824
movq %r15, %rdi
movq %r14, %rsi
callq 0x1050
jmp 0xb804
cmpb $0x0, 0x20(%rbx)
je 0xb832
movq %r14, %rdi
callq 0x10e0
testw %bp, %bp
pushq $0xb
popq %rax
pushq $0x3d
popq %r12
cmovnel %eax, %r12d
jmp 0xb7ad