// File: /workspace/github_c_makefile_star2_O0/j-hoppe[P]tu58fs/rt11.c
// Function: render_directory_entry
static int render_directory_entry(rt11_filesystem_t *_this, rt11_file_t *f, int ds_nr,
		int de_nr) {
	uint16_t *ds = DIR_SEGMENT(_this, ds_nr); // ptr to dir segment in image
	uint16_t *de; // ptr to dir entry in image
	int dir_entry_word_count = 7 + (_this->dir_entry_extra_bytes / 2);
	uint16_t w;
	char buff[80];
	if (de_nr == 0) {
		// 1st entry in segment: write 5 word header
		IMAGE_PUT_WORD(ds + 0, _this->dir_total_seg_num);
		if (ds_nr == _this->dir_max_seg_nr)
			IMAGE_PUT_WORD(ds + 1, 0); // last segment
		else
			IMAGE_PUT_WORD(ds + 1, ds_nr + 1); // link to next segment
		IMAGE_PUT_WORD(ds + 2, _this->dir_max_seg_nr);
		IMAGE_PUT_WORD(ds + 3, _this->dir_entry_extra_bytes);
		if (f)
			IMAGE_PUT_WORD(ds + 4, f->block_nr); // start of first file on disk
		else
			// end marker at first entry
			IMAGE_PUT_WORD(ds + 4, _this->file_space_blocknr);
	}
	// write dir_entry
	de = ds + 5 + de_nr * dir_entry_word_count;
	//fprintf(stderr, "ds_nr=%d, de_nr=%d, ds in img=0x%lx, de in img =0x%lx\n", ds_nr, de_nr,
	//		(uint8_t*) ds - *_this->image_data_ptr, (uint8_t*) de - *_this->image_data_ptr);
	if (f == NULL) {
		// write start of free chain: space after last file
		IMAGE_PUT_WORD(de + 0, RT11_FILE_EMPTY);
		// after INIT free space has the name " EMPTY.FIL"
		IMAGE_PUT_WORD(de + 1, rad50_encode(" EM"));
		IMAGE_PUT_WORD(de + 2, rad50_encode("PTY"));
		IMAGE_PUT_WORD(de + 3, rad50_encode("FIL"));
		IMAGE_PUT_WORD(de + 4, _this->free_blocks); // block count
		IMAGE_PUT_WORD(de + 5, 0); // job/channel
		IMAGE_PUT_WORD(de + 6, 0); // INIT sets a creation date ... don't need to!
	} else {
		// regular file
		// status
		w = RT11_FILE_EPERM;
		if (f->readonly)
			w |= RT11_FILE_EREAD | RT11_FILE_EPROT;
		if (f->prefix)
			w |= RT11_FILE_EPRE;
		IMAGE_PUT_WORD(de + 0, w);

		// filename chars 0..2
		strncpy(buff, f->filnam, 3);
		buff[3] = 0;
		IMAGE_PUT_WORD(de + 1, rad50_encode(buff));

		// filename chars 3..5
		if (strlen(f->filnam) < 4)
			buff[0] = 0;
		else
			strncpy(buff, f->filnam + 3, 3);
		buff[3] = 0;
		IMAGE_PUT_WORD(de + 2, rad50_encode(buff));
		// ext
		IMAGE_PUT_WORD(de + 3, rad50_encode(f->ext));
		// total file len
		IMAGE_PUT_WORD(de + 4, f->block_count);
		// clr job/channel
		IMAGE_PUT_WORD(de + 5, 0);
		//date. do not set "age", as it is not evaluated by DEC software.
		// year already in range 1972..1999
		w = f->date.tm_year - 72;
		w |= f->date.tm_mday << 5;
		w |= (f->date.tm_mon + 1) << 10;
		IMAGE_PUT_WORD(de + 6, w);
		if (f->dir_ext) {
			// write bytes from "dir extension" stream into directory entry
			if (f->dir_ext->data_size > _this->dir_entry_extra_bytes)
				return error_set(ERROR_FILESYSTEM_OVERFLOW,
						"render_directory(): file %s dir_ext size %d > extra bytes in dir %d\n",
						f->filnam, f->dir_ext->data_size, _this->dir_entry_extra_bytes);
			memcpy(de + 7, f->dir_ext->data, f->dir_ext->data_size);
		}
	}
	// write end-of-segment marker behind dir_entry.
	IMAGE_PUT_WORD(de + dir_entry_word_count, RT11_DIR_EEOS);
	// this is overwritten by next entry; and remains if last entry in segment
	return ERROR_OK;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x90, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl %edx, -0x1c(%rbp)
movl %ecx, -0x20(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq -0x10(%rbp), %rcx
movzwl 0x34(%rcx), %ecx
shll $0x9, %ecx
movslq %ecx, %rcx
addq %rcx, %rax
movl -0x1c(%rbp), %ecx
subl $0x1, %ecx
shll %ecx
shll $0x9, %ecx
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0x28(%rbp)
movq -0x10(%rbp), %rax
movzwl 0x68(%rax), %eax
movl $0x2, %ecx
cltd
idivl %ecx
addl $0x7, %eax
movl %eax, -0x34(%rbp)
cmpl $0x0, -0x20(%rbp)
jne 0x16b3d
movq -0x10(%rbp), %rax
movzwl 0x64(%rax), %eax
andl $0xff, %eax
movb %al, %cl
movq -0x28(%rbp), %rax
movb %cl, (%rax)
movq -0x10(%rbp), %rax
movzwl 0x64(%rax), %eax
sarl $0x8, %eax
andl $0xff, %eax
movb %al, %cl
movq -0x28(%rbp), %rax
movb %cl, 0x1(%rax)
movl -0x1c(%rbp), %eax
movq -0x10(%rbp), %rcx
movzwl 0x66(%rcx), %ecx
cmpl %ecx, %eax
jne 0x16a45
movq -0x28(%rbp), %rax
movb $0x0, 0x2(%rax)
movq -0x28(%rbp), %rax
movb $0x0, 0x3(%rax)
jmp 0x16a70
movl -0x1c(%rbp), %eax
addl $0x1, %eax
andl $0xff, %eax
movb %al, %cl
movq -0x28(%rbp), %rax
movb %cl, 0x2(%rax)
movl -0x1c(%rbp), %eax
addl $0x1, %eax
sarl $0x8, %eax
andl $0xff, %eax
movb %al, %cl
movq -0x28(%rbp), %rax
movb %cl, 0x3(%rax)
movq -0x10(%rbp), %rax
movzwl 0x66(%rax), %eax
andl $0xff, %eax
movb %al, %cl
movq -0x28(%rbp), %rax
movb %cl, 0x4(%rax)
movq -0x10(%rbp), %rax
movzwl 0x66(%rax), %eax
sarl $0x8, %eax
andl $0xff, %eax
movb %al, %cl
movq -0x28(%rbp), %rax
movb %cl, 0x5(%rax)
movq -0x10(%rbp), %rax
movzwl 0x68(%rax), %eax
andl $0xff, %eax
movb %al, %cl
movq -0x28(%rbp), %rax
movb %cl, 0x6(%rax)
movq -0x10(%rbp), %rax
movzwl 0x68(%rax), %eax
sarl $0x8, %eax
andl $0xff, %eax
movb %al, %cl
movq -0x28(%rbp), %rax
movb %cl, 0x7(%rax)
cmpq $0x0, -0x18(%rbp)
je 0x16b06
movq -0x18(%rbp), %rax
movzwl 0x7a(%rax), %eax
andl $0xff, %eax
movb %al, %cl
movq -0x28(%rbp), %rax
movb %cl, 0x8(%rax)
movq -0x18(%rbp), %rax
movzwl 0x7a(%rax), %eax
sarl $0x8, %eax
andl $0xff, %eax
movb %al, %cl
movq -0x28(%rbp), %rax
movb %cl, 0x9(%rax)
jmp 0x16b3b
movq -0x10(%rbp), %rax
movzwl 0x1fd4(%rax), %eax
andl $0xff, %eax
movb %al, %cl
movq -0x28(%rbp), %rax
movb %cl, 0x8(%rax)
movq -0x10(%rbp), %rax
movzwl 0x1fd4(%rax), %eax
sarl $0x8, %eax
andl $0xff, %eax
movb %al, %cl
movq -0x28(%rbp), %rax
movb %cl, 0x9(%rax)
jmp 0x16b3d
movq -0x28(%rbp), %rax
addq $0xa, %rax
movl -0x20(%rbp), %ecx
imull -0x34(%rbp), %ecx
movslq %ecx, %rcx
shlq %rcx
addq %rcx, %rax
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x18(%rbp)
jne 0x16c84
movq -0x30(%rbp), %rax
movb $0x0, (%rax)
movq -0x30(%rbp), %rax
movb $0x2, 0x1(%rax)
leaq 0x6fa1(%rip), %rdi      # 0x1db1b
callq 0xdaa0
movzwl %ax, %eax
andl $0xff, %eax
movb %al, %cl
movq -0x30(%rbp), %rax
movb %cl, 0x2(%rax)
leaq 0x6f84(%rip), %rdi      # 0x1db1b
callq 0xdaa0
movzwl %ax, %eax
sarl $0x8, %eax
andl $0xff, %eax
movb %al, %cl
movq -0x30(%rbp), %rax
movb %cl, 0x3(%rax)
leaq 0x6f68(%rip), %rdi      # 0x1db1f
callq 0xdaa0
movzwl %ax, %eax
andl $0xff, %eax
movb %al, %cl
movq -0x30(%rbp), %rax
movb %cl, 0x4(%rax)
leaq 0x6f4b(%rip), %rdi      # 0x1db1f
callq 0xdaa0
movzwl %ax, %eax
sarl $0x8, %eax
andl $0xff, %eax
movb %al, %cl
movq -0x30(%rbp), %rax
movb %cl, 0x5(%rax)
leaq 0x6f2f(%rip), %rdi      # 0x1db23
callq 0xdaa0
movzwl %ax, %eax
andl $0xff, %eax
movb %al, %cl
movq -0x30(%rbp), %rax
movb %cl, 0x6(%rax)
leaq 0x6f12(%rip), %rdi      # 0x1db23
callq 0xdaa0
movzwl %ax, %eax
sarl $0x8, %eax
andl $0xff, %eax
movb %al, %cl
movq -0x30(%rbp), %rax
movb %cl, 0x7(%rax)
movq -0x10(%rbp), %rax
movzwl 0x1fd2(%rax), %eax
andl $0xff, %eax
movb %al, %cl
movq -0x30(%rbp), %rax
movb %cl, 0x8(%rax)
movq -0x10(%rbp), %rax
movzwl 0x1fd2(%rax), %eax
sarl $0x8, %eax
andl $0xff, %eax
movb %al, %cl
movq -0x30(%rbp), %rax
movb %cl, 0x9(%rax)
movq -0x30(%rbp), %rax
movb $0x0, 0xa(%rax)
movq -0x30(%rbp), %rax
movb $0x0, 0xb(%rax)
movq -0x30(%rbp), %rax
movb $0x0, 0xc(%rax)
movq -0x30(%rbp), %rax
movb $0x0, 0xd(%rax)
jmp 0x16f31
movw $0x400, -0x36(%rbp)     # imm = 0x400
movq -0x18(%rbp), %rax
cmpl $0x0, 0xd0(%rax)
je 0x16ca4
movzwl -0x36(%rbp), %eax
orl $0xc000, %eax           # imm = 0xC000
movw %ax, -0x36(%rbp)
movq -0x18(%rbp), %rax
cmpq $0x0, 0x88(%rax)
je 0x16cbd
movzwl -0x36(%rbp), %eax
orl $0x10, %eax
movw %ax, -0x36(%rbp)
movzwl -0x36(%rbp), %eax
andl $0xff, %eax
movb %al, %cl
movq -0x30(%rbp), %rax
movb %cl, (%rax)
movzwl -0x36(%rbp), %eax
sarl $0x8, %eax
andl $0xff, %eax
movb %al, %cl
movq -0x30(%rbp), %rax
movb %cl, 0x1(%rax)
leaq -0x90(%rbp), %rdi
movq -0x18(%rbp), %rsi
addq $0x2, %rsi
movl $0x3, %edx
callq 0x3090
movb $0x0, -0x8d(%rbp)
leaq -0x90(%rbp), %rdi
callq 0xdaa0
movzwl %ax, %eax
andl $0xff, %eax
movb %al, %cl
movq -0x30(%rbp), %rax
movb %cl, 0x2(%rax)
leaq -0x90(%rbp), %rdi
callq 0xdaa0
movzwl %ax, %eax
sarl $0x8, %eax
andl $0xff, %eax
movb %al, %cl
movq -0x30(%rbp), %rax
movb %cl, 0x3(%rax)
movq -0x18(%rbp), %rdi
addq $0x2, %rdi
callq 0x3190
cmpq $0x4, %rax
jae 0x16d5c
movb $0x0, -0x90(%rbp)
jmp 0x16d79
leaq -0x90(%rbp), %rdi
movq -0x18(%rbp), %rsi
addq $0x2, %rsi
addq $0x3, %rsi
movl $0x3, %edx
callq 0x3090
movb $0x0, -0x8d(%rbp)
leaq -0x90(%rbp), %rdi
callq 0xdaa0
movzwl %ax, %eax
andl $0xff, %eax
movb %al, %cl
movq -0x30(%rbp), %rax
movb %cl, 0x4(%rax)
leaq -0x90(%rbp), %rdi
callq 0xdaa0
movzwl %ax, %eax
sarl $0x8, %eax
andl $0xff, %eax
movb %al, %cl
movq -0x30(%rbp), %rax
movb %cl, 0x5(%rax)
movq -0x18(%rbp), %rdi
addq $0x52, %rdi
callq 0xdaa0
movzwl %ax, %eax
andl $0xff, %eax
movb %al, %cl
movq -0x30(%rbp), %rax
movb %cl, 0x6(%rax)
movq -0x18(%rbp), %rdi
addq $0x52, %rdi
callq 0xdaa0
movzwl %ax, %eax
sarl $0x8, %eax
andl $0xff, %eax
movb %al, %cl
movq -0x30(%rbp), %rax
movb %cl, 0x7(%rax)
movq -0x18(%rbp), %rax
movzwl 0x7c(%rax), %eax
andl $0xff, %eax
movb %al, %cl
movq -0x30(%rbp), %rax
movb %cl, 0x8(%rax)
movq -0x18(%rbp), %rax
movzwl 0x7c(%rax), %eax
sarl $0x8, %eax
andl $0xff, %eax
movb %al, %cl
movq -0x30(%rbp), %rax
movb %cl, 0x9(%rax)
movq -0x30(%rbp), %rax
movb $0x0, 0xa(%rax)
movq -0x30(%rbp), %rax
movb $0x0, 0xb(%rax)
movq -0x18(%rbp), %rax
movl 0xac(%rax), %eax
subl $0x48, %eax
movw %ax, -0x36(%rbp)
movq -0x18(%rbp), %rax
movl 0xa4(%rax), %ecx
shll $0x5, %ecx
movzwl -0x36(%rbp), %eax
orl %ecx, %eax
movw %ax, -0x36(%rbp)
movq -0x18(%rbp), %rax
movl 0xa8(%rax), %ecx
addl $0x1, %ecx
shll $0xa, %ecx
movzwl -0x36(%rbp), %eax
orl %ecx, %eax
movw %ax, -0x36(%rbp)
movzwl -0x36(%rbp), %eax
andl $0xff, %eax
movb %al, %cl
movq -0x30(%rbp), %rax
movb %cl, 0xc(%rax)
movzwl -0x36(%rbp), %eax
sarl $0x8, %eax
andl $0xff, %eax
movb %al, %cl
movq -0x30(%rbp), %rax
movb %cl, 0xd(%rax)
movq -0x18(%rbp), %rax
cmpq $0x0, 0x90(%rax)
je 0x16f2f
movq -0x18(%rbp), %rax
movq 0x90(%rax), %rax
movl 0x10(%rax), %eax
movq -0x10(%rbp), %rcx
movzwl 0x68(%rcx), %ecx
cmpl %ecx, %eax
jbe 0x16f03
movq -0x18(%rbp), %rdx
addq $0x2, %rdx
movq -0x18(%rbp), %rax
movq 0x90(%rax), %rax
movl 0x10(%rax), %ecx
movq -0x10(%rbp), %rax
movzwl 0x68(%rax), %r8d
movl $0xfffffffe, %edi       # imm = 0xFFFFFFFE
leaq 0x6c30(%rip), %rsi      # 0x1db27
movb $0x0, %al
callq 0xcd70
movl %eax, -0x4(%rbp)
jmp 0x16f51
movq -0x30(%rbp), %rdi
addq $0xe, %rdi
movq -0x18(%rbp), %rax
movq 0x90(%rax), %rax
movq 0x8(%rax), %rsi
movq -0x18(%rbp), %rax
movq 0x90(%rax), %rax
movl 0x10(%rax), %eax
movl %eax, %edx
callq 0x3330
jmp 0x16f31
movq -0x30(%rbp), %rax
movslq -0x34(%rbp), %rcx
movb $0x0, (%rax,%rcx,2)
movq -0x30(%rbp), %rax
movslq -0x34(%rbp), %rcx
movb $0x8, 0x1(%rax,%rcx,2)
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x90, %rsp
popq %rbp
retq
nopl (%rax)