// File: /workspace/github_c_makefile_star2_O3/jsummers[P]deark/modules/misc3.c
// Function: de_run_dwc
static void de_run_dwc(deark *c, de_module_params *mparams)
{
	de_arch_lctx *d = NULL;
	i64 trailer_pos;
	i64 trailer_len;
	i64 nmembers;
	i64 fhsize; // size of each file header
	i64 pos;
	i64 i;
	struct de_timestamp tmpts;
	int need_errmsg = 0;
	int saved_indent_level;

	de_dbg_indent_save(c, &saved_indent_level);

	d = de_arch_create_lctx(c);
	d->is_le = 1;
	d->input_encoding = de_get_input_encoding(c, NULL, DE_ENCODING_CP437);
	d->private1 = de_get_ext_option_bool(c, "dwc:extract", 0);

	if(!has_dwc_sig(c)) {
		de_err(c, "Not a DWC file");
		goto done;
	}
	de_declare_fmt(c, "DWC archive");

	if(!d->private1) {
		de_info(c, "Note: Use \"-opt dwc:extract\" to attempt decompression "
			"(works for most small files).");
	}

	de_dbg(c, "trailer");
	de_dbg_indent(c, 1);

	pos = c->infile->len - 27; // Position of the "trailer size" field
	trailer_len = de_getu16le_p(&pos); // Usually 27
	trailer_pos = c->infile->len - trailer_len;
	de_dbg(c, "size: %"I64_FMT" (starts at %"I64_FMT")", trailer_len, trailer_pos);
	if(trailer_len<27 || trailer_pos<0) {
		need_errmsg = 1;
		goto done;
	}

	fhsize = (i64)de_getbyte_p(&pos);
	de_dbg(c, "file header entry size: %d", (int)fhsize);
	if(fhsize<30) {
		need_errmsg = 1;
		goto done;
	}

	pos += 13; // TODO?: name of header file ("h" command)
	de_arch_read_field_dttm_p(d, &tmpts, "archive last-modified", DE_ARCH_TSTYPE_UNIX, &pos);

	nmembers = de_getu16le_p(&pos);
	de_dbg(c, "number of member files: %d", (int)nmembers);
	de_dbg_indent(c, -1);

	pos = trailer_pos - fhsize*nmembers;
	if(pos<0) {
		need_errmsg = 1;
		goto done;
	}
	for(i=0; i<nmembers; i++) {
		do_dwc_member(c, d, pos, fhsize);
		if(d->fatalerrflag) goto done;
		pos += fhsize;
	}

done:
	if(need_errmsg) {
		de_err(c, "Bad DWC file");
	}
	de_arch_destroy_lctx(c, d);
	de_dbg_indent_restore(c, saved_indent_level);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rdi, %rbx
leaq 0x34(%rsp), %rsi
callq 0xe8a1c
movq %rbx, %rdi
callq 0xfd4b2
movq %rax, %r14
movl $0x1, 0x10(%rax)
movq %rbx, %rdi
xorl %esi, %esi
movl $0xd, %edx
callq 0xeb220
movl %eax, 0x18(%r14)
leaq 0xb8537(%rip), %rsi     # 0x125f3d
movq %rbx, %rdi
xorl %edx, %edx
callq 0xe9bd0
movl %eax, 0x5c(%r14)
movq 0x18(%rbx), %rdi
movq 0x18(%rdi), %rsi
addq $-0x3, %rsi
leaq 0xb8604(%rip), %rdx     # 0x12602b
movl $0x3, %ecx
callq 0xefc85
testl %eax, %eax
je 0x6da41
leaq 0xb850d(%rip), %r13     # 0x125f49
jmp 0x6dfef
leaq 0xb8510(%rip), %rsi     # 0x125f58
movq %rbx, %rdi
callq 0xeb125
cmpl $0x0, 0x5c(%r14)
jne 0x6da68
leaq 0xb8506(%rip), %rsi     # 0x125f64
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9393
leaq 0xddd5e(%rip), %rsi     # 0x14b7cd
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8617
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe8a15
movq 0x18(%rbx), %rdi
movq 0x18(%rdi), %rax
addq $-0x1b, %rax
leaq 0x18(%rsp), %rsi
movq %rax, (%rsi)
callq 0xeebeb
movq %rax, %r15
movq 0x18(%rbx), %rax
movq 0x18(%rax), %r12
subq %r15, %r12
leaq 0xb8504(%rip), %rsi     # 0x125fb8
sets %bpl
movq %rbx, %rdi
movq %r15, %rdx
movq %r12, %rcx
xorl %eax, %eax
callq 0xe8617
cmpq $0x1b, %r15
setl %al
orb %bpl, %al
leaq 0xb8545(%rip), %r13     # 0x12601e
jne 0x6dfef
movq 0x18(%rbx), %rdi
leaq 0x18(%rsp), %rsi
callq 0xeeb4b
movzbl %al, %r15d
leaq 0xb84da(%rip), %rsi     # 0x125fd2
movq %rbx, %rdi
movl %r15d, %edx
xorl %eax, %eax
callq 0xe8617
cmpb $0x1e, %r15b
jb 0x6dfef
leaq 0x18(%rsp), %rbp
addq $0xd, (%rbp)
leaq 0xb84cd(%rip), %rdx     # 0x125fed
leaq 0x48(%rsp), %rsi
movq %r14, %rdi
movl $0x1, %ecx
movq %rbp, %r8
callq 0xfd638
movq 0x18(%rbx), %rdi
movq %rbp, %rsi
callq 0xeebeb
movq %rax, %rbp
leaq 0xb84b8(%rip), %rsi     # 0x126003
movq %rbx, %rdi
movl %ebp, %edx
xorl %eax, %eax
callq 0xe8617
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe8a15
movq %rbp, %rax
movq %r15, 0x10(%rsp)
imulq %r15, %rax
subq %rax, %r12
leaq 0x18(%rsp), %rax
movq %r12, (%rax)
js 0x6dfef
testq %rbp, %rbp
movq 0x10(%rsp), %r13
jle 0x6dffc
cmpb $0x22, %r13b
setb 0xe(%rsp)
leaq 0x28(%rsp), %r15
xorl %eax, %eax
movq %rbp, 0x38(%rsp)
movq %rax, 0x40(%rsp)
movq %rbx, %rdi
movq %r14, %rsi
callq 0xfd414
movq %rax, %rbp
movq %rbx, %rdi
leaq 0xd6fb1(%rip), %rsi     # 0x144b72
movq %r12, %rdx
xorl %eax, %eax
callq 0xe8617
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe8a15
movq %rbx, %rdi
callq 0xf6410
movq %rax, 0x68(%rbp)
movq 0x18(%rbx), %rdi
movl 0x18(%r14), %r9d
movl $0xc, %edx
movq %r12, %rsi
movq %rax, %rcx
movl $0x1, %r8d
callq 0xefbd7
movq 0x68(%rbp), %rdi
callq 0xf70f6
movq %rbx, %rdi
leaq 0xce139(%rip), %rsi     # 0x13bd4e
movq %rax, %rdx
xorl %eax, %eax
callq 0xe8617
movq 0x58(%rbp), %rdi
movq 0x68(%rbp), %rsi
callq 0xf6509
addq $0xd, %r12
movq %r12, 0x28(%rsp)
movq %rbp, %rdi
movq %r15, %rsi
callq 0xfd538
movq 0x50(%rbp), %rsi
addq $0x18, %rsi
movq %r14, %rdi
leaq 0xaa584(%rip), %rdx     # 0x1181d6
movl $0x1, %ecx
movq %r15, %r8
callq 0xfd638
movq %rbp, %rdi
movq %r15, %rsi
callq 0xfd57d
movq 0x18(%rbx), %rdi
movq %r15, %rsi
callq 0xeed83
movq %rax, 0x40(%rbp)
movq %rbx, %rdi
leaq 0xb83ab(%rip), %rsi     # 0x12602f
movq %rax, %rdx
xorl %eax, %eax
callq 0xe8617
movq 0x18(%rbx), %rdi
movq %r15, %rsi
callq 0xeeb4b
movzbl %al, %r15d
movl %r15d, %edx
andl $0xf, %edx
movl %edx, 0x90(%rbp)
movq %rbx, %rdi
leaq 0xb81f8(%rip), %rsi     # 0x125eac
xorl %eax, %eax
callq 0xe8617
shrl $0x4, %r15d
movl %r15d, 0x94(%rbp)
movq %rbx, %rdi
leaq 0xd792e(%rip), %rsi     # 0x1455fe
movl %r15d, %edx
xorl %eax, %eax
callq 0xe8617
testb $0x4, 0x94(%rbp)
je 0x6dcea
movb $0x1, 0x80(%rbp)
cmpl $0x1e, %r13d
jne 0x6dcf5
xorl %r13d, %r13d
jmp 0x6dd27
movq %r13, %r12
movq 0x18(%rbx), %rdi
leaq 0x28(%rsp), %r15
movq %r15, %rsi
callq 0xeeb4b
movzbl %al, %r13d
movq %rbx, %rdi
leaq 0xb832c(%rip), %rsi     # 0x126043
movl %r13d, %edx
xorl %eax, %eax
callq 0xe8617
cmpl $0x1f, %r12d
jne 0x6dd4b
movq %rbp, %rdi
callq 0xfd7af
xorl %r15d, %r15d
movb $0x1, %r12b
testl %eax, %eax
je 0x6df99
movq $0x0, 0x20(%rsp)
jmp 0x6de0a
movq 0x18(%rbx), %rdi
movq %r15, %rsi
callq 0xeeb4b
movzbl %al, %r15d
movq $0x0, 0x20(%rsp)
movq %rbx, %rdi
leaq 0xb82e5(%rip), %rsi     # 0x126053
movl %r15d, %edx
xorl %eax, %eax
callq 0xe8617
cmpb $0x22, %r12b
jb 0x6dda4
movq 0x18(%rbx), %rdi
leaq 0x28(%rsp), %rsi
callq 0xeebeb
movq %rbx, %rdi
leaq 0xb82ca(%rip), %rsi     # 0x126060
movq %rax, 0x20(%rsp)
movl %eax, %edx
xorl %eax, %eax
callq 0xe8617
movq %rbp, %rdi
callq 0xfd7af
testl %eax, %eax
je 0x6df96
cmpb $0x2, %r15b
jb 0x6de05
movq %rbx, %rdi
callq 0xf6410
movq %rax, 0x70(%rbp)
movq 0x18(%rbx), %rdi
movq 0x30(%rbp), %rsi
addq 0x40(%rbp), %rsi
leaq -0x1(%r15), %rdx
movl 0x18(%r14), %r9d
movq %rax, %rcx
movl $0x1, %r8d
callq 0xefbd7
movq 0x70(%rbp), %rdi
callq 0xf70f6
movq %rbx, %rdi
leaq 0xd652e(%rip), %rsi     # 0x144329
movq %rax, %rdx
xorl %eax, %eax
callq 0xe8617
movb 0xe(%rsp), %r12b
cmpl $0x2, %r13d
jb 0x6de6d
movq %rbx, %rdi
callq 0xf6410
movb %r12b, 0xf(%rsp)
movq %rax, %r12
movq 0x18(%rbx), %rdi
addq 0x40(%rbp), %r15
addq 0x30(%rbp), %r15
decq %r13
movl 0x18(%r14), %r9d
movq %r15, %rsi
movq %r13, %rdx
movq %rax, %rcx
movl $0x1, %r8d
callq 0xefbd7
movq %r12, %rdi
callq 0xf70f6
movq %rbx, %rdi
leaq 0xa6a27(%rip), %rsi     # 0x114880
movq %rax, %rdx
xorl %eax, %eax
callq 0xe8617
movq %r12, %r15
movb 0xf(%rsp), %r12b
jmp 0x6de70
xorl %r15d, %r15d
movq 0x10(%rsp), %r13
movq 0x58(%rbp), %rdi
callq 0xf6423
movq 0x68(%rbp), %rcx
movq 0x10(%rcx), %rax
testq %rax, %rax
jle 0x6dea6
movq 0x8(%rcx), %rcx
xorl %edx, %edx
cmpl $0x2f, (%rcx,%rdx,4)
jne 0x6de9e
movl $0x5f, (%rcx,%rdx,4)
incq %rdx
cmpq %rdx, %rax
jne 0x6de91
movq 0x70(%rbp), %rdi
callq 0xf678d
testl %eax, %eax
je 0x6dec2
movq 0x58(%rbp), %rdi
movq 0x68(%rbp), %rsi
callq 0xf6509
jmp 0x6defc
orb $0x1, 0x78(%rbp)
movq 0x58(%rbp), %rdi
movq 0x70(%rbp), %rsi
callq 0xf6509
movq 0x58(%rbp), %rdi
movl $0x1, %esi
callq 0xfd4f8
movq 0x68(%rbp), %rdi
callq 0xf678d
movq 0x58(%rbp), %rdi
testl %eax, %eax
je 0x6deb7
movl $0x5f, %esi
callq 0xf6542
testb %r12b, %r12b
jne 0x6df77
movq 0x30(%r14), %rdi
testq %rdi, %rdi
jne 0x6df1e
movq %rbx, %rdi
movl $0x21, %esi
callq 0xeb499
movq %rax, %rdi
movq %rax, 0x30(%r14)
callq 0xeb7fe
movq 0x30(%r14), %rdi
movq 0x18(%rbx), %rsi
movq 0x30(%rbp), %rcx
movq 0x40(%rbp), %rdx
callq 0xeb944
movq 0x30(%r14), %rdi
callq 0xeb865
movl %eax, %r12d
movq %rbx, %rdi
leaq 0xb8137(%rip), %rsi     # 0x126085
movl %eax, %edx
xorl %eax, %eax
callq 0xe8617
cmpl 0x20(%rsp), %r12d
je 0x6df77
movq %rbx, %rdi
leaq 0xb8144(%rip), %rsi     # 0x1260ac
movq 0x20(%rsp), %rdx
movl %r12d, %ecx
xorl %eax, %eax
callq 0xe9295
cmpl $0x0, 0x5c(%r14)
je 0x6df9e
leaq 0x3425(%rip), %rax      # 0x713aa
movq %rax, 0xa8(%rbp)
movq %rbp, %rdi
callq 0xfd85e
jmp 0x6df9e
xorl %r15d, %r15d
movq 0x10(%rsp), %r13
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe8a15
movq %rbx, %rdi
movq %rbp, %rsi
callq 0xfd460
movq %r15, %rdi
callq 0xf6753
cmpl $0x0, 0x38(%r14)
movq 0x38(%rsp), %rbp
leaq 0x28(%rsp), %r15
movq 0x40(%rsp), %rax
jne 0x6dffc
movq 0x18(%rsp), %r12
addq %r13, %r12
movq %r12, 0x18(%rsp)
incq %rax
cmpq %rbp, %rax
jne 0x6dba4
jmp 0x6dffc
movq %rbx, %rdi
movq %r13, %rsi
xorl %eax, %eax
callq 0xe9295
movq %rbx, %rdi
movq %r14, %rsi
callq 0xfd4cd
movl 0x34(%rsp), %esi
movq %rbx, %rdi
callq 0xe8a25
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq