#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 */

/* Variables and functions */
int /*<<< orphan*/ I2C_SLAVE;
int i2c_smbus_read_byte(int);
int i2c_smbus_write_word_data(int, int, int);
scalar_t__ ioctl(int, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ usleep(unsigned int);
void communicate(int device) {
  if (ioctl(device, I2C_SLAVE, 0x12) < 0) {
    perror("ioctl() I2C_SLAVE failed\n");
    return;
  }
  unsigned int sleepTime = 250 * 1000;
  unsigned int sleepTimeLong = 1000 * 1000;
  printf("Reading block data from slave\n");
  int res;
  int x = 10;
  int counter = 0;
  while (x == 10) {
    res = i2c_smbus_read_byte(device);
    counter++;
    printf("Writing 2bytes to slave\n");
    if (counter >= 65000) {
      counter = 0;
    }
    unsigned char high = (unsigned char)(counter >> 8);
    unsigned char low = counter & 0xff;
    int err = i2c_smbus_write_word_data(device, 0x72, counter);
    printf("err:%i\n", err);
    usleep(sleepTime);
  }
}


x86 assembly (clang, -O0):
<communicate>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2e73(%rip),%rax
mov    (%rax),%esi
mov    $0x12,%edx
call   1040 <ioctl@plt>
cmp    $0x0,%rax
jge    119c <communicate+0x3c>
lea    0xe6e(%rip),%rdi
call   1070 <perror@plt>
jmp    124b <communicate+0xeb>
movl   $0x3d090,-0x8(%rbp)
movl   $0xf4240,-0xc(%rbp)
lea    0xe69(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0xa,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
cmpl   $0xa,-0x14(%rbp)
jne    124b <communicate+0xeb>
mov    -0x4(%rbp),%edi
call   1050 <i2c_smbus_read_byte@plt>
mov    %eax,-0x10(%rbp)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
lea    0xe4e(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
cmpl   $0xfde8,-0x18(%rbp)
jl     1206 <communicate+0xa6>
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
sar    $0x8,%eax
mov    %al,-0x19(%rbp)
mov    -0x18(%rbp),%eax
and    $0xff,%eax
mov    %al,-0x1a(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x18(%rbp),%edx
mov    $0x72,%esi
call   1060 <i2c_smbus_write_word_data@plt>
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%esi
lea    0xe1b(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x8(%rbp),%edi
call   1080 <usleep@plt>
jmp    11c6 <communicate+0x66>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<communicate>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edi,%r13d
mov    0x2e63(%rip),%rax
mov    (%rax),%esi
mov    $0x12,%edx
call   1050 <ioctl@plt>
test   %rax,%rax
js     1206 <communicate+0x96>
lea    0xe85(%rip),%rdi
call   1030 <puts@plt>
xor    %r12d,%r12d
lea    0xe94(%rip),%r14
lea    0xe67(%rip),%r15
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r13d,%edi
call   1060 <i2c_smbus_read_byte@plt>
lea    0x1(%rbx),%ebp
mov    %r14,%rdi
call   1030 <puts@plt>
cmp    $0xfde7,%ebx
cmovge %r12d,%ebp
mov    %r13d,%edi
mov    $0x72,%esi
mov    %ebp,%edx
call   1070 <i2c_smbus_write_word_data@plt>
mov    %r15,%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x3d090,%edi
call   1090 <usleep@plt>
mov    %ebp,%ebx
jmp    11c0 <communicate+0x50>
lea    0xdf3(%rip),%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1080 <perror@plt>

LLVM IR (-O0):
@I2C_SLAVE = global i32 0
@.str = [26 x i8] c"ioctl() I2C_SLAVE failed\0A\00"
@.str.1 = [31 x i8] c"Reading block data from slave\0A\00"
@.str.2 = [25 x i8] c"Writing 2bytes to slave\0A\00"
@.str.3 = [8 x i8] c"err:%i\0A\00"
define void @communicate(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8
%9 = alloca i8
%10 = alloca i32
store i32 %0, i32* %2
%11 = load i32, i32* %2
%12 = load i32, i32* @I2C_SLAVE
%13 = call i64 @ioctl(i32 %11, i32 %12, i32 18)
%14 = icmp slt i64 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %1
%16 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
br label %45
17: ; preds = %1
store i32 250000, i32* %3
store i32 1000000, i32* %4
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0))
store i32 10, i32* %6
store i32 0, i32* %7
br label %19
19: ; preds = %31, %17
%20 = load i32, i32* %6
%21 = icmp eq i32 %20, 10
br i1 %21, label %22, label %45
22: ; preds = %19
%23 = load i32, i32* %2
%24 = call i32 @i2c_smbus_read_byte(i32 %23)
store i32 %24, i32* %5
%25 = load i32, i32* %7
%26 = add nsw i32 %25, 1
store i32 %26, i32* %7
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
%28 = load i32, i32* %7
%29 = icmp sge i32 %28, 65000
br i1 %29, label %30, label %31
30: ; preds = %22
store i32 0, i32* %7
br label %31
31: ; preds = %30, %22
%32 = load i32, i32* %7
%33 = ashr i32 %32, 8
%34 = trunc i32 %33 to i8
store i8 %34, i8* %8
%35 = load i32, i32* %7
%36 = and i32 %35, 255
%37 = trunc i32 %36 to i8
store i8 %37, i8* %9
%38 = load i32, i32* %2
%39 = load i32, i32* %7
%40 = call i32 @i2c_smbus_write_word_data(i32 %38, i32 114, i32 %39)
store i32 %40, i32* %10
%41 = load i32, i32* %10
%42 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i32 %41)
%43 = load i32, i32* %3
%44 = call i32 @usleep(i32 %43)
br label %19
45: ; preds = %15, %19
ret void
}

LLVM IR (-O2):
@I2C_SLAVE = global i32 0
@.str = [26 x i8] c"ioctl() I2C_SLAVE failed\0A\00"
@.str.3 = [8 x i8] c"err:%i\0A\00"
@str = [30 x i8] c"Reading block data from slave\00"
@str.4 = [24 x i8] c"Writing 2bytes to slave\00"
define void @communicate(i32 %0) {
%2 = load i32, i32* @I2C_SLAVE
%3 = tail call i64 @ioctl(i32 %0, i32 %2, i32 18)
%4 = icmp slt i64 %3, 0
br i1 %4, label %5, label %7
5: ; preds = %1
%6 = tail call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
ret void
7: ; preds = %1
%8 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @str, i64 0, i64 0))
br label %9
9: ; preds = %9, %7
%10 = phi i32 [ 0, %7 ], [ %15, %9 ]
%11 = tail call i32 @i2c_smbus_read_byte(i32 %0)
%12 = add nsw i32 %10, 1
%13 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @str.4, i64 0, i64 0))
%14 = icmp sgt i32 %10, 64998
%15 = select i1 %14, i32 0, i32 %12
%16 = tail call i32 @i2c_smbus_write_word_data(i32 %0, i32 114, i32 %15)
%17 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i32 %16)
%18 = tail call i32 @usleep(i32 250000)
br label %9
}