// File: /workspace/github_c_makefile_star2_O1/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 0xf6870
movq %rax, %rbx
testq %r13, %r13
je 0x109378
cmpb $0x0, (%r13)
je 0x109378
movq %rbx, %rdi
movq %r13, %rsi
movl $0x2, %edx
callq 0xf6a40
movq 0x10(%rbx), %rax
testq %rax, %rax
jle 0x109378
testq %rbx, %rbx
je 0x10936b
movq 0x8(%rbx), %rcx
cmpl $0x2f, -0x4(%rcx,%rax,4)
je 0x109378
movq %rbx, %rdi
movl $0x2f, %esi
callq 0xf69a2
testb $0x1, %bpl
je 0x10939a
movb (%r12), %al
testb %al, %al
je 0x10939a
leaq 0x1(%r12), %rcx
cmpb $0x2f, %al
cmoveq %rcx, %r12
movb (%rcx), %al
incq %rcx
testb %al, %al
jne 0x10938b
movq 0x10(%rbx), %r13
testq %r12, %r12
je 0x1093b3
movq %rbx, %rdi
movq %r12, %rsi
movl $0x2, %edx
callq 0xf6a40
cmpq %r13, 0x10(%rbx)
jg 0x1093cd
leaq 0x252b4(%rip), %rsi     # 0x12e674
movq %rbx, %rdi
movl $0x5, %edx
callq 0xf6a40
testq %r15, %r15
je 0x1093e2
movq %rbx, %rdi
movq %r15, %rsi
movl $0x2, %edx
callq 0xf6a40
testb $0x2, %bpl
je 0x10942a
movq 0x10(%rbx), %rax
subq %r13, %rax
jle 0x10942a
shlq $0x2, %r13
addq 0x8(%rbx), %r13
xorl %ecx, %ecx
cmpl $0x2f, (%r13,%rcx,4)
jne 0x10940c
movl $0x5f, (%r13,%rcx,4)
testq %rcx, %rcx
jne 0x109422
cmpl $0x2e, (%r13,%rcx,4)
jne 0x109422
movl $0x5f, (%r13,%rcx,4)
incq %rcx
cmpq %rcx, %rax
jne 0x1093fb
cmpq $0x0, 0x10(%rbx)
jle 0x109463
movq %rbx, %rdi
callq 0xf71a1
leaq 0x1(%rax), %r15
movq %r14, %rdi
movq %r15, %rsi
callq 0xe969b
movq %rax, %r14
movq %rbx, %rdi
movq %rax, %rsi
movq %r15, %rdx
xorl %ecx, %ecx
movl $0x2, %r8d
callq 0xf727b
jmp 0x109466
xorl %r14d, %r14d
movq %rbx, %rdi
callq 0xf6bb0
movq %r14, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq