// File: /workspace/github_c_makefile_star2_O3/Openvario[P]sensord/ms5611.c
// Function: ms5611_read_temp
int ms5611_read_temp(t_ms5611 *sensor, int glitch)
{
	//variables
	uint8_t buf[10]={0x00};
	//variables
	//long d2;
	int64_t OFF2=0;
	int64_t SENS2=0;
	int64_t T2=0;

	// read result
	buf[0] = 0x00;
	if ((write(sensor->fd, buf, 1)) != 1) {								// Send register we want to read from
		fprintf(stderr, "Error writing to i2c slave(%s)\n", __func__);
		return(1);
	}

	if (read(sensor->fd, buf, 3) != 3) {								// Read back data into buf[]
		fprintf(stderr, "Unable to read from slave(%s)\n", __func__);
		return(1);
	}

	// Put temperature readings together
	sensor->D2l = sensor->D2;
	sensor->D2 = (buf[0] << 16) + (buf[1] << 8) + buf[2];
	if (glitch==0)
		if ((sensor->D2<=sensor->D2l+100e3) && (sensor->D2l<=sensor->D2+300))
		{
			sensor->D2f = (sensor->D2f*7+sensor->D2)/8;

			// calculate dT and absolute temperature
			sensor->dT = sensor->D2f - sensor->C5s;
			sensor->temp = 2000 + (((int64_t)sensor->dT * sensor->C6) / 8388608);

			// these calculations are copied from the data sheet
			//OFF = C2 * 2**16 + (C4 * dT) / 2**7
			//SENS = C1 * 2**15 + (C3 * dT) / 2**8
			//P = (D1 * SENS / 2**21 - OFF) / 2**15

			sensor->off = (sensor->C2s + (((int64_t)sensor->C4 * sensor->dT) >> 7));
			sensor->sens = (sensor->C1s + (((int64_t)sensor->C3 * sensor->dT) >> 8));

			if (sensor->secordcomp)
			{
				// second order correction
				if (sensor->temp < 2000)
				{
					T2 = (sensor->dT * sensor->dT) >> 31;
					OFF2 = 5 * ((int64_t)(sensor->temp - 2000) * (sensor->temp - 2000)) >> 1;
					SENS2 = 5 * ((int64_t)(sensor->temp - 2000) * (sensor->temp - 2000)) >> 2;

					if (sensor->temp < -1500)
					{
						OFF2 = OFF2 + 7 * ((int64_t)(sensor->temp + 1500) * (sensor->temp + 1500));
						SENS2 = SENS2 + ((11 * ((int64_t)(sensor->temp + 1500) * (sensor->temp + 1500))) >> 1);
					}

					sensor->temp = sensor->temp - T2;
					sensor->off = sensor->off - OFF2;
					sensor->sens = sensor->sens - SENS2;
				}
			}
		}


	// debug print
	ddebug_print("%s @ 0x%x: D2 = %u\n", __func__, sensor->address, sensor->D2);
	ddebug_print("%s @ 0x%x: dT = %d\n", __func__, sensor->address, sensor->dT);
	debug_print("%s @ 0x%x: temp = %d\n", __func__, sensor->address, sensor->temp);

	return(0);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %rbx
subq $0x18, %rsp
movl %esi, %ebp
movq %rdi, %rbx
leaq 0x8(%rsp), %rsi
movw $0x0, 0x8(%rsi)
movq $0x0, (%rsi)
movl (%rdi), %edi
movl $0x1, %edx
callq 0x20b0
cmpq $0x1, %rax
jne 0x431a
movl (%rbx), %edi
leaq 0x8(%rsp), %rsi
movl $0x3, %edx
callq 0x2180
cmpq $0x3, %rax
jne 0x4321
movl 0x28(%rbx), %eax
movl %eax, 0x2c(%rbx)
movzbl 0x8(%rsp), %ecx
shll $0x10, %ecx
movzbl 0x9(%rsp), %edx
shll $0x8, %edx
orl %ecx, %edx
movzbl 0xa(%rsp), %r8d
orl %edx, %r8d
movl %r8d, 0x28(%rbx)
testl %ebp, %ebp
jne 0x42f8
movl %r8d, %ecx
movq %rax, %rdx
addq $0x186a0, %rdx          # imm = 0x186A0
cmpq %rcx, %rdx
setb %cl
leal 0x12c(%r8), %edx
cmpl %edx, %eax
seta %al
orb %cl, %al
jne 0x42f8
movl 0x30(%rbx), %eax
leal (,%rax,8), %ecx
subl %eax, %ecx
addl %r8d, %ecx
shrl $0x3, %ecx
movl %ecx, 0x30(%rbx)
subl 0x14(%rbx), %ecx
movl %ecx, 0x34(%rbx)
movslq %ecx, %rdi
movzwl 0x18(%rbx), %esi
imulq %rdi, %rsi
leaq 0x7fffff(%rsi), %rcx
testq %rsi, %rsi
cmovnsq %rsi, %rcx
sarq $0x17, %rcx
leal 0x7d0(%rcx), %eax
movl %eax, 0x38(%rbx)
movl 0x8(%rbx), %r9d
movl 0xc(%rbx), %edx
movzwl 0x12(%rbx), %eax
imulq %rdi, %rax
sarq $0x7, %rax
addq %rdx, %rax
movq %rax, 0x40(%rbx)
movzwl 0x10(%rbx), %edx
imulq %rdi, %rdx
sarq $0x8, %rdx
addq %r9, %rdx
movq %rdx, 0x48(%rbx)
cmpl $0x0, 0x98(%rbx)
je 0x42f8
cmpq $-0x800000, %rsi        # imm = 0xFF800000
jg 0x42f8
movq %rcx, %rdi
imulq %rcx, %rdi
leaq (%rdi,%rdi,4), %rdi
movq %rdi, %r9
shrq %r9
shrq $0x2, %rdi
movabsq $-0x6d6800000, %r10     # imm = 0xFFFFFFF929800000
cmpq %r10, %rsi
ja 0x42ea
addq $0xdac, %rcx            # imm = 0xDAC
imulq %rcx, %rcx
leaq (,%rcx,8), %rsi
subq %rcx, %rsi
addq %rsi, %r9
leaq (%rcx,%rcx,4), %rsi
leaq (%rcx,%rsi,2), %rcx
shrq %rcx
addq %rcx, %rdi
subq %r9, %rax
movq %rax, 0x40(%rbx)
subq %rdi, %rdx
movq %rdx, 0x48(%rbx)
leaq 0xa175(%rip), %r14      # 0xe474
movl (%r14), %eax
cmpl $0x2, %eax
jge 0x432d
xorl %ebp, %ebp
testl %eax, %eax
jg 0x437d
movl %ebp, %eax
addq $0x18, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
callq 0x2535
jmp 0x4326
callq 0x2554
movl $0x1, %ebp
jmp 0x430d
movq 0x9cc4(%rip), %r15      # 0xdff8
movq (%r15), %rdi
movzbl 0x4(%rbx), %ecx
leaq 0x4f53(%rip), %rsi      # 0x9295
leaq 0x4f1c(%rip), %rdx      # 0x9265
xorl %eax, %eax
callq 0x21c0
movl (%r14), %eax
cmpl $0x2, %eax
jl 0x4307
movq (%r15), %rdi
movzbl 0x4(%rbx), %ecx
movl 0x34(%rbx), %r8d
leaq 0x4f3f(%rip), %rsi      # 0x92a9
leaq 0x4ef4(%rip), %rdx      # 0x9265
xorl %eax, %eax
callq 0x21c0
movl (%r14), %eax
jmp 0x4307
movq %rbx, %rdi
callq 0x2573
jmp 0x430d