// File: /workspace/github_c_makefile_star2_O0/tvlabs[P]edge264/edge264_test.c
// Function: decode_file
static int decode_file(const char *name0)
{
	// process file names
	if (trace_file)
		fprintf(trace_file, "\n--- # %s\n", name0);
	int len = strrchr(name0, '.') - name0;
	if (strcmp(name0 + len + 1, "264") != 0)
		return 0;
	char name1[len + 5], name2[len + 7];
	snprintf(name1, sizeof(name1), "%.*s.yuv", len, name0);
	snprintf(name2, sizeof(name2), "%.*s.1.yuv", len, name0);
	
	// open and memory map all input files
	int quit = 0;
	conf[0] = conf[1] = NULL;
	#ifdef _WIN32
		HANDLE f0 = NULL, f1 = NULL, f2 = NULL, m0 = NULL, m1 = NULL, m2 = NULL;
		void *v0 = NULL, *v1 = NULL, *v2 = NULL;
		if ((f0 = CreateFileA(name0, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL)) == INVALID_HANDLE_VALUE ||
			(m0 = CreateFileMappingA(f0, NULL, PAGE_READONLY, 0, 0, NULL)) == NULL ||
			(v0 = MapViewOfFile(m0, FILE_MAP_READ, 0, 0, 0)) == NULL) {
			printf("Error opening file %s: %lu\n", name0, GetLastError());
			quit = 1;
		}
		if (enable_yuv) {
			if ((f1 = CreateFileA(name1, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL)) == INVALID_HANDLE_VALUE) {
				printf("%s%s not found\n", moveup, name1);
				moveup = "";
			} else if ((m1 = CreateFileMappingA(f1, NULL, PAGE_READONLY, 0, 0, NULL)) == NULL ||
				(conf[0] = v1 = MapViewOfFile(m1, FILE_MAP_READ, 0, 0, 0)) == NULL) {
				printf("Error opening file %s: %lu\n", name1, GetLastError());
				quit = 1;
			}
			f2 = CreateFileA(name2, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
			if (f2 != INVALID_HANDLE_VALUE &&
				((m2 = CreateFileMappingA(f2, NULL, PAGE_READONLY, 0, 0, NULL)) == NULL ||
				(conf[1] = v2 = MapViewOfFile(m2, FILE_MAP_READ, 0, 0, 0)) == NULL)) {
				printf("Error opening file %s: %lu\n", name2, GetLastError());
				quit = 1;
			}
		}
		const uint8_t *nal = v0;
		const uint8_t *end0 = v0 + GetFileSize(f0, NULL);
		const uint8_t *end1 = v1 + GetFileSize(f1, NULL);
	#else
		int fd0 = -1, fd1 = -1, fd2 = -1;
		struct stat st0, st1, st2;
		uint8_t *mm0 = MAP_FAILED, *mm1 = MAP_FAILED, *mm2 = MAP_FAILED;
		if ((fd0 = open(name0, O_RDONLY)) < 0 ||
			fstat(fd0, &st0) < 0 ||
			(mm0 = mmap(NULL, st0.st_size, PROT_READ, MAP_SHARED, fd0, 0)) == MAP_FAILED) {
			perror(name0);
			quit = 1;
		}
		if (enable_yuv) {
			if ((fd1 = open(name1, O_RDONLY)) < 0) {
				printf("%s%s not found\n", moveup, name1);
				moveup = "";
			} else if (fstat(fd1, &st1) < 0 ||
				(conf[0] = mm1 = mmap(NULL, st1.st_size, PROT_READ, MAP_SHARED, fd1, 0)) == MAP_FAILED) {
				perror(name1);
				quit = 1;
			}
			fd2 = open(name2, O_RDONLY);
			if (fd2 >= 0 && (fstat(fd2, &st2) < 0 ||
				(conf[1] = mm2 = mmap(NULL, st2.st_size, PROT_READ, MAP_SHARED, fd2, 0)) == MAP_FAILED)) {
				perror(name2);
				quit = 1;
			}
		}
		const uint8_t *nal = mm0;
		const uint8_t *end0 = mm0 + st0.st_size;
		const uint8_t *end1 = mm1 + st1.st_size;
	#endif
	
	// print the success counts
	if (!quit) {
		if (count_flag > 0)
			printf("%s%d " GREEN "PASS" RESET ", %d " YELLOW "UNSUPPORTED" RESET ", %d " RED "FAIL" RESET ", %d " BLUE "FLAGGED" RESET " (%s)\n", moveup, count_pass, count_unsup, count_fail, count_flag, name0);
		else
			printf("%s%d " GREEN "PASS" RESET ", %d " YELLOW "UNSUPPORTED" RESET ", %d " RED "FAIL" RESET " (%s)\n", moveup, count_pass, count_unsup, count_fail, name0);
		moveup = "\e[A\e[K";
		
		// decode the entire file and FAIL on any error
		nal += 3 + (nal[2] == 0); // skip the [0]001 delimiter
		int res;
		do {
			res = edge264_decode_NAL(d, nal, end0, 0, NULL, NULL, &nal);
			while (!edge264_get_frame(d, &out, 0)) {
				if (conf[0] != NULL && check_frame()) {
					res = EBADMSG;
					break;
				} else if (display && draw_frame()) {
					res = ENODATA;
					quit = 1;
					break;
				}
			}
		} while (res == 0 || res == ENOBUFS);
		edge264_flush(d);
		if (res == ENOBUFS || (res == ENODATA && conf[0] != NULL && conf[0] != end1))
			res = EBADMSG;
		// FIXME interrupt all threads before closing the files!
		
		// print the file that was decoded
		count_pass += res == ENODATA;
		count_unsup += res == ENOTSUP;
		count_fail += res == EBADMSG;
		count_flag += res == ESRCH;
		if (res == ENODATA && print_passed) {
			printf("%s%s: " GREEN "PASS" RESET "\n", moveup, name0);
			moveup = "";
		} else if (res == ENOTSUP && print_unsupported) {
			printf("%s%s: " YELLOW "UNSUPPORTED" RESET "\n", moveup, name0);
			moveup = "";
		} else if (res == EBADMSG && print_failed) {
			printf("%s%s: " RED "FAIL" RESET "\n", moveup, name0);
			moveup = "";
		} else if (res == ESRCH) {
			printf("%s%s: " BLUE "FLAGGED" RESET "\n", moveup, name0);
			moveup = "";
		}
	}
	
	// close everything
	#ifdef _WIN32
		UnmapViewOfFile(v0);
		UnmapViewOfFile(v1);
		UnmapViewOfFile(v2);
		CloseHandle(m0);
		CloseHandle(m1);
		CloseHandle(m2);
		CloseHandle(f0);
		CloseHandle(f1);
		CloseHandle(f2);
	#else
		munmap(mm0, st0.st_size);
		munmap(mm1, st1.st_size);
		munmap(mm2, st2.st_size);
		close(fd0);
		close(fd1);
		close(fd2);
	#endif
	return quit;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x250, %rsp            # imm = 0x250
movq %rdi, -0x10(%rbp)
cmpq $0x0, 0x4439(%rip)      # 0x6130
je 0x1d12
movq 0x4430(%rip), %rdi      # 0x6130
movq -0x10(%rbp), %rdx
leaq 0x2903(%rip), %rsi      # 0x460e
movb $0x0, %al
callq 0x1140
movq -0x10(%rbp), %rdi
movl $0x2e, %esi
callq 0x10d0
movq -0x10(%rbp), %rcx
subq %rcx, %rax
movl %eax, -0x14(%rbp)
movq -0x10(%rbp), %rdi
movslq -0x14(%rbp), %rax
addq %rax, %rdi
addq $0x1, %rdi
leaq 0x2acf(%rip), %rsi      # 0x480f
callq 0x1120
cmpl $0x0, %eax
je 0x1d56
movl $0x0, -0x4(%rbp)
jmp 0x23a3
movl -0x14(%rbp), %eax
addl $0x5, %eax
movl %eax, %esi
movq %rsp, %rax
movq %rax, -0x20(%rbp)
movq %rsi, %rax
addq $0xf, %rax
andq $-0x10, %rax
movq %rsp, %rdi
subq %rax, %rdi
movq %rdi, -0x240(%rbp)
movq %rdi, %rsp
movq %rsi, -0x28(%rbp)
movl -0x14(%rbp), %eax
addl $0x7, %eax
movq %rax, -0x230(%rbp)
movq %rax, %rdx
addq $0xf, %rdx
andq $-0x10, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0x238(%rbp)
movq %rcx, %rsp
movq %rax, -0x30(%rbp)
movl -0x14(%rbp), %ecx
movq -0x10(%rbp), %r8
leaq 0x285b(%rip), %rdx      # 0x4619
movb $0x0, %al
callq 0x10c0
movq -0x238(%rbp), %rdi
movq -0x230(%rbp), %rsi
movl -0x14(%rbp), %ecx
movq -0x10(%rbp), %r8
leaq 0x2841(%rip), %rdx      # 0x4622
movb $0x0, %al
callq 0x10c0
movl $0x0, -0x34(%rbp)
movq $0x0, 0x441e(%rip) # 0x6218
movq $0x0, 0x440b(%rip) # 0x6210
movl $0xffffffff, -0x38(%rbp) # imm = 0xFFFFFFFF
movl $0xffffffff, -0x3c(%rbp) # imm = 0xFFFFFFFF
movl $0xffffffff, -0x40(%rbp) # imm = 0xFFFFFFFF
movq $-0x1, %rax
movq %rax, -0x1f8(%rbp)
movq $-0x1, %rax
movq %rax, -0x200(%rbp)
movq $-0x1, %rax
movq %rax, -0x208(%rbp)
movq -0x10(%rbp), %rdi
xorl %esi, %esi
movb $0x0, %al
callq 0x1180
movl %eax, -0x38(%rbp)
cmpl $0x0, %eax
jl 0x1e9f
movl -0x38(%rbp), %edi
leaq -0xd0(%rbp), %rsi
callq 0x11f0
cmpl $0x0, %eax
jl 0x1e9f
movq -0xa0(%rbp), %rsi
movl -0x38(%rbp), %r8d
xorl %eax, %eax
movl %eax, %r9d
movl $0x1, %ecx
movq %r9, %rdi
movl %ecx, %edx
callq 0x10a0
movq %rax, -0x1f8(%rbp)
movq $-0x1, %rcx
cmpq %rcx, %rax
jne 0x1eaf
movq -0x10(%rbp), %rdi
callq 0x11b0
movl $0x1, -0x34(%rbp)
cmpl $0x0, 0x425a(%rip)      # 0x6110
je 0x1fdd
movq -0x240(%rbp), %rdi
xorl %esi, %esi
movb $0x0, %al
callq 0x1180
movl %eax, -0x3c(%rbp)
cmpl $0x0, %eax
jge 0x1f00
movq -0x240(%rbp), %rdx
movq 0x4236(%rip), %rsi      # 0x6118
leaq 0x2744(%rip), %rdi      # 0x462d
movb $0x0, %al
callq 0x10b0
leaq 0x2894(%rip), %rax      # 0x478b
movq %rax, 0x421a(%rip)      # 0x6118
jmp 0x1f62
movl -0x3c(%rbp), %edi
leaq -0x160(%rbp), %rsi
callq 0x11f0
cmpl $0x0, %eax
jl 0x1f4d
movq -0x130(%rbp), %rsi
movl -0x3c(%rbp), %r8d
xorl %eax, %eax
movl %eax, %r9d
movl $0x1, %ecx
movq %r9, %rdi
movl %ecx, %edx
callq 0x10a0
movq %rax, -0x200(%rbp)
movq %rax, 0x42cf(%rip)      # 0x6210
movq $-0x1, %rcx
cmpq %rcx, %rax
jne 0x1f60
movq -0x240(%rbp), %rdi
callq 0x11b0
movl $0x1, -0x34(%rbp)
jmp 0x1f62
movq -0x238(%rbp), %rdi
xorl %esi, %esi
movb $0x0, %al
callq 0x1180
movl %eax, -0x40(%rbp)
cmpl $0x0, -0x40(%rbp)
jl 0x1fdb
movl -0x40(%rbp), %edi
leaq -0x1f0(%rbp), %rsi
callq 0x11f0
cmpl $0x0, %eax
jl 0x1fc8
movq -0x1c0(%rbp), %rsi
movl -0x40(%rbp), %r8d
xorl %eax, %eax
movl %eax, %r9d
movl $0x1, %ecx
movq %r9, %rdi
movl %ecx, %edx
callq 0x10a0
movq %rax, -0x208(%rbp)
movq %rax, 0x425c(%rip)      # 0x6218
movq $-0x1, %rcx
cmpq %rcx, %rax
jne 0x1fdb
movq -0x238(%rbp), %rdi
callq 0x11b0
movl $0x1, -0x34(%rbp)
jmp 0x1fdd
movq -0x1f8(%rbp), %rax
movq %rax, -0x210(%rbp)
movq -0x1f8(%rbp), %rax
addq -0xa0(%rbp), %rax
movq %rax, -0x218(%rbp)
movq -0x200(%rbp), %rax
addq -0x130(%rbp), %rax
movq %rax, -0x220(%rbp)
cmpl $0x0, -0x34(%rbp)
jne 0x2345
cmpl $0x0, 0x412a(%rip)      # 0x6150
jle 0x2069
movq 0x40e9(%rip), %rsi      # 0x6118
movl 0x411f(%rip), %edx      # 0x6154
movl 0x411d(%rip), %ecx      # 0x6158
movl 0x411a(%rip), %r8d      # 0x615c
movl 0x4107(%rip), %r9d      # 0x6150
movq -0x10(%rbp), %rax
leaq 0x25e9(%rip), %rdi      # 0x463d
subq $0x10, %rsp
movq %rax, (%rsp)
movb $0x0, %al
callq 0x10b0
addq $0x10, %rsp
jmp 0x2095
movq 0x40a8(%rip), %rsi      # 0x6118
movl 0x40de(%rip), %edx      # 0x6154
movl 0x40dc(%rip), %ecx      # 0x6158
movl 0x40d9(%rip), %r8d      # 0x615c
movq -0x10(%rbp), %r9
leaq 0x2608(%rip), %rdi      # 0x4696
movb $0x0, %al
callq 0x10b0
leaq 0x263e(%rip), %rax      # 0x46da
movq %rax, 0x4075(%rip)      # 0x6118
movq -0x210(%rbp), %rax
movzbl 0x2(%rax), %eax
cmpl $0x0, %eax
sete %al
andb $0x1, %al
movzbl %al, %ecx
addl $0x3, %ecx
movq -0x210(%rbp), %rax
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0x210(%rbp)
movq 0x4071(%rip), %rdi      # 0x6148
movq -0x210(%rbp), %rsi
movq -0x218(%rbp), %rdx
xorl %ecx, %ecx
xorl %eax, %eax
movl %eax, %r9d
leaq -0x210(%rbp), %rax
subq $0x10, %rsp
movq %r9, %r8
movq %rax, (%rsp)
callq 0x1050
addq $0x10, %rsp
movl %eax, -0x224(%rbp)
movq 0x4034(%rip), %rdi      # 0x6148
leaq 0x4105(%rip), %rsi      # 0x6220
xorl %edx, %edx
callq 0x11d0
cmpl $0x0, %eax
setne %al
xorb $-0x1, %al
testb $0x1, %al
jne 0x2130
jmp 0x217a
cmpq $0x0, 0x40d8(%rip)      # 0x6210
je 0x2150
callq 0x2450
cmpl $0x0, %eax
je 0x2150
movl $0x4a, -0x224(%rbp)
jmp 0x217a
cmpl $0x0, 0x3fe1(%rip)      # 0x6138
je 0x2176
callq 0x2e10
cmpl $0x0, %eax
je 0x2176
movl $0x3d, -0x224(%rbp)
movl $0x1, -0x34(%rbp)
jmp 0x217a
jmp 0x2178
jmp 0x210d
jmp 0x217c
movb $0x1, %al
cmpl $0x0, -0x224(%rbp)
movb %al, -0x241(%rbp)
je 0x219d
cmpl $0x69, -0x224(%rbp)
sete %al
movb %al, -0x241(%rbp)
movb -0x241(%rbp), %al
testb $0x1, %al
jne 0x20d0
movq 0x3f96(%rip), %rdi      # 0x6148
callq 0x1150
cmpl $0x69, -0x224(%rbp)
je 0x21e3
cmpl $0x3d, -0x224(%rbp)
jne 0x21ed
cmpq $0x0, 0x403f(%rip)      # 0x6210
je 0x21ed
movq 0x4036(%rip), %rax      # 0x6210
cmpq -0x220(%rbp), %rax
je 0x21ed
movl $0x4a, -0x224(%rbp)
cmpl $0x3d, -0x224(%rbp)
sete %al
andb $0x1, %al
movzbl %al, %eax
addl 0x3f52(%rip), %eax      # 0x6154
movl %eax, 0x3f4c(%rip)      # 0x6154
cmpl $0x5f, -0x224(%rbp)
sete %al
andb $0x1, %al
movzbl %al, %eax
addl 0x3f3b(%rip), %eax      # 0x6158
movl %eax, 0x3f35(%rip)      # 0x6158
cmpl $0x4a, -0x224(%rbp)
sete %al
andb $0x1, %al
movzbl %al, %eax
addl 0x3f24(%rip), %eax      # 0x615c
movl %eax, 0x3f1e(%rip)      # 0x615c
cmpl $0x3, -0x224(%rbp)
sete %al
andb $0x1, %al
movzbl %al, %eax
addl 0x3efd(%rip), %eax      # 0x6150
movl %eax, 0x3ef7(%rip)      # 0x6150
cmpl $0x3d, -0x224(%rbp)
jne 0x2297
cmpl $0x0, 0x3ed7(%rip)      # 0x6140
je 0x2297
movq 0x3ea6(%rip), %rsi      # 0x6118
movq -0x10(%rbp), %rdx
leaq 0x2464(%rip), %rdi      # 0x46e1
movb $0x0, %al
callq 0x10b0
leaq 0x2500(%rip), %rax      # 0x478b
movq %rax, 0x3e86(%rip)      # 0x6118
jmp 0x2343
cmpl $0x5f, -0x224(%rbp)
jne 0x22d2
cmpl $0x0, 0x3e9d(%rip)      # 0x6144
je 0x22d2
movq 0x3e68(%rip), %rsi      # 0x6118
movq -0x10(%rbp), %rdx
leaq 0x243b(%rip), %rdi      # 0x46f6
movb $0x0, %al
callq 0x10b0
leaq 0x24c2(%rip), %rax      # 0x478b
movq %rax, 0x3e48(%rip)      # 0x6118
jmp 0x2341
cmpl $0x4a, -0x224(%rbp)
jne 0x230d
cmpl $0x0, 0x3e5a(%rip)      # 0x613c
je 0x230d
movq 0x3e2d(%rip), %rsi      # 0x6118
movq -0x10(%rbp), %rdx
leaq 0x241c(%rip), %rdi      # 0x4712
movb $0x0, %al
callq 0x10b0
leaq 0x2487(%rip), %rax      # 0x478b
movq %rax, 0x3e0d(%rip)      # 0x6118
jmp 0x233f
cmpl $0x3, -0x224(%rbp)
jne 0x233d
movq 0x3dfb(%rip), %rsi      # 0x6118
movq -0x10(%rbp), %rdx
leaq 0x23ff(%rip), %rdi      # 0x4727
movb $0x0, %al
callq 0x10b0
leaq 0x2455(%rip), %rax      # 0x478b
movq %rax, 0x3ddb(%rip)      # 0x6118
jmp 0x233f
jmp 0x2341
jmp 0x2343
jmp 0x2345
movq -0x1f8(%rbp), %rdi
movq -0xa0(%rbp), %rsi
callq 0x1160
movq -0x200(%rbp), %rdi
movq -0x130(%rbp), %rsi
callq 0x1160
movq -0x208(%rbp), %rdi
movq -0x1c0(%rbp), %rsi
callq 0x1160
movl -0x38(%rbp), %edi
callq 0x10f0
movl -0x3c(%rbp), %edi
callq 0x10f0
movl -0x40(%rbp), %edi
callq 0x10f0
movl -0x34(%rbp), %eax
movl %eax, -0x4(%rbp)
movq -0x20(%rbp), %rax
movq %rax, %rsp
movl -0x4(%rbp), %eax
movq %rbp, %rsp
popq %rbp
retq
nopl (%rax,%rax)