#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_PEC;
int /*<<< orphan*/ I2C_SLAVE;
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ close(int);
int ioctl(int, int /*<<< orphan*/, int);
int open(char *, int /*<<< orphan*/);
int snprintf(char *, int, char *, unsigned int);
int i2c_open(unsigned int bus_id, char addr) {
  int fd;
  char filename[128];
  if (snprintf(filename, 128,
               "/dev/i2c-"
               "%d",
               bus_id) >= 128)
    return -1;
  if ((fd = open(filename, O_RDWR)) == -1)
    return -1;
  if (ioctl(fd, I2C_SLAVE, addr) == -1 || ioctl(fd, I2C_PEC, 1) == -1) {
    close(fd);
    return -1;
  }
  return fd;
}


x86 assembly (clang, -O0):
<i2c_open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
mov    %sil,%al
mov    %edi,-0x8(%rbp)
mov    %al,-0x9(%rbp)
lea    -0x90(%rbp),%rdi
mov    -0x8(%rbp),%ecx
mov    $0x80,%esi
lea    0xe96(%rip),%rdx
call   1030 <snprintf@plt>
cmp    $0x80,%eax
jl     1186 <i2c_open+0x46>
movl   $0xffffffff,-0x4(%rbp)
jmp    120a <i2c_open+0xca>
lea    -0x90(%rbp),%rdi
mov    0x2e54(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x10(%rbp)
cmp    $0xffffffff,%eax
jne    11b3 <i2c_open+0x73>
movl   $0xffffffff,-0x4(%rbp)
jmp    120a <i2c_open+0xca>
mov    -0x10(%rbp),%edi
mov    0x2e23(%rip),%rax
mov    (%rax),%esi
movsbl -0x9(%rbp),%edx
call   1040 <ioctl@plt>
cmp    $0xffffffff,%eax
je     11f0 <i2c_open+0xb0>
mov    -0x10(%rbp),%edi
mov    0x2df5(%rip),%rax
mov    (%rax),%esi
mov    $0x1,%edx
call   1040 <ioctl@plt>
cmp    $0xffffffff,%eax
jne    1204 <i2c_open+0xc4>
mov    -0x10(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    120a <i2c_open+0xca>
mov    -0x10(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x90,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<i2c_open>:
push   %rbp
push   %r14
push   %rbx
sub    $0x80,%rsp
mov    %esi,%r14d
mov    %edi,%ecx
lea    0xea9(%rip),%rdx
mov    %rsp,%rdi
mov    $0x80,%esi
call   1030 <snprintf@plt>
mov    $0xffffffff,%ebx
cmp    $0x7f,%eax
jg     11c4 <i2c_open+0x84>
mov    0x2e73(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdi
call   1060 <open@plt>
cmp    $0xffffffff,%eax
je     11c4 <i2c_open+0x84>
mov    %eax,%ebp
mov    0x2e53(%rip),%rax
mov    (%rax),%esi
movsbl %r14b,%edx
mov    %ebp,%edi
call   1040 <ioctl@plt>
cmp    $0xffffffff,%eax
je     11bd <i2c_open+0x7d>
mov    0x2e2a(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
mov    $0x1,%edx
call   1040 <ioctl@plt>
cmp    $0xffffffff,%eax
je     11bd <i2c_open+0x7d>
mov    %ebp,%ebx
jmp    11c4 <i2c_open+0x84>
mov    %ebp,%edi
call   1050 <close@plt>
mov    %ebx,%eax
add    $0x80,%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
@I2C_SLAVE = global i32 0
@I2C_PEC = global i32 0
define i32 @i2c_open(i32 %0, i8 signext %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8
%6 = alloca i32
%7 = alloca [128 x i8]
store i32 %0, i32* %4
store i8 %1, i8* %5
%8 = getelementptr inbounds [128 x i8], [128 x i8]* %7, i64 0, i64 0
%9 = load i32, i32* %4
%10 = call i32 @snprintf(i8* %8, i32 128, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %9)
%11 = icmp sge i32 %10, 128
br i1 %11, label %12, label %13
12: ; preds = %2
store i32 -1, i32* %3
br label %36
13: ; preds = %2
%14 = getelementptr inbounds [128 x i8], [128 x i8]* %7, i64 0, i64 0
%15 = load i32, i32* @O_RDWR
%16 = call i32 @open(i8* %14, i32 %15)
store i32 %16, i32* %6
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %19
18: ; preds = %13
store i32 -1, i32* %3
br label %36
19: ; preds = %13
%20 = load i32, i32* %6
%21 = load i32, i32* @I2C_SLAVE
%22 = load i8, i8* %5
%23 = sext i8 %22 to i32
%24 = call i32 @ioctl(i32 %20, i32 %21, i32 %23)
%25 = icmp eq i32 %24, -1
br i1 %25, label %31, label %26
26: ; preds = %19
%27 = load i32, i32* %6
%28 = load i32, i32* @I2C_PEC
%29 = call i32 @ioctl(i32 %27, i32 %28, i32 1)
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %34
31: ; preds = %26, %19
%32 = load i32, i32* %6
%33 = call i32 @close(i32 %32)
store i32 -1, i32* %3
br label %36
34: ; preds = %26
%35 = load i32, i32* %6
store i32 %35, i32* %3
br label %36
36: ; preds = %34, %31, %18, %12
%37 = load i32, i32* %3
ret i32 %37
}

LLVM IR (-O2):
@.str = [12 x i8] c"/dev/i2c-%d\00"
@O_RDWR = global i32 0
@I2C_SLAVE = global i32 0
@I2C_PEC = global i32 0
define i32 @i2c_open(i32 %0, i8 signext %1) {
%3 = alloca [128 x i8]
%4 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
%5 = call i32 @snprintf(i8* %4, i32 128, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %0)
%6 = icmp sgt i32 %5, 127
br i1 %6, label %22, label %7
7: ; preds = %2
%8 = load i32, i32* @O_RDWR
%9 = call i32 @open(i8* %4, i32 %8)
%10 = icmp eq i32 %9, -1
br i1 %10, label %22, label %11
11: ; preds = %7
%12 = load i32, i32* @I2C_SLAVE
%13 = sext i8 %1 to i32
%14 = call i32 @ioctl(i32 %9, i32 %12, i32 %13)
%15 = icmp eq i32 %14, -1
br i1 %15, label %20, label %16
16: ; preds = %11
%17 = load i32, i32* @I2C_PEC
%18 = call i32 @ioctl(i32 %9, i32 %17, i32 1)
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %22
20: ; preds = %16, %11
%21 = call i32 @close(i32 %9)
br label %22
22: ; preds = %16, %7, %2, %20
%23 = phi i32 [ -1, %20 ], [ -1, %2 ], [ -1, %7 ], [ %9, %16 ]
ret i32 %23
}