#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*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
scalar_t__ ioctl(int, int /*<<< orphan*/, unsigned char);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ sprintf(char *, char *, unsigned char);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int i2c_open(unsigned char bus, unsigned char addr) {
  int file;
  char filename[16];
  sprintf(filename, "/dev/i2c-%d", bus);
  if ((file = open(filename, O_RDWR)) < 0) {
    fprintf(stderr, "i2c_open open error: %s\n", strerror(errno));
    return (file);
  }
  if (ioctl(file, I2C_SLAVE, addr) < 0) {
    fprintf(stderr, "i2c_open ioctl error: %s\n", strerror(errno));
    return (-1);
  }
  return (file);
}


x86 assembly (clang, -O0):
<i2c_open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %sil,%al
mov    %dil,%cl
mov    %cl,-0x5(%rbp)
mov    %al,-0x6(%rbp)
lea    -0x20(%rbp),%rdi
lea    0xe91(%rip),%rsi
movzbl -0x5(%rbp),%edx
call   1060 <sprintf@plt>
lea    -0x20(%rbp),%rdi
mov    0x2e65(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
jge    11cd <i2c_open+0x7d>
mov    0x2e33(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    0x2e1f(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x24(%rbp),%edi
mov    %rax,%rdx
lea    0xe4f(%rip),%rsi
call   1040 <fprintf@plt>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    122a <i2c_open+0xda>
mov    -0xc(%rbp),%edi
mov    0x2e09(%rip),%rax
mov    (%rax),%esi
movzbl -0x6(%rbp),%edx
call   1030 <ioctl@plt>
cmp    $0x0,%rax
jge    1224 <i2c_open+0xd4>
mov    0x2ddd(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    0x2dc9(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x28(%rbp),%edi
mov    %rax,%rdx
lea    0xe12(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    122a <i2c_open+0xda>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<i2c_open>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %esi,%ebp
mov    %edi,%edx
lea    0xe9d(%rip),%rsi
mov    %rsp,%rbx
mov    %rbx,%rdi
call   1060 <sprintf@plt>
mov    0x2e73(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1050 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
js     11ac <i2c_open+0x5c>
mov    0x2e54(%rip),%rax
mov    (%rax),%esi
movzbl %bpl,%edx
mov    %ebx,%edi
call   1030 <ioctl@plt>
test   %rax,%rax
jns    11d7 <i2c_open+0x87>
mov    $0xffffffff,%ebx
lea    0xe7b(%rip),%r14
jmp    11b3 <i2c_open+0x63>
lea    0xe59(%rip),%r14
mov    0x2e16(%rip),%rax
mov    (%rax),%ebp
mov    0x2e05(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    %ebp,%edi
mov    %r14,%rsi
mov    %rax,%rdx
call   1040 <fprintf@plt>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [12 x i8] c"/dev/i2c-%d\00"
@O_RDWR = global i32 0
@stderr = global i32 0
@.str.1 = [25 x i8] c"i2c_open open error: %s\0A\00"
@errno = global i32 0
@I2C_SLAVE = global i32 0
@.str.2 = [26 x i8] c"i2c_open ioctl error: %s\0A\00"
define i32 @i2c_open(i8 zeroext %0, i8 zeroext %1) {
%3 = alloca i32
%4 = alloca i8
%5 = alloca i8
%6 = alloca i32
%7 = alloca [16 x i8]
store i8 %0, i8* %4
store i8 %1, i8* %5
%8 = getelementptr inbounds [16 x i8], [16 x i8]* %7, i64 0, i64 0
%9 = load i8, i8* %4
%10 = call i32 @sprintf(i8* %8, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8 zeroext %9)
%11 = getelementptr inbounds [16 x i8], [16 x i8]* %7, i64 0, i64 0
%12 = load i32, i32* @O_RDWR
%13 = call i32 @open(i8* %11, i32 %12)
store i32 %13, i32* %6
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %21
15: ; preds = %2
%16 = load i32, i32* @stderr
%17 = load i32, i32* @errno
%18 = call i8* @strerror(i32 %17)
%19 = call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i8* %18)
%20 = load i32, i32* %6
store i32 %20, i32* %3
br label %34
21: ; preds = %2
%22 = load i32, i32* %6
%23 = load i32, i32* @I2C_SLAVE
%24 = load i8, i8* %5
%25 = call i64 @ioctl(i32 %22, i32 %23, i8 zeroext %24)
%26 = icmp slt i64 %25, 0
br i1 %26, label %27, label %32
27: ; preds = %21
%28 = load i32, i32* @stderr
%29 = load i32, i32* @errno
%30 = call i8* @strerror(i32 %29)
%31 = call i32 @fprintf(i32 %28, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i8* %30)
store i32 -1, i32* %3
br label %34
32: ; preds = %21
%33 = load i32, i32* %6
store i32 %33, i32* %3
br label %34
34: ; preds = %32, %27, %15
%35 = load i32, i32* %3
ret i32 %35
}

LLVM IR (-O2):
@.str = [12 x i8] c"/dev/i2c-%d\00"
@O_RDWR = global i32 0
@stderr = global i32 0
@.str.1 = [25 x i8] c"i2c_open open error: %s\0A\00"
@errno = global i32 0
@I2C_SLAVE = global i32 0
@.str.2 = [26 x i8] c"i2c_open ioctl error: %s\0A\00"
define i32 @i2c_open(i8 zeroext %0, i8 zeroext %1) {
%3 = alloca [16 x i8]
%4 = getelementptr inbounds [16 x i8], [16 x i8]* %3, i64 0, i64 0
%5 = call i32 @sprintf(i8* %4, i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8 zeroext %0)
%6 = load i32, i32* @O_RDWR
%7 = call i32 @open(i8* %4, i32 %6)
%8 = icmp slt i32 %7, 0
br i1 %8, label %13, label %9
9: ; preds = %2
%10 = load i32, i32* @I2C_SLAVE
%11 = tail call i64 @ioctl(i32 %7, i32 %10, i8 zeroext %1)
%12 = icmp slt i64 %11, 0
br i1 %12, label %13, label %20
13: ; preds = %9, %2
%14 = phi i8* [ getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), %2 ], [ getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), %9 ]
%15 = phi i32 [ %7, %2 ], [ -1, %9 ]
%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* %14, i8* %18)
br label %20
20: ; preds = %13, %9
%21 = phi i32 [ %7, %9 ], [ %15, %13 ]
ret i32 %21
}