// File: /workspace/github_c_makefile_star2_O2/j-hoppe[P]tu58fs/hostdir.c
// Function: snapshot_scan_hostdir
static int snapshot_scan_hostdir(hostdir_t *_this) {
	int i;
	struct stat sb;
	DIR *dfd;
	struct dirent *dp;
	char pathbuff[4096];
	char file_to_delete[4096];
	hostdir_file_t *f;

	// set all files "deleted", found files are overwritten with other state
	// only deleted files remain "deleted"
	for (i = 0; i < _this->snapshot.file_count; i++)
		_this->snapshot.file[i].state[side_host] = fs_missing;

	dfd = opendir(_this->path); // error checking done, compact code
	// make list of regular files
	file_to_delete[0] = 0;
	while ((dp = readdir(dfd))) {
		sprintf(pathbuff, "%s/%s", _this->path, dp->d_name);
		// beware of . and .., not regular file
		if (stat(pathbuff, &sb))
			break;
		if (S_ISREG(sb.st_mode)) {
			// file create on both sides handled
			char *pdp_filename_ext;
			// convert to PDP conventions. Then perhaps not unique!
			pdp_filename_ext = filesystem_filename_from_host(_this->pdp_fs, dp->d_name, NULL,
			NULL);
			// Find entries with same pdp filename, but different host fname.
			// These are the cases were truncing the hostname leads double PDP name
			// Delete those hostfiles.
			f = snapshot_file_register(&_this->snapshot, pdp_filename_ext, side_host);
			if (strlen(f->hostfilename) && strcasecmp(f->hostfilename, dp->d_name)
					&& file_exists(_this->path, f->hostfilename)) {
				// duplicate PDP file with different hostnames
				fprintf(ferr,
						"Host file \"%s\" maps to duplicate PDP filename \"%s\", will be deleted\n",
						dp->d_name, pdp_filename_ext);
				strcpy(file_to_delete, pathbuff);
			} else {
				strcpy(f->hostfilename, dp->d_name);
				if (f->state[side_host] != fs_created) {
					if (f->host_len != sb.st_size || f->host_mtime != STAT_ST_MTIM(sb).tv_sec)
						f->state[side_host] = fs_changed;
					else
						f->state[side_host] = fs_unchanged;
				}
				// update to newest state
				f->host_len = sb.st_size;
				f->host_mtime = STAT_ST_MTIM(sb).tv_sec;
			}
		}
	}
	closedir(dfd);
	// delete only one hostfile per round ... in fact a whole file list should be maintained
	if (strlen(file_to_delete))
		unlink(file_to_delete);
	return ERROR_OK;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x20b8, %rsp           # imm = 0x20B8
movslq 0x1018(%rdi), %rax
movq %rdi, 0x8(%rsp)
leaq 0x114c(%rdi), %rcx
xorl %edx, %edx
cmpq %rax, %rdx
jge 0x934d
movl $0x1, (%rcx)
incq %rdx
addq $0x150, %rcx            # imm = 0x150
jmp 0x9336
movq 0x8(%rsp), %rax
leaq 0x1010(%rax), %rcx
movq %rcx, 0x18(%rsp)
leaq 0x4(%rax), %rdi
movq %rdi, 0x10(%rsp)
callq 0x3160
movq %rax, %r14
leaq 0xb0(%rsp), %rax
movb $0x0, (%rax)
leaq 0xb10e(%rip), %rbx      # 0x1448f
leaq 0x10b0(%rsp), %r13
leaq 0x20(%rsp), %r15
movl $0xf000, %r12d          # imm = 0xF000
movq %r14, (%rsp)
movq %r14, %rdi
callq 0x3390
testq %rax, %rax
je 0x94f9
movq %rax, %rbp
addq $0x13, %rbp
movq %r13, %rdi
movq %rbx, %rsi
movq 0x10(%rsp), %rdx
movq %rbp, %rcx
xorl %eax, %eax
callq 0x3530
movq %r13, %rdi
movq %r15, %rsi
callq 0x3320
testl %eax, %eax
jne 0x94f9
movl 0x38(%rsp), %eax
andl %r12d, %eax
cmpl $0x8000, %eax           # imm = 0x8000
jne 0x9398
movq %r13, %r15
movq %rbx, %r12
movq 0x8(%rsp), %rax
movq 0x1008(%rax), %rdi
movq %rbp, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0xb232
movq %rax, %rbx
movq 0x18(%rsp), %rdi
movq %rax, %rsi
pushq $0x1
popq %rdx
callq 0x871b
movq %rax, %r14
leaq 0x28(%rax), %r13
cmpb $0x0, 0x28(%rax)
je 0x948c
movq %r13, %rdi
movq %rbp, %rsi
callq 0x3060
testl %eax, %eax
je 0x948c
movq 0x10(%rsp), %rdi
movq %r13, %rsi
callq 0xa291
testl %eax, %eax
je 0x948c
leaq 0x14de5(%rip), %rax     # 0x1e230
movq (%rax), %rdi
leaq 0xb24a(%rip), %rsi      # 0x1469f
movq %rbp, %rdx
movq %rbx, %rcx
xorl %eax, %eax
callq 0x3310
leaq 0xb0(%rsp), %rdi
movq %r15, %r13
movq %r15, %rsi
callq 0x30c0
movq %r12, %rbx
movq (%rsp), %r14
leaq 0x20(%rsp), %r15
movl $0xf000, %r12d          # imm = 0xF000
jmp 0x9398
movq %r13, %rdi
movq %rbp, %rsi
callq 0x30c0
cmpl $0x3, 0x12c(%r14)
movq 0x50(%rsp), %rax
movq %r12, %rbx
movl $0xf000, %r12d          # imm = 0xF000
je 0x94d5
pushq $0x2
popq %rcx
cmpq %rax, 0x130(%r14)
jne 0x94ce
movq 0x138(%r14), %rdx
xorl %ecx, %ecx
cmpq 0x78(%rsp), %rdx
setne %cl
addl %ecx, %ecx
movl %ecx, 0x12c(%r14)
movq %rax, 0x130(%r14)
movq 0x78(%rsp), %rax
movq %rax, 0x138(%r14)
movq (%rsp), %r14
movq %r15, %r13
leaq 0x20(%rsp), %r15
jmp 0x9398
movq %r14, %rdi
callq 0x3270
cmpb $0x0, 0xb0(%rsp)
je 0x9518
leaq 0xb0(%rsp), %rdi
callq 0x3090
addq $0x20b8, %rsp           # imm = 0x20B8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq