#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*/ perror(char *);
int usleep(int);
int write(int, unsigned char *, int);
int send_i2c_data(int fd, unsigned char *data) {
  unsigned char *ptr = &data[1];
  if (write(fd, data, 2) != 2) {
    perror("write");
    return -1;
  }
  int err = usleep(1);
  if (err == -1)
    return -1;
  *ptr |= (1 << 2);
  if (write(fd, data, 2) != 2) {
    perror("write");
    return -1;
  }
  err = usleep(1);
  if (err == -1)
    return -1;
  *ptr &= ~(1 << 2);
  if (write(fd, data, 2) != 2) {
    perror("write");
    return -1;
  }
  err = usleep(1);
  if (err == -1)
    return -1;
  *ptr |= (1 << 2);
  if (write(fd, data, 2) != 2) {
    perror("write");
    return -1;
  }
  err = usleep(1);
  if (err == -1)
    return -1;
  *ptr &= ~(1 << 2);
  if (write(fd, data, 2) != 2) {
    perror("write");
    return -1;
  }
  err = usleep(100);
  if (err == -1)
    return -1;
  return 0;
}


x86 assembly (clang, -O0):
<send_i2c_data>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    $0x2,%edx
call   1030 <write@plt>
cmp    $0x2,%eax
je     117d <send_i2c_data+0x4d>
lea    0xe94(%rip),%rdi
call   1040 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    132b <send_i2c_data+0x1fb>
mov    $0x1,%edi
call   1050 <usleep@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    11a0 <send_i2c_data+0x70>
movl   $0xffffffff,-0x4(%rbp)
jmp    132b <send_i2c_data+0x1fb>
mov    -0x18(%rbp),%rax
movzbl (%rax),%ecx
or     $0x4,%ecx
mov    %cl,(%rax)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    $0x2,%edx
call   1030 <write@plt>
cmp    $0x2,%eax
je     11de <send_i2c_data+0xae>
lea    0xe33(%rip),%rdi
call   1040 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    132b <send_i2c_data+0x1fb>
mov    $0x1,%edi
call   1050 <usleep@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    1201 <send_i2c_data+0xd1>
movl   $0xffffffff,-0x4(%rbp)
jmp    132b <send_i2c_data+0x1fb>
mov    -0x18(%rbp),%rax
movzbl (%rax),%ecx
and    $0xfffffffb,%ecx
mov    %cl,(%rax)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    $0x2,%edx
call   1030 <write@plt>
cmp    $0x2,%eax
je     123f <send_i2c_data+0x10f>
lea    0xdd2(%rip),%rdi
call   1040 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    132b <send_i2c_data+0x1fb>
mov    $0x1,%edi
call   1050 <usleep@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    1262 <send_i2c_data+0x132>
movl   $0xffffffff,-0x4(%rbp)
jmp    132b <send_i2c_data+0x1fb>
mov    -0x18(%rbp),%rax
movzbl (%rax),%ecx
or     $0x4,%ecx
mov    %cl,(%rax)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    $0x2,%edx
call   1030 <write@plt>
cmp    $0x2,%eax
je     12a0 <send_i2c_data+0x170>
lea    0xd71(%rip),%rdi
call   1040 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    132b <send_i2c_data+0x1fb>
mov    $0x1,%edi
call   1050 <usleep@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    12c3 <send_i2c_data+0x193>
movl   $0xffffffff,-0x4(%rbp)
jmp    132b <send_i2c_data+0x1fb>
mov    -0x18(%rbp),%rax
movzbl (%rax),%ecx
and    $0xfffffffb,%ecx
mov    %cl,(%rax)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    $0x2,%edx
call   1030 <write@plt>
cmp    $0x2,%eax
je     1301 <send_i2c_data+0x1d1>
lea    0xd10(%rip),%rdi
call   1040 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    132b <send_i2c_data+0x1fb>
mov    $0x64,%edi
call   1050 <usleep@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    1324 <send_i2c_data+0x1f4>
movl   $0xffffffff,-0x4(%rbp)
jmp    132b <send_i2c_data+0x1fb>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<send_i2c_data>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%rbx
mov    %edi,%ebp
mov    $0x2,%edx
call   1030 <write@plt>
cmp    $0x2,%eax
jne    1221 <send_i2c_data+0xf1>
mov    $0x1,%edi
call   1050 <usleep@plt>
mov    $0xffffffff,%r14d
cmp    $0xffffffff,%eax
je     1233 <send_i2c_data+0x103>
orb    $0x4,0x1(%rbx)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x2,%edx
call   1030 <write@plt>
cmp    $0x2,%eax
jne    1213 <send_i2c_data+0xe3>
mov    $0x1,%edi
call   1050 <usleep@plt>
cmp    $0xffffffff,%eax
je     1233 <send_i2c_data+0x103>
andb   $0xfb,0x1(%rbx)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x2,%edx
call   1030 <write@plt>
cmp    $0x2,%eax
jne    1213 <send_i2c_data+0xe3>
mov    $0x1,%edi
call   1050 <usleep@plt>
cmp    $0xffffffff,%eax
je     1233 <send_i2c_data+0x103>
orb    $0x4,0x1(%rbx)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x2,%edx
call   1030 <write@plt>
cmp    $0x2,%eax
jne    1213 <send_i2c_data+0xe3>
mov    $0x1,%edi
call   1050 <usleep@plt>
cmp    $0xffffffff,%eax
je     1233 <send_i2c_data+0x103>
andb   $0xfb,0x1(%rbx)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x2,%edx
call   1030 <write@plt>
cmp    $0x2,%eax
jne    1213 <send_i2c_data+0xe3>
mov    $0x64,%edi
call   1050 <usleep@plt>
xor    %r14d,%r14d
cmp    $0xffffffff,%eax
sete   %r14b
neg    %r14d
jmp    1233 <send_i2c_data+0x103>
lea    0xde6(%rip),%rdi
call   1040 <perror@plt>
jmp    1233 <send_i2c_data+0x103>
lea    0xdd8(%rip),%rdi
call   1040 <perror@plt>
mov    $0xffffffff,%r14d
mov    %r14d,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [6 x i8] c"write\00"
define i32 @send_i2c_data(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
%8 = load i8*, i8** %5
%9 = getelementptr inbounds i8, i8* %8, i64 1
store i8* %9, i8** %6
%10 = load i32, i32* %4
%11 = load i8*, i8** %5
%12 = call i32 @write(i32 %10, i8* %11, i32 2)
%13 = icmp ne i32 %12, 2
br i1 %13, label %14, label %16
14: ; preds = %2
%15 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %90
16: ; preds = %2
%17 = call i32 @usleep(i32 1)
store i32 %17, i32* %7
%18 = load i32, i32* %7
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %21
20: ; preds = %16
store i32 -1, i32* %3
br label %90
21: ; preds = %16
%22 = load i8*, i8** %6
%23 = load i8, i8* %22
%24 = zext i8 %23 to i32
%25 = or i32 %24, 4
%26 = trunc i32 %25 to i8
store i8 %26, i8* %22
%27 = load i32, i32* %4
%28 = load i8*, i8** %5
%29 = call i32 @write(i32 %27, i8* %28, i32 2)
%30 = icmp ne i32 %29, 2
br i1 %30, label %31, label %33
31: ; preds = %21
%32 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %90
33: ; preds = %21
%34 = call i32 @usleep(i32 1)
store i32 %34, i32* %7
%35 = load i32, i32* %7
%36 = icmp eq i32 %35, -1
br i1 %36, label %37, label %38
37: ; preds = %33
store i32 -1, i32* %3
br label %90
38: ; preds = %33
%39 = load i8*, i8** %6
%40 = load i8, i8* %39
%41 = zext i8 %40 to i32
%42 = and i32 %41, -5
%43 = trunc i32 %42 to i8
store i8 %43, i8* %39
%44 = load i32, i32* %4
%45 = load i8*, i8** %5
%46 = call i32 @write(i32 %44, i8* %45, i32 2)
%47 = icmp ne i32 %46, 2
br i1 %47, label %48, label %50
48: ; preds = %38
%49 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %90
50: ; preds = %38
%51 = call i32 @usleep(i32 1)
store i32 %51, i32* %7
%52 = load i32, i32* %7
%53 = icmp eq i32 %52, -1
br i1 %53, label %54, label %55
54: ; preds = %50
store i32 -1, i32* %3
br label %90
55: ; preds = %50
%56 = load i8*, i8** %6
%57 = load i8, i8* %56
%58 = zext i8 %57 to i32
%59 = or i32 %58, 4
%60 = trunc i32 %59 to i8
store i8 %60, i8* %56
%61 = load i32, i32* %4
%62 = load i8*, i8** %5
%63 = call i32 @write(i32 %61, i8* %62, i32 2)
%64 = icmp ne i32 %63, 2
br i1 %64, label %65, label %67
65: ; preds = %55
%66 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %90
67: ; preds = %55
%68 = call i32 @usleep(i32 1)
store i32 %68, i32* %7
%69 = load i32, i32* %7
%70 = icmp eq i32 %69, -1
br i1 %70, label %71, label %72
71: ; preds = %67
store i32 -1, i32* %3
br label %90
72: ; preds = %67
%73 = load i8*, i8** %6
%74 = load i8, i8* %73
%75 = zext i8 %74 to i32
%76 = and i32 %75, -5
%77 = trunc i32 %76 to i8
store i8 %77, i8* %73
%78 = load i32, i32* %4
%79 = load i8*, i8** %5
%80 = call i32 @write(i32 %78, i8* %79, i32 2)
%81 = icmp ne i32 %80, 2
br i1 %81, label %82, label %84
82: ; preds = %72
%83 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %90
84: ; preds = %72
%85 = call i32 @usleep(i32 100)
store i32 %85, i32* %7
%86 = load i32, i32* %7
%87 = icmp eq i32 %86, -1
br i1 %87, label %88, label %89
88: ; preds = %84
store i32 -1, i32* %3
br label %90
89: ; preds = %84
store i32 0, i32* %3
br label %90
90: ; preds = %89, %88, %82, %71, %65, %54, %48, %37, %31, %20, %14
%91 = load i32, i32* %3
ret i32 %91
}

LLVM IR (-O2):
@.str = [6 x i8] c"write\00"
define i32 @send_i2c_data(i32 %0, i8* nocapture %1) {
%3 = getelementptr inbounds i8, i8* %1, i64 1
%4 = tail call i32 @write(i32 %0, i8* %1, i32 2)
%5 = icmp eq i32 %4, 2
br i1 %5, label %8, label %6
6: ; preds = %2
%7 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
br label %52
8: ; preds = %2
%9 = tail call i32 @usleep(i32 1)
%10 = icmp eq i32 %9, -1
br i1 %10, label %52, label %11
11: ; preds = %8
%12 = load i8, i8* %3
%13 = or i8 %12, 4
store i8 %13, i8* %3
%14 = tail call i32 @write(i32 %0, i8* %1, i32 2)
%15 = icmp eq i32 %14, 2
br i1 %15, label %18, label %16
16: ; preds = %11
%17 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
br label %52
18: ; preds = %11
%19 = tail call i32 @usleep(i32 1)
%20 = icmp eq i32 %19, -1
br i1 %20, label %52, label %21
21: ; preds = %18
%22 = load i8, i8* %3
%23 = and i8 %22, -5
store i8 %23, i8* %3
%24 = tail call i32 @write(i32 %0, i8* %1, i32 2)
%25 = icmp eq i32 %24, 2
br i1 %25, label %28, label %26
26: ; preds = %21
%27 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
br label %52
28: ; preds = %21
%29 = tail call i32 @usleep(i32 1)
%30 = icmp eq i32 %29, -1
br i1 %30, label %52, label %31
31: ; preds = %28
%32 = load i8, i8* %3
%33 = or i8 %32, 4
store i8 %33, i8* %3
%34 = tail call i32 @write(i32 %0, i8* %1, i32 2)
%35 = icmp eq i32 %34, 2
br i1 %35, label %38, label %36
36: ; preds = %31
%37 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
br label %52
38: ; preds = %31
%39 = tail call i32 @usleep(i32 1)
%40 = icmp eq i32 %39, -1
br i1 %40, label %52, label %41
41: ; preds = %38
%42 = load i8, i8* %3
%43 = and i8 %42, -5
store i8 %43, i8* %3
%44 = tail call i32 @write(i32 %0, i8* %1, i32 2)
%45 = icmp eq i32 %44, 2
br i1 %45, label %48, label %46
46: ; preds = %41
%47 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
br label %52
48: ; preds = %41
%49 = tail call i32 @usleep(i32 100)
%50 = icmp eq i32 %49, -1
%51 = sext i1 %50 to i32
br label %52
52: ; preds = %16, %26, %36, %46, %8, %18, %28, %38, %48, %6
%53 = phi i32 [ -1, %6 ], [ -1, %16 ], [ -1, %26 ], [ -1, %36 ], [ -1, %46 ], [ -1, %8 ], [ -1, %18 ], [ -1, %28 ], [ -1, %38 ], [ %51, %48 ]
ret i32 %53
}