// File: /workspace/github_c_makefile_star2_O1/albertito[P]libjio/libjio/trans.c
// Function: jtrans_commit
ssize_t jtrans_commit(struct jtrans *ts)
{
	ssize_t r, retval = -1;
	struct operation *op;
	struct jlinger *linger;
	jop_t *jop = NULL;
	size_t written = 0;

	pthread_mutex_lock(&(ts->lock));

	/* clear the flags */
	ts->flags = ts->flags & ~J_COMMITTED;
	ts->flags = ts->flags & ~J_ROLLBACKED;

	if (ts->numops_r + ts->numops_w == 0)
		goto exit;

	/* fail for read-only accesses if we have write operations */
	if (ts->numops_w && (ts->flags & J_RDONLY))
		goto exit;

	/* Lock all the regions we're going to work with; otherwise there
	 * could be another transaction trying to write the same spots and we
	 * could end up with interleaved writes, that could break atomicity
	 * warantees if we need to rollback.
	 * Note we do this before creating a new transaction, so we know it's
	 * not possible to have two overlapping transactions on disk at the
	 * same time. */
	if (lock_file_ranges(ts, F_LOCKW) != 0)
		goto unlock_exit;

	/* create and fill the transaction file only if we have at least one
	 * write operation */
	if (ts->numops_w) {
		jop = journal_new(ts->fs, ts->flags);
		if (jop == NULL)
			goto unlock_exit;
	}

	for (op = ts->op; op != NULL; op = op->next) {
		if (op->direction == D_READ)
			continue;

		r = journal_add_op(jop, op->buf, op->len, op->offset);
		if (r != 0)
			goto unlink_exit;

		fiu_exit_on("jio/commit/tf_opdata");
	}

	if (jop)
		journal_pre_commit(jop);

	fiu_exit_on("jio/commit/tf_data");

	if (!(ts->flags & J_NOROLLBACK)) {
		for (op = ts->op; op != NULL; op = op->next) {
			if (op->direction == D_READ)
				continue;

			 r = operation_read_prev(ts, op);
			 if (r < 0)
				 goto unlink_exit;
		}
	}

	if (jop) {
		r = journal_commit(jop);
		if (r < 0)
			goto unlink_exit;
	}

	/* now that we have a safe transaction file, let's apply it */
	written = 0;
	for (op = ts->op; op != NULL; op = op->next) {
		if (op->direction == D_READ) {
			r = spread(ts->fs->fd, op->buf, op->len, op->offset);
			if (r != op->len)
				goto rollback_exit;

			continue;
		}

		/* from now on, write ops (which are more interesting) */

		r = spwrite(ts->fs->fd, op->buf, op->len, op->offset);
		if (r != op->len)
			goto rollback_exit;

		written += r;

		if (have_sync_range && !(ts->flags & J_LINGER)) {
			r = sync_range_submit(ts->fs->fd, op->len,
					op->offset);
			if (r != 0)
				goto rollback_exit;
		}

		fiu_exit_on("jio/commit/wrote_op");
	}

	fiu_exit_on("jio/commit/wrote_all_ops");

	if (jop && (ts->flags & J_LINGER)) {
		struct jlinger *lp;

		linger = malloc(sizeof(struct jlinger));
		if (linger == NULL)
			goto rollback_exit;

		linger->jop = jop;
		linger->next = NULL;

		pthread_mutex_lock(&(ts->fs->ltlock));

		/* add it to the end of the list so they're in order */
		if (ts->fs->ltrans == NULL) {
			ts->fs->ltrans = linger;
		} else {
			lp = ts->fs->ltrans;
			while (lp->next != NULL)
				lp = lp->next;
			lp->next = linger;
		}

		ts->fs->ltrans_len += written;
		autosync_check(ts->fs);

		pthread_mutex_unlock(&(ts->fs->ltlock));

		/* Leave the journal_free() up to jsync() */
		jop = NULL;
	} else if (jop) {
		if (have_sync_range) {
			for (op = ts->op; op != NULL; op = op->next) {
				if (op->direction == D_READ)
					continue;

				r = sync_range_wait(ts->fs->fd, op->len,
						op->offset);
				if (r != 0)
					goto rollback_exit;
			}
		} else {
			if (fdatasync(ts->fs->fd) != 0)
				goto rollback_exit;
		}
	}

	/* mark the transaction as committed */
	ts->flags = ts->flags | J_COMMITTED;

	retval = 1;

rollback_exit:
	/* If the transaction failed we try to recover by rolling it back.
	 * Only used if it has at least one write operation.
	 *
	 * NOTE: on extreme conditions (ENOSPC/disk failure) this can fail
	 * too! There's nothing much we can do in that case, the caller should
	 * take care of it by itself.
	 *
	 * Transactions that were successfuly recovered by rolling them back
	 * will have J_ROLLBACKED in their flags. */
	if (jop && !(ts->flags & J_COMMITTED) &&
			!(ts->flags & J_ROLLBACKING)) {
		r = ts->flags;
		ts->flags = ts->flags | J_NOLOCK | J_ROLLBACKING;
		if (jtrans_rollback(ts) >= 0) {
			ts->flags = r | J_ROLLBACKED;
			retval = -1;
		} else {
			ts->flags = r;
			retval = -2;
		}
	}

unlink_exit:
	/* If the journal operation is no longer needed, we remove it from the
	 * disk.
	 *
	 * Extreme conditions (filesystem just got read-only, for example) can
	 * cause journal_free() to fail, but there's not much left to do at
	 * that point, and the caller will have to be careful and stop its
	 * operations. In that case, we will return -2, and the transaction
	 * will be marked as J_COMMITTED to indicate that the data was
	 * effectively written to disk. */
	if (jop) {
		/* Note we only unlink if we've written down the real data, or
		 * at least rolled it back properly */
		int data_is_safe = (ts->flags & J_COMMITTED) ||
			(ts->flags & J_ROLLBACKED);
		r = journal_free(jop, data_is_safe ? 1 : 0);
		if (r != 0)
			retval = -2;

		jop = NULL;
	}

unlock_exit:
	/* always unlock everything at the end; otherwise we could have
	 * half-overlapping transactions applying simultaneously, and if
	 * anything goes wrong it would be possible to break consistency */
	lock_file_ranges(ts, F_UNLOCK);

exit:
	pthread_mutex_unlock(&(ts->lock));

	return retval;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rdi, %r14
leaq 0x20(%rdi), %rbx
movq %rbx, %rdi
callq 0x2590
movl 0xc(%r14), %eax
movl 0x14(%r14), %ecx
movl %eax, %edx
andl $0xfffff3ff, %edx       # imm = 0xFFFFF3FF
movl %edx, 0xc(%r14)
movq $-0x1, %r12
movl 0x10(%r14), %edx
addl %ecx, %edx
je 0x316e
testl %ecx, %ecx
sete %cl
btl $0x9, %eax
setae %al
orb %cl, %al
cmpb $0x1, %al
jne 0x316e
movq %r14, %rdi
movl $0x110, %esi            # imm = 0x110
callq 0x3195
movq $-0x1, %r12
testl %eax, %eax
jne 0x3161
cmpl $0x0, 0x14(%r14)
je 0x2e84
movq (%r14), %rdi
movl 0xc(%r14), %esi
callq 0x2060
movq %rax, %r15
testq %rax, %rax
jne 0x2e87
jmp 0x3161
xorl %r15d, %r15d
movq 0x48(%r14), %r12
testq %r12, %r12
je 0x2ebe
cmpl $0x1, 0x20(%r12)
je 0x2eb7
movq 0x18(%r12), %rsi
movq 0x8(%r12), %rcx
movq 0x10(%r12), %rdx
movq %r15, %rdi
callq 0x2150
testl %eax, %eax
jne 0x312d
movq 0x40(%r12), %r12
jmp 0x2e8b
testq %r15, %r15
je 0x2ecb
movq %r15, %rdi
callq 0x23a0
testb $0x2, 0xc(%r14)
jne 0x2f36
movq 0x48(%r14), %rbp
testq %rbp, %rbp
je 0x2f36
movq $-0x1, %r12
cmpl $0x1, 0x20(%rbp)
je 0x2f2d
movq 0x10(%rbp), %r13
movq %r13, %rdi
callq 0x2390
movq %rax, 0x30(%rbp)
testq %rax, %rax
je 0x3139
movq (%r14), %rcx
movl (%rcx), %edi
movq 0x8(%rbp), %rcx
movq %rax, %rsi
movq %r13, %rdx
callq 0x22d0
testq %rax, %rax
js 0x3085
movq 0x10(%rbp), %rcx
cmpq %rcx, %rax
cmovbq %rax, %rcx
movq %rcx, 0x28(%rbp)
movq 0x40(%rbp), %rbp
testq %rbp, %rbp
jne 0x2ee2
testq %r15, %r15
je 0x2f4b
movq %r15, %rdi
callq 0x22a0
testl %eax, %eax
js 0x312d
movq 0x48(%r14), %rbp
testq %rbp, %rbp
je 0x2fda
movq 0x5079(%rip), %rax      # 0x7fd8
movl (%rax), %eax
movl %eax, 0x4(%rsp)
xorl %r13d, %r13d
movq $-0x1, %r12
cmpl $0x1, 0x20(%rbp)
movq (%r14), %rax
movl (%rax), %edi
movq 0x18(%rbp), %rsi
movq 0x8(%rbp), %rcx
movq 0x10(%rbp), %rdx
jne 0x2f96
callq 0x22d0
cmpq 0x10(%rbp), %rax
je 0x2fcf
jmp 0x307c
callq 0x2440
movq 0x10(%rbp), %rsi
cmpq %rsi, %rax
jne 0x30fa
addq %rax, %r13
cmpl $0x0, 0x4(%rsp)
je 0x2fcf
testb $0x4, 0xc(%r14)
jne 0x2fcf
movq (%r14), %rax
movl (%rax), %edi
movq 0x8(%rbp), %rdx
callq 0x2050
testl %eax, %eax
jne 0x30fa
movq 0x40(%rbp), %rbp
testq %rbp, %rbp
jne 0x2f6f
jmp 0x2fdd
xorl %r13d, %r13d
testq %r15, %r15
je 0x302d
testb $0x4, 0xc(%r14)
jne 0x3035
movq 0x4fe8(%rip), %rax      # 0x7fd8
cmpl $0x0, (%rax)
je 0x309b
movq 0x48(%r14), %r12
testq %r12, %r12
je 0x30ef
cmpl $0x1, 0x20(%r12)
je 0x3026
movq (%r14), %rax
movl (%rax), %edi
movq 0x8(%r12), %rdx
movq 0x10(%r12), %rsi
callq 0x2030
testl %eax, %eax
jne 0x307c
movq 0x40(%r12), %r12
jmp 0x2ffd
xorl %r15d, %r15d
jmp 0x30ef
movl $0x10, %edi
callq 0x2390
testq %rax, %rax
je 0x30b2
movq %rax, %r12
movq %r15, (%rax)
movq $0x0, 0x8(%rax)
movq (%r14), %rdi
addq $0x40, %rdi
callq 0x2590
movq (%r14), %rdi
movq 0x30(%rdi), %rcx
testq %rcx, %rcx
je 0x30b9
movq %rcx, %rax
movq 0x8(%rcx), %rcx
testq %rcx, %rcx
jne 0x306a
addq $0x8, %rax
jmp 0x30bd
movq $-0x1, %r12
jmp 0x30fa
movq 0x30(%rbp), %rdi
callq 0x2040
movq $0x0, 0x30(%rbp)
jmp 0x3139
movq (%r14), %rax
movl (%rax), %edi
callq 0x2080
movq $-0x1, %r12
testl %eax, %eax
jne 0x30fa
jmp 0x30ef
movl $0xe, %eax
jmp 0x30da
leaq 0x30(%rdi), %rax
movq %r12, (%rax)
addq %r13, 0x38(%rdi)
callq 0x2470
movq (%r14), %rdi
addq $0x40, %rdi
callq 0x2370
xorl %r15d, %r15d
xorl %eax, %eax
testl %eax, %eax
je 0x30ef
movq $-0x1, %r12
cmpl $0xe, %eax
je 0x30fa
jmp 0x3176
orb $0x4, 0xd(%r14)
movl $0x1, %r12d
testq %r15, %r15
je 0x3136
movl 0xc(%r14), %ebp
testl $0x1400, %ebp           # imm = 0x1400
jne 0x3139
movl %ebp, %eax
orl $0x1001, %eax           # imm = 0x1001
movl %eax, 0xc(%r14)
movq %r14, %rdi
callq 0x20e0
testq %rax, %rax
js 0x3188
orl $0x800, %ebp            # imm = 0x800
movl %ebp, 0xc(%r14)
movq $-0x1, %r12
jmp 0x3139
xorl %r15d, %r15d
testq %r15, %r15
je 0x3161
xorl %esi, %esi
testl $0xc00, 0xc(%r14)       # imm = 0xC00
setne %sil
movq %r15, %rdi
callq 0x23c0
testl %eax, %eax
movq $-0x2, %rax
cmovneq %rax, %r12
movq %r14, %rdi
movl $0x10000, %esi          # imm = 0x10000
callq 0x3195
movq %rbx, %rdi
callq 0x2370
movq %r12, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl %ebp, 0xc(%r14)
movq $-0x2, %r12
jmp 0x3139