// File: /workspace/github_c_makefile_star2_O3/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 $0x68, %rsp
movl %r8d, %r15d
movq %rcx, %rbx
movq %rsi, %r12
movq %rdi, %r14
movq %rdx, 0x8(%rsp)
leaq 0x8168(%rip), %rsi      # 0x15528
movq %r12, %rdi
callq 0x3060
xorl %ebp, %ebp
testl %eax, %eax
je 0xd74b
leaq 0x884d(%rip), %rsi      # 0x15c26
movq %r12, %rdi
callq 0x3060
testl %eax, %eax
je 0xd429
leaq 0x885d(%rip), %rsi      # 0x15c49
movq %r12, %rdi
callq 0x3060
testl %eax, %eax
je 0xd46d
movslq 0xa8(%r14), %rdx
cmpq $0x3e7, %rdx            # imm = 0x3E7
jl 0xd4c3
leaq 0x886e(%rip), %rsi      # 0x15c81
movl $0xfffffffe, %edi       # imm = 0xFFFFFFFE
movl $0x3e8, %edx            # imm = 0x3E8
xorl %eax, %eax
callq 0x9f73
jmp 0xd749
cmpl $0x200, %r15d           # imm = 0x200
jne 0xd59e
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 %rbx, %rsi
jmp 0xd725
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 0xd6fe
leaq 0x87ad(%rip), %rsi      # 0x15c53
movl $0xfffffffe, %edi       # imm = 0xFFFFFFFE
movl %r15d, %edx
xorl %eax, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x9f73
leal 0x1c(%rdx), %eax
cltq
imulq $-0x6db6db6d, %rax, %rax # imm = 0x92492493
shrq $0x20, %rax
addl %edx, %eax
addl $0x1c, %eax
movl %eax, %ecx
shrl $0x1f, %ecx
sarl $0x4, %eax
addl %ecx, %eax
xorl %ecx, %ecx
movl $0x0, %esi
testl %edx, %edx
jle 0xd522
xorl %edi, %edi
movl $0x1fd, %r8d            # imm = 0x1FD
movl $0x80808081, %r9d       # imm = 0x80808081
xorl %esi, %esi
movq 0xb0(%r14,%rdi,8), %r10
movl 0x884(%r10), %r10d
addl %r8d, %r10d
imulq %r9, %r10
shrq $0x28, %r10
addl %r10d, %esi
incq %rdi
cmpq %rdi, %rdx
jne 0xd4fd
leal 0x1fd(%r15), %edx
movslq %edx, %rdx
imulq $-0x7f7f7f7f, %rdx, %rdx # imm = 0x80808081
shrq $0x20, %rdx
addl %r15d, %edx
addl $0x1fd, %edx            # imm = 0x1FD
movl %edx, %edi
shrl $0x1f, %edi
sarl $0x8, %edx
addl %edi, %edx
movl 0x4c(%r14), %edi
movzwl 0x50(%r14), %r8d
subl %r8d, %edi
movzwl 0x2e(%r14), %r8d
subl %r8d, %eax
cmovgl %eax, %ecx
addl %edx, %ecx
addl %esi, %ecx
cmpl %edi, %ecx
jle 0xd5c4
leaq 0x8ef8(%rip), %rsi      # 0x16468
movl $0xfffffffe, %edi       # imm = 0xFFFFFFFE
xorl %eax, %eax
callq 0x9f73
testl %eax, %eax
je 0xd5c4
leaq 0x871a(%rip), %rsi      # 0x15ca1
movl $0xfffffffe, %edi       # imm = 0xFFFFFFFE
movq %r12, %rdx
movl %r15d, %ecx
xorl %eax, %eax
callq 0x9f73
jmp 0xd749
leaq 0x868b(%rip), %rsi      # 0x15c30
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
movl $0x200, %edx            # imm = 0x200
xorl %eax, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x9f73
movq %rbx, (%rsp)
leaq 0x40(%rsp), %rsi
leaq 0x10(%rsp), %rdx
movq %r12, %rdi
callq 0xd75c
movslq 0xa8(%r14), %rbx
testq %rbx, %rbx
jle 0xd62d
xorl %r13d, %r13d
leaq 0x40(%rsp), %r12
movq 0xb0(%r14,%r13,8), %rbp
leaq 0x808(%rbp), %rsi
movq %r12, %rdi
callq 0x3060
testl %eax, %eax
jne 0xd625
addq $0x858, %rbp            # imm = 0x858
leaq 0x10(%rsp), %rdi
movq %rbp, %rsi
callq 0x3060
testl %eax, %eax
je 0xd72c
incq %r13
cmpq %r13, %rbx
jne 0xd5ee
movl $0x8d0, %edi            # imm = 0x8D0
callq 0x33b0
movq %rax, %r12
leal 0x1(%rbx), %eax
movl %eax, 0xa8(%r14)
movq %r12, 0xb0(%r14,%rbx,8)
movl %r15d, 0x884(%r12)
movl %r15d, %r14d
movq %r14, %rdi
callq 0x33b0
movq %rax, 0x888(%r12)
movq %rax, %rdi
movq (%rsp), %rsi
movq %r14, %rdx
callq 0x3340
leaq 0x808(%r12), %rdi
leaq 0x40(%rsp), %rsi
callq 0x30c0
leaq 0x858(%r12), %rdi
leaq 0x10(%rsp), %rsi
callq 0x30c0
leaq 0x8(%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), %ecx
movl $0x46, %eax
cmpl $0x46, %ecx
jl 0xd6f2
movl $0x63, %eax
cmpl $0x64, %ecx
jb 0xd6fa
movl %eax, 0x8a4(%r12)
xorl %ebp, %ebp
jmp 0xd74b
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 %rbx, %rsi
movq %r15, %rdx
callq 0x3340
jmp 0xd74b
leaq 0x859b(%rip), %rsi      # 0x15cce
leaq 0x40(%rsp), %rdx
leaq 0x10(%rsp), %rcx
movl $0xfffffffd, %edi       # imm = 0xFFFFFFFD
xorl %eax, %eax
callq 0x9f73
movl %eax, %ebp
movl %ebp, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq