// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/pklite.c
// Function: do_pklite_exe
static void do_pklite_exe(deark *c, lctx *d)
{
	const char *s;

	d->raw_mode = (u8)de_get_ext_option_bool(c, "pklite:raw", 0xff);

	s = de_get_ext_option(c, "execomp:align");
	if(s) {
		d->default_code_alignment = de_atoi(s);
	}
	if(d->default_code_alignment != 512) {
		d->default_code_alignment = 16;
	}

	fmtutil_collect_exe_info(c, c->infile, d->host_ei);

	de_read(d->epbytes, d->host_ei->entry_point, EPBYTES_LEN);
	d->intro_class_fmtutil = fmtutil_detect_pklite_by_exe_ep(c, d->epbytes, EPBYTES_LEN, 0xff);

	if(d->intro_class_fmtutil==0) {
		de_err(c, "Not a PKLITE-compressed file, or not a known type");
		d->errflag = 1;
		d->errmsg_handled = 1;
		goto done;
	}

	de_declare_fmt(c, "PKLITE-compressed EXE");

	do_read_version_info(c, d, 28);

	do_analyze_pklite_exe(c, d);
	if(d->errflag) goto done;

	do_decompress(c, d);
	dbuf_flush(d->dcmpr_code);
	if(d->errflag) goto done;
	d->dcmpr_ok = 1;

	if(d->raw_mode==1) {
		do_write_data_only(c, d);
		goto done;
	}

	do_read_reloc_table(c, d);
	if(d->errflag) goto done;

	d->footer_pos = d->reloc_tbl_endpos;
	if(d->footer_pos!=0) {
		i64 footer_capacity;

		footer_capacity = d->cmpr_data_area_endpos - d->footer_pos;
		de_dbg(c, "footer at %"I64_FMT", len=%"I64_FMT, d->footer_pos, footer_capacity);
		de_dbg_indent(c, 1);

		if(c->debug_level>=3) {
			de_dbg_hexdump(c, c->infile, d->footer_pos, footer_capacity, 32, "footer", 0);
		}

		// Footer is usually 8 bytes, but there can be up to 15 extra bytes, to
		// accommodate the checksum feature.
		if(footer_capacity < 8) {
			d->footer_pos = 0; // Error
		}
		else if(footer_capacity > 8+15) {
			de_warn(c, "Unexpected data at end of code segment (near %"I64_FMT")",
				d->footer_pos+8);
		}

		if(d->footer_pos!=0) {
			d->footer.regSS = de_geti16le(d->footer_pos);
			d->footer.regSP = de_getu16le(d->footer_pos+2);
			d->footer.regCS = de_geti16le(d->footer_pos+4);
			d->footer.regIP = de_getu16le(d->footer_pos+6);
		}

		de_dbg_indent(c, -1);
	}

	if(d->footer_pos==0) {
		d->errflag = 1;
		goto done;
	}

	acquire_new_exe_header(c, d);
	if(d->errflag) goto done;

	do_write_dcmpr(c, d);
	if(d->errflag) goto done;

	de_stdwarn_execomp(c);
	if(d->has_psp_sig) {
		de_warn(c, "This file has a tamper-detection feature (PSP signature \"%s\"). "
			"It might not run correctly when decompressed.",
			((d->psp_sig_type==2) ? "pk" : "PK"));

		// TODO: It is possible to patch the decompressed file, so that it stands
		// a chance of passing this protection check. But it's not easy.
	}

done:
	;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x30, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x8(%rbp), %rdi
leaq 0xb0c06(%rip), %rsi     # 0x1c3ed1
movl $0xff, %edx
callq 0x15cbc0
movb %al, %cl
movq -0x10(%rbp), %rax
movb %cl, 0x4(%rax)
movq -0x8(%rbp), %rdi
leaq 0x9636b(%rip), %rsi     # 0x1a9654
callq 0x15cb30
movq %rax, -0x18(%rbp)
cmpq $0x0, -0x18(%rbp)
je 0x11330b
movq -0x18(%rbp), %rdi
callq 0x15cca0
movl %eax, %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0xc(%rax)
movq -0x10(%rbp), %rax
cmpl $0x200, 0xc(%rax)       # imm = 0x200
je 0x113323
movq -0x10(%rbp), %rax
movl $0x10, 0xc(%rax)
movq -0x8(%rbp), %rdi
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rsi
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rdx
callq 0x188930
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x10(%rbp), %rsi
addq $0x10c, %rsi            # imm = 0x10C
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movq 0x40(%rax), %rdx
movl $0x3e8, %ecx            # imm = 0x3E8
callq 0x164d10
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
addq $0x10c, %rsi            # imm = 0x10C
movl $0x3e8, %edx            # imm = 0x3E8
movl $0xff, %ecx
callq 0x188790
movl %eax, %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x30(%rax)
movq -0x10(%rbp), %rax
cmpl $0x0, 0x30(%rax)
jne 0x1133bc
movq -0x8(%rbp), %rdi
leaq 0xb0b3b(%rip), %rsi     # 0x1c3edc
movb $0x0, %al
callq 0x15bfb0
movq -0x10(%rbp), %rax
movb $0x1, (%rax)
movq -0x10(%rbp), %rax
movb $0x1, 0x1(%rax)
jmp 0x1136a7
movq -0x8(%rbp), %rdi
leaq 0xb0b47(%rip), %rsi     # 0x1c3f0e
callq 0x15e380
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movl $0x1c, %edx
callq 0x113850
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x1138c0
movq -0x10(%rbp), %rax
cmpb $0x0, (%rax)
je 0x1133f9
jmp 0x1136a7
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x113ae0
movq -0x10(%rbp), %rax
movq 0xd8(%rax), %rdi
callq 0x167c30
movq -0x10(%rbp), %rax
cmpb $0x0, (%rax)
je 0x113424
jmp 0x1136a7
movq -0x10(%rbp), %rax
movb $0x1, 0x2(%rax)
movq -0x10(%rbp), %rax
movzbl 0x4(%rax), %eax
cmpl $0x1, %eax
jne 0x11344b
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x114150
jmp 0x1136a7
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x1141b0
movq -0x10(%rbp), %rax
cmpb $0x0, (%rax)
je 0x113466
jmp 0x1136a7
movq -0x10(%rbp), %rax
movq 0x90(%rax), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0xa0(%rax)
movq -0x10(%rbp), %rax
cmpq $0x0, 0xa0(%rax)
je 0x11361c
movq -0x10(%rbp), %rax
movq 0x98(%rax), %rax
movq -0x10(%rbp), %rcx
subq 0xa0(%rcx), %rax
movq %rax, -0x20(%rbp)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0xa0(%rax), %rdx
movq -0x20(%rbp), %rcx
leaq 0xb0a62(%rip), %rsi     # 0x1c3f24
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movq -0x8(%rbp), %rax
cmpl $0x3, (%rax)
jl 0x113516
movq -0x8(%rbp), %rdi
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rsi
movq -0x10(%rbp), %rax
movq 0xa0(%rax), %rdx
movq -0x20(%rbp), %rcx
movl $0x20, %r8d
leaq 0xa91fe(%rip), %r9      # 0x1bc706
xorl %eax, %eax
movl $0x0, (%rsp)
callq 0x15b840
cmpq $0x8, -0x20(%rbp)
jge 0x11352e
movq -0x10(%rbp), %rax
movq $0x0, 0xa0(%rax)
jmp 0x113558
cmpq $0x17, -0x20(%rbp)
jle 0x113556
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0xa0(%rax), %rdx
addq $0x8, %rdx
leaq 0xb09ec(%rip), %rsi     # 0x1c3f3b
movb $0x0, %al
callq 0x15bc10
jmp 0x113558
movq -0x10(%rbp), %rax
cmpq $0x0, 0xa0(%rax)
je 0x11360e
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x10(%rbp), %rax
movq 0xa0(%rax), %rsi
callq 0x165360
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0xa8(%rax)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x10(%rbp), %rax
movq 0xa0(%rax), %rsi
addq $0x2, %rsi
callq 0x165280
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0xb0(%rax)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x10(%rbp), %rax
movq 0xa0(%rax), %rsi
addq $0x4, %rsi
callq 0x165360
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0xb8(%rax)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x10(%rbp), %rax
movq 0xa0(%rax), %rsi
addq $0x6, %rsi
callq 0x165280
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0xc0(%rax)
movq -0x8(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x15b560
movq -0x10(%rbp), %rax
cmpq $0x0, 0xa0(%rax)
jne 0x113633
movq -0x10(%rbp), %rax
movb $0x1, (%rax)
jmp 0x1136a7
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x114270
movq -0x10(%rbp), %rax
cmpb $0x0, (%rax)
je 0x11364b
jmp 0x1136a7
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x114310
movq -0x10(%rbp), %rax
cmpb $0x0, (%rax)
je 0x113663
jmp 0x1136a7
movq -0x8(%rbp), %rdi
callq 0x15bbe0
movq -0x10(%rbp), %rax
cmpb $0x0, 0x8(%rax)
je 0x1136a5
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movzbl 0x9(%rax), %ecx
leaq 0xa49b6(%rip), %rdx     # 0x1b803f
leaq 0x8d399(%rip), %rax     # 0x1a0a29
cmpl $0x2, %ecx
cmoveq %rax, %rdx
leaq 0xb08cf(%rip), %rsi     # 0x1c3f6d
movb $0x0, %al
callq 0x15bc10
jmp 0x1136a7
addq $0x30, %rsp
popq %rbp
retq
nopl (%rax)