#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*/ I2C_SLAVE;
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int);
scalar_t__ init_sensor(int);
scalar_t__ ioctl(int, int /*<<< orphan*/, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ snprintf(char *, int, char *);
char *strerror(int /*<<< orphan*/);
int main(void) {
  int file;
  char filename[20];
  snprintf(filename, 19, "/dev/i2c-0");
  file = open(filename, O_RDWR);
  if (file < 0) {
    printf("Couldn't open /dev/i2c-0. RC=%s\n", strerror(errno));
    exit(1);
  }
  if (ioctl(file, I2C_SLAVE, 0x19) < 0) {
    printf("Couldn't reach address %x. RC=%s\n", 0x19, strerror(errno));
    close(file);
    exit(-1);
  }
  if (init_sensor(file) != 0) {
    printf("Something went wrong\n");
    close(file);
    exit(1);
  }
  printf("LSM303DLHC I2C test completed successfully!\n");
  close(file);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0x20(%rbp),%rdi
mov    $0x13,%esi
lea    0xe61(%rip),%rdx
call   1040 <snprintf@plt>
lea    -0x20(%rbp),%rdi
mov    0x2e39(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    11ec <main+0x6c>
mov    0x2e06(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    %rax,%rsi
lea    0xe30(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x8(%rbp),%edi
mov    0x2dea(%rip),%rax
mov    (%rax),%esi
mov    $0x19,%edx
call   1050 <ioctl@plt>
cmp    $0x0,%rax
jge    1242 <main+0xc2>
mov    0x2dbd(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    %rax,%rdx
lea    0xe08(%rip),%rdi
mov    $0x19,%esi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>
mov    -0x8(%rbp),%edi
call   1070 <init_sensor@plt>
cmp    $0x0,%rax
je     1274 <main+0xf4>
lea    0xdf3(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
lea    0xde9(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x20,%rsp
lea    0xe64(%rip),%rdx
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x13,%esi
call   1050 <snprintf@plt>
mov    0x2e35(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1090 <open@plt>
test   %eax,%eax
js     1204 <main+0x74>
mov    %eax,%ebx
mov    0x2e16(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    $0x19,%edx
call   1060 <ioctl@plt>
test   %rax,%rax
js     122d <main+0x9d>
mov    %ebx,%edi
call   1080 <init_sensor@plt>
test   %rax,%rax
jne    1262 <main+0xd2>
lea    0xe5e(%rip),%rdi
call   1030 <puts@plt>
mov    %ebx,%edi
call   1070 <close@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbx
ret
mov    0x2dc5(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xdf2(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    0x2d9c(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xdea(%rip),%rdi
mov    $0x19,%esi
mov    %rax,%rdx
xor    %eax,%eax
call   1040 <printf@plt>
mov    %ebx,%edi
call   1070 <close@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>
lea    0xe11(%rip),%rdi
call   1030 <puts@plt>
mov    %ebx,%edi
call   1070 <close@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [11 x i8] c"/dev/i2c-0\00"
@O_RDWR = global i32 0
@.str.1 = [33 x i8] c"Couldn't open /dev/i2c-0. RC=%s\0A\00"
@errno = global i32 0
@I2C_SLAVE = global i32 0
@.str.2 = [34 x i8] c"Couldn't reach address %x. RC=%s\0A\00"
@.str.3 = [22 x i8] c"Something went wrong\0A\00"
@.str.4 = [45 x i8] c"LSM303DLHC I2C test completed successfully!\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca [20 x i8]
store i32 0, i32* %1
%4 = getelementptr inbounds [20 x i8], [20 x i8]* %3, i64 0, i64 0
%5 = call i32 @snprintf(i8* %4, i32 19, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%6 = getelementptr inbounds [20 x i8], [20 x i8]* %3, i64 0, i64 0
%7 = load i32, i32* @O_RDWR
%8 = call i32 @open(i8* %6, i32 %7)
store i32 %8, i32* %2
%9 = load i32, i32* %2
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %16
11: ; preds = %0
%12 = load i32, i32* @errno
%13 = call i8* @strerror(i32 %12)
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i8* %13)
%15 = call i32 @exit(i32 1)
unreachable
16: ; preds = %0
%17 = load i32, i32* %2
%18 = load i32, i32* @I2C_SLAVE
%19 = call i64 @ioctl(i32 %17, i32 %18, i32 25)
%20 = icmp slt i64 %19, 0
br i1 %20, label %21, label %28
21: ; preds = %16
%22 = load i32, i32* @errno
%23 = call i8* @strerror(i32 %22)
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0), i32 25, i8* %23)
%25 = load i32, i32* %2
%26 = call i32 @close(i32 %25)
%27 = call i32 @exit(i32 -1)
unreachable
28: ; preds = %16
%29 = load i32, i32* %2
%30 = call i64 @init_sensor(i32 %29)
%31 = icmp ne i64 %30, 0
br i1 %31, label %32, label %37
32: ; preds = %28
%33 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0))
%34 = load i32, i32* %2
%35 = call i32 @close(i32 %34)
%36 = call i32 @exit(i32 1)
unreachable
37: ; preds = %28
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.4, i64 0, i64 0))
%39 = load i32, i32* %2
%40 = call i32 @close(i32 %39)
ret i32 0
}

LLVM IR (-O2):
@.str = [11 x i8] c"/dev/i2c-0\00"
@O_RDWR = global i32 0
@.str.1 = [33 x i8] c"Couldn't open /dev/i2c-0. RC=%s\0A\00"
@errno = global i32 0
@I2C_SLAVE = global i32 0
@.str.2 = [34 x i8] c"Couldn't reach address %x. RC=%s\0A\00"
@str = [44 x i8] c"LSM303DLHC I2C test completed successfully!\00"
@str.5 = [21 x i8] c"Something went wrong\00"
define i32 @main() {
%1 = alloca [20 x i8]
%2 = getelementptr inbounds [20 x i8], [20 x i8]* %1, i64 0, i64 0
%3 = call i32 @snprintf(i8* %2, i32 19, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%4 = load i32, i32* @O_RDWR
%5 = call i32 @open(i8* %2, i32 %4)
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %12
7: ; preds = %0
%8 = load i32, i32* @errno
%9 = call i8* @strerror(i32 %8)
%10 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i8* %9)
%11 = call i32 @exit(i32 1)
unreachable
12: ; preds = %0
%13 = load i32, i32* @I2C_SLAVE
%14 = call i64 @ioctl(i32 %5, i32 %13, i32 25)
%15 = icmp slt i64 %14, 0
br i1 %15, label %16, label %22
16: ; preds = %12
%17 = load i32, i32* @errno
%18 = call i8* @strerror(i32 %17)
%19 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0), i32 25, i8* %18)
%20 = call i32 @close(i32 %5)
%21 = call i32 @exit(i32 -1)
unreachable
22: ; preds = %12
%23 = call i64 @init_sensor(i32 %5)
%24 = icmp eq i64 %23, 0
br i1 %24, label %29, label %25
25: ; preds = %22
%26 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str.5, i64 0, i64 0))
%27 = call i32 @close(i32 %5)
%28 = call i32 @exit(i32 1)
unreachable
29: ; preds = %22
%30 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([44 x i8], [44 x i8]* @str, i64 0, i64 0))
%31 = call i32 @close(i32 %5)
ret i32 0
}