// File: /workspace/github_c_makefile_star2_O1/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 %r14
pushq %rbx
subq $0x10, %rsp
movl %esi, %ebp
movq %rdi, %rbx
movq %rsp, %rsi
movw $0x0, 0x8(%rsi)
movq $0x0, (%rsi)
movl (%rdi), %edi
movl $0x1, %edx
callq 0x20b0
cmpq $0x1, %rax
jne 0x431d
movl (%rbx), %edi
movq %rsp, %rsi
movl $0x3, %edx
callq 0x2180
cmpq $0x3, %rax
jne 0x4324
movl 0x28(%rbx), %eax
movl %eax, 0x2c(%rbx)
movzbl (%rsp), %ecx
shll $0x10, %ecx
movzbl 0x1(%rsp), %edx
shll $0x8, %edx
orl %ecx, %edx
movzbl 0x2(%rsp), %r8d
orl %edx, %r8d
movl %r8d, 0x28(%rbx)
testl %ebp, %ebp
jne 0x42f7
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 0x42f7
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 0x42f7
cmpq $-0x800000, %rsi        # imm = 0xFF800000
jg 0x42f7
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
jg 0x42e9
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 0xa176(%rip), %r14      # 0xe474
cmpl $0x2, (%r14)
jge 0x4330
cmpl $0x2, (%r14)
jge 0x4355
xorl %ebp, %ebp
cmpl $0x0, (%r14)
jg 0x435f
movl %ebp, %eax
addq $0x10, %rsp
popq %rbx
popq %r14
popq %rbp
retq
callq 0x2579
jmp 0x4329
callq 0x2598
movl $0x1, %ebp
jmp 0x4312
movq 0x9cc1(%rip), %rax      # 0xdff8
movq (%rax), %rdi
movzbl 0x4(%rbx), %ecx
leaq 0x4f50(%rip), %rsi      # 0x9295
leaq 0x4f19(%rip), %rdx      # 0x9265
xorl %eax, %eax
callq 0x21c0
jmp 0x4304
movq %rbx, %rdi
callq 0x25b7
jmp 0x430a
movq %rbx, %rdi
callq 0x25e1
jmp 0x4312