// File: /workspace/github_c_makefile_star2_O0/Openvario[P]sensord/ds2482.c
// Function: OWWriteByte
int OWWriteByte(t_ds2482 *sensor, unsigned char writeval) {
	int i;
	unsigned char data[2];

	// server.log("Function: Write Byte to One-Wire");
	if (write(sensor->fd,"\xe1\xf0",2)!=2) { // Device failed to acknowledge
		// server.log("I2C Write Failed");
		return -1;
	}
	for (i=0;i<100;++i) {
		if (read(sensor->fd,data,1)==0) { // Read the status register
			// server.log("I2C Read Status Failed");
			return -1;
		} else {
			// server.log(format("Read Status Byte = %d", data[0]));
			if (data[0] & 0x01) { // 1-Wire Busy bit
				// server.log("One-Wire bus is busy");
				struct timespec nstime = {0,1e6};
				while (nanosleep(&nstime,&nstime)) ; // Wait, try again

			} else {
				// server.log("One-Wire bus is idle");
				break;
			}
		}
	}
	if (i == 100) {
		// server.log("One-Wire busy for too long");
		return -1;
	}
	data[0]=0xa5;
	data[1]=writeval;
	if (write(sensor->fd,data,2)!=2) { // set write byte command (A5) and send data (byte)
		// Device failed to acknowledge
		// server.log(format("I2C Write Byte Failed. Data: %#.2X", byte));
		return -1;
	}
	for (i=0;i<100;++i) {
		if (read(sensor->fd,data,1)==0) { // Read the status register
			// server.log("I2C Read Status Failed");
			return -1;
		} else {
			// server.log(format("Read Status Byte = %d", data[0]));
			if (data[0] & 1) { // 1-Wire Busy bit
				// server.log("One-Wire bus is busy");
				struct timespec nstime = {0,1e6};
				while (nanosleep(&nstime,&nstime)) ; // Wait, try again

			} else {
				// server.log("One-Wire bus is idle");
				break;
			}
		}
	}
	if (i == 100) {
		// server.log("One-Wire busy for too long");
		return -1;
	}

	// server.log("One-Wire Write Byte complete");
	return 1;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movb %sil, %al
movq %rdi, -0x10(%rbp)
movb %al, -0x11(%rbp)
movq -0x10(%rbp), %rax
movl (%rax), %edi
leaq 0x4be2(%rip), %rsi      # 0xc8f1
movl $0x2, %edx
callq 0x20a0
cmpq $0x2, %rax
je 0x7d2b
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x7e76
movl $0x0, -0x18(%rbp)
cmpl $0x64, -0x18(%rbp)
jge 0x7da6
movq -0x10(%rbp), %rax
movl (%rax), %edi
leaq -0x1a(%rbp), %rsi
movl $0x1, %edx
callq 0x2180
cmpq $0x0, %rax
jne 0x7d5e
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x7e76
movzbl -0x1a(%rbp), %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0x7d95
movq 0x4bb7(%rip), %rax      # 0xc928
movq %rax, -0x30(%rbp)
movq 0x4bb4(%rip), %rax      # 0xc930
movq %rax, -0x28(%rbp)
leaq -0x30(%rbp), %rsi
movq %rsi, %rdi
callq 0x2120
cmpl $0x0, %eax
je 0x7d93
jmp 0x7d80
jmp 0x7d97
jmp 0x7da6
jmp 0x7d99
jmp 0x7d9b
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
jmp 0x7d32
cmpl $0x64, -0x18(%rbp)
jne 0x7db8
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x7e76
movb $-0x5b, -0x1a(%rbp)
movb -0x11(%rbp), %al
movb %al, -0x19(%rbp)
movq -0x10(%rbp), %rax
movl (%rax), %edi
leaq -0x1a(%rbp), %rsi
movl $0x2, %edx
callq 0x20a0
cmpq $0x2, %rax
je 0x7de8
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x7e76
movl $0x0, -0x18(%rbp)
cmpl $0x64, -0x18(%rbp)
jge 0x7e60
movq -0x10(%rbp), %rax
movl (%rax), %edi
leaq -0x1a(%rbp), %rsi
movl $0x1, %edx
callq 0x2180
cmpq $0x0, %rax
jne 0x7e18
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x7e76
movzbl -0x1a(%rbp), %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0x7e4f
movq 0x4afd(%rip), %rax      # 0xc928
movq %rax, -0x40(%rbp)
movq 0x4afa(%rip), %rax      # 0xc930
movq %rax, -0x38(%rbp)
leaq -0x40(%rbp), %rsi
movq %rsi, %rdi
callq 0x2120
cmpl $0x0, %eax
je 0x7e4d
jmp 0x7e3a
jmp 0x7e51
jmp 0x7e60
jmp 0x7e53
jmp 0x7e55
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
jmp 0x7def
cmpl $0x64, -0x18(%rbp)
jne 0x7e6f
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x7e76
movl $0x1, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x40, %rsp
popq %rbp
retq
nop