// File: /workspace/github_c_makefile_star2_O2/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, %r14d
movq %rdi, %r12
testq %rcx, %rcx
je 0x8851
andl $0x0, (%r15)
leaq 0x4(%r12), %rbx
leaq 0x10(%rsp), %rsi
movq %rbx, %rdi
callq 0x3320
testl %eax, %eax
je 0x88ee
testl %ebp, %ebp
je 0x88b4
movq %rbx, %rdi
movl $0x1ff, %esi            # imm = 0x1FF
callq 0x30d0
testl %eax, %eax
je 0x88e2
movl (%r12), %ebp
leaq 0xa0(%rsp), %rdi
movl $0x1000, %esi           # imm = 0x1000
callq 0x3240
leaq 0xbb4a(%rip), %rdi      # 0x143e7
movl %ebp, %esi
movq %rbx, %rdx
movq %rax, %rcx
xorl %eax, %eax
callq 0x98f2
pushq $-0x1
popq %rbp
jmp 0x89e5
movl (%r12), %ebp
leaq 0xa0(%rsp), %rdi
movl $0x1000, %esi           # imm = 0x1000
callq 0x3240
leaq 0xbb49(%rip), %rdi      # 0x1441a
movl %ebp, %esi
movq %rbx, %rdx
movq %rax, %rcx
xorl %eax, %eax
callq 0x98f2
jmp 0x88ee
testq %r15, %r15
je 0x88ee
movl $0x1, (%r15)
leaq 0x10(%rsp), %rsi
movq %rbx, %rdi
callq 0x3320
testl %eax, %eax
jne 0x89cb
movl $0xf000, %eax           # imm = 0xF000
andl 0x28(%rsp), %eax
cmpl $0x4000, %eax           # imm = 0x4000
jne 0x89cb
movq %rbx, %rdi
callq 0x3160
testq %rax, %rax
je 0x89f9
movq %rax, %r15
movq %r12, 0x8(%rsp)
movl %r14d, 0x4(%rsp)
pushq $0x1
popq %r14
leaq 0xa0(%rsp), %r13
testl %r14d, %r14d
je 0x8a06
movq %r15, %rdi
callq 0x3390
testq %rax, %rax
je 0x8a1e
movq %rax, %r12
leaq 0x13(%rax), %rbp
xorl %r14d, %r14d
movq %r13, %rdi
leaq 0xbb20(%rip), %rsi      # 0x1448f
movq %rbx, %rdx
movq %rbp, %rcx
xorl %eax, %eax
callq 0x3530
movq %r13, %rdi
leaq 0x10(%rsp), %rsi
callq 0x3320
testl %eax, %eax
jne 0x8941
cmpb $0x2e, (%rbp)
jne 0x89af
cmpb $0x0, 0x14(%r12)
pushq $0x1
popq %r14
je 0x8941
cmpb $0x2e, 0x14(%r12)
jne 0x89af
cmpb $0x0, 0x15(%r12)
je 0x8941
movl 0x28(%rsp), %eax
movl $0xf000, %ecx           # imm = 0xF000
andl %ecx, %eax
xorl %r14d, %r14d
cmpl $0x8000, %eax           # imm = 0x8000
sete %r14b
jmp 0x8941
movl (%r12), %edx
leaq 0xba82(%rip), %rsi      # 0x14458
pushq $-0x5
popq %rdi
movq %rbx, %rcx
xorl %eax, %eax
callq 0x982c
movl %eax, %ebp
movl %ebp, %eax
addq $0x10a8, %rsp           # imm = 0x10A8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl (%r12), %edx
leaq 0xba72(%rip), %rsi      # 0x14476
jmp 0x89d6
movq %r15, %rdi
callq 0x3270
movq 0x8(%rsp), %rax
movl (%rax), %edx
leaq 0xba79(%rip), %rsi      # 0x14495
jmp 0x89d6
movq %r15, %rdi
callq 0x3270
leaq 0xba9d(%rip), %rsi      # 0x144ca
leaq 0xa0(%rsp), %r12
movq %r12, %rdi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x3530
leaq 0xd690(%rip), %rsi      # 0x160d9
movq %r12, %rdi
callq 0x34b0
movq %rax, %r13
leaq 0xc9d7(%rip), %rdi      # 0x15432
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 0x8b3b
testl %ebp, %ebp
js 0x8b3b
orl %r15d, %eax
jne 0x8b3b
leaq 0xb9f6(%rip), %rsi      # 0x1448f
leaq 0xba60(%rip), %rcx      # 0x14500
leaq 0xa0(%rsp), %r14
movq %r14, %rdi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x3530
movq %r14, %rdi
callq 0x30b0
cmpl $0x0, 0x4(%rsp)
je 0x8b4e
movq %rbx, %rdi
callq 0x3160
movq %rax, %r14
leaq 0xa0(%rsp), %r12
leaq 0x10(%rsp), %r13
movl $0xf000, %r15d          # imm = 0xF000
xorl %ebp, %ebp
movq %r14, %rdi
callq 0x3390
testq %rax, %rax
je 0x8b4e
addq $0x13, %rax
movq %r12, %rdi
leaq 0xb98c(%rip), %rsi      # 0x1448f
movq %rbx, %rdx
movq %rax, %rcx
xorl %eax, %eax
callq 0x3530
movq %r12, %rdi
movq %r13, %rsi
callq 0x3320
testl %eax, %eax
jne 0x89e5
movl 0x28(%rsp), %eax
andl %r15d, %eax
cmpl $0x8000, %eax           # imm = 0x8000
jne 0x8ae8
movq %r12, %rdi
callq 0x30b0
jmp 0x8ae8
movq 0x8(%rsp), %rax
movl (%rax), %edx
leaq 0xb995(%rip), %rsi      # 0x144de
jmp 0x89d6
xorl %ebp, %ebp
jmp 0x89e5