// File: /workspace/github_c_makefile_star2_O3/jsummers[P]deark/src/deark-dbuf.c
// Function: dbuf_create_output_file
dbuf *dbuf_create_output_file(deark *c, const char *ext1, de_finfo *fi,
	unsigned int createflags)
{
	char nbuf[500];
	char msgbuf[200];
	char ext[128];
	int have_ext;
	dbuf *f;
	const char *basefn;
	u8 is_directory = 0;
	char *name_from_finfo = NULL;
	i64 name_from_finfo_len = 0;

	if(ext1) {
		have_ext = 1;
		sanitize_ext(ext1, ext, sizeof(ext));
	}
	else {
		have_ext = 0;
		ext[0] = '\0';
	}

	if(have_ext && fi && fi->original_filename_flag) {
		de_dbg(c, "[internal warning: Incorrect use of create_output_file]");
	}

	f = create_dbuf_lowlevel(c);
	f->max_len_hard = c->max_output_file_size;
	f->is_managed = 1;

	if(fi && fi->is_volume_label) {
		if(c->output_style!=DE_OUTPUTSTYLE_ARCHIVE || c->archive_fmt!=DE_ARCHIVEFMT_ZIP) {
			de_dbg(c, "skipping volume label");
			f->btype = DBUF_TYPE_NULL;
			goto done;
		}
	}

	if(fi && fi->is_directory) {
		is_directory = 1;
	}

	if(is_directory && !c->keep_dir_entries) {
		de_dbg(c, "skipping 'directory' file");
		f->btype = DBUF_TYPE_NULL;
		goto done;
	}

	if(c->extract_policy==DE_EXTRACTPOLICY_MAINONLY) {
		if(createflags&DE_CREATEFLAG_IS_AUX) {
			de_dbg(c, "skipping 'auxiliary' file");
			f->btype = DBUF_TYPE_NULL;
			goto done;
		}
	}
	else if(c->extract_policy==DE_EXTRACTPOLICY_AUXONLY) {
		if(!(createflags&DE_CREATEFLAG_IS_AUX)) {
			de_dbg(c, "skipping 'main' file");
			f->btype = DBUF_TYPE_NULL;
			goto done;
		}
	}

	f->file_id = c->file_count;
	c->file_count++;

	basefn = c->base_output_filename ? c->base_output_filename : "output";

	if(fi && ucstring_isnonempty(fi->file_name_internal)) {
		name_from_finfo_len = 1 + ucstring_count_utf8_bytes(fi->file_name_internal);
		name_from_finfo = de_malloc(c, name_from_finfo_len);
		ucstring_to_sz(fi->file_name_internal, name_from_finfo, (size_t)name_from_finfo_len, 0,
			DE_ENCODING_UTF8);
	}

	if(c->output_style==DE_OUTPUTSTYLE_ARCHIVE && !c->base_output_filename &&
		fi && fi->is_directory &&
		(fi->is_root_dir || (fi->detect_root_dot_dir && fi->orig_name_was_dot)))
	{
		de_strlcpy(nbuf, ".", sizeof(nbuf));
	}
	else if(c->special_1st_filename && (f->file_id==c->first_output_file) &&
		!is_directory)
	{
		de_strlcpy(nbuf, c->special_1st_filename, sizeof(nbuf));
	}
	else if(c->output_style==DE_OUTPUTSTYLE_ARCHIVE && !c->base_output_filename &&
		fi && fi->original_filename_flag && name_from_finfo)
	{
		// TODO: This is a "temporary" hack to allow us to, when both reading from
		// and writing to an archive format, use some semblance of the correct
		// filename (instead of "output.xxx.yyy").
		// There are some things that we don't handle optimally, such as
		// subdirectories.
		// A major redesign of the file naming logic would be good.
		de_strlcpy(nbuf, name_from_finfo, sizeof(nbuf));
	}
	else {
		char fn_suffix[256];

		if(have_ext && name_from_finfo) {
			de_snprintf(fn_suffix, sizeof(fn_suffix), "%s.%s", name_from_finfo, ext);
		}
		else if(have_ext) {
			de_strlcpy(fn_suffix, ext, sizeof(fn_suffix));
		}
		else if(is_directory && name_from_finfo) {
			de_snprintf(fn_suffix, sizeof(fn_suffix), "%s.dir", name_from_finfo);
		}
		else if(name_from_finfo) {
			de_strlcpy(fn_suffix, name_from_finfo, sizeof(fn_suffix));
		}
		else if(is_directory) {
			de_strlcpy(fn_suffix, "dir", sizeof(fn_suffix));
		}
		else {
			de_strlcpy(fn_suffix, "bin", sizeof(fn_suffix));
		}

		de_snprintf(nbuf, sizeof(nbuf), "%s.%03d.%s", basefn, f->file_id, fn_suffix);
	}

	f->name = de_strdup(c, nbuf);

	if(fi) {
		// The finfo object passed to us at file creation is not required to
		// remain valid, so make a copy of anything in it that we might need
		// later.
		f->fi_copy = de_finfo_create(c);
		finfo_shallow_copy(c, fi, f->fi_copy);
		fi->riscos_appended_type = 0;

		// Here's where we respect the -intz option, by using it to convert to
		// UTC in some cases.
		if(f->fi_copy->timestamp[DE_TIMESTAMPIDX_MODIFY].is_valid && f->fi_copy->timestamp[DE_TIMESTAMPIDX_MODIFY].tzcode==DE_TZCODE_LOCAL &&
			c->input_tz_offs_seconds!=0)
		{
			de_timestamp_cvt_to_utc(&f->fi_copy->timestamp[DE_TIMESTAMPIDX_MODIFY], -c->input_tz_offs_seconds);
		}

		if(f->fi_copy->internal_mod_time.is_valid && f->fi_copy->internal_mod_time.tzcode==DE_TZCODE_LOCAL &&
			c->input_tz_offs_seconds!=0)
		{
			de_timestamp_cvt_to_utc(&f->fi_copy->internal_mod_time, -c->input_tz_offs_seconds);
		}
	}

	if(f->file_id < c->first_output_file) {
		f->btype = DBUF_TYPE_NULL;
		goto done;
	}

	if(f->file_id >= c->first_output_file + c->max_output_files)
	{
		f->btype = DBUF_TYPE_NULL;
		if(f->file_id == c->first_output_file + c->max_output_files) {
			if(!c->user_set_max_output_files) {
				de_err(c, "Limit of %d output files exceeded", c->max_output_files);
			}
		}
		goto done;
	}

	c->num_files_extracted++;

	if(c->extrlist_dbuf) {
		dbuf_printf(c->extrlist_dbuf, "%s\n", f->name);
		dbuf_flush_lowlevel(c->extrlist_dbuf);
	}

	if(c->enable_wbuffer_test && !(createflags & DE_CREATEFLAG_NO_WBUFFER))
	{
		dbuf_enable_wbuffer(f);
	}

	if(c->enable_oinfo) {
		f->crco_for_oinfo = de_crcobj_create(c, DE_CRCOBJ_CRC32_IEEE);
	}

	if(c->list_mode) {
		f->btype = DBUF_TYPE_NULL;
		if(c->list_mode_include_file_id) {
			de_msg(c, "%d:%s", f->file_id, f->name);
		}
		else {
			de_msg(c, "%s", f->name);
		}
		goto done;
	}

	if(c->output_style==DE_OUTPUTSTYLE_ARCHIVE && c->archive_fmt==DE_ARCHIVEFMT_TAR) {
		de_info(c, "Adding %s to TAR file", f->name);
		f->btype = DBUF_TYPE_ODBUF;
		// A dummy max_len_hard value. The parent will do the checking.
		f->max_len_hard = DE_DUMMY_MAX_FILE_SIZE;
		f->writing_to_tar_archive = 1;
		de_tar_start_member_file(c, f);
	}
	else if(c->output_style==DE_OUTPUTSTYLE_ARCHIVE) { // ZIP
		i64 initial_alloc;
		de_info(c, "Adding %s to ZIP file", f->name);
		f->btype = DBUF_TYPE_MEMBUF;
		f->max_len_hard = DE_MAX_MEMBUF_SIZE;
		if(is_directory) {
			// A directory entry is not expected to have any data associated
			// with it (besides the files it contains).
			initial_alloc = 16;
		}
		else {
			initial_alloc = 65536;
		}
		f->membuf_buf = de_malloc(c, initial_alloc);
		f->membuf_alloc = initial_alloc;
		f->write_memfile_to_zip_archive = 1;
	}
	else if(c->output_style==DE_OUTPUTSTYLE_STDOUT) {
		de_info(c, "Writing %s to [stdout]", f->name);
		f->btype = DBUF_TYPE_STDOUT;
		// TODO: Should we increase f->max_len_hard?
		f->fp = stdout;
	}
	else {
		de_info(c, "Writing %s", f->name);
		f->btype = DBUF_TYPE_OFILE;
		f->fp = de_fopen_for_write(c, f->name, msgbuf, sizeof(msgbuf),
			c->overwrite_mode, 0);

		if(!f->fp) {
			de_err(c, "Failed to write %s: %s", f->name, msgbuf);
			f->btype = DBUF_TYPE_NULL;
			c->serious_error_flag = 1;
		}
	}

done:
	de_free(c, name_from_finfo);
	return f;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x398, %rsp            # imm = 0x398
movl %ecx, %r15d
movq %rdx, %r12
movq %rsi, %r13
movq %rdi, %rbx
testq %rsi, %rsi
je 0xefe50
leaq 0x20(%rsp), %r14
movl $0x80, %edx
movq %r14, %rdi
movq %r13, %rsi
callq 0xe8219
movb (%r14), %dl
testb %dl, %dl
je 0xefe2d
leaq 0x21(%rsp), %rax
movabsq $0x1000000000000d, %rcx # imm = 0x1000000000000D
leal -0x30(%rdx), %esi
cmpb $0xa, %sil
jb 0xefe1c
movl %edx, %esi
andb $-0x21, %sil
addb $-0x41, %sil
cmpb $0x1a, %sil
jb 0xefe1c
movzbl %dl, %edx
addl $-0x2b, %edx
cmpl $0x34, %edx
ja 0xefe27
btq %rdx, %rcx
jae 0xefe27
movb (%rax), %dl
incq %rax
testb %dl, %dl
jne 0xefdf2
jmp 0xefe2d
movb $0x5f, -0x1(%rax)
jmp 0xefe1c
testq %r12, %r12
je 0xefe5e
movb $0x1, %bpl
cmpb $0x0, 0x8(%r12)
je 0xefe60
leaq 0x593ed(%rip), %rsi     # 0x149231
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8617
jmp 0xefe60
movb $0x0, 0x20(%rsp)
testq %r12, %r12
setne %bpl
jmp 0xefe60
xorl %ebp, %ebp
movl $0xd8, %esi
movq %rbx, %rdi
callq 0xe8b2b
movq %rax, %r14
movq %rbx, 0x8(%rax)
movl $0xffffffff, 0x64(%rax) # imm = 0xFFFFFFFF
movq 0xb0(%rbx), %rax
movq %rax, 0x20(%r14)
movb $0x1, 0x4(%r14)
movb $0x1, %cl
testb %bpl, %bpl
je 0xefecd
cmpb $0x0, 0xa(%r12)
je 0xefeb1
cmpl $0x1, 0x7c(%rbx)
jne 0xeff81
cmpl $0x1, 0x80(%rbx)
jne 0xeff81
cmpb $0x0, 0x9(%r12)
je 0xefecd
cmpb $0x0, 0xcf(%rbx)
je 0xeffc9
movb $0x1, %dil
xorl %ecx, %ecx
jmp 0xefecf
xorl %edi, %edi
movl 0x8c(%rbx), %eax
cmpl $0x2, %eax
je 0xefef1
cmpl $0x1, %eax
jne 0xeff03
testb $0x1, %r15b
je 0xeff03
leaq 0x593ad(%rip), %rsi     # 0x149299
jmp 0xeffd0
testb $0x1, %r15b
jne 0xeff03
leaq 0x593b5(%rip), %rsi     # 0x1492b3
jmp 0xeffd0
movl 0x38(%rbx), %eax
movl %eax, 0x64(%r14)
incl %eax
movl %eax, 0x38(%rbx)
movq 0x150(%rbx), %rax
testq %rax, %rax
leaq 0x5bcfe(%rip), %rdx     # 0x14bc1e
cmovneq %rax, %rdx
movq %rdx, 0x10(%rsp)
testb %bpl, %bpl
movl %r15d, 0x1c(%rsp)
movl %ecx, 0xc(%rsp)
je 0xeff7c
movl %edi, 0x8(%rsp)
movq (%r12), %rdi
callq 0xf67a3
testl %eax, %eax
je 0xeff8a
movq (%r12), %rdi
callq 0xf6d4d
leaq 0x1(%rax), %r15
movq %rbx, %rdi
movq %r15, %rsi
callq 0xe8b2b
movq (%r12), %rdi
movq %rax, %rsi
movq %r15, %rdx
movq %rax, %r15
xorl %ecx, %ecx
movl $0x2, %r8d
callq 0xf6e1a
jmp 0xeff8d
xorl %r15d, %r15d
jmp 0xeff91
leaq 0x592e1(%rip), %rsi     # 0x149269
jmp 0xeffd0
xorl %r15d, %r15d
movl 0x8(%rsp), %edi
movl 0x7c(%rbx), %eax
cmpl $0x1, %eax
jne 0xefff2
cmpq $0x0, 0x150(%rbx)
setne %cl
movl %ebp, %edx
xorb $0x1, %dl
orb %cl, %dl
jne 0xefff2
cmpb $0x0, 0x9(%r12)
je 0xefff2
cmpb $0x0, 0xb(%r12)
je 0xeffe2
leaq 0x4b9aa(%rip), %rsi     # 0x13b96e
jmp 0xf0077
leaq 0x592af(%rip), %rsi     # 0x14927f
xorl %r15d, %r15d
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8617
jmp 0xf0207
cmpb $0x0, 0xc(%r12)
je 0xefff2
cmpb $0x0, 0xd(%r12)
jne 0xeffbd
movq 0x158(%rbx), %rsi
testq %rsi, %rsi
je 0xf0010
movl 0x64(%r14), %ecx
cmpl 0x98(%rbx), %ecx
setne %cl
orb %dil, %cl
je 0xf0077
cmpl $0x1, %eax
jne 0xf0048
cmpq $0x0, 0x150(%rbx)
setne %al
movl %ebp, %ecx
xorb $0x1, %cl
orb %al, %cl
jne 0xf0048
testq %r15, %r15
je 0xf0048
cmpb $0x0, 0x8(%r12)
je 0xf0048
leaq 0x1a0(%rsp), %rdi
movl $0x1f4, %edx            # imm = 0x1F4
movq %r15, %rsi
jmp 0xf0084
testq %r13, %r13
je 0xf008b
testq %r15, %r15
je 0xf008b
leaq 0x4aac1(%rip), %rdx     # 0x13ab1a
leaq 0xa0(%rsp), %rdi
leaq 0x20(%rsp), %r8
movl $0x100, %esi            # imm = 0x100
movq %r15, %rcx
xorl %eax, %eax
callq 0xe8193
jmp 0xf00ab
leaq 0x1a0(%rsp), %rdi
movl $0x1f4, %edx            # imm = 0x1F4
callq 0xe8219
jmp 0xf00da
testq %r13, %r13
je 0xf032a
leaq 0xa0(%rsp), %rdi
leaq 0x20(%rsp), %rsi
movl $0x100, %edx            # imm = 0x100
callq 0xe8219
movq 0x10(%rsp), %r13
movl 0x64(%r14), %r8d
leaq 0x59214(%rip), %rdx     # 0x1492cf
leaq 0x1a0(%rsp), %rdi
leaq 0xa0(%rsp), %r9
movl $0x1f4, %esi            # imm = 0x1F4
movq %r13, %rcx
xorl %eax, %eax
callq 0xe8193
leaq 0x1a0(%rsp), %rsi
movq %rbx, %rdi
callq 0xe9865
movq %rax, 0x68(%r14)
testb %bpl, %bpl
je 0xf01f9
movq %rbx, %rdi
callq 0xea504
movq %rax, 0xd0(%r14)
movb 0x9(%r12), %cl
movb %cl, 0x9(%rax)
movb 0xa(%r12), %cl
movb %cl, 0xa(%rax)
movb 0x10(%r12), %cl
movb %cl, 0x10(%rax)
movb 0x11(%r12), %cl
movb %cl, 0x11(%rax)
movl 0xa0(%r12), %ecx
movl %ecx, 0xa0(%rax)
movl 0x14(%r12), %ecx
movl %ecx, 0x14(%rax)
movl $0x18, %ecx
movups (%r12,%rcx), %xmm0
movups %xmm0, (%rax,%rcx)
addq $0x10, %rcx
cmpq $0x68, %rcx
jne 0xf0141
movups 0x68(%r12), %xmm0
movups %xmm0, 0x68(%rax)
movups 0x78(%r12), %xmm0
movups %xmm0, 0x78(%rax)
movq 0x88(%r12), %rcx
movq %rcx, 0x88(%rax)
movb 0xe(%r12), %cl
movb %cl, 0xe(%rax)
movq 0x98(%r12), %rcx
movq %rcx, 0x98(%rax)
movq 0xa4(%r12), %rcx
movq %rcx, 0xa4(%rax)
movb $0x0, 0x11(%r12)
movq 0xd0(%r14), %rdi
cmpb $0x0, 0x18(%rdi)
je 0xf01d5
cmpb $0x2, 0x19(%rdi)
jne 0xf01d5
movq 0x100(%rbx), %rsi
testq %rsi, %rsi
je 0xf01d5
addq $0x18, %rdi
negq %rsi
callq 0xead31
movq 0xd0(%r14), %rdi
cmpb $0x0, 0x68(%rdi)
je 0xf01f9
cmpb $0x2, 0x69(%rdi)
jne 0xf01f9
movq 0x100(%rbx), %rsi
testq %rsi, %rsi
je 0xf01f9
addq $0x68, %rdi
negq %rsi
callq 0xead31
movl 0x64(%r14), %eax
movl 0x98(%rbx), %ecx
cmpl %ecx, %eax
jge 0xf022e
movl $0x0, (%r14)
movq %rbx, %rdi
movq %r15, %rsi
callq 0xe8be2
movq %r14, %rax
addq $0x398, %rsp            # imm = 0x398
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl 0x9c(%rbx), %edx
addl %edx, %ecx
cmpl %ecx, %eax
jge 0xf02fa
incl 0x3c(%rbx)
movq 0x148(%rbx), %rdi
testq %rdi, %rdi
je 0xf026b
movq 0x68(%r14), %rdx
leaq 0x20534(%rip), %rsi     # 0x11078c
xorl %eax, %eax
callq 0xf08da
movq 0x148(%rbx), %rdi
callq 0xf0993
btl $0x9, 0x1c(%rsp)
jb 0xf029e
cmpb $0x0, 0x12b(%rbx)
je 0xf029e
movq 0x8(%r14), %rdi
cmpb $0x0, 0x12c(%rdi)
jne 0xf029e
cmpq $0x0, 0x48(%r14)
jne 0xf029e
movl $0x200, %esi            # imm = 0x200
callq 0xe8b2b
movq %rax, 0x48(%r14)
cmpb $0x0, 0x96(%rbx)
je 0xf02bb
movq %rbx, %rdi
movl $0x10, %esi
callq 0xeb499
movq %rax, 0x80(%r14)
cmpb $0x0, 0x94(%rbx)
je 0xf0365
movl $0x0, (%r14)
cmpb $0x0, 0x95(%rbx)
je 0xf03c6
movl 0x64(%r14), %edx
movq 0x68(%r14), %rcx
leaq 0x59011(%rip), %rsi     # 0x1492fc
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9461
jmp 0xf020e
movl $0x0, (%r14)
jne 0xf020e
cmpb $0x0, 0xa0(%rbx)
jne 0xf020e
leaq 0x58fbf(%rip), %rsi     # 0x1492da
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9295
jmp 0xf020e
testq %r15, %r15
sete %al
xorb $0x1, %dil
orb %al, %dil
movq 0x10(%rsp), %r13
jne 0xf03e0
leaq 0x58f7f(%rip), %rdx     # 0x1492c8
leaq 0xa0(%rsp), %rdi
movl $0x100, %esi            # imm = 0x100
movq %r15, %rcx
xorl %eax, %eax
callq 0xe8193
jmp 0xf00b0
movl 0x7c(%rbx), %eax
cmpl $0x2, %eax
je 0xf03fe
cmpl $0x1, %eax
jne 0xf042d
cmpl $0x2, 0x80(%rbx)
movq 0x68(%r14), %rdx
jne 0xf04b6
leaq 0x58f70(%rip), %rsi     # 0x149302
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9393
movl $0x8, (%r14)
movabsq $0x100000000000000, %rax # imm = 0x100000000000000
movq %rax, 0x20(%r14)
movb $0x1, 0x61(%r14)
movq %rbx, %rdi
movq %r14, %rsi
callq 0xedcf7
jmp 0xf020e
movq 0x68(%r14), %rdx
leaq 0x2498e(%rip), %rsi     # 0x114d5f
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9461
jmp 0xf020e
testq %r15, %r15
je 0xf04a6
leaq 0xa0(%rsp), %rdi
movl $0x100, %edx            # imm = 0x100
movq %r15, %rsi
jmp 0xf0521
movq 0x68(%r14), %rdx
leaq 0x58f25(%rip), %rsi     # 0x14932e
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9393
movl $0x5, (%r14)
movq 0x9ebaf(%rip), %rax     # 0x18efd0
movq (%rax), %rax
movq %rax, 0x10(%r14)
jmp 0xf020e
movq 0x68(%r14), %rdx
leaq 0x58f0d(%rip), %rsi     # 0x149345
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9393
movl $0x2, (%r14)
movq 0x68(%r14), %rsi
movl 0xd8(%rbx), %r8d
leaq 0xa0(%rsp), %rdx
movl $0xc8, %ecx
movq %rbx, %rdi
xorl %r9d, %r9d
callq 0x1094ab
movq %rax, 0x10(%r14)
testq %rax, %rax
jne 0xf020e
movq 0x68(%r14), %rdx
leaq 0x58d96(%rip), %rsi     # 0x14921a
leaq 0xa0(%rsp), %rcx
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9295
movl $0x0, (%r14)
movb $0x1, 0x4c(%rbx)
jmp 0xf020e
cmpb $0x0, 0xc(%rsp)
je 0xf050d
leaq 0x45309(%rip), %rsi     # 0x1357bd
jmp 0xf0514
leaq 0x58e5b(%rip), %rsi     # 0x149318
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9393
movl $0x3, (%r14)
movq $0x77359400, 0x20(%r14) # imm = 0x77359400
cmpb $0x0, 0xc(%rsp)
movl $0x10000, %eax          # imm = 0x10000
movq %r15, %r12
movl $0x10, %r15d
cmovneq %rax, %r15
movq %rbx, %rdi
movq %r15, %rsi
callq 0xe8b2b
movq %rax, 0x78(%r14)
movq %r15, 0x70(%r14)
movq %r12, %r15
movb $0x1, 0x60(%r14)
jmp 0xf020e
leaq 0x500d3(%rip), %rsi     # 0x1405e7
leaq 0xa0(%rsp), %rdi
movl $0x100, %edx            # imm = 0x100
callq 0xe8219
jmp 0xf00b0