// File: /workspace/github_c_makefile_star2_O3/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 $0x28, %rsp
movl $0x16, %r13d
testq %rdi, %rdi
je 0xeec6
movl %ecx, %r15d
movq %rdx, %r12
movq %rsi, %rbp
testq %rsi, %rsi
sete %al
testq %rdx, %rdx
setne %cl
testb %cl, %al
jne 0xeec6
movq %rdi, %rbx
movq %r9, 0x18(%rsp)
movq %r8, 0x10(%rsp)
cmpb $0x0, 0x20(%rdi)
je 0xed38
leaq 0x1590(%rbx), %rdi
callq 0x1160
leaq 0x20(%rbx), %rdx
movq %r12, %r14
subq %rbp, %r14
testq %r14, %r14
jle 0xeeeb
movq %rdx, 0x8(%rsp)
movzbl (%rbp), %ecx
shrl $0x5, %ecx
movb %cl, 0x21(%rbx)
movzbl (%rbp), %eax
andl $0x1f, %eax
movb %al, 0x22(%rbx)
movl %eax, %edx
movq 0x288(%rbx,%rdx,8), %r13
cmpq $0x0, 0x7ca8(%rbx)
je 0xedc4
leaq 0x7cb2(%rbx), %rdi
movzbl %al, %r8d
leaq 0x25506(%rip), %rdx     # 0x34290
movslq (%rdx,%rax,4), %r9
addq %rdx, %r9
testq %r13, %r13
leaq 0x2895f(%rip), %rax     # 0x376fa
leaq 0x2bc3d(%rip), %rdx     # 0x3a9df
cmoveq %rax, %rdx
movq %rdx, (%rsp)
leaq 0x28881(%rip), %rdx     # 0x37632
movl $0x2542, %esi           # imm = 0x2542
xorl %eax, %eax
callq 0x1070
movw %ax, 0x7cb0(%rbx)
testq %r13, %r13
je 0xedde
cmpq $0x1, %r14
jne 0xee12
incq %rbp
movabsq $-0x8000000000000000, %rax # imm = 0x8000000000000000
jmp 0xee23
movq 0x7ca8(%rbx), %rsi
testq %rsi, %rsi
je 0xee00
movw $0x0, 0x7cb0(%rbx)
leaq 0x7cb2(%rbx), %rdi
callq *0x7ca0(%rbx)
movl $0x5f, %r13d
movq 0x10(%rsp), %r14
testq %r14, %r14
jne 0xee84
jmp 0xee8f
movzbl 0x1(%rbp), %eax
shlq $0x38, %rax
btsq $0x37, %rax
addq $0x2, %rbp
movq 0x10(%rsp), %r14
movq %rax, 0x18(%rbx)
movq %rbp, (%rbx)
movq $0x0, 0x10(%rbx)
movq %r12, 0x8(%rbx)
movq %rbx, %rdi
callq 0xef03
movq %rbx, %rdi
movl %r15d, %esi
movq %r14, %rdx
movq 0x18(%rsp), %rcx
callq *%r13
movl %eax, %r13d
movq (%rbx), %rbp
movq 0x8(%rbx), %rax
addq $-0x2, %rbp
cmpq %rax, %rbp
cmovnsq %rax, %rbp
testl %r13d, %r13d
je 0xee7a
cmpl $0x5f, %r13d
je 0xee7a
cmpl $0x4a, %r13d
jne 0xeead
testq %r14, %r14
je 0xee8f
testl %r13d, %r13d
je 0xeed8
movq 0x18(%rsp), %rdi
movl %r13d, %esi
callq *%r14
movq 0x60(%rsp), %r14
testq %r14, %r14
je 0xeead
movq %rbp, %rdi
movq %r12, %rsi
xorl %edx, %edx
callq 0x1150
addq $0x3, %rax
movq %rax, (%r14)
movq 0x8(%rsp), %rax
cmpb $0x0, (%rax)
je 0xeec6
addq $0x1590, %rbx           # imm = 0x1590
movq %rbx, %rdi
callq 0x10e0
movl %r13d, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movzbl 0x22(%rbx), %eax
xorl %r13d, %r13d
movl $0x100022, %ecx         # imm = 0x100022
btl %eax, %ecx
jb 0xee8f
jmp 0xee84
leaq 0x24(%rsp), %r14
movq %rbx, %rdi
movl %r15d, %esi
movq %r14, %rcx
callq 0x1190
movl (%r14), %r13d
jmp 0xeec6