// File: /workspace/github_c_makefile_star2_O1/jsummers[P]deark/modules/asf.c
// Function: handler_ESP
static void handler_ESP(deark *c, lctx *d, struct handler_params *hp)
{
	i64 pos = hp->dpos;
	i64 name_count, pes_count;
	i64 k;
	i64 x, xlen;
	i64 bytes_remaining;
	int saved_indent_level;
	u8 guid_raw[16];
	char guid_string[50];

	de_dbg_indent_save(c, &saved_indent_level);
	if(hp->dlen<64) goto done;

	x = de_geti64le(pos);
	de_dbg(c, "start time: %"I64_FMT, x);
	pos += 8;
	x = de_geti64le(pos);
	de_dbg(c, "end time: %"I64_FMT, x);
	pos += 8;

	x = de_getu32le_p(&pos);
	de_dbg(c, "data bitrate: %u", (unsigned int)x);
	x = de_getu32le_p(&pos);
	de_dbg(c, "buffer size: %u", (unsigned int)x);
	x = de_getu32le_p(&pos);
	de_dbg(c, "initial buffer fullness: %u", (unsigned int)x);
	x = de_getu32le_p(&pos);
	de_dbg(c, "alt data bitrate: %u", (unsigned int)x);
	x = de_getu32le_p(&pos);
	de_dbg(c, "alt buffer size: %u", (unsigned int)x);
	x = de_getu32le_p(&pos);
	de_dbg(c, "alt initial buffer fullness: %u", (unsigned int)x);
	x = de_getu32le_p(&pos);
	de_dbg(c, "max object size: %u", (unsigned int)x);
	x = de_getu32le_p(&pos);
	de_dbg(c, "flags: 0x%08x", (unsigned int)x);

	x = de_getu16le_p(&pos);
	de_dbg(c, "stream number: %d", (int)x);
	x = de_getu16le_p(&pos);
	de_dbg(c, "language id index: %d", (int)x);
	x = de_geti64le(pos);
	de_dbg(c, "average time per frame: %"I64_FMT, x);
	pos += 8;

	name_count = de_getu16le_p(&pos);
	de_dbg(c, "name count: %d", (int)name_count);
	pes_count = de_getu16le_p(&pos);
	de_dbg(c, "payload ext. system count: %d", (int)pes_count);

	// Stream names (TODO)
	for(k=0; k<name_count; k++) {
		if(pos+4 > hp->dpos+hp->dlen) goto done;
		de_dbg(c, "name[%d] at %"I64_FMT, (int)k, pos);
		pos += 2; // language id index
		xlen = de_getu16le_p(&pos);
		pos += xlen;
	}

	// Payload extension systems
	for(k=0; k<pes_count; k++) {
		if(pos+22 > hp->dpos+hp->dlen) {
			goto done;
		}
		de_dbg(c, "payload ext. system[%d] at %"I64_FMT, (int)k, pos);
		de_dbg_indent(c, 1);

		read_and_render_guid(c->infile, guid_raw, pos, guid_string, sizeof(guid_string));
		de_dbg(c, "ext. system id: {%s} (%s)", guid_string, get_uuid_name(guid_raw));
		pos += 16;

		x = de_getu16le_p(&pos);
		de_dbg(c, "ext. data size: %d", (int)x);

		xlen = de_getu32le_p(&pos);
		de_dbg(c, "payload ext. system info length: %d", (int)xlen);

		if(pos+xlen > hp->dpos+hp->dlen) {
			goto done;
		}
		if(xlen>0) {
			de_dbg(c, "[%d bytes of payload ext. system info at %"I64_FMT, (int)xlen, pos);
			de_dbg_indent(c, 1);
			de_dbg_hexdump(c, c->infile, pos, xlen, 256, NULL, 0x1);
			de_dbg_indent(c, -1);
		}
		pos += xlen;
		de_dbg_indent(c, -1);
	}

	bytes_remaining = hp->dpos + hp->dlen - pos;
	// There is an optional Stream Properties object here, but the spec seems
	// less than clear about how to tell whether it is present.
	if(bytes_remaining>24+54) {
		do_object_sequence(c, d, pos, bytes_remaining, hp->level+1, 1, 1);
	}

done:
	de_dbg_indent_restore(c, saved_indent_level);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movq %rdx, %r15
movq %rsi, 0x28(%rsp)
movq %rdi, %rbx
movq 0x10(%rdx), %rax
movq %rax, 0x10(%rsp)
leaq 0x1c(%rsp), %rsi
callq 0xe958c
cmpq $0x40, 0x18(%r15)
jge 0x1f51c
movl 0x1c(%rsp), %esi
movq %rbx, %rdi
callq 0xe9595
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x18(%rbx), %rdi
leaq 0x10(%rsp), %r12
movq (%r12), %rsi
callq 0xef536
leaq 0xf334c(%rip), %rsi     # 0x112881
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xe9226
movq (%r12), %rsi
addq $0x8, %rsi
movq %rsi, (%r12)
movq 0x18(%rbx), %rdi
callq 0xef536
leaq 0xf3333(%rip), %rsi     # 0x112891
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xe9226
addq $0x8, (%r12)
movq 0x18(%rbx), %rdi
movq %r12, %rsi
callq 0xef3a0
leaq 0xf3320(%rip), %rsi     # 0x1128a3
movq %rbx, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xe9226
movq 0x18(%rbx), %rdi
movq %r12, %rsi
callq 0xef3a0
leaq 0xf3316(%rip), %rsi     # 0x1128b8
movq %rbx, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xe9226
movq 0x18(%rbx), %rdi
movq %r12, %rsi
callq 0xef3a0
leaq 0xf330b(%rip), %rsi     # 0x1128cc
movq %rbx, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xe9226
movq 0x18(%rbx), %rdi
movq %r12, %rsi
callq 0xef3a0
leaq 0xf32bf(%rip), %rsi     # 0x11289f
movq %rbx, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xe9226
movq 0x18(%rbx), %rdi
movq %r12, %rsi
callq 0xef3a0
leaq 0xf32b5(%rip), %rsi     # 0x1128b4
movq %rbx, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xe9226
movq 0x18(%rbx), %rdi
movq %r12, %rsi
callq 0xef3a0
leaq 0xf32aa(%rip), %rsi     # 0x1128c8
movq %rbx, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xe9226
movq 0x18(%rbx), %rdi
movq %r12, %rsi
callq 0xef3a0
leaq 0xf32ab(%rip), %rsi     # 0x1128e8
movq %rbx, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xe9226
movq 0x18(%rbx), %rdi
movq %r12, %rsi
callq 0xef3a0
leaq 0x106fa2(%rip), %rsi    # 0x1265fe
movq %rbx, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xe9226
movq 0x18(%rbx), %rdi
movq %r12, %rsi
callq 0xef1ed
leaq 0xf319e(%rip), %rsi     # 0x112819
movq %rbx, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xe9226
movq 0x18(%rbx), %rdi
movq %r12, %rsi
callq 0xef1ed
leaq 0xf3262(%rip), %rsi     # 0x1128fc
movq %rbx, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xe9226
movq 0x18(%rbx), %rdi
movq (%r12), %rsi
callq 0xef536
leaq 0xf3258(%rip), %rsi     # 0x112912
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xe9226
addq $0x8, (%r12)
movq 0x18(%rbx), %rdi
movq %r12, %rsi
callq 0xef1ed
movq %rax, %r14
leaq 0xf324c(%rip), %rsi     # 0x11292e
movq %rbx, %rdi
movl %r14d, %edx
xorl %eax, %eax
callq 0xe9226
movq 0x18(%rbx), %rdi
movq %r12, %rsi
callq 0xef1ed
leaq 0xf323b(%rip), %rsi     # 0x11293d
movq %rbx, %rdi
movq %rax, 0x20(%rsp)
movl %eax, %edx
xorl %eax, %eax
callq 0xe9226
testq %r14, %r14
jle 0x1f770
leaq 0xf323c(%rip), %r13     # 0x11295b
leaq 0x10(%rsp), %rbp
xorl %r12d, %r12d
movq 0x10(%rsp), %rcx
leaq 0x4(%rcx), %rax
movq 0x18(%r15), %rdx
addq 0x10(%r15), %rdx
cmpq %rdx, %rax
jg 0x1f501
movq %rbx, %rdi
movq %r13, %rsi
movl %r12d, %edx
xorl %eax, %eax
callq 0xe9226
addq $0x2, 0x10(%rsp)
movq 0x18(%rbx), %rdi
movq %rbp, %rsi
callq 0xef1ed
addq %rax, 0x10(%rsp)
incq %r12
cmpq %r12, %r14
jne 0x1f727
cmpq $0x0, 0x20(%rsp)
jle 0x1f8d3
leaq 0x10(%rsp), %r13
xorl %ebp, %ebp
movq 0x10(%rsp), %rcx
leaq 0x16(%rcx), %rax
movq 0x18(%r15), %rdx
addq 0x10(%r15), %rdx
cmpq %rdx, %rax
jg 0x1f501
movq %rbx, %rdi
leaq 0xf31c4(%rip), %rsi     # 0x11296b
movl %ebp, %edx
xorl %eax, %eax
callq 0xe9226
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe9585
movq 0x18(%rbx), %rdi
movq 0x10(%rsp), %rdx
leaq 0x30(%rsp), %r14
movq %r14, %rsi
leaq 0x40(%rsp), %r12
movq %r12, %rcx
callq 0x1e29d
movq %r14, %rdi
callq 0x1fa9b
movq %rbx, %rdi
leaq 0xf319d(%rip), %rsi     # 0x11298a
movq %r12, %rdx
movq %rax, %rcx
xorl %eax, %eax
callq 0xe9226
addq $0x10, 0x10(%rsp)
movq 0x18(%rbx), %rdi
movq %r13, %rsi
callq 0xef1ed
movq %rbx, %rdi
leaq 0xf318e(%rip), %rsi     # 0x1129a4
movl %eax, %edx
xorl %eax, %eax
callq 0xe9226
movq 0x18(%rbx), %rdi
movq %r13, %rsi
callq 0xef3a0
movq %rax, %r14
movq %rbx, %rdi
leaq 0xf317f(%rip), %rsi     # 0x1129b7
movl %r14d, %edx
xorl %eax, %eax
callq 0xe9226
movq 0x10(%rsp), %rcx
leaq (%rcx,%r14), %rax
movq 0x18(%r15), %rdx
addq 0x10(%r15), %rdx
cmpq %rdx, %rax
jg 0x1f501
testq %r14, %r14
jle 0x1f8b3
movq %rbx, %rdi
leaq 0xf3170(%rip), %rsi     # 0x1129db
movl %r14d, %edx
xorl %eax, %eax
callq 0xe9226
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe9585
movq 0x18(%rbx), %rsi
movq 0x10(%rsp), %rdx
movl $0x1, (%rsp)
movl $0x100, %r8d            # imm = 0x100
movq %rbx, %rdi
movq %r14, %rcx
xorl %r9d, %r9d
callq 0xe975a
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe9585
addq %r14, 0x10(%rsp)
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe9585
incq %rbp
cmpq %rbp, 0x20(%rsp)
jne 0x1f783
movq 0x18(%r15), %rcx
addq 0x10(%r15), %rcx
movq 0x10(%rsp), %rdx
subq %rdx, %rcx
cmpq $0x4f, %rcx
jl 0x1f501
movl 0x20(%r15), %r8d
incl %r8d
movq $0x1, (%rsp)
movq %rbx, %rdi
movq 0x28(%rsp), %rsi
movl $0x1, %r9d
callq 0x1dfe3
jmp 0x1f501