// File: /workspace/github_c_makefile_star2_O2/j-hoppe[P]tu58fs/xxdp.c
// Function: xxdp_filesystem_file_add
int xxdp_filesystem_file_add(xxdp_filesystem_t *_this, char *hostfname, time_t hostfdate,
		uint8_t *data, uint32_t data_size) {

	if (!strcasecmp(hostfname, XXDP_VOLUMEINFO_FILNAM "." XXDP_VOLUMEINFO_EXT)) {
		// evaluate parameter file ?
	} else if (!strcasecmp(hostfname, XXDP_BOOTBLOCK_FILNAM "." XXDP_BOOTBLOCK_EXT)) {
		if (data_size != XXDP_BLOCKSIZE)
			return error_set(ERROR_FILESYSTEM_FORMAT, "Boot block not %d bytes\n",
			XXDP_BLOCKSIZE);
		_this->bootblock->data_size = data_size;
		_this->bootblock->data = realloc(_this->bootblock->data, data_size);
		memcpy(_this->bootblock->data, data, data_size);
	} else if (!strcasecmp(hostfname, XXDP_MONITOR_FILNAM "." XXDP_MONITOR_EXT)) {
		// assume max len of monitor
		int n = XXDP_BLOCKSIZE * (_this->preallocated_blockcount - _this->monitor->blocknr + 1);
		if (n < 0 || data_size > (unsigned)n)
			return error_set(ERROR_FILESYSTEM_OVERFLOW,
					"Monitor too big: is %d bytes, only %d allowed", data_size, n);
		_this->monitor->data_size = data_size;
		_this->monitor->data = realloc(_this->monitor->data, data_size);
		memcpy(_this->monitor->data, data, data_size);
	} else {
		xxdp_file_t *f;
		char filnam[40], ext[40];
		int file_idx;
		// regular file
		if (_this->file_count + 1 >= XXDP_MAX_FILES_PER_IMAGE)
			return error_set(ERROR_FILESYSTEM_OVERFLOW, "Too many files, only %d allowed",
			XXDP_MAX_FILES_PER_IMAGE);
		if (xxdp_filesystem_layout_test(_this, data_size))
			return error_set(ERROR_FILESYSTEM_OVERFLOW,
					"Disk full, file \"%s\" with %d bytes too large", hostfname, data_size);

		// make filename.extension to "FILN  .E  "
		xxdp_filename_from_host(hostfname, filnam, ext);

		// duplicate file name? Likely! because of trunc to six letters
		for (file_idx = 0; file_idx < _this->file_count; file_idx++) {
			xxdp_file_t *f1 = _this->file[file_idx];
			if (!strcasecmp(filnam, f1->filnam) && !strcasecmp(ext, f1->ext))
				return error_set(ERROR_FILESYSTEM_DUPLICATE, "Duplicate filename %s.%s", filnam,
						ext);
		}
		// now insert
		f = malloc(sizeof(xxdp_file_t));
		_this->file[_this->file_count++] = f;
		f->data_size = data_size;
		f->data = malloc(data_size);
		memcpy(f->data, data, data_size);
		strcpy(f->filnam, filnam);
		strcpy(f->ext, ext);

		f->date = *localtime(&hostfdate);
		// only range 1970..1999 allowed
		if (f->date.tm_year < 70)
			f->date.tm_year = 70;
		else if (f->date.tm_year > 99)
			f->date.tm_year = 99;
	}
	return ERROR_OK;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movl %r8d, %r15d
movq %rcx, %r13
movq %rsi, %r12
movq %rdi, %r14
movq %rdx, 0x18(%rsp)
leaq 0x7c7c(%rip), %rsi      # 0x14500
movq %r12, %rdi
callq 0x3060
xorl %ebp, %ebp
testl %eax, %eax
je 0xcbe4
leaq 0x833c(%rip), %rsi      # 0x14bd9
movq %r12, %rdi
callq 0x3060
testl %eax, %eax
je 0xc8ea
leaq 0x834c(%rip), %rsi      # 0x14bfc
movq %r12, %rdi
callq 0x3060
testl %eax, %eax
je 0xc92e
movl 0xa8(%r14), %ecx
cmpl $0x3e7, %ecx            # imm = 0x3E7
jl 0xc982
leaq 0x835e(%rip), %rsi      # 0x14c34
pushq $-0x2
popq %rdi
movl $0x3e8, %edx            # imm = 0x3E8
xorl %eax, %eax
callq 0x982c
jmp 0xcbe2
cmpl $0x200, %r15d           # imm = 0x200
jne 0xca2c
movq 0x80(%r14), %rax
movl $0x200, 0x10(%rax)      # imm = 0x200
movq 0x8(%rax), %rdi
movl $0x200, %esi            # imm = 0x200
callq 0x3420
movq 0x80(%r14), %rcx
movq %rax, 0x8(%rcx)
movl $0x200, %edx            # imm = 0x200
movq %rax, %rdi
movq %r13, %rsi
jmp 0xcae9
movzwl 0x50(%r14), %edx
movq 0x88(%r14), %rax
movzwl (%rax), %ecx
subl %ecx, %edx
movl %edx, %ecx
shll $0x9, %ecx
addl $0x200, %ecx            # imm = 0x200
cmpl $-0x1, %edx
setl %dl
cmpl %r15d, %ecx
setb %sil
orb %dl, %sil
je 0xcac2
leaq 0x829f(%rip), %rsi      # 0x14c06
pushq $-0x2
popq %rdi
movl %r15d, %edx
xorl %eax, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x982c
xorl %edi, %edi
testl %ecx, %ecx
movl $0x0, %r8d
cmovgl %ecx, %r8d
movl $0x1fd, %r9d            # imm = 0x1FD
movl $0x1fe, %r10d           # imm = 0x1FE
xorl %esi, %esi
cmpq %rdi, %r8
je 0xc9c0
movq 0xb0(%r14,%rdi,8), %rax
movl 0x884(%rax), %eax
addl %r9d, %eax
xorl %edx, %edx
divl %r10d
addl %eax, %esi
incq %rdi
jmp 0xc99e
addl $0x1c, %ecx
pushq $0x1c
popq %rdi
movl %ecx, %eax
cltd
idivl %edi
movl %eax, %ecx
leal 0x1fd(%r15), %eax
movl $0x1fe, %edi            # imm = 0x1FE
cltd
idivl %edi
addl %eax, %esi
movl 0x4c(%r14), %eax
movzwl 0x50(%r14), %edx
subl %edx, %eax
movzwl 0x2e(%r14), %edx
xorl %ebx, %ebx
subl %edx, %ecx
cmovlel %ebx, %ecx
addl %esi, %ecx
cmpl %eax, %ecx
jle 0xca50
leaq 0x8a19(%rip), %rsi      # 0x1541b
pushq $-0x2
popq %rdi
xorl %eax, %eax
callq 0x982c
testl %eax, %eax
je 0xca50
leaq 0x823d(%rip), %rsi      # 0x14c54
pushq $-0x2
popq %rdi
movq %r12, %rdx
movl %r15d, %ecx
xorl %eax, %eax
callq 0x982c
jmp 0xcbe2
leaq 0x81b0(%rip), %rsi      # 0x14be3
pushq $-0x1
popq %rdi
movl $0x200, %edx            # imm = 0x200
xorl %eax, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x982c
movq %r13, 0x10(%rsp)
leaq 0x50(%rsp), %r13
leaq 0x20(%rsp), %rdx
movq %r12, %rdi
movq %r13, %rsi
callq 0xcbf5
movslq 0xa8(%r14), %rax
testl %eax, %eax
movl $0x0, %ebp
movq %rax, 0x8(%rsp)
cmovgl %eax, %ebp
cmpq %rbx, %rbp
je 0xcaf3
movq 0xb0(%r14,%rbx,8), %r12
leaq 0x808(%r12), %rsi
movq %r13, %rdi
callq 0x3060
testl %eax, %eax
jne 0xcabd
addq $0x858, %r12            # imm = 0x858
leaq 0x20(%rsp), %rdi
movq %r12, %rsi
callq 0x3060
testl %eax, %eax
je 0xcbc7
incq %rbx
jmp 0xca80
movl %r15d, 0x10(%rax)
movq 0x8(%rax), %rdi
movl %r15d, %r15d
movq %r15, %rsi
callq 0x3420
movq 0x88(%r14), %rcx
movq %rax, 0x8(%rcx)
movq %rax, %rdi
movq %r13, %rsi
movq %r15, %rdx
callq 0x3340
jmp 0xcbe4
movl $0x8d0, %edi            # imm = 0x8D0
callq 0x33b0
movq %rax, %r12
movq 0x8(%rsp), %rcx
leal 0x1(%rcx), %eax
movl %eax, 0xa8(%r14)
movq %r12, 0xb0(%r14,%rcx,8)
movl %r15d, 0x884(%r12)
movl %r15d, %r14d
movq %r14, %rdi
callq 0x33b0
movq %rax, 0x888(%r12)
movq %rax, %rdi
movq 0x10(%rsp), %rsi
movq %r14, %rdx
callq 0x3340
leaq 0x808(%r12), %rdi
leaq 0x50(%rsp), %rsi
callq 0x30c0
leaq 0x858(%r12), %rdi
leaq 0x20(%rsp), %rsi
callq 0x30c0
leaq 0x18(%rsp), %rdi
callq 0x3070
movups (%rax), %xmm0
movups 0x10(%rax), %xmm1
movups 0x20(%rax), %xmm2
movups %xmm1, 0x8a0(%r12)
movups %xmm0, 0x890(%r12)
movups %xmm2, 0x8b0(%r12)
movq 0x30(%rax), %rax
movq %rax, 0x8c0(%r12)
movl 0x8a4(%r12), %eax
cmpl $0x46, %eax
jge 0xcbb3
pushq $0x46
jmp 0xcbba
cmpl $0x64, %eax
jb 0xcbc3
pushq $0x63
popq %rax
movl %eax, 0x8a4(%r12)
xorl %ebp, %ebp
jmp 0xcbe4
leaq 0x80b3(%rip), %rsi      # 0x14c81
pushq $-0x3
popq %rdi
leaq 0x50(%rsp), %rdx
leaq 0x20(%rsp), %rcx
xorl %eax, %eax
callq 0x982c
movl %eax, %ebp
movl %ebp, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq