#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */
typedef int /*<<< orphan*/ uint8_t;

/* Variables and functions */
int /*<<< orphan*/ I2C_SLAVE;
scalar_t__ ioctl(int, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ read(int, int /*<<< orphan*/ *, int);
int /*<<< orphan*/ usleep(int);
int /*<<< orphan*/ write(int, int /*<<< orphan*/ *, int);
uint8_t read8(int fh, uint8_t addr) {
  if (ioctl(fh, I2C_SLAVE, 0x40) < 0)
    printf("ERROR in read8: ioctl before write()\n");
  write(fh, &addr, 1);
  usleep(50);
  uint8_t data;
  if (ioctl(fh, I2C_SLAVE, 0x40) < 0)
    printf("ERROR in read8: ioctl before read()\n");
  return read(fh, &data, 1);
}


x86 assembly (clang, -O0):
<read8>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2e80(%rip),%rax
mov    (%rax),%esi
mov    $0x40,%edx
call   1050 <ioctl@plt>
cmp    $0x0,%rax
jge    118a <read8+0x3a>
lea    0xe7b(%rip),%rdi
call   1040 <printf@plt>
mov    -0x4(%rbp),%edi
lea    -0x8(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    $0x32,%edi
call   1070 <usleep@plt>
mov    -0x4(%rbp),%edi
mov    0x2e39(%rip),%rax
mov    (%rax),%esi
mov    $0x40,%edx
call   1050 <ioctl@plt>
cmp    $0x0,%rax
jge    11d1 <read8+0x81>
lea    0xe5a(%rip),%rdi
call   1040 <printf@plt>
mov    -0x4(%rbp),%edi
lea    -0xc(%rbp),%rsi
mov    $0x1,%edx
call   1060 <read@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read8>:
push   %r14
push   %rbx
push   %rax
mov    %edi,%ebx
mov    %esi,(%rsp)
mov    0x2e88(%rip),%r14
mov    (%r14),%esi
mov    $0x40,%edx
call   1050 <ioctl@plt>
test   %rax,%rax
jns    117e <read8+0x2e>
lea    0xeab(%rip),%rdi
call   1030 <puts@plt>
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0x1,%edx
call   1040 <write@plt>
mov    $0x32,%edi
call   1070 <usleep@plt>
mov    (%r14),%esi
mov    %ebx,%edi
mov    $0x40,%edx
call   1050 <ioctl@plt>
test   %rax,%rax
jns    11b7 <read8+0x67>
lea    0xe4e(%rip),%rdi
call   1030 <puts@plt>
lea    0x4(%rsp),%rsi
mov    %ebx,%edi
mov    $0x1,%edx
call   1060 <read@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@I2C_SLAVE = global i32 0
@.str = [38 x i8] c"ERROR in read8: ioctl before write()\0A\00"
@.str.1 = [37 x i8] c"ERROR in read8: ioctl before read()\0A\00"
define i32 @read8(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store i32 %0, i32* %3
store i32 %1, i32* %4
%6 = load i32, i32* %3
%7 = load i32, i32* @I2C_SLAVE
%8 = call i64 @ioctl(i32 %6, i32 %7, i32 64)
%9 = icmp slt i64 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %2
%11 = call i32 @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0))
br label %12
12: ; preds = %10, %2
%13 = load i32, i32* %3
%14 = call i32 @write(i32 %13, i32* %4, i32 1)
%15 = call i32 @usleep(i32 50)
%16 = load i32, i32* %3
%17 = load i32, i32* @I2C_SLAVE
%18 = call i64 @ioctl(i32 %16, i32 %17, i32 64)
%19 = icmp slt i64 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %12
%21 = call i32 @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0))
br label %22
22: ; preds = %20, %12
%23 = load i32, i32* %3
%24 = call i32 @read(i32 %23, i32* %5, i32 1)
ret i32 %24
}

LLVM IR (-O2):
@I2C_SLAVE = global i32 0
@str = [36 x i8] c"ERROR in read8: ioctl before read()\00"
@str.2 = [37 x i8] c"ERROR in read8: ioctl before write()\00"
define i32 @read8(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
store i32 %1, i32* %3
%5 = load i32, i32* @I2C_SLAVE
%6 = tail call i64 @ioctl(i32 %0, i32 %5, i32 64)
%7 = icmp slt i64 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %2
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @str.2, i64 0, i64 0))
br label %10
10: ; preds = %8, %2
%11 = call i32 @write(i32 %0, i32* %3, i32 1)
%12 = tail call i32 @usleep(i32 50)
%13 = bitcast i32* %4 to i8*
%14 = load i32, i32* @I2C_SLAVE
%15 = tail call i64 @ioctl(i32 %0, i32 %14, i32 64)
%16 = icmp slt i64 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %10
%18 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @str, i64 0, i64 0))
br label %19
19: ; preds = %17, %10
%20 = call i32 @read(i32 %0, i32* %4, i32 1)
ret i32 %20
}