// File: /workspace/github_c_makefile_star2_O2/jsummers[P]deark/modules/misc.c
// Function: de_run_bytefreq
static void de_run_bytefreq(deark *c, de_module_params *mparams)
{
	struct bytefreqctx_struct *bfctx = NULL;
	de_ucstring *s = NULL;
	unsigned int k;
	de_encoding input_encoding;
	struct de_encconv_state es;

	bfctx = de_malloc(c, sizeof(struct bytefreqctx_struct));
	input_encoding = de_get_input_encoding(c, NULL, DE_ENCODING_WINDOWS1252);
	if(input_encoding==DE_ENCODING_UTF8) {
		input_encoding=DE_ENCODING_ASCII;
	}
	de_encconv_init(&es, input_encoding);

	dbuf_buffered_read(c->infile, 0, c->infile->len, bytefreq_cbfn, (void*)bfctx);

	de_msg(c, "====Byte==== ===Count=== ==Locations==");
	s = ucstring_create(c);
	for(k=0; k<256; k++) {
		i32 ch;
		int cflag;
		unsigned int z;
		struct bytefreqentry *bf = &bfctx->e[k];

		if(bf->count==0) continue;
		ucstring_empty(s);

		ucstring_printf(s, DE_ENCODING_LATIN1, "%3u 0x%02x ", k, k);

		ch = de_char_to_unicode_ex((i32)k, &es);
		if(ch==DE_CODEPOINT_INVALID) {
			cflag = 0;
		}
		else {
			cflag = de_is_printable_uchar(ch);
		}

		if(cflag) {
			ucstring_append_sz(s, "'", DE_ENCODING_LATIN1);
			ucstring_append_char(s, ch);
			ucstring_append_sz(s, "'", DE_ENCODING_LATIN1);
		}
		else {
			ucstring_append_sz(s, "   ", DE_ENCODING_LATIN1);
		}

		ucstring_printf(s, DE_ENCODING_LATIN1, " %11"I64_FMT" ", bf->count);

		for(z=0; z<DE_BYTEFREQ_NUMLOC && z<bf->count; z++) {
			ucstring_printf(s, DE_ENCODING_LATIN1, "%"I64_FMT, bf->locations[z]);
			if(z<bf->count-1) {
				ucstring_append_sz(s, ",", DE_ENCODING_LATIN1);
			}
		}
		if(bf->count>DE_BYTEFREQ_NUMLOC) {
			ucstring_append_sz(s, "...", DE_ENCODING_LATIN1);
		}

		de_msg(c, "%s", ucstring_getpsz(s));
	}
	de_msg(c, "      Total: %11"I64_FMT, c->infile->len);
	ucstring_destroy(s);
	de_free(c, bfctx);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rdi, %rbx
movl $0x2000, %esi           # imm = 0x2000
callq 0xdecaf
movq %rax, %r14
pushq $0x9
popq %rdx
movq %rbx, %rdi
xorl %esi, %esi
callq 0xe08d2
cmpl $0x2, %eax
pushq $0x1
popq %rsi
cmovnel %eax, %esi
leaq 0x18(%rsp), %rdi
callq 0xe11fc
movq 0x18(%rbx), %rdi
movq 0x18(%rdi), %rdx
leaq 0x19d1(%rip), %rcx      # 0x63698
xorl %esi, %esi
movq %r14, %r8
callq 0xe4745
leaq 0xb5982(%rip), %rsi     # 0x11765a
movq %rbx, %rdi
xorl %eax, %eax
callq 0xdf42e
movq %rbx, 0x10(%rsp)
movq %rbx, %rdi
callq 0xeac74
movq %rax, %r12
movq %r14, 0x8(%rsp)
leaq 0x8(%r14), %r13
pushq $0x5
popq %rbp
xorl %r15d, %r15d
cmpq $0x100, %r15            # imm = 0x100
je 0x61e49
movq %r15, %rbx
shlq $0x5, %rbx
movq 0x8(%rsp), %rax
cmpq $0x0, (%rax,%rbx)
je 0x61e3d
movq %r12, %rdi
callq 0xeac85
movq %r12, %rdi
movl %ebp, %esi
leaq 0xb5948(%rip), %rdx     # 0x117681
movl %r15d, %ecx
movl %r15d, %r8d
xorl %eax, %eax
callq 0xeae8b
movl %r15d, %edi
leaq 0x18(%rsp), %rsi
callq 0xe1679
leaq 0xbdd01(%rip), %rsi     # 0x11fa5b
cmpl $0xfffffff, %eax        # imm = 0xFFFFFFF
je 0x61d99
movl %eax, %r14d
movl %eax, %edi
callq 0xeb7bd
leaq 0xbdce9(%rip), %rsi     # 0x11fa5b
testl %eax, %eax
je 0x61d99
movq %r12, %rdi
leaq 0xa7a13(%rip), %rsi     # 0x109793
movl %ebp, %edx
callq 0xeae42
movq %r12, %rdi
movl %r14d, %esi
callq 0xeada5
leaq 0xa79fa(%rip), %rsi     # 0x109793
addq 0x8(%rsp), %rbx
movq %r12, %rdi
movl %ebp, %edx
callq 0xeae42
movq (%rbx), %rcx
movq %r12, %rdi
movl %ebp, %esi
leaq 0xb58d6(%rip), %rdx     # 0x11768d
xorl %eax, %eax
callq 0xeae8b
xorl %r14d, %r14d
movq (%rbx), %rax
cmpq $0x3, %r14
je 0x61e08
cmpq %r14, %rax
jle 0x61e1f
movq (%r13,%r14,8), %rcx
movq %r12, %rdi
movl %ebp, %esi
leaq 0xba7c1(%rip), %rdx     # 0x11c5a1
xorl %eax, %eax
callq 0xeae8b
movq (%rbx), %rax
decq %rax
cmpq %r14, %rax
jle 0x61e03
movq %r12, %rdi
leaq 0xb5899(%rip), %rsi     # 0x117695
movl %ebp, %edx
callq 0xeae42
incq %r14
jmp 0x61dc1
cmpq $0x4, %rax
jl 0x61e1f
movq %r12, %rdi
leaq 0xd7279(%rip), %rsi     # 0x139091
movl %ebp, %edx
callq 0xeae42
movq %r12, %rdi
callq 0xeb7e4
movq 0x10(%rsp), %rdi
leaq 0xa5ecc(%rip), %rsi     # 0x107cff
movq %rax, %rdx
xorl %eax, %eax
callq 0xdf42e
incq %r15
addq $0x20, %r13
jmp 0x61d01
movq 0x10(%rsp), %rbx
movq 0x18(%rbx), %rax
movq 0x18(%rax), %rdx
leaq 0xb583a(%rip), %rsi     # 0x117697
movq %rbx, %rdi
xorl %eax, %eax
callq 0xdf42e
movq %r12, %rdi
callq 0xeaf3c
movq %rbx, %rdi
movq 0x8(%rsp), %rsi
callq 0xded32
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq