// File: /workspace/github_c_makefile_star2_O1/jsummers[P]deark/modules/misc.c
// Function: de_run_xor
static void de_run_xor(deark *c, de_module_params *mparams)
{
	struct xorctx_struct *xorctx = NULL;
	int badkey_flag = 0;
	const char *opt_key;

	xorctx = de_malloc(c, sizeof(struct xorctx_struct));
	xorctx->key[0] = 0xff;
	xorctx->key_len = 1;

	opt_key = de_get_ext_option(c, "xor:key");
	if(opt_key) {
		UI key_strlen;
		UI k;

		key_strlen = (UI)de_strlen(opt_key);
		if(key_strlen%2) { badkey_flag = 1; goto done; }
		xorctx->key_len = key_strlen/2;
		if(xorctx->key_len<1 || (size_t)xorctx->key_len>sizeof(xorctx->key)) {
			badkey_flag = 1;
			goto done;
		}
		for(k=0; k<xorctx->key_len; k++) {
			u8 d1, d2;
			int errflag1 = 0;
			int errflag2 = 0;

			d1 = de_decode_hex_digit(opt_key[k*2], &errflag1);
			d2 = de_decode_hex_digit(opt_key[k*2+1], &errflag2);
			if(errflag1 || errflag2) { badkey_flag = 1; goto done; }
			xorctx->key[k] = (d1<<4) | d2;
		}
	}

	xorctx->outf = dbuf_create_output_file(c, "bin", NULL, 0);
	dbuf_enable_wbuffer(xorctx->outf);
	dbuf_buffered_read(c->infile, 0, c->infile->len, xor_cbfn, (void*)xorctx);

done:
	if(badkey_flag) {
		de_err(c, "Bad XOR key");
	}
	if(xorctx) {
		dbuf_close(xorctx->outf);
		de_free(c, xorctx);
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rdi, %rbx
movl $0x78, %esi
callq 0xe969b
movq %rax, %rbp
movb $-0x1, 0x10(%rax)
movl $0x1, 0x8(%rax)
leaq 0xbf5bf(%rip), %rsi     # 0x1244b7
movq %rbx, %rdi
callq 0xea6cb
testq %rax, %rax
je 0x64fdf
movq %rax, %r15
movq %rax, %rdi
callq 0x180e0
movl $0x1, %r14d
testb $0x1, %al
jne 0x64fe4
movl %eax, %ecx
shrl %ecx
movl %ecx, 0x8(%rbp)
addl $0xffffff37, %eax       # imm = 0xFFFFFF37
xorl %ecx, %ecx
cmpl $0xffffff38, %eax       # imm = 0xFFFFFF38
jb 0x64fe6
cmpl $0x0, 0x8(%rbp)
je 0x65060
movq %r15, 0x10(%rsp)
movq %rbx, 0x8(%rsp)
movl $0x1, %r13d
xorl %ebx, %ebx
xorl %r12d, %r12d
movq %rbp, %r15
xorl %eax, %eax
movl %eax, 0x4(%rsp)
movl %eax, (%rsp)
leal -0x1(%r13), %eax
movq 0x10(%rsp), %r14
movzbl (%r14,%rax), %edi
leaq 0x4(%rsp), %rsi
callq 0xec1cc
movl %eax, %ebp
movl %r13d, %eax
movzbl (%r14,%rax), %edi
movq %rsp, %rsi
callq 0xec1cc
movl (%rsp), %ecx
orl 0x4(%rsp), %ecx
sete %dl
movl $0x2, %ecx
movl $0x1, %r14d
jne 0x64fb8
shlb $0x4, %bpl
orb %bpl, %al
movb %al, 0x10(%r15,%rbx)
xorl %ecx, %ecx
movl %r12d, %r14d
testb %dl, %dl
movq %r15, %rbp
je 0x64fd3
incq %rbx
movl 0x8(%rbp), %eax
addl $0x2, %r13d
movl %r14d, %r12d
cmpq %rax, %rbx
jb 0x64f5a
xorl %ecx, %ecx
testl %ecx, %ecx
sete %cl
movq 0x8(%rsp), %rbx
jmp 0x64fe6
xorl %r14d, %r14d
jmp 0x64fea
xorl %ecx, %ecx
testb %cl, %cl
je 0x65022
leaq 0xd0440(%rip), %rsi     # 0x135431
movq %rbx, %rdi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0xf037a
movq %rax, (%rbp)
movq %rax, %rdi
callq 0xf0e3f
movq 0x18(%rbx), %rdi
movq 0x18(%rdi), %rdx
leaq 0x1fcd(%rip), %rcx      # 0x66fe5
xorl %esi, %esi
movq %rbp, %r8
callq 0xefc28
testl %r14d, %r14d
je 0x65038
leaq 0xbf491(%rip), %rsi     # 0x1244bf
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9df7
testq %rbp, %rbp
je 0x65051
movq (%rbp), %rdi
callq 0xf0b13
movq %rbx, %rdi
movq %rbp, %rsi
callq 0xe9752
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movb $0x1, %cl
xorl %r14d, %r14d
jmp 0x64fe6