// File: /workspace/github_c_makefile_star2_O2/jsummers[P]deark/modules/palmpdb.c
// Function: handle_palm_timestamp
static void handle_palm_timestamp(deark *c, lctx *d, i64 pos, const char *name,
	struct de_timestamp *returned_ts)
{
	struct de_timestamp ts;
	char timestamp_buf[64];
	i64 ts_int;

	de_zeromem(&ts, sizeof(struct de_timestamp));
	if(returned_ts) {
		de_zeromem(returned_ts, sizeof(struct de_timestamp));
	}

	ts_int = de_getu32be(pos);
	if(ts_int==0) {
		de_dbg(c, "%s: 0 (not set)", name);
		goto done;
	}

	de_dbg(c, "%s: ...", name);
	de_dbg_indent(c, 1);

	// I've seen three different ways to interpret this 32-bit timestamp, and
	// I don't know how to guess the correct one.

	if(d->timestampfmt==TIMESTAMPFMT_MACBE || d->timestampfmt==TIMESTAMPFMT_UNKNOWN) {
		de_mac_time_to_timestamp(ts_int, &ts);
		de_timestamp_to_string(&ts, timestamp_buf, sizeof(timestamp_buf), 0);
		de_dbg(c, "... if Mac-BE: %"I64_FMT" (%s)", ts_int, timestamp_buf);
	}

	if(d->timestampfmt==TIMESTAMPFMT_UNIXBE || d->timestampfmt==TIMESTAMPFMT_UNKNOWN) {
		ts_int = de_geti32be(pos);
		de_unix_time_to_timestamp(ts_int, &ts, 0x1);
		if(d->timestampfmt==TIMESTAMPFMT_UNIXBE || timestamp_is_plausible(&ts)) {
			de_timestamp_to_string(&ts, timestamp_buf, sizeof(timestamp_buf), 0);
			de_dbg(c, "... if Unix-BE: %"I64_FMT" (%s)", ts_int, timestamp_buf);
		}
	}

	if(d->timestampfmt==TIMESTAMPFMT_MACLE || d->timestampfmt==TIMESTAMPFMT_UNKNOWN) {
		ts_int = de_getu32le(pos);
		de_mac_time_to_timestamp(ts_int, &ts);
		if(d->timestampfmt==TIMESTAMPFMT_MACLE || timestamp_is_plausible(&ts)) {
			de_timestamp_to_string(&ts, timestamp_buf, sizeof(timestamp_buf), 0);
			de_dbg(c, "... if Mac-LE: %"I64_FMT" (%s)", ts_int, timestamp_buf);
		}
	}

	de_dbg_indent(c, -1);

done:
	if(returned_ts && d->timestampfmt!=TIMESTAMPFMT_UNKNOWN) {
		*returned_ts = ts;
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %r8, %rbx
movq %rcx, %rbp
movq %rdx, %r12
movq %rsi, %r14
movq %rdi, %r15
xorps %xmm0, %xmm0
movaps %xmm0, (%rsp)
testq %r8, %r8
je 0xa7f27
movups %xmm0, (%rbx)
movq 0x18(%r15), %rdi
movq %r12, %rsi
callq 0xe404f
testq %rax, %rax
je 0xa807b
movq %rax, %r13
leaq 0x80369(%rip), %rsi     # 0x1282af
movq %r15, %rdi
movq %rbp, %rdx
xorl %eax, %eax
callq 0xde896
pushq $0x1
popq %rsi
movq %r15, %rdi
callq 0xdeba7
movl 0x8(%r14), %eax
cmpl $0x1, %eax
ja 0xa7fa5
movq %rsp, %rbp
movq %r13, %rdi
movq %rbp, %rsi
callq 0xe0102
leaq 0x10(%rsp), %rsi
pushq $0x40
popq %rdx
movq %rbp, %rdi
movq %rsi, %rbp
xorl %ecx, %ecx
callq 0xe0644
leaq 0x80326(%rip), %rsi     # 0x1282b7
movq %r15, %rdi
movq %r13, %rdx
movq %rbp, %rcx
xorl %eax, %eax
callq 0xde896
movl 0x8(%r14), %eax
testl $0xfffffffd, %eax       # imm = 0xFFFFFFFD
jne 0xa8008
movq 0x18(%r15), %rdi
movq %r12, %rsi
callq 0xe40e8
movq %rax, %r13
movq %rsp, %rsi
pushq $0x1
popq %rdx
movq %rax, %rdi
callq 0xe002e
cmpl $0x2, 0x8(%r14)
je 0xa7fdc
movq %rsp, %rdi
callq 0xa80b1
testl %eax, %eax
je 0xa8008
movq %rsp, %rdi
leaq 0x10(%rsp), %rbp
pushq $0x40
popq %rdx
movq %rbp, %rsi
xorl %ecx, %ecx
callq 0xe0644
leaq 0x802d7(%rip), %rsi     # 0x1282cf
movq %r15, %rdi
movq %r13, %rdx
movq %rbp, %rcx
xorl %eax, %eax
callq 0xde896
movl 0x8(%r14), %eax
cmpl $0x3, %eax
je 0xa8015
testl %eax, %eax
jne 0xa806e
movq 0x18(%r15), %rdi
movq %r12, %rsi
callq 0xe409f
movq %rax, %r12
movq %rsp, %rsi
movq %rax, %rdi
callq 0xe0102
cmpl $0x3, 0x8(%r14)
je 0xa8042
movq %rsp, %rdi
callq 0xa80b1
testl %eax, %eax
je 0xa806e
movq %rsp, %rdi
leaq 0x10(%rsp), %r13
pushq $0x40
popq %rdx
movq %r13, %rsi
xorl %ecx, %ecx
callq 0xe0644
leaq 0x8028a(%rip), %rsi     # 0x1282e8
movq %r15, %rdi
movq %r12, %rdx
movq %r13, %rcx
xorl %eax, %eax
callq 0xde896
pushq $-0x1
popq %rsi
movq %r15, %rdi
callq 0xdeba7
jmp 0xa808f
leaq 0x8021d(%rip), %rsi     # 0x12829f
movq %r15, %rdi
movq %rbp, %rdx
xorl %eax, %eax
callq 0xde896
testq %rbx, %rbx
je 0xa80a2
cmpl $0x0, 0x8(%r14)
je 0xa80a2
movaps (%rsp), %xmm0
movups %xmm0, (%rbx)
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq