// File: /workspace/github_c_makefile_star2_O3/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, %r13
movq %rdi, %rbx
movq (%rsi), %r12
imulq %rdx, %r12
movq %r12, 0x18(%rsp)
leaq 0x14(%rsp), %rsi
callq 0xe8a1c
movq 0x18(%rbx), %rdi
leaq 0x10be8c(%rip), %rdx    # 0x1316ae
movl $0x2, %ecx
movq %r12, %rsi
callq 0xefc85
testl %eax, %eax
je 0x2584e
leaq 0xeeac4(%rip), %rsi     # 0x1142fe
xorl %ebp, %ebp
movq %rbx, %rdi
movq %r12, %rdx
xorl %eax, %eax
callq 0xe9295
jmp 0x25b95
movl $0x38, %esi
movq %rbx, %rdi
callq 0xe8b2b
movq %rax, %r14
leaq 0xeeabe(%rip), %rsi     # 0x114323
movq %rbx, %rdi
movq %r12, %rdx
xorl %eax, %eax
callq 0xe8617
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe8a15
addq $0x4, %r12
leaq 0x18(%rsp), %rsi
movq %r12, (%rsi)
movq 0x18(%rbx), %rdi
callq 0xeed31
movq %rax, %r12
leaq 0xeeaa0(%rip), %rsi     # 0x11433e
movq %rbx, %rdi
movl %r12d, %edx
xorl %eax, %eax
callq 0xe8617
cmpq $0x1, %r15
jne 0x258b5
movq %r12, 0x8(%r13)
movq 0x18(%rbx), %rdi
leaq 0x18(%rsp), %r12
movq %r12, %rsi
callq 0xeed31
movq %rax, (%r14)
movq (%r13), %rcx
imulq %rax, %rcx
movq %rcx, 0x8(%r14)
leaq 0xeea81(%rip), %rsi     # 0x11435d
xorl %r15d, %r15d
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xe8617
movq 0x18(%rbx), %rdi
movq %r12, %rsi
callq 0xeed31
movq %rax, 0x10(%r14)
movq (%r13), %rcx
imulq %rax, %rcx
movq %rcx, 0x18(%r14)
leaq 0xeea7d(%rip), %rsi     # 0x11438c
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xe8617
movq %rbx, %rdi
callq 0xf6410
movq %rax, 0x28(%r14)
movq 0x18(%rbx), %rdi
movq (%r12), %rsi
movl $0x1, %ebp
movl $0x20, %edx
movq %rax, %rcx
movl $0x1, %r8d
movl $0x1, %r9d
callq 0xefbd7
movq 0x28(%r14), %rdi
callq 0xf705c
leaq 0xeea56(%rip), %rsi     # 0x1143b4
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xe8617
movq (%r12), %rsi
addq $0x20, %rsi
movq %rsi, (%r12)
movq 0x18(%rbx), %rdi
movl $0x20, %edx
movl $0x20, %ecx
movl $0x1, %r8d
movl $0x1, %r9d
callq 0xef87c
movq %rax, 0x30(%r14)
movq 0x18(%rax), %rdi
callq 0xf705c
leaq 0xeea1f(%rip), %rsi     # 0x1143c9
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xe8617
addq $0x20, (%r12)
movq 0x18(%rbx), %rdi
movq %r12, %rsi
callq 0xeed31
movl %eax, 0x20(%r14)
leaq 0xeea0b(%rip), %rsi     # 0x1143de
movq %rbx, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xe8617
movq 0x8(%r14), %rdx
movq 0x18(%rbx), %rax
movq 0x18(%rax), %rax
cmpq %rax, %rdx
jge 0x25a78
movq 0x18(%r14), %rsi
leaq (%rsi,%rdx), %rcx
cmpq %rax, %rcx
jle 0x25a1e
leaq 0xee9ef(%rip), %rsi     # 0x1143f7
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8faa
movq 0x18(%rbx), %rax
movq 0x18(%rax), %rsi
subq 0x8(%r14), %rsi
movq %rsi, 0x20(%rsp)
movq %rbx, %rdi
callq 0xea504
movq %rax, %r13
movq %rbx, %rdi
callq 0xf6410
movq %rax, %r15
movq 0x30(%r14), %rax
movq 0x8(%rax), %r12
leaq 0xee9d8(%rip), %rsi     # 0x114420
movq %r12, %rdi
callq 0x181a0
testl %eax, %eax
je 0x25a80
leaq 0xee9d9(%rip), %rsi     # 0x114434
movq %r12, %rdi
callq 0x181a0
leaq 0xee983(%rip), %rcx     # 0x1143ed
movq %rcx, 0x8(%rsp)
testl %eax, %eax
je 0x25a89
xorl %r12d, %r12d
jmp 0x25ad1
xorl %r13d, %r13d
jmp 0x25b62
leaq 0x10fd36(%rip), %r12    # 0x1357bd
jmp 0x25af4
movq 0x18(%rbx), %rdi
movl $0x400, %esi            # imm = 0x400
addq 0x8(%r14), %rsi
callq 0xeeb67
movb $0x1, %r12b
cmpl $0x4244, %eax           # imm = 0x4244
je 0x25ac5
cmpl $0x4858, %eax           # imm = 0x4858
je 0x25abc
cmpl $0x482b, %eax           # imm = 0x482B
jne 0x25a73
leaq 0xee984(%rip), %rax     # 0x11443e
jmp 0x25acc
leaq 0xee984(%rip), %rax     # 0x114447
jmp 0x25acc
leaq 0xee977(%rip), %rax     # 0x114443
movq %rax, 0x8(%rsp)
movq 0x28(%r14), %rdi
callq 0xf67a3
testl %eax, %eax
je 0x25aea
movq 0x28(%r14), %rsi
movq %r15, %rdi
callq 0xf6509
testb %r12b, %r12b
movq 0x8(%rsp), %r12
jne 0x25b30
movq 0x30(%r14), %rax
movq 0x18(%rax), %rdi
callq 0xf67a3
testl %eax, %eax
je 0x25b30
cmpq $0x2, 0x10(%r15)
jl 0x25b20
leaq 0x115e5b(%rip), %rsi    # 0x13b96e
movq %r15, %rdi
movl $0x5, %edx
callq 0xf65e3
movq 0x30(%r14), %rax
movq 0x18(%rax), %rsi
movq %r15, %rdi
callq 0xf6509
cmpq $0x2, 0x10(%r15)
jl 0x25b47
movq %rbx, %rdi
movq %r13, %rsi
movq %r15, %rdx
xorl %ecx, %ecx
callq 0xea53f
movq 0x18(%rbx), %rdi
movq 0x8(%r14), %rsi
movq 0x20(%rsp), %rdx
movq %r12, %rcx
movq %r13, %r8
xorl %r9d, %r9d
callq 0xefd48
movq %rbx, %rdi
movq %r13, %rsi
callq 0xea50e
movq %r15, %rdi
callq 0xf6753
movq 0x28(%r14), %rdi
callq 0xf6753
movq 0x30(%r14), %rsi
movq %rbx, %rdi
callq 0xefaca
movq %rbx, %rdi
movq %r14, %rsi
callq 0xe8be2
movl 0x14(%rsp), %esi
movq %rbx, %rdi
callq 0xe8a25
movl %ebp, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop