// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/ebml.c
// Function: do_element
static int do_element(deark *c, lctx *d, i64 pos1,
	i64 nbytes_avail, i64 *bytes_used)
{
	i64 ele_id;
	i64 ele_dlen;
	i64 pos = pos1;
	int retval = 0;
	const struct ele_id_info *einfo;
	const char *ele_name;
	int saved_indent_level;
	unsigned int dtype;
	int should_call_start_handler = 0;
	int should_decode_default = 0;
	int should_print_NOT_DECODING_msg = 0;
	int should_call_end_handler = 0;
	int len_ret;
	char tmpbuf[80];

	de_dbg_indent_save(c, &saved_indent_level);

	de_dbg(c, "element at %"I64_FMT", max_len=%"I64_FMT, pos1, nbytes_avail);
	de_dbg_indent(c, 1);

	if(1!=get_var_size_int(c->infile, &ele_id, &pos, nbytes_avail)) {
		de_err(c, "Failed to read ID of element at %"I64_FMT, pos1);
		goto done;
	}

	einfo = find_ele_id_info(ele_id);
	if(einfo && einfo->name)
		ele_name = einfo->name;
	else
		ele_name = "?";

	if(einfo)
		dtype = einfo->flags & 0xff;
	else
		dtype = 0;

	de_dbg(c, "id: 0x%"U64_FMTx" (%s)", (u64)ele_id, ele_name);
	if(d->show_encoded_id) {
		print_encoded_id(c, d, pos1, pos-pos1);
	}

	len_ret = get_var_size_int(c->infile, &ele_dlen, &pos, pos1+nbytes_avail-pos);
	if(len_ret==1) {
		de_snprintf(tmpbuf, sizeof(tmpbuf), "%"I64_FMT, ele_dlen);
	}
	else if(len_ret==2) {
		ele_dlen = c->infile->len - pos;
		de_strlcpy(tmpbuf, "unknown", sizeof(tmpbuf));
	}
	else {
		de_err(c, "Failed to read length of element at %"I64_FMT, pos1);
		goto done;
	}
	de_dbg(c, "data at %"I64_FMT", dlen=%s, type=%s", pos, tmpbuf,
		get_type_name(dtype));

	if(len_ret==2) {
		// EBML does not have any sort of end-of-master-element marker, which
		// presents a problem when a master element has an unknown length.
		//
		// EBML's "solution" is this:
		// "The end of an Unknown-Sized Element is determined by whichever
		// comes first: the end of the file or the beginning of the next EBML
		// Element, defined by this document or the corresponding EBML Schema,
		// that is not independently valid as Descendant Element of the
		// Unknown-Sized Element."
		//
		// This would appear to require a sophisticated, high-level algorithm
		// with 100% complete knowledge of the latest version of the specific
		// application format. We do not have such an algorithm.

		de_err(c, "EBML files with unknown-length elements are not supported");
		goto done;
	}

	if(pos + ele_dlen > c->infile->len) {
		de_err(c, "Element at %"I64_FMT" goes beyond end of file", pos1);
		goto done;
	}

	if(einfo) {
		should_decode_default = 1;

		if(einfo->flags & 0x0200) {
			should_decode_default = 0;
		}
		else if((einfo->flags & 0x0100) && c->debug_level<2) {
			should_decode_default = 0;
			should_print_NOT_DECODING_msg = 1;
		}
	}

	if(should_decode_default && einfo && einfo->hfn) {
		should_call_start_handler = 1;
	}

	if(should_decode_default && einfo && einfo->hfn && (einfo->flags & 0x0800)) {
		should_call_end_handler = 1;
	}

	if(should_call_start_handler) {
		struct handler_params hp;
		de_zeromem(&hp, sizeof(struct handler_params));
		hp.dpos = pos;
		hp.dlen = ele_dlen;
		einfo->hfn(c, d, &hp);
	}

	if(should_decode_default) {
		switch(dtype) {
		case TY_m:
			do_element_sequence(c, d, pos, ele_dlen);
			break;
		case TY_u:
			decode_uint(c, d, einfo, pos, ele_dlen);
			break;
		case TY_f:
			decode_float(c, d, einfo, pos, ele_dlen);
			break;
		case TY_8:
			decode_string(c, d, einfo, pos, ele_dlen, DE_ENCODING_UTF8);
			break;
		case TY_s:
			decode_string(c, d, einfo, pos, ele_dlen,
				DE_EXTENC_MAKE(DE_ENCODING_ASCII, DE_ENCSUBTYPE_PRINTABLE));
			break;
		case TY_d:
			decode_date(c, d, einfo, pos, ele_dlen);
			break;
		}
	}
	else {
		if(should_print_NOT_DECODING_msg) {
			de_dbg(c, "[not decoding this element]");
		}
	}

	if(should_call_end_handler) {
		struct handler_params hp;
		de_zeromem(&hp, sizeof(struct handler_params));
		hp.dpos = pos;
		hp.dlen = ele_dlen;
		hp.end_flag = 1;
		einfo->hfn(c, d, &hp);
	}

	pos += ele_dlen;

	*bytes_used = pos - pos1;
	retval = 1;

done:
	de_dbg_indent_restore(c, saved_indent_level);
	return retval;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x120, %rsp            # imm = 0x120
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0x40(%rbp)
movl $0x0, -0x44(%rbp)
movl $0x0, -0x64(%rbp)
movl $0x0, -0x68(%rbp)
movl $0x0, -0x6c(%rbp)
movl $0x0, -0x70(%rbp)
movq -0x8(%rbp), %rdi
leaq -0x5c(%rbp), %rsi
callq 0x15b580
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rdx
movq -0x20(%rbp), %rcx
leaq 0x14d22c(%rip), %rsi    # 0x1a67a6
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x20(%rbp), %rcx
leaq -0x30(%rbp), %rsi
leaq -0x40(%rbp), %rdx
callq 0x59a10
movl %eax, %ecx
movl $0x1, %eax
cmpl %ecx, %eax
je 0x595ce
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rdx
leaq 0x14d200(%rip), %rsi    # 0x1a67c2
movb $0x0, %al
callq 0x15bfb0
jmp 0x599f4
movq -0x30(%rbp), %rdi
callq 0x59b80
movq %rax, -0x50(%rbp)
cmpq $0x0, -0x50(%rbp)
je 0x595fb
movq -0x50(%rbp), %rax
cmpq $0x0, 0x10(%rax)
je 0x595fb
movq -0x50(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x58(%rbp)
jmp 0x59606
leaq 0x15e62b(%rip), %rax    # 0x1b7c2d
movq %rax, -0x58(%rbp)
cmpq $0x0, -0x50(%rbp)
je 0x5961d
movq -0x50(%rbp), %rax
movl (%rax), %eax
andl $0xff, %eax
movl %eax, -0x60(%rbp)
jmp 0x59624
movl $0x0, -0x60(%rbp)
movq -0x8(%rbp), %rdi
movq -0x30(%rbp), %rdx
movq -0x58(%rbp), %rcx
leaq 0x14d1af(%rip), %rsi    # 0x1a67e6
movb $0x0, %al
callq 0x15b0d0
movq -0x10(%rbp), %rax
cmpl $0x0, 0x4(%rax)
je 0x59661
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x18(%rbp), %rdx
movq -0x40(%rbp), %rcx
subq -0x18(%rbp), %rcx
callq 0x59bf0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x18(%rbp), %rcx
addq -0x20(%rbp), %rcx
subq -0x40(%rbp), %rcx
leaq -0x38(%rbp), %rsi
leaq -0x40(%rbp), %rdx
callq 0x59a10
movl %eax, -0x74(%rbp)
cmpl $0x1, -0x74(%rbp)
jne 0x596ab
leaq -0xd0(%rbp), %rdi
movq -0x38(%rbp), %rcx
movl $0x50, %esi
leaq 0x15ba6d(%rip), %rdx    # 0x1b510f
movb $0x0, %al
callq 0x15a980
jmp 0x596fc
cmpl $0x2, -0x74(%rbp)
jne 0x596df
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x18(%rax), %rax
subq -0x40(%rbp), %rax
movq %rax, -0x38(%rbp)
leaq -0xd0(%rbp), %rdi
leaq 0x144e89(%rip), %rsi    # 0x19e55c
movl $0x50, %edx
callq 0x15aa30
jmp 0x596fa
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rdx
leaq 0x14d107(%rip), %rsi    # 0x1a67f5
movb $0x0, %al
callq 0x15bfb0
jmp 0x599f4
jmp 0x596fc
movq -0x8(%rbp), %rax
movq %rax, -0x118(%rbp)
movq -0x40(%rbp), %rax
movq %rax, -0x110(%rbp)
leaq -0xd0(%rbp), %rax
movq %rax, -0x108(%rbp)
movl -0x60(%rbp), %edi
callq 0x59cc0
movq -0x118(%rbp), %rdi
movq -0x110(%rbp), %rdx
movq -0x108(%rbp), %rcx
movq %rax, %r8
leaq 0x14d0d6(%rip), %rsi    # 0x1a681d
movb $0x0, %al
callq 0x15b0d0
cmpl $0x2, -0x74(%rbp)
jne 0x5976b
movq -0x8(%rbp), %rdi
leaq 0x14d0dc(%rip), %rsi    # 0x1a683b
movb $0x0, %al
callq 0x15bfb0
jmp 0x599f4
movq -0x40(%rbp), %rax
addq -0x38(%rbp), %rax
movq -0x8(%rbp), %rcx
movq 0x18(%rcx), %rcx
cmpq 0x18(%rcx), %rax
jle 0x5979c
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rdx
leaq 0x14d0e5(%rip), %rsi    # 0x1a6875
movb $0x0, %al
callq 0x15bfb0
jmp 0x599f4
cmpq $0x0, -0x50(%rbp)
je 0x597ee
movl $0x1, -0x68(%rbp)
movq -0x50(%rbp), %rax
movl (%rax), %eax
andl $0x200, %eax            # imm = 0x200
cmpl $0x0, %eax
je 0x597c3
movl $0x0, -0x68(%rbp)
jmp 0x597ec
movq -0x50(%rbp), %rax
movl (%rax), %eax
andl $0x100, %eax            # imm = 0x100
cmpl $0x0, %eax
je 0x597ea
movq -0x8(%rbp), %rax
cmpl $0x2, (%rax)
jge 0x597ea
movl $0x0, -0x68(%rbp)
movl $0x1, -0x6c(%rbp)
jmp 0x597ec
jmp 0x597ee
cmpl $0x0, -0x68(%rbp)
je 0x5980d
cmpq $0x0, -0x50(%rbp)
je 0x5980d
movq -0x50(%rbp), %rax
cmpq $0x0, 0x18(%rax)
je 0x5980d
movl $0x1, -0x64(%rbp)
cmpl $0x0, -0x68(%rbp)
je 0x5983c
cmpq $0x0, -0x50(%rbp)
je 0x5983c
movq -0x50(%rbp), %rax
cmpq $0x0, 0x18(%rax)
je 0x5983c
movq -0x50(%rbp), %rax
movl (%rax), %eax
andl $0x800, %eax            # imm = 0x800
cmpl $0x0, %eax
je 0x5983c
movl $0x1, -0x70(%rbp)
cmpl $0x0, -0x64(%rbp)
je 0x59884
leaq -0xe8(%rbp), %rdi
xorl %esi, %esi
movl $0x18, %edx
callq 0x19140
movq -0x40(%rbp), %rax
movq %rax, -0xe8(%rbp)
movq -0x38(%rbp), %rax
movq %rax, -0xe0(%rbp)
movq -0x50(%rbp), %rax
movq 0x18(%rax), %rax
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
leaq -0xe8(%rbp), %rdx
callq *%rax
cmpl $0x0, -0x68(%rbp)
je 0x59969
movl -0x60(%rbp), %eax
decl %eax
movl %eax, %ecx
movq %rcx, -0x120(%rbp)
subl $0x7, %eax
ja 0x59967
movq -0x120(%rbp), %rax
leaq 0x14ce69(%rip), %rcx    # 0x1a671c
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x40(%rbp), %rdx
movq -0x38(%rbp), %rcx
callq 0x593b0
jmp 0x59967
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x50(%rbp), %rdx
movq -0x40(%rbp), %rcx
movq -0x38(%rbp), %r8
callq 0x59d70
jmp 0x59967
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x50(%rbp), %rdx
movq -0x40(%rbp), %rcx
movq -0x38(%rbp), %r8
callq 0x59e30
jmp 0x59967
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x50(%rbp), %rdx
movq -0x40(%rbp), %rcx
movq -0x38(%rbp), %r8
movl $0x2, %r9d
callq 0x59eb0
jmp 0x59967
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x50(%rbp), %rdx
movq -0x40(%rbp), %rcx
movq -0x38(%rbp), %r8
movl $0x401, %r9d            # imm = 0x401
callq 0x59eb0
jmp 0x59967
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x50(%rbp), %rdx
movq -0x40(%rbp), %rcx
movq -0x38(%rbp), %r8
callq 0x59f50
jmp 0x59983
cmpl $0x0, -0x6c(%rbp)
je 0x59981
movq -0x8(%rbp), %rdi
leaq 0x14cf22(%rip), %rsi    # 0x1a689c
movb $0x0, %al
callq 0x15b0d0
jmp 0x59983
cmpl $0x0, -0x70(%rbp)
je 0x599d2
leaq -0x100(%rbp), %rdi
xorl %esi, %esi
movl $0x18, %edx
callq 0x19140
movq -0x40(%rbp), %rax
movq %rax, -0x100(%rbp)
movq -0x38(%rbp), %rax
movq %rax, -0xf8(%rbp)
movb $0x1, -0xf0(%rbp)
movq -0x50(%rbp), %rax
movq 0x18(%rax), %rax
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
leaq -0x100(%rbp), %rdx
callq *%rax
movq -0x38(%rbp), %rax
addq -0x40(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rcx
subq -0x18(%rbp), %rcx
movq -0x28(%rbp), %rax
movq %rcx, (%rax)
movl $0x1, -0x44(%rbp)
movq -0x8(%rbp), %rdi
movl -0x5c(%rbp), %esi
callq 0x15b5a0
movl -0x44(%rbp), %eax
addq $0x120, %rsp            # imm = 0x120
popq %rbp
retq
nopl (%rax)