// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/apm.c
// Function: do_entry_at_sector
static int do_entry_at_sector(deark *c, lctx *d, i64 secnum)
{
	i64 pos = secnum*d->secsize;
	i64 npartitions;
	int retval = 0;
	int saved_indent_level;
	struct partition_info *pi = NULL;

	de_dbg_indent_save(c, &saved_indent_level);

	if(dbuf_memcmp(c->infile, pos, "PM", 2)) {
		de_err(c, "Partition map entry not found at %"I64_FMT, pos);
		goto done;
	}

	pi = de_malloc(c, sizeof(struct partition_info));

	de_dbg(c, "partition map entry at %"I64_FMT, pos);
	de_dbg_indent(c, 1);
	pos += 4;

	npartitions = de_getu32be_p(&pos);
	de_dbg(c, "total number of partitions: %d", (int)npartitions);
	if(secnum==1) {
		d->npartitions = npartitions;
	}
	retval = 1;

	pi->partition_startsec = de_getu32be_p(&pos);
	pi->partition_startpos = pi->partition_startsec * d->secsize;
	de_dbg(c, "starting sector of partition: %"I64_FMT" (offset %"I64_FMT")",
		pi->partition_startsec, pi->partition_startpos);

	pi->partition_size_in_secs = de_getu32be_p(&pos);
	pi->partition_size_in_bytes = pi->partition_size_in_secs * d->secsize;
	de_dbg(c, "partition size: %"I64_FMT" sectors (%"I64_FMT" bytes)",
		pi->partition_size_in_secs, pi->partition_size_in_bytes);

	pi->pname = ucstring_create(c);
	dbuf_read_to_ucstring(c->infile, pos, 32, pi->pname, DE_CONVFLAG_STOP_AT_NUL,
		DE_ENCODING_ASCII);
	de_dbg(c, "partition name: \"%s\"", ucstring_getpsz(pi->pname));
	pos += 32;

	pi->ptype = dbuf_read_string(c->infile, pos, 32, 32, DE_CONVFLAG_STOP_AT_NUL,
		DE_ENCODING_ASCII);
	de_dbg(c, "partition type: \"%s\"", ucstring_getpsz(pi->ptype->str));
	pos += 32;

	pi->partition_status = (unsigned int)de_getu32be_p(&pos);
	de_dbg(c, "status: 0x%08x", (unsigned int)pi->partition_status);

	// TODO: More fields here

	do_extract_partition(c, d, pi);

done:
	if(pi) destroy_partition_info(c, pi);
	de_dbg_indent_restore(c, saved_indent_level);
	return retval;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq -0x18(%rbp), %rax
movq -0x10(%rbp), %rcx
imulq (%rcx), %rax
movq %rax, -0x20(%rbp)
movl $0x0, -0x2c(%rbp)
movq $0x0, -0x38(%rbp)
movq -0x8(%rbp), %rdi
leaq -0x30(%rbp), %rsi
callq 0x15b580
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x20(%rbp), %rsi
leaq 0x18eb8d(%rip), %rdx    # 0x1bd0e0
movl $0x2, %ecx
callq 0x166ab0
cmpl $0x0, %eax
je 0x2e57d
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rdx
leaq 0x171b72(%rip), %rsi    # 0x1a00e3
movb $0x0, %al
callq 0x15bfb0
jmp 0x2e7cb
movq -0x8(%rbp), %rdi
movl $0x38, %esi
callq 0x15b780
movq %rax, -0x38(%rbp)
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rdx
leaq 0x171b6a(%rip), %rsi    # 0x1a0108
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movq -0x20(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x20(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x20(%rbp), %rsi
callq 0x1654b0
movq %rax, -0x28(%rbp)
movq -0x8(%rbp), %rdi
movq -0x28(%rbp), %rax
movl %eax, %edx
leaq 0x171b3e(%rip), %rsi    # 0x1a0123
movb $0x0, %al
callq 0x15b0d0
cmpq $0x1, -0x18(%rbp)
jne 0x2e5ff
movq -0x28(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x8(%rax)
movl $0x1, -0x2c(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x20(%rbp), %rsi
callq 0x1654b0
movq %rax, %rcx
movq -0x38(%rbp), %rax
movq %rcx, (%rax)
movq -0x38(%rbp), %rax
movq (%rax), %rcx
movq -0x10(%rbp), %rax
imulq (%rax), %rcx
movq -0x38(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x8(%rbp), %rdi
movq -0x38(%rbp), %rax
movq (%rax), %rdx
movq -0x38(%rbp), %rax
movq 0x8(%rax), %rcx
leaq 0x171af0(%rip), %rsi    # 0x1a0142
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x20(%rbp), %rsi
callq 0x1654b0
movq %rax, %rcx
movq -0x38(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x38(%rbp), %rax
movq 0x10(%rax), %rcx
movq -0x10(%rbp), %rax
imulq (%rax), %rcx
movq -0x38(%rbp), %rax
movq %rcx, 0x18(%rax)
movq -0x8(%rbp), %rdi
movq -0x38(%rbp), %rax
movq 0x10(%rax), %rdx
movq -0x38(%rbp), %rax
movq 0x18(%rax), %rcx
leaq 0x171ac9(%rip), %rsi    # 0x1a0171
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
callq 0x1711d0
movq %rax, %rcx
movq -0x38(%rbp), %rax
movq %rcx, 0x28(%rax)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x20(%rbp), %rsi
movq -0x38(%rbp), %rax
movq 0x28(%rax), %rcx
movl $0x20, %edx
movl $0x1, %r9d
movl %r9d, %r8d
callq 0x1669f0
movq -0x8(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0x38(%rbp), %rax
movq 0x28(%rax), %rdi
callq 0x172500
movq -0x48(%rbp), %rdi
movq %rax, %rdx
leaq 0x171a8c(%rip), %rsi    # 0x1a0199
movb $0x0, %al
callq 0x15b0d0
movq -0x20(%rbp), %rax
addq $0x20, %rax
movq %rax, -0x20(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x20(%rbp), %rsi
movl $0x20, %ecx
movl $0x1, %r9d
movq %rcx, %rdx
movl %r9d, %r8d
callq 0x1664c0
movq %rax, %rcx
movq -0x38(%rbp), %rax
movq %rcx, 0x30(%rax)
movq -0x8(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0x38(%rbp), %rax
movq 0x30(%rax), %rax
movq 0x18(%rax), %rdi
callq 0x172500
movq -0x40(%rbp), %rdi
movq %rax, %rdx
leaq 0x171a3a(%rip), %rsi    # 0x1a01ae
movb $0x0, %al
callq 0x15b0d0
movq -0x20(%rbp), %rax
addq $0x20, %rax
movq %rax, -0x20(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x20(%rbp), %rsi
callq 0x1654b0
movl %eax, %ecx
movq -0x38(%rbp), %rax
movl %ecx, 0x20(%rax)
movq -0x8(%rbp), %rdi
movq -0x38(%rbp), %rax
movl 0x20(%rax), %edx
leaq 0x171a10(%rip), %rsi    # 0x1a01c3
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x38(%rbp), %rdx
callq 0x2e800
cmpq $0x0, -0x38(%rbp)
je 0x2e7df
movq -0x8(%rbp), %rdi
movq -0x38(%rbp), %rsi
callq 0x2ea70
movq -0x8(%rbp), %rdi
movl -0x30(%rbp), %esi
callq 0x15b5a0
movl -0x2c(%rbp), %eax
addq $0x50, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)