// File: /workspace/github_c_makefile_star2_O3/j-hoppe[P]tu58fs/hostdir.c
// Function: hostdir_prepare
int hostdir_prepare(hostdir_t *_this, int wipe, int allowcreate, int *created) {
	struct stat sb;
	struct dirent *dp;
	DIR *dfd;
	int ok;
	char pathbuff[4096];
	FILE *f;

	if (created)
		*created = 0;
	if (stat(_this->path, &sb)) {
		// not found
		if (allowcreate) {
			if (mkdir(_this->path, 0777)) { // open rw for all
				error("Unit %d: Creation of directory \"%s\" failed in \"%s\"", _this->unit, _this->path,
						getcwd(pathbuff, sizeof(pathbuff)));
				return -1;
			}
			if (created)
				*created = 1;
		} else {
			error("Unit %d: Directory \"%s\" not found in \"%s\", creation forbidden", _this->unit, _this->path,
					getcwd(pathbuff, sizeof(pathbuff)));
		}
	}
	// again
	if (stat(_this->path, &sb) || !S_ISDIR(sb.st_mode))
		return error_set(ERROR_HOSTDIR, "Unit %d: \"%s\" is no directory", _this->unit, _this->path);

	// has it subdirs? iterate through
	if ((dfd = opendir(_this->path)) == NULL)
		return error_set(ERROR_HOSTDIR, "Unit %d: Can't open \"%s\"", _this->unit, _this->path);

	ok = 1;
	while (ok && (dp = readdir(dfd)) != NULL) {
		sprintf(pathbuff, "%s/%s", _this->path, dp->d_name);
		if (stat(pathbuff, &sb))
			ok = 0; // can not access something inside?
		else if (strcmp(dp->d_name, ".") && strcmp(dp->d_name, "..") && !S_ISREG(sb.st_mode))
			// only regular files and . and .. allowed
			ok = 0;
	}
	closedir(dfd);
	if (!ok)
		return error_set(ERROR_HOSTDIR, "Unit %d: Dir \"%s\" contains subdirs or strange stuff\n", _this->unit,
				_this->path);

	// can I write to it?
	sprintf(pathbuff, "%s/_tu58_file_test_", _this->path);
	f = fopen(pathbuff, "w");
	if (!f)
		ok = 0;
	if (fputs("test", f) < 0)
		ok = 0;
	if (fclose(f))
		ok = 0;
	if (remove(pathbuff))
		ok = 0;
	if (!ok)
		return error_set(ERROR_HOSTDIR, "Unit %d: Can't work with dir \"%s\"", _this->unit, _this->path);

	sprintf(pathbuff, "%s/%s", _this->path, "$VOLUM.INF");
	remove(pathbuff);

	// delete content
	if (wipe) {
		dfd = opendir(_this->path); // error checking done, compact code
		while ((dp = readdir(dfd))) {
			sprintf(pathbuff, "%s/%s", _this->path, dp->d_name);
			// beware of . and ..
			if (stat(pathbuff, &sb))
				break;
			if (S_ISREG(sb.st_mode))
				remove(pathbuff);
		}
	}

	return ERROR_OK;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x10a8, %rsp           # imm = 0x10A8
movq %rcx, %r15
movl %edx, %ebp
movl %esi, %r12d
movq %rdi, %r14
testq %rcx, %rcx
je 0x8ca0
movl $0x0, (%r15)
leaq 0x4(%r14), %rbx
leaq 0x10(%rsp), %rsi
movq %rbx, %rdi
callq 0x3320
testl %eax, %eax
je 0x8d3d
testl %ebp, %ebp
je 0x8d04
movq %rbx, %rdi
movl $0x1ff, %esi            # imm = 0x1FF
callq 0x30d0
testl %eax, %eax
je 0x8d31
movl (%r14), %ebp
leaq 0xa0(%rsp), %rdi
movl $0x1000, %esi           # imm = 0x1000
callq 0x3240
leaq 0xc725(%rip), %rdi      # 0x1540f
movl %ebp, %esi
movq %rbx, %rdx
movq %rax, %rcx
xorl %eax, %eax
callq 0xa03c
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
jmp 0x8f5a
movl (%r14), %ebp
leaq 0xa0(%rsp), %rdi
movl $0x1000, %esi           # imm = 0x1000
callq 0x3240
leaq 0xc722(%rip), %rdi      # 0x15442
movl %ebp, %esi
movq %rbx, %rdx
movq %rax, %rcx
xorl %eax, %eax
callq 0xa03c
jmp 0x8d3d
testq %r15, %r15
je 0x8d3d
movl $0x1, (%r15)
leaq 0x10(%rsp), %rsi
movq %rbx, %rdi
callq 0x3320
testl %eax, %eax
jne 0x8f3e
movl $0xf000, %eax           # imm = 0xF000
andl 0x28(%rsp), %eax
cmpl $0x4000, %eax           # imm = 0x4000
jne 0x8f3e
movq %rbx, %rdi
callq 0x3160
testq %rax, %rax
je 0x8f6f
movq %rax, %r15
movl %r12d, 0xc(%rsp)
movq %rax, %rdi
callq 0x3390
testq %rax, %rax
je 0x8e10
movq %rax, %r12
leaq 0xa0(%rsp), %r13
leaq 0x13(%r12), %rbp
movq %r13, %rdi
leaq 0xc70d(%rip), %rsi      # 0x154b7
movq %rbx, %rdx
movq %rbp, %rcx
xorl %eax, %eax
callq 0x3530
movq %r13, %rdi
leaq 0x10(%rsp), %rsi
callq 0x3320
testl %eax, %eax
jne 0x8f7b
cmpb $0x2e, (%rbp)
jne 0x8dea
cmpb $0x0, 0x14(%r12)
je 0x8e00
cmpb $0x2e, 0x14(%r12)
jne 0x8dea
cmpb $0x0, 0x15(%r12)
je 0x8e00
movl 0x28(%rsp), %eax
movl $0xf000, %ecx           # imm = 0xF000
andl %ecx, %eax
cmpl $0x8000, %eax           # imm = 0x8000
jne 0x8f7b
movq %r15, %rdi
callq 0x3390
movq %rax, %r12
testq %rax, %rax
jne 0x8d9b
movq %r15, %rdi
callq 0x3270
leaq 0xc6d3(%rip), %rsi      # 0x154f2
leaq 0xa0(%rsp), %r12
movq %r12, %rdi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x3530
leaq 0xe2ee(%rip), %rsi      # 0x17129
movq %r12, %rdi
callq 0x34b0
movq %rax, %r13
leaq 0xd632(%rip), %rdi      # 0x1647f
movq %rax, %rsi
callq 0x3210
movl %eax, %ebp
movq %r13, %rdi
callq 0x3150
movl %eax, %r15d
movq %r12, %rdi
callq 0x30b0
testq %r13, %r13
je 0x8f8f
testl %ebp, %ebp
js 0x8f8f
orl %r15d, %eax
jne 0x8f8f
leaq 0xc62c(%rip), %rsi      # 0x154b7
leaq 0xc696(%rip), %rcx      # 0x15528
xorl %r14d, %r14d
leaq 0xa0(%rsp), %r15
movq %r15, %rdi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x3530
movq %r15, %rdi
callq 0x30b0
cmpl $0x0, 0xc(%rsp)
je 0x8f5a
movq %rbx, %rdi
callq 0x3160
movq %rax, %r15
movq %rax, %rdi
callq 0x3390
testq %rax, %rax
je 0x8f5a
leaq 0xa0(%rsp), %r13
leaq 0x10(%rsp), %rbp
movl $0xf000, %r12d          # imm = 0xF000
xorl %r14d, %r14d
addq $0x13, %rax
movq %r13, %rdi
leaq 0xc5ba(%rip), %rsi      # 0x154b7
movq %rbx, %rdx
movq %rax, %rcx
xorl %eax, %eax
callq 0x3530
movq %r13, %rdi
movq %rbp, %rsi
callq 0x3320
testl %eax, %eax
jne 0x8f5a
movl 0x28(%rsp), %eax
andl %r12d, %eax
cmpl $0x8000, %eax           # imm = 0x8000
jne 0x8f2f
movq %r13, %rdi
callq 0x30b0
movq %r15, %rdi
callq 0x3390
testq %rax, %rax
jne 0x8eef
jmp 0x8f5a
movl (%r14), %edx
leaq 0xc538(%rip), %rsi      # 0x15480
movl $0xfffffffb, %edi       # imm = 0xFFFFFFFB
movq %rbx, %rcx
xorl %eax, %eax
callq 0x9f73
movl %eax, %r14d
movl %r14d, %eax
addq $0x10a8, %rsp           # imm = 0x10A8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl (%r14), %edx
leaq 0xc525(%rip), %rsi      # 0x1549e
jmp 0x8f48
movq %r15, %rdi
callq 0x3270
movl (%r14), %edx
leaq 0xc530(%rip), %rsi      # 0x154bd
jmp 0x8f48
movl (%r14), %edx
leaq 0xc56d(%rip), %rsi      # 0x15506
jmp 0x8f48