// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/palmpdb.c
// Function: do_prescan_records
static int do_prescan_records(deark *c, lctx *d, i64 pos1)
{
	i64 i;

	if(d->rec_list.num_recs<1) return 1;
	// num_recs is untrusted, but it is a 16-bit int that can be at most 65535.
	d->rec_list.rec_data = de_mallocarray(c, d->rec_list.num_recs, sizeof(struct rec_data_struct));
	d->rec_list.order_to_read = de_mallocarray(c, d->rec_list.num_recs, sizeof(size_t));
	for(i=0; i<d->rec_list.num_recs; i++) {
		// By default, read the records in the order they appear in the file.
		d->rec_list.order_to_read[i] = (size_t)i;

		if(d->file_fmt==FMT_PRC) {
			u32 rsrc_type;
			rsrc_type = (u32)de_getu32be(pos1 + d->rec_size*i);
			if(rsrc_type==CODE_tAIN && d->rec_list.icon_name_count==0) {
				// "Move" the tAIN record to the beginning, so we will read it
				// before any tAIB resources.
				rec_list_insert_at_start(&d->rec_list, i);
				d->rec_list.icon_name_count++;
			}
			d->rec_list.rec_data[i].offset = (u32)de_getu32be(pos1 + d->rec_size*i + 6);
		}
		else {
			u32 id;
			d->rec_list.rec_data[i].offset = (u32)de_getu32be(pos1 + d->rec_size*i);
			if(!d->has_nonzero_ids) {
				id = (de_getbyte(pos1+d->rec_size*i+5)<<16) |
					(de_getbyte(pos1+d->rec_size*i+6)<<8) |
					(de_getbyte(pos1+d->rec_size*i+7));
				if(id!=0) d->has_nonzero_ids = 1;
			}
		}

		// Record data must not start beyond the end of file.
		if((i64)d->rec_list.rec_data[i].offset > c->infile->len) {
			de_err(c, "Record %d (at %d) starts after end of file (%d)",
				(int)i, (int)d->rec_list.rec_data[i].offset, (int)c->infile->len);
			return 0;
		}

		// Record data must not start before the previous record's data.
		if(i>0) {
			if(d->rec_list.rec_data[i].offset < d->rec_list.rec_data[i-1].offset) {
				de_err(c, "Record %d (at %d) starts before previous record (at %d)",
					(int)i, (int)d->rec_list.rec_data[i].offset, (int)d->rec_list.rec_data[i-1].offset);
				return 0;
			}
		}
	}
	return 1;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq -0x18(%rbp), %rax
cmpq $0x1, 0xa0(%rax)
jge 0xff8ce
movl $0x1, -0x4(%rbp)
jmp 0xffbe9
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0xa0(%rax), %rsi
movl $0x4, %edx
callq 0x15c450
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0xa8(%rax)
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0xa0(%rax), %rsi
movl $0x8, %edx
callq 0x15c450
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0xb0(%rax)
movq $0x0, -0x28(%rbp)
movq -0x28(%rbp), %rax
movq -0x18(%rbp), %rcx
cmpq 0xa0(%rcx), %rax
jge 0xffbe2
movq -0x28(%rbp), %rdx
movq -0x18(%rbp), %rax
movq 0xb0(%rax), %rax
movq -0x28(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movq -0x18(%rbp), %rax
cmpl $0x2, (%rax)
jne 0xffa00
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x20(%rbp), %rsi
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rax
imulq -0x28(%rbp), %rax
addq %rax, %rsi
callq 0x165470
movl %eax, -0x2c(%rbp)
cmpl $0x7441494e, -0x2c(%rbp) # imm = 0x7441494E
jne 0xff9c2
movq -0x18(%rbp), %rax
cmpq $0x0, 0xb8(%rax)
jne 0xff9c2
movq -0x18(%rbp), %rdi
addq $0xa0, %rdi
movq -0x28(%rbp), %rsi
callq 0x100290
movq -0x18(%rbp), %rax
movq 0xb8(%rax), %rcx
addq $0x1, %rcx
movq %rcx, 0xb8(%rax)
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x20(%rbp), %rsi
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rax
imulq -0x28(%rbp), %rax
addq %rax, %rsi
addq $0x6, %rsi
callq 0x165470
movl %eax, %edx
movq -0x18(%rbp), %rax
movq 0xa8(%rax), %rax
movq -0x28(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
jmp 0xffaed
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x20(%rbp), %rsi
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rax
imulq -0x28(%rbp), %rax
addq %rax, %rsi
callq 0x165470
movl %eax, %edx
movq -0x18(%rbp), %rax
movq 0xa8(%rax), %rax
movq -0x28(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movq -0x18(%rbp), %rax
cmpl $0x0, 0xc(%rax)
jne 0xffaeb
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x20(%rbp), %rsi
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rax
imulq -0x28(%rbp), %rax
addq %rax, %rsi
addq $0x5, %rsi
callq 0x165060
movzbl %al, %eax
shll $0x10, %eax
movl %eax, -0x38(%rbp)
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x20(%rbp), %rsi
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rax
imulq -0x28(%rbp), %rax
addq %rax, %rsi
addq $0x6, %rsi
callq 0x165060
movb %al, %cl
movl -0x38(%rbp), %eax
movzbl %cl, %ecx
shll $0x8, %ecx
orl %ecx, %eax
movl %eax, -0x34(%rbp)
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x20(%rbp), %rsi
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rax
imulq -0x28(%rbp), %rax
addq %rax, %rsi
addq $0x7, %rsi
callq 0x165060
movb %al, %cl
movl -0x34(%rbp), %eax
movzbl %cl, %ecx
orl %ecx, %eax
movl %eax, -0x30(%rbp)
cmpl $0x0, -0x30(%rbp)
je 0xffae9
movq -0x18(%rbp), %rax
movl $0x1, 0xc(%rax)
jmp 0xffaeb
jmp 0xffaed
movq -0x18(%rbp), %rax
movq 0xa8(%rax), %rax
movq -0x28(%rbp), %rcx
movl (%rax,%rcx,4), %eax
movq -0x10(%rbp), %rcx
movq 0x18(%rcx), %rcx
cmpq 0x18(%rcx), %rax
jle 0xffb52
movq -0x10(%rbp), %rdi
movq -0x28(%rbp), %rax
movl %eax, %edx
movq -0x18(%rbp), %rax
movq 0xa8(%rax), %rax
movq -0x28(%rbp), %rcx
movl (%rax,%rcx,4), %ecx
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x18(%rax), %rax
movl %eax, %r8d
leaq 0xc1592(%rip), %rsi     # 0x1c10d1
movb $0x0, %al
callq 0x15bfb0
movl $0x0, -0x4(%rbp)
jmp 0xffbe9
cmpq $0x0, -0x28(%rbp)
jle 0xffbcf
movq -0x18(%rbp), %rax
movq 0xa8(%rax), %rax
movq -0x28(%rbp), %rcx
movl (%rax,%rcx,4), %eax
movq -0x18(%rbp), %rcx
movq 0xa8(%rcx), %rcx
movq -0x28(%rbp), %rdx
subq $0x1, %rdx
cmpl (%rcx,%rdx,4), %eax
jae 0xffbcd
movq -0x10(%rbp), %rdi
movq -0x28(%rbp), %rax
movl %eax, %edx
movq -0x18(%rbp), %rax
movq 0xa8(%rax), %rax
movq -0x28(%rbp), %rcx
movl (%rax,%rcx,4), %ecx
movq -0x18(%rbp), %rax
movq 0xa8(%rax), %rax
movq -0x28(%rbp), %rsi
subq $0x1, %rsi
movl (%rax,%rsi,4), %r8d
leaq 0xc1544(%rip), %rsi     # 0x1c1101
movb $0x0, %al
callq 0x15bfb0
movl $0x0, -0x4(%rbp)
jmp 0xffbe9
jmp 0xffbcf
jmp 0xffbd1
movq -0x28(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x28(%rbp)
jmp 0xff924
movl $0x1, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x40, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)