#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 */
scalar_t__ EACCES;
scalar_t__ ENOENT;
scalar_t__ ENOTDIR;
int /*<<< orphan*/ O_RDWR;
scalar_t__ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ snprintf(char *, size_t, char *, int);
int /*<<< orphan*/ sprintf(char *, char *, int);
int /*<<< orphan*/ stderr;
char *strerror(scalar_t__);
int open_i2c_dev(int i2cbus, char *filename, size_t size, int quiet) {
  int file;
  snprintf(filename, size, "/dev/i2c/%d", i2cbus);
  filename[size - 1] = '\0';
  file = open(filename, O_RDWR);
  if (file < 0 && (errno == ENOENT || errno == ENOTDIR)) {
    sprintf(filename, "/dev/i2c-%d", i2cbus);
    file = open(filename, O_RDWR);
  }
  if (file < 0 && !quiet) {
    if (errno == ENOENT) {
      fprintf(stderr,
              "Error: Could not open file "
              "`/dev/i2c-%d' or `/dev/i2c/%d': %s\n",
              i2cbus, i2cbus, strerror(ENOENT));
    } else {
      fprintf(stderr,
              "Error: Could not open file "
              "`%s': %s\n",
              filename, strerror(errno));
      if (errno == EACCES)
        fprintf(stderr, "Run as root?\n");
    }
  }
  return file;
}


x86 assembly (clang, -O0):
<open_i2c_dev>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x4(%rbp),%ecx
lea    0xe88(%rip),%rdx
call   1030 <snprintf@plt>
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%rcx
sub    $0x1,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%rdi
mov    0x2e48(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    1208 <open_i2c_dev+0xb8>
mov    0x2e0d(%rip),%rax
mov    (%rax),%rax
mov    0x2df3(%rip),%rcx
cmp    (%rcx),%rax
je     11e0 <open_i2c_dev+0x90>
mov    0x2df3(%rip),%rax
mov    (%rax),%rax
mov    0x2e21(%rip),%rcx
cmp    (%rcx),%rax
jne    1208 <open_i2c_dev+0xb8>
mov    -0x10(%rbp),%rdi
mov    -0x4(%rbp),%edx
lea    0xe1e(%rip),%rsi
call   1060 <sprintf@plt>
mov    -0x10(%rbp),%rdi
mov    0x2de2(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    12f2 <open_i2c_dev+0x1a2>
cmpl   $0x0,-0x1c(%rbp)
jne    12f2 <open_i2c_dev+0x1a2>
mov    0x2d9d(%rip),%rax
mov    (%rax),%rax
mov    0x2d83(%rip),%rcx
cmp    (%rcx),%rax
jne    127c <open_i2c_dev+0x12c>
mov    0x2d8b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    0x2d5b(%rip),%rax
mov    (%rax),%rdi
call   1070 <strerror@plt>
mov    -0x2c(%rbp),%edi
mov    -0x28(%rbp),%edx
mov    -0x24(%rbp),%ecx
mov    %rax,%r8
lea    0xda8(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
jmp    12ed <open_i2c_dev+0x19d>
mov    0x2d45(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    0x2d29(%rip),%rax
mov    (%rax),%rdi
call   1070 <strerror@plt>
mov    -0x3c(%rbp),%edi
mov    -0x38(%rbp),%rdx
mov    %rax,%rcx
lea    0xda7(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    0x2d02(%rip),%rax
mov    (%rax),%rax
mov    0x2d10(%rip),%rcx
cmp    (%rcx),%rax
jne    12e8 <open_i2c_dev+0x198>
mov    0x2cf0(%rip),%rax
mov    (%rax),%edi
lea    0xd9b(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
jmp    12ed <open_i2c_dev+0x19d>
jmp    12f2 <open_i2c_dev+0x1a2>
mov    -0x20(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_i2c_dev>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %ecx,%r15d
mov    %rdx,%rbx
mov    %rsi,%rbp
mov    %edi,%r14d
lea    0xe92(%rip),%rdx
mov    %rsi,%rdi
mov    %rbx,%rsi
mov    %r14d,%ecx
call   1030 <snprintf@plt>
movb   $0x0,-0x1(%rbx,%rbp,1)
mov    0x2e58(%rip),%r12
mov    (%r12),%esi
mov    %rbp,%rdi
call   1050 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
js     11ab <open_i2c_dev+0x5b>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2e0e(%rip),%rdx
mov    (%rdx),%rax
mov    0x2df4(%rip),%r13
cmp    0x0(%r13),%rax
je     11ce <open_i2c_dev+0x7e>
mov    0x2e2f(%rip),%rcx
cmp    (%rcx),%rax
jne    11f4 <open_i2c_dev+0xa4>
lea    0xe37(%rip),%rsi
mov    %rbp,%rdi
mov    %rdx,%rbx
mov    %r14d,%edx
call   1060 <sprintf@plt>
mov    (%r12),%esi
mov    %rbp,%rdi
call   1050 <open@plt>
mov    %rbx,%rdx
mov    %eax,%ebx
test   %r15d,%r15d
jne    119a <open_i2c_dev+0x4a>
test   %ebx,%ebx
jns    119a <open_i2c_dev+0x4a>
mov    (%rdx),%r12
mov    0x0(%r13),%r13
mov    0x2dbd(%rip),%rax
mov    (%rax),%r15d
mov    %r12,%rdi
call   1070 <strerror@plt>
cmp    %r13,%r12
jne    123a <open_i2c_dev+0xea>
lea    0xdf6(%rip),%rsi
mov    %r15d,%edi
mov    %r14d,%edx
mov    %r14d,%ecx
mov    %rax,%r8
xor    %eax,%eax
call   1040 <fprintf@plt>
jmp    119a <open_i2c_dev+0x4a>
mov    0x2d87(%rip),%r14
lea    0xe0f(%rip),%rsi
mov    %r15d,%edi
mov    %rbp,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    0x2d61(%rip),%rax
mov    (%rax),%rax
mov    0x2d6f(%rip),%rcx
cmp    (%rcx),%rax
jne    119a <open_i2c_dev+0x4a>
mov    (%r14),%edi
lea    0xe00(%rip),%rsi
xor    %eax,%eax
call   1040 <fprintf@plt>
jmp    119a <open_i2c_dev+0x4a>

LLVM IR (-O0):
@.str = [12 x i8] c"/dev/i2c/%d\00"
@O_RDWR = global i32 0
@errno = global i64 0
@ENOENT = global i64 0
@ENOTDIR = global i64 0
@.str.1 = [12 x i8] c"/dev/i2c-%d\00"
@stderr = global i32 0
@.str.2 = [63 x i8] c"Error: Could not open file `/dev/i2c-%d' or `/dev/i2c/%d': %s\0A\00"
@.str.3 = [37 x i8] c"Error: Could not open file `%s': %s\0A\00"
@EACCES = global i64 0
@.str.4 = [14 x i8] c"Run as root?\0A\00"
define i32 @open_i2c_dev(i32 %0, i8* %1, i64 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
%9 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i64 %2, i64* %7
store i32 %3, i32* %8
%10 = load i8*, i8** %6
%11 = load i64, i64* %7
%12 = load i32, i32* %5
%13 = call i32 @snprintf(i8* %10, i64 %11, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %12)
%14 = load i8*, i8** %6
%15 = load i64, i64* %7
%16 = sub i64 %15, 1
%17 = getelementptr inbounds i8, i8* %14, i64 %16
store i8 0, i8* %17
%18 = load i8*, i8** %6
%19 = load i32, i32* @O_RDWR
%20 = call i32 @open(i8* %18, i32 %19)
store i32 %20, i32* %9
%21 = load i32, i32* %9
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %38
23: ; preds = %4
%24 = load i64, i64* @errno
%25 = load i64, i64* @ENOENT
%26 = icmp eq i64 %24, %25
br i1 %26, label %31, label %27
27: ; preds = %23
%28 = load i64, i64* @errno
%29 = load i64, i64* @ENOTDIR
%30 = icmp eq i64 %28, %29
br i1 %30, label %31, label %38
31: ; preds = %27, %23
%32 = load i8*, i8** %6
%33 = load i32, i32* %5
%34 = call i32 @sprintf(i8* %32, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %33)
%35 = load i8*, i8** %6
%36 = load i32, i32* @O_RDWR
%37 = call i32 @open(i8* %35, i32 %36)
store i32 %37, i32* %9
br label %38
38: ; preds = %31, %27, %4
%39 = load i32, i32* %9
%40 = icmp slt i32 %39, 0
br i1 %40, label %41, label %69
41: ; preds = %38
%42 = load i32, i32* %8
%43 = icmp ne i32 %42, 0
br i1 %43, label %69, label %44
44: ; preds = %41
%45 = load i64, i64* @errno
%46 = load i64, i64* @ENOENT
%47 = icmp eq i64 %45, %46
br i1 %47, label %48, label %55
48: ; preds = %44
%49 = load i32, i32* @stderr
%50 = load i32, i32* %5
%51 = load i32, i32* %5
%52 = load i64, i64* @ENOENT
%53 = call i8* @strerror(i64 %52)
%54 = call i32 (i32, i8*, ...) @fprintf(i32 %49, i8* getelementptr inbounds ([63 x i8], [63 x i8]* @.str.2, i64 0, i64 0), i32 %50, i32 %51, i8* %53)
br label %68
55: ; preds = %44
%56 = load i32, i32* @stderr
%57 = load i8*, i8** %6
%58 = load i64, i64* @errno
%59 = call i8* @strerror(i64 %58)
%60 = call i32 (i32, i8*, ...) @fprintf(i32 %56, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.3, i64 0, i64 0), i8* %57, i8* %59)
%61 = load i64, i64* @errno
%62 = load i64, i64* @EACCES
%63 = icmp eq i64 %61, %62
br i1 %63, label %64, label %67
64: ; preds = %55
%65 = load i32, i32* @stderr
%66 = call i32 (i32, i8*, ...) @fprintf(i32 %65, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0))
br label %67
67: ; preds = %64, %55
br label %68
68: ; preds = %67, %48
br label %69
69: ; preds = %68, %41, %38
%70 = load i32, i32* %9
ret i32 %70
}

LLVM IR (-O2):
@.str = [12 x i8] c"/dev/i2c/%d\00"
@O_RDWR = global i32 0
@errno = global i64 0
@ENOENT = global i64 0
@ENOTDIR = global i64 0
@.str.1 = [12 x i8] c"/dev/i2c-%d\00"
@stderr = global i32 0
@.str.2 = [63 x i8] c"Error: Could not open file `/dev/i2c-%d' or `/dev/i2c/%d': %s\0A\00"
@.str.3 = [37 x i8] c"Error: Could not open file `%s': %s\0A\00"
@EACCES = global i64 0
@.str.4 = [14 x i8] c"Run as root?\0A\00"
define i32 @open_i2c_dev(i32 %0, i8* %1, i64 %2, i32 %3) {
%5 = tail call i32 @snprintf(i8* %1, i64 %2, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %0)
%6 = add i64 %2, -1
%7 = getelementptr inbounds i8, i8* %1, i64 %6
store i8 0, i8* %7
%8 = load i32, i32* @O_RDWR
%9 = tail call i32 @open(i8* %1, i32 %8)
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %43
11: ; preds = %4
%12 = load i64, i64* @errno
%13 = load i64, i64* @ENOENT
%14 = icmp eq i64 %12, %13
%15 = load i64, i64* @ENOTDIR
%16 = icmp eq i64 %12, %15
%17 = select i1 %14, i1 true, i1 %16
br i1 %17, label %18, label %22
18: ; preds = %11
%19 = tail call i32 @sprintf(i8* dereferenceable(1) %1, i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %0)
%20 = load i32, i32* @O_RDWR
%21 = tail call i32 @open(i8* %1, i32 %20)
br label %22
22: ; preds = %11, %18
%23 = phi i32 [ %21, %18 ], [ %9, %11 ]
%24 = icmp sgt i32 %23, -1
%25 = icmp ne i32 %3, 0
%26 = or i1 %25, %24
br i1 %26, label %43, label %27
27: ; preds = %22
%28 = load i64, i64* @errno
%29 = load i64, i64* @ENOENT
%30 = icmp eq i64 %28, %29
%31 = load i32, i32* @stderr
%32 = tail call i8* @strerror(i64 %28)
br i1 %30, label %33, label %35
33: ; preds = %27
%34 = tail call i32 (i32, i8*, ...) @fprintf(i32 %31, i8* getelementptr inbounds ([63 x i8], [63 x i8]* @.str.2, i64 0, i64 0), i32 %0, i32 %0, i8* %32)
br label %43
35: ; preds = %27
%36 = tail call i32 (i32, i8*, ...) @fprintf(i32 %31, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.3, i64 0, i64 0), i8* %1, i8* %32)
%37 = load i64, i64* @errno
%38 = load i64, i64* @EACCES
%39 = icmp eq i64 %37, %38
br i1 %39, label %40, label %43
40: ; preds = %35
%41 = load i32, i32* @stderr
%42 = tail call i32 (i32, i8*, ...) @fprintf(i32 %41, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0))
br label %43
43: ; preds = %4, %33, %40, %35, %22
%44 = phi i32 [ %23, %33 ], [ %23, %40 ], [ %23, %35 ], [ %23, %22 ], [ %9, %4 ]
ret i32 %44
}