// File: /workspace/github_c_makefile_star2_O1/j-hoppe[P]tu58fs/hostdir.c
// Function: hostdir_sync
int hostdir_sync(hostdir_t *_this) {
	// int any_file_change;
	int update_pdp;
	int update_snapshot;
	int i;
	// entry: snapshot contains files in shared dir,
	// as PDP filesystem and hostdir where synched

	// scan hostdir
	snapshot_scan_hostdir(_this);

	// scan PDP image
	snapshot_scan_pdpimage(_this);

	// print state
	if (opt_debug)
		snapshot_print(&_this->snapshot, ferr, 1);
	else if (opt_verbose)
		snapshot_print(&_this->snapshot, ferr, 0);

	update_pdp = 0;
	update_snapshot = 0;
	if (_this->pdp_fs->readonly) {
		// readonly: only sync hostdir from PDP file system
		int hostdir_changed = 0;
		for (i = 0; i < _this->snapshot.file_count; i++) {
			hostdir_file_t *f = &_this->snapshot.file[i];
			if (f->state[side_host] != fs_unchanged)
				hostdir_changed = 1;
		}
		if (hostdir_changed) {
			// short code: rebuild whole hostdir
			info("Unit %d: Device is readonly, reverting changes in shared dir \"%s\".", _this->unit, _this->path) ;
			hostdir_prepare(_this, /*wipe*/1, /*exists*/0, NULL);
			hostdir_from_pdp_fs(_this);
			update_snapshot = 1;
		}
	} else {
		// not readonly: update hostdir and PDP file system
		for (i = 0; i < _this->snapshot.file_count; i++) {
			hostdir_file_t *f = &_this->snapshot.file[i];
			// 16 cases. The cases when one side is unchanged are easy
			if (f->state[side_pdp] == fs_unchanged && f->state[side_host] == fs_unchanged) {
				// do nothing
			} else if (f->state[side_pdp] == fs_unchanged
					&& f->state[side_host] == fs_missing) {
				if (f->pdp_fixed)
					hostdir_file_copy_from_pdp(_this, f); // restore
				else
					update_pdp = 1; // update PDP from hostdir
			} else if (f->state[side_pdp] == fs_unchanged
					&& f->state[side_host] == fs_changed) {
				update_pdp = 1; // update PDP from hostdir
			} else if (f->state[side_pdp] == fs_unchanged
					&& f->state[side_host] == fs_created) {
				update_pdp = 1; // update PDP from hostdir

			} else if (f->state[side_pdp] == fs_missing
					&& f->state[side_host] == fs_unchanged) {
				hostdir_file_delete(_this, f);
				update_snapshot = 1;
			} else if (f->state[side_pdp] == fs_missing && f->state[side_host] == fs_missing) {
				// no need to update hostdir from pdp
				update_snapshot = 1; // but delete file from snapshot!
			} else if (f->state[side_pdp] == fs_missing && f->state[side_host] == fs_changed) {
				if (_this->pdp_priority)
					hostdir_file_delete(_this, f);
				else
					update_pdp = 1;
				update_snapshot = 1;
			} else if (f->state[side_pdp] == fs_missing && f->state[side_host] == fs_created) {
				// the file was created on host and is not yet on PDP
				update_pdp = 1; // force reload
			} else if (f->state[side_pdp] == fs_changed
					&& f->state[side_host] == fs_unchanged) {
				hostdir_file_copy_from_pdp(_this, f);
				update_snapshot = 1;
			} else if (f->state[side_pdp] == fs_changed && f->state[side_host] == fs_missing) {
				if (f->pdp_fixed || _this->pdp_priority)
					hostdir_file_copy_from_pdp(_this, f);
				else
					update_pdp = 1;
				update_snapshot = 1;
			} else if (f->state[side_pdp] == fs_changed && f->state[side_host] == fs_changed) {
				if (_this->pdp_priority)
					hostdir_file_copy_from_pdp(_this, f);
				else
					update_pdp = 1;
				update_snapshot = 1;
			} else if (f->state[side_pdp] == fs_changed && f->state[side_host] == fs_created) {
				if (_this->pdp_priority)
					hostdir_file_copy_from_pdp(_this, f);
				else
					update_pdp = 1;
				update_snapshot = 1;
			} else if (f->state[side_pdp] == fs_created
					&& f->state[side_host] == fs_unchanged) {
				hostdir_file_copy_from_pdp(_this, f);
				update_snapshot = 1;
			} else if (f->state[side_pdp] == fs_created && f->state[side_host] == fs_missing) {
				// new on PDP
				hostdir_file_copy_from_pdp(_this, f);
				update_snapshot = 1;
			} else if (f->state[side_pdp] == fs_created && f->state[side_host] == fs_changed) {
				if (_this->pdp_priority)
					hostdir_file_copy_from_pdp(_this, f);
				else
					update_pdp = 1;
				update_snapshot = 1;
			} else if (f->state[side_pdp] == fs_created && f->state[side_host] == fs_created) {
				if (_this->pdp_priority)
					hostdir_file_copy_from_pdp(_this, f);
				else
					update_pdp = 1;
				update_snapshot = 1;
			}
		}
	}
	if (update_pdp) {
		hostdir_file_t *f;
		// files in the host dir have changed:
		// reload the tu58 image
		hostdir_image_reload(_this);
		// send volum.inf (RT11)
		f = snapshot_file_find(&_this->snapshot, "$VOLUM.INF");
		if (f)
			hostdir_file_copy_from_pdp(_this, f);
		if (opt_verbose)
			info("Unit %d: Updated PDP image with shared dir \"%s\".", _this->unit, _this->path);
	}
	if (update_pdp || update_snapshot) {
		// if file was deleted
		snapshot_init(_this);
		if (opt_verbose)
			info("Unit %d: Scanned content of shared dir \"%s\".", _this->unit, _this->path);
	}

	return ERROR_OK;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rdi, %rbx
callq 0x99e7
movq %rbx, %rdi
callq 0x9c08
leaq 0x13435(%rip), %rax     # 0x1ca84
movl $0x1, %esi
cmpl $0x0, (%rax)
jne 0x9667
leaq 0x13414(%rip), %rax     # 0x1ca74
xorl %esi, %esi
cmpl $0x0, (%rax)
je 0x9673
leaq 0x1010(%rbx), %rdi
callq 0x9d54
movq 0x1008(%rbx), %rax
cmpl $0x0, 0x4(%rax)
movl 0x1018(%rbx), %eax
je 0x96f8
xorl %r15d, %r15d
testl %eax, %eax
jle 0x98f8
imulq $0x150, %rax, %rax      # imm = 0x150
movl $0x1, %ecx
xorl %edx, %edx
cmpl $0x0, 0x114c(%rbx,%rdx)
cmovnel %ecx, %r15d
addq $0x150, %rdx            # imm = 0x150
cmpq %rdx, %rax
jne 0x969f
testl %r15d, %r15d
je 0x98f5
movl (%rbx), %esi
leaq 0x4(%rbx), %rdx
leaq 0xbe7d(%rip), %rdi      # 0x1554a
xorl %r14d, %r14d
xorl %eax, %eax
callq 0xa127
movq %rbx, %rdi
movl $0x1, %esi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x8d7a
movq %rbx, %rdi
callq 0x90a7
movb $0x1, %r15b
jmp 0x98fb
xorl %r15d, %r15d
testl %eax, %eax
jle 0x98f8
leaq 0x1020(%rbx), %r14
xorl %r12d, %r12d
xorl %ebp, %ebp
movl 0x128(%r14), %eax
testl %eax, %eax
je 0x977d
cmpl $0x1, %eax
jne 0x9800
cmpl $0x0, 0x12c(%r14)
je 0x97ac
cmpl $0x1, %eax
jne 0x9800
cmpl $0x1, 0x12c(%r14)
je 0x9882
cmpl $0x1, %eax
jne 0x9800
cmpl $0x2, 0x12c(%r14)
jne 0x97ed
movl $0x1, %ebp
cmpl $0x0, 0x530a0(%rbx)
je 0x98cd
movq %rbx, %rdi
movq %r14, %rsi
callq 0x9eda
jmp 0x9887
cmpl $0x0, 0x12c(%r14)
je 0x9887
testl %eax, %eax
jne 0x971a
cmpl $0x1, 0x12c(%r14)
jne 0x97bc
cmpl $0x0, 0x148(%r14)
jne 0x98c0
jmp 0x98cd
movq %rbx, %rdi
movq %r14, %rsi
callq 0x9eda
jmp 0x9882
testl %eax, %eax
jne 0x971a
cmpl $0x2, 0x12c(%r14)
je 0x98cd
testl %eax, %eax
jne 0x971a
cmpl $0x3, 0x12c(%r14)
jne 0x971a
jmp 0x98cd
cmpl $0x1, %eax
jne 0x9800
cmpl $0x3, 0x12c(%r14)
je 0x98cd
cmpl $0x2, %eax
jne 0x9859
cmpl $0x0, 0x12c(%r14)
je 0x9877
cmpl $0x2, %eax
jne 0x9859
cmpl $0x1, 0x12c(%r14)
jne 0x983b
cmpl $0x0, 0x148(%r14)
jne 0x9877
cmpl $0x0, 0x530a0(%rbx)
jne 0x9877
movl $0x1, %ebp
jmp 0x98cd
cmpl $0x2, %eax
jne 0x9859
cmpl $0x2, 0x12c(%r14)
je 0x98b2
cmpl $0x2, %eax
jne 0x9859
cmpl $0x3, 0x12c(%r14)
je 0x98b2
cmpl $0x3, %eax
jne 0x9887
cmpl $0x0, 0x12c(%r14)
je 0x9877
cmpl $0x3, %eax
jne 0x9887
cmpl $0x1, 0x12c(%r14)
jne 0x98a3
movq %rbx, %rdi
movq %r14, %rsi
callq 0x9e4d
movl $0x1, %ebp
incq %r12
movslq 0x1018(%rbx), %rax
addq $0x150, %r14            # imm = 0x150
cmpq %rax, %r12
jl 0x970f
jmp 0x98e6
cmpl $0x3, %eax
jne 0x9887
cmpl $0x2, 0x12c(%r14)
jne 0x98d5
movl $0x1, %ebp
cmpl $0x0, 0x530a0(%rbx)
je 0x98cd
movq %rbx, %rdi
movq %r14, %rsi
callq 0x9e4d
jmp 0x9887
movl $0x1, %r15d
jmp 0x9887
cmpl $0x3, %eax
jne 0x9887
cmpl $0x3, 0x12c(%r14)
jne 0x9887
jmp 0x98b2
testl %r15d, %r15d
setne %r14b
testl %ebp, %ebp
setne %r15b
jmp 0x98fb
xorl %r15d, %r15d
xorl %r14d, %r14d
testb %r14b, %r14b
je 0x99a6
movq %rbx, %rdi
callq 0x9516
movl 0x1018(%rbx), %eax
xorl %esi, %esi
testl %eax, %eax
cmovlel %esi, %eax
jle 0x9977
leaq 0x1020(%rbx), %r14
imulq $0x150, %rax, %r12      # imm = 0x150
addq $-0x150, %r12           # imm = 0xFEB0
xorl %r13d, %r13d
leaq 0xbb81(%rip), %r15      # 0x154bb
movq %r14, %rdi
movq %r15, %rsi
callq 0x3060
leaq -0x150(%r13), %rcx
testl %eax, %eax
je 0x9962
addq %r12, %r13
addq $0x150, %r14            # imm = 0x150
testq %r13, %r13
movq %rcx, %r13
jne 0x993a
movq %rbx, %rdx
subq %rcx, %rdx
addq $0xed0, %rdx            # imm = 0xED0
xorl %esi, %esi
testl %eax, %eax
cmoveq %rdx, %rsi
testq %rsi, %rsi
je 0x9984
movq %rbx, %rdi
callq 0x9e4d
leaq 0x130e9(%rip), %rax     # 0x1ca74
cmpl $0x0, (%rax)
je 0x99ab
movl (%rbx), %esi
leaq 0x4(%rbx), %rdx
leaq 0xbbf0(%rip), %rdi      # 0x1558d
xorl %eax, %eax
callq 0xa127
jmp 0x99ab
testb %r15b, %r15b
je 0x99d6
movq %rbx, %rdi
callq 0x9f37
leaq 0x130ba(%rip), %rax     # 0x1ca74
cmpl $0x0, (%rax)
je 0x99d6
movl (%rbx), %esi
addq $0x4, %rbx
leaq 0xbbf2(%rip), %rdi      # 0x155be
movq %rbx, %rdx
xorl %eax, %eax
callq 0xa127
xorl %eax, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq