#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*/ O_RDWR;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, float, float, float);
int read(int, unsigned char *, int);
int /*<<< orphan*/ selectDevice(int, int, char *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ usleep(int);
int write(int, unsigned char *, int);
int /*<<< orphan*/ writeToDevice(int, int, int);
int main(int argc, char **argv) {
  unsigned int range;
  int count, b;
  short x, y, z;
  float xa, ya, za;
  int fd;
  unsigned char buf[16];
  if ((fd = open("/dev/i2c-1", O_RDWR)) < 0) {
    fprintf(stderr, "Failed to open i2c bus\n");
    exit(1);
  }
  selectDevice(fd, 0x53, "ADXL345");
  writeToDevice(fd, 0x2d, 0);
  writeToDevice(fd, 0x2d, 16);
  writeToDevice(fd, 0x2d, 8);
  writeToDevice(fd, 0x2c, 11);
  writeToDevice(fd, 0x31, 11);
  while (1) {
    selectDevice(fd, 0x53, "ADXL345");
    buf[0] = 0x32;
    if ((write(fd, buf, 1)) != 1) {
      fprintf(stderr, "Error writing to i2c slave\n");
    }
    if (read(fd, buf, 6) != 6) {
      fprintf(stderr, "Unable to read from ADXL345\n");
    } else {
      x = buf[1] << 8 | buf[0];
      y = buf[3] << 8 | buf[2];
      z = buf[5] << 8 | buf[4];
      xa = (90.0 / 256.0) * (float)x;
      ya = (90.0 / 256.0) * (float)y;
      za = (90.0 / 256.0) * (float)z;
      printf("%4.0f %4.0f %4.0f\n", xa, ya, za);
    }
    usleep(20000);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e3b(%rip),%rax
mov    (%rax),%esi
lea    0xe52(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x34(%rbp)
cmp    $0x0,%eax
jge    11e6 <main+0x56>
mov    0x2e0a(%rip),%rax
mov    (%rax),%edi
lea    0xe3c(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x34(%rbp),%edi
mov    $0x53,%esi
lea    0xe36(%rip),%rdx
call   10a0 <selectDevice@plt>
mov    -0x34(%rbp),%edi
mov    $0x2d,%esi
xor    %edx,%edx
call   1050 <writeToDevice@plt>
mov    -0x34(%rbp),%edi
mov    $0x2d,%esi
mov    $0x10,%edx
call   1050 <writeToDevice@plt>
mov    -0x34(%rbp),%edi
mov    $0x2d,%esi
mov    $0x8,%edx
call   1050 <writeToDevice@plt>
mov    -0x34(%rbp),%edi
mov    $0x2c,%esi
mov    $0xb,%edx
call   1050 <writeToDevice@plt>
mov    -0x34(%rbp),%edi
mov    $0x31,%esi
mov    $0xb,%edx
call   1050 <writeToDevice@plt>
mov    -0x34(%rbp),%edi
mov    $0x53,%esi
lea    0xdcb(%rip),%rdx
call   10a0 <selectDevice@plt>
movb   $0x32,-0x50(%rbp)
mov    -0x34(%rbp),%edi
lea    -0x50(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
cmp    $0x1,%eax
je     1298 <main+0x108>
mov    0x2d4e(%rip),%rax
mov    (%rax),%edi
lea    0xda0(%rip),%rsi
call   1070 <fprintf@plt>
mov    -0x34(%rbp),%edi
lea    -0x50(%rbp),%rsi
mov    $0x6,%edx
call   1060 <read@plt>
cmp    $0x6,%eax
je     12cc <main+0x13c>
mov    0x2d1f(%rip),%rax
mov    (%rax),%edi
lea    0xd8d(%rip),%rsi
call   1070 <fprintf@plt>
jmp    1375 <main+0x1e5>
movzbl -0x4f(%rbp),%eax
shl    $0x8,%eax
movzbl -0x50(%rbp),%ecx
or     %ecx,%eax
mov    %ax,-0x1e(%rbp)
movzbl -0x4d(%rbp),%eax
shl    $0x8,%eax
movzbl -0x4e(%rbp),%ecx
or     %ecx,%eax
mov    %ax,-0x20(%rbp)
movzbl -0x4b(%rbp),%eax
shl    $0x8,%eax
movzbl -0x4c(%rbp),%ecx
or     %ecx,%eax
mov    %ax,-0x22(%rbp)
movswl -0x1e(%rbp),%eax
cvtsi2ss %eax,%xmm0
cvtss2sd %xmm0,%xmm0
movsd  0xced(%rip),%xmm1
mulsd  %xmm1,%xmm0
cvtsd2ss %xmm0,%xmm0
movss  %xmm0,-0x28(%rbp)
movswl -0x20(%rbp),%eax
cvtsi2ss %eax,%xmm0
cvtss2sd %xmm0,%xmm0
mulsd  %xmm1,%xmm0
cvtsd2ss %xmm0,%xmm0
movss  %xmm0,-0x2c(%rbp)
movswl -0x22(%rbp),%eax
cvtsi2ss %eax,%xmm0
cvtss2sd %xmm0,%xmm1
movsd  0xcb3(%rip),%xmm0
mulsd  %xmm1,%xmm0
cvtsd2ss %xmm0,%xmm0
movss  %xmm0,-0x30(%rbp)
movss  -0x28(%rbp),%xmm0
movss  -0x2c(%rbp),%xmm1
movss  -0x30(%rbp),%xmm2
lea    0xcfc(%rip),%rdi
call   1040 <printf@plt>
mov    $0x4e20,%edi
call   10b0 <usleep@plt>
jmp    1251 <main+0xc1>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    0x2e43(%rip),%rax
mov    (%rax),%esi
lea    0xe5a(%rip),%rdi
call   1080 <open@plt>
test   %eax,%eax
js     12f2 <main+0x162>
mov    %eax,%ebx
lea    0xe67(%rip),%r14
mov    %eax,%edi
mov    $0x53,%esi
mov    %r14,%rdx
call   10a0 <selectDevice@plt>
mov    %ebx,%edi
mov    $0x2d,%esi
xor    %edx,%edx
call   1050 <writeToDevice@plt>
mov    %ebx,%edi
mov    $0x2d,%esi
mov    $0x10,%edx
call   1050 <writeToDevice@plt>
mov    %ebx,%edi
mov    $0x2d,%esi
mov    $0x8,%edx
call   1050 <writeToDevice@plt>
mov    %ebx,%edi
mov    $0x2c,%esi
mov    $0xb,%edx
call   1050 <writeToDevice@plt>
mov    %ebx,%edi
mov    $0x31,%esi
mov    $0xb,%edx
call   1050 <writeToDevice@plt>
mov    %rsp,%r15
mov    0x2da9(%rip),%r12
lea    0xe19(%rip),%r13
lea    0xdf6(%rip),%rbp
jmp    1299 <main+0x109>
nop
movswl (%rsp),%eax
xorps  %xmm0,%xmm0
cvtsi2sd %eax,%xmm0
movswl 0x2(%rsp),%eax
movsd  0xda8(%rip),%xmm3
mulsd  %xmm3,%xmm0
cvtsd2ss %xmm0,%xmm0
movswl 0x4(%rsp),%ecx
xorps  %xmm1,%xmm1
cvtsi2sd %eax,%xmm1
mulsd  %xmm3,%xmm1
cvtsd2ss %xmm1,%xmm1
xorps  %xmm2,%xmm2
cvtsi2sd %ecx,%xmm2
mulsd  %xmm3,%xmm2
cvtsd2ss %xmm2,%xmm2
lea    0xde2(%rip),%rdi
call   1040 <printf@plt>
mov    $0x4e20,%edi
call   10b0 <usleep@plt>
mov    %ebx,%edi
mov    $0x53,%esi
mov    %r14,%rdx
call   10a0 <selectDevice@plt>
movb   $0x32,(%rsp)
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0x1,%edx
call   1030 <write@plt>
cmp    $0x1,%eax
je     12cc <main+0x13c>
mov    (%r12),%edi
mov    %rbp,%rsi
call   1070 <fprintf@plt>
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0x6,%edx
call   1060 <read@plt>
cmp    $0x6,%eax
je     1240 <main+0xb0>
mov    (%r12),%edi
mov    %r13,%rsi
call   1070 <fprintf@plt>
jmp    128f <main+0xff>
mov    0x2cdf(%rip),%rax
mov    (%rax),%edi
lea    0xd11(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [11 x i8] c"/dev/i2c-1\00"
@O_RDWR = global i32 0
@stderr = global i32 0
@.str.1 = [24 x i8] c"Failed to open i2c bus\0A\00"
@.str.2 = [8 x i8] c"ADXL345\00"
@.str.3 = [28 x i8] c"Error writing to i2c slave\0A\00"
@.str.4 = [29 x i8] c"Unable to read from ADXL345\0A\00"
@.str.5 = [19 x i8] c"%4.0f %4.0f %4.0f\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 i16
%10 = alloca i16
%11 = alloca i16
%12 = alloca float
%13 = alloca float
%14 = alloca float
%15 = alloca i32
%16 = alloca [16 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%17 = load i32, i32* @O_RDWR
%18 = call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %17)
store i32 %18, i32* %15
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %24
20: ; preds = %2
%21 = load i32, i32* @stderr
%22 = call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
%23 = call i32 @exit(i32 1)
unreachable
24: ; preds = %2
%25 = load i32, i32* %15
%26 = call i32 @selectDevice(i32 %25, i32 83, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
%27 = load i32, i32* %15
%28 = call i32 @writeToDevice(i32 %27, i32 45, i32 0)
%29 = load i32, i32* %15
%30 = call i32 @writeToDevice(i32 %29, i32 45, i32 16)
%31 = load i32, i32* %15
%32 = call i32 @writeToDevice(i32 %31, i32 45, i32 8)
%33 = load i32, i32* %15
%34 = call i32 @writeToDevice(i32 %33, i32 44, i32 11)
%35 = load i32, i32* %15
%36 = call i32 @writeToDevice(i32 %35, i32 49, i32 11)
br label %37
37: ; preds = %24, %103
%38 = load i32, i32* %15
%39 = call i32 @selectDevice(i32 %38, i32 83, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
%40 = getelementptr inbounds [16 x i8], [16 x i8]* %16, i64 0, i64 0
store i8 50, i8* %40
%41 = load i32, i32* %15
%42 = getelementptr inbounds [16 x i8], [16 x i8]* %16, i64 0, i64 0
%43 = call i32 @write(i32 %41, i8* %42, i32 1)
%44 = icmp ne i32 %43, 1
br i1 %44, label %45, label %48
45: ; preds = %37
%46 = load i32, i32* @stderr
%47 = call i32 @fprintf(i32 %46, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0))
br label %48
48: ; preds = %45, %37
%49 = load i32, i32* %15
%50 = getelementptr inbounds [16 x i8], [16 x i8]* %16, i64 0, i64 0
%51 = call i32 @read(i32 %49, i8* %50, i32 6)
%52 = icmp ne i32 %51, 6
br i1 %52, label %53, label %56
53: ; preds = %48
%54 = load i32, i32* @stderr
%55 = call i32 @fprintf(i32 %54, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0))
br label %103
56: ; preds = %48
%57 = getelementptr inbounds [16 x i8], [16 x i8]* %16, i64 0, i64 1
%58 = load i8, i8* %57
%59 = zext i8 %58 to i32
%60 = shl i32 %59, 8
%61 = getelementptr inbounds [16 x i8], [16 x i8]* %16, i64 0, i64 0
%62 = load i8, i8* %61
%63 = zext i8 %62 to i32
%64 = or i32 %60, %63
%65 = trunc i32 %64 to i16
store i16 %65, i16* %9
%66 = getelementptr inbounds [16 x i8], [16 x i8]* %16, i64 0, i64 3
%67 = load i8, i8* %66
%68 = zext i8 %67 to i32
%69 = shl i32 %68, 8
%70 = getelementptr inbounds [16 x i8], [16 x i8]* %16, i64 0, i64 2
%71 = load i8, i8* %70
%72 = zext i8 %71 to i32
%73 = or i32 %69, %72
%74 = trunc i32 %73 to i16
store i16 %74, i16* %10
%75 = getelementptr inbounds [16 x i8], [16 x i8]* %16, i64 0, i64 5
%76 = load i8, i8* %75
%77 = zext i8 %76 to i32
%78 = shl i32 %77, 8
%79 = getelementptr inbounds [16 x i8], [16 x i8]* %16, i64 0, i64 4
%80 = load i8, i8* %79
%81 = zext i8 %80 to i32
%82 = or i32 %78, %81
%83 = trunc i32 %82 to i16
store i16 %83, i16* %11
%84 = load i16, i16* %9
%85 = sitofp i16 %84 to float
%86 = fpext float %85 to double
%87 = fmul double 0x3FD6800000000000, %86
%88 = fptrunc double %87 to float
store float %88, float* %12
%89 = load i16, i16* %10
%90 = sitofp i16 %89 to float
%91 = fpext float %90 to double
%92 = fmul double 0x3FD6800000000000, %91
%93 = fptrunc double %92 to float
store float %93, float* %13
%94 = load i16, i16* %11
%95 = sitofp i16 %94 to float
%96 = fpext float %95 to double
%97 = fmul double 0x3FD6800000000000, %96
%98 = fptrunc double %97 to float
store float %98, float* %14
%99 = load float, float* %12
%100 = load float, float* %13
%101 = load float, float* %14
%102 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.5, i64 0, i64 0), float %99, float %100, float %101)
br label %103
103: ; preds = %56, %53
%104 = call i32 @usleep(i32 20000)
br label %37
}

LLVM IR (-O2):
@.str = [11 x i8] c"/dev/i2c-1\00"
@O_RDWR = global i32 0
@stderr = global i32 0
@.str.1 = [24 x i8] c"Failed to open i2c bus\0A\00"
@.str.2 = [8 x i8] c"ADXL345\00"
@.str.3 = [28 x i8] c"Error writing to i2c slave\0A\00"
@.str.4 = [29 x i8] c"Unable to read from ADXL345\0A\00"
@.str.5 = [19 x i8] c"%4.0f %4.0f %4.0f\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [16 x i8]
%4 = getelementptr inbounds [16 x i8], [16 x i8]* %3, i64 0, i64 0
%5 = load i32, i32* @O_RDWR
%6 = tail call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %5)
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %12
8: ; preds = %2
%9 = load i32, i32* @stderr
%10 = tail call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
%11 = tail call i32 @exit(i32 1)
unreachable
12: ; preds = %2
%13 = tail call i32 @selectDevice(i32 %6, i32 83, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
%14 = tail call i32 @writeToDevice(i32 %6, i32 45, i32 0)
%15 = tail call i32 @writeToDevice(i32 %6, i32 45, i32 16)
%16 = tail call i32 @writeToDevice(i32 %6, i32 45, i32 8)
%17 = tail call i32 @writeToDevice(i32 %6, i32 44, i32 11)
%18 = tail call i32 @writeToDevice(i32 %6, i32 49, i32 11)
%19 = getelementptr inbounds [16 x i8], [16 x i8]* %3, i64 0, i64 1
%20 = getelementptr inbounds [16 x i8], [16 x i8]* %3, i64 0, i64 3
%21 = getelementptr inbounds [16 x i8], [16 x i8]* %3, i64 0, i64 2
%22 = getelementptr inbounds [16 x i8], [16 x i8]* %3, i64 0, i64 5
%23 = getelementptr inbounds [16 x i8], [16 x i8]* %3, i64 0, i64 4
br label %24
24: ; preds = %66, %12
%25 = tail call i32 @selectDevice(i32 %6, i32 83, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
store i8 50, i8* %4
%26 = call i32 @write(i32 %6, i8* %4, i32 1)
%27 = icmp eq i32 %26, 1
br i1 %27, label %31, label %28
28: ; preds = %24
%29 = load i32, i32* @stderr
%30 = tail call i32 @fprintf(i32 %29, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0))
br label %31
31: ; preds = %28, %24
%32 = call i32 @read(i32 %6, i8* %4, i32 6)
%33 = icmp eq i32 %32, 6
br i1 %33, label %37, label %34
34: ; preds = %31
%35 = load i32, i32* @stderr
%36 = tail call i32 @fprintf(i32 %35, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0))
br label %66
37: ; preds = %31
%38 = load i8, i8* %19
%39 = zext i8 %38 to i16
%40 = shl nuw i16 %39, 8
%41 = load i8, i8* %4
%42 = zext i8 %41 to i16
%43 = or i16 %40, %42
%44 = load i8, i8* %20
%45 = zext i8 %44 to i16
%46 = shl nuw i16 %45, 8
%47 = load i8, i8* %21
%48 = zext i8 %47 to i16
%49 = or i16 %46, %48
%50 = load i8, i8* %22
%51 = zext i8 %50 to i16
%52 = shl nuw i16 %51, 8
%53 = load i8, i8* %23
%54 = zext i8 %53 to i16
%55 = or i16 %52, %54
%56 = sitofp i16 %43 to double
%57 = fmul double %56, 0x3FD6800000000000
%58 = fptrunc double %57 to float
%59 = sitofp i16 %49 to double
%60 = fmul double %59, 0x3FD6800000000000
%61 = fptrunc double %60 to float
%62 = sitofp i16 %55 to double
%63 = fmul double %62, 0x3FD6800000000000
%64 = fptrunc double %63 to float
%65 = tail call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.5, i64 0, i64 0), float %58, float %61, float %64)
br label %66
66: ; preds = %37, %34
%67 = tail call i32 @usleep(i32 20000)
br label %24
}