#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 enable_i2c_clocls();
int /*<<< orphan*/ i2c_init(int, int);
int i2c_reg_read(int, int);
int /*<<< orphan*/ i2c_reg_write(int, 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;
  uint16_t range = 0;
  uint8_t SONAR = 0xE0;
  if (res) {
    printf("Error enabling I2C clocks: %i\n", res);
    return res;
  }
  if (argc == 2)
    SONAR = strtol(argv[1], NULL, 16);
  printf("Using address 0x%X (0x%X).\nYou can provide address as a parameter "
         "(in hexadecimal without 0x)\n",
         SONAR, SONAR >> 1);
  SONAR >>= 1;
  i2c_init(100000, 1);
  printf("I2C-2 initialized\n");
  usleep(100 * 1000);
  version = i2c_reg_read(SONAR, 0);
  printf("Sonar firmware version: %i\n", version);
  i2c_reg_write(SONAR, 1, 7);
  for (i = 0; i < 200; ++i) {
    i2c_reg_write(SONAR, 0, 0x51);
    usleep(65 * 1000);
    hi = i2c_reg_read(SONAR, 2);
    lo = i2c_reg_read(SONAR, 3);
    range = (hi << 8) + lo;
    printf("Range: %u (%u %u)\n", range, hi, lo);
    usleep(100 * 1000);
  }
  i2c_uninit();
  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    $0x0,%al
call   1090 <enable_i2c_clocls@plt>
mov    %eax,-0x20(%rbp)
movl   $0x0,-0x2c(%rbp)
movl   $0xe0,-0x30(%rbp)
cmpl   $0x0,-0x20(%rbp)
je     11d4 <main+0x54>
mov    -0x20(%rbp),%esi
lea    0xe3e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1309 <main+0x189>
cmpl   $0x2,-0x8(%rbp)
jne    11f7 <main+0x77>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0x10,%edx
call   1050 <strtol@plt>
mov    %eax,-0x30(%rbp)
mov    -0x30(%rbp),%esi
mov    -0x30(%rbp),%edx
sar    $0x1,%edx
lea    0xe18(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x30(%rbp),%eax
sar    $0x1,%eax
mov    %eax,-0x30(%rbp)
mov    $0x186a0,%edi
mov    $0x1,%esi
call   1030 <i2c_init@plt>
lea    0xe51(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x186a0,%edi
call   10a0 <usleep@plt>
mov    -0x30(%rbp),%edi
xor    %esi,%esi
call   1080 <i2c_reg_read@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%esi
lea    0xe3c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x30(%rbp),%edi
mov    $0x1,%esi
mov    $0x7,%edx
call   1070 <i2c_reg_write@plt>
movq   $0x0,-0x18(%rbp)
cmpq   $0xc8,-0x18(%rbp)
jae    12fb <main+0x17b>
mov    -0x30(%rbp),%edi
xor    %esi,%esi
mov    $0x51,%edx
call   1070 <i2c_reg_write@plt>
mov    $0xfde8,%edi
call   10a0 <usleep@plt>
mov    -0x30(%rbp),%edi
mov    $0x2,%esi
call   1080 <i2c_reg_read@plt>
mov    %eax,-0x24(%rbp)
mov    -0x30(%rbp),%edi
mov    $0x3,%esi
call   1080 <i2c_reg_read@plt>
mov    %eax,-0x28(%rbp)
mov    -0x24(%rbp),%eax
shl    $0x8,%eax
add    -0x28(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%esi
mov    -0x24(%rbp),%edx
mov    -0x28(%rbp),%ecx
lea    0xdd4(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x186a0,%edi
call   10a0 <usleep@plt>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    1276 <main+0xf6>
mov    $0x0,%al
call   1060 <i2c_uninit@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   %rbx
push   %rax
mov    %rsi,%r14
mov    %edi,%ebp
xor    %eax,%eax
call   10a0 <enable_i2c_clocls@plt>
test   %eax,%eax
je     11be <main+0x2e>
mov    %eax,%ebx
lea    0xe50(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    12b3 <main+0x123>
mov    $0xe0,%esi
cmp    $0x2,%ebp
jne    11da <main+0x4a>
mov    0x8(%r14),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1060 <strtol@plt>
mov    %eax,%esi
mov    %esi,%r15d
sar    %r15d
lea    0xe38(%rip),%rdi
mov    %r15d,%edx
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x186a0,%edi
mov    $0x1,%esi
call   1030 <i2c_init@plt>
lea    0xea6(%rip),%rdi
call   1040 <puts@plt>
mov    $0x186a0,%edi
call   10b0 <usleep@plt>
mov    %r15d,%edi
xor    %esi,%esi
call   1090 <i2c_reg_read@plt>
lea    0xe57(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %r15d,%edi
mov    $0x1,%esi
mov    $0x7,%edx
call   1080 <i2c_reg_write@plt>
mov    $0xc8,%ebx
lea    0xe4c(%rip),%r14
xchg   %ax,%ax
mov    %r15d,%edi
xor    %esi,%esi
mov    $0x51,%edx
call   1080 <i2c_reg_write@plt>
mov    $0xfde8,%edi
call   10b0 <usleep@plt>
mov    %r15d,%edi
mov    $0x2,%esi
call   1090 <i2c_reg_read@plt>
mov    %eax,%ebp
mov    %r15d,%edi
mov    $0x3,%esi
call   1090 <i2c_reg_read@plt>
mov    %ebp,%esi
shl    $0x8,%esi
add    %eax,%esi
mov    %r14,%rdi
mov    %ebp,%edx
mov    %eax,%ecx
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x186a0,%edi
call   10b0 <usleep@plt>
add    $0xffffffffffffffff,%rbx
jne    1250 <main+0xc0>
xor    %ebx,%ebx
xor    %eax,%eax
call   1070 <i2c_uninit@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
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 = [28 x i8] c"Sonar firmware version: %i\0A\00"
@.str.4 = [19 x i8] c"Range: %u (%u %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
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%13 = call i32 (...) @enable_i2c_clocls()
store i32 %13, i32* %8
store i32 0, i32* %11
store i32 224, i32* %12
%14 = load i32, i32* %8
%15 = icmp ne i32 %14, 0
br i1 %15, label %16, label %20
16: ; preds = %2
%17 = load i32, i32* %8
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i32 %17)
%19 = load i32, i32* %8
store i32 %19, i32* %3
br label %69
20: ; preds = %2
%21 = load i32, i32* %4
%22 = icmp eq i32 %21, 2
br i1 %22, label %23, label %28
23: ; preds = %20
%24 = load i8**, i8*** %5
%25 = getelementptr inbounds i8*, i8** %24, i64 1
%26 = load i8*, i8** %25
%27 = call i32 @strtol(i8* %26, i32* null, i32 16)
store i32 %27, i32* %12
br label %28
28: ; preds = %23, %20
%29 = load i32, i32* %12
%30 = load i32, i32* %12
%31 = ashr i32 %30, 1
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([95 x i8], [95 x i8]* @.str.1, i64 0, i64 0), i32 %29, i32 %31)
%33 = load i32, i32* %12
%34 = ashr i32 %33, 1
store i32 %34, i32* %12
%35 = call i32 @i2c_init(i32 100000, i32 1)
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
%37 = call i32 @usleep(i32 100000)
%38 = load i32, i32* %12
%39 = call i32 @i2c_reg_read(i32 %38, i32 0)
store i32 %39, i32* %7
%40 = load i32, i32* %7
%41 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i32 %40)
%42 = load i32, i32* %12
%43 = call i32 @i2c_reg_write(i32 %42, i32 1, i32 7)
store i64 0, i64* %6
br label %44
44: ; preds = %64, %28
%45 = load i64, i64* %6
%46 = icmp ult i64 %45, 200
br i1 %46, label %47, label %67
47: ; preds = %44
%48 = load i32, i32* %12
%49 = call i32 @i2c_reg_write(i32 %48, i32 0, i32 81)
%50 = call i32 @usleep(i32 65000)
%51 = load i32, i32* %12
%52 = call i32 @i2c_reg_read(i32 %51, i32 2)
store i32 %52, i32* %9
%53 = load i32, i32* %12
%54 = call i32 @i2c_reg_read(i32 %53, i32 3)
store i32 %54, i32* %10
%55 = load i32, i32* %9
%56 = shl i32 %55, 8
%57 = load i32, i32* %10
%58 = add nsw i32 %56, %57
store i32 %58, i32* %11
%59 = load i32, i32* %11
%60 = load i32, i32* %9
%61 = load i32, i32* %10
%62 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0), i32 %59, i32 %60, i32 %61)
%63 = call i32 @usleep(i32 100000)
br label %64
64: ; preds = %47
%65 = load i64, i64* %6
%66 = add i64 %65, 1
store i64 %66, i64* %6
br label %44
67: ; preds = %44
%68 = call i32 (...) @i2c_uninit()
store i32 0, i32* %3
br label %69
69: ; preds = %67, %16
%70 = load i32, i32* %3
ret i32 %70
}

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 = [28 x i8] c"Sonar firmware version: %i\0A\00"
@.str.4 = [19 x i8] c"Range: %u (%u %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 %37
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 = %9, %7
%14 = phi i32 [ %12, %9 ], [ 224, %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 ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i32 %20)
%22 = tail call i32 @i2c_reg_write(i32 %15, i32 1, i32 7)
br label %23
23: ; preds = %13, %23
%24 = phi i64 [ 0, %13 ], [ %33, %23 ]
%25 = tail call i32 @i2c_reg_write(i32 %15, i32 0, i32 81)
%26 = tail call i32 @usleep(i32 65000)
%27 = tail call i32 @i2c_reg_read(i32 %15, i32 2)
%28 = tail call i32 @i2c_reg_read(i32 %15, i32 3)
%29 = shl i32 %27, 8
%30 = add nsw i32 %29, %28
%31 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0), i32 %30, i32 %27, i32 %28)
%32 = tail call i32 @usleep(i32 100000)
%33 = add nuw nsw i64 %24, 1
%34 = icmp eq i64 %33, 200
br i1 %34, label %35, label %23
35: ; preds = %23
%36 = tail call i32 (...) @i2c_uninit()
br label %37
37: ; preds = %35, %5
%38 = phi i32 [ %3, %5 ], [ 0, %35 ]
ret i32 %38
}