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

/* Variables and functions */
scalar_t__ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
struct dirent *readdir(int /*<<< orphan*/ *);
int main(int argc, char **argv) {
  char *d_to_open = ".";
  if (argc == 2) {
    d_to_open = argv[1];
  }
  if (argc > 2) {
    printf("please specify one directory name to open\n");
    exit(1);
  }
  DIR *dstream = opendir(d_to_open);
  if (dstream == NULL) {
    perror("opening directory failed");
    exit(2);
  }
  struct dirent *d;
  while (1) {
    errno = 0;
    d = readdir(dstream);
    if (d == NULL) {
      if (errno != 0) {
        perror("readdir error");
        exit(3);
      } else {
        break;
      }
    }
    printf("inode: %lld, name: %s\n", (long long)d->d_ino, d->d_name);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe93(%rip),%rax
mov    %rax,-0x18(%rbp)
cmpl   $0x2,-0x8(%rbp)
jne    1187 <main+0x37>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x18(%rbp)
cmpl   $0x2,-0x8(%rbp)
jle    11a9 <main+0x59>
lea    0xe6a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x18(%rbp),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11d7 <main+0x87>
lea    0xe65(%rip),%rdi
call   1060 <perror@plt>
mov    $0x2,%edi
call   1070 <exit@plt>
jmp    11dc <main+0x8c>
mov    0x2dfd(%rip),%rax
movq   $0x0,(%rax)
mov    -0x20(%rbp),%rdi
call   1050 <readdir@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    122e <main+0xde>
mov    0x2dd7(%rip),%rax
cmpq   $0x0,(%rax)
je     1229 <main+0xd9>
lea    0xe2c(%rip),%rdi
call   1060 <perror@plt>
mov    $0x3,%edi
call   1070 <exit@plt>
jmp    124f <main+0xff>
mov    -0x28(%rbp),%rax
mov    0x8(%rax),%rsi
mov    -0x28(%rbp),%rax
mov    (%rax),%edx
lea    0xe11(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    11dc <main+0x8c>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
cmp    $0x2,%edi
jne    1170 <main+0x10>
mov    0x8(%rsi),%rdi
jmp    117d <main+0x1d>
jg     120e <main+0xae>
lea    0xe83(%rip),%rdi
call   1040 <opendir@plt>
test   %rax,%rax
je     11e2 <main+0x82>
mov    %rax,%rbx
mov    0x2e4f(%rip),%r15
movq   $0x0,(%r15)
mov    %rax,%rdi
call   1060 <readdir@plt>
test   %rax,%rax
je     11d4 <main+0x74>
lea    0xe7d(%rip),%r14
nopl   0x0(%rax)
mov    0x8(%rax),%rsi
mov    (%rax),%edx
mov    %r14,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
movq   $0x0,(%r15)
mov    %rbx,%rdi
call   1060 <readdir@plt>
test   %rax,%rax
jne    11b0 <main+0x50>
cmpq   $0x0,(%r15)
jne    11f8 <main+0x98>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xe19(%rip),%rdi
call   1070 <perror@plt>
mov    $0x2,%edi
call   1080 <exit@plt>
lea    0xe1c(%rip),%rdi
call   1070 <perror@plt>
mov    $0x3,%edi
call   1080 <exit@plt>
lea    0xe2b(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
%struct.dirent = type { i32, i64 }
@.str = [2 x i8] c".\00"
@.str.1 = [43 x i8] c"please specify one directory name to open\0A\00"
@.str.2 = [25 x i8] c"opening directory failed\00"
@errno = global i64 0
@.str.3 = [14 x i8] c"readdir error\00"
@.str.4 = [23 x i8] c"inode: %lld, name: %s\0A\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 %struct.dirent*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8** %6
%9 = load i32, i32* %4
%10 = icmp eq i32 %9, 2
br i1 %10, label %11, label %15
11: ; preds = %2
%12 = load i8**, i8*** %5
%13 = getelementptr inbounds i8*, i8** %12, i64 1
%14 = load i8*, i8** %13
store i8* %14, i8** %6
br label %15
15: ; preds = %11, %2
%16 = load i32, i32* %4
%17 = icmp sgt i32 %16, 2
br i1 %17, label %18, label %21
18: ; preds = %15
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.1, i64 0, i64 0))
%20 = call i32 @exit(i32 1)
unreachable
21: ; preds = %15
%22 = load i8*, i8** %6
%23 = call i32* @opendir(i8* %22)
store i32* %23, i32** %7
%24 = load i32*, i32** %7
%25 = icmp eq i32* %24, null
br i1 %25, label %26, label %29
26: ; preds = %21
%27 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
%28 = call i32 @exit(i32 2)
unreachable
29: ; preds = %21
br label %30
30: ; preds = %29, %42
store i64 0, i64* @errno
%31 = load i32*, i32** %7
%32 = call %struct.dirent* @readdir(i32* %31)
store %struct.dirent* %32, %struct.dirent** %8
%33 = load %struct.dirent*, %struct.dirent** %8
%34 = icmp eq %struct.dirent* %33, null
br i1 %34, label %35, label %42
35: ; preds = %30
%36 = load i64, i64* @errno
%37 = icmp ne i64 %36, 0
br i1 %37, label %38, label %41
38: ; preds = %35
%39 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
%40 = call i32 @exit(i32 3)
unreachable
41: ; preds = %35
br label %50
42: ; preds = %30
%43 = load %struct.dirent*, %struct.dirent** %8
%44 = getelementptr inbounds %struct.dirent, %struct.dirent* %43, i32 0, i32 1
%45 = load i64, i64* %44
%46 = load %struct.dirent*, %struct.dirent** %8
%47 = getelementptr inbounds %struct.dirent, %struct.dirent* %46, i32 0, i32 0
%48 = load i32, i32* %47
%49 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0), i64 %45, i32 %48)
br label %30
50: ; preds = %41
ret i32 0
}

LLVM IR (-O2):
%struct.dirent = type { i32, i64 }
@.str = [2 x i8] c".\00"
@.str.2 = [25 x i8] c"opening directory failed\00"
@errno = global i64 0
@.str.3 = [14 x i8] c"readdir error\00"
@.str.4 = [23 x i8] c"inode: %lld, name: %s\0A\00"
@str = [42 x i8] c"please specify one directory name to open\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 2
br i1 %3, label %4, label %7
4: ; preds = %2
%5 = getelementptr inbounds i8*, i8** %1, i64 1
%6 = load i8*, i8** %5
br label %12
7: ; preds = %2
%8 = icmp sgt i32 %0, 2
br i1 %8, label %9, label %12
9: ; preds = %7
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([42 x i8], [42 x i8]* @str, i64 0, i64 0))
%11 = tail call i32 @exit(i32 1)
unreachable
12: ; preds = %4, %7
%13 = phi i8* [ %6, %4 ], [ getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), %7 ]
%14 = tail call i32* @opendir(i8* %13)
%15 = icmp eq i32* %14, null
br i1 %15, label %19, label %16
16: ; preds = %12
store i64 0, i64* @errno
%17 = tail call %struct.dirent* @readdir(i32* %14)
%18 = icmp eq %struct.dirent* %17, null
br i1 %18, label %22, label %28
19: ; preds = %12
%20 = tail call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
%21 = tail call i32 @exit(i32 2)
unreachable
22: ; preds = %28, %16
%23 = load i64, i64* @errno
%24 = icmp eq i64 %23, 0
br i1 %24, label %37, label %25
25: ; preds = %22
%26 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
%27 = tail call i32 @exit(i32 3)
unreachable
28: ; preds = %16, %28
%29 = phi %struct.dirent* [ %35, %28 ], [ %17, %16 ]
%30 = getelementptr inbounds %struct.dirent, %struct.dirent* %29, i64 0, i32 1
%31 = load i64, i64* %30
%32 = getelementptr inbounds %struct.dirent, %struct.dirent* %29, i64 0, i32 0
%33 = load i32, i32* %32
%34 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0), i64 %31, i32 %33)
store i64 0, i64* @errno
%35 = tail call %struct.dirent* @readdir(i32* %14)
%36 = icmp eq %struct.dirent* %35, null
br i1 %36, label %22, label %28
37: ; preds = %22
ret i32 0
}