// File: /workspace/github_c_makefile_star2_O2/albertito[P]libjio/libjio/journal.c
// Function: fill_trans
int fill_trans(unsigned char *map, off_t len, struct jtrans *ts)
{
	int rv;
	unsigned char *p;
	struct operation *op, *tmp;
	struct on_disk_hdr hdr;
	struct on_disk_ophdr ophdr;
	struct on_disk_trailer trailer;

	rv = -1;

	if (len < sizeof(hdr) + sizeof(ophdr) + sizeof(trailer))
		return -1;

	p = map;

	memcpy(&hdr, p, sizeof(hdr));
	p += sizeof(hdr);

	hdr_ntoh(&hdr);
	if (hdr.ver != 1)
		return -1;

	ts->id = hdr.trans_id;
	ts->flags = hdr.flags;
	ts->numops_r = 0;
	ts->numops_w = 0;
	ts->len_w = 0;

	for (;;) {
		if (p + sizeof(ophdr) > map + len)
			goto error;

		memcpy(&ophdr, p,  sizeof(ophdr));
		p += sizeof(ophdr);

		ophdr_ntoh(&ophdr);

		if (ophdr.len == 0 && ophdr.offset == 0) {
			/* This header marks the end of the operations */
			break;
		}

		if (p + ophdr.len > map + len)
			goto error;

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

		op->len = ophdr.len;
		op->offset = ophdr.offset;
		op->direction = D_WRITE;

		op->buf = (void *) p;
		p += op->len;

		op->pdata = NULL;

		if (ts->op == NULL) {
			ts->op = op;
			op->prev = NULL;
			op->next = NULL;
		} else {
			for (tmp = ts->op; tmp->next != NULL; tmp = tmp->next)
				;
			tmp->next = op;
			op->prev = tmp;
			op->next = NULL;
		}

		ts->numops_w++;
		ts->len_w += op->len;
	}

	if (p + sizeof(trailer) > map + len)
		goto error;

	memcpy(&trailer, p, sizeof(trailer));
	p += sizeof(trailer);

	trailer_ntoh(&trailer);

	if (trailer.numops != ts->numops_w)
		goto error;

	if (checksum_buf(0, map, len - sizeof(trailer)) != trailer.checksum) {
		rv = -2;
		goto error;
	}

	return 0;

error:
	while (ts->op != NULL) {
		tmp = ts->op->next;
		free(ts->op);
		ts->op = tmp;
	}
	return rv;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
pushq $-0x1
popq %rbp
cmpq $0x1c, %rsi
jb 0x43af
movq (%rdi), %rax
cmpw $0x100, %ax             # imm = 0x100
jne 0x43af
movq %rdx, %rbx
movq %rax, %rcx
shrq $0x20, %rcx
bswapl %ecx
shrl $0x10, %eax
rolw $0x8, %ax
leaq 0x8(%rdi), %r12
movl %ecx, 0x8(%rdx)
movzwl %ax, %eax
movl %eax, 0xc(%rdx)
andq $0x0, 0x10(%rdx)
movq %rsi, 0x10(%rsp)
movq %rdi, 0x8(%rsp)
leaq (%rdi,%rsi), %r15
xorl %ebp, %ebp
movq %rbp, 0x18(%rbx)
leaq 0xc(%r12), %r14
cmpq %r15, %r14
ja 0x4355
movl (%r12), %ebp
movq 0x4(%r12), %rdi
callq 0x2280
movq %rax, %r13
testl %ebp, %ebp
sete %al
testq %r13, %r13
sete %cl
testb %cl, %al
jne 0x4373
bswapl %ebp
leaq (%r14,%rbp), %r12
cmpq %r15, %r12
ja 0x4355
pushq $0x48
popq %rdi
callq 0x2390
testq %rax, %rax
je 0x4355
movq %rbp, 0x10(%rax)
movq %r13, 0x8(%rax)
movl $0x2, 0x20(%rax)
movq %r14, 0x18(%rax)
andq $0x0, 0x30(%rax)
movq 0x48(%rbx), %rcx
testq %rcx, %rcx
je 0x433e
movq %rcx, %rdx
movq 0x40(%rcx), %rcx
testq %rcx, %rcx
jne 0x4323
movq %rax, 0x40(%rdx)
movq %rdx, 0x38(%rax)
andq $0x0, 0x40(%rax)
jmp 0x4349
movq %rax, 0x48(%rbx)
xorps %xmm0, %xmm0
movups %xmm0, 0x38(%rax)
incl 0x14(%rbx)
addq 0x18(%rbx), %rbp
jmp 0x42b4
pushq $-0x1
popq %rbp
movq 0x48(%rbx), %rdi
testq %rdi, %rdi
je 0x43af
movq 0x40(%rdi), %r14
callq 0x2040
movq %r14, 0x48(%rbx)
movq %r14, %rdi
jmp 0x435c
addq $0x14, %r12
cmpq %r15, %r12
pushq $-0x1
popq %rbp
ja 0x4358
movq (%r14), %r14
movl %r14d, %eax
bswapl %eax
cmpl 0x14(%rbx), %eax
jne 0x4358
shrq $0x20, %r14
bswapl %r14d
movq 0x10(%rsp), %rdx
addq $-0x8, %rdx
xorl %ebp, %ebp
xorl %edi, %edi
movq 0x8(%rsp), %rsi
callq 0x2230
cmpl %r14d, %eax
jne 0x43c0
movl %ebp, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
pushq $-0x2
jmp 0x4357