// File: /workspace/github_c_makefile_star2_O1/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, %r12d
testq %rdi, %rdi
je 0xeb56
movq %rdx, %r15
movq %rsi, %rbp
testq %rsi, %rsi
sete %al
testq %rdx, %rdx
setne %dl
testb %dl, %al
jne 0xeb56
movq %r8, %r13
movq %rdi, %rbx
movl %ecx, 0xc(%rsp)
movq %r9, 0x10(%rsp)
cmpb $0x0, 0x20(%rdi)
je 0xe9bd
leaq 0x1590(%rbx), %rdi
callq 0x1160
leaq 0x20(%rbx), %rdx
movq %r15, %r14
subq %rbp, %r14
testq %r14, %r14
jle 0xeb68
movq %rdx, 0x18(%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), %r12
cmpq $0x0, 0x7ca8(%rbx)
je 0xea49
leaq 0x7cb2(%rbx), %rdi
movzbl %al, %r8d
leaq 0x25751(%rip), %rdx     # 0x34160
movslq (%rdx,%rax,4), %r9
addq %rdx, %r9
testq %r12, %r12
leaq 0x28b6a(%rip), %rax     # 0x3758a
leaq 0x2be38(%rip), %rdx     # 0x3a85f
cmoveq %rax, %rdx
movq %rdx, (%rsp)
leaq 0x28a8c(%rip), %rdx     # 0x374c2
movl $0x2542, %esi           # imm = 0x2542
xorl %eax, %eax
callq 0x1070
movw %ax, 0x7cb0(%rbx)
testq %r12, %r12
je 0xea65
cmpq $0x1, %r14
jne 0xea8f
movl $0x1, %ecx
movabsq $-0x8000000000000000, %rax # imm = 0x8000000000000000
jmp 0xeaa1
movq 0x7ca8(%rbx), %rsi
movl $0x5f, %r12d
testq %rsi, %rsi
je 0xeae9
movw $0x0, 0x7cb0(%rbx)
leaq 0x7cb2(%rbx), %rdi
callq *0x7ca0(%rbx)
jmp 0xeae9
movzbl 0x1(%rbp), %eax
shlq $0x38, %rax
btsq $0x37, %rax
movl $0x2, %ecx
movq 0x10(%rsp), %r14
movq %rax, 0x18(%rbx)
addq %rcx, %rbp
movq %rbp, (%rbx)
movq $0x0, 0x10(%rbx)
movq %r15, 0x8(%rbx)
movq %rbx, %rdi
callq 0xeb81
movq %rbx, %rdi
movl 0xc(%rsp), %esi
movq %r13, %rdx
movq %r14, %rcx
callq *%r12
movl %eax, %r12d
movq (%rbx), %rbp
movq 0x8(%rbx), %rax
addq $-0x2, %rbp
cmpq %rax, %rbp
cmovnsq %rax, %rbp
testl %r12d, %r12d
je 0xeafa
cmpl $0x5f, %r12d
je 0xeafa
cmpl $0x4a, %r12d
jne 0xeb3d
movq 0x60(%rsp), %r14
testq %r13, %r13
je 0xeb24
testl %r12d, %r12d
je 0xeb16
movq 0x10(%rsp), %rdi
movl %r12d, %esi
callq *%r13
jmp 0xeb24
movzbl 0x22(%rbx), %eax
movl $0x100022, %ecx         # imm = 0x100022
btl %eax, %ecx
jae 0xeb09
testq %r14, %r14
je 0xeb3d
movq %rbp, %rdi
movq %r15, %rsi
xorl %edx, %edx
callq 0x1150
addq $0x3, %rax
movq %rax, (%r14)
movq 0x18(%rsp), %rax
cmpb $0x0, (%rax)
je 0xeb56
addq $0x1590, %rbx           # imm = 0x1590
movq %rbx, %rdi
callq 0x10e0
movl %r12d, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x24(%rsp), %r14
movq %rbx, %rdi
movl 0xc(%rsp), %esi
movq %r14, %rcx
callq 0x1190
movl (%r14), %r12d
jmp 0xeb56