// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x3190, %rsp           # imm = 0x3190
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movl %ecx, -0x1c(%rbp)
movl $0xffffffff, -0x20(%rbp) # imm = 0xFFFFFFFF
movq $0x0, -0x3188(%rbp)
movq $0x0, -0x3170(%rbp)
movl $0xffffffff, -0x3158(%rbp) # imm = 0xFFFFFFFF
movl $0xffffffff, -0x313c(%rbp) # imm = 0xFFFFFFFF
movq $0x0, -0x3148(%rbp)
movl $0xffffffff, -0x3140(%rbp) # imm = 0xFFFFFFFF
movq $-0x1, %rax
movq %rax, -0x3138(%rbp)
movq $0x0, -0x3180(%rbp)
movl $0x0, -0x2c(%rbp)
movq -0x18(%rbp), %rax
movl $0x0, (%rax)
movq -0x18(%rbp), %rax
movl $0x0, 0x4(%rax)
movq -0x18(%rbp), %rax
movl $0x0, 0x8(%rax)
movq -0x18(%rbp), %rax
movl $0x0, 0xc(%rax)
movq -0x18(%rbp), %rax
movl $0x0, 0x10(%rax)
movq -0x18(%rbp), %rax
movl $0x0, 0x14(%rax)
movq -0x8(%rbp), %rdi
movl $0x101002, %esi         # imm = 0x101002
movb $0x0, %al
callq 0x2500
movl %eax, -0x3158(%rbp)
cmpl $0x0, -0x3158(%rbp)
jge 0x47dd
movl $0xfffffffb, -0x2c(%rbp) # imm = 0xFFFFFFFB
callq 0x2070
cmpl $0x2, (%rax)
jne 0x47d8
movl $0xffffffff, -0x2c(%rbp) # imm = 0xFFFFFFFF
jmp 0x4e67
movq -0x8(%rbp), %rax
movq %rax, -0x3150(%rbp)
movl -0x3158(%rbp), %edi
movl $0x110, %esi            # imm = 0x110
xorl %eax, %eax
movl %eax, %ecx
movq %rcx, %rdx
callq 0x24d0
movq %rax, -0x3190(%rbp)
cmpq $-0x1, -0x3190(%rbp)
jne 0x481c
movl $0xfffffffb, -0x2c(%rbp) # imm = 0xFFFFFFFB
jmp 0x4e67
cmpq $0x0, -0x10(%rbp)
jne 0x486d
movl $0x1000, %edi           # imm = 0x1000
callq 0x23c0
movq %rax, -0x3148(%rbp)
cmpq $0x0, -0x3148(%rbp)
jne 0x484a
movl $0xfffffffd, -0x2c(%rbp) # imm = 0xFFFFFFFD
jmp 0x4e67
movq -0x8(%rbp), %rdi
movq -0x3148(%rbp), %rsi
callq 0x24a0
cmpl $0x0, %eax
jne 0x486b
movl $0xfffffffd, -0x2c(%rbp) # imm = 0xFFFFFFFD
jmp 0x4e67
jmp 0x4895
movq -0x10(%rbp), %rdi
callq 0x25c0
movq %rax, -0x3148(%rbp)
cmpq $0x0, -0x3148(%rbp)
jne 0x4893
movl $0xfffffffd, -0x2c(%rbp) # imm = 0xFFFFFFFD
jmp 0x4e67
jmp 0x4895
movq -0x3148(%rbp), %rdi
leaq -0x30c0(%rbp), %rsi
callq 0x2280
movl %eax, -0x24(%rbp)
cmpl $0x0, -0x24(%rbp)
jge 0x48ce
movl $0xfffffffb, -0x2c(%rbp) # imm = 0xFFFFFFFB
callq 0x2070
cmpl $0x2, (%rax)
jne 0x48c9
movl $0xfffffffe, -0x2c(%rbp) # imm = 0xFFFFFFFE
jmp 0x4e67
movl -0x30a8(%rbp), %eax
andl $0xf000, %eax           # imm = 0xF000
cmpl $0x4000, %eax           # imm = 0x4000
je 0x48ec
movl $0xfffffffe, -0x2c(%rbp) # imm = 0xFFFFFFFE
jmp 0x4e67
movq -0x3148(%rbp), %rdi
xorl %esi, %esi
movb $0x0, %al
callq 0x2500
movl %eax, -0x3140(%rbp)
cmpl $0x0, -0x3140(%rbp)
jge 0x4928
movl $0xfffffffb, -0x2c(%rbp) # imm = 0xFFFFFFFB
callq 0x2070
cmpl $0x2, (%rax)
jne 0x4923
movl $0xfffffffe, -0x2c(%rbp) # imm = 0xFFFFFFFE
jmp 0x4e67
leaq -0x1030(%rbp), %rdi
movq -0x3148(%rbp), %rcx
movl $0x1000, %esi           # imm = 0x1000
leaq 0x26de(%rip), %rdx      # 0x7020
leaq 0x26d2(%rip), %r8       # 0x701b
movb $0x0, %al
callq 0x21c0
leaq -0x1030(%rbp), %rdi
movl $0x42, %esi
movl $0x180, %edx            # imm = 0x180
movb $0x0, %al
callq 0x2500
movl %eax, -0x24(%rbp)
cmpl $0x0, -0x24(%rbp)
jge 0x498e
movl $0xfffffffb, -0x2c(%rbp) # imm = 0xFFFFFFFB
callq 0x2070
cmpl $0x2, (%rax)
jne 0x4989
movl $0xfffffffe, -0x2c(%rbp) # imm = 0xFFFFFFFE
jmp 0x4e67
movl -0x24(%rbp), %eax
movl %eax, -0x313c(%rbp)
movl -0x313c(%rbp), %r8d
xorl %eax, %eax
movl %eax, %r9d
movl $0x4, %esi
movl $0x3, %edx
movl $0x1, %ecx
movq %r9, %rdi
callq 0x2160
movq %rax, -0x3138(%rbp)
movq $-0x1, %rax
cmpq %rax, -0x3138(%rbp)
jne 0x49dd
movl $0xfffffffb, -0x2c(%rbp) # imm = 0xFFFFFFFB
jmp 0x4e67
movq -0x3148(%rbp), %rdi
callq 0x2120
movq %rax, -0x3170(%rbp)
cmpq $0x0, -0x3170(%rbp)
jne 0x4a17
movl $0xfffffffb, -0x2c(%rbp) # imm = 0xFFFFFFFB
callq 0x2070
cmpl $0x2, (%rax)
jne 0x4a12
movl $0xfffffffe, -0x2c(%rbp) # imm = 0xFFFFFFFE
jmp 0x4e67
movl $0x0, -0x30(%rbp)
callq 0x2070
movl $0x0, (%rax)
movq -0x3170(%rbp), %rdi
callq 0x2380
movq %rax, -0x3178(%rbp)
cmpq $0x0, -0x3178(%rbp)
je 0x4a91
movq -0x3178(%rbp), %rdi
addq $0x13, %rdi
callq 0x2540
movl %eax, -0x24(%rbp)
cmpl $0x0, -0x24(%rbp)
jg 0x4a61
jmp 0x4a71
movl -0x24(%rbp), %eax
cmpl -0x30(%rbp), %eax
jbe 0x4a6f
movl -0x24(%rbp), %eax
movl %eax, -0x30(%rbp)
jmp 0x4a71
callq 0x2070
movl $0x0, (%rax)
movq -0x3170(%rbp), %rdi
callq 0x2380
movq %rax, -0x3178(%rbp)
jmp 0x4a3c
callq 0x2070
cmpl $0x0, (%rax)
je 0x4aa7
movl $0xfffffffb, -0x2c(%rbp) # imm = 0xFFFFFFFB
jmp 0x4e67
movl -0x313c(%rbp), %edi
leaq -0x30(%rbp), %rsi
movl $0x4, %edx
xorl %eax, %eax
movl %eax, %ecx
callq 0x2490
movl %eax, -0x24(%rbp)
movslq -0x24(%rbp), %rax
cmpq $0x4, %rax
je 0x4ad8
movl $0xfffffffd, -0x2c(%rbp) # imm = 0xFFFFFFFD
jmp 0x4e67
leaq -0x3030(%rbp), %rdi
movq -0x3148(%rbp), %rcx
movl $0x1000, %esi           # imm = 0x1000
leaq 0x2534(%rip), %rdx      # 0x7026
movb $0x0, %al
callq 0x21c0
leaq -0x3030(%rbp), %rdi
xorl %esi, %esi
callq 0x2510
movl %eax, -0x24(%rbp)
cmpl $0x0, -0x24(%rbp)
jne 0x4b2f
leaq -0x3030(%rbp), %rdi
callq 0x2090
cmpl $0x0, %eax
je 0x4b2d
movl $0xfffffffb, -0x2c(%rbp) # imm = 0xFFFFFFFB
jmp 0x4e67
jmp 0x4b47
callq 0x2070
cmpl $0x2, (%rax)
je 0x4b45
movl $0xfffffffb, -0x2c(%rbp) # imm = 0xFFFFFFFB
jmp 0x4e67
jmp 0x4b47
movl $0x1, -0x28(%rbp)
movl -0x28(%rbp), %eax
cmpl -0x30(%rbp), %eax
ja 0x4e3c
leaq -0x3158(%rbp), %rdi
xorl %esi, %esi
callq 0x20d0
movq %rax, -0x3160(%rbp)
cmpq $0x0, -0x3160(%rbp)
jne 0x4b85
movl $0xfffffffd, -0x2c(%rbp) # imm = 0xFFFFFFFD
jmp 0x4e67
movl -0x28(%rbp), %ecx
movq -0x3160(%rbp), %rax
movl %ecx, 0x8(%rax)
movl -0x28(%rbp), %esi
leaq -0x2030(%rbp), %rdx
leaq -0x3158(%rbp), %rdi
callq 0x22e0
leaq -0x2030(%rbp), %rdi
movl $0x101002, %esi         # imm = 0x101002
movl $0x180, %edx            # imm = 0x180
movb $0x0, %al
callq 0x2500
movl %eax, -0x20(%rbp)
cmpl $0x0, -0x20(%rbp)
jge 0x4bf1
callq 0x2070
cmpl $0x2, (%rax)
jne 0x4be5
movq -0x18(%rbp), %rax
movl 0x4(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x4(%rax)
jmp 0x4d65
movl $0xfffffffb, -0x2c(%rbp) # imm = 0xFFFFFFFB
jmp 0x4e67
movl -0x20(%rbp), %edi
movl $0x1010, %esi           # imm = 0x1010
xorl %eax, %eax
movl %eax, %ecx
movq %rcx, %rdx
callq 0x24d0
movq %rax, -0x3190(%rbp)
cmpq $-0x1, -0x3190(%rbp)
jne 0x4c28
movq -0x18(%rbp), %rax
movl 0x8(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x8(%rax)
jmp 0x4d46
movl -0x20(%rbp), %edi
xorl %eax, %eax
movl %eax, %esi
movl $0x2, %edx
callq 0x2200
movq %rax, -0x3188(%rbp)
cmpq $0x0, -0x3188(%rbp)
jne 0x4c5c
movq -0x18(%rbp), %rax
movl 0xc(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0xc(%rax)
jmp 0x4d46
cmpq $0x0, -0x3188(%rbp)
jge 0x4c72
movl $0xfffffffb, -0x2c(%rbp) # imm = 0xFFFFFFFB
jmp 0x4e67
jmp 0x4c74
movq -0x3188(%rbp), %rsi
movl -0x20(%rbp), %r8d
xorl %eax, %eax
movl %eax, %r9d
movl $0x1, %ecx
movq %r9, %rdi
movl %ecx, %edx
callq 0x2160
movq %rax, -0x3180(%rbp)
movq $-0x1, %rax
cmpq %rax, -0x3180(%rbp)
jne 0x4cc1
movq $0x0, -0x3180(%rbp)
movl $0xfffffffb, -0x2c(%rbp) # imm = 0xFFFFFFFB
jmp 0x4e67
movq -0x3180(%rbp), %rdi
movq -0x3188(%rbp), %rsi
movq -0x3160(%rbp), %rdx
callq 0x23e0
movl %eax, -0x24(%rbp)
cmpl $-0x1, -0x24(%rbp)
jne 0x4cf3
movq -0x18(%rbp), %rax
movl 0xc(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0xc(%rax)
jmp 0x4d46
cmpl $-0x2, -0x24(%rbp)
jne 0x4d08
movq -0x18(%rbp), %rax
movl 0x10(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x10(%rax)
jmp 0x4d46
jmp 0x4d0a
movq -0x3160(%rbp), %rax
movl $0x0, 0xc(%rax)
movq -0x3160(%rbp), %rdi
callq 0x2520
movl %eax, -0x24(%rbp)
cmpl $0x0, -0x24(%rbp)
jge 0x4d39
movl $0xfffffffb, -0x2c(%rbp) # imm = 0xFFFFFFFB
jmp 0x4e67
movq -0x18(%rbp), %rax
movl 0x14(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x14(%rax)
leaq -0x2030(%rbp), %rdi
callq 0x2090
cmpl $0x0, %eax
je 0x4d63
movl $0xfffffffb, -0x2c(%rbp) # imm = 0xFFFFFFFB
jmp 0x4e67
jmp 0x4d65
cmpl $0x0, -0x20(%rbp)
jl 0x4d7a
movl -0x20(%rbp), %edi
callq 0x2240
movl $0xffffffff, -0x20(%rbp) # imm = 0xFFFFFFFF
cmpq $0x0, -0x3180(%rbp)
je 0x4d97
movq -0x3180(%rbp), %rdi
movq -0x3188(%rbp), %rsi
callq 0x2410
jmp 0x4d99
movq -0x3160(%rbp), %rax
cmpq $0x0, 0x48(%rax)
je 0x4e07
movq -0x3160(%rbp), %rax
movq 0x48(%rax), %rax
movq 0x40(%rax), %rax
movq %rax, -0x3168(%rbp)
movq -0x3160(%rbp), %rax
movq 0x48(%rax), %rax
cmpq $0x0, 0x30(%rax)
je 0x4de3
movq -0x3160(%rbp), %rax
movq 0x48(%rax), %rax
movq 0x30(%rax), %rdi
callq 0x2040
movq -0x3160(%rbp), %rax
movq 0x48(%rax), %rdi
callq 0x2040
movq -0x3168(%rbp), %rcx
movq -0x3160(%rbp), %rax
movq %rcx, 0x48(%rax)
jmp 0x4d99
movq -0x3160(%rbp), %rdi
addq $0x20, %rdi
callq 0x21b0
movq -0x3160(%rbp), %rdi
callq 0x2040
movq -0x18(%rbp), %rax
movl (%rax), %ecx
addl $0x1, %ecx
movl %ecx, (%rax)
movl -0x28(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x28(%rbp)
jmp 0x4b4e
movl -0x1c(%rbp), %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0x4e65
movq -0x8(%rbp), %rdi
movq -0x3148(%rbp), %rsi
callq 0x4f00
cmpl $0x0, %eax
jge 0x4e63
movl $0xfffffffc, -0x2c(%rbp) # imm = 0xFFFFFFFC
jmp 0x4e65
jmp 0x4e67
cmpl $0x0, -0x3158(%rbp)
jl 0x4e7b
movl -0x3158(%rbp), %edi
callq 0x2240
cmpl $0x0, -0x313c(%rbp)
jl 0x4e8f
movl -0x313c(%rbp), %edi
callq 0x2240
cmpl $0x0, -0x3140(%rbp)
jl 0x4ea3
movl -0x3140(%rbp), %edi
callq 0x2240
cmpq $0x0, -0x3148(%rbp)
je 0x4eb9
movq -0x3148(%rbp), %rdi
callq 0x2040
cmpq $0x0, -0x3170(%rbp)
je 0x4ecf
movq -0x3170(%rbp), %rdi
callq 0x2260
movq $-0x1, %rax
cmpq %rax, -0x3138(%rbp)
je 0x4ef0
movq -0x3138(%rbp), %rdi
movl $0x4, %esi
callq 0x2410
movl -0x2c(%rbp), %eax
addq $0x3190, %rsp           # imm = 0x3190
popq %rbp
retq
nopl (%rax)