// File: /workspace/github_c_makefile_star2_O3/jsummers[P]deark/src/deark-user.c
// Function: make_output_filename
static char *make_output_filename(deark *c, const char *dirname, const char *fn,
	const char *suffix, unsigned int flags)
{
	char *newfn = NULL;
	const char *fn_part;
	size_t newfn_alloc;
	de_ucstring *tmps = NULL;
	i64 fnpartpos;

	tmps = ucstring_create(c);

	if(dirname && dirname[0]) {
		ucstring_append_sz(tmps, dirname, DE_ENCODING_UTF8);
		append_pathsep_if_needed(tmps);
	}

	fnpartpos = tmps->len;

	if(flags & 0x1) {
		// Use base filename only
		fn_part = get_basename_ptr(fn);
	}
	else {
		fn_part = fn;
	}
	if(fn_part) {
		ucstring_append_sz(tmps, fn_part, DE_ENCODING_UTF8);
	}

	if(tmps->len <= fnpartpos) {
		// Disallow empty filename
		ucstring_append_sz(tmps, "_", DE_ENCODING_LATIN1);
	}

	if(suffix) {
		ucstring_append_sz(tmps, suffix, DE_ENCODING_UTF8);
	}

	if(flags & 0x2) {
		// Remove path separators; sanitize
		i64 i;

		for(i=fnpartpos; i<tmps->len; i++) {
			if(is_pathsep(tmps->str[i])) {
				tmps->str[i] = '_';
			}

			if(i==fnpartpos && tmps->str[i]=='.') {
				tmps->str[i] = '_';
			}
		}
	}

#ifdef DE_WINDOWS
	if(flags & 0x4) {
		// When a filename-like option is supplied on the command line, there are
		// cases where we can't be agnostic about which characters are path
		// separators. One of them is when that name is written to an archive
		// (zip/tar) file.
		// Our rule is that, for Windows builds, both "\" and "/" are path
		// separators. For Unix builds, only "/" is, and "\" will be treated as
		// an ordinary filename character, if allowed by the archive format.
		// This difference in behavior on different platforms is unfortunate, but
		// I think it's the least bad thing to do.
		backslashes_to_slashes(tmps);
	}
#endif

	// Don't allow empty filename
	if(tmps->len<1) goto done;

	newfn_alloc = (size_t)ucstring_count_utf8_bytes(tmps) + 1;
	newfn = de_malloc(c, newfn_alloc);
	ucstring_to_sz(tmps, newfn, newfn_alloc, 0, DE_ENCODING_UTF8);

done:
	ucstring_destroy(tmps);
	return newfn;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movl %r8d, %ebp
movq %rcx, %r15
movq %rdx, %r12
movq %rsi, %r13
movq %rdi, %r14
callq 0xf6410
movq %rax, %rbx
testq %r13, %r13
je 0x108e5f
cmpb $0x0, (%r13)
je 0x108e5f
movq %rbx, %rdi
movq %r13, %rsi
movl $0x2, %edx
callq 0xf65e3
movq 0x10(%rbx), %rax
testq %rax, %rax
jle 0x108e5f
movq 0x8(%rbx), %rcx
cmpl $0x2f, -0x4(%rcx,%rax,4)
je 0x108e5f
movq %rbx, %rdi
movl $0x2f, %esi
callq 0xf6542
movq 0x10(%rbx), %r13
testb $0x1, %bpl
je 0x108e85
movb (%r12), %al
testb %al, %al
je 0x108e8a
leaq 0x1(%r12), %rcx
cmpb $0x2f, %al
cmoveq %rcx, %r12
movb (%rcx), %al
incq %rcx
testb %al, %al
jne 0x108e76
testq %r12, %r12
je 0x108ea0
movq %rbx, %rdi
movq %r12, %rsi
movl $0x2, %edx
callq 0xf65e3
cmpq %r13, 0x10(%rbx)
jg 0x108eb4
leaq 0x25add(%rip), %rsi     # 0x12e984
movq %rbx, %rdi
movl $0x5, %edx
callq 0xf65e3
testq %r15, %r15
je 0x108ec9
movq %rbx, %rdi
movq %r15, %rsi
movl $0x2, %edx
callq 0xf65e3
shrb %bpl
movq 0x10(%rbx), %rax
movq %rax, %rcx
subq %r13, %rcx
setg %dl
andb %bpl, %dl
cmpb $0x1, %dl
jne 0x108f10
shlq $0x2, %r13
addq 0x8(%rbx), %r13
xorl %edx, %edx
movl (%r13,%rdx,4), %esi
cmpl $0x2f, %esi
je 0x108eff
testq %rdx, %rdx
jne 0x108f08
cmpl $0x2e, %esi
jne 0x108f08
movl $0x5f, (%r13,%rdx,4)
incq %rdx
cmpq %rdx, %rcx
jne 0x108eeb
testq %rax, %rax
jle 0x108f47
movq %rbx, %rdi
callq 0xf6d4d
leaq 0x1(%rax), %r15
movq %r14, %rdi
movq %r15, %rsi
callq 0xe8b2b
movq %rax, %r14
movq %rbx, %rdi
movq %rax, %rsi
movq %r15, %rdx
xorl %ecx, %ecx
movl $0x2, %r8d
callq 0xf6e1a
jmp 0x108f4a
xorl %r14d, %r14d
movq %rbx, %rdi
callq 0xf6753
movq %r14, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq