// File: /workspace/github_c_makefile_star2_O2/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, %r14
movl %r8d, 0x4(%rsp)
movq %rdx, %r13
movq %rsi, %rbx
movq %rdi, %r12
movl 0xb0(%rsp), %ebp
movq %rcx, 0x18(%rsp)
leaq 0x5cd7(%rip), %rsi      # 0x14500
movq %rbx, %rdi
callq 0x3060
xorl %r15d, %r15d
testl %eax, %eax
je 0xeb3e
leaq 0x6396(%rip), %rsi      # 0x14bd9
movq %rbx, %rdi
callq 0x3060
testl %eax, %eax
je 0xe88f
leaq 0x63a6(%rip), %rsi      # 0x14bfc
movq %rbx, %rdi
callq 0x3060
testl %eax, %eax
je 0xe8d7
cmpl $0x3e7, 0x8c(%r12) # imm = 0x3E7
jl 0xe912
leaq 0x63b9(%rip), %rsi      # 0x14c34
pushq $-0x2
popq %rdi
movl $0x3e8, %edx            # imm = 0x3E8
xorl %eax, %eax
callq 0x982c
jmp 0xeb3b
testq %r13, %r13
jne 0xeb3e
cmpl $0x200, %ebp            # imm = 0x200
jne 0xea3c
movq 0x78(%r12), %rax
movl $0x200, 0x10(%rax)      # imm = 0x200
movq 0x8(%rax), %rdi
movl $0x200, %esi            # imm = 0x200
callq 0x3420
movq 0x78(%r12), %rcx
movq %rax, 0x8(%rcx)
movl $0x200, %edx            # imm = 0x200
movq %rax, %rdi
movq %r14, %rsi
jmp 0xea8e
testq %r13, %r13
jne 0xeb3e
cmpl $0x801, %ebp            # imm = 0x801
jb 0xea60
leaq 0x6f56(%rip), %rsi      # 0x15849
pushq $-0x1
popq %rdi
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 0x982c
movq %r12, %rdi
movl %ebp, %esi
callq 0xec19
testl %eax, %eax
je 0xe93b
leaq 0x632d(%rip), %rsi      # 0x14c54
pushq $-0x2
popq %rdi
movq %rbx, %rdx
movl %ebp, %ecx
xorl %eax, %eax
callq 0x982c
jmp 0xeb3b
movq %r13, 0x10(%rsp)
movl %ebp, (%rsp)
movq %r14, 0x8(%rsp)
leaq 0x50(%rsp), %rbp
leaq 0x20(%rsp), %rdx
movq %rbx, %rdi
movq %rbp, %rsi
callq 0xedc7
movl 0x8c(%r12), %ebx
xorl %r13d, %r13d
testl %ebx, %ebx
cmovlel %r13d, %ebx
xorl %r15d, %r15d
cmpq %r13, %rbx
je 0xe9a7
movq 0x90(%r12,%r13,8), %r14
leaq 0x2(%r14), %rsi
movq %rbp, %rdi
callq 0x3060
testl %eax, %eax
jne 0xe9a2
leaq 0x52(%r14), %rsi
leaq 0x20(%rsp), %rdi
callq 0x3060
testl %eax, %eax
cmoveq %r14, %r15
incq %r13
jmp 0xe971
testq %r15, %r15
movq 0x10(%rsp), %r14
jne 0xeaaf
callq 0xd8f4
movq %rax, %r15
movslq 0x8c(%r12), %rax
leal 0x1(%rax), %ecx
movl %ecx, 0x8c(%r12)
movq %r15, 0x90(%r12,%rax,8)
leaq 0x2(%r15), %rdi
leaq 0x50(%rsp), %rsi
callq 0x30c0
leaq 0x52(%r15), %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, 0xa8(%r15)
movups %xmm0, 0x98(%r15)
movups %xmm2, 0xb8(%r15)
movq 0x30(%rax), %rax
movq %rax, 0xc8(%r15)
movl 0xac(%r15), %eax
cmpl $0x48, %eax
jge 0xea98
pushq $0x48
jmp 0xea9f
leaq 0x6dee(%rip), %rsi      # 0x15831
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 0x80(%r12), %rax
movl %ebp, 0x10(%rax)
movq 0x8(%rax), %rdi
movl %ebp, %ebx
movq %rbx, %rsi
callq 0x3420
movq 0x80(%r12), %rcx
movq %rax, 0x8(%rcx)
movq %rax, %rdi
movq %r14, %rsi
movq %rbx, %rdx
callq 0x3340
jmp 0xeb3e
cmpl $0x64, %eax
jb 0xeaa7
pushq $0x63
popq %rax
movl %eax, 0xac(%r15)
andl $0x0, 0xd0(%r15)
testq %r14, %r14
je 0xeafe
cmpb $0x0, (%r14)
je 0xeafe
leaq 0x5fbd(%rip), %rsi      # 0x14a7e
movq %r14, %rdi
callq 0x3060
testl %eax, %eax
je 0xebc2
leaq 0x5fad(%rip), %rsi      # 0x14a85
movq %r14, %rdi
callq 0x3060
testl %eax, %eax
je 0xec0d
leaq 0x6d86(%rip), %rsi      # 0x15875
pushq $-0x1
popq %rdi
movq %r14, %rdx
xorl %eax, %eax
callq 0x982c
jmp 0xeb3b
leaq 0x80(%r15), %rbp
xorl %eax, %eax
cmpb $0x0, 0x4(%rsp)
setns %al
movl %eax, 0xd0(%r15)
cmpq $0x0, (%rbp)
je 0xeb50
leaq 0x6d68(%rip), %rsi      # 0x1588c
pushq $-0x3
popq %rdi
leaq 0x50(%rsp), %rdx
leaq 0x20(%rsp), %rcx
movq %r14, %r8
xorl %eax, %eax
callq 0x982c
movl %eax, %r15d
movl %r15d, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0xfa43
movq %rax, %rbx
movq %rax, (%rbp)
testq %r14, %r14
je 0xeb74
addq $0x14, %rbx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x30c0
movq (%rbp), %rbx
movl (%rsp), %eax
movl %eax, 0x10(%rbx)
movl %eax, %r14d
movq %r14, %rdi
callq 0x33b0
movq %rax, 0x8(%rbx)
movq (%rbp), %rax
movq 0x8(%rax), %rdi
movq 0x8(%rsp), %rsi
movq %r14, %rdx
callq 0x3340
andw $0x0, 0x7c(%r15)
movq 0x88(%r15), %rcx
testq %rcx, %rcx
je 0xebe5
movl $0x201, %eax            # imm = 0x201
addl 0x10(%rcx), %eax
shrl $0x9, %eax
movw %ax, 0x7c(%r15)
jmp 0xebe7
leaq 0x90(%r15), %rbp
movzwl 0x68(%r12), %eax
movl (%rsp), %ecx
cmpl %ecx, %eax
jae 0xeb16
movw %cx, 0x68(%r12)
jmp 0xeb16
xorl %eax, %eax
movq 0x80(%r15), %rcx
testq %rcx, %rcx
je 0xec05
movl $0x1ff, %edx            # imm = 0x1FF
addl 0x10(%rcx), %edx
shrl $0x9, %edx
addl %edx, %eax
movw %ax, 0x7c(%r15)
xorl %r15d, %r15d
jmp 0xeb3e
leaq 0x88(%r15), %rbp
jmp 0xeb16