#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 /*<<< orphan*/ filename;
typedef scalar_t__ __s32;

/* Variables and functions */
int /*<<< orphan*/ I2C_SLAVE;
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ exit(int);
scalar_t__ i2c_smbus_read_byte_data(int);
scalar_t__ ioctl(int, int /*<<< orphan*/, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int main(void) {
  int file;
  int adapter_nr = 0;
  char filename[20];
  snprintf(filename, sizeof(filename), "/dev/i2c-%d", adapter_nr);
  file = open(filename, O_RDWR);
  if (file < 0) {
    perror("Could not open device");
    exit(1);
  }
  if (ioctl(file, I2C_SLAVE, 0) < 0) {
    perror("Could not set I2C_SLAVE");
    exit(2);
  }
  __s32 v = 0xdeadbeef;
  v = i2c_smbus_read_byte_data(file);
  if (v < 0) {
    perror("i2c_smbus_read_word failed (2)");
    exit(3);
  }
  printf("Done!!!");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0xc(%rbp)
lea    -0x20(%rbp),%rdi
mov    -0xc(%rbp),%ecx
mov    $0x14,%esi
lea    0xe67(%rip),%rdx
call   1040 <snprintf@plt>
lea    -0x20(%rbp),%rdi
mov    0x2e3f(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    11d3 <main+0x63>
lea    0xe48(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x8(%rbp),%edi
mov    0x2e03(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1050 <ioctl@plt>
cmp    $0x0,%rax
jge    1206 <main+0x96>
lea    0xe2b(%rip),%rdi
call   1070 <perror@plt>
mov    $0x2,%edi
call   1090 <exit@plt>
mov    $0xdeadbeef,%eax
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%edi
call   1080 <i2c_smbus_read_byte_data@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jge    123c <main+0xcc>
lea    0xe0d(%rip),%rdi
call   1070 <perror@plt>
mov    $0x3,%edi
call   1090 <exit@plt>
lea    0xe16(%rip),%rdi
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x20,%rsp
lea    0xe84(%rip),%rdx
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x14,%esi
xor    %ecx,%ecx
call   1040 <snprintf@plt>
mov    0x2e53(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1060 <open@plt>
test   %eax,%eax
js     11dc <main+0x6c>
mov    %eax,%ebx
mov    0x2e34(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
xor    %edx,%edx
call   1050 <ioctl@plt>
test   %rax,%rax
js     11f2 <main+0x82>
mov    %ebx,%edi
call   1080 <i2c_smbus_read_byte_data@plt>
test   %rax,%rax
js     1208 <main+0x98>
lea    0xe8a(%rip),%rdi
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbx
ret
lea    0xe29(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
lea    0xe29(%rip),%rdi
call   1070 <perror@plt>
mov    $0x2,%edi
call   1090 <exit@plt>
lea    0xe2b(%rip),%rdi
call   1070 <perror@plt>
mov    $0x3,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [12 x i8] c"/dev/i2c-%d\00"
@O_RDWR = global i32 0
@.str.1 = [22 x i8] c"Could not open device\00"
@I2C_SLAVE = global i32 0
@.str.2 = [24 x i8] c"Could not set I2C_SLAVE\00"
@.str.3 = [31 x i8] c"i2c_smbus_read_word failed (2)\00"
@.str.4 = [8 x i8] c"Done!!!\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca [20 x i8]
%5 = alloca i64
store i32 0, i32* %1
store i32 0, i32* %3
%6 = getelementptr inbounds [20 x i8], [20 x i8]* %4, i64 0, i64 0
%7 = load i32, i32* %3
%8 = call i32 @snprintf(i8* %6, i32 20, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %7)
%9 = getelementptr inbounds [20 x i8], [20 x i8]* %4, i64 0, i64 0
%10 = load i32, i32* @O_RDWR
%11 = call i32 @open(i8* %9, i32 %10)
store i32 %11, i32* %2
%12 = load i32, i32* %2
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %0
%15 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
%16 = call i32 @exit(i32 1)
unreachable
17: ; preds = %0
%18 = load i32, i32* %2
%19 = load i32, i32* @I2C_SLAVE
%20 = call i64 @ioctl(i32 %18, i32 %19, i32 0)
%21 = icmp slt i64 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %17
%23 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
%24 = call i32 @exit(i32 2)
unreachable
25: ; preds = %17
store i64 3735928559, i64* %5
%26 = load i32, i32* %2
%27 = call i64 @i2c_smbus_read_byte_data(i32 %26)
store i64 %27, i64* %5
%28 = load i64, i64* %5
%29 = icmp slt i64 %28, 0
br i1 %29, label %30, label %33
30: ; preds = %25
%31 = call i32 @perror(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0))
%32 = call i32 @exit(i32 3)
unreachable
33: ; preds = %25
%34 = call i32 @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0))
ret i32 0
}

LLVM IR (-O2):
@.str = [12 x i8] c"/dev/i2c-%d\00"
@O_RDWR = global i32 0
@.str.1 = [22 x i8] c"Could not open device\00"
@I2C_SLAVE = global i32 0
@.str.2 = [24 x i8] c"Could not set I2C_SLAVE\00"
@.str.3 = [31 x i8] c"i2c_smbus_read_word failed (2)\00"
@.str.4 = [8 x i8] c"Done!!!\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 20, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 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 %10
7: ; preds = %0
%8 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
%9 = call i32 @exit(i32 1)
unreachable
10: ; preds = %0
%11 = load i32, i32* @I2C_SLAVE
%12 = call i64 @ioctl(i32 %5, i32 %11, i32 0)
%13 = icmp slt i64 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %10
%15 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
%16 = call i32 @exit(i32 2)
unreachable
17: ; preds = %10
%18 = call i64 @i2c_smbus_read_byte_data(i32 %5)
%19 = icmp slt i64 %18, 0
br i1 %19, label %20, label %23
20: ; preds = %17
%21 = call i32 @perror(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0))
%22 = call i32 @exit(i32 3)
unreachable
23: ; preds = %17
%24 = call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0))
ret i32 0
}