#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*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int open_i2c_dev(int);
int /*<<< orphan*/ printf(char *, ...);
int read_distance(int, double *);
int reset_gp2y0e03(int);
int set_slave_addr(int, int);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int main(int argc, char **argv) {
  int fd, res;
  double distance;
  if ((fd = open_i2c_dev(1)) < 0) {
    fprintf(stderr, "open_i2c_dev() failed: %s\n", strerror(errno));
    return fd;
  }
  if ((res = set_slave_addr(fd, 0x40)) < 0) {
    fprintf(stderr, "set_slave_addr() failed: %s\n", strerror(errno));
    close(fd);
    return res;
  }
  if ((res = reset_gp2y0e03(fd)) < 0) {
    fprintf(stderr, "enable_gp2y0e03() failed: %s\n", strerror(errno));
    close(fd);
    return res;
  }
  while (1) {
    sleep(1);
    if ((res = read_distance(fd, &distance)) < 0) {
      printf("Cannot get pressure\n");
    } else {
      printf("distance = %g cm\n", distance);
    }
  }
  close(fd);
  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    $0x1,%edi
call   1090 <open_i2c_dev@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    11f3 <main+0x63>
mov    0x2e1d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    0x2e09(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x24(%rbp),%edi
mov    %rax,%rdx
lea    0xe1d(%rip),%rsi
call   1070 <fprintf@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12f0 <main+0x160>
mov    -0x14(%rbp),%edi
mov    $0x40,%esi
call   1080 <set_slave_addr@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jge    124b <main+0xbb>
mov    0x2dcd(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    0x2db9(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x28(%rbp),%edi
mov    %rax,%rdx
lea    0xde8(%rip),%rsi
call   1070 <fprintf@plt>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12f0 <main+0x160>
mov    -0x14(%rbp),%edi
call   1040 <reset_gp2y0e03@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jge    129e <main+0x10e>
mov    0x2d7a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    0x2d66(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x2c(%rbp),%edi
mov    %rax,%rdx
lea    0xdb2(%rip),%rsi
call   1070 <fprintf@plt>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12f0 <main+0x160>
jmp    12a3 <main+0x113>
mov    $0x1,%edi
call   10b0 <sleep@plt>
mov    -0x14(%rbp),%edi
lea    -0x20(%rbp),%rsi
call   1030 <read_distance@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jge    12d8 <main+0x148>
lea    0xd8a(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    12eb <main+0x15b>
movsd  -0x20(%rbp),%xmm0
lea    0xd87(%rip),%rdi
mov    $0x1,%al
call   1050 <printf@plt>
jmp    12a3 <main+0x113>
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    $0x1,%edi
call   10a0 <open_i2c_dev@plt>
mov    %eax,%ebp
test   %eax,%eax
js     1221 <main+0x81>
mov    %ebp,%edi
mov    $0x40,%esi
call   1090 <set_slave_addr@plt>
test   %eax,%eax
js     124b <main+0xab>
mov    %ebp,%edi
call   1050 <reset_gp2y0e03@plt>
test   %eax,%eax
js     126e <main+0xce>
mov    %rsp,%rbx
lea    0xe84(%rip),%r14
lea    0xe6b(%rip),%r15
jmp    11ff <main+0x5f>
nopl   (%rax)
movsd  (%rsp),%xmm0
mov    %r15,%rdi
mov    $0x1,%al
call   1060 <printf@plt>
mov    $0x1,%edi
call   10c0 <sleep@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
call   1030 <read_distance@plt>
test   %eax,%eax
jns    11f0 <main+0x50>
mov    %r14,%rdi
call   1040 <puts@plt>
jmp    11ff <main+0x5f>
mov    0x2db8(%rip),%rax
mov    (%rax),%ebx
mov    0x2da7(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xdc1(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1080 <fprintf@plt>
jmp    12a3 <main+0x103>
mov    %eax,%ebx
mov    0x2d8c(%rip),%rax
mov    (%rax),%r14d
mov    0x2d7a(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xdaf(%rip),%rsi
jmp    128f <main+0xef>
mov    %eax,%ebx
mov    0x2d69(%rip),%rax
mov    (%rax),%r14d
mov    0x2d57(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xda9(%rip),%rsi
mov    %r14d,%edi
mov    %rax,%rdx
call   1080 <fprintf@plt>
mov    %ebp,%edi
call   1070 <close@plt>
mov    %ebx,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [27 x i8] c"open_i2c_dev() failed: %s\0A\00"
@errno = global i32 0
@.str.1 = [29 x i8] c"set_slave_addr() failed: %s\0A\00"
@.str.2 = [30 x i8] c"enable_gp2y0e03() failed: %s\0A\00"
@.str.3 = [21 x i8] c"Cannot get pressure\0A\00"
@.str.4 = [18 x i8] c"distance = %g cm\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 double
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = call i32 @open_i2c_dev(i32 1)
store i32 %9, i32* %6
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %17
11: ; preds = %2
%12 = load i32, i32* @stderr
%13 = load i32, i32* @errno
%14 = call i8* @strerror(i32 %13)
%15 = call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i8* %14)
%16 = load i32, i32* %6
store i32 %16, i32* %3
br label %53
17: ; preds = %2
%18 = load i32, i32* %6
%19 = call i32 @set_slave_addr(i32 %18, i32 64)
store i32 %19, i32* %7
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %29
21: ; preds = %17
%22 = load i32, i32* @stderr
%23 = load i32, i32* @errno
%24 = call i8* @strerror(i32 %23)
%25 = call i32 @fprintf(i32 %22, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i8* %24)
%26 = load i32, i32* %6
%27 = call i32 @close(i32 %26)
%28 = load i32, i32* %7
store i32 %28, i32* %3
br label %53
29: ; preds = %17
%30 = load i32, i32* %6
%31 = call i32 @reset_gp2y0e03(i32 %30)
store i32 %31, i32* %7
%32 = icmp slt i32 %31, 0
br i1 %32, label %33, label %41
33: ; preds = %29
%34 = load i32, i32* @stderr
%35 = load i32, i32* @errno
%36 = call i8* @strerror(i32 %35)
%37 = call i32 @fprintf(i32 %34, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0), i8* %36)
%38 = load i32, i32* %6
%39 = call i32 @close(i32 %38)
%40 = load i32, i32* %7
store i32 %40, i32* %3
br label %53
41: ; preds = %29
br label %42
42: ; preds = %41, %52
%43 = call i32 @sleep(i32 1)
%44 = load i32, i32* %6
%45 = call i32 @read_distance(i32 %44, double* %8)
store i32 %45, i32* %7
%46 = icmp slt i32 %45, 0
br i1 %46, label %47, label %49
47: ; preds = %42
%48 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
br label %52
49: ; preds = %42
%50 = load double, double* %8
%51 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0), double %50)
br label %52
52: ; preds = %49, %47
br label %42
53: ; preds = %33, %21, %11
%54 = load i32, i32* %3
ret i32 %54
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [27 x i8] c"open_i2c_dev() failed: %s\0A\00"
@errno = global i32 0
@.str.1 = [29 x i8] c"set_slave_addr() failed: %s\0A\00"
@.str.2 = [30 x i8] c"enable_gp2y0e03() failed: %s\0A\00"
@.str.4 = [18 x i8] c"distance = %g cm\0A\00"
@str = [20 x i8] c"Cannot get pressure\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca double
%4 = bitcast double* %3 to i8*
%5 = tail call i32 @open_i2c_dev(i32 1)
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %12
7: ; preds = %2
%8 = load i32, i32* @stderr
%9 = load i32, i32* @errno
%10 = tail call i8* @strerror(i32 %9)
%11 = tail call i32 @fprintf(i32 %8, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i8* %10)
br label %40
12: ; preds = %2
%13 = tail call i32 @set_slave_addr(i32 %5, i32 64)
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %21
15: ; preds = %12
%16 = load i32, i32* @stderr
%17 = load i32, i32* @errno
%18 = tail call i8* @strerror(i32 %17)
%19 = tail call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i8* %18)
%20 = tail call i32 @close(i32 %5)
br label %40
21: ; preds = %12
%22 = tail call i32 @reset_gp2y0e03(i32 %5)
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %30
24: ; preds = %21
%25 = load i32, i32* @stderr
%26 = load i32, i32* @errno
%27 = tail call i8* @strerror(i32 %26)
%28 = tail call i32 @fprintf(i32 %25, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0), i8* %27)
%29 = tail call i32 @close(i32 %5)
br label %40
30: ; preds = %21, %39
%31 = call i32 @sleep(i32 1)
%32 = call i32 @read_distance(i32 %5, double* %3)
%33 = icmp slt i32 %32, 0
br i1 %33, label %34, label %36
34: ; preds = %30
%35 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str, i64 0, i64 0))
br label %39
36: ; preds = %30
%37 = load double, double* %3
%38 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0), double %37)
br label %39
39: ; preds = %36, %34
br label %30
40: ; preds = %24, %15, %7
%41 = phi i32 [ %5, %7 ], [ %13, %15 ], [ %22, %24 ]
ret i32 %41
}