// File: /workspace/github_c_makefile_star2_O3/jsummers[P]deark/src/deark-tar.c
// Function: de_tar_start_member_file
void de_tar_start_member_file(deark *c, dbuf *f)
{
	struct tar_ctx *tctx = NULL;
	struct tar_md *md = NULL;
	int tsidx;

	if(!c->tar_data) {
		de_tar_create_file(c);
	}
	tctx = (struct tar_ctx *)c->tar_data;
	if(!tctx) return;
	destroy_md(c, tctx->md);
	tctx->md = de_malloc(c, sizeof(struct tar_md));
	md = tctx->md;

	f->parent_dbuf = tctx->outf;

	md->headers_pos = tctx->outf->len;

	if(c->preserve_file_times_archives && f->fi_copy) {
		for(tsidx=0; tsidx<DE_TIMESTAMPIDX_COUNT; tsidx++) {
			//if(tsidx != DE_TIMESTAMPIDX_MODIFY) continue;

			if(f->fi_copy->timestamp[tsidx].is_valid) {
				md->tsdata[tsidx].timestamp = f->fi_copy->timestamp[tsidx];
			}
			else if(tsidx == DE_TIMESTAMPIDX_MODIFY) {
				// Special handling if we don't have a mod time.
				if(c->reproducible_output) {
					de_get_reproducible_timestamp(c, &md->tsdata[tsidx].timestamp);
				}
				else {
					de_cached_current_time_to_timestamp(c, &md->tsdata[tsidx].timestamp);
					// Although c->current_time is probably high precision, we treat it as
					// low precision, so as not to write an "mtime" extended header.
					// TODO: If we write "mtime" for some other reason, it can be high prec.
					md->tsdata[tsidx].timestamp.precision = DE_TSPREC_1SEC;
				}
			}
			else {
				// Unavailable timestamp that isn't the mod time.
				continue;
			}

			md->tsdata[tsidx].timestamp_unix = de_timestamp_to_unix_time(&md->tsdata[tsidx].timestamp);
		}
	}

	if(f->fi_copy && f->fi_copy->is_directory) {
		md->is_dir = 1;
	}

	md->namelen = de_strlen(f->name);
	if(md->is_dir) {
		// Append a '/' to directory names
		md->filename = de_malloc(c, (i64)md->namelen+2);
		de_snprintf(md->filename, md->namelen+2, "%s/", f->name);
		md->namelen = de_strlen(md->filename);
	}
	else {
		md->filename = de_strdup(c, f->name);
	}

	if(md->namelen>100) {
		md->need_exthdr_path = 1;
	}
	else if(!de_is_ascii((const u8*)md->filename, md->namelen)) {
		md->need_exthdr_path = 1;
	}

	md->extdata_nbytes_needed += 23; // For "size"; this is enough for 10TB

	if(md->need_exthdr_path) {
		// Likely an overestimate: up to 6 bytes for the item size,
		// 4 for the "path" string, 3 for field separators.
		md->extdata_nbytes_needed += (i64)md->namelen + 13;
	}

	prepare_timestamp_exthdr(c, md, DE_TIMESTAMPIDX_MODIFY);
	prepare_timestamp_exthdr(c, md, DE_TIMESTAMPIDX_ACCESS);
	prepare_timestamp_exthdr(c, md, DE_TIMESTAMPIDX_ATTRCHANGE);
	prepare_timestamp_exthdr(c, md, DE_TIMESTAMPIDX_CREATE);

	if(md->extdata_nbytes_needed>0) {
		md->has_exthdr = 1;
	}

	if(md->has_exthdr) {
		md->exthdr_num_data_blocks = (md->extdata_nbytes_needed+511)/512;
		md->headers_size = (1 + md->exthdr_num_data_blocks + 1) * 512;
	}
	else {
		md->exthdr_num_data_blocks = 0;
		md->headers_size = 512;
	}

	// Reserve space for the tar headers. We won't know the member file size
	// until it has been completely written, so we can't write the headers
	// yet. Instead we'll write them to headers_tmpdbuf, and seek back later
	// and patch them into the main tar file.
	dbuf_write_zeroes(tctx->outf, md->headers_size);

	f->offset_into_parent_dbuf = tctx->outf->len;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rsi, %rbx
movq %rdi, %r15
movq 0x140(%rdi), %r12
testq %r12, %r12
jne 0xedd2c
movq %r15, %rdi
callq 0xedbc0
movq 0x140(%r15), %r12
testq %r12, %r12
je 0xedf75
movq 0x20(%r12), %rsi
movq %r15, %rdi
callq 0xedccc
movl $0x180, %esi            # imm = 0x180
movq %r15, %rdi
callq 0xe8b2b
movq %rax, %r14
movq %rax, 0x20(%r12)
movq %r12, (%rsp)
movq 0x8(%r12), %rax
movq %rax, 0x50(%rbx)
movq 0x18(%rax), %rax
movq %rax, 0x10(%r14)
cmpb $0x0, 0xdd(%r15)
je 0xeddec
cmpq $0x0, 0xd0(%rbx)
je 0xeddec
leaq 0x40(%r14), %r12
xorl %ebp, %ebp
xorl %r13d, %r13d
movq 0xd0(%rbx), %rax
cmpb $0x0, 0x18(%rax,%r13)
je 0xeddae
addq %r13, %rax
addq $0x18, %rax
movups (%rax), %xmm0
movups %xmm0, (%r12,%r13,4)
leaq (%r12,%rbp), %rdi
callq 0xeacfd
movq %rax, 0x10(%r12,%r13,4)
jmp 0xeddb3
testq %r13, %r13
je 0xeddc3
addq $0x10, %r13
addq $0x40, %rbp
cmpq $0x50, %r13
jne 0xedd80
jmp 0xeddec
cmpb $0x0, 0xdf(%r15)
je 0xeddda
movq %r15, %rdi
movq %r12, %rsi
callq 0xeb9eb
jmp 0xedd9e
movq %r15, %rdi
movq %r12, %rsi
callq 0xeb0f7
movb $0x1e, 0x42(%r14)
jmp 0xedd9e
movq 0xd0(%rbx), %rax
testq %rax, %rax
je 0xede19
cmpb $0x0, 0x9(%rax)
je 0xede19
movb $0x1, (%r14)
leaq 0x68(%rbx), %r12
movq 0x68(%rbx), %rdi
callq 0x180e0
leaq 0x8(%r14), %rbp
movq %rax, 0x8(%r14)
jmp 0xede39
movb (%r14), %r13b
movq 0x68(%rbx), %r12
movq %r12, %rdi
callq 0x180e0
leaq 0x8(%r14), %rbp
movq %rax, 0x8(%r14)
testb %r13b, %r13b
je 0xede7b
leaq 0x68(%rbx), %r12
addq $0x2, %rax
movq %r15, %rdi
movq %rax, %rsi
callq 0xe8b2b
movq %rax, 0x38(%r14)
movq 0x8(%r14), %rsi
addq $0x2, %rsi
movq (%r12), %rcx
leaq 0x5b165(%rip), %rdx     # 0x148fc4
movq %rax, %rdi
xorl %eax, %eax
callq 0xe8193
movq 0x38(%r14), %r15
movq %r15, %rdi
callq 0x180e0
movq %rax, 0x8(%r14)
jmp 0xede91
movq %r15, %rdi
movq %r12, %rsi
callq 0xe9865
movq %rax, %r15
movq %rax, 0x38(%r14)
movq 0x8(%r14), %rax
cmpq $0x65, %rax
movq (%rsp), %r12
jae 0xedec6
movq %r15, %rdi
movq %rax, %rsi
callq 0xec446
testl %eax, %eax
je 0xedec6
movq 0x28(%r14), %rax
leaq 0x17(%rax), %rcx
cmpb $0x0, 0x3(%r14)
movq %rcx, 0x28(%r14)
je 0xedee4
movq %r14, %rcx
addq $0x28, %rcx
jmp 0xeded6
movb $0x1, 0x3(%r14)
movq %r14, %rcx
addq $0x28, %rcx
movq 0x28(%r14), %rax
movq (%rbp), %rdx
addq %rdx, %rax
addq $0x24, %rax
movq %rax, (%rcx)
movq %r14, %rdi
xorl %esi, %esi
callq 0xedf8d
movq %r14, %rdi
movl $0x2, %esi
callq 0xedf8d
movq %r14, %rdi
movl $0x3, %esi
callq 0xedf8d
movq %r14, %rdi
movl $0x1, %esi
callq 0xedf8d
movq 0x28(%r14), %rsi
testq %rsi, %rsi
jle 0xedf25
movb $0x1, 0x1(%r14)
jmp 0xedf2c
cmpb $0x0, 0x1(%r14)
je 0xedf84
leaq 0x1ff(%rsi), %rax
addq $0x3fe, %rsi            # imm = 0x3FE
testq %rax, %rax
cmovnsq %rax, %rsi
movq %rsi, %rax
sarq $0x9, %rax
andq $-0x200, %rsi           # imm = 0xFE00
addq $0x400, %rsi            # imm = 0x400
movq %rax, 0x20(%r14)
movq %rsi, 0x18(%r14)
movq 0x8(%r12), %rdi
callq 0xf0d35
movq 0x8(%r12), %rax
movq 0x18(%rax), %rax
movq %rax, 0x58(%rbx)
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x200, %esi            # imm = 0x200
xorl %eax, %eax
jmp 0xedf56