// File: /workspace/github_c_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rdx, %r15
movq %rsi, %r12
movq %rdi, %rbx
movq (%rsi), %r13
imulq %rdx, %r13
movq %r13, 0x18(%rsp)
leaq 0x14(%rsp), %rsi
callq 0xdebae
movq 0x18(%rbx), %rdi
leaq 0xff498(%rip), %rdx     # 0x1243be
pushq $0x2
popq %rcx
movq %r13, %rsi
callq 0xe4d48
testl %eax, %eax
je 0x24f50
leaq 0xe2362(%rip), %rsi     # 0x10729e
xorl %ebp, %ebp
movq %rbx, %rdi
movq %r13, %rdx
xorl %eax, %eax
callq 0xdf2a5
jmp 0x25282
pushq $0x38
popq %rsi
movq %rbx, %rdi
callq 0xdecaf
movq %rax, %r14
leaq 0xe235e(%rip), %rsi     # 0x1072c3
movq %rbx, %rdi
movq %r13, %rdx
xorl %eax, %eax
callq 0xde896
pushq $0x1
popq %rsi
movq %rbx, %rdi
callq 0xdeba7
addq $0x4, %r13
leaq 0x18(%rsp), %rsi
movq %r13, (%rsi)
movq 0x18(%rbx), %rdi
callq 0xe4071
movq %rax, %r13
leaq 0xe2342(%rip), %rsi     # 0x1072de
movq %rbx, %rdi
movl %r13d, %edx
xorl %eax, %eax
callq 0xde896
cmpq $0x1, %r15
jne 0x24fb4
movq %r13, 0x8(%r12)
movq 0x18(%rbx), %rdi
leaq 0x18(%rsp), %r13
movq %r13, %rsi
callq 0xe4071
movq %rax, (%r14)
movq (%r12), %rcx
imulq %rax, %rcx
movq %rcx, 0x8(%r14)
leaq 0xe2322(%rip), %rsi     # 0x1072fd
xorl %r15d, %r15d
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xde896
movq 0x18(%rbx), %rdi
movq %r13, %rsi
callq 0xe4071
movq %rax, 0x10(%r14)
movq (%r12), %rcx
imulq %rax, %rcx
movq %rcx, 0x18(%r14)
leaq 0xe231e(%rip), %rsi     # 0x10732c
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xde896
movq %rbx, %rdi
callq 0xeac74
movq %rax, 0x28(%r14)
movq 0x18(%rbx), %rdi
movq (%r13), %rsi
pushq $0x20
popq %r12
pushq $0x1
popq %rbp
movq %r12, %rdx
movq %rax, %rcx
movl %ebp, %r8d
movl %ebp, %r9d
callq 0xe4c9a
movq 0x28(%r14), %rdi
callq 0xeb7e4
leaq 0xe22fd(%rip), %rsi     # 0x107354
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xde896
movq (%r13), %rsi
addq $0x20, %rsi
movq %rsi, (%r13)
movq 0x18(%rbx), %rdi
movq %r12, %rdx
movq %r12, %rcx
movl %ebp, %r8d
movl %ebp, %r9d
callq 0xe495c
movq %rax, 0x30(%r14)
movq 0x18(%rax), %rdi
callq 0xeb7e4
leaq 0xe22d0(%rip), %rsi     # 0x107369
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xde896
addq $0x20, (%r13)
movq 0x18(%rbx), %rdi
movq %r13, %rsi
callq 0xe4071
movl %eax, 0x20(%r14)
leaq 0xe22bc(%rip), %rsi     # 0x10737e
movq %rbx, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xde896
movq 0x8(%r14), %rdx
movq 0x18(%rbx), %rax
movq 0x18(%rax), %rax
cmpq %rax, %rdx
jge 0x25167
movq 0x18(%r14), %rsi
leaq (%rsi,%rdx), %rcx
cmpq %rax, %rcx
jle 0x2510d
leaq 0xe22a0(%rip), %rsi     # 0x107397
movq %rbx, %rdi
xorl %eax, %eax
callq 0xdf05a
movq 0x18(%rbx), %rax
movq 0x18(%rax), %rsi
subq 0x8(%r14), %rsi
movq %rsi, 0x20(%rsp)
movq %rbx, %rdi
callq 0xdfd1a
movq %rax, %r13
movq %rbx, %rdi
callq 0xeac74
movq %rax, %r15
movq 0x30(%r14), %rax
movq 0x8(%rax), %r12
leaq 0xe2289(%rip), %rsi     # 0x1073c0
movq %r12, %rdi
callq 0x181a0
testl %eax, %eax
je 0x2516f
leaq 0xe228a(%rip), %rsi     # 0x1073d4
movq %r12, %rdi
callq 0x181a0
leaq 0xe2234(%rip), %rcx     # 0x10738d
movq %rcx, 0x8(%rsp)
testl %eax, %eax
je 0x25178
xorl %r12d, %r12d
jmp 0x251c0
xorl %r13d, %r13d
jmp 0x2524f
leaq 0x103367(%rip), %r12    # 0x1284dd
jmp 0x251e3
movq 0x18(%rbx), %rdi
movl $0x400, %esi            # imm = 0x400
addq 0x8(%r14), %rsi
callq 0xe3f46
movb $0x1, %r12b
cmpl $0x4244, %eax           # imm = 0x4244
je 0x251b4
cmpl $0x4858, %eax           # imm = 0x4858
je 0x251ab
cmpl $0x482b, %eax           # imm = 0x482B
jne 0x25162
leaq 0xe2235(%rip), %rax     # 0x1073de
jmp 0x251bb
leaq 0xe2235(%rip), %rax     # 0x1073e7
jmp 0x251bb
leaq 0xe2228(%rip), %rax     # 0x1073e3
movq %rax, 0x8(%rsp)
movq 0x28(%r14), %rdi
callq 0xeaf8a
testl %eax, %eax
je 0x251d9
movq 0x28(%r14), %rsi
movq %r15, %rdi
callq 0xead71
testb %r12b, %r12b
movq 0x8(%rsp), %r12
jne 0x2521d
movq 0x30(%r14), %rax
movq 0x18(%rax), %rdi
callq 0xeaf8a
testl %eax, %eax
je 0x2521d
cmpq $0x2, 0x10(%r15)
jl 0x2520d
leaq 0x10947c(%rip), %rsi    # 0x12e67e
pushq $0x5
popq %rdx
movq %r15, %rdi
callq 0xeae42
movq 0x30(%r14), %rax
movq 0x18(%rax), %rsi
movq %r15, %rdi
callq 0xead71
cmpq $0x2, 0x10(%r15)
jl 0x25234
movq %rbx, %rdi
movq %r13, %rsi
movq %r15, %rdx
xorl %ecx, %ecx
callq 0xdfd55
movq 0x18(%rbx), %rdi
movq 0x8(%r14), %rsi
movq 0x20(%rsp), %rdx
movq %r12, %rcx
movq %r13, %r8
xorl %r9d, %r9d
callq 0xe4e0b
movq %rbx, %rdi
movq %r13, %rsi
callq 0xdfd24
movq %r15, %rdi
callq 0xeaf3c
movq 0x28(%r14), %rdi
callq 0xeaf3c
movq 0x30(%r14), %rsi
movq %rbx, %rdi
callq 0xe4b94
movq %rbx, %rdi
movq %r14, %rsi
callq 0xded32
movl 0x14(%rsp), %esi
movq %rbx, %rdi
callq 0xdebb7
movl %ebp, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop