// File: /workspace/github_c_makefile_star2_O3/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 $0x3188, %rsp           # imm = 0x3188
movl %ecx, %ebp
movq %rdx, %r14
movq %rsi, %r15
movq %rdi, %r12
xorl %eax, %eax
movq %rax, 0x68(%rsp)
movq $-0x1, %rcx
movq %rcx, 0x70(%rsp)
movq %rcx, 0x78(%rsp)
movq %rax, 0x10(%rdx)
xorps %xmm0, %xmm0
movups %xmm0, (%rdx)
movl $0x101002, %esi         # imm = 0x101002
xorl %eax, %eax
callq 0x24b0
movl %eax, 0x58(%rsp)
testl %eax, %eax
js 0x390c
movq %r12, 0x60(%rsp)
xorl %ebx, %ebx
movl %eax, %edi
movl $0x110, %esi            # imm = 0x110
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x2480
cmpq $-0x1, %rax
je 0x3a96
testq %r15, %r15
je 0x3927
movq %r15, %rdi
callq 0x2560
movq %rax, %rbx
movq %rax, 0x68(%rsp)
testq %rax, %rax
jne 0x395b
movl $0xfffffffd, %r15d      # imm = 0xFFFFFFFD
jmp 0x3a8b
callq 0x2070
xorl %r15d, %r15d
cmpl $0x2, (%rax)
sete %r15b
negl %r15d
orl $-0x5, %r15d
jmp 0x3a8b
movl $0x1000, %edi           # imm = 0x1000
callq 0x2390
movq %rax, 0x68(%rsp)
movl $0xfffffffd, %r15d      # imm = 0xFFFFFFFD
testq %rax, %rax
je 0x3a8b
movq %rax, %rbx
movq %r12, %rdi
movq %rax, %rsi
callq 0x2450
testl %eax, %eax
je 0x3a8b
leaq 0xf0(%rsp), %rsi
movq %rbx, %rdi
callq 0x2260
testl %eax, %eax
js 0x3a6e
movl $0xf000, %eax           # imm = 0xF000
andl 0x108(%rsp), %eax
cmpl $0x4000, %eax           # imm = 0x4000
jne 0x3a85
movq 0x68(%rsp), %rbx
movq %rbx, %rdi
xorl %esi, %esi
xorl %eax, %eax
callq 0x24b0
movl %eax, 0x70(%rsp)
testl %eax, %eax
js 0x3a6e
leaq 0x1a8a(%rip), %rdx      # 0x5438
leaq 0x1a7e(%rip), %r8       # 0x5433
leaq 0x2180(%rsp), %r15
movl $0x1000, %esi           # imm = 0x1000
movq %r15, %rdi
movq %rbx, %rcx
xorl %eax, %eax
callq 0x21b0
movq %r15, %rdi
movl $0x42, %esi
movl $0x180, %edx            # imm = 0x180
xorl %eax, %eax
callq 0x24b0
testl %eax, %eax
js 0x3a6e
movl %eax, 0x74(%rsp)
xorl %ebx, %ebx
movl $0x4, %esi
xorl %edi, %edi
movl $0x3, %edx
movl $0x1, %ecx
movl %eax, %r8d
xorl %r9d, %r9d
callq 0x2160
movq %rax, 0x78(%rsp)
cmpq $-0x1, %rax
je 0x3a96
movq 0x68(%rsp), %rdi
callq 0x2120
testq %rax, %rax
je 0x3a6e
movq %rax, %rbx
xorl %r15d, %r15d
movl %r15d, 0xc(%rsp)
callq 0x2070
movq %rax, %r12
movl %r15d, (%rax)
movq %rbx, %rdi
callq 0x2350
testq %rax, %rax
je 0x3a8f
addq $0x13, %rax
movq %rax, %rdi
callq 0x24f0
testl %eax, %eax
jle 0x3a64
cmpl 0xc(%rsp), %eax
jbe 0x3a64
movl %eax, 0xc(%rsp)
movl $0x0, (%r12)
jmp 0x3a3d
callq 0x2070
xorl %ecx, %ecx
cmpl $0x2, (%rax)
sete %cl
leal (%rcx,%rcx,2), %r15d
addl $-0x5, %r15d
jmp 0x3a8b
movl $0xfffffffe, %r15d      # imm = 0xFFFFFFFE
xorl %ebx, %ebx
jmp 0x3a9c
cmpl $0x0, (%r12)
je 0x3b09
movl $0xfffffffb, %r15d      # imm = 0xFFFFFFFB
movl 0x58(%rsp), %edi
testl %edi, %edi
js 0x3aa9
callq 0x2220
movl 0x74(%rsp), %edi
testl %edi, %edi
js 0x3ab6
callq 0x2220
movl 0x70(%rsp), %edi
testl %edi, %edi
js 0x3ac3
callq 0x2220
movq 0x68(%rsp), %rdi
testq %rdi, %rdi
je 0x3ad2
callq 0x2040
testq %rbx, %rbx
je 0x3adf
movq %rbx, %rdi
callq 0x2240
movq 0x78(%rsp), %rdi
cmpq $-0x1, %rdi
je 0x3af4
movl $0x4, %esi
callq 0x23e0
movl %r15d, %eax
addq $0x3188, %rsp           # imm = 0x3188
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl 0x74(%rsp), %edi
leaq 0xc(%rsp), %rsi
movl $0x4, %edx
xorl %ecx, %ecx
callq 0x2440
movl $0xfffffffd, %r15d      # imm = 0xFFFFFFFD
cmpl $0x4, %eax
jne 0x3a9c
movq %r12, 0x38(%rsp)
movq 0x68(%rsp), %rcx
leaq 0x1900(%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 0x3b73
movq 0x38(%rsp), %rax
cmpl $0x2, (%rax)
jne 0x3a96
jmp 0x3b88
leaq 0x180(%rsp), %rdi
callq 0x2090
testl %eax, %eax
jne 0x3a96
cmpl $0x0, 0xc(%rsp)
je 0x3dbc
leaq 0x8(%r14), %rax
movq %rax, 0x50(%rsp)
leaq 0xc(%r14), %rax
movq %rax, 0x30(%rsp)
leaq 0x10(%r14), %rax
movq %rax, 0x48(%rsp)
leaq 0x14(%r14), %rax
movq %rax, 0x40(%rsp)
movl $0x1, %eax
movq %rax, 0x28(%rsp)
leaq 0x58(%rsp), %r12
movq $0x0, 0x20(%rsp)
movq $0x0, 0x10(%rsp)
movq %r12, %rdi
xorl %esi, %esi
callq 0x20d0
testq %rax, %rax
je 0x3a9c
movq %rax, %r13
movq 0x28(%rsp), %rsi
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 0x3cc8
movl %eax, %edi
movl %eax, 0x1c(%rsp)
movl $0x1010, %esi           # imm = 0x1010
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x2480
cmpq $-0x1, %rax
movq 0x10(%rsp), %r12
je 0x3ce1
movl 0x1c(%rsp), %edi
xorl %esi, %esi
movl $0x2, %edx
callq 0x21f0
movq %rax, %rdx
movq 0x30(%rsp), %rcx
movl $0x0, %eax
movq %rax, 0x20(%rsp)
testq %rdx, %rdx
je 0x3d20
js 0x3a96
xorl %edi, %edi
movq %rdx, 0x10(%rsp)
movq %rdx, %rsi
movl $0x1, %edx
movl $0x1, %ecx
movl 0x1c(%rsp), %r8d
xorl %r9d, %r9d
callq 0x2160
cmpq $-0x1, %rax
je 0x3a96
movq %rax, %r12
movq %rax, %rdi
movq 0x10(%rsp), %rsi
movq %r13, %rdx
callq 0x23b0
cmpl $-0x1, %eax
je 0x3ce8
cmpl $-0x2, %eax
jne 0x3cf9
movq 0x48(%rsp), %rcx
jmp 0x3ced
movq 0x38(%rsp), %rax
cmpl $0x2, (%rax)
movq 0x10(%rsp), %r12
jne 0x3a96
incl 0x4(%r14)
jmp 0x3d40
movq 0x50(%rsp), %rcx
jmp 0x3d20
movq 0x30(%rsp), %rcx
movq 0x10(%rsp), %rax
movq %rax, 0x20(%rsp)
jmp 0x3d20
movl $0x0, 0xc(%r13)
movq %r13, %rdi
callq 0x24d0
movq 0x40(%rsp), %rcx
movq 0x10(%rsp), %rdx
movq %rdx, 0x20(%rsp)
testl %eax, %eax
js 0x3a96
incl (%rcx)
leaq 0x1180(%rsp), %rdi
callq 0x2090
testl %eax, %eax
jne 0x3a96
movl 0x1c(%rsp), %edi
callq 0x2220
testq %r12, %r12
je 0x3d52
movq %r12, %rdi
movq 0x20(%rsp), %rsi
callq 0x23e0
movq %r12, 0x10(%rsp)
movq 0x48(%r13), %r12
testq %r12, %r12
je 0x3d89
movq %r12, %rdi
movq 0x30(%r12), %rax
movq 0x40(%r12), %r12
testq %rax, %rax
je 0x3d7e
movq %rax, %rdi
callq 0x2040
movq 0x48(%r13), %rdi
callq 0x2040
movq %r12, 0x48(%r13)
jmp 0x3d5b
movq %r13, %rdi
addq $0x20, %rdi
callq 0x21a0
movq %r13, %rdi
callq 0x2040
incl (%r14)
movq 0x28(%rsp), %rcx
leal 0x1(%rcx), %eax
cmpl 0xc(%rsp), %ecx
movq %rax, 0x28(%rsp)
leaq 0x58(%rsp), %r12
jb 0x3bd8
xorl %r15d, %r15d
testb $0x1, %bpl
je 0x3a9c
movq 0x68(%rsp), %rdi
callq 0x3de3
movl %eax, %r15d
sarl $0x1f, %r15d
andl $-0x4, %r15d
jmp 0x3a9c