// File: /workspace/github_c_makefile_star2_O0/STMicroelectronics[P]STMems_Android_Sensor_HAL_Input/AccelSensor.cpp
// Function: AccelSensor::enable(int, int, int)
int AccelSensor::enable(int32_t handle, int en,
			int  __attribute__((unused))type)
{
	int err = 0, errSM1 = 0, errSM2 = 0;
	int flags = en ? 1 : 0;
	int what = -1;

	what = getWhatFromHandle(handle);
	if (what < 0)
		return what;

	if (flags) {
#if (SENSORS_SIGNIFICANT_MOTION_ENABLE == 1)
		if (what == SignificantMotion) {
			//enable sysfs state machine
			if(!(mEnabled & (1<<Acceleration)))
				errSM1 = writeSysfsCommand(SENSORS_SIGNIFICANT_MOTION_HANDLE,
							   SIGN_MOTION_POLL_EN_FILE_NAME,
							   "%lld" ,0);

			errSM2 = writeEnable(SENSORS_SIGNIFICANT_MOTION_HANDLE,
					     SIGN_MOTION_ENABLE_VALUE);

		}
		if (what == Acceleration) {
			errSM2 = writeSysfsCommand(SENSORS_ACCELEROMETER_HANDLE,
						   SIGN_MOTION_POLL_EN_FILE_NAME,
						   "%lld", 1);
		}
#endif
		mEnabled |= (1<<what);
		writeMinDelay();

		if ((mEnabled == (1 << what)) &&
		    (errSM1 >= 0) &&
		    (errSM2 >= 0)) {
#if !defined(NOT_SET_ACC_INITIAL_STATE)
			setInitialState();
#endif
			err = writeEnable(SENSORS_ACCELEROMETER_HANDLE,
					  flags);
		}

#if (ACCEL_CALIBRATION_ENABLE == 1)
		ST_AccCalibration_API_Init(ACCEL_CALIB_PERIOD_MS);
#endif

	} else {
		int tmp = mEnabled;
		mEnabled &= ~(1<<what);
#if (SENSORS_SIGNIFICANT_MOTION_ENABLE == 1)
		if (what == SignificantMotion)
			errSM1 = writeEnable(SENSORS_SIGNIFICANT_MOTION_HANDLE,
					     SIGN_MOTION_DISABLE_VALUE);
		if (what == Acceleration)
			errSM2 = writeSysfsCommand(SENSORS_ACCELEROMETER_HANDLE,
						   SIGN_MOTION_POLL_EN_FILE_NAME,
						   "%lld", 0);
#endif
		if((!mEnabled) && (tmp != 0)) {
			err = writeEnable(SENSORS_ACCELEROMETER_HANDLE,
					  flags);
		}
		if ( (errSM1 < 0) || (errSM2 < 0) ){
			err = -1;
			mEnabled = tmp;
		}
#if (ACCEL_CALIBRATION_ENABLE == 1)
		ST_AccCalibration_API_DeInit(ACCEL_CALIB_PERIOD_MS);
#endif
		//setDelay(handle, DELAY_OFF);
		if (mEnabled) {
			writeMinDelay();
		}
	}

	if(err >= 0 ) {
		STLOGD("%s: (%d), handle: %d, what: %d, mEnabled: %x",
		       __func__, flags, handle, what, mEnabled);
	} else {
		STLOGE("%s: (%d), handle: %d, what: %d, mEnabled: %x",
		       __func__, flags, handle, what, mEnabled);
	}

	return err;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movl %edx, -0x18(%rbp)
movl %ecx, -0x1c(%rbp)
movq -0x10(%rbp), %rdi
movq %rdi, -0x40(%rbp)
movl $0x0, -0x20(%rbp)
movl $0x0, -0x24(%rbp)
movl $0x0, -0x28(%rbp)
movl -0x18(%rbp), %edx
xorl %eax, %eax
movl $0x1, %ecx
cmpl $0x0, %edx
cmovnel %ecx, %eax
movl %eax, -0x2c(%rbp)
movl $0xffffffff, -0x30(%rbp) # imm = 0xFFFFFFFF
movl -0x14(%rbp), %esi
movq (%rdi), %rax
callq *0x58(%rax)
movl %eax, -0x30(%rbp)
cmpl $0x0, -0x30(%rbp)
jge 0x6dd9
movl -0x30(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x6eea
cmpl $0x0, -0x2c(%rbp)
je 0x6e4f
movq -0x40(%rbp), %rdi
movl -0x30(%rbp), %ecx
movl $0x1, %eax
shll %cl, %eax
movl %eax, %ecx
movq 0x71da(%rip), %rax      # 0xdfd0
orl (%rax), %ecx
movq 0x71d1(%rip), %rax      # 0xdfd0
movl %ecx, (%rax)
movq (%rdi), %rax
callq *0x68(%rax)
movq 0x71c2(%rip), %rax      # 0xdfd0
movl (%rax), %eax
movl -0x30(%rbp), %ecx
movl $0x1, %edx
shll %cl, %edx
movl %edx, %ecx
cmpl %ecx, %eax
jne 0x6e4a
cmpl $0x0, -0x24(%rbp)
jl 0x6e4a
cmpl $0x0, -0x28(%rbp)
jl 0x6e4a
movq -0x40(%rbp), %rdi
callq 0x50d0
movq -0x40(%rbp), %rdi
movl -0x2c(%rbp), %edx
movq (%rdi), %rax
movl $0x1, %esi
callq *0x40(%rax)
movl %eax, -0x20(%rbp)
jmp 0x6eda
movq 0x717a(%rip), %rax      # 0xdfd0
movl (%rax), %eax
movl %eax, -0x34(%rbp)
movl -0x30(%rbp), %ecx
movl $0x1, %eax
shll %cl, %eax
movl %eax, %ecx
xorl $-0x1, %ecx
movq 0x715f(%rip), %rax      # 0xdfd0
andl (%rax), %ecx
movq 0x7156(%rip), %rax      # 0xdfd0
movl %ecx, (%rax)
movq 0x714d(%rip), %rax      # 0xdfd0
cmpl $0x0, (%rax)
jne 0x6ea3
cmpl $0x0, -0x34(%rbp)
je 0x6ea3
movq -0x40(%rbp), %rdi
movl -0x2c(%rbp), %edx
movq (%rdi), %rax
movl $0x1, %esi
callq *0x40(%rax)
movl %eax, -0x20(%rbp)
cmpl $0x0, -0x24(%rbp)
jl 0x6eaf
cmpl $0x0, -0x28(%rbp)
jge 0x6ec2
movl $0xffffffff, -0x20(%rbp) # imm = 0xFFFFFFFF
movl -0x34(%rbp), %ecx
movq 0x7110(%rip), %rax      # 0xdfd0
movl %ecx, (%rax)
movq 0x7107(%rip), %rax      # 0xdfd0
cmpl $0x0, (%rax)
je 0x6ed8
movq -0x40(%rbp), %rdi
movq (%rdi), %rax
callq *0x68(%rax)
jmp 0x6eda
cmpl $0x0, -0x20(%rbp)
jl 0x6ee2
jmp 0x6ee4
jmp 0x6ee4
movl -0x20(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x40, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)