// File: /workspace/github_c_makefile_star2_O2/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
andq $0x0, (%r8)
xorl %ebp, %ebp
testq %rcx, %rcx
js 0xc7524
movq %rcx, %r14
cmpq %rcx, 0x18(%rdx)
jle 0xc7524
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 0xc7413
movq %rsi, %r15
leaq 0x68125(%rip), %rcx     # 0x12f4e4
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq 0x18(%r12), %rdi
movq %r14, %rsi
callq 0xe3eb4
movzbl %al, %eax
jmp 0xc751e
movq 0x18(%r12), %rdi
movl (%r15), %edx
movq %r14, %rsi
callq 0xe41cb
jmp 0xc751e
movq 0x18(%r12), %rdi
movl (%r15), %edx
movq %r14, %rsi
callq 0xe41e5
movq %rax, (%rbx)
xorl %ebp, %ebp
testq %rax, %rax
setns %bpl
jmp 0xc7524
cmpl $0x81, %ecx
je 0xc73c8
jmp 0xc7524
movq 0x18(%r12), %rdi
movl (%r15), %edx
movq %r14, %rsi
callq 0xe41d8
jmp 0xc751e
movq 0x18(%r12), %rdi
movl (%r15), %edx
movq %r14, %rsi
callq 0xe41b1
jmp 0xc751e
movq 0x18(%r12), %rdi
movq %r14, %rsi
callq 0xe3f12
jmp 0xc751e
movq 0x18(%r12), %rdi
movl (%r15), %edx
movq %r14, %rsi
callq 0xe41d8
movq %rax, %r13
movq 0x18(%r12), %rdi
addq $0x4, %r14
movl (%r15), %edx
movq %r14, %rsi
callq 0xe41d8
jmp 0xc74e2
cmpl $0x0, 0x10(%r15)
je 0xc7524
movq 0x18(%r12), %rdi
movl (%r15), %edx
movq %r14, %rsi
callq 0xe4470
jmp 0xc74f5
cmpl $0x0, 0x10(%r15)
je 0xc7524
movq 0x18(%r12), %rdi
movl (%r15), %edx
movq %r14, %rsi
callq 0xe451a
jmp 0xc74f5
movq 0x18(%r12), %rdi
movl (%r15), %edx
movq %r14, %rsi
callq 0xe41cb
movq %rax, %r13
movq 0x18(%r12), %rdi
addq $0x4, %r14
movl (%r15), %edx
movq %r14, %rsi
callq 0xe41cb
testq %rax, %rax
je 0xc7524
cvtsi2sd %r13, %xmm0
cvtsi2sd %rax, %xmm1
divsd %xmm1, %xmm0
cvttsd2si %xmm0, %rax
jmp 0xc751e
movq 0x18(%r12), %rdi
movl (%r15), %edx
movq %r14, %rsi
callq 0xe41be
jmp 0xc751e
movq 0x18(%r12), %rdi
movl (%r15), %edx
movq %r14, %rsi
callq 0xe41e5
movq %rax, (%rbx)
pushq $0x1
popq %rbp
movl %ebp, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq