// File: /workspace/github_c_makefile_star2_O2/albertito[P]libjio/libjio/check.c
// Function: jfsck
enum jfsck_return jfsck(const char *name, const char *jdir,
		struct jfsck_result *res, unsigned int flags)
{
	int tfd, rv, i, ret;
	unsigned int maxtid;
	char jlockfile[PATH_MAX], tname[PATH_MAX], brokenname[PATH_MAX];
	struct stat sinfo;
	struct jfs fs;
	struct jtrans *curts;
	struct operation *tmpop;
	DIR *dir;
	struct dirent *dent;
	unsigned char *map;
	off_t filelen, lr;

	tfd = -1;
	filelen = 0;
	dir = NULL;
	fs.fd = -1;
	fs.jfd = -1;
	fs.jdir = NULL;
	fs.jdirfd = -1;
	fs.jmap = MAP_FAILED;
	map = NULL;
	ret = 0;

	res->total = 0;
	res->invalid = 0;
	res->in_progress = 0;
	res->broken = 0;
	res->corrupt = 0;
	res->reapplied = 0;

	fs.fd = open(name, O_RDWR | O_SYNC);
	if (fs.fd < 0) {
		ret = J_EIO;
		if (errno == ENOENT)
			ret = J_ENOENT;
		goto exit;
	}

	fs.name = (char *) name;

	/* Locking the whole file protect us from concurrent runs, but it's
	 * not to be trusted nor assumed (lingering transactions break it): it
	 * just helps prevent some accidents. */
	lr = plockf(fs.fd, F_LOCKW, 0, 0);
	if (lr == -1) {
		/* In the future, we may want to differentiate this case from
		 * a normal I/O error. */
		ret = J_EIO;
		goto exit;
	}

	if (jdir == NULL) {
		fs.jdir = (char *) malloc(PATH_MAX);
		if (fs.jdir == NULL) {
			ret = J_ENOMEM;
			goto exit;
		}

		if (!get_jdir(name, fs.jdir)) {
			ret = J_ENOMEM;
			goto exit;
		}
	} else {
		fs.jdir = strdup(jdir);
		if (fs.jdir == NULL) {
			ret = J_ENOMEM;
			goto exit;
		}
	}

	rv = lstat(fs.jdir, &sinfo);
	if (rv < 0) {
		ret = J_EIO;
		if (errno == ENOENT)
			ret = J_ENOJOURNAL;
		goto exit;
	}
	if (!S_ISDIR(sinfo.st_mode)) {
		ret = J_ENOJOURNAL;
		goto exit;
	}

	fs.jdirfd = open(fs.jdir, O_RDONLY);
	if (fs.jdirfd < 0) {
		ret = J_EIO;
		if (errno == ENOENT)
			ret = J_ENOJOURNAL;
		goto exit;
	}

	/* open the lock file, which is only used to complete the jfs
	 * structure */
	snprintf(jlockfile, PATH_MAX, "%s/%s", fs.jdir, "lock");
	rv = open(jlockfile, O_RDWR | O_CREAT, 0600);
	if (rv < 0) {
		ret = J_EIO;
		if (errno == ENOENT)
			ret = J_ENOJOURNAL;
		goto exit;
	}
	fs.jfd = rv;

	fs.jmap = (unsigned int *) mmap(NULL, sizeof(unsigned int),
			PROT_READ | PROT_WRITE, MAP_SHARED, fs.jfd, 0);
	if (fs.jmap == MAP_FAILED) {
		ret = J_EIO;
		goto exit;
	}

	dir = opendir(fs.jdir);
	if (dir == NULL) {
		ret = J_EIO;
		if (errno == ENOENT)
			ret = J_ENOJOURNAL;
		goto exit;
	}

	/* find the greatest transaction number by looking into the journal
	 * directory */
	maxtid = 0;
	for (errno = 0, dent = readdir(dir); dent != NULL;
			errno = 0, dent = readdir(dir)) {
		/* see if the file is named like a transaction, ignore
		 * otherwise; as transactions are named as numbers > 0, a
		 * simple atoi() is enough testing */
		rv = atoi(dent->d_name);
		if (rv <= 0)
			continue;
		if (rv > maxtid)
			maxtid = rv;
	}
	if (errno) {
		ret = J_EIO;
		goto exit;
	}

	/* rewrite the lockfile, writing the new maxtid on it, so that when we
	 * rollback a transaction it doesn't step over existing ones */
	rv = spwrite(fs.jfd, &maxtid, sizeof(maxtid), 0);
	if (rv != sizeof(maxtid)) {
		ret = J_ENOMEM;
		goto exit;
	}

	/* remove the broken mark so we can call jtrans_commit() */
	snprintf(brokenname, PATH_MAX, "%s/broken", fs.jdir);
	rv = access(brokenname, F_OK);
	if (rv == 0) {
		if (unlink(brokenname) != 0) {
			ret = J_EIO;
			goto exit;
		}
	} else if (errno != ENOENT) {
		ret = J_EIO;
		goto exit;
	}

	/* verify (and possibly fix) all the transactions */
	for (i = 1; i <= maxtid; i++) {
		curts = jtrans_new(&fs, 0);
		if (curts == NULL) {
			ret = J_ENOMEM;
			goto exit;
		}

		curts->id = i;

		/* open the transaction file, using i as its name, so we are
		 * really looping in order (recovering transaction in a
		 * different order as they were applied would result in
		 * corruption) */
		get_jtfile(&fs, i, tname);
		tfd = open(tname, O_RDWR | O_SYNC, 0600);
		if (tfd < 0) {
			if (errno == ENOENT) {
				res->invalid++;
				goto nounlink_loop;
			} else {
				ret = J_EIO;
				goto exit;
			}
		}

		/* try to lock the transaction file, if it's locked then it is
		 * currently being used so we skip it */
		lr = plockf(tfd, F_TLOCKW, 0, 0);
		if (lr == -1) {
			res->in_progress++;
			goto loop;
		}

		filelen = lseek(tfd, 0, SEEK_END);
		if (filelen == 0) {
			res->broken++;
			goto loop;
		} else if (filelen < 0) {
			ret = J_EIO;
			goto exit;
		}

		/* no overflow problems because we know the transaction size
		 * is limited to SSIZE_MAX */
		map = mmap((void *) 0, filelen, PROT_READ, MAP_SHARED, tfd, 0);
		if (map == MAP_FAILED) {
			map = NULL;
			ret = J_EIO;
			goto exit;
		}

		rv = fill_trans(map, filelen, curts);
		if (rv == -1) {
			res->broken++;
			goto loop;
		} else if (rv == -2) {
			res->corrupt++;
			goto loop;
		}

		/* remove flags from the transaction, so we don't have issues
		 * re-committing */
		curts->flags = 0;

		rv = jtrans_commit(curts);

		if (rv < 0) {
			ret = J_EIO;
			goto exit;
		}
		res->reapplied++;

loop:
		if (unlink(tname) != 0) {
			ret = J_EIO;
			goto exit;
		}

nounlink_loop:
		if (tfd >= 0) {
			close(tfd);
			tfd = -1;
		}
		if (map != NULL)
			munmap(map, filelen);

		while (curts->op != NULL) {
			tmpop = curts->op->next;
			if (curts->op->pdata)
				free(curts->op->pdata);
			free(curts->op);
			curts->op = tmpop;
		}
		pthread_mutex_destroy(&(curts->lock));
		free(curts);

		res->total++;
	}

	if (flags & J_CLEANUP) {
		if (jfsck_cleanup(name, fs.jdir) < 0) {
			ret = J_ECLEANUP;
		}
	}

exit:
	if (fs.fd >= 0)
		close(fs.fd);
	if (fs.jfd >= 0)
		close(fs.jfd);
	if (fs.jdirfd >= 0)
		close(fs.jdirfd);
	if (fs.jdir)
		free(fs.jdir);
	if (dir != NULL)
		closedir(dir);
	if (fs.jmap != MAP_FAILED)
		munmap(fs.jmap, sizeof(unsigned int));

	return ret;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x6188, %rsp           # imm = 0x6188
movl %ecx, %ebp
movq %rdx, %r15
movq %rsi, %r13
andq $0x0, 0x68(%rsp)
movq %rdi, %r12
orq $-0x1, 0x70(%rsp)
orq $-0x1, 0x78(%rsp)
xorps %xmm0, %xmm0
movups %xmm0, (%rdx)
andq $0x0, 0x10(%rdx)
movl $0x101002, %esi         # imm = 0x101002
xorl %eax, %eax
callq 0x24b0
movl %eax, 0x58(%rsp)
testl %eax, %eax
js 0x3830
movq %r12, 0x60(%rsp)
xorl %r14d, %r14d
movl %eax, %edi
movl $0x110, %esi            # imm = 0x110
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x2480
pushq $-0x5
popq %rbx
cmpq $-0x1, %rax
je 0x39ba
pushq $-0x3
popq %rbx
testq %r13, %r13
je 0x3847
movq %r13, %rdi
callq 0x2570
movq %rax, %r14
movq %rax, 0x68(%rsp)
testq %rax, %rax
jne 0x3875
jmp 0x39b7
callq 0x2070
xorl %ebx, %ebx
cmpl $0x2, (%rax)
sete %bl
negl %ebx
orl $-0x5, %ebx
jmp 0x39b7
movl $0x1000, %edi           # imm = 0x1000
callq 0x2390
movq %rax, 0x68(%rsp)
testq %rax, %rax
je 0x39b7
movq %rax, %r14
movq %r12, %rdi
movq %rax, %rsi
callq 0x2450
testl %eax, %eax
je 0x39b7
leaq 0xf0(%rsp), %rsi
movq %r14, %rdi
callq 0x2260
testl %eax, %eax
js 0x399f
movl $0xf000, %eax           # imm = 0xF000
andl 0x108(%rsp), %eax
cmpl $0x4000, %eax           # imm = 0x4000
jne 0x39b4
movq 0x68(%rsp), %r14
movq %r14, %rdi
xorl %esi, %esi
xorl %eax, %eax
callq 0x24b0
movl %eax, 0x70(%rsp)
testl %eax, %eax
js 0x399f
leaq 0x1b70(%rip), %rdx      # 0x5438
leaq 0x1b64(%rip), %r8       # 0x5433
leaq 0x2180(%rsp), %r12
movl $0x1000, %esi           # imm = 0x1000
movq %r12, %rdi
movq %r14, %rcx
xorl %eax, %eax
callq 0x21b0
pushq $0x42
popq %rsi
movq %r12, %rdi
movl $0x180, %edx            # imm = 0x180
xorl %eax, %eax
callq 0x24b0
testl %eax, %eax
js 0x399f
movl %eax, 0x74(%rsp)
xorl %r14d, %r14d
pushq $0x4
popq %rsi
pushq $0x3
popq %rdx
pushq $0x1
popq %rcx
xorl %edi, %edi
movl %eax, %r8d
xorl %r9d, %r9d
callq 0x2160
movq %rax, 0x78(%rsp)
cmpq $-0x1, %rax
je 0x3a26
movq 0x68(%rsp), %rdi
callq 0x2120
testq %rax, %rax
je 0x399f
movq %rax, %r14
leaq 0x8(%r15), %r13
leaq 0xc(%r15), %rax
movq %rax, 0x38(%rsp)
leaq 0x10(%r15), %rax
movq %rax, 0x50(%rsp)
leaq 0x14(%r15), %rax
movq %rax, 0x48(%rsp)
andl $0x0, 0xc(%rsp)
callq 0x2070
movq %rax, %r12
andl $0x0, (%r12)
movq %r14, %rdi
callq 0x2350
testq %rax, %rax
je 0x3a1f
addq $0x13, %rax
movq %rax, %rdi
callq 0x24f0
testl %eax, %eax
jle 0x396d
cmpl 0xc(%rsp), %eax
jbe 0x396d
movl %eax, 0xc(%rsp)
jmp 0x396d
callq 0x2070
xorl %ecx, %ecx
cmpl $0x2, (%rax)
sete %cl
leal (%rcx,%rcx,2), %ebx
addl $-0x5, %ebx
jmp 0x39b7
pushq $-0x2
popq %rbx
xorl %r14d, %r14d
movl 0x58(%rsp), %edi
testl %edi, %edi
js 0x39c7
callq 0x2220
movl 0x74(%rsp), %edi
testl %edi, %edi
js 0x39d4
callq 0x2220
movl 0x70(%rsp), %edi
testl %edi, %edi
js 0x39e1
callq 0x2220
movq 0x68(%rsp), %rdi
callq 0x2040
testq %r14, %r14
je 0x39f8
movq %r14, %rdi
callq 0x2240
movq 0x78(%rsp), %rdi
cmpq $-0x1, %rdi
je 0x3a0b
pushq $0x4
popq %rsi
callq 0x23e0
movl %ebx, %eax
addq $0x6188, %rsp           # imm = 0x6188
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpl $0x0, (%r12)
je 0x3a2b
pushq $-0x5
popq %rbx
jmp 0x39ba
movl 0x74(%rsp), %edi
leaq 0xc(%rsp), %rsi
pushq $0x4
popq %rdx
xorl %ecx, %ecx
callq 0x2440
cmpl $0x4, %eax
jne 0x39ba
movq %r13, 0x40(%rsp)
movq 0x68(%rsp), %rcx
leaq 0x19e6(%rip), %rdx      # 0x543e
leaq 0x180(%rsp), %r13
movl $0x1000, %esi           # imm = 0x1000
movq %r13, %rdi
xorl %eax, %eax
callq 0x21b0
movq %r13, %rdi
xorl %esi, %esi
callq 0x24c0
testl %eax, %eax
je 0x3a86
cmpl $0x2, (%r12)
jne 0x3a26
jmp 0x3a97
leaq 0x180(%rsp), %rdi
callq 0x2090
testl %eax, %eax
jne 0x3a26
movq %r12, 0x28(%rsp)
leaq 0x58(%rsp), %r12
pushq $0x1
popq %rax
movq $0x0, 0x30(%rsp)
movq $0x0, 0x10(%rsp)
cmpl 0xc(%rsp), %eax
ja 0x3c6d
movl %eax, 0x18(%rsp)
movq %r12, %rdi
xorl %esi, %esi
callq 0x20d0
testq %rax, %rax
je 0x39ba
movq %rax, %r13
movl 0x18(%rsp), %esi
movl %esi, 0x8(%rax)
movq %r12, %rdi
leaq 0x1180(%rsp), %r12
movq %r12, %rdx
callq 0x22c0
movq %r12, %rdi
movl $0x101002, %esi         # imm = 0x101002
movl $0x180, %edx            # imm = 0x180
xorl %eax, %eax
callq 0x24b0
testl %eax, %eax
js 0x3ba5
movl %eax, %r12d
movl %eax, %edi
movl $0x1010, %esi           # imm = 0x1010
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x2480
cmpq $-0x1, %rax
movl %r12d, 0x24(%rsp)
je 0x3bb9
movl %r12d, %edi
xorl %esi, %esi
pushq $0x2
popq %rdx
callq 0x21f0
movq %rax, %r12
movq 0x38(%rsp), %rcx
movl $0x0, %eax
movq %rax, 0x10(%rsp)
testq %r12, %r12
je 0x3beb
js 0x3a26
xorl %edi, %edi
movq %r12, %rsi
pushq $0x1
popq %rcx
movl %ecx, %edx
movl 0x24(%rsp), %r8d
xorl %r9d, %r9d
callq 0x2160
cmpq $-0x1, %rax
je 0x3a26
movq %rax, 0x30(%rsp)
movq %rax, %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0x23b0
cmpl $-0x1, %eax
je 0x3bc0
cmpl $-0x2, %eax
jne 0x3bcc
movq 0x50(%rsp), %rcx
jmp 0x3bc5
movq 0x28(%rsp), %rax
cmpl $0x2, (%rax)
jne 0x3a26
incl 0x4(%r15)
jmp 0x3c0b
movq 0x40(%rsp), %rcx
jmp 0x3beb
movq 0x38(%rsp), %rcx
movq %r12, 0x10(%rsp)
jmp 0x3beb
andl $0x0, 0xc(%r13)
movq %r13, %rdi
callq 0x24d0
movq 0x48(%rsp), %rcx
movq %r12, 0x10(%rsp)
testl %eax, %eax
js 0x3a26
incl (%rcx)
leaq 0x1180(%rsp), %rdi
callq 0x2090
testl %eax, %eax
jne 0x3a26
movl 0x24(%rsp), %edi
callq 0x2220
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0x3c1f
movq 0x10(%rsp), %rsi
callq 0x23e0
movq 0x48(%r13), %r12
testq %r12, %r12
je 0x3c46
movq 0x30(%r12), %rdi
movq 0x40(%r12), %r12
callq 0x2040
movq 0x48(%r13), %rdi
callq 0x2040
movq %r12, 0x48(%r13)
jmp 0x3c23
movq %r13, %rdi
addq $0x20, %rdi
callq 0x21a0
movq %r13, %rdi
callq 0x2040
incl (%r15)
movl 0x18(%rsp), %eax
incl %eax
leaq 0x58(%rsp), %r12
jmp 0x3ab6
xorl %ebx, %ebx
testb $0x1, %bpl
je 0x39ba
movq 0x68(%rsp), %rdi
movq %rdi, 0x18(%rsp)
callq 0x2120
testq %rax, %rax
je 0x3d37
movq %rax, %r13
leaq 0x1798(%rip), %rbx      # 0x5433
leaq 0x3180(%rsp), %rbp
movq 0x28(%rsp), %r12
andl $0x0, (%r12)
movq %r13, %rdi
callq 0x2350
testq %rax, %rax
je 0x3d47
movq %rax, %r15
addq $0x13, %r15
movq %r15, %rdi
movq %rbx, %rsi
callq 0x2290
testl %eax, %eax
jne 0x3ca8
movl $0x3000, %edx           # imm = 0x3000
movq %rbp, %rdi
xorl %esi, %esi
callq 0x2200
movq %rbp, %rdi
movq 0x18(%rsp), %rsi
callq 0x2510
movq %rbp, %rdi
callq 0x2140
movw $0x2f, 0x3180(%rsp,%rax)
movq %rbp, %rdi
movq %r15, %rsi
callq 0x2510
movq %rbp, %rdi
callq 0x2140
cmpq $0x1000, %rax           # imm = 0x1000
ja 0x3d2d
movq %rbp, %rdi
callq 0x2090
testl %eax, %eax
je 0x3ca8
movq %r13, %rdi
callq 0x2240
jmp 0x3d65
movq 0x28(%rsp), %rax
cmpl $0x2, (%rax)
je 0x39ba
jmp 0x3d65
movl (%r12), %ebx
movq %r13, %rdi
callq 0x2240
orl %ebx, %eax
jne 0x3d65
movq 0x18(%rsp), %rdi
callq 0x2130
testl %eax, %eax
je 0x3d6c
pushq $-0x4
jmp 0x3a28
xorl %ebx, %ebx
jmp 0x39ba
nop