// File: /workspace/github_c_makefile_star2_O2/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
andl $0x0, (%r8)
movq %rsi, %r12
movq %rdi, %r13
leaq 0x8(%r8), %r15
xorpd %xmm0, %xmm0
movupd %xmm0, 0x8(%r8)
movl 0xc(%rdx), %esi
leal -0x1(%rsi), %eax
cmpl $0x11, %eax
ja 0xc78ee
movq 0x28(%rdx), %rbp
imulq %rcx, %rbp
addq 0x20(%rdx), %rbp
leaq 0x67c96(%rip), %rsi     # 0x12f52c
movslq (%rsi,%rax,4), %rax
addq %rsi, %rax
jmpq *%rax
movq %r13, %rdi
movq %r12, %rsi
movq %r15, %r8
callq 0xc7374
movq 0x8(%r14), %rcx
cvtsi2sd %rcx, %xmm0
movsd %xmm0, 0x10(%r14)
movl %eax, (%r14)
testq %rbx, %rbx
je 0xc7a28
testl %eax, %eax
je 0xc78ff
leaq 0x54cce(%rip), %rdx     # 0x11c5a1
pushq $0x5
popq %rsi
movq %rbx, %rdi
xorl %eax, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xeae8b
cmpl $0x81, %esi
je 0xc789f
testq %rbx, %rbx
je 0xc7a28
leaq 0x575db(%rip), %rsi     # 0x11eee1
pushq $0x5
popq %rdx
movq %rbx, %rdi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xeae42
movq 0x18(%r13), %rdi
movl (%r12), %edx
movq %rbp, %rsi
callq 0xe41d8
movq %rax, (%rsp)
movq 0x18(%r13), %rdi
addq $0x4, %rbp
movl (%r12), %edx
movq %rbp, %rsi
callq 0xe41d8
jmp 0xc7983
movq 0x18(%r13), %rdi
movl (%r12), %edx
movq %rbp, %rsi
callq 0xe451a
jmp 0xc79bf
movq 0x18(%r13), %rdi
movl (%r12), %edx
movq %rbp, %rsi
callq 0xe41cb
movq %rax, (%rsp)
movq 0x18(%r13), %rdi
addq $0x4, %rbp
movl (%r12), %edx
movq %rbp, %rsi
callq 0xe41cb
testq %rax, %rax
je 0xc79ed
movl $0x1, (%r14)
cvtsi2sdq (%rsp), %xmm0
cvtsi2sd %rax, %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, 0x10(%r14)
cvttsd2si %xmm0, %rax
movq %rax, 0x8(%r14)
jmp 0xc79d5
movq 0x18(%r13), %rdi
movl (%r12), %edx
movq %rbp, %rsi
callq 0xe4470
cvttsd2si %xmm0, %rax
movsd %xmm0, 0x10(%r14)
movq %rax, 0x8(%r14)
movl $0x1, (%r14)
testq %rbx, %rbx
je 0xc7a28
movq %rbx, %rdi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xc7a37
andl $0x0, (%r14)
xorpd %xmm0, %xmm0
movupd %xmm0, (%r15)
testq %rbx, %rbx
je 0xc7a28
leaq 0x6a2ef(%rip), %rdx     # 0x131cf5
pushq $0x5
popq %rsi
movq %rbx, %rdi
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 0xeae8b
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq