#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 */
struct dirent {
  char *d_name;
};
typedef int /*<<< orphan*/ DIR;

/* Variables and functions */
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ die_errno(char *, char const *);
scalar_t__ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *);
scalar_t__ name_file_matches(char const *, char *, char const *);
int open_i2c_dev(char *);
int /*<<< orphan*/ *opendir(char const *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
int open_i2c_bus(const char *name) {
  const char *dir = "/sys/class/i2c-dev";
  struct dirent *de;
  DIR *dp = opendir(dir);
  if (!dp)
    die_errno("opening directory %s", dir);
  for (;;) {
    errno = 0;
    de = readdir(dp);
    if (!de) {
      if (errno)
        die_errno("reading directory %s", dir);
      break;
    }
    if (de->d_name[0] == '.')
      continue;
    if (name_file_matches(dir, de->d_name, name)) {
      int fd = open_i2c_dev(de->d_name);
      closedir(dp);
      return fd;
    }
  }
  fprintf(stderr, "could not find i2c bus %s\n", name);
  exit(1);
}


x86 assembly (clang, -O0):
<open_i2c_bus>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
lea    0xe6d(%rip),%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11bf <open_i2c_bus+0x3f>
mov    -0x10(%rbp),%rsi
lea    0xe59(%rip),%rdi
call   1080 <die_errno@plt>
jmp    11c4 <open_i2c_bus+0x44>
mov    0x2e0d(%rip),%rax
movq   $0x0,(%rax)
mov    -0x20(%rbp),%rdi
call   1070 <readdir@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1210 <open_i2c_bus+0x90>
mov    0x2de7(%rip),%rax
cmpq   $0x0,(%rax)
je     120b <open_i2c_bus+0x8b>
mov    -0x10(%rbp),%rsi
lea    0xe22(%rip),%rdi
call   1080 <die_errno@plt>
jmp    126c <open_i2c_bus+0xec>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
movsbl (%rax),%eax
cmp    $0x2e,%eax
jne    1228 <open_i2c_bus+0xa8>
jmp    11c4 <open_i2c_bus+0x44>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rax
mov    (%rax),%rsi
mov    -0x8(%rbp),%rdx
call   1040 <name_file_matches@plt>
cmp    $0x0,%rax
je     1267 <open_i2c_bus+0xe7>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
call   1090 <open_i2c_dev@plt>
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%rdi
call   1050 <closedir@plt>
mov    -0x24(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret
jmp    11c4 <open_i2c_bus+0x44>
mov    0x2d6d(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xdbd(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>

x86 assembly (clang, -O2):
<open_i2c_bus>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rdi,%r14
lea    0xe6d(%rip),%rdi
call   1030 <opendir@plt>
mov    %rax,%r12
test   %rax,%rax
jne    11b3 <open_i2c_bus+0x33>
lea    0xe6c(%rip),%rdi
lea    0xe52(%rip),%rsi
call   1080 <die_errno@plt>
mov    0x2e1e(%rip),%r13
movq   $0x0,0x0(%r13)
mov    %r12,%rdi
call   1070 <readdir@plt>
test   %rax,%rax
je     122e <open_i2c_bus+0xae>
mov    %rax,%rbx
lea    0xe27(%rip),%r15
jmp    11f8 <open_i2c_bus+0x78>
nopl   0x0(%rax,%rax,1)
movq   $0x0,0x0(%r13)
mov    %r12,%rdi
call   1070 <readdir@plt>
mov    %rax,%rbx
test   %rax,%rax
je     122e <open_i2c_bus+0xae>
mov    (%rbx),%rsi
cmpb   $0x2e,(%rsi)
je     11e0 <open_i2c_bus+0x60>
mov    %r15,%rdi
mov    %r14,%rdx
call   1040 <name_file_matches@plt>
test   %rax,%rax
je     11e0 <open_i2c_bus+0x60>
mov    (%rbx),%rdi
call   1090 <open_i2c_dev@plt>
mov    %eax,%ebx
mov    %r12,%rdi
call   1050 <closedir@plt>
mov    %ebx,%eax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret
cmpq   $0x0,0x0(%r13)
je     1248 <open_i2c_bus+0xc8>
lea    0xdec(%rip),%rdi
lea    0xdbd(%rip),%rsi
call   1080 <die_errno@plt>
mov    0x2d91(%rip),%rax
mov    (%rax),%edi
lea    0xde5(%rip),%rsi
mov    %r14,%rdx
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
%struct.dirent = type { i8* }
@.str = [19 x i8] c"/sys/class/i2c-dev\00"
@.str.1 = [21 x i8] c"opening directory %s\00"
@errno = global i64 0
@.str.2 = [21 x i8] c"reading directory %s\00"
@stderr = global i32 0
@.str.3 = [27 x i8] c"could not find i2c bus %s\0A\00"
define i32 @open_i2c_bus(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca %struct.dirent*
%5 = alloca i32*
%6 = alloca i32
store i8* %0, i8** %2
store i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8** %3
%7 = load i8*, i8** %3
%8 = call i32* @opendir(i8* %7)
store i32* %8, i32** %5
%9 = load i32*, i32** %5
%10 = icmp ne i32* %9, null
br i1 %10, label %14, label %11
11: ; preds = %1
%12 = load i8*, i8** %3
%13 = call i32 @die_errno(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i8* %12)
br label %14
14: ; preds = %11, %1
br label %15
15: ; preds = %52, %35, %14
store i64 0, i64* @errno
%16 = load i32*, i32** %5
%17 = call %struct.dirent* @readdir(i32* %16)
store %struct.dirent* %17, %struct.dirent** %4
%18 = load %struct.dirent*, %struct.dirent** %4
%19 = icmp ne %struct.dirent* %18, null
br i1 %19, label %27, label %20
20: ; preds = %15
%21 = load i64, i64* @errno
%22 = icmp ne i64 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %20
%24 = load i8*, i8** %3
%25 = call i32 @die_errno(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i8* %24)
br label %26
26: ; preds = %23, %20
br label %53
27: ; preds = %15
%28 = load %struct.dirent*, %struct.dirent** %4
%29 = getelementptr inbounds %struct.dirent, %struct.dirent* %28, i32 0, i32 0
%30 = load i8*, i8** %29
%31 = getelementptr inbounds i8, i8* %30, i64 0
%32 = load i8, i8* %31
%33 = sext i8 %32 to i32
%34 = icmp eq i32 %33, 46
br i1 %34, label %35, label %36
35: ; preds = %27
br label %15
36: ; preds = %27
%37 = load i8*, i8** %3
%38 = load %struct.dirent*, %struct.dirent** %4
%39 = getelementptr inbounds %struct.dirent, %struct.dirent* %38, i32 0, i32 0
%40 = load i8*, i8** %39
%41 = load i8*, i8** %2
%42 = call i64 @name_file_matches(i8* %37, i8* %40, i8* %41)
%43 = icmp ne i64 %42, 0
br i1 %43, label %44, label %52
44: ; preds = %36
%45 = load %struct.dirent*, %struct.dirent** %4
%46 = getelementptr inbounds %struct.dirent, %struct.dirent* %45, i32 0, i32 0
%47 = load i8*, i8** %46
%48 = call i32 @open_i2c_dev(i8* %47)
store i32 %48, i32* %6
%49 = load i32*, i32** %5
%50 = call i32 @closedir(i32* %49)
%51 = load i32, i32* %6
ret i32 %51
52: ; preds = %36
br label %15
53: ; preds = %26
%54 = load i32, i32* @stderr
%55 = load i8*, i8** %2
%56 = call i32 @fprintf(i32 %54, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0), i8* %55)
%57 = call i32 @exit(i32 1)
unreachable
}

LLVM IR (-O2):
%struct.dirent = type { i8* }
@.str = [19 x i8] c"/sys/class/i2c-dev\00"
@.str.1 = [21 x i8] c"opening directory %s\00"
@errno = global i64 0
@.str.2 = [21 x i8] c"reading directory %s\00"
@stderr = global i32 0
@.str.3 = [27 x i8] c"could not find i2c bus %s\0A\00"
define i32 @open_i2c_bus(i8* %0) {
%2 = tail call i32* @opendir(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
%3 = icmp eq i32* %2, null
br i1 %3, label %4, label %6
4: ; preds = %1
%5 = tail call i32 @die_errno(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
br label %6
6: ; preds = %4, %1
store i64 0, i64* @errno
%7 = tail call %struct.dirent* @readdir(i32* %2)
%8 = icmp eq %struct.dirent* %7, null
br i1 %8, label %9, label %14
9: ; preds = %20, %6
%10 = load i64, i64* @errno
%11 = icmp eq i64 %10, 0
br i1 %11, label %31, label %12
12: ; preds = %9
%13 = tail call i32 @die_errno(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
br label %31
14: ; preds = %6, %20
%15 = phi %struct.dirent* [ %21, %20 ], [ %7, %6 ]
%16 = getelementptr inbounds %struct.dirent, %struct.dirent* %15, i64 0, i32 0
%17 = load i8*, i8** %16
%18 = load i8, i8* %17
%19 = icmp eq i8 %18, 46
br i1 %19, label %20, label %23
20: ; preds = %14, %23
store i64 0, i64* @errno
%21 = tail call %struct.dirent* @readdir(i32* %2)
%22 = icmp eq %struct.dirent* %21, null
br i1 %22, label %9, label %14
23: ; preds = %14
%24 = tail call i64 @name_file_matches(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* %17, i8* %0)
%25 = icmp eq i64 %24, 0
br i1 %25, label %20, label %26
26: ; preds = %23
%27 = getelementptr inbounds %struct.dirent, %struct.dirent* %15, i64 0, i32 0
%28 = load i8*, i8** %27
%29 = tail call i32 @open_i2c_dev(i8* %28)
%30 = tail call i32 @closedir(i32* %2)
ret i32 %29
31: ; preds = %9, %12
%32 = load i32, i32* @stderr
%33 = tail call i32 @fprintf(i32 %32, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0), i8* %0)
%34 = tail call i32 @exit(i32 1)
unreachable
}