#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);
scalar_t__ ioctl(int, int /*<<< orphan*/, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ snprintf(char *, int, char *, int const);
int open_i2c(const int port) {
  int fd;
  char filename[20];
  snprintf(filename, 19, "/dev/i2c-%d", port);
  fd = open(filename, O_RDWR);
  if (fd < 0) {
    perror("Unable to open i2c adapter");
    return -1;
  }
  if (ioctl(fd, I2C_SLAVE, 0x3B) < 0) {
    perror("Unable to select proper address");
    close(fd);
    return -1;
  }
  return fd;
}


x86 assembly (clang, -O0):
<open_i2c>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
lea    -0x20(%rbp),%rdi
mov    -0x8(%rbp),%ecx
mov    $0x13,%esi
lea    0xe92(%rip),%rdx
call   1030 <snprintf@plt>
lea    -0x20(%rbp),%rdi
mov    0x2e6a(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    11aa <open_i2c+0x5a>
lea    0xe73(%rip),%rdi
call   1070 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f0 <open_i2c+0xa0>
mov    -0xc(%rbp),%edi
mov    0x2e2c(%rip),%rax
mov    (%rax),%esi
mov    $0x3b,%edx
call   1040 <ioctl@plt>
cmp    $0x0,%rax
jge    11ea <open_i2c+0x9a>
lea    0xe56(%rip),%rdi
call   1070 <perror@plt>
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f0 <open_i2c+0xa0>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_i2c>:
push   %rbx
sub    $0x20,%rsp
mov    %edi,%ecx
lea    0xea2(%rip),%rdx
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x13,%esi
call   1030 <snprintf@plt>
mov    0x2e73(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1060 <open@plt>
test   %eax,%eax
js     11a7 <open_i2c+0x57>
mov    %eax,%ebx
mov    0x2e54(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    $0x3b,%edx
call   1040 <ioctl@plt>
test   %rax,%rax
js     11b5 <open_i2c+0x65>
mov    %ebx,%eax
add    $0x20,%rsp
pop    %rbx
ret
lea    0xe5e(%rip),%rdi
call   1070 <perror@plt>
jmp    11c8 <open_i2c+0x78>
lea    0xe6b(%rip),%rdi
call   1070 <perror@plt>
mov    %ebx,%edi
call   1050 <close@plt>
mov    $0xffffffff,%ebx
jmp    119f <open_i2c+0x4f>

LLVM IR (-O0):
@.str = [12 x i8] c"/dev/i2c-%d\00"
@O_RDWR = global i32 0
@.str.1 = [27 x i8] c"Unable to open i2c adapter\00"
@I2C_SLAVE = global i32 0
@.str.2 = [32 x i8] c"Unable to select proper address\00"
define i32 @open_i2c(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca [20 x i8]
store i32 %0, i32* %3
%6 = getelementptr inbounds [20 x i8], [20 x i8]* %5, i64 0, i64 0
%7 = load i32, i32* %3
%8 = call i32 @snprintf(i8* %6, i32 19, 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]* %5, i64 0, i64 0
%10 = load i32, i32* @O_RDWR
%11 = call i32 @open(i8* %9, i32 %10)
store i32 %11, i32* %4
%12 = load i32, i32* %4
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %1
%15 = call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %2
br label %27
16: ; preds = %1
%17 = load i32, i32* %4
%18 = load i32, i32* @I2C_SLAVE
%19 = call i64 @ioctl(i32 %17, i32 %18, i32 59)
%20 = icmp slt i64 %19, 0
br i1 %20, label %21, label %25
21: ; preds = %16
%22 = call i32 @perror(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0))
%23 = load i32, i32* %4
%24 = call i32 @close(i32 %23)
store i32 -1, i32* %2
br label %27
25: ; preds = %16
%26 = load i32, i32* %4
store i32 %26, i32* %2
br label %27
27: ; preds = %25, %21, %14
%28 = load i32, i32* %2
ret i32 %28
}

LLVM IR (-O2):
@.str = [12 x i8] c"/dev/i2c-%d\00"
@O_RDWR = global i32 0
@.str.1 = [27 x i8] c"Unable to open i2c adapter\00"
@I2C_SLAVE = global i32 0
@.str.2 = [32 x i8] c"Unable to select proper address\00"
define i32 @open_i2c(i32 %0) {
%2 = alloca [20 x i8]
%3 = getelementptr inbounds [20 x i8], [20 x i8]* %2, i64 0, i64 0
%4 = call i32 @snprintf(i8* %3, i32 19, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %0)
%5 = load i32, i32* @O_RDWR
%6 = call i32 @open(i8* %3, i32 %5)
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %1
%9 = call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0))
br label %17
10: ; preds = %1
%11 = load i32, i32* @I2C_SLAVE
%12 = call i64 @ioctl(i32 %6, i32 %11, i32 59)
%13 = icmp slt i64 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %10
%15 = call i32 @perror(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0))
%16 = call i32 @close(i32 %6)
br label %17
17: ; preds = %10, %14, %8
%18 = phi i32 [ -1, %8 ], [ -1, %14 ], [ %6, %10 ]
ret i32 %18
}