// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/mscompress.c
// Function: do_header_KWAJ
static int do_header_KWAJ(deark *c, lctx *d, i64 pos1)
{
	unsigned int flags;
	i64 pos = pos1;
	i64 n;
	i64 foundpos;
	int retval = 0;
	int ret;

	de_dbg(c, "header at %d", (int)pos);
	de_dbg_indent(c, 1);

	pos += 8; // signature

	d->cmpr_meth = (UI)dbuf_getu16le_p(d->inf, &pos);
	de_dbg(c, "compression method: %u (%s)", d->cmpr_meth, get_cmpr_meth_name(d->cmpr_meth));

	d->cmpr_data_pos = dbuf_getu16le_p(d->inf, &pos);
	de_dbg(c, "compressed data offset: %"I64_FMT, d->cmpr_data_pos);
	d->cmpr_data_len = d->inf->len - d->cmpr_data_pos;

	flags = (UI)dbuf_getu16le_p(d->inf, &pos);
	de_dbg(c, "header extension flags: 0x%04x", flags);

	if(flags & 0x0001) { // bit 0
		d->uncmpr_len = dbuf_getu32le_p(d->inf, &pos);
		d->uncmpr_len_known = 1;
		de_dbg(c, "uncompressed len: %"I64_FMT, d->uncmpr_len);
	}
	if(flags & 0x0002) { // bit 1
		pos += 2;
	}
	if(flags & 0x0004) { // bit 2
		n = dbuf_getu16le_p(d->inf, &pos);
		pos += n;
	}
	if(flags & 0x0008) { // bit 3, base part of filename
		foundpos = 0;
		ret = dbuf_search_byte(d->inf, 0x00, pos, 9, &foundpos);
		if(!ret) goto header_extensions_done;
		d->filename = ucstring_create(c);
		dbuf_read_to_ucstring(d->inf, pos, foundpos-pos, d->filename, 0, d->input_encoding);
		pos = foundpos+1;
	}
	if(flags & 0x0010) { // bit 4, filename extension
		foundpos = 0;
		ret = dbuf_search_byte(d->inf, 0x00, pos, 4, &foundpos);
		if(!ret) goto header_extensions_done;
		if(d->filename && (foundpos-pos > 0)) {
			ucstring_append_char(d->filename, '.');
			dbuf_read_to_ucstring(d->inf, pos, foundpos-pos, d->filename, 0, d->input_encoding);
		}
		pos = foundpos+1;
	}
	if(flags & 0x0020) { // bit 5
		// TODO (comment?)
	}

header_extensions_done:
	if(ucstring_isnonempty(d->filename)) {
		de_dbg(c, "filename: \"%s\"", ucstring_getpsz_d(d->filename));
	}

	// If no compression, don't copy/convert more bytes than given by the uncmpr_len field.
	if(d->uncmpr_len_known && (d->cmpr_meth==CMPR_NONE || d->cmpr_meth==CMPR_XOR) &&
		d->uncmpr_len < d->cmpr_data_len)
	{
		d->cmpr_data_len = d->uncmpr_len;
	}

	retval = 1;

	de_dbg_indent(c, -1);
	return retval;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0x28(%rbp)
movl $0x0, -0x3c(%rbp)
movq -0x8(%rbp), %rdi
movq -0x28(%rbp), %rax
movl %eax, %edx
leaq 0xe330a(%rip), %rsi     # 0x1c4f8e
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movq -0x28(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x28(%rbp)
movq -0x10(%rbp), %rax
movq 0x40(%rax), %rdi
leaq -0x28(%rbp), %rsi
callq 0x1652c0
movl %eax, %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0xc(%rax)
movq -0x8(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0x10(%rbp), %rax
movl 0xc(%rax), %eax
movl %eax, -0x44(%rbp)
movq -0x10(%rbp), %rax
movl 0xc(%rax), %edi
callq 0xe2490
movq -0x50(%rbp), %rdi
movl -0x44(%rbp), %edx
movq %rax, %rcx
leaq 0xec32a(%rip), %rsi     # 0x1ce018
movb $0x0, %al
callq 0x15b0d0
movq -0x10(%rbp), %rax
movq 0x40(%rax), %rdi
leaq -0x28(%rbp), %rsi
callq 0x1652c0
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rdx
leaq 0xdaf1a(%rip), %rsi     # 0x1bcc3e
movb $0x0, %al
callq 0x15b0d0
movq -0x10(%rbp), %rax
movq 0x40(%rax), %rax
movq 0x18(%rax), %rcx
movq -0x10(%rbp), %rax
subq 0x10(%rax), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x18(%rax)
movq -0x10(%rbp), %rax
movq 0x40(%rax), %rdi
leaq -0x28(%rbp), %rsi
callq 0x1652c0
movl %eax, -0x1c(%rbp)
movq -0x8(%rbp), %rdi
movl -0x1c(%rbp), %edx
leaq 0xdaef1(%rip), %rsi     # 0x1bcc5a
movb $0x0, %al
callq 0x15b0d0
movl -0x1c(%rbp), %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0xe1db9
movq -0x10(%rbp), %rax
movq 0x40(%rax), %rdi
leaq -0x28(%rbp), %rsi
callq 0x165580
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x28(%rax)
movq -0x10(%rbp), %rax
movb $0x1, 0x20(%rax)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rdx
leaq 0xc7a70(%rip), %rsi     # 0x1a9822
movb $0x0, %al
callq 0x15b0d0
movl -0x1c(%rbp), %eax
andl $0x2, %eax
cmpl $0x0, %eax
je 0xe1dd0
movq -0x28(%rbp), %rax
addq $0x2, %rax
movq %rax, -0x28(%rbp)
movl -0x1c(%rbp), %eax
andl $0x4, %eax
cmpl $0x0, %eax
je 0xe1dfc
movq -0x10(%rbp), %rax
movq 0x40(%rax), %rdi
leaq -0x28(%rbp), %rsi
callq 0x1652c0
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
addq -0x28(%rbp), %rax
movq %rax, -0x28(%rbp)
movl -0x1c(%rbp), %eax
andl $0x8, %eax
cmpl $0x0, %eax
je 0xe1e85
movq $0x0, -0x38(%rbp)
movq -0x10(%rbp), %rax
movq 0x40(%rax), %rdi
movq -0x28(%rbp), %rdx
xorl %esi, %esi
movl $0x9, %ecx
leaq -0x38(%rbp), %r8
callq 0x1667a0
movl %eax, -0x40(%rbp)
cmpl $0x0, -0x40(%rbp)
jne 0xe1e39
jmp 0xe1f35
movq -0x8(%rbp), %rdi
callq 0x1711d0
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x30(%rax)
movq -0x10(%rbp), %rax
movq 0x40(%rax), %rdi
movq -0x28(%rbp), %rsi
movq -0x38(%rbp), %rdx
subq -0x28(%rbp), %rdx
movq -0x10(%rbp), %rax
movq 0x30(%rax), %rcx
movq -0x10(%rbp), %rax
movl 0x4(%rax), %r9d
xorl %r8d, %r8d
callq 0x1669f0
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x28(%rbp)
movl -0x1c(%rbp), %eax
andl $0x10, %eax
cmpl $0x0, %eax
je 0xe1f26
movq $0x0, -0x38(%rbp)
movq -0x10(%rbp), %rax
movq 0x40(%rax), %rdi
movq -0x28(%rbp), %rdx
xorl %esi, %esi
movl $0x4, %ecx
leaq -0x38(%rbp), %r8
callq 0x1667a0
movl %eax, -0x40(%rbp)
cmpl $0x0, -0x40(%rbp)
jne 0xe1ec3
jmp 0xe1f35
movq -0x10(%rbp), %rax
cmpq $0x0, 0x30(%rax)
je 0xe1f1a
movq -0x38(%rbp), %rax
subq -0x28(%rbp), %rax
cmpq $0x0, %rax
jle 0xe1f1a
movq -0x10(%rbp), %rax
movq 0x30(%rax), %rdi
movl $0x2e, %esi
callq 0x1714c0
movq -0x10(%rbp), %rax
movq 0x40(%rax), %rdi
movq -0x28(%rbp), %rsi
movq -0x38(%rbp), %rdx
subq -0x28(%rbp), %rdx
movq -0x10(%rbp), %rax
movq 0x30(%rax), %rcx
movq -0x10(%rbp), %rax
movl 0x4(%rax), %r9d
xorl %r8d, %r8d
callq 0x1669f0
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x28(%rbp)
movl -0x1c(%rbp), %eax
andl $0x20, %eax
cmpl $0x0, %eax
je 0xe1f33
jmp 0xe1f33
jmp 0xe1f35
movq -0x10(%rbp), %rax
movq 0x30(%rax), %rdi
callq 0x1717f0
cmpl $0x0, %eax
je 0xe1f71
movq -0x8(%rbp), %rax
movq %rax, -0x58(%rbp)
movq -0x10(%rbp), %rax
movq 0x30(%rax), %rdi
callq 0x172650
movq -0x58(%rbp), %rdi
movq %rax, %rdx
leaq 0xe5619(%rip), %rsi     # 0x1c7583
movb $0x0, %al
callq 0x15b0d0
movq -0x10(%rbp), %rax
movzbl 0x20(%rax), %eax
cmpl $0x0, %eax
je 0xe1fb4
movq -0x10(%rbp), %rax
cmpl $0x0, 0xc(%rax)
je 0xe1f92
movq -0x10(%rbp), %rax
cmpl $0x1, 0xc(%rax)
jne 0xe1fb4
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movq -0x10(%rbp), %rcx
cmpq 0x18(%rcx), %rax
jge 0xe1fb4
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x18(%rax)
movl $0x1, -0x3c(%rbp)
movq -0x8(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x15b560
movl -0x3c(%rbp), %eax
addq $0x60, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)