// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
leaq -0x1e(%rbp), %rdi
xorl %esi, %esi
movl $0xa, %edx
callq 0x2130
movq $0x0, -0x28(%rbp)
movq $0x0, -0x30(%rbp)
movq $0x0, -0x38(%rbp)
movb $0x0, -0x1e(%rbp)
movq -0x10(%rbp), %rax
movl (%rax), %edi
leaq -0x1e(%rbp), %rsi
movl $0x1, %edx
callq 0x20a0
cmpq $0x1, %rax
je 0x2e20
movq 0xc1fc(%rip), %rax      # 0xeff8
movq (%rax), %rdi
leaq 0x844b(%rip), %rsi      # 0xb251
leaq 0x8464(%rip), %rdx      # 0xb271
movb $0x0, %al
callq 0x21d0
movl $0x1, -0x4(%rbp)
jmp 0x3199
movq -0x10(%rbp), %rax
movl (%rax), %edi
leaq -0x1e(%rbp), %rsi
movl $0x3, %edx
callq 0x2180
cmpq $0x3, %rax
je 0x2e65
movq 0xc1b7(%rip), %rax      # 0xeff8
movq (%rax), %rdi
leaq 0x8437(%rip), %rsi      # 0xb282
leaq 0x841f(%rip), %rdx      # 0xb271
movb $0x0, %al
callq 0x21d0
movl $0x1, -0x4(%rbp)
jmp 0x3199
movq -0x10(%rbp), %rax
movl 0x28(%rax), %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x2c(%rax)
movzbl -0x1e(%rbp), %ecx
shll $0x10, %ecx
movzbl -0x1d(%rbp), %eax
shll $0x8, %eax
addl %eax, %ecx
movzbl -0x1c(%rbp), %eax
addl %eax, %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x28(%rax)
cmpl $0x0, -0x14(%rbp)
jne 0x30e1
movq -0x10(%rbp), %rax
movl 0x28(%rax), %ecx
movl 0x2c(%rax), %eax
cvtsi2sd %rcx, %xmm1
cvtsi2sd %rax, %xmm0
movsd 0x8162(%rip), %xmm2     # 0xb018
addsd %xmm2, %xmm0
ucomisd %xmm1, %xmm0
jb 0x30df
movq -0x10(%rbp), %rax
movl 0x2c(%rax), %eax
movq -0x10(%rbp), %rcx
movl 0x28(%rcx), %ecx
addl $0x12c, %ecx            # imm = 0x12C
cmpl %ecx, %eax
ja 0x30df
movq -0x10(%rbp), %rax
imull $0x7, 0x30(%rax), %ecx
movq -0x10(%rbp), %rax
addl 0x28(%rax), %ecx
shrl $0x3, %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x30(%rax)
movq -0x10(%rbp), %rax
movl 0x30(%rax), %ecx
movq -0x10(%rbp), %rax
subl 0x14(%rax), %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x34(%rax)
movq -0x10(%rbp), %rax
movslq 0x34(%rax), %rax
movq -0x10(%rbp), %rcx
movzwl 0x18(%rcx), %ecx
imulq %rcx, %rax
movl $0x800000, %ecx         # imm = 0x800000
cqto
idivq %rcx
addq $0x7d0, %rax            # imm = 0x7D0
movl %eax, %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x38(%rax)
movq -0x10(%rbp), %rax
movl 0xc(%rax), %eax
movl %eax, %ecx
movq -0x10(%rbp), %rax
movzwl 0x12(%rax), %eax
movq -0x10(%rbp), %rdx
movslq 0x34(%rdx), %rdx
imulq %rdx, %rax
sarq $0x7, %rax
addq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x40(%rax)
movq -0x10(%rbp), %rax
movl 0x8(%rax), %eax
movl %eax, %ecx
movq -0x10(%rbp), %rax
movzwl 0x10(%rax), %eax
movq -0x10(%rbp), %rdx
movslq 0x34(%rdx), %rdx
imulq %rdx, %rax
sarq $0x8, %rax
addq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x48(%rax)
movq -0x10(%rbp), %rax
cmpl $0x0, 0x98(%rax)
je 0x30dd
movq -0x10(%rbp), %rax
cmpl $0x7d0, 0x38(%rax)      # imm = 0x7D0
jge 0x30db
movq -0x10(%rbp), %rax
movl 0x34(%rax), %eax
movq -0x10(%rbp), %rcx
imull 0x34(%rcx), %eax
sarl $0x1f, %eax
cltq
movq %rax, -0x38(%rbp)
movq -0x10(%rbp), %rax
movl 0x38(%rax), %eax
subl $0x7d0, %eax            # imm = 0x7D0
cltq
movq -0x10(%rbp), %rcx
movl 0x38(%rcx), %ecx
subl $0x7d0, %ecx            # imm = 0x7D0
movslq %ecx, %rcx
imulq %rcx, %rax
imulq $0x5, %rax, %rax
sarq %rax
movq %rax, -0x28(%rbp)
movq -0x10(%rbp), %rax
movl 0x38(%rax), %eax
subl $0x7d0, %eax            # imm = 0x7D0
cltq
movq -0x10(%rbp), %rcx
movl 0x38(%rcx), %ecx
subl $0x7d0, %ecx            # imm = 0x7D0
movslq %ecx, %rcx
imulq %rcx, %rax
imulq $0x5, %rax, %rax
sarq $0x2, %rax
movq %rax, -0x30(%rbp)
movq -0x10(%rbp), %rax
cmpl $0xfffffa24, 0x38(%rax) # imm = 0xFFFFFA24
jge 0x309e
movq -0x28(%rbp), %rax
movq -0x10(%rbp), %rcx
movl 0x38(%rcx), %ecx
addl $0x5dc, %ecx            # imm = 0x5DC
movslq %ecx, %rcx
movq -0x10(%rbp), %rdx
movl 0x38(%rdx), %edx
addl $0x5dc, %edx            # imm = 0x5DC
movslq %edx, %rdx
imulq %rdx, %rcx
imulq $0x7, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x28(%rbp)
movq -0x30(%rbp), %rax
movq -0x10(%rbp), %rcx
movl 0x38(%rcx), %ecx
addl $0x5dc, %ecx            # imm = 0x5DC
movslq %ecx, %rcx
movq -0x10(%rbp), %rdx
movl 0x38(%rdx), %edx
addl $0x5dc, %edx            # imm = 0x5DC
movslq %edx, %rdx
imulq %rdx, %rcx
imulq $0xb, %rcx, %rcx
sarq %rcx
addq %rcx, %rax
movq %rax, -0x30(%rbp)
movq -0x10(%rbp), %rax
movslq 0x38(%rax), %rax
subq -0x38(%rbp), %rax
movl %eax, %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x38(%rax)
movq -0x10(%rbp), %rax
movq 0x40(%rax), %rcx
subq -0x28(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x40(%rax)
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rcx
subq -0x30(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x48(%rax)
jmp 0x30dd
jmp 0x30df
jmp 0x30e1
leaq 0xc38c(%rip), %rax      # 0xf474
cmpl $0x1, (%rax)
jle 0x311c
movq 0xbf04(%rip), %rax      # 0xeff8
movq (%rax), %rdi
movq -0x10(%rbp), %rax
movzbl 0x4(%rax), %ecx
movq -0x10(%rbp), %rax
movl 0x28(%rax), %r8d
leaq 0x8193(%rip), %rsi      # 0xb2a1
leaq 0x815c(%rip), %rdx      # 0xb271
movb $0x0, %al
callq 0x21d0
leaq 0xc351(%rip), %rax      # 0xf474
cmpl $0x1, (%rax)
jle 0x3157
movq 0xbec9(%rip), %rax      # 0xeff8
movq (%rax), %rdi
movq -0x10(%rbp), %rax
movzbl 0x4(%rax), %ecx
movq -0x10(%rbp), %rax
movl 0x34(%rax), %r8d
leaq 0x816c(%rip), %rsi      # 0xb2b5
leaq 0x8121(%rip), %rdx      # 0xb271
movb $0x0, %al
callq 0x21d0
leaq 0xc316(%rip), %rax      # 0xf474
cmpl $0x0, (%rax)
jle 0x3192
movq 0xbe8e(%rip), %rax      # 0xeff8
movq (%rax), %rdi
movq -0x10(%rbp), %rax
movzbl 0x4(%rax), %ecx
movq -0x10(%rbp), %rax
movl 0x38(%rax), %r8d
leaq 0x8145(%rip), %rsi      # 0xb2c9
leaq 0x80e6(%rip), %rdx      # 0xb271
movb $0x0, %al
callq 0x21d0
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x40, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)