#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 EINVAL;
int /*<<< orphan*/ I2C_FUNCS;
int I2C_FUNC_I2C;
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ close(int);
scalar_t__ ioctl(int, int /*<<< orphan*/, int *);
int open(char *, int /*<<< orphan*/);
scalar_t__ snprintf(char *, int, char *, unsigned int);
int i2c_open(unsigned chan) {
  int fd;
  char name[16];
  int funcs;
  if (snprintf(name, 16, "/dev/i2c-%u", chan) < 0)
    return -EINVAL;
  fd = open(name, O_RDWR);
  if (fd < 0)
    return fd;
  if (ioctl(fd, I2C_FUNCS, &funcs) < 0 || !(funcs & I2C_FUNC_I2C)) {
    close(fd);
    return -EINVAL;
  }
  return fd;
}


x86 assembly (clang, -O0):
<i2c_open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
lea    -0x20(%rbp),%rdi
mov    -0x8(%rbp),%ecx
mov    $0x10,%esi
lea    0xea2(%rip),%rdx
call   1030 <snprintf@plt>
cmp    $0x0,%rax
jge    1180 <i2c_open+0x40>
xor    %eax,%eax
mov    0x2e52(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    11ff <i2c_open+0xbf>
lea    -0x20(%rbp),%rdi
mov    0x2e5d(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    11aa <i2c_open+0x6a>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11ff <i2c_open+0xbf>
mov    -0xc(%rbp),%edi
mov    0x2e1c(%rip),%rax
mov    (%rax),%esi
lea    -0x24(%rbp),%rdx
call   1040 <ioctl@plt>
cmp    $0x0,%rax
jl     11de <i2c_open+0x9e>
mov    -0x24(%rbp),%eax
mov    0x2e05(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
jne    11f9 <i2c_open+0xb9>
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
xor    %eax,%eax
mov    0x2dd9(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    11ff <i2c_open+0xbf>
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   %rbx
sub    $0x20,%rsp
mov    %edi,%ecx
lea    0xeb2(%rip),%rdx
lea    0x10(%rsp),%rdi
mov    $0x10,%esi
call   1030 <snprintf@plt>
test   %rax,%rax
js     11ab <i2c_open+0x6b>
mov    0x2e7f(%rip),%rax
mov    (%rax),%esi
lea    0x10(%rsp),%rdi
call   1060 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
js     11b6 <i2c_open+0x76>
mov    0x2e4e(%rip),%rax
mov    (%rax),%esi
lea    0xc(%rsp),%rdx
mov    %ebx,%edi
call   1040 <ioctl@plt>
test   %rax,%rax
js     11a4 <i2c_open+0x64>
mov    0x2e3c(%rip),%rax
mov    (%rax),%eax
test   %eax,0xc(%rsp)
jne    11b6 <i2c_open+0x76>
mov    %ebx,%edi
call   1050 <close@plt>
mov    0x2e16(%rip),%rax
xor    %ebx,%ebx
sub    (%rax),%ebx
mov    %ebx,%eax
add    $0x20,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [12 x i8] c"/dev/i2c-%u\00"
@EINVAL = global i32 0
@O_RDWR = global i32 0
@I2C_FUNCS = global i32 0
@I2C_FUNC_I2C = global i32 0
define i32 @i2c_open(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca [16 x i8]
%6 = alloca i32
store i32 %0, i32* %3
%7 = getelementptr inbounds [16 x i8], [16 x i8]* %5, i64 0, i64 0
%8 = load i32, i32* %3
%9 = call i64 @snprintf(i8* %7, i32 16, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %8)
%10 = icmp slt i64 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %1
%12 = load i32, i32* @EINVAL
%13 = sub nsw i32 0, %12
store i32 %13, i32* %2
br label %39
14: ; preds = %1
%15 = getelementptr inbounds [16 x i8], [16 x i8]* %5, i64 0, i64 0
%16 = load i32, i32* @O_RDWR
%17 = call i32 @open(i8* %15, i32 %16)
store i32 %17, i32* %4
%18 = load i32, i32* %4
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %14
%21 = load i32, i32* %4
store i32 %21, i32* %2
br label %39
22: ; preds = %14
%23 = load i32, i32* %4
%24 = load i32, i32* @I2C_FUNCS
%25 = call i64 @ioctl(i32 %23, i32 %24, i32* %6)
%26 = icmp slt i64 %25, 0
br i1 %26, label %32, label %27
27: ; preds = %22
%28 = load i32, i32* %6
%29 = load i32, i32* @I2C_FUNC_I2C
%30 = and i32 %28, %29
%31 = icmp ne i32 %30, 0
br i1 %31, label %37, label %32
32: ; preds = %27, %22
%33 = load i32, i32* %4
%34 = call i32 @close(i32 %33)
%35 = load i32, i32* @EINVAL
%36 = sub nsw i32 0, %35
store i32 %36, i32* %2
br label %39
37: ; preds = %27
%38 = load i32, i32* %4
store i32 %38, i32* %2
br label %39
39: ; preds = %37, %32, %20, %11
%40 = load i32, i32* %2
ret i32 %40
}

LLVM IR (-O2):
@.str = [12 x i8] c"/dev/i2c-%u\00"
@EINVAL = global i32 0
@O_RDWR = global i32 0
@I2C_FUNCS = global i32 0
@I2C_FUNC_I2C = global i32 0
define i32 @i2c_open(i32 %0) {
%2 = alloca [16 x i8]
%3 = alloca i32
%4 = getelementptr inbounds [16 x i8], [16 x i8]* %2, i64 0, i64 0
%5 = bitcast i32* %3 to i8*
%6 = call i64 @snprintf(i8* %4, i32 16, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %0)
%7 = icmp slt i64 %6, 0
br i1 %7, label %8, label %11
8: ; preds = %1
%9 = load i32, i32* @EINVAL
%10 = sub nsw i32 0, %9
br label %28
11: ; preds = %1
%12 = load i32, i32* @O_RDWR
%13 = call i32 @open(i8* %4, i32 %12)
%14 = icmp slt i32 %13, 0
br i1 %14, label %28, label %15
15: ; preds = %11
%16 = load i32, i32* @I2C_FUNCS
%17 = call i64 @ioctl(i32 %13, i32 %16, i32* %3)
%18 = icmp slt i64 %17, 0
br i1 %18, label %24, label %19
19: ; preds = %15
%20 = load i32, i32* %3
%21 = load i32, i32* @I2C_FUNC_I2C
%22 = and i32 %21, %20
%23 = icmp eq i32 %22, 0
br i1 %23, label %24, label %28
24: ; preds = %19, %15
%25 = call i32 @close(i32 %13)
%26 = load i32, i32* @EINVAL
%27 = sub nsw i32 0, %26
br label %28
28: ; preds = %19, %11, %24, %8
%29 = phi i32 [ %10, %8 ], [ %27, %24 ], [ %13, %11 ], [ %13, %19 ]
ret i32 %29
}