// File: /workspace/github_c_makefile_star2_O1/jsummers[P]deark/modules/tiff.c
// Function: read_numeric_value
static void read_numeric_value(deark *c, lctx *d, const struct taginfo *tg,
	i64 value_index, struct numeric_value *nv, de_ucstring *dbgline)
{
	int ret;
	i64 offs;

	nv->isvalid = 0;
	nv->val_int64 = 0;
	nv->val_double = 0.0;

	// FIXME: This is recalculated in read_tag_value_as_int64.
	offs = tg->val_offset + value_index*tg->unit_size;

	switch(tg->datatype) {
	case DATATYPE_BYTE:
	case DATATYPE_SBYTE:
	case DATATYPE_UNDEF:
	case DATATYPE_ASCII:
	case DATATYPE_UTF8:
	case DATATYPE_UINT16:
	case DATATYPE_SINT16:
	case DATATYPE_UINT32:
	case DATATYPE_SINT32:
	case DATATYPE_IFD32:
	case DATATYPE_UINT64:
	case DATATYPE_SINT64:
	case DATATYPE_IFD64:
		ret = read_tag_value_as_int64(c, d, tg, value_index, &nv->val_int64);
		nv->val_double = (double)nv->val_int64;
		nv->isvalid = ret;
		if(dbgline) {
			if(nv->isvalid)
				ucstring_printf(dbgline, DE_ENCODING_LATIN1, "%" I64_FMT, nv->val_int64);
			else
				ucstring_append_sz(dbgline, "?", DE_ENCODING_LATIN1);
		}
		break;

	case DATATYPE_RATIONAL:
	case DATATYPE_SRATIONAL:
		{
			i64 num, den;

			if(tg->datatype==DATATYPE_SRATIONAL) {
				num = dbuf_geti32x(c->infile, offs, d->is_le);
				den = dbuf_geti32x(c->infile, offs+4, d->is_le);
			}
			else {
				num = dbuf_getu32x(c->infile, offs, d->is_le);
				den = dbuf_getu32x(c->infile, offs+4, d->is_le);
			}

			if(den==0) {
				nv->isvalid = 0;
				nv->val_double = 0.0;
				nv->val_int64 = 0;
				if(dbgline) {
					ucstring_printf(dbgline, DE_ENCODING_LATIN1, "%" I64_FMT "/%" I64_FMT, num, den);
				}

			}
			else {
				nv->isvalid = 1;
				nv->val_double = (double)num/(double)den;
				nv->val_int64 = (i64)nv->val_double;
				if(dbgline) {
					format_double(dbgline, nv->val_double);
				}
			}
		}
		break;

	case DATATYPE_FLOAT32:
	case DATATYPE_FLOAT64:
		if(tg->datatype==DATATYPE_FLOAT64) {
			nv->val_double = dbuf_getfloat64x(c->infile, offs, d->is_le);
		}
		else {
			nv->val_double = dbuf_getfloat32x(c->infile, offs, d->is_le);
		}
		nv->val_int64 = (i64)nv->val_double;
		nv->isvalid = 1;
		if(dbgline) {
			format_double(dbgline, nv->val_double);
		}
		break;

	default:
		if(dbgline) {
			ucstring_append_sz(dbgline, "?", DE_ENCODING_LATIN1);
		}
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %r9, %rbx
movq %r8, %r14
movq %rsi, %r12
movq %rdi, %r13
movl $0x0, (%r8)
leaq 0x8(%r8), %r15
xorpd %xmm0, %xmm0
movupd %xmm0, 0x8(%r8)
movl 0xc(%rdx), %esi
leal -0x1(%rsi), %eax
cmpl $0x11, %eax
ja 0xd090e
movq 0x28(%rdx), %rbp
imulq %rcx, %rbp
addq 0x20(%rdx), %rbp
leaq 0x6bb9b(%rip), %rsi     # 0x13c44c
movslq (%rsi,%rax,4), %rax
addq %rsi, %rax
jmpq *%rax
movq %r13, %rdi
movq %r12, %rsi
movq %r15, %r8
callq 0xd03d8
movq 0x8(%r14), %rcx
xorps %xmm0, %xmm0
cvtsi2sd %rcx, %xmm0
movsd %xmm0, 0x10(%r14)
movl %eax, (%r14)
testq %rbx, %rbx
je 0xd0a63
testl %eax, %eax
je 0xd091f
leaq 0x58cc0(%rip), %rdx     # 0x1295b1
movq %rbx, %rdi
movl $0x5, %esi
xorl %eax, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xf6ab0
cmpl $0x81, %esi
je 0xd08ba
testq %rbx, %rbx
je 0xd0a63
leaq 0x5b543(%rip), %rsi     # 0x12be69
movq %rbx, %rdi
movl $0x5, %edx
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xf6a40
movq 0x18(%r13), %rdi
movl (%r12), %edx
movq %rbp, %rsi
callq 0xef654
movq %rax, (%rsp)
movq 0x18(%r13), %rdi
addq $0x4, %rbp
movl (%r12), %edx
movq %rbp, %rsi
callq 0xef654
jmp 0xd09a5
movq 0x18(%r13), %rdi
movl (%r12), %edx
movq %rbp, %rsi
callq 0xef9cf
jmp 0xd09e4
movq 0x18(%r13), %rdi
movl (%r12), %edx
movq %rbp, %rsi
callq 0xef614
movq %rax, (%rsp)
movq 0x18(%r13), %rdi
addq $0x4, %rbp
movl (%r12), %edx
movq %rbp, %rsi
callq 0xef614
testq %rax, %rax
je 0xd0a23
movl $0x1, (%r14)
xorps %xmm0, %xmm0
cvtsi2sdq (%rsp), %xmm0
cvtsi2sd %rax, %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, 0x10(%r14)
cvttsd2si %xmm0, %rax
movq %rax, 0x8(%r14)
jmp 0xd09fa
movq 0x18(%r13), %rdi
movl (%r12), %edx
movq %rbp, %rsi
callq 0xef935
cvttsd2si %xmm0, %rax
movsd %xmm0, 0x10(%r14)
movq %rax, 0x8(%r14)
movl $0x1, (%r14)
testq %rbx, %rbx
je 0xd0a63
leaq 0x6373c(%rip), %rdx     # 0x134142
movq %rbx, %rdi
movl $0x5, %esi
movb $0x1, %al
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xf6ab0
movl $0x0, (%r14)
xorpd %xmm0, %xmm0
movupd %xmm0, (%r15)
testq %rbx, %rbx
je 0xd0a63
leaq 0x6e1e6(%rip), %rdx     # 0x13ec25
movq %rbx, %rdi
movl $0x5, %esi
movq (%rsp), %rcx
xorl %r8d, %r8d
xorl %eax, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xf6ab0
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq