// File: /workspace/github_c_makefile_star2_O3/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 0xe8b2b
movq %rax, %r14
movq %rbx, %rdi
xorl %esi, %esi
movl $0x9, %edx
callq 0xeb220
cmpl $0x2, %eax
movl $0x1, %esi
cmovnel %eax, %esi
leaq 0x18(%rsp), %rdi
callq 0xebbf7
movq 0x18(%rbx), %rdi
movq 0x18(%rdi), %rdx
leaq 0x1a42(%rip), %rcx      # 0x66ee4
xorl %esi, %esi
movq %r14, %r8
callq 0xef5e4
leaq 0xbf3a7(%rip), %rsi     # 0x12485a
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9461
movq %rbx, 0x10(%rsp)
movq %rbx, %rdi
callq 0xf6410
movq %rax, %r12
movq %r14, 0x8(%rsp)
addq $0x8, %r14
leaq 0xc4304(%rip), %r15     # 0x1297e1
xorl %r13d, %r13d
movq %r13, %rbp
shlq $0x5, %rbp
movq 0x8(%rsp), %rax
cmpq $0x0, (%rax,%rbp)
je 0x6561b
movq %r12, %rdi
callq 0xf6423
movq %r12, %rdi
movl $0x5, %esi
leaq 0xbf373(%rip), %rdx     # 0x124881
movl %r13d, %ecx
movl %r13d, %r8d
xorl %eax, %eax
callq 0xf6653
movl %r13d, %edi
leaq 0x18(%rsp), %rsi
callq 0xec0ad
leaq 0xc779c(%rip), %rsi     # 0x12cccb
cmpl $0xfffffff, %eax        # imm = 0xFFFFFFF
je 0x6556f
movl %eax, %ebx
movl %eax, %edi
callq 0xf7036
leaq 0xc7785(%rip), %rsi     # 0x12cccb
testl %eax, %eax
je 0x6556f
movq %r12, %rdi
leaq 0xb12cf(%rip), %rsi     # 0x116823
movl $0x5, %edx
callq 0xf65e3
movq %r12, %rdi
movl %ebx, %esi
callq 0xf6542
leaq 0xb12b4(%rip), %rsi     # 0x116823
addq 0x8(%rsp), %rbp
movq %r12, %rdi
movl $0x5, %edx
callq 0xf65e3
movq (%rbp), %rcx
movq %r12, %rdi
movl $0x5, %esi
leaq 0xbf2f9(%rip), %rdx     # 0x12488d
xorl %eax, %eax
callq 0xf6653
xorl %ebx, %ebx
cmpq %rbx, (%rbp)
jle 0x655fd
movq (%r14,%rbx,8), %rcx
movq %r12, %rdi
movl $0x5, %esi
movq %r15, %rdx
xorl %eax, %eax
callq 0xf6653
movq (%rbp), %rax
decq %rax
cmpq %rbx, %rax
jle 0x655d9
movq %r12, %rdi
leaq 0xbf2c6(%rip), %rsi     # 0x124895
movl $0x5, %edx
callq 0xf65e3
incq %rbx
cmpq $0x3, %rbx
jne 0x6559d
cmpq $0x4, (%rbp)
jl 0x655fd
movq %r12, %rdi
leaq 0xe0f6e(%rip), %rsi     # 0x146561
movl $0x5, %edx
callq 0xf65e3
movq %r12, %rdi
callq 0xf705c
movq 0x10(%rsp), %rdi
leaq 0xaf74e(%rip), %rsi     # 0x114d5f
movq %rax, %rdx
xorl %eax, %eax
callq 0xe9461
incq %r13
addq $0x20, %r14
cmpq $0x100, %r13            # imm = 0x100
jne 0x654e0
movq 0x10(%rsp), %rbx
movq 0x18(%rbx), %rax
movq 0x18(%rax), %rdx
leaq 0xbf254(%rip), %rsi     # 0x124897
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9461
movq %r12, %rdi
callq 0xf6753
movq %rbx, %rdi
movq 0x8(%rsp), %rsi
callq 0xe8be2
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq