// File: /workspace/github_c_makefile_star2_O2/albertito[P]libjio/libjio/trans.c
// Function: jtrans_add_common
static int jtrans_add_common(struct jtrans *ts, const void *buf, size_t count,
		off_t offset, enum op_direction direction)
{
	struct operation *op, *tmpop;

	op = tmpop = NULL;

	pthread_mutex_lock(&(ts->lock));

	/* Writes are not allowed in read-only mode, they fail early */
	if ((ts->flags & J_RDONLY) && direction == D_WRITE)
		goto error;

	if (count == 0)
		goto error;

	if ((long long) ts->len_w + count > MAX_TSIZE)
		goto error;

	op = malloc(sizeof(struct operation));
	if (op == NULL)
		goto error;

	if (direction == D_WRITE) {
		op->buf = malloc(count);
		if (op->buf == NULL)
			goto error;

		ts->numops_w++;
	} else {
		ts->numops_r++;
	}

	/* add op to the end of the linked list */
	op->next = NULL;
	if (ts->op == NULL) {
		ts->op = op;
		op->prev = NULL;
	} else {
		for (tmpop = ts->op; tmpop->next != NULL; tmpop = tmpop->next)
			;
		tmpop->next = op;
		op->prev = tmpop;
	}

	pthread_mutex_unlock(&(ts->lock));

	op->len = count;
	op->offset = offset;
	op->plen = 0;
	op->pdata = NULL;
	op->locked = 0;
	op->direction = direction;

	if (direction == D_WRITE) {
		memcpy(op->buf, buf, count);

		if (!(ts->flags & J_NOROLLBACK)) {
			/* jtrans_commit() will want to read the current data,
			 * so we tell the kernel about that */
			posix_fadvise(ts->fs->fd, offset, count,
					POSIX_FADV_WILLNEED);
		}
	} else {
		/* this casts the const away, which is ugly but let us have a
		 * common read/write path and avoid useless code repetition
		 * just to handle it */
		op->buf = (void *) buf;

		/* if there are no overlapping writes, jtrans_commit() will
		 * want to read the data from the disk; and if there are we
		 * will already have submitted a request and one more won't
		 * hurt */
		posix_fadvise(ts->fs->fd, offset, count, POSIX_FADV_WILLNEED);
	}

	return 0;

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

	if (op && direction == D_WRITE)
		free(op->buf);
	free(op);

	return -1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movl %r8d, %ebp
movq %rcx, %r14
movq %rdx, %rbx
movq %rsi, (%rsp)
movq %rdi, %r12
leaq 0x20(%rdi), %r13
movq %r13, %rdi
callq 0x25a0
cmpl $0x2, %ebp
sete %al
xorl %r15d, %r15d
testq %rbx, %rbx
je 0x2c7f
movb 0xd(%r12), %cl
andb $0x2, %cl
shrb %cl
andb %cl, %al
jne 0x2c7f
movq 0x18(%r12), %rax
addq %rbx, %rax
js 0x2c7c
pushq $0x48
popq %rdi
callq 0x2390
testq %rax, %rax
je 0x2c7c
movq %rax, %r15
cmpl $0x2, %ebp
jne 0x2cbc
movq %rbx, %rdi
callq 0x2390
movq %rax, 0x18(%r15)
testq %rax, %rax
je 0x2c7f
incl 0x14(%r12)
jmp 0x2cc1
xorl %r15d, %r15d
cmpl $0x2, %ebp
setne %bl
movq %r13, %rdi
callq 0x2370
testq %r15, %r15
sete %al
orb %bl, %al
jne 0x2ca0
movq 0x18(%r15), %rdi
callq 0x2040
movq %r15, %rdi
callq 0x2040
pushq $-0x1
popq %rbp
movl %ebp, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
incl 0x10(%r12)
andq $0x0, 0x40(%r15)
movq 0x48(%r12), %rcx
testq %rcx, %rcx
je 0x2ce2
movq %rcx, %rax
movq 0x40(%rcx), %rcx
testq %rcx, %rcx
jne 0x2cd0
leaq 0x40(%rax), %rcx
jmp 0x2ce9
leaq 0x48(%r12), %rcx
xorl %eax, %eax
movq %r15, (%rcx)
movq %rax, 0x38(%r15)
movq %r13, %rdi
callq 0x2370
andl $0x0, (%r15)
movq %rbx, 0x10(%r15)
movq %r14, 0x8(%r15)
xorps %xmm0, %xmm0
movups %xmm0, 0x28(%r15)
movl %ebp, 0x20(%r15)
cmpl $0x2, %ebp
jne 0x2d4c
movq 0x18(%r15), %rdi
movq (%rsp), %rsi
movq %rbx, %rdx
callq 0x2320
xorl %ebp, %ebp
testb $0x2, 0xc(%r12)
jne 0x2cab
movq (%r12), %rax
movl (%rax), %edi
pushq $0x3
popq %rcx
movq %r14, %rsi
movq %rbx, %rdx
callq 0x2250
jmp 0x2cab
movq (%rsp), %rax
movq %rax, 0x18(%r15)
movq (%r12), %rax
movl (%rax), %edi
pushq $0x3
popq %rcx
movq %r14, %rsi
movq %rbx, %rdx
callq 0x2250
xorl %ebp, %ebp
jmp 0x2cab