// File: /workspace/github_c_makefile_star2_O3/jsummers[P]deark/modules/tiff.c
// Function: read_tag_value_as_int64
static int read_tag_value_as_int64(deark *c, lctx *d, const struct taginfo *tg,
	i64 value_index, i64 *n)
{
	double v_dbl;
	i64 offs;

	*n = 0;
	if(value_index<0 || value_index>=tg->valcount) return 0;
	offs = tg->val_offset + value_index*tg->unit_size;

	switch(tg->datatype) {
	case DATATYPE_UINT16:
		*n = dbuf_getu16x(c->infile, offs, d->is_le);
		return 1;
	case DATATYPE_UINT32:
	case DATATYPE_IFD32:
		*n = dbuf_getu32x(c->infile, offs, d->is_le);
		return 1;
	case DATATYPE_BYTE:
	case DATATYPE_UNDEF:
	case DATATYPE_ASCII:
	case DATATYPE_UTF8:
		*n = (i64)de_getbyte(offs);
		return 1;
	case DATATYPE_UINT64:
	case DATATYPE_IFD64:
		// TODO: Somehow support unsigned 64-bit ints that don't fit into
		// a i64?
		*n = dbuf_geti64x(c->infile, offs, d->is_le);
		if(*n < 0) return 0;
		return 1;
	case DATATYPE_SINT16:
		*n = dbuf_geti16x(c->infile, offs, d->is_le);
		return 1;
	case DATATYPE_SINT32:
		*n = dbuf_geti32x(c->infile, offs, d->is_le);
		return 1;
	case DATATYPE_SINT64:
		*n = dbuf_geti64x(c->infile, offs, d->is_le);
		return 1;
	case DATATYPE_SBYTE:
		*n = dbuf_geti8(c->infile, offs);
		return 1;
	case DATATYPE_RATIONAL:
	case DATATYPE_SRATIONAL:
	case DATATYPE_FLOAT32:
	case DATATYPE_FLOAT64:
		if(read_tag_value_as_double(c, d, tg, value_index, &v_dbl)) {
			*n = (i64)v_dbl;
			return 1;
		}
		return 0;
	}
	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq $0x0, (%r8)
xorl %ebp, %ebp
testq %rcx, %rcx
js 0xcf974
movq %rcx, %r14
cmpq %rcx, 0x18(%rdx)
jle 0xcf974
movq %r8, %rbx
movq %rdi, %r12
imulq 0x28(%rdx), %r14
addq 0x20(%rdx), %r14
movl 0xc(%rdx), %ecx
leal -0x1(%rcx), %eax
cmpl $0x11, %eax
ja 0xcf861
movq %rsi, %r15
leaq 0x6d007(%rip), %rcx     # 0x13c814
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq 0x18(%r12), %rdi
movq %r14, %rsi
callq 0xeead3
movzbl %al, %eax
jmp 0xcf96c
movq 0x18(%r12), %rdi
movl (%r15), %edx
movq %r14, %rsi
callq 0xeeff3
jmp 0xcf96c
movq 0x18(%r12), %rdi
movl (%r15), %edx
movq %r14, %rsi
callq 0xef065
movq %rax, (%rbx)
xorl %ebp, %ebp
testq %rax, %rax
setns %bpl
jmp 0xcf974
cmpl $0x81, %ecx
je 0xcf816
jmp 0xcf974
movq 0x18(%r12), %rdi
movl (%r15), %edx
movq %r14, %rsi
callq 0xef033
jmp 0xcf96c
movq 0x18(%r12), %rdi
movl (%r15), %edx
movq %r14, %rsi
callq 0xeef43
jmp 0xcf96c
movq 0x18(%r12), %rdi
movq %r14, %rsi
callq 0xeeb33
jmp 0xcf96c
movq 0x18(%r12), %rdi
movl (%r15), %edx
movq %r14, %rsi
callq 0xef033
movq %rax, %r13
movq 0x18(%r12), %rdi
addq $0x4, %r14
movl (%r15), %edx
movq %r14, %rsi
callq 0xef033
jmp 0xcf930
cmpl $0x0, 0x10(%r15)
je 0xcf974
movq 0x18(%r12), %rdi
movl (%r15), %edx
movq %r14, %rsi
callq 0xef2fc
jmp 0xcf943
cmpl $0x0, 0x10(%r15)
je 0xcf974
movq 0x18(%r12), %rdi
movl (%r15), %edx
movq %r14, %rsi
callq 0xef39e
jmp 0xcf943
movq 0x18(%r12), %rdi
movl (%r15), %edx
movq %r14, %rsi
callq 0xeeff3
movq %rax, %r13
movq 0x18(%r12), %rdi
addq $0x4, %r14
movl (%r15), %edx
movq %r14, %rsi
callq 0xeeff3
testq %rax, %rax
je 0xcf974
cvtsi2sd %r13, %xmm0
cvtsi2sd %rax, %xmm1
divsd %xmm1, %xmm0
cvttsd2si %xmm0, %rax
jmp 0xcf96c
movq 0x18(%r12), %rdi
movl (%r15), %edx
movq %r14, %rsi
callq 0xeef8a
jmp 0xcf96c
movq 0x18(%r12), %rdi
movl (%r15), %edx
movq %r14, %rsi
callq 0xef065
movq %rax, (%rbx)
movl $0x1, %ebp
movl %ebp, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq