// File: /workspace/github_c_makefile_star2_O3/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 0x97f2
movq %rbx, %rdi
callq 0x9afd
leaq 0x13541(%rip), %rax     # 0x1ca74
movl $0x1, %esi
cmpl $0x0, (%rax)
jne 0x954b
leaq 0x13520(%rip), %rax     # 0x1ca64
xorl %esi, %esi
cmpl $0x0, (%rax)
je 0x9557
leaq 0x1010(%rbx), %rdi
callq 0x9d3b
movq 0x1008(%rbx), %rax
cmpl $0x0, 0x4(%rax)
movl 0x1018(%rbx), %eax
je 0x95d3
testl %eax, %eax
jle 0x97b0
imulq $0x150, %rax, %rax      # imm = 0x150
xorl %ecx, %ecx
movl $0x1, %edx
xorl %esi, %esi
cmpl $0x0, 0x114c(%rbx,%rcx)
cmovnel %edx, %esi
addq $0x150, %rcx            # imm = 0x150
cmpq %rcx, %rax
jne 0x9582
testl %esi, %esi
je 0x97b0
movl (%rbx), %esi
leaq 0x4(%rbx), %rdx
leaq 0xc009(%rip), %rdi      # 0x155b7
xorl %eax, %eax
callq 0xa10f
movq %rbx, %rdi
movl $0x1, %esi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x8c78
movq %rbx, %rdi
callq 0x8f9b
jmp 0x97b6
testl %eax, %eax
jle 0x97b0
leaq 0x1020(%rbx), %r14
xorl %r15d, %r15d
leaq 0xbce4(%rip), %r12      # 0x152d0
xorl %r13d, %r13d
xorl %ebp, %ebp
movl 0x128(%r14), %eax
cmpq $0x3, %rax
ja 0x971a
movslq (%r12,%rax,4), %rax
addq %r12, %rax
jmpq *%rax
movl 0x12c(%r14), %eax
cmpq $0x3, %rax
ja 0x971a
leaq 0xbced(%rip), %rcx      # 0x15310
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
cmpl $0x0, 0x148(%r14)
jne 0x96e8
jmp 0x9714
movl 0x12c(%r14), %eax
cmpq $0x3, %rax
ja 0x971a
leaq 0xbc99(%rip), %rcx      # 0x152f0
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
cmpl $0x0, 0x148(%r14)
jne 0x969e
cmpl $0x0, 0x530a0(%rbx)
jne 0x969e
movl $0x1, %ebp
jmp 0x9714
movl 0x12c(%r14), %eax
cmpq $0x3, %rax
ja 0x971a
leaq 0xbc4b(%rip), %rcx      # 0x152e0
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq %rbx, %rdi
movq %r14, %rsi
callq 0x9e34
jmp 0x96d3
movl 0x12c(%r14), %eax
cmpq $0x3, %rax
ja 0x971a
leaq 0xbc41(%rip), %rdx      # 0x15300
movslq (%rdx,%rax,4), %rcx
addq %rdx, %rcx
jmpq *%rcx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x9ec1
movl $0x1, %ebp
jmp 0x971a
movl $0x1, %ebp
cmpl $0x0, 0x530a0(%rbx)
je 0x9714
movq %rbx, %rdi
movq %r14, %rsi
callq 0x9e34
jmp 0x971a
movl %eax, %ebp
jmp 0x971a
movl $0x1, %ebp
cmpl $0x0, 0x530a0(%rbx)
je 0x9714
movq %rbx, %rdi
movq %r14, %rsi
callq 0x9ec1
jmp 0x971a
movl $0x1, %r13d
incq %r15
movslq 0x1018(%rbx), %rax
addq $0x150, %r14            # imm = 0x150
cmpq %rax, %r15
jl 0x95f1
testl %r13d, %r13d
je 0x97b2
movq %rbx, %rdi
callq 0x940c
movl 0x1018(%rbx), %eax
xorl %r12d, %r12d
testl %eax, %eax
cmovgl %eax, %r12d
leaq 0xed0(%rbx), %rdi
incq %r12
leaq 0xbdc7(%rip), %r14      # 0x15528
decq %r12
je 0x978e
leaq 0x150(%rdi), %r15
addq $0x150, %rdi            # imm = 0x150
movq %r14, %rsi
callq 0x3060
movq %r15, %rdi
testl %eax, %eax
jne 0x9761
movq %rbx, %rdi
movq %r15, %rsi
callq 0x9e34
leaq 0x132cf(%rip), %rax     # 0x1ca64
cmpl $0x0, (%rax)
je 0x97b6
movl (%rbx), %esi
leaq 0x4(%rbx), %rdx
leaq 0xbe53(%rip), %rdi      # 0x155fa
xorl %eax, %eax
callq 0xa10f
jmp 0x97b6
xorl %ebp, %ebp
testl %ebp, %ebp
je 0x97e1
movq %rbx, %rdi
callq 0x9f1e
leaq 0x1329f(%rip), %rax     # 0x1ca64
cmpl $0x0, (%rax)
je 0x97e1
movl (%rbx), %esi
addq $0x4, %rbx
leaq 0xbe54(%rip), %rdi      # 0x1562b
movq %rbx, %rdx
xorl %eax, %eax
callq 0xa10f
xorl %eax, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq