#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 /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ close(int);
scalar_t__ ioctl(int, int /*<<< orphan*/, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int write(int, char *, int);
int main(void) {
  int file;
  char device[] = "/dev/i2c-0";
  int addr = 0xC0;
  char buf[4];
  char result[4] = {0};
  file = open(device, O_RDWR);
  if (file < 0) {
    perror("Failed to open i2c device");
    return 0;
  }
  if (ioctl(file, I2C_SLAVE, addr >> 1) < 0) {
    perror("Error setting address");
    return 0;
  }
  buf[0] = 0;
  if (write(file, buf, 1) != 1) {
    printf("failed\n");
    return 0;
  }
  if (read(file, buf, 1) != 1) {
    printf("failed\n");
    return 0;
  }
  printf("Compass software revision %d\n", (int)buf[0]);
  buf[0] = 1;
  if (write(file, buf, 1) != 1) {
    printf("failed\n");
    return 0;
  }
  if (read(file, buf, 1) != 1) {
    printf("failed\n");
    return 0;
  }
  printf("Compass bearing as byte [0-255] %d\n", (int)buf[0]);
  buf[0] = 2;
  if (write(file, buf, 1) != 1) {
    printf("failed\n");
    return 0;
  }
  if (read(file, buf, 2) != 2) {
    printf("failed\n");
    return 0;
  }
  result[1] = buf[0];
  result[0] = buf[1];
  close(file);
  printf("compass bearing as short [0-3599] %d\n", *((int *)result));
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    0xe6a(%rip),%rax
mov    %rax,-0x13(%rbp)
mov    0xe67(%rip),%ax
mov    %ax,-0xb(%rbp)
mov    0xe5f(%rip),%al
mov    %al,-0x9(%rbp)
movl   $0xc0,-0x18(%rbp)
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1050 <memset@plt>
lea    -0x13(%rbp),%rdi
mov    0x2e18(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    11fc <main+0x7c>
lea    0xe20(%rip),%rdi
call   10a0 <perror@plt>
movl   $0x0,-0x4(%rbp)
jmp    13c9 <main+0x249>
mov    -0x8(%rbp),%edi
mov    0x2dda(%rip),%rax
mov    (%rax),%esi
mov    -0x18(%rbp),%edx
sar    $0x1,%edx
call   1060 <ioctl@plt>
cmp    $0x0,%rax
jge    1235 <main+0xb5>
lea    0xe01(%rip),%rdi
call   10a0 <perror@plt>
movl   $0x0,-0x4(%rbp)
jmp    13c9 <main+0x249>
movb   $0x0,-0x1c(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x1c(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
cmp    $0x1,%eax
je     126d <main+0xed>
lea    0xde1(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    13c9 <main+0x249>
mov    -0x8(%rbp),%edi
lea    -0x1c(%rbp),%rsi
mov    $0x1,%edx
call   1080 <read@plt>
cmp    $0x1,%eax
je     12a1 <main+0x121>
lea    0xdad(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    13c9 <main+0x249>
movsbl -0x1c(%rbp),%esi
lea    0xd97(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movb   $0x1,-0x1c(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x1c(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
cmp    $0x1,%eax
je     12eb <main+0x16b>
lea    0xd63(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    13c9 <main+0x249>
mov    -0x8(%rbp),%edi
lea    -0x1c(%rbp),%rsi
mov    $0x1,%edx
call   1080 <read@plt>
cmp    $0x1,%eax
je     131f <main+0x19f>
lea    0xd2f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    13c9 <main+0x249>
movsbl -0x1c(%rbp),%esi
lea    0xd37(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movb   $0x2,-0x1c(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x1c(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
cmp    $0x1,%eax
je     1369 <main+0x1e9>
lea    0xce5(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    13c9 <main+0x249>
mov    -0x8(%rbp),%edi
lea    -0x1c(%rbp),%rsi
mov    $0x2,%edx
call   1080 <read@plt>
cmp    $0x2,%eax
je     139d <main+0x21d>
lea    0xcb1(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    13c9 <main+0x249>
mov    -0x1c(%rbp),%al
mov    %al,-0x1f(%rbp)
mov    -0x1b(%rbp),%al
mov    %al,-0x20(%rbp)
mov    -0x8(%rbp),%edi
call   1070 <close@plt>
mov    -0x20(%rbp),%esi
lea    0xcca(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
movabs $0x6332692f7665642f,%rax
69 32 63
mov    %rax,0x8(%rsp)
movl   $0x302d63,0xf(%rsp)
mov    0x2e44(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdi
call   1090 <open@plt>
test   %eax,%eax
js     12d1 <main+0x151>
mov    %eax,%ebx
mov    0x2e1f(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    $0x60,%edx
call   1060 <ioctl@plt>
test   %rax,%rax
js     12df <main+0x15f>
movb   $0x0,0x4(%rsp)
lea    0x4(%rsp),%rsi
mov    %ebx,%edi
mov    $0x1,%edx
call   1040 <write@plt>
cmp    $0x1,%eax
jne    12bc <main+0x13c>
lea    0x4(%rsp),%rsi
mov    %ebx,%edi
mov    $0x1,%edx
call   1080 <read@plt>
cmp    $0x1,%eax
jne    12bc <main+0x13c>
movsbl 0x4(%rsp),%esi
lea    0xe1e(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
movb   $0x1,0x4(%rsp)
lea    0x4(%rsp),%rsi
mov    %ebx,%edi
mov    $0x1,%edx
call   1040 <write@plt>
cmp    $0x1,%eax
jne    12bc <main+0x13c>
lea    0x4(%rsp),%rsi
mov    %ebx,%edi
mov    $0x1,%edx
call   1080 <read@plt>
cmp    $0x1,%eax
jne    12bc <main+0x13c>
movsbl 0x4(%rsp),%esi
lea    0xdf8(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
movb   $0x2,0x4(%rsp)
lea    0x4(%rsp),%rsi
mov    %ebx,%edi
mov    $0x1,%edx
call   1040 <write@plt>
cmp    $0x1,%eax
jne    12bc <main+0x13c>
lea    0x4(%rsp),%rsi
mov    %ebx,%edi
mov    $0x2,%edx
call   1080 <read@plt>
cmp    $0x2,%eax
jne    12bc <main+0x13c>
movzwl 0x4(%rsp),%ebp
mov    %ebx,%edi
call   1070 <close@plt>
shl    $0x10,%ebp
bswap  %ebp
lea    0xdcc(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    12c8 <main+0x148>
lea    0xde0(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xd33(%rip),%rdi
call   10a0 <perror@plt>
jmp    12c8 <main+0x148>
lea    0xd3f(%rip),%rdi
call   10a0 <perror@plt>
jmp    12c8 <main+0x148>

LLVM IR (-O0):
@__const.main.device = [11 x i8] c"/dev/i2c-0\00"
@O_RDWR = global i32 0
@.str = [26 x i8] c"Failed to open i2c device\00"
@I2C_SLAVE = global i32 0
@.str.1 = [22 x i8] c"Error setting address\00"
@.str.2 = [8 x i8] c"failed\0A\00"
@.str.3 = [30 x i8] c"Compass software revision %d\0A\00"
@.str.4 = [36 x i8] c"Compass bearing as byte [0-255] %d\0A\00"
@.str.5 = [38 x i8] c"compass bearing as short [0-3599] %d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca [11 x i8]
%4 = alloca i32
%5 = alloca [4 x i8]
%6 = alloca [4 x i8]
store i32 0, i32* %1
%7 = bitcast [11 x i8]* %3 to i8*
store i32 192, i32* %4
%8 = bitcast [4 x i8]* %6 to i8*
%9 = getelementptr inbounds [11 x i8], [11 x i8]* %3, i64 0, i64 0
%10 = load i32, i32* @O_RDWR
%11 = call i32 @open(i8* %9, i32 %10)
store i32 %11, i32* %2
%12 = load i32, i32* %2
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %0
%15 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %1
br label %91
16: ; preds = %0
%17 = load i32, i32* %2
%18 = load i32, i32* @I2C_SLAVE
%19 = load i32, i32* %4
%20 = ashr i32 %19, 1
%21 = call i64 @ioctl(i32 %17, i32 %18, i32 %20)
%22 = icmp slt i64 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %16
%24 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %1
br label %91
25: ; preds = %16
%26 = getelementptr inbounds [4 x i8], [4 x i8]* %5, i64 0, i64 0
store i8 0, i8* %26
%27 = load i32, i32* %2
%28 = getelementptr inbounds [4 x i8], [4 x i8]* %5, i64 0, i64 0
%29 = call i32 @write(i32 %27, i8* %28, i32 1)
%30 = icmp ne i32 %29, 1
br i1 %30, label %31, label %33
31: ; preds = %25
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
store i32 0, i32* %1
br label %91
33: ; preds = %25
%34 = load i32, i32* %2
%35 = getelementptr inbounds [4 x i8], [4 x i8]* %5, i64 0, i64 0
%36 = call i32 @read(i32 %34, i8* %35, i32 1)
%37 = icmp ne i32 %36, 1
br i1 %37, label %38, label %40
38: ; preds = %33
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
store i32 0, i32* %1
br label %91
40: ; preds = %33
%41 = getelementptr inbounds [4 x i8], [4 x i8]* %5, i64 0, i64 0
%42 = load i8, i8* %41
%43 = sext i8 %42 to i32
%44 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0), i32 %43)
%45 = getelementptr inbounds [4 x i8], [4 x i8]* %5, i64 0, i64 0
store i8 1, i8* %45
%46 = load i32, i32* %2
%47 = getelementptr inbounds [4 x i8], [4 x i8]* %5, i64 0, i64 0
%48 = call i32 @write(i32 %46, i8* %47, i32 1)
%49 = icmp ne i32 %48, 1
br i1 %49, label %50, label %52
50: ; preds = %40
%51 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
store i32 0, i32* %1
br label %91
52: ; preds = %40
%53 = load i32, i32* %2
%54 = getelementptr inbounds [4 x i8], [4 x i8]* %5, i64 0, i64 0
%55 = call i32 @read(i32 %53, i8* %54, i32 1)
%56 = icmp ne i32 %55, 1
br i1 %56, label %57, label %59
57: ; preds = %52
%58 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
store i32 0, i32* %1
br label %91
59: ; preds = %52
%60 = getelementptr inbounds [4 x i8], [4 x i8]* %5, i64 0, i64 0
%61 = load i8, i8* %60
%62 = sext i8 %61 to i32
%63 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.4, i64 0, i64 0), i32 %62)
%64 = getelementptr inbounds [4 x i8], [4 x i8]* %5, i64 0, i64 0
store i8 2, i8* %64
%65 = load i32, i32* %2
%66 = getelementptr inbounds [4 x i8], [4 x i8]* %5, i64 0, i64 0
%67 = call i32 @write(i32 %65, i8* %66, i32 1)
%68 = icmp ne i32 %67, 1
br i1 %68, label %69, label %71
69: ; preds = %59
%70 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
store i32 0, i32* %1
br label %91
71: ; preds = %59
%72 = load i32, i32* %2
%73 = getelementptr inbounds [4 x i8], [4 x i8]* %5, i64 0, i64 0
%74 = call i32 @read(i32 %72, i8* %73, i32 2)
%75 = icmp ne i32 %74, 2
br i1 %75, label %76, label %78
76: ; preds = %71
%77 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
store i32 0, i32* %1
br label %91
78: ; preds = %71
%79 = getelementptr inbounds [4 x i8], [4 x i8]* %5, i64 0, i64 0
%80 = load i8, i8* %79
%81 = getelementptr inbounds [4 x i8], [4 x i8]* %6, i64 0, i64 1
store i8 %80, i8* %81
%82 = getelementptr inbounds [4 x i8], [4 x i8]* %5, i64 0, i64 1
%83 = load i8, i8* %82
%84 = getelementptr inbounds [4 x i8], [4 x i8]* %6, i64 0, i64 0
store i8 %83, i8* %84
%85 = load i32, i32* %2
%86 = call i32 @close(i32 %85)
%87 = getelementptr inbounds [4 x i8], [4 x i8]* %6, i64 0, i64 0
%88 = bitcast i8* %87 to i32*
%89 = load i32, i32* %88
%90 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.5, i64 0, i64 0), i32 %89)
store i32 0, i32* %1
br label %91
91: ; preds = %78, %76, %69, %57, %50, %38, %31, %23, %14
%92 = load i32, i32* %1
ret i32 %92
}

LLVM IR (-O2):
@__const.main.device = [11 x i8] c"/dev/i2c-0\00"
@O_RDWR = global i32 0
@.str = [26 x i8] c"Failed to open i2c device\00"
@I2C_SLAVE = global i32 0
@.str.1 = [22 x i8] c"Error setting address\00"
@.str.3 = [30 x i8] c"Compass software revision %d\0A\00"
@.str.4 = [36 x i8] c"Compass bearing as byte [0-255] %d\0A\00"
@.str.5 = [38 x i8] c"compass bearing as short [0-3599] %d\0A\00"
@str.10 = [7 x i8] c"failed\00"
define i32 @main() {
%1 = alloca [11 x i8]
%2 = alloca [4 x i8]
%3 = getelementptr inbounds [11 x i8], [11 x i8]* %1, i64 0, i64 0
%4 = getelementptr inbounds [4 x i8], [4 x i8]* %2, i64 0, i64 0
%5 = load i32, i32* @O_RDWR
%6 = call i32 @open(i8* %3, i32 %5)
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %0
%9 = tail call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
br label %62
10: ; preds = %0
%11 = load i32, i32* @I2C_SLAVE
%12 = tail call i64 @ioctl(i32 %6, i32 %11, i32 96)
%13 = icmp slt i64 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %10
%15 = tail call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
br label %62
16: ; preds = %10
store i8 0, i8* %4
%17 = call i32 @write(i32 %6, i8* %4, i32 1)
%18 = icmp eq i32 %17, 1
br i1 %18, label %21, label %19
19: ; preds = %16
%20 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @str.10, i64 0, i64 0))
br label %62
21: ; preds = %16
%22 = call i32 @read(i32 %6, i8* %4, i32 1)
%23 = icmp eq i32 %22, 1
br i1 %23, label %26, label %24
24: ; preds = %21
%25 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @str.10, i64 0, i64 0))
br label %62
26: ; preds = %21
%27 = load i8, i8* %4
%28 = sext i8 %27 to i32
%29 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0), i32 %28)
store i8 1, i8* %4
%30 = call i32 @write(i32 %6, i8* %4, i32 1)
%31 = icmp eq i32 %30, 1
br i1 %31, label %34, label %32
32: ; preds = %26
%33 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @str.10, i64 0, i64 0))
br label %62
34: ; preds = %26
%35 = call i32 @read(i32 %6, i8* %4, i32 1)
%36 = icmp eq i32 %35, 1
br i1 %36, label %39, label %37
37: ; preds = %34
%38 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @str.10, i64 0, i64 0))
br label %62
39: ; preds = %34
%40 = load i8, i8* %4
%41 = sext i8 %40 to i32
%42 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @.str.4, i64 0, i64 0), i32 %41)
store i8 2, i8* %4
%43 = call i32 @write(i32 %6, i8* %4, i32 1)
%44 = icmp eq i32 %43, 1
br i1 %44, label %47, label %45
45: ; preds = %39
%46 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @str.10, i64 0, i64 0))
br label %62
47: ; preds = %39
%48 = call i32 @read(i32 %6, i8* %4, i32 2)
%49 = icmp eq i32 %48, 2
br i1 %49, label %52, label %50
50: ; preds = %47
%51 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @str.10, i64 0, i64 0))
br label %62
52: ; preds = %47
%53 = load i8, i8* %4
%54 = getelementptr inbounds [4 x i8], [4 x i8]* %2, i64 0, i64 1
%55 = load i8, i8* %54
%56 = tail call i32 @close(i32 %6)
%57 = zext i8 %53 to i32
%58 = shl nuw nsw i32 %57, 8
%59 = zext i8 %55 to i32
%60 = or i32 %58, %59
%61 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @.str.5, i64 0, i64 0), i32 %60)
br label %62
62: ; preds = %52, %50, %45, %37, %32, %24, %19, %14, %8
ret i32 0
}