#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;

/* Variables and functions */
int enable_i2c_clocls();
int /*<<< orphan*/ i2c_init(int, int);
int i2c_reg_read(int, int);
int /*<<< orphan*/ i2c_uninit();
int /*<<< orphan*/ printf(char *, ...);
int strtol(char *, int /*<<< orphan*/ *, int);
int /*<<< orphan*/ usleep(int);
int main(int argc, char **argv) {
  size_t i;
  int version;
  int res = enable_i2c_clocls();
  uint8_t hi, lo;
  uint8_t pitch, roll;
  float bearing = 0;
  uint8_t COMPAS = 0xC0;
  if (res) {
    printf("Error enabling I2C clocks: %i\n", res);
    return res;
  }
  if (argc == 2)
    COMPAS = strtol(argv[1], NULL, 16);
  else
    COMPAS = 0xC0;
  printf("Using address 0x%X (0x%X).\nYou can provide address as a parameter "
         "(in hexadecimal without 0x)\n",
         COMPAS, COMPAS >> 1);
  COMPAS >>= 1;
  i2c_init(100000, 1);
  printf("I2C-2 initialized\n");
  usleep(100 * 1000);
  version = i2c_reg_read(COMPAS, 0);
  printf("Compas firmware version: %i\n", version);
  for (i = 0; i < 200; ++i) {
    usleep(100 * 1000);
    hi = i2c_reg_read(COMPAS, 2);
    lo = i2c_reg_read(COMPAS, 3);
    pitch = i2c_reg_read(COMPAS, 4);
    roll = i2c_reg_read(COMPAS, 5);
    bearing = ((float)((hi << 8) + lo)) / 10.0;
    printf("Bearing: %f, pitch %u, roll %u\n", bearing, pitch, roll);
    usleep(200 * 1000);
  }
  i2c_uninit();
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x0,%al
call   1080 <enable_i2c_clocls@plt>
mov    %eax,-0x20(%rbp)
xorps  %xmm0,%xmm0
movss  %xmm0,-0x34(%rbp)
movl   $0xc0,-0x38(%rbp)
cmpl   $0x0,-0x20(%rbp)
je     11c5 <main+0x55>
mov    -0x20(%rbp),%esi
lea    0xe55(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1325 <main+0x1b5>
cmpl   $0x2,-0x8(%rbp)
jne    11ed <main+0x7d>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0x10,%edx
call   1050 <strtol@plt>
mov    %eax,-0x38(%rbp)
jmp    11f4 <main+0x84>
movl   $0xc0,-0x38(%rbp)
mov    -0x38(%rbp),%esi
mov    -0x38(%rbp),%edx
sar    $0x1,%edx
lea    0xe23(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x38(%rbp),%eax
sar    $0x1,%eax
mov    %eax,-0x38(%rbp)
mov    $0x186a0,%edi
mov    $0x1,%esi
call   1030 <i2c_init@plt>
lea    0xe5c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x186a0,%edi
call   1090 <usleep@plt>
mov    -0x38(%rbp),%edi
xor    %esi,%esi
call   1070 <i2c_reg_read@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%esi
lea    0xe47(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movq   $0x0,-0x18(%rbp)
cmpq   $0xc8,-0x18(%rbp)
jae    1317 <main+0x1a7>
mov    $0x186a0,%edi
call   1090 <usleep@plt>
mov    -0x38(%rbp),%edi
mov    $0x2,%esi
call   1070 <i2c_reg_read@plt>
mov    %eax,-0x24(%rbp)
mov    -0x38(%rbp),%edi
mov    $0x3,%esi
call   1070 <i2c_reg_read@plt>
mov    %eax,-0x28(%rbp)
mov    -0x38(%rbp),%edi
mov    $0x4,%esi
call   1070 <i2c_reg_read@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x38(%rbp),%edi
mov    $0x5,%esi
call   1070 <i2c_reg_read@plt>
mov    %eax,-0x30(%rbp)
mov    -0x24(%rbp),%eax
shl    $0x8,%eax
add    -0x28(%rbp),%eax
cvtsi2ss %eax,%xmm0
cvtss2sd %xmm0,%xmm0
movsd  0xd2e(%rip),%xmm1
divsd  %xmm1,%xmm0
cvtsd2ss %xmm0,%xmm0
movss  %xmm0,-0x34(%rbp)
movss  -0x34(%rbp),%xmm0
cvtss2sd %xmm0,%xmm0
mov    -0x2c(%rbp),%esi
mov    -0x30(%rbp),%edx
lea    0xdc1(%rip),%rdi
mov    $0x1,%al
call   1040 <printf@plt>
mov    $0x30d40,%edi
call   1090 <usleep@plt>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    1261 <main+0xf1>
mov    $0x0,%al
call   1060 <i2c_uninit@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %edi,%ebp
xor    %eax,%eax
call   1090 <enable_i2c_clocls@plt>
test   %eax,%eax
je     11b2 <main+0x32>
mov    %eax,%ebx
lea    0xe60(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    12b7 <main+0x137>
mov    $0xc0,%esi
cmp    $0x2,%ebp
jne    11ce <main+0x4e>
mov    0x8(%r14),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1060 <strtol@plt>
mov    %eax,%esi
mov    %esi,%r15d
sar    %r15d
lea    0xe48(%rip),%rdi
mov    %r15d,%edx
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x186a0,%edi
mov    $0x1,%esi
call   1030 <i2c_init@plt>
lea    0xec4(%rip),%rdi
call   1040 <puts@plt>
mov    $0x186a0,%edi
call   10a0 <usleep@plt>
mov    %r15d,%edi
xor    %esi,%esi
call   1080 <i2c_reg_read@plt>
lea    0xe67(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0xc8,%ebx
lea    0xe6f(%rip),%r14
mov    $0x186a0,%edi
call   10a0 <usleep@plt>
mov    %r15d,%edi
mov    $0x2,%esi
call   1080 <i2c_reg_read@plt>
mov    %eax,%ebp
mov    %r15d,%edi
mov    $0x3,%esi
call   1080 <i2c_reg_read@plt>
mov    %eax,%r13d
mov    %r15d,%edi
mov    $0x4,%esi
call   1080 <i2c_reg_read@plt>
mov    %eax,%r12d
mov    %r15d,%edi
mov    $0x5,%esi
call   1080 <i2c_reg_read@plt>
shl    $0x8,%ebp
add    %r13d,%ebp
xorps  %xmm0,%xmm0
cvtsi2ss %ebp,%xmm0
divss  0xd75(%rip),%xmm0
cvtss2sd %xmm0,%xmm0
mov    %r14,%rdi
mov    %r12d,%esi
mov    %eax,%edx
mov    $0x1,%al
call   1050 <printf@plt>
mov    $0x30d40,%edi
call   10a0 <usleep@plt>
add    $0xffffffffffffffff,%rbx
jne    1230 <main+0xb0>
xor    %ebx,%ebx
xor    %eax,%eax
call   1070 <i2c_uninit@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [31 x i8] c"Error enabling I2C clocks: %i\0A\00"
@.str.1 = [95 x i8] c"Using address 0x%X (0x%X).\0AYou can provide address as a parameter (in hexadecimal without 0x)\0A\00"
@.str.2 = [19 x i8] c"I2C-2 initialized\0A\00"
@.str.3 = [29 x i8] c"Compas firmware version: %i\0A\00"
@.str.4 = [32 x i8] c"Bearing: %f, pitch %u, roll %u\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i64
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca float
%14 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%15 = call i32 (...) @enable_i2c_clocls()
store i32 %15, i32* %8
store float 0.000000e+00, float* %13
store i32 192, i32* %14
%16 = load i32, i32* %8
%17 = icmp ne i32 %16, 0
br i1 %17, label %18, label %22
18: ; preds = %2
%19 = load i32, i32* %8
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i32 %19)
%21 = load i32, i32* %8
store i32 %21, i32* %3
br label %77
22: ; preds = %2
%23 = load i32, i32* %4
%24 = icmp eq i32 %23, 2
br i1 %24, label %25, label %30
25: ; preds = %22
%26 = load i8**, i8*** %5
%27 = getelementptr inbounds i8*, i8** %26, i64 1
%28 = load i8*, i8** %27
%29 = call i32 @strtol(i8* %28, i32* null, i32 16)
store i32 %29, i32* %14
br label %31
30: ; preds = %22
store i32 192, i32* %14
br label %31
31: ; preds = %30, %25
%32 = load i32, i32* %14
%33 = load i32, i32* %14
%34 = ashr i32 %33, 1
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([95 x i8], [95 x i8]* @.str.1, i64 0, i64 0), i32 %32, i32 %34)
%36 = load i32, i32* %14
%37 = ashr i32 %36, 1
store i32 %37, i32* %14
%38 = call i32 @i2c_init(i32 100000, i32 1)
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
%40 = call i32 @usleep(i32 100000)
%41 = load i32, i32* %14
%42 = call i32 @i2c_reg_read(i32 %41, i32 0)
store i32 %42, i32* %7
%43 = load i32, i32* %7
%44 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0), i32 %43)
store i64 0, i64* %6
br label %45
45: ; preds = %72, %31
%46 = load i64, i64* %6
%47 = icmp ult i64 %46, 200
br i1 %47, label %48, label %75
48: ; preds = %45
%49 = call i32 @usleep(i32 100000)
%50 = load i32, i32* %14
%51 = call i32 @i2c_reg_read(i32 %50, i32 2)
store i32 %51, i32* %9
%52 = load i32, i32* %14
%53 = call i32 @i2c_reg_read(i32 %52, i32 3)
store i32 %53, i32* %10
%54 = load i32, i32* %14
%55 = call i32 @i2c_reg_read(i32 %54, i32 4)
store i32 %55, i32* %11
%56 = load i32, i32* %14
%57 = call i32 @i2c_reg_read(i32 %56, i32 5)
store i32 %57, i32* %12
%58 = load i32, i32* %9
%59 = shl i32 %58, 8
%60 = load i32, i32* %10
%61 = add nsw i32 %59, %60
%62 = sitofp i32 %61 to float
%63 = fpext float %62 to double
%64 = fdiv double %63, 1.000000e+01
%65 = fptrunc double %64 to float
store float %65, float* %13
%66 = load float, float* %13
%67 = fpext float %66 to double
%68 = load i32, i32* %11
%69 = load i32, i32* %12
%70 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.4, i64 0, i64 0), double %67, i32 %68, i32 %69)
%71 = call i32 @usleep(i32 200000)
br label %72
72: ; preds = %48
%73 = load i64, i64* %6
%74 = add i64 %73, 1
store i64 %74, i64* %6
br label %45
75: ; preds = %45
%76 = call i32 (...) @i2c_uninit()
store i32 0, i32* %3
br label %77
77: ; preds = %75, %18
%78 = load i32, i32* %3
ret i32 %78
}

LLVM IR (-O2):
@.str = [31 x i8] c"Error enabling I2C clocks: %i\0A\00"
@.str.1 = [95 x i8] c"Using address 0x%X (0x%X).\0AYou can provide address as a parameter (in hexadecimal without 0x)\0A\00"
@.str.3 = [29 x i8] c"Compas firmware version: %i\0A\00"
@.str.4 = [32 x i8] c"Bearing: %f, pitch %u, roll %u\0A\00"
@str = [18 x i8] c"I2C-2 initialized\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = tail call i32 (...) @enable_i2c_clocls()
%4 = icmp eq i32 %3, 0
br i1 %4, label %7, label %5
5: ; preds = %2
%6 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i32 %3)
br label %40
7: ; preds = %2
%8 = icmp eq i32 %0, 2
br i1 %8, label %9, label %13
9: ; preds = %7
%10 = getelementptr inbounds i8*, i8** %1, i64 1
%11 = load i8*, i8** %10
%12 = tail call i32 @strtol(i8* nocapture %11, i32* null, i32 16)
br label %13
13: ; preds = %7, %9
%14 = phi i32 [ %12, %9 ], [ 192, %7 ]
%15 = ashr i32 %14, 1
%16 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([95 x i8], [95 x i8]* @.str.1, i64 0, i64 0), i32 %14, i32 %15)
%17 = tail call i32 @i2c_init(i32 100000, i32 1)
%18 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str, i64 0, i64 0))
%19 = tail call i32 @usleep(i32 100000)
%20 = tail call i32 @i2c_reg_read(i32 %15, i32 0)
%21 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0), i32 %20)
br label %22
22: ; preds = %13, %22
%23 = phi i64 [ 0, %13 ], [ %36, %22 ]
%24 = tail call i32 @usleep(i32 100000)
%25 = tail call i32 @i2c_reg_read(i32 %15, i32 2)
%26 = tail call i32 @i2c_reg_read(i32 %15, i32 3)
%27 = tail call i32 @i2c_reg_read(i32 %15, i32 4)
%28 = tail call i32 @i2c_reg_read(i32 %15, i32 5)
%29 = shl i32 %25, 8
%30 = add nsw i32 %29, %26
%31 = sitofp i32 %30 to float
%32 = fdiv float %31, 1.000000e+01
%33 = fpext float %32 to double
%34 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @.str.4, i64 0, i64 0), double %33, i32 %27, i32 %28)
%35 = tail call i32 @usleep(i32 200000)
%36 = add nuw nsw i64 %23, 1
%37 = icmp eq i64 %36, 200
br i1 %37, label %38, label %22
38: ; preds = %22
%39 = tail call i32 (...) @i2c_uninit()
br label %40
40: ; preds = %38, %5
%41 = phi i32 [ %3, %5 ], [ 0, %38 ]
ret i32 %41
}