// File: /workspace/github_c_makefile_star2_O1/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, %r12
movq %rsi, %r15
movq %rdi, %rbx
movq (%rsi), %rax
imulq %rdx, %rax
movq %rax, 0x8(%rsp)
leaq 0x14(%rsp), %rsi
callq 0xe958c
movq 0x18(%rbx), %rdi
movq 0x8(%rsp), %rsi
leaq 0x10bae3(%rip), %rdx    # 0x131351
movl $0x2, %ecx
callq 0xf025e
testl %eax, %eax
je 0x2589d
movq 0x8(%rsp), %rdx
leaq 0xee9c6(%rip), %rsi     # 0x11424e
xorl %r15d, %r15d
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9df7
xorl %r14d, %r14d
jmp 0x25bc8
movl $0x38, %esi
movq %rbx, %rdi
callq 0xe969b
movq %rax, %r14
leaq 0x8(%rsp), %r13
movq (%r13), %rdx
leaq 0xee9b6(%rip), %rsi     # 0x114273
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9226
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe9585
addq $0x4, (%r13)
movq 0x18(%rbx), %rdi
movq %r13, %rsi
callq 0xef34e
movq %rax, %r13
leaq 0xee99f(%rip), %rsi     # 0x11428e
movq %rbx, %rdi
movl %r13d, %edx
xorl %eax, %eax
callq 0xe9226
cmpq $0x1, %r12
jne 0x25906
movq %r13, 0x8(%r15)
movq 0x18(%rbx), %rdi
leaq 0x8(%rsp), %r13
movq %r13, %rsi
callq 0xef34e
movq %rax, (%r14)
movq (%r15), %rcx
imulq %rax, %rcx
movq %rcx, 0x8(%r14)
leaq 0xee981(%rip), %rsi     # 0x1142ad
xorl %r12d, %r12d
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xe9226
movq 0x18(%rbx), %rdi
movq %r13, %rsi
callq 0xef34e
movq %rax, 0x10(%r14)
movq (%r15), %rcx
imulq %rax, %rcx
movq %rcx, 0x18(%r14)
leaq 0xee97e(%rip), %rsi     # 0x1142dc
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xe9226
movq %rbx, %rdi
callq 0xf6870
movq %rax, 0x28(%r14)
movq 0x18(%rbx), %rdi
movq (%r13), %rsi
movl $0x1, %r15d
movl $0x20, %edx
movq %rax, %rcx
movl $0x1, %r8d
movl $0x1, %r9d
callq 0xf01b0
movq 0x28(%r14), %rdi
callq 0xf74bd
leaq 0xee956(%rip), %rsi     # 0x114304
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xe9226
movq (%r13), %rsi
addq $0x20, %rsi
movq %rsi, (%r13)
movq 0x18(%rbx), %rdi
movl $0x20, %edx
movl $0x20, %ecx
movl $0x1, %r8d
movl $0x1, %r9d
callq 0xefe67
movq %rax, 0x30(%r14)
movq 0x18(%rax), %rdi
callq 0xf74bd
leaq 0xee91f(%rip), %rsi     # 0x114319
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xe9226
addq $0x20, (%r13)
movq 0x18(%rbx), %rdi
movq %r13, %rsi
callq 0xef34e
movl %eax, 0x20(%r14)
leaq 0xee90b(%rip), %rsi     # 0x11432e
movq %rbx, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xe9226
movq 0x8(%r14), %rdx
movq 0x18(%rbx), %rax
movq 0x18(%rax), %rax
cmpq %rax, %rdx
jge 0x25b05
movq 0x18(%r14), %rsi
leaq (%rsi,%rdx), %rcx
cmpq %rax, %rcx
jle 0x25a6e
leaq 0xee8ef(%rip), %rsi     # 0x114347
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9b27
movq 0x18(%rbx), %rax
movq 0x18(%rax), %rsi
subq 0x8(%r14), %rsi
movq %rsi, 0x20(%rsp)
movq %rbx, %rdi
callq 0xea9a9
movq %rax, %rbp
movq %rbx, %rdi
callq 0xf6870
movq %rax, 0x18(%rsp)
movq 0x30(%r14), %rax
movq 0x8(%rax), %r12
leaq 0xee8d6(%rip), %rsi     # 0x114370
movq %r12, %rdi
callq 0x181a0
movl %eax, %r13d
testl %eax, %eax
je 0x25b0c
leaq 0xee8d4(%rip), %rsi     # 0x114384
movq %r12, %rdi
callq 0x181a0
leaq 0xee87e(%rip), %rcx     # 0x11433d
movq %rcx, (%rsp)
testl %eax, %eax
jne 0x25b17
movq 0x18(%rbx), %rdi
movl $0x400, %esi            # imm = 0x400
addq 0x8(%r14), %rsi
callq 0xef169
movb $0x1, %r12b
cmpl $0x4244, %eax           # imm = 0x4244
je 0x25c14
cmpl $0x4858, %eax           # imm = 0x4858
je 0x25c0b
cmpl $0x482b, %eax           # imm = 0x482B
jne 0x25b17
leaq 0xee88e(%rip), %rax     # 0x11438e
jmp 0x25c1b
xorl %ebp, %ebp
jmp 0x25bb5
leaq 0x10f91e(%rip), %rax    # 0x135431
movq %rax, (%rsp)
xorl %r12d, %r12d
testl %r13d, %r13d
je 0x25b3a
movq 0x28(%r14), %rdi
callq 0xf6c00
testl %eax, %eax
je 0x25b3a
movq 0x28(%r14), %rsi
movq 0x18(%rsp), %rdi
callq 0xf6969
testb %r12b, %r12b
movq 0x18(%rsp), %r12
jne 0x25b81
movq 0x30(%r14), %rax
movq 0x18(%rax), %rdi
callq 0xf6c00
testl %eax, %eax
je 0x25b81
cmpq $0x2, 0x10(%r12)
jl 0x25b71
leaq 0x115a52(%rip), %rsi    # 0x13b5b6
movq %r12, %rdi
movl $0x5, %edx
callq 0xf6a40
movq 0x30(%r14), %rax
movq 0x18(%rax), %rsi
movq %r12, %rdi
callq 0xf6969
cmpq $0x2, 0x10(%r12)
jl 0x25b99
movq %rbx, %rdi
movq %rbp, %rsi
movq %r12, %rdx
xorl %ecx, %ecx
callq 0xea9e4
movq 0x18(%rbx), %rdi
movq 0x8(%r14), %rsi
movq 0x20(%rsp), %rdx
movq (%rsp), %rcx
movq %rbp, %r8
xorl %r9d, %r9d
callq 0xf0321
movq %rbx, %rdi
movq %rbp, %rsi
callq 0xea9b3
movq %r12, %rdi
callq 0xf6bb0
testq %r14, %r14
je 0x25bed
movq 0x28(%r14), %rdi
callq 0xf6bb0
movq 0x30(%r14), %rsi
movq %rbx, %rdi
callq 0xf00aa
movq %rbx, %rdi
movq %r14, %rsi
callq 0xe9752
movl 0x14(%rsp), %esi
movq %rbx, %rdi
callq 0xe9595
movl %r15d, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xee785(%rip), %rax     # 0x114397
jmp 0x25c1b
leaq 0xee778(%rip), %rax     # 0x114393
movq %rax, (%rsp)
jmp 0x25b1a