// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x20, %rsp
movq %rdi, -0x10(%rbp)
movl $0x1, -0x14(%rbp)
movq -0x10(%rbp), %rax
movb $0x0, 0x2c(%rax)
movq -0x10(%rbp), %rax
cmpl $0x0, 0x1c(%rax)
je 0x81a5
movq -0x10(%rbp), %rax
movl $0x0, 0x1c(%rax)
movq -0x10(%rbp), %rax
movl $0x0, 0x20(%rax)
movq -0x10(%rbp), %rax
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
movq %rcx, 0x8(%rax)
movq -0x10(%rbp), %rax
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
movq %rcx, 0x10(%rax)
movq -0x10(%rbp), %rax
cmpl $0x0, 0x1c(%rax)
jne 0x838b
movq -0x10(%rbp), %rdi
callq 0x7bf0
cmpl $0x0, %eax
jne 0x81d8
movq -0x10(%rbp), %rax
movl $0x0, 0x20(%rax)
movl $0x0, -0x4(%rbp)
jmp 0x83a8
movl $0x1, -0x18(%rbp)
movl $0x0, -0x1c(%rbp)
movl $0x0, -0x20(%rbp)
movq -0x10(%rbp), %rdi
movl $0xf0, %esi
callq 0x7cf0
movl -0x18(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl 0x20(%rcx), %eax
jge 0x823f
movq -0x10(%rbp), %rax
movslq -0x20(%rbp), %rcx
movq 0x8(%rax,%rcx,8), %rax
movslq -0x14(%rbp), %rcx
andq %rcx, %rax
cmpq $0x0, %rax
je 0x8230
movq -0x10(%rbp), %rax
movl 0x18(%rax), %ecx
orl $0x4, %ecx
movl %ecx, 0x18(%rax)
jmp 0x823d
movq -0x10(%rbp), %rax
movl 0x18(%rax), %ecx
andl $0x3, %ecx
movl %ecx, 0x18(%rax)
jmp 0x8269
movl -0x18(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl 0x20(%rcx), %eax
jne 0x825a
movq -0x10(%rbp), %rax
movl 0x18(%rax), %ecx
orl $0x4, %ecx
movl %ecx, 0x18(%rax)
jmp 0x8267
movq -0x10(%rbp), %rax
movl 0x18(%rax), %ecx
andl $0x3, %ecx
movl %ecx, 0x18(%rax)
jmp 0x8269
movq -0x10(%rbp), %rdi
callq 0x8050
cmpl $0x0, %eax
jne 0x8283
movl $0x0, -0x4(%rbp)
jmp 0x83a8
movq -0x10(%rbp), %rax
cmpl $0x0, 0x18(%rax)
jne 0x8293
movl -0x18(%rbp), %eax
movl %eax, -0x1c(%rbp)
movq -0x10(%rbp), %rax
movl 0x18(%rax), %eax
andl $0x3, %eax
cmpl $0x3, %eax
jne 0x82a4
jmp 0x8319
movq -0x10(%rbp), %rax
movl 0x18(%rax), %eax
andl $0x4, %eax
cmpl $0x0, %eax
je 0x82cb
movslq -0x14(%rbp), %rdx
movq -0x10(%rbp), %rax
movslq -0x20(%rbp), %rcx
orq 0x8(%rax,%rcx,8), %rdx
movq %rdx, 0x8(%rax,%rcx,8)
jmp 0x82e6
movl -0x14(%rbp), %eax
xorl $-0x1, %eax
movslq %eax, %rdx
movq -0x10(%rbp), %rax
movslq -0x20(%rbp), %rcx
andq 0x8(%rax,%rcx,8), %rdx
movq %rdx, 0x8(%rax,%rcx,8)
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
movl -0x14(%rbp), %eax
shll %eax
movl %eax, -0x14(%rbp)
cmpl $0x0, -0x14(%rbp)
jne 0x830d
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
movl $0x1, -0x14(%rbp)
jmp 0x830f
cmpl $0x2, -0x20(%rbp)
jl 0x81fb
cmpl $0x41, -0x18(%rbp)
jne 0x8389
movl -0x1c(%rbp), %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x20(%rax)
movq -0x10(%rbp), %rax
cmpl $0x0, 0x20(%rax)
jne 0x8340
movq -0x10(%rbp), %rax
movl $0x1, 0x1c(%rax)
jmp 0x834b
movq -0x10(%rbp), %rax
movl $0x0, 0x1c(%rax)
movq -0x10(%rbp), %rdi
callq 0x83c0
cmpl $0x0, %eax
je 0x8387
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
andq $0xff, %rax
cmpq $0x28, %rax
jne 0x837e
movq -0x10(%rbp), %rax
movb $0x1, 0x2c(%rax)
movl $0x1, -0x4(%rbp)
jmp 0x83a8
movl $0x1, -0x4(%rbp)
jmp 0x83a8
jmp 0x8389
jmp 0x838b
movq -0x10(%rbp), %rax
movl $0x0, 0x20(%rax)
movq -0x10(%rbp), %rax
movl $0x0, 0x1c(%rax)
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x20, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)