// File: /workspace/github_c_makefile_star2_O1/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
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
cmpq $0x1c, %rsi
jb 0x4549
movq (%rdi), %rax
movzwl %ax, %ecx
cmpl $0x100, %ecx            # imm = 0x100
jne 0x4549
movq %rdx, %rbx
movq %rax, %rcx
shrq $0x20, %rcx
shrl $0x10, %eax
rolw $0x8, %ax
bswapl %ecx
movl %ecx, 0x8(%rdx)
movzwl %ax, %eax
movl %eax, 0xc(%rdx)
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%rdx)
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
cmpq $0x14, %rsi
jl 0x4528
movq %rsi, 0x10(%rsp)
leaq (%rdi,%rsi), %rbp
leaq 0x14(%rdi), %r14
movq %rdi, 0x8(%rsp)
leaq 0x8(%rdi), %r13
movl (%r13), %r15d
movq 0x4(%r13), %rdi
callq 0x2280
movq %rax, %r12
testl %r15d, %r15d
sete %al
testq %r12, %r12
sete %cl
testb %cl, %al
jne 0x455a
bswapl %r15d
leaq (%r14,%r15), %r13
cmpq %rbp, %r13
ja 0x4522
movl $0x48, %edi
callq 0x2390
testq %rax, %rax
je 0x4522
movq %r15, 0x10(%rax)
movq %r12, 0x8(%rax)
movl $0x2, 0x20(%rax)
movq %r14, 0x18(%rax)
movq $0x0, 0x30(%rax)
movq 0x48(%rbx), %rcx
testq %rcx, %rcx
je 0x4503
movq %rcx, %rdx
movq 0x40(%rcx), %rcx
testq %rcx, %rcx
jne 0x44e5
movq %rax, 0x40(%rdx)
movq %rdx, 0x38(%rax)
movq $0x0, 0x40(%rax)
jmp 0x450e
movq %rax, 0x48(%rbx)
xorps %xmm0, %xmm0
movups %xmm0, 0x38(%rax)
incl 0x14(%rbx)
addq %r15, 0x18(%rbx)
leaq 0xc(%r13), %r14
cmpq %rbp, %r14
jbe 0x4482
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
movq 0x48(%rbx), %rdi
testq %rdi, %rdi
je 0x4546
movq 0x40(%rdi), %r14
callq 0x2040
movq %r14, 0x48(%rbx)
movq %r14, %rdi
testq %r14, %r14
jne 0x4531
movl %r15d, %ebp
movl %ebp, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
addq $0x14, %r13
cmpq %rbp, %r13
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
ja 0x4528
movq (%r14), %r14
movl %r14d, %eax
bswapl %eax
cmpl 0x14(%rbx), %eax
movq 0x10(%rsp), %rdx
movq 0x8(%rsp), %rsi
jne 0x4528
shrq $0x20, %r14
bswapl %r14d
addq $-0x8, %rdx
xorl %ebp, %ebp
xorl %edi, %edi
callq 0x2230
movl $0xfffffffe, %r15d      # imm = 0xFFFFFFFE
cmpl %r14d, %eax
jne 0x4528
jmp 0x4549
nopl (%rax)