// File: /workspace/github_c_makefile_star2_O0/STMicroelectronics[P]STMems_Android_Sensor_HAL_Input/GyroSensor.cpp
// Function: GyroSensor::readEvents(sensors_event_t*, int)
int GyroSensor::readEvents(sensors_event_t* data, int count)
{
	if (count < 1)
		return -EINVAL;

	if (mHasPendingEvent) {
		mHasPendingEvent = false;
	}

	ssize_t n = mInputReader.fill(data_fd);
	if (n < 0)
		return n;

	int numEventReceived = 0;
	input_event const* event;

#if (FETCH_FULL_EVENT_BEFORE_RETURN)
	again:
#endif

	while (count && mInputReader.readEvent(&event)) {

		if (event->type == EVENT_TYPE_GYRO) {

#if (DEBUG_GYROSCOPE == 1)
	STLOGD("%s (event_code=%d)", __func__, event->code);
#endif

			float value = (float) event->value;
			if (event->code == EVENT_TYPE_GYRO_X) {
				data_raw[0] = value * CONVERT_GYRO_X;
			} else if (event->code == EVENT_TYPE_GYRO_Y) {
				data_raw[1] = value * CONVERT_GYRO_Y;
			} else if (event->code == EVENT_TYPE_GYRO_Z) {
				data_raw[2] = value * CONVERT_GYRO_Z;
			}
#if defined(GYRO_EVENT_HAS_TIMESTAMP)
			else if (event->code == EVENT_TYPE_TIME_MSB) {
				timestamp = ((int64_t)(event->value)) << 32;
			} else if (event->code == EVENT_TYPE_TIME_LSB) {
				timestamp |= (uint32_t)(event->value);
			}
#endif
			else {
				STLOGE("%s: unknown event code (type = %d, code = %d)",
					__func__, event->type, event->code);
			}
		} else if (event->type == EV_SYN) {
			if (startup_samples) {
				startup_samples--;

#if (DEBUG_GYROSCOPE == 1)
				STLOGD("%s: Start-up samples = %d",
					__func__, startup_samples);
#endif
				goto no_data;
			}

			data_rot[0] = data_raw[0]*matrix_gyr[0][0] +
					data_raw[1]*matrix_gyr[1][0] +
					data_raw[2]*matrix_gyr[2][0];
			data_rot[1] = data_raw[0]*matrix_gyr[0][1] +
					data_raw[1]*matrix_gyr[1][1] +
					data_raw[2]*matrix_gyr[2][1];
			data_rot[2] = data_raw[0]*matrix_gyr[0][2] +
					data_raw[1]*matrix_gyr[1][2] +
					data_raw[2]*matrix_gyr[2][2];

#if defined(STORE_CALIB_GYRO_ENABLED)
			data_rot[0] -= pStoreCalibration->getCalibration(
					StoreCalibration::GYROSCOPE_BIAS,
					StoreCalibration::XAxis);
			data_rot[1] -= pStoreCalibration->getCalibration(
					StoreCalibration::GYROSCOPE_BIAS,
					StoreCalibration::YAxis);
			data_rot[2] -= pStoreCalibration->getCalibration(
					StoreCalibration::GYROSCOPE_BIAS,
					StoreCalibration::ZAxis);
			data_rot[0] *= pStoreCalibration->getCalibration(
					StoreCalibration::GYROSCOPE_SENS,
					StoreCalibration::XAxis);
			data_rot[1] *= pStoreCalibration->getCalibration(
					StoreCalibration::GYROSCOPE_SENS,
					StoreCalibration::YAxis);
			data_rot[2] *= pStoreCalibration->getCalibration(
					StoreCalibration::GYROSCOPE_SENS,
					StoreCalibration::ZAxis);
#endif

#if !defined(GYRO_EVENT_HAS_TIMESTAMP)
			timestamp = timevalToNano(event->time);
#endif

#if !(GYROSCOPE_GBIAS_ESTIMATION_FUSION == 1)
			memset(gbias_out, 0, sizeof(gbias_out));
#if (GYROSCOPE_GBIAS_ESTIMATION_STANDALONE == 1)
#if (SENSORS_ACCELEROMETER_ENABLE == 1)
			sensors_vec_t tmp_data_acc;
			AccelSensor::getBufferData(&tmp_data_acc);
			memcpy(data_acc, tmp_data_acc.v, sizeof(float) * 3);
#else
			memset(data_acc, 0, sizeof(data_acc));
#endif
			iNemoEngine_API_gbias_Run(data_acc, data_rot);
			iNemoEngine_API_Get_gbias(gbias_out);
#endif
			DecimationCount[Gyro]++;
			if(mEnabled & (1<<Gyro) && (DecimationCount[Gyro] >= DecimationBuffer[Gyro])) {
				DecimationCount[Gyro] = 0;
				mPendingEvent[Gyro].data[0] = data_rot[0] - gbias_out[0];
				mPendingEvent[Gyro].data[1] = data_rot[1] - gbias_out[1];
				mPendingEvent[Gyro].data[2] = data_rot[2] - gbias_out[2];
				mPendingEvent[Gyro].timestamp = timestamp;
				mPendingEvent[Gyro].gyro.status = SENSOR_STATUS_ACCURACY_HIGH;

				*data++ = mPendingEvent[Gyro];
				count--;
				numEventReceived++;
			}

  #if ((SENSORS_UNCALIB_GYROSCOPE_ENABLE == 1) && (GYROSCOPE_GBIAS_ESTIMATION_STANDALONE == 1))
			DecimationCount[GyroUncalib]++;
			if(mEnabled & (1<<GyroUncalib) && (DecimationCount[GyroUncalib] >= DecimationBuffer[GyroUncalib])) {
				DecimationCount[GyroUncalib] = 0;
				mPendingEvent[GyroUncalib].uncalibrated_gyro.uncalib[0] = data_rot[0];
				mPendingEvent[GyroUncalib].uncalibrated_gyro.uncalib[1] = data_rot[1];
				mPendingEvent[GyroUncalib].uncalibrated_gyro.uncalib[2] = data_rot[2];
				mPendingEvent[GyroUncalib].uncalibrated_gyro.bias[0] = gbias_out[0];
				mPendingEvent[GyroUncalib].uncalibrated_gyro.bias[1] = gbias_out[1];
				mPendingEvent[GyroUncalib].uncalibrated_gyro.bias[2] = gbias_out[2];
				mPendingEvent[GyroUncalib].timestamp = timestamp;
				mPendingEvent[GyroUncalib].gyro.status = SENSOR_STATUS_ACCURACY_HIGH;

				*data++ = mPendingEvent[GyroUncalib];
				count--;
				numEventReceived++;
			}
  #endif
#endif
			if(mEnabled & (1<<iNemoGyro)) {
				sensors_vec_t sData;
				sData.x = data_rot[0] - gbias_out[0];
				sData.y = data_rot[1] - gbias_out[1];
				sData.z = data_rot[2] - gbias_out[2];
				setBufferData(&sData);
			}

#if (DEBUG_GYROSCOPE == 1)
			STLOGD("%s: (time = %lld), count(%d), received(%d)",
				__func__, mPendingEvent[Gyro].timestamp,
				count, numEventReceived);
#endif

		} else {
			STLOGE("%s: unknown event (type=%d, code=%d)",
				__func__, event->type, event->code);
		}
no_data:
		mInputReader.next();
	}

#if (FETCH_FULL_EVENT_BEFORE_RETURN)
	/* if we didn't read a complete event, see if we can fill and
	try again instead of returning with nothing and redoing poll. */
	if (numEventReceived == 0 && mEnabled == 1) {
		n = mInputReader.fill(data_fd);
		if (n)
			goto again;
	}
#endif

	return numEventReceived;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl %edx, -0x1c(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x58(%rbp)
cmpl $0x1, -0x1c(%rbp)
jge 0x803d
movl $0xffffffea, -0x4(%rbp) # imm = 0xFFFFFFEA
jmp 0x847e
movq -0x58(%rbp), %rax
testb $0x1, 0x1188(%rax)
je 0x8055
movq -0x58(%rbp), %rax
movb $0x0, 0x1188(%rax)
movq -0x58(%rbp), %rax
movq %rax, %rdi
addq $0x1028, %rdi           # imm = 0x1028
movl 0x1020(%rax), %esi
callq 0x50a0
movq %rax, -0x28(%rbp)
cmpq $0x0, -0x28(%rbp)
jge 0x8085
movq -0x28(%rbp), %rax
movl %eax, -0x4(%rbp)
jmp 0x847e
movl $0x0, -0x2c(%rbp)
xorl %eax, %eax
cmpl $0x0, -0x1c(%rbp)
movb %al, -0x59(%rbp)
je 0x80b5
movq -0x58(%rbp), %rdi
addq $0x1028, %rdi           # imm = 0x1028
leaq -0x38(%rbp), %rsi
callq 0x52a0
cmpq $0x0, %rax
setne %al
movb %al, -0x59(%rbp)
movb -0x59(%rbp), %al
testb $0x1, %al
jne 0x80c1
jmp 0x8478
movq -0x38(%rbp), %rax
movzwl 0x10(%rax), %eax
cmpl $0x4, %eax
jne 0x81b8
movq -0x38(%rbp), %rax
cvtsi2ssl 0x14(%rax), %xmm0
movss %xmm0, -0x3c(%rbp)
movq -0x38(%rbp), %rax
movzwl 0x12(%rax), %eax
cmpl $0x0, %eax
jne 0x810b
movq -0x58(%rbp), %rax
movss 0x2537(%rip), %xmm0     # 0xa630
mulss -0x3c(%rbp), %xmm0
movss %xmm0, 0x118c(%rax)
jmp 0x81b3
movq -0x38(%rbp), %rax
movzwl 0x12(%rax), %eax
cmpl $0x1, %eax
jne 0x8133
movq -0x58(%rbp), %rax
movss 0x250c(%rip), %xmm0     # 0xa630
mulss -0x3c(%rbp), %xmm0
movss %xmm0, 0x1190(%rax)
jmp 0x81b1
movq -0x38(%rbp), %rax
movzwl 0x12(%rax), %eax
cmpl $0x2, %eax
jne 0x815b
movq -0x58(%rbp), %rax
movss 0x24e4(%rip), %xmm0     # 0xa630
mulss -0x3c(%rbp), %xmm0
movss %xmm0, 0x1194(%rax)
jmp 0x81af
movq -0x38(%rbp), %rax
movzwl 0x12(%rax), %eax
cmpl $0x4, %eax
jne 0x8181
movq -0x58(%rbp), %rax
movq -0x38(%rbp), %rcx
movslq 0x14(%rcx), %rcx
shlq $0x20, %rcx
movq %rcx, 0x11a8(%rax)
jmp 0x81ad
movq -0x38(%rbp), %rax
movzwl 0x12(%rax), %eax
cmpl $0x7, %eax
jne 0x81a9
movq -0x58(%rbp), %rax
movq -0x38(%rbp), %rcx
movl 0x14(%rcx), %ecx
orq 0x11a8(%rax), %rcx
movq %rcx, 0x11a8(%rax)
jmp 0x81ab
jmp 0x81ab
jmp 0x81ad
jmp 0x81af
jmp 0x81b1
jmp 0x81b3
jmp 0x8461
movq -0x38(%rbp), %rax
movzwl 0x10(%rax), %eax
cmpl $0x0, %eax
jne 0x845d
movq 0x5d20(%rip), %rax      # 0xdef0
cmpl $0x0, (%rax)
je 0x81ef
movq 0x5d14(%rip), %rax      # 0xdef0
movl (%rax), %ecx
addl $-0x1, %ecx
movq 0x5d08(%rip), %rax      # 0xdef0
movl %ecx, (%rax)
jmp 0x8463
movq -0x58(%rbp), %rax
movss 0x118c(%rax), %xmm1
movswl 0x615e(%rip), %ecx      # 0xe360
cvtsi2ss %ecx, %xmm2
movss 0x1190(%rax), %xmm0
movswl 0x6151(%rip), %ecx      # 0xe366
cvtsi2ss %ecx, %xmm3
mulss %xmm3, %xmm0
mulss %xmm2, %xmm1
addss %xmm0, %xmm1
movss 0x1194(%rax), %xmm0
movswl 0x6138(%rip), %ecx      # 0xe36c
cvtsi2ss %ecx, %xmm2
mulss %xmm2, %xmm0
addss %xmm1, %xmm0
movss %xmm0, 0x1198(%rax)
movss 0x118c(%rax), %xmm1
movswl 0x610b(%rip), %ecx      # 0xe362
cvtsi2ss %ecx, %xmm2
movss 0x1190(%rax), %xmm0
movswl 0x60fe(%rip), %ecx      # 0xe368
cvtsi2ss %ecx, %xmm3
mulss %xmm3, %xmm0
mulss %xmm2, %xmm1
addss %xmm0, %xmm1
movss 0x1194(%rax), %xmm0
movswl 0x60e5(%rip), %ecx      # 0xe36e
cvtsi2ss %ecx, %xmm2
mulss %xmm2, %xmm0
addss %xmm1, %xmm0
movss %xmm0, 0x119c(%rax)
movss 0x118c(%rax), %xmm1
movswl 0x60b8(%rip), %ecx      # 0xe364
cvtsi2ss %ecx, %xmm2
movss 0x1190(%rax), %xmm0
movswl 0x60ab(%rip), %ecx      # 0xe36a
cvtsi2ss %ecx, %xmm3
mulss %xmm3, %xmm0
mulss %xmm2, %xmm1
addss %xmm0, %xmm1
movss 0x1194(%rax), %xmm0
movswl 0x6092(%rip), %ecx      # 0xe370
cvtsi2ss %ecx, %xmm2
mulss %xmm2, %xmm0
addss %xmm1, %xmm0
movss %xmm0, 0x11a0(%rax)
movq 0x5c47(%rip), %rdi      # 0xdf40
xorl %esi, %esi
movl $0xc, %edx
callq 0x5130
movq 0x5c44(%rip), %rax      # 0xdf50
movl (%rax), %ecx
addl $0x1, %ecx
movq 0x5c38(%rip), %rax      # 0xdf50
movl %ecx, (%rax)
movq 0x5cb7(%rip), %rax      # 0xdfd8
movl (%rax), %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0x83f2
movq 0x5c1a(%rip), %rax      # 0xdf50
movl (%rax), %eax
movq 0x5c41(%rip), %rcx      # 0xdf80
cmpl (%rcx), %eax
jl 0x83f2
movq -0x58(%rbp), %rsi
movq 0x5bfe(%rip), %rax      # 0xdf50
movl $0x0, (%rax)
movss 0x1198(%rsi), %xmm0
movq 0x5bd9(%rip), %rax      # 0xdf40
subss (%rax), %xmm0
movss %xmm0, 0x1068(%rsi)
movss 0x119c(%rsi), %xmm0
movq 0x5bbe(%rip), %rax      # 0xdf40
subss 0x4(%rax), %xmm0
movss %xmm0, 0x106c(%rsi)
movss 0x11a0(%rsi), %xmm0
movq 0x5ba2(%rip), %rax      # 0xdf40
subss 0x8(%rax), %xmm0
movss %xmm0, 0x1070(%rsi)
movq 0x11a8(%rsi), %rax
movq %rax, 0x1060(%rsi)
movb $0x3, 0x1074(%rsi)
addq $0x1050, %rsi           # imm = 0x1050
movq -0x18(%rbp), %rdi
movq %rdi, %rax
addq $0x68, %rax
movq %rax, -0x18(%rbp)
movl $0x68, %edx
callq 0x51a0
movl -0x1c(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x1c(%rbp)
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
movq 0x5bdf(%rip), %rax      # 0xdfd8
movl (%rax), %eax
andl $0x4, %eax
cmpl $0x0, %eax
je 0x845b
movq -0x58(%rbp), %rdi
movss 0x1198(%rdi), %xmm0
movq 0x5b2a(%rip), %rax      # 0xdf40
subss (%rax), %xmm0
movss %xmm0, -0x4c(%rbp)
movss 0x119c(%rdi), %xmm0
movq 0x5b12(%rip), %rax      # 0xdf40
subss 0x4(%rax), %xmm0
movss %xmm0, -0x48(%rbp)
movss 0x11a0(%rdi), %xmm0
movq 0x5af9(%rip), %rax      # 0xdf40
subss 0x8(%rax), %xmm0
movss %xmm0, -0x44(%rbp)
movq (%rdi), %rax
leaq -0x4c(%rbp), %rsi
callq *0x60(%rax)
jmp 0x845f
jmp 0x845f
jmp 0x8461
jmp 0x8463
movq -0x58(%rbp), %rdi
addq $0x1028, %rdi           # imm = 0x1028
callq 0x5220
jmp 0x808c
movl -0x2c(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x60, %rsp
popq %rbp
retq
nopw (%rax,%rax)