#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;
int /*<<< orphan*/ O_RDWR;
scalar_t__ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ sprintf(char *, char *, int const);
int /*<<< orphan*/ stderr;
char *strerror(scalar_t__);
int open_i2c_dev(const int i2cbus, char *filename, const int quiet) {
  int file;
  sprintf(filename, "/dev/i2c/%d", i2cbus);
  file = open(filename, O_RDWR);
  if (file < 0 && errno == ENOENT) {
    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    %edx,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x4(%rbp),%edx
lea    0xea0(%rip),%rsi
call   1050 <sprintf@plt>
mov    -0x10(%rbp),%rdi
mov    0x2e78(%rip),%rax
mov    (%rax),%esi
call   1040 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    11c6 <open_i2c_dev+0x86>
mov    0x2e3d(%rip),%rax
mov    (%rax),%rax
mov    0x2e23(%rip),%rcx
cmp    (%rcx),%rax
jne    11c6 <open_i2c_dev+0x86>
mov    -0x10(%rbp),%rdi
mov    -0x4(%rbp),%edx
lea    0xe60(%rip),%rsi
call   1050 <sprintf@plt>
mov    -0x10(%rbp),%rdi
mov    0x2e2c(%rip),%rax
mov    (%rax),%esi
call   1040 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    12b0 <open_i2c_dev+0x170>
cmpl   $0x0,-0x14(%rbp)
jne    12b0 <open_i2c_dev+0x170>
mov    0x2de7(%rip),%rax
mov    (%rax),%rax
mov    0x2dcd(%rip),%rcx
cmp    (%rcx),%rax
jne    123a <open_i2c_dev+0xfa>
mov    0x2dd5(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2da5(%rip),%rax
mov    (%rax),%rdi
call   1060 <strerror@plt>
mov    -0x24(%rbp),%edi
mov    -0x20(%rbp),%edx
mov    -0x1c(%rbp),%ecx
mov    %rax,%r8
lea    0xdea(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
jmp    12ab <open_i2c_dev+0x16b>
mov    0x2d8f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    0x2d73(%rip),%rax
mov    (%rax),%rdi
call   1060 <strerror@plt>
mov    -0x34(%rbp),%edi
mov    -0x30(%rbp),%rdx
mov    %rax,%rcx
lea    0xde9(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
mov    0x2d4c(%rip),%rax
mov    (%rax),%rax
mov    0x2d5a(%rip),%rcx
cmp    (%rcx),%rax
jne    12a6 <open_i2c_dev+0x166>
mov    0x2d3a(%rip),%rax
mov    (%rax),%edi
lea    0xddd(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
jmp    12ab <open_i2c_dev+0x16b>
jmp    12b0 <open_i2c_dev+0x170>
mov    -0x18(%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    %edx,%r15d
mov    %rsi,%rbp
mov    %edi,%r14d
lea    0xea5(%rip),%rsi
mov    %rbp,%rdi
mov    %r14d,%edx
call   1050 <sprintf@plt>
mov    0x2e7b(%rip),%r12
mov    (%r12),%esi
mov    %rbp,%rdi
call   1040 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
js     1190 <open_i2c_dev+0x50>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2e31(%rip),%rcx
mov    (%rcx),%rax
mov    0x2e17(%rip),%r13
cmp    0x0(%r13),%rax
jne    11cd <open_i2c_dev+0x8d>
lea    0xe5e(%rip),%rsi
mov    %rbp,%rdi
mov    %r14d,%edx
mov    %rcx,%rbx
call   1050 <sprintf@plt>
mov    (%r12),%esi
mov    %rbp,%rdi
call   1040 <open@plt>
mov    %rbx,%rcx
mov    %eax,%ebx
test   %r15d,%r15d
jne    117f <open_i2c_dev+0x3f>
test   %ebx,%ebx
jns    117f <open_i2c_dev+0x3f>
mov    (%rcx),%r12
mov    0x0(%r13),%r13
mov    0x2dec(%rip),%rax
mov    (%rax),%r15d
mov    %r12,%rdi
call   1060 <strerror@plt>
cmp    %r13,%r12
jne    1213 <open_i2c_dev+0xd3>
lea    0xe1d(%rip),%rsi
mov    %r15d,%edi
mov    %r14d,%edx
mov    %r14d,%ecx
mov    %rax,%r8
xor    %eax,%eax
call   1030 <fprintf@plt>
jmp    117f <open_i2c_dev+0x3f>
mov    0x2db6(%rip),%r14
lea    0xe36(%rip),%rsi
mov    %r15d,%edi
mov    %rbp,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   1030 <fprintf@plt>
mov    0x2d90(%rip),%rax
mov    (%rax),%rax
mov    0x2d9e(%rip),%rcx
cmp    (%rcx),%rax
jne    117f <open_i2c_dev+0x3f>
mov    (%r14),%edi
lea    0xe27(%rip),%rsi
xor    %eax,%eax
call   1030 <fprintf@plt>
jmp    117f <open_i2c_dev+0x3f>

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
@.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, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
store i32 %2, i32* %6
%8 = load i8*, i8** %5
%9 = load i32, i32* %4
%10 = call i32 @sprintf(i8* %8, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %9)
%11 = load i8*, i8** %5
%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 %27
16: ; preds = %3
%17 = load i64, i64* @errno
%18 = load i64, i64* @ENOENT
%19 = icmp eq i64 %17, %18
br i1 %19, label %20, label %27
20: ; preds = %16
%21 = load i8*, i8** %5
%22 = load i32, i32* %4
%23 = call i32 @sprintf(i8* %21, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %22)
%24 = load i8*, i8** %5
%25 = load i32, i32* @O_RDWR
%26 = call i32 @open(i8* %24, i32 %25)
store i32 %26, i32* %7
br label %27
27: ; preds = %20, %16, %3
%28 = load i32, i32* %7
%29 = icmp slt i32 %28, 0
br i1 %29, label %30, label %58
30: ; preds = %27
%31 = load i32, i32* %6
%32 = icmp ne i32 %31, 0
br i1 %32, label %58, label %33
33: ; preds = %30
%34 = load i64, i64* @errno
%35 = load i64, i64* @ENOENT
%36 = icmp eq i64 %34, %35
br i1 %36, label %37, label %44
37: ; preds = %33
%38 = load i32, i32* @stderr
%39 = load i32, i32* %4
%40 = load i32, i32* %4
%41 = load i64, i64* @ENOENT
%42 = call i8* @strerror(i64 %41)
%43 = call i32 (i32, i8*, ...) @fprintf(i32 %38, i8* getelementptr inbounds ([63 x i8], [63 x i8]* @.str.2, i64 0, i64 0), i32 %39, i32 %40, i8* %42)
br label %57
44: ; preds = %33
%45 = load i32, i32* @stderr
%46 = load i8*, i8** %5
%47 = load i64, i64* @errno
%48 = call i8* @strerror(i64 %47)
%49 = call i32 (i32, i8*, ...) @fprintf(i32 %45, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.3, i64 0, i64 0), i8* %46, i8* %48)
%50 = load i64, i64* @errno
%51 = load i64, i64* @EACCES
%52 = icmp eq i64 %50, %51
br i1 %52, label %53, label %56
53: ; preds = %44
%54 = load i32, i32* @stderr
%55 = call i32 (i32, i8*, ...) @fprintf(i32 %54, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0))
br label %56
56: ; preds = %53, %44
br label %57
57: ; preds = %56, %37
br label %58
58: ; preds = %57, %30, %27
%59 = load i32, i32* %7
ret i32 %59
}

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
@.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, i32 %2) {
%4 = tail call i32 @sprintf(i8* dereferenceable(1) %1, i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %0)
%5 = load i32, i32* @O_RDWR
%6 = tail call i32 @open(i8* %1, i32 %5)
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %37
8: ; preds = %3
%9 = load i64, i64* @errno
%10 = load i64, i64* @ENOENT
%11 = icmp eq i64 %9, %10
br i1 %11, label %12, label %16
12: ; preds = %8
%13 = 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)
%14 = load i32, i32* @O_RDWR
%15 = tail call i32 @open(i8* %1, i32 %14)
br label %16
16: ; preds = %12, %8
%17 = phi i32 [ %15, %12 ], [ %6, %8 ]
%18 = icmp sgt i32 %17, -1
%19 = icmp ne i32 %2, 0
%20 = or i1 %19, %18
br i1 %20, label %37, label %21
21: ; preds = %16
%22 = load i64, i64* @errno
%23 = load i64, i64* @ENOENT
%24 = icmp eq i64 %22, %23
%25 = load i32, i32* @stderr
%26 = tail call i8* @strerror(i64 %22)
br i1 %24, label %27, label %29
27: ; preds = %21
%28 = tail call i32 (i32, i8*, ...) @fprintf(i32 %25, i8* getelementptr inbounds ([63 x i8], [63 x i8]* @.str.2, i64 0, i64 0), i32 %0, i32 %0, i8* %26)
br label %37
29: ; preds = %21
%30 = tail call i32 (i32, i8*, ...) @fprintf(i32 %25, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.3, i64 0, i64 0), i8* %1, i8* %26)
%31 = load i64, i64* @errno
%32 = load i64, i64* @EACCES
%33 = icmp eq i64 %31, %32
br i1 %33, label %34, label %37
34: ; preds = %29
%35 = load i32, i32* @stderr
%36 = tail call i32 (i32, i8*, ...) @fprintf(i32 %35, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0))
br label %37
37: ; preds = %3, %27, %34, %29, %16
%38 = phi i32 [ %17, %27 ], [ %17, %34 ], [ %17, %29 ], [ %17, %16 ], [ %6, %3 ]
ret i32 %38
}