// File: /workspace/github_c_makefile_star2_O1/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 0x2590
cmpl $0x2, %ebp
sete %al
xorl %r15d, %r15d
testq %rbx, %rbx
je 0x2cea
movb 0xd(%r12), %cl
andb $0x2, %cl
shrb %cl
andb %cl, %al
jne 0x2cea
movq 0x18(%r12), %rax
addq %rbx, %rax
js 0x2ce7
movl $0x48, %edi
callq 0x2390
testq %rax, %rax
je 0x2ce7
movq %rax, %r15
cmpl $0x2, %ebp
jne 0x2d29
movq %rbx, %rdi
callq 0x2390
movq %rax, 0x18(%r15)
testq %rax, %rax
je 0x2cea
incl 0x14(%r12)
jmp 0x2d2e
xorl %r15d, %r15d
cmpl $0x2, %ebp
setne %bl
movq %r13, %rdi
callq 0x2370
testq %r15, %r15
sete %al
orb %bl, %al
jne 0x2d0b
movq 0x18(%r15), %rdi
callq 0x2040
movq %r15, %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(%r12)
movq $0x0, 0x40(%r15)
movq 0x48(%r12), %rcx
testq %rcx, %rcx
je 0x2d52
movq %rcx, %rax
movq 0x40(%rcx), %rcx
testq %rcx, %rcx
jne 0x2d40
leaq 0x40(%rax), %rcx
jmp 0x2d59
leaq 0x48(%r12), %rcx
xorl %eax, %eax
movq %r15, (%rcx)
movq %rax, 0x38(%r15)
movq %r13, %rdi
callq 0x2370
movq %rbx, 0x10(%r15)
movq %r14, 0x8(%r15)
movl $0x0, (%r15)
xorps %xmm0, %xmm0
movups %xmm0, 0x28(%r15)
movl %ebp, 0x20(%r15)
cmpl $0x2, %ebp
jne 0x2dc1
movq 0x18(%r15), %rdi
movq (%rsp), %rsi
movq %rbx, %rdx
callq 0x2320
xorl %ebp, %ebp
testb $0x2, 0xc(%r12)
jne 0x2d18
movq (%r12), %rax
movl (%rax), %edi
movq %r14, %rsi
movq %rbx, %rdx
movl $0x3, %ecx
callq 0x2250
jmp 0x2d18
movq (%rsp), %rax
movq %rax, 0x18(%r15)
movq (%r12), %rax
movl (%rax), %edi
movq %r14, %rsi
movq %rbx, %rdx
movl $0x3, %ecx
callq 0x2250
xorl %ebp, %ebp
jmp 0x2d18