#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 const *d_name;
};
typedef int /*<<< orphan*/ DIR;

/* Variables and functions */
int ENOENT;
scalar_t__ check_prefix(char const *, char *);
int closedir(int /*<<< orphan*/ *);
int errno;
int getopt(int, char **, char *);
int list_device(char const *);
int /*<<< orphan*/ *opendir(char *);
char *optarg;
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ print_usage();
struct dirent *readdir(int /*<<< orphan*/ *);
int main(int argc, char **argv) {
  const char *device_name = NULL;
  int ret;
  int c;
  while ((c = getopt(argc, argv, "n:")) != -1) {
    switch (c) {
    case 'n':
      device_name = optarg;
      break;
    case '?':
      print_usage();
      return -1;
    }
  }
  if (device_name) {
    ret = list_device(device_name);
  } else {
    const struct dirent *ent;
    DIR *dp;
    dp = opendir("/dev");
    if (!dp) {
      ret = -errno;
      goto error_out;
    }
    ret = -ENOENT;
    while (ent = readdir(dp), ent) {
      if (check_prefix(ent->d_name, "gpiochip")) {
        ret = list_device(ent->d_name);
        if (ret) {
          break;
        }
      }
    }
    ret = 0;
    if (closedir(dp) == -1) {
      perror("scanning devices: Failed to close directory");
      ret = -errno;
    }
  }
error_out:
  return ret;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
lea    0xe54(%rip),%rdx
call   1090 <getopt@plt>
mov    %eax,-0x20(%rbp)
cmp    $0xffffffff,%eax
je     120d <main+0x8d>
mov    -0x20(%rbp),%eax
mov    %eax,-0x34(%rbp)
sub    $0x3f,%eax
je     11f5 <main+0x75>
jmp    11d1 <main+0x51>
mov    -0x34(%rbp),%eax
sub    $0x6e,%eax
jne    1208 <main+0x88>
jmp    11e2 <main+0x62>
mov    0x2e0f(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x18(%rbp)
jmp    1208 <main+0x88>
mov    $0x0,%al
call   10a0 <print_usage@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1305 <main+0x185>
jmp    119e <main+0x1e>
cmpq   $0x0,-0x18(%rbp)
je     1229 <main+0xa9>
mov    -0x18(%rbp),%rdi
call   1070 <list_device@plt>
mov    %eax,-0x1c(%rbp)
jmp    12fa <main+0x17a>
lea    0xdd3(%rip),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1257 <main+0xd7>
xor    %eax,%eax
mov    0x2d8b(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x1c(%rbp)
jmp    12ff <main+0x17f>
xor    %eax,%eax
mov    0x2d68(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x30(%rbp),%rdi
call   1060 <readdir@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     12c2 <main+0x142>
mov    -0x28(%rbp),%rax
mov    (%rax),%rdi
lea    0xd7d(%rip),%rsi
call   1050 <check_prefix@plt>
cmp    $0x0,%rax
je     12bd <main+0x13d>
mov    -0x28(%rbp),%rax
mov    (%rax),%rdi
call   1070 <list_device@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     12b8 <main+0x138>
jmp    12c2 <main+0x142>
jmp    12bd <main+0x13d>
jmp    1265 <main+0xe5>
movl   $0x0,-0x1c(%rbp)
mov    -0x30(%rbp),%rdi
call   1040 <closedir@plt>
cmp    $0xffffffff,%eax
jne    12f5 <main+0x175>
lea    0xd2f(%rip),%rdi
call   1080 <perror@plt>
xor    %eax,%eax
mov    0x2ce8(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x1c(%rbp)
jmp    12fa <main+0x17a>
jmp    12ff <main+0x17f>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rsi,%rbx
mov    %edi,%ebp
xor    %r14d,%r14d
lea    0xe69(%rip),%r15
mov    0x2e5a(%rip),%r12
xchg   %ax,%ax
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r15,%rdx
call   1090 <getopt@plt>
cmp    $0xffffffff,%eax
je     11c2 <main+0x42>
cmp    $0x3f,%eax
je     11d7 <main+0x57>
cmp    $0x6e,%eax
jne    11a0 <main+0x20>
mov    (%r12),%r14
jmp    11a0 <main+0x20>
test   %r14,%r14
je     11ec <main+0x6c>
mov    %r14,%rdi
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1070 <list_device@plt>
xor    %eax,%eax
call   10a0 <print_usage@plt>
mov    $0xffffffff,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe10(%rip),%rdi
call   1030 <opendir@plt>
test   %rax,%rax
je     126b <main+0xeb>
mov    %rax,%rbx
mov    %rax,%rdi
call   1060 <readdir@plt>
test   %rax,%rax
je     124e <main+0xce>
mov    %rax,%rbp
lea    0xdf1(%rip),%r14
jmp    1230 <main+0xb0>
nopl   0x0(%rax)
mov    %rbx,%rdi
call   1060 <readdir@plt>
mov    %rax,%rbp
test   %rax,%rax
je     124e <main+0xce>
mov    0x0(%rbp),%rdi
mov    %r14,%rsi
call   1050 <check_prefix@plt>
test   %rax,%rax
je     1220 <main+0xa0>
mov    0x0(%rbp),%rdi
call   1070 <list_device@plt>
test   %eax,%eax
je     1220 <main+0xa0>
mov    %rbx,%rdi
call   1040 <closedir@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0xffffffff,%ecx
jne    11e3 <main+0x63>
lea    0xdab(%rip),%rdi
call   1080 <perror@plt>
mov    0x2d66(%rip),%rcx
xor    %eax,%eax
sub    (%rcx),%eax
jmp    11e3 <main+0x63>

LLVM IR (-O0):
%struct.dirent = type { i8* }
@.str = [3 x i8] c"n:\00"
@optarg = global i8* null
@.str.1 = [5 x i8] c"/dev\00"
@errno = global i32 0
@ENOENT = global i32 0
@.str.2 = [9 x i8] c"gpiochip\00"
@.str.3 = [44 x i8] c"scanning devices: Failed to close directory\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca %struct.dirent*
%10 = alloca i32*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i8* null, i8** %6
br label %11
11: ; preds = %22, %2
%12 = load i32, i32* %4
%13 = load i8**, i8*** %5
%14 = call i32 @getopt(i32 %12, i8** %13, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32 %14, i32* %8
%15 = icmp ne i32 %14, -1
br i1 %15, label %16, label %23
16: ; preds = %11
%17 = load i32, i32* %8
switch i32 %17, label %22 [
i32 110, label %18
i32 63, label %20
]
18: ; preds = %16
%19 = load i8*, i8** @optarg
store i8* %19, i8** %6
br label %22
20: ; preds = %16
%21 = call i32 (...) @print_usage()
store i32 -1, i32* %3
br label %72
22: ; preds = %16, %18
br label %11
23: ; preds = %11
%24 = load i8*, i8** %6
%25 = icmp ne i8* %24, null
br i1 %25, label %26, label %29
26: ; preds = %23
%27 = load i8*, i8** %6
%28 = call i32 @list_device(i8* %27)
store i32 %28, i32* %7
br label %69
29: ; preds = %23
%30 = call i32* @opendir(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
store i32* %30, i32** %10
%31 = load i32*, i32** %10
%32 = icmp ne i32* %31, null
br i1 %32, label %36, label %33
33: ; preds = %29
%34 = load i32, i32* @errno
%35 = sub nsw i32 0, %34
store i32 %35, i32* %7
br label %70
36: ; preds = %29
%37 = load i32, i32* @ENOENT
%38 = sub nsw i32 0, %37
store i32 %38, i32* %7
br label %39
39: ; preds = %59, %36
%40 = load i32*, i32** %10
%41 = call %struct.dirent* @readdir(i32* %40)
store %struct.dirent* %41, %struct.dirent** %9
%42 = load %struct.dirent*, %struct.dirent** %9
%43 = icmp ne %struct.dirent* %42, null
br i1 %43, label %44, label %60
44: ; preds = %39
%45 = load %struct.dirent*, %struct.dirent** %9
%46 = getelementptr inbounds %struct.dirent, %struct.dirent* %45, i32 0, i32 0
%47 = load i8*, i8** %46
%48 = call i64 @check_prefix(i8* %47, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
%49 = icmp ne i64 %48, 0
br i1 %49, label %50, label %59
50: ; preds = %44
%51 = load %struct.dirent*, %struct.dirent** %9
%52 = getelementptr inbounds %struct.dirent, %struct.dirent* %51, i32 0, i32 0
%53 = load i8*, i8** %52
%54 = call i32 @list_device(i8* %53)
store i32 %54, i32* %7
%55 = load i32, i32* %7
%56 = icmp ne i32 %55, 0
br i1 %56, label %57, label %58
57: ; preds = %50
br label %60
58: ; preds = %50
br label %59
59: ; preds = %58, %44
br label %39
60: ; preds = %57, %39
store i32 0, i32* %7
%61 = load i32*, i32** %10
%62 = call i32 @closedir(i32* %61)
%63 = icmp eq i32 %62, -1
br i1 %63, label %64, label %68
64: ; preds = %60
%65 = call i32 @perror(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.3, i64 0, i64 0))
%66 = load i32, i32* @errno
%67 = sub nsw i32 0, %66
store i32 %67, i32* %7
br label %68
68: ; preds = %64, %60
br label %69
69: ; preds = %68, %26
br label %70
70: ; preds = %69, %33
%71 = load i32, i32* %7
store i32 %71, i32* %3
br label %72
72: ; preds = %70, %20
%73 = load i32, i32* %3
ret i32 %73
}

LLVM IR (-O2):
%struct.dirent = type { i8* }
@.str = [3 x i8] c"n:\00"
@optarg = global i8* null
@.str.1 = [5 x i8] c"/dev\00"
@errno = global i32 0
@ENOENT = global i32 0
@.str.2 = [9 x i8] c"gpiochip\00"
@.str.3 = [44 x i8] c"scanning devices: Failed to close directory\00"
define i32 @main(i32 %0, i8** %1) {
br label %3
3: ; preds = %7, %2
%4 = phi i8* [ %8, %7 ], [ null, %2 ]
br label %5
5: ; preds = %3, %5
%6 = tail call i32 @getopt(i32 %0, i8** %1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
switch i32 %6, label %5 [
i32 -1, label %11
i32 110, label %7
i32 63, label %9
]
7: ; preds = %5
%8 = load i8*, i8** @optarg
br label %3
9: ; preds = %5
%10 = tail call i32 (...) @print_usage()
br label %44
11: ; preds = %5
%12 = icmp eq i8* %4, null
br i1 %12, label %15, label %13
13: ; preds = %11
%14 = tail call i32 @list_device(i8* %4)
br label %44
15: ; preds = %11
%16 = tail call i32* @opendir(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%17 = icmp eq i32* %16, null
br i1 %17, label %21, label %18
18: ; preds = %15
%19 = tail call %struct.dirent* @readdir(i32* %16)
%20 = icmp eq %struct.dirent* %19, null
br i1 %20, label %37, label %24
21: ; preds = %15
%22 = load i32, i32* @errno
%23 = sub nsw i32 0, %22
br label %44
24: ; preds = %18, %34
%25 = phi %struct.dirent* [ %35, %34 ], [ %19, %18 ]
%26 = getelementptr inbounds %struct.dirent, %struct.dirent* %25, i64 0, i32 0
%27 = load i8*, i8** %26
%28 = tail call i64 @check_prefix(i8* %27, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
%29 = icmp eq i64 %28, 0
br i1 %29, label %34, label %30
30: ; preds = %24
%31 = load i8*, i8** %26
%32 = tail call i32 @list_device(i8* %31)
%33 = icmp eq i32 %32, 0
br i1 %33, label %34, label %37
34: ; preds = %30, %24
%35 = tail call %struct.dirent* @readdir(i32* %16)
%36 = icmp eq %struct.dirent* %35, null
br i1 %36, label %37, label %24
37: ; preds = %34, %30, %18
%38 = tail call i32 @closedir(i32* %16)
%39 = icmp eq i32 %38, -1
br i1 %39, label %40, label %44
40: ; preds = %37
%41 = tail call i32 @perror(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.3, i64 0, i64 0))
%42 = load i32, i32* @errno
%43 = sub nsw i32 0, %42
br label %44
44: ; preds = %13, %37, %40, %21, %9
%45 = phi i32 [ -1, %9 ], [ %14, %13 ], [ %23, %21 ], [ %43, %40 ], [ 0, %37 ]
ret i32 %45
}