// File: /workspace/github_c_makefile_star2_O1/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 0x8da2
movl $0x0, (%r15)
leaq 0x4(%r14), %rbx
leaq 0x10(%rsp), %rsi
movq %rbx, %rdi
callq 0x3330
testl %eax, %eax
je 0x8e3f
testl %ebp, %ebp
je 0x8e06
movq %rbx, %rdi
movl $0x1ff, %esi            # imm = 0x1FF
callq 0x30e0
testl %eax, %eax
je 0x8e33
movl (%r14), %ebp
leaq 0xa0(%rsp), %rdi
movl $0x1000, %esi           # imm = 0x1000
callq 0x3250
leaq 0xc5b3(%rip), %rdi      # 0x1539f
movl %ebp, %esi
movq %rbx, %rdx
movq %rax, %rcx
xorl %eax, %eax
callq 0xa054
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
jmp 0x9066
movl (%r14), %ebp
leaq 0xa0(%rsp), %rdi
movl $0x1000, %esi           # imm = 0x1000
callq 0x3250
leaq 0xc5b0(%rip), %rdi      # 0x153d2
movl %ebp, %esi
movq %rbx, %rdx
movq %rax, %rcx
xorl %eax, %eax
callq 0xa054
jmp 0x8e3f
testq %r15, %r15
je 0x8e3f
movl $0x1, (%r15)
leaq 0x10(%rsp), %rsi
movq %rbx, %rdi
callq 0x3330
testl %eax, %eax
jne 0x904a
movl $0xf000, %eax           # imm = 0xF000
andl 0x28(%rsp), %eax
cmpl $0x4000, %eax           # imm = 0x4000
jne 0x904a
movq %rbx, %rdi
callq 0x3170
testq %rax, %rax
je 0x907b
movq %rax, %r15
movl %r12d, 0xc(%rsp)
movq %rax, %rdi
callq 0x33a0
testq %rax, %rax
je 0x8f1c
movq %rax, %r12
leaq 0xa0(%rsp), %r13
leaq 0x10(%rsp), %rbp
addq $0x13, %r12
movq %r13, %rdi
leaq 0xc597(%rip), %rsi      # 0x15447
movq %rbx, %rdx
movq %r12, %rcx
xorl %eax, %eax
callq 0x3540
movq %r13, %rdi
movq %rbp, %rsi
callq 0x3330
testl %eax, %eax
jne 0x9087
movq %r12, %rdi
leaq 0xb134(%rip), %rsi      # 0x1400e
callq 0x32f0
testl %eax, %eax
je 0x8f0c
movq %r12, %rdi
leaq 0xc560(%rip), %rsi      # 0x1544d
callq 0x32f0
testl %eax, %eax
je 0x8f0c
movl 0x28(%rsp), %eax
movl $0xf000, %ecx           # imm = 0xF000
andl %ecx, %eax
cmpl $0x8000, %eax           # imm = 0x8000
jne 0x9087
movq %r15, %rdi
callq 0x33a0
movq %rax, %r12
testq %rax, %rax
jne 0x8ea2
movq %r15, %rdi
callq 0x3280
leaq 0xc55a(%rip), %rsi      # 0x15485
leaq 0xa0(%rsp), %r12
movq %r12, %rdi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x3540
leaq 0xe14a(%rip), %rsi      # 0x17091
movq %r12, %rdi
callq 0x34c0
movq %rax, %r13
leaq 0xd48e(%rip), %rdi      # 0x163e7
movq %rax, %rsi
callq 0x3220
movl %eax, %ebp
movq %r13, %rdi
callq 0x3160
movl %eax, %r15d
movq %r12, %rdi
callq 0x30b0
testq %r13, %r13
je 0x909b
testl %ebp, %ebp
js 0x909b
orl %r15d, %eax
jne 0x909b
leaq 0xc4b0(%rip), %rsi      # 0x15447
leaq 0xc51d(%rip), %rcx      # 0x154bb
xorl %r14d, %r14d
leaq 0xa0(%rsp), %r15
movq %r15, %rdi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x3540
movq %r15, %rdi
callq 0x30b0
cmpl $0x0, 0xc(%rsp)
je 0x9066
movq %rbx, %rdi
callq 0x3170
movq %rax, %r15
movq %rax, %rdi
callq 0x33a0
testq %rax, %rax
je 0x9066
leaq 0xa0(%rsp), %r13
leaq 0x10(%rsp), %rbp
movl $0xf000, %r12d          # imm = 0xF000
xorl %r14d, %r14d
addq $0x13, %rax
movq %r13, %rdi
leaq 0xc43e(%rip), %rsi      # 0x15447
movq %rbx, %rdx
movq %rax, %rcx
xorl %eax, %eax
callq 0x3540
movq %r13, %rdi
movq %rbp, %rsi
callq 0x3330
testl %eax, %eax
jne 0x9066
movl 0x28(%rsp), %eax
andl %r12d, %eax
cmpl $0x8000, %eax           # imm = 0x8000
jne 0x903b
movq %r13, %rdi
callq 0x30b0
movq %r15, %rdi
callq 0x33a0
testq %rax, %rax
jne 0x8ffb
jmp 0x9066
movl (%r14), %edx
leaq 0xc3bc(%rip), %rsi      # 0x15410
movl $0xfffffffb, %edi       # imm = 0xFFFFFFFB
movq %rbx, %rcx
xorl %eax, %eax
callq 0x9f8f
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 0xc3a9(%rip), %rsi      # 0x1542e
jmp 0x9054
movq %r15, %rdi
callq 0x3280
movl (%r14), %edx
leaq 0xc3b7(%rip), %rsi      # 0x15450
jmp 0x9054
movl (%r14), %edx
leaq 0xc3f4(%rip), %rsi      # 0x15499
jmp 0x9054