#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 uint8_t;
typedef int uint16_t;

/* Variables and functions */
int /*<<< orphan*/ DelayMicrosecondsNoSleep(int);
int /*<<< orphan*/ I2C_SLAVE;
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit_on_error(char *);
int i2c_rd16(int, int);
int i2c_rd8(int, int);
int /*<<< orphan*/ i2c_wr16(int, int, int);
int /*<<< orphan*/ i2c_wr8(int, int, int);
scalar_t__ ioctl(int, int /*<<< orphan*/, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, int);
int main(int argc, char *argv[]) {
  int i2c_fd = open("/dev/i2c-1", O_RDWR);
  ;
  if (!i2c_fd)
    exit_on_error("Can't open I2C device");
  if (ioctl(i2c_fd, I2C_SLAVE, 0x0F) < 0) {
    exit_on_error("Failed to set I2C address");
    return -1;
  }
  DelayMicrosecondsNoSleep(650000 - 25000);
  uint8_t loop_counter = 0;
  bool completed_audio_0 = false;
  bool completed_audio_1 = false;
loop:
  loop_counter++;
  if (loop_counter > 8)
    exit_on_error("Looped too many times");
  if (!completed_audio_0) {
    uint16_t data_0 = i2c_rd16(i2c_fd, 0x0004);
    if (data_0 != 0x0F37) {
      DelayMicrosecondsNoSleep(25000);
      i2c_wr16(i2c_fd, 0x0004, 0x0F37);
      if (i2c_rd16(i2c_fd, 0x0004) == 0x0F37) {
        completed_audio_0 = true;
        printf("Audio Data 0 at %04x Re-Written\n", 0x0004);
      } else
        goto loop;
    } else {
      completed_audio_0 = true;
    }
  }
  if (!completed_audio_1) {
    uint8_t data_1 = i2c_rd8(i2c_fd, 0x8651);
    if (data_1 != 0x04) {
      DelayMicrosecondsNoSleep(25000);
      i2c_wr8(i2c_fd, 0x8651, 0x04);
      if (i2c_rd8(i2c_fd, 0x8651) == 0x04) {
        completed_audio_1 = true;
        printf("Audio Data 1 at %04x Re-Written\n", 0x8651);
      } else
        goto loop;
    } else {
      completed_audio_1 = true;
    }
  }
  close(i2c_fd);
  return (0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e2b(%rip),%rax
mov    (%rax),%esi
lea    0xe3a(%rip),%rdi
call   10a0 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jne    11e4 <main+0x44>
lea    0xe2c(%rip),%rdi
call   1060 <exit_on_error@plt>
mov    -0x14(%rbp),%edi
mov    0x2df2(%rip),%rax
mov    (%rax),%esi
mov    $0xf,%edx
call   1050 <ioctl@plt>
cmp    $0x0,%rax
jge    121c <main+0x7c>
lea    0xe16(%rip),%rdi
call   1060 <exit_on_error@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1380 <main+0x1e0>
mov    $0x98968,%edi
call   1090 <DelayMicrosecondsNoSleep@plt>
movl   $0x0,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
cmpl   $0x8,-0x18(%rbp)
jle    125a <main+0xba>
lea    0xde6(%rip),%rdi
call   1060 <exit_on_error@plt>
cmpl   $0x0,-0x1c(%rbp)
jne    12e8 <main+0x148>
mov    -0x14(%rbp),%edi
mov    $0x4,%esi
call   10c0 <i2c_rd16@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xf37,-0x24(%rbp)
je     12dc <main+0x13c>
mov    $0x61a8,%edi
call   1090 <DelayMicrosecondsNoSleep@plt>
mov    -0x14(%rbp),%edi
mov    $0x4,%esi
mov    $0xf37,%edx
call   10b0 <i2c_wr16@plt>
mov    -0x14(%rbp),%edi
mov    $0x4,%esi
call   10c0 <i2c_rd16@plt>
cmp    $0xf37,%eax
jne    12d2 <main+0x132>
movl   $0x1,-0x1c(%rbp)
lea    0xd8e(%rip),%rdi
mov    $0x4,%esi
call   1040 <printf@plt>
jmp    12d7 <main+0x137>
jmp    123b <main+0x9b>
jmp    12e3 <main+0x143>
movl   $0x1,-0x1c(%rbp)
jmp    12e8 <main+0x148>
cmpl   $0x0,-0x20(%rbp)
jne    1371 <main+0x1d1>
mov    -0x14(%rbp),%edi
mov    $0x8651,%esi
call   1080 <i2c_rd8@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x4,-0x28(%rbp)
je     1365 <main+0x1c5>
mov    $0x61a8,%edi
call   1090 <DelayMicrosecondsNoSleep@plt>
mov    -0x14(%rbp),%edi
mov    $0x8651,%esi
mov    $0x4,%edx
call   1030 <i2c_wr8@plt>
mov    -0x14(%rbp),%edi
mov    $0x8651,%esi
call   1080 <i2c_rd8@plt>
cmp    $0x4,%eax
jne    135b <main+0x1bb>
movl   $0x1,-0x20(%rbp)
lea    0xd26(%rip),%rdi
mov    $0x8651,%esi
call   1040 <printf@plt>
jmp    1360 <main+0x1c0>
jmp    123b <main+0x9b>
jmp    136c <main+0x1cc>
movl   $0x1,-0x20(%rbp)
jmp    1371 <main+0x1d1>
mov    -0x14(%rbp),%edi
call   1070 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    0x2e39(%rip),%rax
mov    (%rax),%esi
lea    0xe48(%rip),%rdi
call   10a0 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
jne    11cf <main+0x2f>
lea    0xe41(%rip),%rdi
call   1060 <exit_on_error@plt>
mov    0x2e0a(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    $0xf,%edx
call   1050 <ioctl@plt>
test   %rax,%rax
js     12e4 <main+0x144>
mov    $0x98968,%edi
call   1090 <DelayMicrosecondsNoSleep@plt>
mov    $0x1,%bpl
xor    %r12d,%r12d
lea    0xe37(%rip),%r14
lea    0xe46(%rip),%r15
cmp    $0x8,%r12d
jb     123a <main+0x9a>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %r14,%rdi
call   1060 <exit_on_error@plt>
jmp    123a <main+0x9a>
nopw   0x0(%rax,%rax,1)
add    $0x1,%r12d
cmp    $0x8,%r12d
jae    1220 <main+0x80>
test   $0x1,%bpl
je     12a0 <main+0x100>
mov    %ebx,%edi
mov    $0x4,%esi
call   10c0 <i2c_rd16@plt>
cmp    $0xf37,%eax
je     12a0 <main+0x100>
mov    $0x61a8,%edi
call   1090 <DelayMicrosecondsNoSleep@plt>
mov    %ebx,%edi
mov    $0x4,%esi
mov    $0xf37,%edx
call   10b0 <i2c_wr16@plt>
mov    %ebx,%edi
mov    $0x4,%esi
call   10c0 <i2c_rd16@plt>
mov    $0x1,%bpl
cmp    $0xf37,%eax
jne    1230 <main+0x90>
mov    %r15,%rdi
mov    $0x4,%esi
call   1040 <printf@plt>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %ebx,%edi
mov    $0x8651,%esi
call   1080 <i2c_rd8@plt>
cmp    $0x4,%eax
je     1308 <main+0x168>
mov    $0x61a8,%edi
call   1090 <DelayMicrosecondsNoSleep@plt>
mov    %ebx,%edi
mov    $0x8651,%esi
mov    $0x4,%edx
call   1030 <i2c_wr8@plt>
mov    %ebx,%edi
mov    $0x8651,%esi
call   1080 <i2c_rd8@plt>
cmp    $0x4,%eax
je     12f7 <main+0x157>
xor    %ebp,%ebp
jmp    1230 <main+0x90>
lea    0xd36(%rip),%rdi
call   1060 <exit_on_error@plt>
mov    $0xffffffff,%eax
jmp    1311 <main+0x171>
lea    0xd74(%rip),%rdi
mov    $0x8651,%esi
call   1040 <printf@plt>
mov    %ebx,%edi
call   1070 <close@plt>
xor    %eax,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [11 x i8] c"/dev/i2c-1\00"
@O_RDWR = global i32 0
@.str.1 = [22 x i8] c"Can't open I2C device\00"
@I2C_SLAVE = global i32 0
@.str.2 = [26 x i8] c"Failed to set I2C address\00"
@.str.3 = [22 x i8] c"Looped too many times\00"
@.str.4 = [33 x i8] c"Audio Data 0 at %04x Re-Written\0A\00"
@.str.5 = [33 x i8] c"Audio Data 1 at %04x Re-Written\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = load i32, i32* @O_RDWR
%13 = call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %12)
store i32 %13, i32* %6
%14 = load i32, i32* %6
%15 = icmp ne i32 %14, 0
br i1 %15, label %18, label %16
16: ; preds = %2
%17 = call i32 @exit_on_error(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
br label %18
18: ; preds = %16, %2
%19 = load i32, i32* %6
%20 = load i32, i32* @I2C_SLAVE
%21 = call i64 @ioctl(i32 %19, i32 %20, i32 15)
%22 = icmp slt i64 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %18
%24 = call i32 @exit_on_error(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %3
br label %79
25: ; preds = %18
%26 = call i32 @DelayMicrosecondsNoSleep(i32 625000)
store i32 0, i32* %7
store i32 0, i32* %8
store i32 0, i32* %9
br label %27
27: ; preds = %72, %51, %25
%28 = load i32, i32* %7
%29 = add nsw i32 %28, 1
store i32 %29, i32* %7
%30 = load i32, i32* %7
%31 = icmp sgt i32 %30, 8
br i1 %31, label %32, label %34
32: ; preds = %27
%33 = call i32 @exit_on_error(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0))
br label %34
34: ; preds = %32, %27
%35 = load i32, i32* %8
%36 = icmp ne i32 %35, 0
br i1 %36, label %55, label %37
37: ; preds = %34
%38 = load i32, i32* %6
%39 = call i32 @i2c_rd16(i32 %38, i32 4)
store i32 %39, i32* %10
%40 = load i32, i32* %10
%41 = icmp ne i32 %40, 3895
br i1 %41, label %42, label %53
42: ; preds = %37
%43 = call i32 @DelayMicrosecondsNoSleep(i32 25000)
%44 = load i32, i32* %6
%45 = call i32 @i2c_wr16(i32 %44, i32 4, i32 3895)
%46 = load i32, i32* %6
%47 = call i32 @i2c_rd16(i32 %46, i32 4)
%48 = icmp eq i32 %47, 3895
br i1 %48, label %49, label %51
49: ; preds = %42
store i32 1, i32* %8
%50 = call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.4, i64 0, i64 0), i32 4)
br label %52
51: ; preds = %42
br label %27
52: ; preds = %49
br label %54
53: ; preds = %37
store i32 1, i32* %8
br label %54
54: ; preds = %53, %52
br label %55
55: ; preds = %54, %34
%56 = load i32, i32* %9
%57 = icmp ne i32 %56, 0
br i1 %57, label %76, label %58
58: ; preds = %55
%59 = load i32, i32* %6
%60 = call i32 @i2c_rd8(i32 %59, i32 34385)
store i32 %60, i32* %11
%61 = load i32, i32* %11
%62 = icmp ne i32 %61, 4
br i1 %62, label %63, label %74
63: ; preds = %58
%64 = call i32 @DelayMicrosecondsNoSleep(i32 25000)
%65 = load i32, i32* %6
%66 = call i32 @i2c_wr8(i32 %65, i32 34385, i32 4)
%67 = load i32, i32* %6
%68 = call i32 @i2c_rd8(i32 %67, i32 34385)
%69 = icmp eq i32 %68, 4
br i1 %69, label %70, label %72
70: ; preds = %63
store i32 1, i32* %9
%71 = call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.5, i64 0, i64 0), i32 34385)
br label %73
72: ; preds = %63
br label %27
73: ; preds = %70
br label %75
74: ; preds = %58
store i32 1, i32* %9
br label %75
75: ; preds = %74, %73
br label %76
76: ; preds = %75, %55
%77 = load i32, i32* %6
%78 = call i32 @close(i32 %77)
store i32 0, i32* %3
br label %79
79: ; preds = %76, %23
%80 = load i32, i32* %3
ret i32 %80
}

LLVM IR (-O2):
@.str = [11 x i8] c"/dev/i2c-1\00"
@O_RDWR = global i32 0
@.str.1 = [22 x i8] c"Can't open I2C device\00"
@I2C_SLAVE = global i32 0
@.str.2 = [26 x i8] c"Failed to set I2C address\00"
@.str.3 = [22 x i8] c"Looped too many times\00"
@.str.4 = [33 x i8] c"Audio Data 0 at %04x Re-Written\0A\00"
@.str.5 = [33 x i8] c"Audio Data 1 at %04x Re-Written\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = load i32, i32* @O_RDWR
%4 = tail call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = icmp eq i32 %4, 0
br i1 %5, label %6, label %8
6: ; preds = %2
%7 = tail call i32 @exit_on_error(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
br label %8
8: ; preds = %6, %2
%9 = load i32, i32* @I2C_SLAVE
%10 = tail call i64 @ioctl(i32 %4, i32 %9, i32 15)
%11 = icmp slt i64 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %8
%13 = tail call i32 @exit_on_error(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
br label %48
14: ; preds = %8
%15 = tail call i32 @DelayMicrosecondsNoSleep(i32 625000)
br label %16
16: ; preds = %32, %14
%17 = phi i32 [ 0, %14 ], [ %19, %32 ]
%18 = phi i1 [ true, %14 ], [ %33, %32 ]
%19 = add nuw nsw i32 %17, 1
%20 = icmp ugt i32 %17, 7
br i1 %20, label %21, label %23
21: ; preds = %16
%22 = tail call i32 @exit_on_error(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0))
br label %23
23: ; preds = %21, %16
br i1 %18, label %24, label %36
24: ; preds = %23
%25 = tail call i32 @i2c_rd16(i32 %4, i32 4)
%26 = icmp eq i32 %25, 3895
br i1 %26, label %36, label %27
27: ; preds = %24
%28 = tail call i32 @DelayMicrosecondsNoSleep(i32 25000)
%29 = tail call i32 @i2c_wr16(i32 %4, i32 4, i32 3895)
%30 = tail call i32 @i2c_rd16(i32 %4, i32 4)
%31 = icmp eq i32 %30, 3895
br i1 %31, label %34, label %32
32: ; preds = %27, %39
%33 = phi i1 [ true, %27 ], [ false, %39 ]
br label %16
34: ; preds = %27
%35 = tail call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.4, i64 0, i64 0), i32 4)
br label %36
36: ; preds = %23, %24, %34
%37 = tail call i32 @i2c_rd8(i32 %4, i32 34385)
%38 = icmp eq i32 %37, 4
br i1 %38, label %46, label %39
39: ; preds = %36
%40 = tail call i32 @DelayMicrosecondsNoSleep(i32 25000)
%41 = tail call i32 @i2c_wr8(i32 %4, i32 34385, i32 4)
%42 = tail call i32 @i2c_rd8(i32 %4, i32 34385)
%43 = icmp eq i32 %42, 4
br i1 %43, label %44, label %32
44: ; preds = %39
%45 = tail call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.5, i64 0, i64 0), i32 34385)
br label %46
46: ; preds = %36, %44
%47 = tail call i32 @close(i32 %4)
br label %48
48: ; preds = %46, %12
%49 = phi i32 [ -1, %12 ], [ 0, %46 ]
ret i32 %49
}