// File: /workspace/github_c_makefile_star2_O1/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 0xd0597
movq %rcx, %r14
cmpq %rcx, 0x18(%rdx)
jle 0xd0597
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 0xd047a
movq %rsi, %r15
leaq 0x6bfde(%rip), %rcx     # 0x13c404
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq 0x18(%r12), %rdi
movq %r14, %rsi
callq 0xef0d5
movzbl %al, %eax
jmp 0xd058f
movq 0x18(%r12), %rdi
movl (%r15), %edx
movq %r14, %rsi
callq 0xef614
jmp 0xd058f
movq 0x18(%r12), %rdi
movl (%r15), %edx
movq %r14, %rsi
callq 0xef661
movq %rax, (%rbx)
xorl %ebp, %ebp
testq %rax, %rax
setns %bpl
jmp 0xd0597
cmpl $0x81, %ecx
je 0xd042f
jmp 0xd0597
movq 0x18(%r12), %rdi
movl (%r15), %edx
movq %r14, %rsi
callq 0xef654
jmp 0xd058f
movq 0x18(%r12), %rdi
movl (%r15), %edx
movq %r14, %rsi
callq 0xef570
jmp 0xd058f
movq 0x18(%r12), %rdi
movq %r14, %rsi
callq 0xef135
jmp 0xd058f
movq 0x18(%r12), %rdi
movl (%r15), %edx
movq %r14, %rsi
callq 0xef654
movq %rax, %r13
movq 0x18(%r12), %rdi
addq $0x4, %r14
movl (%r15), %edx
movq %r14, %rsi
callq 0xef654
jmp 0xd0545
cmpl $0x0, 0x10(%r15)
je 0xd055c
movq 0x18(%r12), %rdi
movl (%r15), %edx
movq %r14, %rsi
callq 0xef935
jmp 0xd0558
cmpl $0x0, 0x10(%r15)
je 0xd055c
movq 0x18(%r12), %rdi
movl (%r15), %edx
movq %r14, %rsi
callq 0xef9cf
jmp 0xd0558
movq 0x18(%r12), %rdi
movl (%r15), %edx
movq %r14, %rsi
callq 0xef614
movq %rax, %r13
movq 0x18(%r12), %rdi
addq $0x4, %r14
movl (%r15), %edx
movq %r14, %rsi
callq 0xef614
testq %rax, %rax
je 0xd055c
cvtsi2sd %r13, %xmm0
cvtsi2sd %rax, %xmm1
divsd %xmm1, %xmm0
xorl %eax, %eax
jmp 0xd0562
movb $0x1, %al
xorpd %xmm0, %xmm0
testb %al, %al
jne 0xd0597
cvttsd2si %xmm0, %rax
jmp 0xd058f
movq 0x18(%r12), %rdi
movl (%r15), %edx
movq %r14, %rsi
callq 0xef5b7
jmp 0xd058f
movq 0x18(%r12), %rdi
movl (%r15), %edx
movq %r14, %rsi
callq 0xef661
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