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

/* Variables and functions */
scalar_t__ open_i2c_dev(int *, char *, int);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ read_i2c_reg(int, int, int *);
int /*<<< orphan*/ usleep(int);
scalar_t__ write_i2c_reg(int, int, int);
int main(void) {
  int file_i2c;
  char *filename = (char *)"/dev/i2c-1";
  uint16_t vcc;
  uint16_t vbat;
  if (open_i2c_dev(&file_i2c, filename, 0x13) != 0) {
    printf("Failed to setup i2c");
    return -1;
  }
  if (read_i2c_reg(file_i2c, 0x20, &vcc) == 0) {
    printf("Read VCC as %d mV. \n", vcc);
  } else {
    printf("Failed to read VCC \n");
  }
  if (read_i2c_reg(file_i2c, 0x22, &vbat) == 0) {
    printf("Read VCC as %d mV. \n", vbat);
  }
  usleep(1000);
  uint16_t vbat_conv_set = 4880, vbat_conv_read;
  if (write_i2c_reg(file_i2c, 0x50, vbat_conv_set) == 0) {
    printf("Set vbat_conv to %i. \n", vbat_conv_set);
  }
  usleep(1000);
  if (read_i2c_reg(file_i2c, 0x50, &vbat_conv_read) == 0) {
    printf("Read vbat_conv as %i. \n", vbat_conv_read);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe9a(%rip),%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rsi
lea    -0x8(%rbp),%rdi
mov    $0x13,%edx
call   1060 <open_i2c_dev@plt>
cmp    $0x0,%rax
je     11a0 <main+0x50>
lea    0xe7e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1284 <main+0x134>
mov    -0x8(%rbp),%edi
mov    $0x20,%esi
lea    -0x14(%rbp),%rdx
call   1050 <read_i2c_reg@plt>
cmp    $0x0,%rax
jne    11d1 <main+0x81>
mov    -0x14(%rbp),%esi
lea    0xe5a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    11df <main+0x8f>
lea    0xe5c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x8(%rbp),%edi
mov    $0x22,%esi
lea    -0x18(%rbp),%rdx
call   1050 <read_i2c_reg@plt>
cmp    $0x0,%rax
jne    120b <main+0xbb>
mov    -0x18(%rbp),%esi
lea    0xe1b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x3e8,%edi
call   1070 <usleep@plt>
movl   $0x1310,-0x1c(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x1c(%rbp),%edx
mov    $0x50,%esi
call   1030 <write_i2c_reg@plt>
cmp    $0x0,%rax
jne    1247 <main+0xf7>
mov    -0x1c(%rbp),%esi
lea    0xe09(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x3e8,%edi
call   1070 <usleep@plt>
mov    -0x8(%rbp),%edi
mov    $0x50,%esi
lea    -0x20(%rbp),%rdx
call   1050 <read_i2c_reg@plt>
cmp    $0x0,%rax
jne    127d <main+0x12d>
mov    -0x20(%rbp),%esi
lea    0xdea(%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>:
sub    $0x18,%rsp
lea    0xe95(%rip),%rsi
lea    0x8(%rsp),%rdi
mov    $0x13,%edx
call   1070 <open_i2c_dev@plt>
test   %rax,%rax
je     1197 <main+0x37>
lea    0xe85(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0xffffffff,%eax
add    $0x18,%rsp
ret
mov    0x8(%rsp),%edi
lea    0x14(%rsp),%rdx
mov    $0x20,%esi
call   1060 <read_i2c_reg@plt>
test   %rax,%rax
je     11bd <main+0x5d>
lea    0xead(%rip),%rdi
call   1040 <puts@plt>
jmp    11cf <main+0x6f>
mov    0x14(%rsp),%esi
lea    0xe57(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x8(%rsp),%edi
lea    0x10(%rsp),%rdx
mov    $0x22,%esi
call   1060 <read_i2c_reg@plt>
test   %rax,%rax
jne    11f9 <main+0x99>
mov    0x10(%rsp),%esi
lea    0xe2d(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x3e8,%edi
call   1080 <usleep@plt>
mov    0x8(%rsp),%edi
mov    $0x50,%esi
mov    $0x1310,%edx
call   1030 <write_i2c_reg@plt>
test   %rax,%rax
jne    122e <main+0xce>
lea    0xe12(%rip),%rdi
mov    $0x1310,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x3e8,%edi
call   1080 <usleep@plt>
mov    0x8(%rsp),%edi
lea    0xc(%rsp),%rdx
mov    $0x50,%esi
call   1060 <read_i2c_reg@plt>
test   %rax,%rax
jne    1262 <main+0x102>
mov    0xc(%rsp),%esi
lea    0xdf0(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x18,%rsp
ret

LLVM IR (-O0):
@.str = [11 x i8] c"/dev/i2c-1\00"
@.str.1 = [20 x i8] c"Failed to setup i2c\00"
@.str.2 = [21 x i8] c"Read VCC as %d mV. \0A\00"
@.str.3 = [21 x i8] c"Failed to read VCC \0A\00"
@.str.4 = [23 x i8] c"Set vbat_conv to %i. \0A\00"
@.str.5 = [24 x i8] c"Read vbat_conv as %i. \0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i32 0, i32* %1
store i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8** %3
%8 = load i8*, i8** %3
%9 = call i64 @open_i2c_dev(i32* %2, i8* %8, i32 19)
%10 = icmp ne i64 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %0
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %1
br label %47
13: ; preds = %0
%14 = load i32, i32* %2
%15 = call i64 @read_i2c_reg(i32 %14, i32 32, i32* %4)
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %13
%18 = load i32, i32* %4
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i32 %18)
br label %22
20: ; preds = %13
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
br label %22
22: ; preds = %20, %17
%23 = load i32, i32* %2
%24 = call i64 @read_i2c_reg(i32 %23, i32 34, i32* %5)
%25 = icmp eq i64 %24, 0
br i1 %25, label %26, label %29
26: ; preds = %22
%27 = load i32, i32* %5
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i32 %27)
br label %29
29: ; preds = %26, %22
%30 = call i32 @usleep(i32 1000)
store i32 4880, i32* %6
%31 = load i32, i32* %2
%32 = load i32, i32* %6
%33 = call i64 @write_i2c_reg(i32 %31, i32 80, i32 %32)
%34 = icmp eq i64 %33, 0
br i1 %34, label %35, label %38
35: ; preds = %29
%36 = load i32, i32* %6
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0), i32 %36)
br label %38
38: ; preds = %35, %29
%39 = call i32 @usleep(i32 1000)
%40 = load i32, i32* %2
%41 = call i64 @read_i2c_reg(i32 %40, i32 80, i32* %7)
%42 = icmp eq i64 %41, 0
br i1 %42, label %43, label %46
43: ; preds = %38
%44 = load i32, i32* %7
%45 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.5, i64 0, i64 0), i32 %44)
br label %46
46: ; preds = %43, %38
store i32 0, i32* %1
br label %47
47: ; preds = %46, %11
%48 = load i32, i32* %1
ret i32 %48
}

LLVM IR (-O2):
@.str = [11 x i8] c"/dev/i2c-1\00"
@.str.1 = [20 x i8] c"Failed to setup i2c\00"
@.str.2 = [21 x i8] c"Read VCC as %d mV. \0A\00"
@.str.4 = [23 x i8] c"Set vbat_conv to %i. \0A\00"
@.str.5 = [24 x i8] c"Read vbat_conv as %i. \0A\00"
@str = [20 x i8] c"Failed to read VCC \00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = bitcast i32* %1 to i8*
%6 = bitcast i32* %2 to i8*
%7 = bitcast i32* %3 to i8*
%8 = call i64 @open_i2c_dev(i32* %1, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 19)
%9 = icmp eq i64 %8, 0
br i1 %9, label %12, label %10
10: ; preds = %0
%11 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
br label %45
12: ; preds = %0
%13 = load i32, i32* %1
%14 = call i64 @read_i2c_reg(i32 %13, i32 32, i32* %2)
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %12
%17 = load i32, i32* %2
%18 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i32 %17)
br label %21
19: ; preds = %12
%20 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str, i64 0, i64 0))
br label %21
21: ; preds = %19, %16
%22 = load i32, i32* %1
%23 = call i64 @read_i2c_reg(i32 %22, i32 34, i32* %3)
%24 = icmp eq i64 %23, 0
br i1 %24, label %25, label %28
25: ; preds = %21
%26 = load i32, i32* %3
%27 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i32 %26)
br label %28
28: ; preds = %25, %21
%29 = call i32 @usleep(i32 1000)
%30 = bitcast i32* %4 to i8*
%31 = load i32, i32* %1
%32 = call i64 @write_i2c_reg(i32 %31, i32 80, i32 4880)
%33 = icmp eq i64 %32, 0
br i1 %33, label %34, label %36
34: ; preds = %28
%35 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0), i32 4880)
br label %36
36: ; preds = %34, %28
%37 = call i32 @usleep(i32 1000)
%38 = load i32, i32* %1
%39 = call i64 @read_i2c_reg(i32 %38, i32 80, i32* %4)
%40 = icmp eq i64 %39, 0
br i1 %40, label %41, label %44
41: ; preds = %36
%42 = load i32, i32* %4
%43 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.5, i64 0, i64 0), i32 %42)
br label %44
44: ; preds = %41, %36
br label %45
45: ; preds = %44, %10
%46 = phi i32 [ -1, %10 ], [ 0, %44 ]
ret i32 %46
}