// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movl $0xffffffff, -0x24(%rbp) # imm = 0xFFFFFFFF
cmpq $0x1c, -0x18(%rbp)
jae 0x59be
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x5c55
movq -0x10(%rbp), %rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x48(%rbp)
movq -0x30(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x30(%rbp)
leaq -0x48(%rbp), %rdi
callq 0x5c60
movzwl -0x48(%rbp), %eax
cmpl $0x1, %eax
je 0x59fb
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x5c55
movl -0x44(%rbp), %ecx
movq -0x20(%rbp), %rax
movl %ecx, 0x8(%rax)
movzwl -0x46(%rbp), %ecx
movq -0x20(%rbp), %rax
movl %ecx, 0xc(%rax)
movq -0x20(%rbp), %rax
movl $0x0, 0x10(%rax)
movq -0x20(%rbp), %rax
movl $0x0, 0x14(%rax)
movq -0x20(%rbp), %rax
movq $0x0, 0x18(%rax)
movq -0x30(%rbp), %rax
addq $0xc, %rax
movq -0x10(%rbp), %rcx
addq -0x18(%rbp), %rcx
cmpq %rcx, %rax
jbe 0x5a4c
jmp 0x5c17
movq -0x30(%rbp), %rax
movq (%rax), %rcx
movq %rcx, -0x54(%rbp)
movl 0x8(%rax), %eax
movl %eax, -0x4c(%rbp)
movq -0x30(%rbp), %rax
addq $0xc, %rax
movq %rax, -0x30(%rbp)
leaq -0x54(%rbp), %rdi
callq 0x5cc0
cmpl $0x0, -0x54(%rbp)
jne 0x5a84
cmpq $0x0, -0x50(%rbp)
jne 0x5a84
jmp 0x5ba8
movq -0x30(%rbp), %rax
movl -0x54(%rbp), %ecx
addq %rcx, %rax
movq -0x10(%rbp), %rcx
addq -0x18(%rbp), %rcx
cmpq %rcx, %rax
jbe 0x5aa0
jmp 0x5c17
movl $0x48, %edi
callq 0x23c0
movq %rax, -0x38(%rbp)
cmpq $0x0, -0x38(%rbp)
jne 0x5aba
jmp 0x5c17
movl -0x54(%rbp), %eax
movl %eax, %ecx
movq -0x38(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x50(%rbp), %rcx
movq -0x38(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x38(%rbp), %rax
movl $0x2, 0x20(%rax)
movq -0x30(%rbp), %rcx
movq -0x38(%rbp), %rax
movq %rcx, 0x18(%rax)
movq -0x38(%rbp), %rax
movq 0x10(%rax), %rax
addq -0x30(%rbp), %rax
movq %rax, -0x30(%rbp)
movq -0x38(%rbp), %rax
movq $0x0, 0x30(%rax)
movq -0x20(%rbp), %rax
cmpq $0x0, 0x48(%rax)
jne 0x5b37
movq -0x38(%rbp), %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x48(%rax)
movq -0x38(%rbp), %rax
movq $0x0, 0x38(%rax)
movq -0x38(%rbp), %rax
movq $0x0, 0x40(%rax)
jmp 0x5b82
movq -0x20(%rbp), %rax
movq 0x48(%rax), %rax
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rax
cmpq $0x0, 0x40(%rax)
je 0x5b5e
jmp 0x5b50
movq -0x40(%rbp), %rax
movq 0x40(%rax), %rax
movq %rax, -0x40(%rbp)
jmp 0x5b43
movq -0x38(%rbp), %rcx
movq -0x40(%rbp), %rax
movq %rcx, 0x40(%rax)
movq -0x40(%rbp), %rcx
movq -0x38(%rbp), %rax
movq %rcx, 0x38(%rax)
movq -0x38(%rbp), %rax
movq $0x0, 0x40(%rax)
movq -0x20(%rbp), %rax
movl 0x14(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x14(%rax)
movq -0x38(%rbp), %rax
movq 0x10(%rax), %rcx
movq -0x20(%rbp), %rax
addq 0x18(%rax), %rcx
movq %rcx, 0x18(%rax)
jmp 0x5a32
movq -0x30(%rbp), %rax
addq $0x8, %rax
movq -0x10(%rbp), %rcx
addq -0x18(%rbp), %rcx
cmpq %rcx, %rax
jbe 0x5bbf
jmp 0x5c17
movq -0x30(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x5c(%rbp)
movq -0x30(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x30(%rbp)
leaq -0x5c(%rbp), %rdi
callq 0x5d00
movl -0x5c(%rbp), %eax
movq -0x20(%rbp), %rcx
cmpl 0x14(%rcx), %eax
je 0x5bed
jmp 0x5c17
movq -0x10(%rbp), %rsi
movq -0x18(%rbp), %rdx
subq $0x8, %rdx
xorl %edi, %edi
callq 0x2250
cmpl -0x58(%rbp), %eax
je 0x5c0e
movl $0xfffffffe, -0x24(%rbp) # imm = 0xFFFFFFFE
jmp 0x5c17
movl $0x0, -0x4(%rbp)
jmp 0x5c55
jmp 0x5c19
movq -0x20(%rbp), %rax
cmpq $0x0, 0x48(%rax)
je 0x5c4f
movq -0x20(%rbp), %rax
movq 0x48(%rax), %rax
movq 0x40(%rax), %rax
movq %rax, -0x40(%rbp)
movq -0x20(%rbp), %rax
movq 0x48(%rax), %rdi
callq 0x2040
movq -0x40(%rbp), %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x48(%rax)
jmp 0x5c19
movl -0x24(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x60, %rsp
popq %rbp
retq
nop