// File: /workspace/github_c_makefile_star2_O1/j-hoppe[P]tu58fs/rt11.c
// Function: rt11_filesystem_file_stream_add
int rt11_filesystem_file_stream_add(rt11_filesystem_t *_this, char *hostfname, char *streamcode,
		time_t hostfdate, mode_t hostmode, uint8_t *data, uint32_t data_size) {
	// fprintf(stderr, "rt11_filesystem_file_stream_add(%s)\n", hostfname);
	if (!strcasecmp(hostfname, RT11_VOLUMEINFO_FILNAM "." RT11_VOLUMEINFO_EXT)) {
		// evaluate parameter file ?
	} else if (!strcasecmp(hostfname, RT11_BOOTBLOCK_FILNAM "." RT11_BOOTBLOCK_EXT)) {
		if (!streamcode) { // some tests produced "$BOOT.BLK.dirext" !
			if (data_size != RT11_BLOCKSIZE)
				return error_set(ERROR_FILESYSTEM_FORMAT, "Boot block not %d bytes",
				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, RT11_MONITOR_FILNAM "." RT11_MONITOR_EXT)) {
		if (!streamcode) {
			if (data_size > 4 * RT11_BLOCKSIZE)
				return error_set(ERROR_FILESYSTEM_FORMAT,
						"Monitor block too big, has %d bytes, max %d", data_size,
						4 * RT11_BLOCKSIZE);
			_this->monitor->data_size = data_size;
			_this->monitor->data = realloc(_this->monitor->data, data_size);
			memcpy(_this->monitor->data, data, data_size);
		}
	} else {
		// one of 3 streams of a regular file
		//1. find file
		rt11_file_t *f;
		char filnam[40], ext[40];
		int i;
		rt11_stream_t **streamptr;
		// regular file
		if (_this->file_count + 1 >= RT11_MAX_FILES_PER_IMAGE)
			return error_set(ERROR_FILESYSTEM_OVERFLOW, "Too many files, only %d allowed",
			RT11_MAX_FILES_PER_IMAGE);

		// check wether a new file of "data_size" bytes would fit onto volume
		if (rt11_filesystem_calc_block_use(_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"(not: "FILN  .E  ")
		rt11_filename_from_host(hostfname, filnam, ext);

		// find file with this name. Duplicate check later
		// duplicate file name? Likely! because of trunc to six letters
		f = NULL;
		for (i = 0; i < _this->file_count; i++) {
			rt11_file_t *f1 = _this->file[i];
			if (!strcasecmp(filnam, f1->filnam) && !strcasecmp(ext, f1->ext))
				f = f1; // file exists, earlier another stream for f was written
		}
		if (!f) {
			// new file
			f = rt11_file_create();
			_this->file[_this->file_count++] = f;
			strcpy(f->filnam, filnam);
			strcpy(f->ext, ext);

			f->date = *localtime(&hostfdate);
			// only range 1972..1999 allowed
			if (f->date.tm_year < 72)
				f->date.tm_year = 72;
			else if (f->date.tm_year > 99)
				f->date.tm_year = 99;
			f->readonly = 0; // set from data stream
		}
		streamptr = NULL;
		//2. write correct stream
		if (!streamcode || strlen(streamcode) == 0) {
			streamptr = &f->data;
			// file is readonly, if data stream has no user write permission (see stat(2))
			f->readonly = !(hostmode & S_IWUSR); // set from data stream
		} else if (!strcasecmp(streamcode, RT11_STREAMNAME_DIREXT)) {
			streamptr = &f->dir_ext;
			// size of dir entry extra bytes is largest dir_ext stream
			if (data_size > _this->dir_entry_extra_bytes)
				_this->dir_entry_extra_bytes = data_size;
		} else if (!strcasecmp(streamcode, RT11_STREAMNAME_PREFIX)) {
			streamptr = &f->prefix;
		} else
			return error_set(ERROR_FILESYSTEM_FORMAT, "Illegal stream code %s", streamcode);

		// stream may not be filled before! else duplicate filename
		if (*streamptr != NULL)
			return error_set(ERROR_FILESYSTEM_DUPLICATE, "Duplicate filename/stream %s.%s %s",
					filnam, ext, streamcode);

		*streamptr = stream_create();
		if (streamcode) // else remains ""
			strcpy((*streamptr)->name, streamcode);
		(*streamptr)->data_size = data_size;
		(*streamptr)->data = malloc(data_size);
		memcpy((*streamptr)->data, data, data_size);

		// calc blocks count = prefix +data
		f->block_count = 0;
		if (f->prefix)
			f->block_count += NEEDED_BLOCKS(RT11_BLOCKSIZE, f->prefix->data_size + 2); // 2 bytes length word
		if (f->data)
			f->block_count += NEEDED_BLOCKS(RT11_BLOCKSIZE, f->data->data_size);
	} // if regular file
	return ERROR_OK;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movq %r9, %r13
movl %r8d, %r14d
movq %rdx, %rbx
movq %rsi, %r12
movq %rdi, %r15
movl 0xb0(%rsp), %ebp
movq %rcx, 0x18(%rsp)
leaq 0x5c98(%rip), %rsi      # 0x154bb
movq %r12, %rdi
callq 0x3060
testl %eax, %eax
je 0xfc63
leaq 0x6354(%rip), %rsi      # 0x15b8e
movq %r12, %rdi
callq 0x3060
testl %eax, %eax
je 0xf889
leaq 0x6364(%rip), %rsi      # 0x15bb1
movq %r12, %rdi
callq 0x3060
testl %eax, %eax
je 0xf8cf
cmpl $0x3e7, 0x8c(%r15) # imm = 0x3E7
jl 0xf90c
leaq 0x6378(%rip), %rsi      # 0x15be9
xorl %ebx, %ebx
movl $0xfffffffe, %edi       # imm = 0xFFFFFFFE
movl $0x3e8, %edx            # imm = 0x3E8
xorl %eax, %eax
callq 0x9f8f
jmp 0xfc5f
testq %rbx, %rbx
jne 0xfc63
cmpl $0x200, %ebp            # imm = 0x200
jne 0xf9b0
movq 0x78(%r15), %rax
movl $0x200, 0x10(%rax)      # imm = 0x200
movq 0x8(%rax), %rdi
movl $0x200, %esi            # imm = 0x200
callq 0x3430
movq 0x78(%r15), %rcx
movq %rax, 0x8(%rcx)
movl $0x200, %edx            # imm = 0x200
movq %rax, %rdi
movq %r13, %rsi
jmp 0xfa02
testq %rbx, %rbx
jne 0xfc63
cmpl $0x801, %ebp            # imm = 0x801
jb 0xf9d6
leaq 0x6f12(%rip), %rsi      # 0x167fd
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
movl %ebp, %edx
movl $0x800, %ecx            # imm = 0x800
xorl %eax, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x9f8f
movq %r15, %rdi
movl %ebp, %esi
callq 0xfca3
testl %eax, %eax
je 0xf939
leaq 0x62e8(%rip), %rsi      # 0x15c09
xorl %ebx, %ebx
movl $0xfffffffe, %edi       # imm = 0xFFFFFFFE
movq %r12, %rdx
movl %ebp, %ecx
xorl %eax, %eax
callq 0x9f8f
jmp 0xfc5f
movl %r14d, 0x4(%rsp)
movq %rbx, 0x10(%rsp)
movl %ebp, (%rsp)
movq %r13, 0x8(%rsp)
leaq 0x50(%rsp), %rsi
leaq 0x20(%rsp), %rdx
movq %r12, %rdi
callq 0xfe7a
movslq 0x8c(%r15), %rbp
testq %rbp, %rbp
jle 0xfa0c
xorl %r13d, %r13d
leaq 0x50(%rsp), %rbx
xorl %r14d, %r14d
movq 0x90(%r15,%r13,8), %r12
leaq 0x2(%r12), %rsi
movq %rbx, %rdi
callq 0x3060
testl %eax, %eax
jne 0xf9a6
leaq 0x52(%r12), %rsi
leaq 0x20(%rsp), %rdi
callq 0x3060
testl %eax, %eax
cmoveq %r12, %r14
incq %r13
cmpq %r13, %rbp
jne 0xf978
jmp 0xfa0f
leaq 0x6e2e(%rip), %rsi      # 0x167e5
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
movl $0x200, %edx            # imm = 0x200
xorl %eax, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x9f8f
movq 0x80(%r15), %rax
movl %ebp, 0x10(%rax)
movq 0x8(%rax), %rdi
movl %ebp, %ebx
movq %rbx, %rsi
callq 0x3430
movq 0x80(%r15), %rcx
movq %rax, 0x8(%rcx)
movq %rax, %rdi
movq %r13, %rsi
movq %rbx, %rdx
callq 0x3350
jmp 0xfc63
xorl %r14d, %r14d
movq %r14, %r12
testq %r14, %r14
movq 0x10(%rsp), %r14
jne 0xfb1b
movl $0xd8, %edi
callq 0x33c0
movq %rax, %r12
leaq 0x2(%rax), %rdi
xorl %eax, %eax
movb %al, 0x2(%r12)
leaq 0x52(%r12), %rbx
movb %al, 0x52(%r12)
movw $0x0, 0x7c(%r12)
xorps %xmm0, %xmm0
movups %xmm0, 0x80(%r12)
movups %xmm0, 0x90(%r12)
movups %xmm0, 0xa0(%r12)
movups %xmm0, 0xb0(%r12)
movups %xmm0, 0xc0(%r12)
movq $0x0, 0xd0(%r12)
leal 0x1(%rbp), %eax
movl %eax, 0x8c(%r15)
movq %r12, 0x90(%r15,%rbp,8)
leaq 0x50(%rsp), %rsi
callq 0x30d0
leaq 0x20(%rsp), %rsi
movq %rbx, %rdi
callq 0x30d0
leaq 0x18(%rsp), %rdi
callq 0x3070
movups (%rax), %xmm0
movups 0x10(%rax), %xmm1
movups 0x20(%rax), %xmm2
movups %xmm1, 0xa8(%r12)
movups %xmm0, 0x98(%r12)
movups %xmm2, 0xb8(%r12)
movq 0x30(%rax), %rax
movq %rax, 0xc8(%r12)
movl 0xac(%r12), %ecx
movl $0x48, %eax
cmpl $0x48, %ecx
jl 0xfb07
movl $0x63, %eax
cmpl $0x64, %ecx
jl 0xfb0f
movl %eax, 0xac(%r12)
movl $0x0, 0xd0(%r12)
testq %r14, %r14
je 0xfb71
cmpb $0x0, (%r14)
je 0xfb71
leaq 0x5f06(%rip), %rsi      # 0x15a33
movq %r14, %rdi
callq 0x3060
testl %eax, %eax
je 0xfc74
leaq 0x5ef6(%rip), %rsi      # 0x15a3a
movq %r14, %rdi
callq 0x3060
testl %eax, %eax
je 0xfc96
leaq 0x6cce(%rip), %rsi      # 0x16829
xorl %ebx, %ebx
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
movq %r14, %rdx
xorl %eax, %eax
callq 0x9f8f
jmp 0xfc5f
leaq 0x80(%r12), %rbp
xorl %eax, %eax
cmpb $0x0, 0x4(%rsp)
setns %al
movl %eax, 0xd0(%r12)
cmpq $0x0, (%rbp)
je 0xfbb9
leaq 0x6ca7(%rip), %rsi      # 0x16840
xorl %ebx, %ebx
leaq 0x50(%rsp), %rdx
leaq 0x20(%rsp), %rcx
movl $0xfffffffd, %edi       # imm = 0xFFFFFFFD
movq %r14, %r8
xorl %eax, %eax
callq 0x9f8f
jmp 0xfc5f
movl $0x68, %edi
callq 0x33c0
movw $0x0, (%rax)
xorps %xmm0, %xmm0
movups %xmm0, 0x4(%rax)
movb $0x0, 0x14(%rax)
movq %rax, (%rbp)
testq %r14, %r14
je 0xfbeb
addq $0x14, %rax
movq %rax, %rdi
movq %r14, %rsi
callq 0x30d0
movq (%rbp), %r14
movl (%rsp), %eax
movl %eax, 0x10(%r14)
movl %eax, %ebx
movq %rbx, %rdi
callq 0x33c0
movq %rax, 0x8(%r14)
movq (%rbp), %rax
movq 0x8(%rax), %rdi
movq 0x8(%rsp), %rsi
movq %rbx, %rdx
callq 0x3350
movw $0x0, 0x7c(%r12)
movq 0x88(%r12), %rax
testq %rax, %rax
je 0xfc3f
movl $0x201, %ecx            # imm = 0x201
addl 0x10(%rax), %ecx
shrl $0x9, %ecx
movw %cx, 0x7c(%r12)
movq 0x80(%r12), %rax
movb $0x1, %bl
testq %rax, %rax
je 0xfc5f
movl $0x1ff, %ecx            # imm = 0x1FF
addl 0x10(%rax), %ecx
shrl $0x9, %ecx
addw %cx, 0x7c(%r12)
testb %bl, %bl
je 0xfc65
xorl %eax, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x90(%r12), %rbp
movzwl 0x68(%r15), %eax
movl (%rsp), %ecx
cmpl %ecx, %eax
jae 0xfb8b
movw %cx, 0x68(%r15)
jmp 0xfb8b
leaq 0x88(%r12), %rbp
jmp 0xfb8b