// File: /workspace/github_c_makefile_star2_O1/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 0xe969b
movq %rax, %r14
movq %rbx, %rdi
xorl %esi, %esi
movl $0x9, %edx
callq 0xeb7d1
cmpl $0x2, %eax
movl $0x1, %esi
cmovnel %eax, %esi
leaq 0x18(%rsp), %rdi
callq 0xec207
movq 0x18(%rbx), %rdi
movq 0x18(%rdi), %rdx
leaq 0x1a5b(%rip), %rcx      # 0x67101
xorl %esi, %esi
movq %r14, %r8
callq 0xefc28
leaq 0xbef83(%rip), %rsi     # 0x12463a
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9fc3
movq %rbx, 0x10(%rsp)
movq %rbx, %rdi
callq 0xf6870
movq %rax, %r12
movq %r14, 0x8(%rsp)
addq $0x8, %r14
leaq 0xc3ed0(%rip), %r15     # 0x1295b1
xorl %r13d, %r13d
movq %r13, %rbp
shlq $0x5, %rbp
movq 0x8(%rsp), %rax
cmpq $0x0, (%rax,%rbp)
je 0x6581f
movq %r12, %rdi
callq 0xf6883
movq %r12, %rdi
movl $0x5, %esi
leaq 0xbef4f(%rip), %rdx     # 0x124661
movl %r13d, %ecx
movl %r13d, %r8d
xorl %eax, %eax
callq 0xf6ab0
movl %r13d, %edi
leaq 0x18(%rsp), %rsi
callq 0xec6fd
leaq 0xc72b0(%rip), %rsi     # 0x12c9e3
cmpl $0xfffffff, %eax        # imm = 0xFFFFFFF
je 0x65773
movl %eax, %ebx
movl %eax, %edi
callq 0xf7497
leaq 0xc7299(%rip), %rsi     # 0x12c9e3
testl %eax, %eax
je 0x65773
movq %r12, %rdi
leaq 0xb101b(%rip), %rsi     # 0x116773
movl $0x5, %edx
callq 0xf6a40
movq %r12, %rdi
movl %ebx, %esi
callq 0xf69a2
leaq 0xb1000(%rip), %rsi     # 0x116773
addq 0x8(%rsp), %rbp
movq %r12, %rdi
movl $0x5, %edx
callq 0xf6a40
movq (%rbp), %rcx
movq %r12, %rdi
movl $0x5, %esi
leaq 0xbeed5(%rip), %rdx     # 0x12466d
xorl %eax, %eax
callq 0xf6ab0
xorl %ebx, %ebx
cmpq %rbx, (%rbp)
jle 0x657e6
movq (%r14,%rbx,8), %rcx
movq %r12, %rdi
movl $0x5, %esi
movq %r15, %rdx
xorl %eax, %eax
callq 0xf6ab0
movq (%rbp), %rax
decq %rax
cmpq %rbx, %rax
jle 0x657dd
movq %r12, %rdi
leaq 0xbeea2(%rip), %rsi     # 0x124675
movl $0x5, %edx
callq 0xf6a40
incq %rbx
cmpq $0x3, %rbx
jne 0x657a1
cmpq $0x4, (%rbp)
jl 0x65801
movq %r12, %rdi
leaq 0xe07fa(%rip), %rsi     # 0x145ff1
movl $0x5, %edx
callq 0xf6a40
movq %r12, %rdi
callq 0xf74bd
movq 0x10(%rsp), %rdi
leaq 0xaf49a(%rip), %rsi     # 0x114caf
movq %rax, %rdx
xorl %eax, %eax
callq 0xe9fc3
incq %r13
addq $0x20, %r14
cmpq $0x100, %r13            # imm = 0x100
jne 0x656e4
movq 0x10(%rsp), %rbx
movq 0x18(%rbx), %rax
movq 0x18(%rax), %rdx
leaq 0xbee30(%rip), %rsi     # 0x124677
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9fc3
movq %r12, %rdi
callq 0xf6bb0
movq %rbx, %rdi
movq 0x8(%rsp), %rsi
callq 0xe9752
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq