// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0xc0, %rsp
movl 0x10(%rbp), %eax
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movl %r8d, -0x2c(%rbp)
movq %r9, -0x38(%rbp)
movq -0x18(%rbp), %rdi
leaq 0x72d8(%rip), %rsi      # 0x1c3e9
callq 0x3050
cmpl $0x0, %eax
jne 0x15120
jmp 0x1565f
movq -0x18(%rbp), %rdi
leaq 0x79b1(%rip), %rsi      # 0x1cadc
callq 0x3050
cmpl $0x0, %eax
jne 0x151bb
cmpq $0x0, -0x20(%rbp)
jne 0x151b6
cmpl $0x200, 0x10(%rbp)      # imm = 0x200
je 0x15169
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
leaq 0x862c(%rip), %rsi      # 0x1d781
movl $0x200, %edx            # imm = 0x200
movb $0x0, %al
callq 0xcd70
movl %eax, -0x4(%rbp)
jmp 0x15666
movl 0x10(%rbp), %ecx
movq -0x10(%rbp), %rax
movq 0x78(%rax), %rax
movl %ecx, 0x10(%rax)
movq -0x10(%rbp), %rax
movq 0x78(%rax), %rax
movq 0x8(%rax), %rdi
movl 0x10(%rbp), %eax
movl %eax, %esi
callq 0x3410
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq 0x78(%rax), %rax
movq %rcx, 0x8(%rax)
movq -0x10(%rbp), %rax
movq 0x78(%rax), %rax
movq 0x8(%rax), %rdi
movq -0x38(%rbp), %rsi
movl 0x10(%rbp), %eax
movl %eax, %edx
callq 0x3330
jmp 0x1565d
movq -0x18(%rbp), %rdi
leaq 0x7939(%rip), %rsi      # 0x1caff
callq 0x3050
cmpl $0x0, %eax
jne 0x15269
cmpq $0x0, -0x20(%rbp)
jne 0x15264
cmpl $0x800, 0x10(%rbp)      # imm = 0x800
jbe 0x1520b
movl 0x10(%rbp), %edx
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
leaq 0x85a2(%rip), %rsi      # 0x1d799
movl $0x800, %ecx            # imm = 0x800
movb $0x0, %al
callq 0xcd70
movl %eax, -0x4(%rbp)
jmp 0x15666
movl 0x10(%rbp), %ecx
movq -0x10(%rbp), %rax
movq 0x80(%rax), %rax
movl %ecx, 0x10(%rax)
movq -0x10(%rbp), %rax
movq 0x80(%rax), %rax
movq 0x8(%rax), %rdi
movl 0x10(%rbp), %eax
movl %eax, %esi
callq 0x3410
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq 0x80(%rax), %rax
movq %rcx, 0x8(%rax)
movq -0x10(%rbp), %rax
movq 0x80(%rax), %rax
movq 0x8(%rax), %rdi
movq -0x38(%rbp), %rsi
movl 0x10(%rbp), %eax
movl %eax, %edx
callq 0x3330
jmp 0x1565b
movq -0x10(%rbp), %rax
movl 0x8c(%rax), %eax
addl $0x1, %eax
cmpl $0x3e8, %eax            # imm = 0x3E8
jl 0x1529d
movl $0xfffffffe, %edi       # imm = 0xFFFFFFFE
leaq 0x78ae(%rip), %rsi      # 0x1cb37
movl $0x3e8, %edx            # imm = 0x3E8
movb $0x0, %al
callq 0xcd70
movl %eax, -0x4(%rbp)
jmp 0x15666
movq -0x10(%rbp), %rdi
movl 0x10(%rbp), %esi
callq 0x15680
cmpl $0x0, %eax
je 0x152d0
movq -0x18(%rbp), %rdx
movl 0x10(%rbp), %ecx
movl $0xfffffffe, %edi       # imm = 0xFFFFFFFE
leaq 0x7896(%rip), %rsi      # 0x1cb57
movb $0x0, %al
callq 0xcd70
movl %eax, -0x4(%rbp)
jmp 0x15666
movq -0x18(%rbp), %rdi
leaq -0x70(%rbp), %rsi
leaq -0xa0(%rbp), %rdx
callq 0x15960
movq $0x0, -0x40(%rbp)
movl $0x0, -0xa4(%rbp)
movl -0xa4(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl 0x8c(%rcx), %eax
jge 0x15375
movq -0x10(%rbp), %rax
movslq -0xa4(%rbp), %rcx
movq 0x90(%rax,%rcx,8), %rax
movq %rax, -0xb8(%rbp)
leaq -0x70(%rbp), %rdi
movq -0xb8(%rbp), %rsi
addq $0x2, %rsi
callq 0x3050
cmpl $0x0, %eax
jne 0x15362
leaq -0xa0(%rbp), %rdi
movq -0xb8(%rbp), %rsi
addq $0x52, %rsi
callq 0x3050
cmpl $0x0, %eax
jne 0x15362
movq -0xb8(%rbp), %rax
movq %rax, -0x40(%rbp)
jmp 0x15364
movl -0xa4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xa4(%rbp)
jmp 0x152f6
cmpq $0x0, -0x40(%rbp)
jne 0x1544c
callq 0x13640
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rdx
movq -0x10(%rbp), %rax
movq -0x10(%rbp), %rsi
movl 0x8c(%rsi), %ecx
movl %ecx, %edi
addl $0x1, %edi
movl %edi, 0x8c(%rsi)
movslq %ecx, %rcx
movq %rdx, 0x90(%rax,%rcx,8)
movq -0x40(%rbp), %rdi
addq $0x2, %rdi
leaq -0x70(%rbp), %rsi
callq 0x30c0
movq -0x40(%rbp), %rdi
addq $0x52, %rdi
leaq -0xa0(%rbp), %rsi
callq 0x30c0
movq -0x40(%rbp), %rax
addq $0x98, %rax
movq %rax, -0xc0(%rbp)
leaq -0x28(%rbp), %rdi
callq 0x3060
movq -0xc0(%rbp), %rdi
movq %rax, %rsi
movl $0x38, %edx
callq 0x3330
movq -0x40(%rbp), %rax
cmpl $0x48, 0xac(%rax)
jge 0x15421
movq -0x40(%rbp), %rax
movl $0x48, 0xac(%rax)
jmp 0x1543e
movq -0x40(%rbp), %rax
cmpl $0x63, 0xac(%rax)
jle 0x1543c
movq -0x40(%rbp), %rax
movl $0x63, 0xac(%rax)
jmp 0x1543e
movq -0x40(%rbp), %rax
movl $0x0, 0xd0(%rax)
movq $0x0, -0xb0(%rbp)
cmpq $0x0, -0x20(%rbp)
je 0x1546d
movq -0x20(%rbp), %rdi
callq 0x3190
cmpq $0x0, %rax
jne 0x154a2
movq -0x40(%rbp), %rax
addq $0x80, %rax
movq %rax, -0xb0(%rbp)
movl -0x2c(%rbp), %eax
andl $0x80, %eax
cmpl $0x0, %eax
setne %al
xorb $-0x1, %al
andb $0x1, %al
movzbl %al, %ecx
movq -0x40(%rbp), %rax
movl %ecx, 0xd0(%rax)
jmp 0x15532
movq -0x20(%rbp), %rdi
leaq 0x74b0(%rip), %rsi      # 0x1c95d
callq 0x3050
cmpl $0x0, %eax
jne 0x154e7
movq -0x40(%rbp), %rax
addq $0x90, %rax
movq %rax, -0xb0(%rbp)
movl 0x10(%rbp), %eax
movq -0x10(%rbp), %rcx
movzwl 0x68(%rcx), %ecx
cmpl %ecx, %eax
jbe 0x154e5
movl 0x10(%rbp), %eax
movw %ax, %cx
movq -0x10(%rbp), %rax
movw %cx, 0x68(%rax)
jmp 0x15530
movq -0x20(%rbp), %rdi
leaq 0x7472(%rip), %rsi      # 0x1c964
callq 0x3050
cmpl $0x0, %eax
jne 0x1550f
movq -0x40(%rbp), %rax
addq $0x88, %rax
movq %rax, -0xb0(%rbp)
jmp 0x1552e
movq -0x20(%rbp), %rdx
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
leaq 0x82a6(%rip), %rsi      # 0x1d7c5
movb $0x0, %al
callq 0xcd70
movl %eax, -0x4(%rbp)
jmp 0x15666
jmp 0x15530
jmp 0x15532
movq -0xb0(%rbp), %rax
cmpq $0x0, (%rax)
je 0x15569
leaq -0x70(%rbp), %rdx
leaq -0xa0(%rbp), %rcx
movq -0x20(%rbp), %r8
movl $0xfffffffd, %edi       # imm = 0xFFFFFFFD
leaq 0x8282(%rip), %rsi      # 0x1d7dc
movb $0x0, %al
callq 0xcd70
movl %eax, -0x4(%rbp)
jmp 0x15666
callq 0x17040
movq %rax, %rcx
movq -0xb0(%rbp), %rax
movq %rcx, (%rax)
cmpq $0x0, -0x20(%rbp)
je 0x15599
movq -0xb0(%rbp), %rax
movq (%rax), %rdi
addq $0x14, %rdi
movq -0x20(%rbp), %rsi
callq 0x30c0
movl 0x10(%rbp), %ecx
movq -0xb0(%rbp), %rax
movq (%rax), %rax
movl %ecx, 0x10(%rax)
movl 0x10(%rbp), %eax
movl %eax, %edi
callq 0x33a0
movq %rax, %rcx
movq -0xb0(%rbp), %rax
movq (%rax), %rax
movq %rcx, 0x8(%rax)
movq -0xb0(%rbp), %rax
movq (%rax), %rax
movq 0x8(%rax), %rdi
movq -0x38(%rbp), %rsi
movl 0x10(%rbp), %eax
movl %eax, %edx
callq 0x3330
movq -0x40(%rbp), %rax
movw $0x0, 0x7c(%rax)
movq -0x40(%rbp), %rax
cmpq $0x0, 0x88(%rax)
je 0x15623
movq -0x40(%rbp), %rax
movq 0x88(%rax), %rax
movl 0x10(%rax), %edx
addl $0x2, %edx
addl $0x200, %edx            # imm = 0x200
subl $0x1, %edx
shrl $0x9, %edx
movq -0x40(%rbp), %rax
movzwl 0x7c(%rax), %ecx
addl %edx, %ecx
movw %cx, 0x7c(%rax)
movq -0x40(%rbp), %rax
cmpq $0x0, 0x80(%rax)
je 0x15659
movq -0x40(%rbp), %rax
movq 0x80(%rax), %rax
movl 0x10(%rax), %edx
addl $0x200, %edx            # imm = 0x200
subl $0x1, %edx
shrl $0x9, %edx
movq -0x40(%rbp), %rax
movzwl 0x7c(%rax), %ecx
addl %edx, %ecx
movw %cx, 0x7c(%rax)
jmp 0x1565b
jmp 0x1565d
jmp 0x1565f
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0xc0, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)