#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*/ O_RDWR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
scalar_t__ ioctl(int, int, void *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ sprintf(char *, char *, unsigned char);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int open_i2c_slave(unsigned char i2c_bus, unsigned char slave_address) {
  char devfile[15];
  int fd;
  sprintf(devfile, "/dev/i2c-%d", i2c_bus);
  fd = open(devfile, O_RDWR);
  if (fd < 0) {
    fprintf(stderr, "Couldn't open i2c device file: %s\n", strerror(errno));
    return -1;
  }
  if (ioctl(fd, 0x0703, (void *)(intptr_t)slave_address) < 0) {
    fprintf(stderr, "Could not attach to i2c bus %d slave address %d: %s\n",
            i2c_bus, slave_address, strerror(errno));
    close(fd);
    return -1;
  }
  return fd;
}


x86 assembly (clang, -O0):
<open_i2c_slave>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %sil,%al
mov    %dil,%cl
mov    %cl,-0x5(%rbp)
mov    %al,-0x6(%rbp)
lea    -0x15(%rbp),%rdi
lea    0xe81(%rip),%rsi
movzbl -0x5(%rbp),%edx
call   1070 <sprintf@plt>
lea    -0x15(%rbp),%rdi
mov    0x2e55(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11e1 <open_i2c_slave+0x81>
mov    0x2e2a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2e16(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x20(%rbp),%edi
mov    %rax,%rdx
lea    0xe3e(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    125a <open_i2c_slave+0xfa>
mov    -0x1c(%rbp),%edi
movzbl -0x6(%rbp),%eax
mov    %eax,%edx
mov    $0x703,%esi
call   1030 <ioctl@plt>
cmp    $0x0,%rax
jge    1254 <open_i2c_slave+0xf4>
mov    0x2dd3(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
movzbl -0x5(%rbp),%eax
mov    %eax,-0x28(%rbp)
movzbl -0x6(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    0x2db1(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x2c(%rbp),%edi
mov    -0x28(%rbp),%edx
mov    -0x24(%rbp),%ecx
mov    %rax,%r8
lea    0xdf6(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    125a <open_i2c_slave+0xfa>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_i2c_slave>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %esi,%r14d
mov    %edi,%ebp
lea    0xe8a(%rip),%rsi
lea    0x9(%rsp),%rbx
mov    %rbx,%rdi
mov    %ebp,%edx
call   1070 <sprintf@plt>
mov    0x2e5c(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1060 <open@plt>
test   %eax,%eax
js     11f2 <open_i2c_slave+0x92>
mov    %eax,%ebx
movzbl %r14b,%r14d
mov    %eax,%edi
mov    $0x703,%esi
mov    %r14,%rdx
call   1030 <ioctl@plt>
test   %rax,%rax
jns    1221 <open_i2c_slave+0xc1>
mov    0x2e1d(%rip),%rax
mov    (%rax),%r15d
movzbl %bpl,%ebp
mov    0x2e07(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xe58(%rip),%rsi
mov    %r15d,%edi
mov    %ebp,%edx
mov    %r14d,%ecx
mov    %rax,%r8
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    %ebx,%edi
call   1040 <close@plt>
jmp    121c <open_i2c_slave+0xbc>
mov    0x2ddf(%rip),%rax
mov    (%rax),%ebx
mov    0x2dce(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xdfc(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
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 = [35 x i8] c"Couldn't open i2c device file: %s\0A\00"
@errno = global i32 0
@.str.2 = [53 x i8] c"Could not attach to i2c bus %d slave address %d: %s\0A\00"
define i32 @open_i2c_slave(i8 zeroext %0, i8 zeroext %1) {
%3 = alloca i32
%4 = alloca i8
%5 = alloca i8
%6 = alloca [15 x i8]
%7 = alloca i32
store i8 %0, i8* %4
store i8 %1, i8* %5
%8 = getelementptr inbounds [15 x i8], [15 x i8]* %6, 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 [15 x i8], [15 x i8]* %6, i64 0, i64 0
%12 = load i32, i32* @O_RDWR
%13 = call i32 @open(i8* %11, i32 %12)
store i32 %13, i32* %7
%14 = load i32, i32* %7
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %21
16: ; preds = %2
%17 = load i32, i32* @stderr
%18 = load i32, i32* @errno
%19 = call i8* @strerror(i32 %18)
%20 = call i32 (i32, i8*, ...) @fprintf(i32 %17, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), i8* %19)
store i32 -1, i32* %3
br label %41
21: ; preds = %2
%22 = load i32, i32* %7
%23 = load i8, i8* %5
%24 = zext i8 %23 to i64
%25 = inttoptr i64 %24 to i8*
%26 = call i64 @ioctl(i32 %22, i32 1795, i8* %25)
%27 = icmp slt i64 %26, 0
br i1 %27, label %28, label %39
28: ; preds = %21
%29 = load i32, i32* @stderr
%30 = load i8, i8* %4
%31 = zext i8 %30 to i32
%32 = load i8, i8* %5
%33 = zext i8 %32 to i32
%34 = load i32, i32* @errno
%35 = call i8* @strerror(i32 %34)
%36 = call i32 (i32, i8*, ...) @fprintf(i32 %29, i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.2, i64 0, i64 0), i32 %31, i32 %33, i8* %35)
%37 = load i32, i32* %7
%38 = call i32 @close(i32 %37)
store i32 -1, i32* %3
br label %41
39: ; preds = %21
%40 = load i32, i32* %7
store i32 %40, i32* %3
br label %41
41: ; preds = %39, %28, %16
%42 = load i32, i32* %3
ret i32 %42
}

LLVM IR (-O2):
@.str = [12 x i8] c"/dev/i2c-%d\00"
@O_RDWR = global i32 0
@stderr = global i32 0
@.str.1 = [35 x i8] c"Couldn't open i2c device file: %s\0A\00"
@errno = global i32 0
@.str.2 = [53 x i8] c"Could not attach to i2c bus %d slave address %d: %s\0A\00"
define i32 @open_i2c_slave(i8 zeroext %0, i8 zeroext %1) {
%3 = alloca [15 x i8]
%4 = getelementptr inbounds [15 x i8], [15 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 %9, label %14
9: ; preds = %2
%10 = load i32, i32* @stderr
%11 = load i32, i32* @errno
%12 = tail call i8* @strerror(i32 %11)
%13 = tail call i32 (i32, i8*, ...) @fprintf(i32 %10, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), i8* %12)
br label %27
14: ; preds = %2
%15 = zext i8 %1 to i64
%16 = inttoptr i64 %15 to i8*
%17 = tail call i64 @ioctl(i32 %7, i32 1795, i8* %16)
%18 = icmp slt i64 %17, 0
br i1 %18, label %19, label %27
19: ; preds = %14
%20 = load i32, i32* @stderr
%21 = zext i8 %0 to i32
%22 = zext i8 %1 to i32
%23 = load i32, i32* @errno
%24 = tail call i8* @strerror(i32 %23)
%25 = tail call i32 (i32, i8*, ...) @fprintf(i32 %20, i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.2, i64 0, i64 0), i32 %21, i32 %22, i8* %24)
%26 = tail call i32 @close(i32 %7)
br label %27
27: ; preds = %14, %19, %9
%28 = phi i32 [ -1, %9 ], [ -1, %19 ], [ %7, %14 ]
ret i32 %28
}