// File: /workspace/github_c_makefile_star2_O3/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, 0xc(%rsp)
movq %rdx, %rbp
movq %rsi, %rbx
movq %rdi, %r12
movl 0xb0(%rsp), %r14d
movq %rcx, 0x18(%rsp)
leaq 0x5d6d(%rip), %rsi      # 0x15528
movq %rbx, %rdi
callq 0x3060
xorl %r15d, %r15d
testl %eax, %eax
je 0xfb36
leaq 0x6451(%rip), %rsi      # 0x15c26
movq %rbx, %rdi
callq 0x3060
testl %eax, %eax
je 0xf823
leaq 0x6461(%rip), %rsi      # 0x15c49
movq %rbx, %rdi
callq 0x3060
testl %eax, %eax
je 0xf86c
cmpl $0x3e7, 0x8c(%r12) # imm = 0x3E7
jl 0xf8ab
leaq 0x6474(%rip), %rsi      # 0x15c81
movl $0xfffffffe, %edi       # imm = 0xFFFFFFFE
movl $0x3e8, %edx            # imm = 0x3E8
xorl %eax, %eax
callq 0x9f73
jmp 0xfafc
testq %rbp, %rbp
jne 0xfb36
cmpl $0x200, %r14d           # imm = 0x200
jne 0xfa96
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 %r13, %rsi
jmp 0xfb31
testq %rbp, %rbp
jne 0xfb36
cmpl $0x801, %r14d           # imm = 0x801
jb 0xfb01
leaq 0x700c(%rip), %rsi      # 0x16895
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
movl %r14d, %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 0x9f73
movq %r12, %rdi
movl %r14d, %esi
callq 0xfc22
testl %eax, %eax
je 0xf8d8
leaq 0x63e0(%rip), %rsi      # 0x15ca1
movl $0xfffffffe, %edi       # imm = 0xFFFFFFFE
movq %rbx, %rdx
movl %r14d, %ecx
xorl %eax, %eax
callq 0x9f73
jmp 0xfafc
movq %rbp, (%rsp)
leaq 0x50(%rsp), %rsi
leaq 0x20(%rsp), %rdx
movq %rbx, %rdi
callq 0xfde6
movslq 0x8c(%r12), %rbp
testq %rbp, %rbp
jle 0xf957
movl %r14d, 0x8(%rsp)
movq %r13, 0x10(%rsp)
xorl %r13d, %r13d
leaq 0x50(%rsp), %rbx
xorl %r15d, %r15d
movq 0x90(%r12,%r13,8), %r14
leaq 0x2(%r14), %rsi
movq %rbx, %rdi
callq 0x3060
testl %eax, %eax
jne 0xf93c
leaq 0x52(%r14), %rsi
leaq 0x20(%rsp), %rdi
callq 0x3060
testl %eax, %eax
cmoveq %r14, %r15
incq %r13
cmpq %r13, %rbp
jne 0xf910
testq %r15, %r15
movq 0x10(%rsp), %r13
movl 0x8(%rsp), %r14d
jne 0xfa42
movl $0xd8, %edi
callq 0x33b0
movq %rax, %r15
leaq 0x2(%rax), %rdi
xorl %eax, %eax
movb %al, 0x2(%r15)
leaq 0x52(%r15), %rbx
movb %al, 0x52(%r15)
movw $0x0, 0x7c(%r15)
xorps %xmm0, %xmm0
movups %xmm0, 0x80(%r15)
movups %xmm0, 0x90(%r15)
movups %xmm0, 0xa0(%r15)
movups %xmm0, 0xb0(%r15)
movups %xmm0, 0xc0(%r15)
movq $0x0, 0xd0(%r15)
leal 0x1(%rbp), %eax
movl %eax, 0x8c(%r12)
movq %r15, 0x90(%r12,%rbp,8)
leaq 0x50(%rsp), %rsi
callq 0x30c0
leaq 0x20(%rsp), %rsi
movq %rbx, %rdi
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), %ecx
movl $0x48, %eax
cmpl $0x48, %ecx
jl 0xfa30
movl $0x63, %eax
cmpl $0x64, %ecx
jb 0xfa37
movl %eax, 0xac(%r15)
movl $0x0, 0xd0(%r15)
movq (%rsp), %rbx
testq %rbx, %rbx
je 0xfabc
cmpb $0x0, (%rbx)
je 0xfabc
leaq 0x6040(%rip), %rsi      # 0x15a97
movq %rbx, %rdi
callq 0x3060
testl %eax, %eax
je 0xfbcd
leaq 0x6030(%rip), %rsi      # 0x15a9e
movq %rbx, %rdi
callq 0x3060
testl %eax, %eax
je 0xfc16
leaq 0x6e3c(%rip), %rsi      # 0x168c1
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
movq %rbx, %rdx
xorl %eax, %eax
callq 0x9f73
jmp 0xfafc
leaq 0x6de0(%rip), %rsi      # 0x1687d
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 0x9f73
leaq 0x80(%r15), %rbp
xorl %eax, %eax
cmpb $0x0, 0xc(%rsp)
setns %al
movl %eax, 0xd0(%r15)
cmpq $0x0, (%rbp)
je 0xfb48
leaq 0x6df6(%rip), %rsi      # 0x168d8
leaq 0x50(%rsp), %rdx
leaq 0x20(%rsp), %rcx
movl $0xfffffffd, %edi       # imm = 0xFFFFFFFD
movq (%rsp), %r8
xorl %eax, %eax
callq 0x9f73
movl %eax, %r15d
jmp 0xfb36
movq 0x80(%r12), %rax
movl %r14d, 0x10(%rax)
movq 0x8(%rax), %rdi
movl %r14d, %ebx
movq %rbx, %rsi
callq 0x3420
movq 0x80(%r12), %rcx
movq %rax, 0x8(%rcx)
movq %rax, %rdi
movq %r13, %rsi
movq %rbx, %rdx
callq 0x3340
movl %r15d, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x68, %edi
callq 0x33b0
movq %rax, %rbx
movw $0x0, (%rax)
xorps %xmm0, %xmm0
movups %xmm0, 0x4(%rax)
movb $0x0, 0x14(%rax)
movq %rax, (%rbp)
movq (%rsp), %rsi
testq %rsi, %rsi
je 0xfb82
addq $0x14, %rbx
movq %rbx, %rdi
callq 0x30c0
movq (%rbp), %rbx
movl %r14d, 0x10(%rbx)
movl %r14d, %r14d
movq %r14, %rdi
callq 0x33b0
movq %rax, 0x8(%rbx)
movq (%rbp), %rax
movq 0x8(%rax), %rdi
movq %r13, %rsi
movq %r14, %rdx
callq 0x3340
movw $0x0, 0x7c(%r15)
movq 0x88(%r15), %rcx
testq %rcx, %rcx
je 0xfbee
movl $0x201, %eax            # imm = 0x201
addl 0x10(%rcx), %eax
shrl $0x9, %eax
movw %ax, 0x7c(%r15)
jmp 0xfbf0
leaq 0x90(%r15), %rbp
movzwl 0x68(%r12), %eax
cmpl %r14d, %eax
jae 0xfad4
movw %r14w, 0x68(%r12)
jmp 0xfad4
xorl %eax, %eax
movq 0x80(%r15), %rcx
testq %rcx, %rcx
je 0xfc0e
movl $0x1ff, %edx            # imm = 0x1FF
addl 0x10(%rcx), %edx
shrl $0x9, %edx
addl %edx, %eax
movw %ax, 0x7c(%r15)
xorl %r15d, %r15d
jmp 0xfb36
leaq 0x88(%r15), %rbp
jmp 0xfad4