// File: /workspace/github_c_makefile_star2_O3/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, %r14d
movq %rcx, %r12
movq %rdx, %rbx
movq %rsi, (%rsp)
movq %rdi, %r15
leaq 0x20(%rdi), %r13
movq %r13, %rdi
callq 0x2590
cmpl $0x2, %r14d
sete %al
xorl %ebp, %ebp
testq %rbx, %rbx
je 0x2cf5
movb 0xd(%r15), %cl
andb $0x2, %cl
shrb %cl
andb %cl, %al
jne 0x2cf5
movq 0x18(%r15), %rax
addq %rbx, %rax
js 0x2cf3
movl $0x48, %edi
callq 0x2390
testq %rax, %rax
je 0x2cf3
movq %rax, %rbp
cmpl $0x2, %r14d
jne 0x2d1b
movq %rbx, %rdi
callq 0x2390
movq %rax, 0x18(%rbp)
testq %rax, %rax
je 0x2cf5
incl 0x14(%r15)
jmp 0x2d1f
xorl %ebp, %ebp
movq %r13, %rdi
callq 0x2370
movq %rbp, %rdi
callq 0x2040
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
movl %ebp, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
incl 0x10(%r15)
movq $0x0, 0x40(%rbp)
movq 0x48(%r15), %rcx
testq %rcx, %rcx
je 0x2d42
movq %rcx, %rax
movq 0x40(%rcx), %rcx
testq %rcx, %rcx
jne 0x2d30
leaq 0x40(%rax), %rcx
jmp 0x2d48
leaq 0x48(%r15), %rcx
xorl %eax, %eax
movq %rbp, (%rcx)
movq %rax, 0x38(%rbp)
movq %r13, %rdi
callq 0x2370
movq %rbx, 0x10(%rbp)
movq %r12, 0x8(%rbp)
movl $0x0, (%rbp)
xorps %xmm0, %xmm0
movups %xmm0, 0x28(%rbp)
movl %r14d, 0x20(%rbp)
cmpl $0x2, %r14d
jne 0x2dae
movq 0x18(%rbp), %rdi
movq (%rsp), %rsi
movq %rbx, %rdx
callq 0x2320
xorl %ebp, %ebp
testb $0x2, 0xc(%r15)
jne 0x2d0a
movq (%r15), %rax
movl (%rax), %edi
movq %r12, %rsi
movq %rbx, %rdx
movl $0x3, %ecx
callq 0x2250
jmp 0x2d0a
movq (%rsp), %rax
movq %rax, 0x18(%rbp)
movq (%r15), %rax
movl (%rax), %edi
movq %r12, %rsi
movq %rbx, %rdx
movl $0x3, %ecx
callq 0x2250
xorl %ebp, %ebp
jmp 0x2d0a