// File: /workspace/github_c_makefile_star2_O2/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 0xeac74
movq %rax, %rbx
testq %r13, %r13
je 0xfc3f8
cmpb $0x0, (%r13)
je 0xfc3f8
pushq $0x2
popq %rdx
movq %rbx, %rdi
movq %r13, %rsi
callq 0xeae42
movq 0x10(%rbx), %rax
testq %rax, %rax
jle 0xfc3f8
movq 0x8(%rbx), %rcx
cmpl $0x2f, -0x4(%rcx,%rax,4)
je 0xfc3f8
pushq $0x2f
popq %rsi
movq %rbx, %rdi
callq 0xeada5
testb $0x1, %bpl
jne 0xfc483
movq 0x10(%rbx), %r13
testq %r12, %r12
je 0xfc41f
pushq $0x2
popq %rdx
movq %rbx, %rdi
movq %r12, %rsi
callq 0xeae42
cmpq %r13, 0x10(%rbx)
jg 0xfc431
leaq 0x252d6(%rip), %rsi     # 0x1216fc
pushq $0x5
popq %rdx
movq %rbx, %rdi
callq 0xeae42
testq %r15, %r15
je 0xfc444
pushq $0x2
popq %rdx
movq %rbx, %rdi
movq %r15, %rsi
callq 0xeae42
movq 0x10(%rbx), %rax
testb $0x2, %bpl
jne 0xfc49f
testq %rax, %rax
jle 0xfc4cd
movq %rbx, %rdi
callq 0xeb519
leaq 0x1(%rax), %r15
movq %r14, %rdi
movq %r15, %rsi
callq 0xdecaf
movq %rax, %r14
pushq $0x2
popq %r8
movq %rbx, %rdi
movq %rax, %rsi
movq %r15, %rdx
xorl %ecx, %ecx
callq 0xeb5de
jmp 0xfc4d0
leaq 0x1(%r12), %rax
movb -0x1(%rax), %cl
testb %cl, %cl
je 0xfc402
cmpb $0x2f, %cl
cmoveq %rax, %r12
incq %rax
jmp 0xfc488
xorl %ecx, %ecx
cmpq %rax, %r13
jge 0xfc44e
movq 0x8(%rbx), %rdx
movl (%rdx,%r13,4), %esi
cmpl $0x2f, %esi
je 0xfc4bd
testq %rcx, %rcx
jne 0xfc4c5
cmpl $0x2e, %esi
jne 0xfc4c5
movl $0x5f, (%rdx,%r13,4)
incq %r13
decq %rcx
jmp 0xfc4a1
xorl %r14d, %r14d
movq %rbx, %rdi
callq 0xeaf3c
movq %r14, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq