// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x1100, %rsp           # imm = 0x1100
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movl %edx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
cmpq $0x0, -0x20(%rbp)
je 0xb38a
movq -0x20(%rbp), %rax
movl $0x0, (%rax)
movq -0x10(%rbp), %rdi
addq $0x4, %rdi
leaq -0xb0(%rbp), %rsi
callq 0x3310
cmpl $0x0, %eax
je 0xb47d
cmpl $0x0, -0x18(%rbp)
je 0xb431
movq -0x10(%rbp), %rdi
addq $0x4, %rdi
movl $0x1ff, %esi            # imm = 0x1FF
callq 0x30d0
cmpl $0x0, %eax
je 0xb41e
movq -0x10(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x10e4(%rbp)
movq -0x10(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x10e0(%rbp)
leaq -0x10d0(%rbp), %rdi
movl $0x1000, %esi           # imm = 0x1000
callq 0x3240
movl -0x10e4(%rbp), %esi
movq -0x10e0(%rbp), %rdx
movq %rax, %rcx
leaq 0x10ec2(%rip), %rdi     # 0x1c2cd
movb $0x0, %al
callq 0xce70
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0xb7ea
cmpq $0x0, -0x20(%rbp)
je 0xb42f
movq -0x20(%rbp), %rax
movl $0x1, (%rax)
jmp 0xb47b
movq -0x10(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x10f4(%rbp)
movq -0x10(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x10f0(%rbp)
leaq -0x10d0(%rbp), %rdi
movl $0x1000, %esi           # imm = 0x1000
callq 0x3240
movl -0x10f4(%rbp), %esi
movq -0x10f0(%rbp), %rdx
movq %rax, %rcx
leaq 0x10e8c(%rip), %rdi     # 0x1c300
movb $0x0, %al
callq 0xce70
jmp 0xb47d
movq -0x10(%rbp), %rdi
addq $0x4, %rdi
leaq -0xb0(%rbp), %rsi
callq 0x3310
cmpl $0x0, %eax
jne 0xb4a8
movl -0x98(%rbp), %eax
andl $0xf000, %eax           # imm = 0xF000
cmpl $0x4000, %eax           # imm = 0x4000
je 0xb4d1
movq -0x10(%rbp), %rax
movl (%rax), %edx
movq -0x10(%rbp), %rcx
addq $0x4, %rcx
movl $0xfffffffb, %edi       # imm = 0xFFFFFFFB
leaq 0x10e7c(%rip), %rsi     # 0x1c33e
movb $0x0, %al
callq 0xcd70
movl %eax, -0x4(%rbp)
jmp 0xb7ea
movq -0x10(%rbp), %rdi
addq $0x4, %rdi
callq 0x3160
movq %rax, -0xc0(%rbp)
cmpq $0x0, %rax
jne 0xb514
movq -0x10(%rbp), %rax
movl (%rax), %edx
movq -0x10(%rbp), %rcx
addq $0x4, %rcx
movl $0xfffffffb, %edi       # imm = 0xFFFFFFFB
leaq 0x10e57(%rip), %rsi     # 0x1c35c
movb $0x0, %al
callq 0xcd70
movl %eax, -0x4(%rbp)
jmp 0xb7ea
movl $0x1, -0xc4(%rbp)
xorl %eax, %eax
cmpl $0x0, -0xc4(%rbp)
movb %al, -0x10f5(%rbp)
je 0xb54f
movq -0xc0(%rbp), %rdi
callq 0x3380
movq %rax, -0xb8(%rbp)
cmpq $0x0, %rax
setne %al
movb %al, -0x10f5(%rbp)
movb -0x10f5(%rbp), %al
testb $0x1, %al
jne 0xb55e
jmp 0xb605
leaq -0x10d0(%rbp), %rdi
movq -0x10(%rbp), %rdx
addq $0x4, %rdx
movq -0xb8(%rbp), %rcx
addq $0x13, %rcx
leaq 0x10df6(%rip), %rsi     # 0x1c375
movb $0x0, %al
callq 0x3520
leaq -0x10d0(%rbp), %rdi
leaq -0xb0(%rbp), %rsi
callq 0x3310
cmpl $0x0, %eax
je 0xb5aa
movl $0x0, -0xc4(%rbp)
jmp 0xb600
movq -0xb8(%rbp), %rdi
addq $0x13, %rdi
leaq 0xfa26(%rip), %rsi      # 0x1afe2
callq 0x32d0
cmpl $0x0, %eax
je 0xb5fe
movq -0xb8(%rbp), %rdi
addq $0x13, %rdi
leaq 0x10da3(%rip), %rsi     # 0x1c37b
callq 0x32d0
cmpl $0x0, %eax
je 0xb5fe
movl -0x98(%rbp), %eax
andl $0xf000, %eax           # imm = 0xF000
cmpl $0x8000, %eax           # imm = 0x8000
je 0xb5fe
movl $0x0, -0xc4(%rbp)
jmp 0xb600
jmp 0xb51e
movq -0xc0(%rbp), %rdi
callq 0x3270
cmpl $0x0, -0xc4(%rbp)
jne 0xb643
movq -0x10(%rbp), %rax
movl (%rax), %edx
movq -0x10(%rbp), %rcx
addq $0x4, %rcx
movl $0xfffffffb, %edi       # imm = 0xFFFFFFFB
leaq 0x10d4a(%rip), %rsi     # 0x1c37e
movb $0x0, %al
callq 0xcd70
movl %eax, -0x4(%rbp)
jmp 0xb7ea
leaq -0x10d0(%rbp), %rdi
movq -0x10(%rbp), %rdx
addq $0x4, %rdx
leaq 0x10d5a(%rip), %rsi     # 0x1c3b3
movb $0x0, %al
callq 0x3520
leaq -0x10d0(%rbp), %rdi
leaq 0x129cb(%rip), %rsi     # 0x1e039
callq 0x34a0
movq %rax, -0x10d8(%rbp)
cmpq $0x0, -0x10d8(%rbp)
jne 0xb68e
movl $0x0, -0xc4(%rbp)
movq -0x10d8(%rbp), %rsi
leaq 0x11ccb(%rip), %rdi     # 0x1d367
callq 0x3210
cmpl $0x0, %eax
jge 0xb6b0
movl $0x0, -0xc4(%rbp)
movq -0x10d8(%rbp), %rdi
callq 0x3150
cmpl $0x0, %eax
je 0xb6cb
movl $0x0, -0xc4(%rbp)
leaq -0x10d0(%rbp), %rdi
callq 0x30a0
cmpl $0x0, %eax
je 0xb6e6
movl $0x0, -0xc4(%rbp)
cmpl $0x0, -0xc4(%rbp)
jne 0xb718
movq -0x10(%rbp), %rax
movl (%rax), %edx
movq -0x10(%rbp), %rcx
addq $0x4, %rcx
movl $0xfffffffb, %edi       # imm = 0xFFFFFFFB
leaq 0x10cbe(%rip), %rsi     # 0x1c3c7
movb $0x0, %al
callq 0xcd70
movl %eax, -0x4(%rbp)
jmp 0xb7ea
leaq -0x10d0(%rbp), %rdi
movq -0x10(%rbp), %rdx
addq $0x4, %rdx
leaq 0x10c47(%rip), %rsi     # 0x1c375
leaq 0x10cb4(%rip), %rcx     # 0x1c3e9
movb $0x0, %al
callq 0x3520
leaq -0x10d0(%rbp), %rdi
callq 0x30a0
cmpl $0x0, -0x14(%rbp)
je 0xb7e3
movq -0x10(%rbp), %rdi
addq $0x4, %rdi
callq 0x3160
movq %rax, -0xc0(%rbp)
movq -0xc0(%rbp), %rdi
callq 0x3380
movq %rax, -0xb8(%rbp)
cmpq $0x0, %rax
je 0xb7e1
leaq -0x10d0(%rbp), %rdi
movq -0x10(%rbp), %rdx
addq $0x4, %rdx
movq -0xb8(%rbp), %rcx
addq $0x13, %rcx
leaq 0x10bd5(%rip), %rsi     # 0x1c375
movb $0x0, %al
callq 0x3520
leaq -0x10d0(%rbp), %rdi
leaq -0xb0(%rbp), %rsi
callq 0x3310
cmpl $0x0, %eax
je 0xb7c1
jmp 0xb7e1
movl -0x98(%rbp), %eax
andl $0xf000, %eax           # imm = 0xF000
cmpl $0x8000, %eax           # imm = 0x8000
jne 0xb7df
leaq -0x10d0(%rbp), %rdi
callq 0x30a0
jmp 0xb766
jmp 0xb7e3
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x1100, %rsp           # imm = 0x1100
popq %rbp
retq
nopw %cs:(%rax,%rax)