// File: /workspace/github_c_makefile_star2_O2/albertito[P]libjio/libjio/trans.c
// Function: jtrans_rollback
ssize_t jtrans_rollback(struct jtrans *ts)
{
	ssize_t rv;
	struct jtrans *newts;
	struct operation *op, *curop, *lop;

	newts = jtrans_new(ts->fs, 0);
	if (newts == NULL)
		return -1;

	newts->flags = ts->flags;
	newts->numops_r = 0;
	newts->numops_w = 0;
	newts->len_w = 0;

	if (ts->op == NULL || ts->flags & J_NOROLLBACK) {
		rv = -1;
		goto exit;
	}

	/* find the last operation */
	for (op = ts->op; op->next != NULL; op = op->next)
		;

	/* and traverse the list backwards, skipping read operations */
	for ( ; op != NULL; op = op->prev) {
		if (op->direction == D_READ)
			continue;

		/* if we extended the data in the previous transaction, we
		 * should truncate it back */
		/* DANGEROUS: this is one of the main reasons why rollbacking
		 * is dangerous and should only be done with extreme caution:
		 * if for some reason, after the previous transacton, we have
		 * extended the file further, this will cut it back to what it
		 * was; read the docs for more detail */
		if (op->plen < op->len) {
			rv = ftruncate(ts->fs->fd, op->offset + op->plen);
			if (rv != 0)
				goto exit;
		}

		/* manually add the operation to the new transaction */
		curop = malloc(sizeof(struct operation));
		if (curop == NULL) {
			rv = -1;
			goto exit;
		}

		curop->offset = op->offset;
		curop->len = op->plen;
		curop->buf = op->pdata;
		curop->plen = op->plen;
		curop->pdata = op->pdata;
		curop->direction = op->direction;
		curop->locked = 0;

		newts->numops_w++;
		newts->len_w += curop->len;

		/* add the new transaction to the list */
		if (newts->op == NULL) {
			newts->op = curop;
			curop->prev = NULL;
			curop->next = NULL;
		} else {
			for (lop = newts->op; lop->next != NULL; lop = lop->next)
				;
			lop->next = curop;
			curop->prev = lop;
			curop->next = NULL;
		}
	}

	rv = jtrans_commit(newts);

exit:
	/* Free the transaction, taking care to set buf to NULL first since
	 * points to the same address as pdata, which would otherwise make
	 * jtrans_free() attempt to free it twice. We leave the data at
	 * curop->pdata since it is freed unconditionally, while the action
	 * on curop->buf depends on the direction of the transaction. */
	for (curop = newts->op; curop != NULL; curop = curop->next) {
		curop->buf = NULL;
	}
	jtrans_free(newts);

	return rv;
}

// Assembly:
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
movq %rdi, %r15
movq (%rdi), %rdi
xorl %esi, %esi
callq 0x20d0
testq %rax, %rax
je 0x32c2
movq %rax, %rbx
movl 0xc(%r15), %ecx
movl %ecx, 0xc(%rax)
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%rax)
movq 0x48(%r15), %rax
testq %rax, %rax
setne %dl
testb $0x2, %cl
sete %cl
andb %dl, %cl
pushq $-0x1
popq %r14
cmpb $0x1, %cl
jne 0x32d3
movq %rax, %r13
movq 0x40(%rax), %rax
testq %rax, %rax
jne 0x3208
pushq $0x48
popq %r12
testq %r13, %r13
je 0x32c8
cmpl $0x1, 0x20(%r13)
je 0x32b9
movq 0x28(%r13), %rsi
cmpq 0x10(%r13), %rsi
jae 0x324c
movq (%r15), %rax
movl (%rax), %edi
addq 0x8(%r13), %rsi
callq 0x21d0
testl %eax, %eax
jne 0x3302
movq %r12, %rdi
callq 0x2390
testq %rax, %rax
je 0x32d3
movq 0x8(%r13), %rcx
movq %rcx, 0x8(%rax)
movq 0x28(%r13), %rcx
movq %rcx, 0x10(%rax)
movq 0x30(%r13), %rdx
movq %rdx, 0x18(%rax)
movq %rcx, 0x28(%rax)
movq %rdx, 0x30(%rax)
movl 0x20(%r13), %edx
movl %edx, 0x20(%rax)
andl $0x0, (%rax)
incl 0x14(%rbx)
addq %rcx, 0x18(%rbx)
movq 0x48(%rbx), %rcx
testq %rcx, %rcx
je 0x32ae
movq %rcx, %rdx
movq 0x40(%rcx), %rcx
testq %rcx, %rcx
jne 0x3293
movq %rax, 0x40(%rdx)
movq %rdx, 0x38(%rax)
andq $0x0, 0x40(%rax)
jmp 0x32b9
movq %rax, 0x48(%rbx)
xorps %xmm0, %xmm0
movups %xmm0, 0x38(%rax)
movq 0x38(%r13), %r13
jmp 0x3218
pushq $-0x1
popq %r14
jmp 0x32f5
movq %rbx, %rdi
callq 0x24d0
movq %rax, %r14
movq %rbx, %rax
addq $0x48, %rax
movq (%rax), %rax
testq %rax, %rax
je 0x32ed
andq $0x0, 0x18(%rax)
addq $0x40, %rax
jmp 0x32da
movq %rbx, %rdi
callq 0x2170
movq %r14, %rax
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq
movslq %eax, %r14
jmp 0x32d3