// File: /workspace/github_c_makefile_star2_O3/Openvario[P]sensord/ds2482.c
// Function: OWSearch
int OWSearch(t_ds2482 *sensor) {
	// server.log("Function: OneWire Search");

	int deviceAddress4ByteMask = 1;

	sensor->temp_present=0;
	if (sensor->owLastDevice) {
		// server.log("OneWire Search Complete");
		sensor->owLastDevice = 0;
		sensor->owLastDiscrepancy = 0;
		sensor->owDeviceAddress[0] = 0xFFFFFFFF;
		sensor->owDeviceAddress[1] = 0xFFFFFFFF;
	}
	if (!sensor->owLastDevice) { // if the last call was not the last one
		if (OWReset(sensor)==0) { // if there are no parts on 1-wire, return false
			sensor->owLastDiscrepancy = 0;
			return 0;
		}
		int bitNumber = 1;
		int lastZero = 0;
		int deviceAddress4ByteIndex = 0; // Fill last 4 bytes first, data from onewire comes LSB first.

		OWWriteByte(sensor, 0xF0); //Issue the Search ROM command
		do { // loop to do the search
			if (bitNumber < sensor->owLastDiscrepancy) {
				if (sensor->owDeviceAddress[deviceAddress4ByteIndex] & deviceAddress4ByteMask)
					sensor->owTriplet |= 4;  else sensor->owTriplet &= 3;
			} else if (bitNumber == sensor->owLastDiscrepancy) // if equal to last pick 1, if not pick 0
				sensor->owTriplet |= 4; else sensor->owTriplet &= 3;
			if (!OWTriplet(sensor)) return 0;

			// if 0 was picked then record its position in lastZero
			if (sensor->owTriplet==0) lastZero = bitNumber;

			// check for no devices on 1-wire
			if ((sensor->owTriplet&3)==3) break;

			// set or clear the bit in the SerialNum byte serial_byte_number with mask
			if (sensor->owTriplet&4)
				sensor->owDeviceAddress[deviceAddress4ByteIndex] |= deviceAddress4ByteMask;
			else
				sensor->owDeviceAddress[deviceAddress4ByteIndex] &= ~deviceAddress4ByteMask;
			bitNumber++; // increment the byte counter bit number
			deviceAddress4ByteMask <<=  1; // shift the bit mask left

			if (!deviceAddress4ByteMask) { // if the mask is 0 then go to other address block and reset mask to first bit
				deviceAddress4ByteIndex++;
				deviceAddress4ByteMask = 1;
			}
		} while (deviceAddress4ByteIndex <2);

		if (bitNumber == 65) { // if the search was successful then
			sensor->owLastDiscrepancy = lastZero;
			if (sensor->owLastDiscrepancy==0) sensor->owLastDevice = 1; else sensor->owLastDevice = 0;
			// server.log(format("OneWire Device Address = %.8X%.8X", owDeviceAddress[1], owDeviceAddress[0]));
			if (OWCheckCRC(sensor)) {
				if ((sensor->owDeviceAddress[0] & 0xff) == 0x28) {
					sensor->temp_present=1; return 1;
				} else {
					// server.log("OneWire device address CRC check failed");
					return 1;
				}
			}
		}
	}
	// server.log("No One-Wire Devices Found, Resetting Search");
	sensor->owLastDiscrepancy = 0;
	sensor->owLastDevice = 0;
	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rdi, %rbx
movb $0x0, 0x2c(%rdi)
cmpl $0x0, 0x1c(%rdi)
je 0x7669
movq $0x0, 0x1c(%rbx)
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movq %rax, 0x8(%rbx)
movq %rax, 0x10(%rbx)
movq %rbx, %rdi
callq 0x72c0
testl %eax, %eax
je 0x775f
movq %rbx, %rdi
movl $0xf0, %esi
callq 0x7354
xorl %r14d, %r14d
movl $0x1, %r13d
movl $0x1, %r15d
xorl %r12d, %r12d
xorl %ebp, %ebp
movl %r12d, %edx
cmpl 0x20(%rbx), %r15d
movq %rdx, (%rsp)
jge 0x76b9
movslq %r13d, %rcx
movl 0x18(%rbx), %eax
testq %rcx, 0x8(%rbx,%rdx,8)
je 0x76be
orl $0x4, %eax
jmp 0x76c1
movl 0x18(%rbx), %eax
je 0x76b4
andl $0x3, %eax
movl %eax, 0x18(%rbx)
movq %rbx, %rdi
callq 0x759a
testl %eax, %eax
je 0x7772
movl 0x18(%rbx), %eax
testl %eax, %eax
cmovel %r15d, %r14d
movl %eax, %ecx
notl %ecx
testb $0x3, %cl
je 0x772d
testb $0x4, %al
jne 0x76fc
movl %r13d, %eax
notl %eax
cltq
movq (%rsp), %rcx
andq %rax, 0x8(%rbx,%rcx,8)
jmp 0x7708
movslq %r13d, %rax
movq (%rsp), %rcx
orq %rax, 0x8(%rbx,%rcx,8)
addl %r13d, %r13d
cmpl $0x1, %r13d
movl %r13d, %eax
adcl $0x0, %eax
cmpl $0x1, %r13d
adcl $0x0, %r12d
incl %r15d
movl %eax, %r13d
cmpl $0x2, %r12d
jb 0x769a
cmpl $0x41, %r15d
jne 0x776a
movl %r14d, 0x20(%rbx)
xorl %eax, %eax
testl %r14d, %r14d
sete %al
movl %eax, 0x1c(%rbx)
movq %rbx, %rdi
callq 0x7783
testl %eax, %eax
je 0x776a
movl $0x1, %ebp
cmpb $0x28, 0x8(%rbx)
jne 0x7772
movb $0x1, 0x2c(%rbx)
jmp 0x7772
movl $0x0, 0x20(%rbx)
xorl %ebp, %ebp
jmp 0x7772
movq $0x0, 0x1c(%rbx)
movl %ebp, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq