#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 */
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int readdir_r(int /*<<< orphan*/ *, struct dirent *, struct dirent **);
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;
  struct dirent *result;
  for (;;) {
    int ret = readdir_r(dstream, &d, &result);
    if (ret != 0) {
      perror("readdir_r error");
      exit(3);
    }
    if (!result) {
      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    $0x40,%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   1050 <printf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x18(%rbp),%rdi
call   1040 <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    -0x20(%rbp),%rdi
lea    -0x30(%rbp),%rsi
lea    -0x38(%rbp),%rdx
call   1030 <readdir_r@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
je     1210 <main+0xc0>
lea    0xe45(%rip),%rdi
call   1060 <perror@plt>
mov    $0x3,%edi
call   1070 <exit@plt>
cmpq   $0x0,-0x38(%rbp)
jne    1220 <main+0xd0>
jmp    123a <main+0xea>
mov    -0x28(%rbp),%rsi
mov    -0x30(%rbp),%edx
lea    0xe28(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    11dc <main+0x8c>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x18,%rsp
cmp    $0x2,%edi
jne    1176 <main+0x16>
mov    0x8(%rsi),%rdi
jmp    1183 <main+0x23>
jg     1226 <main+0xc6>
lea    0xe7d(%rip),%rdi
call   1050 <opendir@plt>
test   %rax,%rax
je     1210 <main+0xb0>
mov    %rax,%rbx
lea    0x8(%rsp),%rsi
mov    %rsp,%rdx
mov    %rax,%rdi
call   1040 <readdir_r@plt>
test   %eax,%eax
jne    11ec <main+0x8c>
lea    0xe7c(%rip),%r14
lea    0x8(%rsp),%r15
mov    %rsp,%r12
nopw   0x0(%rax,%rax,1)
cmpq   $0x0,(%rsp)
je     1202 <main+0xa2>
mov    0x10(%rsp),%rsi
mov    0x8(%rsp),%edx
mov    %r14,%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %r12,%rdx
call   1040 <readdir_r@plt>
test   %eax,%eax
je     11c0 <main+0x60>
lea    0xe28(%rip),%rdi
call   1070 <perror@plt>
mov    $0x3,%edi
call   1080 <exit@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
lea    0xdeb(%rip),%rdi
call   1070 <perror@plt>
mov    $0x2,%edi
call   1080 <exit@plt>
lea    0xe15(%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"
@.str.3 = [16 x i8] c"readdir_r 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
%9 = alloca %struct.dirent*
%10 = alloca i32
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
%11 = load i32, i32* %4
%12 = icmp eq i32 %11, 2
br i1 %12, label %13, label %17
13: ; preds = %2
%14 = load i8**, i8*** %5
%15 = getelementptr inbounds i8*, i8** %14, i64 1
%16 = load i8*, i8** %15
store i8* %16, i8** %6
br label %17
17: ; preds = %13, %2
%18 = load i32, i32* %4
%19 = icmp sgt i32 %18, 2
br i1 %19, label %20, label %23
20: ; preds = %17
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.1, i64 0, i64 0))
%22 = call i32 @exit(i32 1)
unreachable
23: ; preds = %17
%24 = load i8*, i8** %6
%25 = call i32* @opendir(i8* %24)
store i32* %25, i32** %7
%26 = load i32*, i32** %7
%27 = icmp eq i32* %26, null
br i1 %27, label %28, label %31
28: ; preds = %23
%29 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
%30 = call i32 @exit(i32 2)
unreachable
31: ; preds = %23
br label %32
32: ; preds = %44, %31
%33 = load i32*, i32** %7
%34 = call i32 @readdir_r(i32* %33, %struct.dirent* %8, %struct.dirent** %9)
store i32 %34, i32* %10
%35 = load i32, i32* %10
%36 = icmp ne i32 %35, 0
br i1 %36, label %37, label %40
37: ; preds = %32
%38 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
%39 = call i32 @exit(i32 3)
unreachable
40: ; preds = %32
%41 = load %struct.dirent*, %struct.dirent** %9
%42 = icmp ne %struct.dirent* %41, null
br i1 %42, label %44, label %43
43: ; preds = %40
br label %50
44: ; preds = %40
%45 = getelementptr inbounds %struct.dirent, %struct.dirent* %8, i32 0, i32 1
%46 = load i64, i64* %45
%47 = getelementptr inbounds %struct.dirent, %struct.dirent* %8, 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 %46, i32 %48)
br label %32
50: ; preds = %43
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"
@.str.3 = [16 x i8] c"readdir_r 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 = alloca %struct.dirent
%4 = alloca %struct.dirent*
%5 = icmp eq i32 %0, 2
br i1 %5, label %6, label %9
6: ; preds = %2
%7 = getelementptr inbounds i8*, i8** %1, i64 1
%8 = load i8*, i8** %7
br label %14
9: ; preds = %2
%10 = icmp sgt i32 %0, 2
br i1 %10, label %11, label %14
11: ; preds = %9
%12 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([42 x i8], [42 x i8]* @str, i64 0, i64 0))
%13 = tail call i32 @exit(i32 1)
unreachable
14: ; preds = %6, %9
%15 = phi i8* [ %8, %6 ], [ getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), %9 ]
%16 = tail call i32* @opendir(i8* %15)
%17 = icmp eq i32* %16, null
br i1 %17, label %18, label %21
18: ; preds = %14
%19 = tail call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
%20 = tail call i32 @exit(i32 2)
unreachable
21: ; preds = %14
%22 = bitcast %struct.dirent* %3 to i8*
%23 = bitcast %struct.dirent** %4 to i8*
%24 = call i32 @readdir_r(i32* %16, %struct.dirent* %3, %struct.dirent** %4)
%25 = icmp eq i32 %24, 0
br i1 %25, label %26, label %29
26: ; preds = %21
%27 = getelementptr inbounds %struct.dirent, %struct.dirent* %3, i64 0, i32 1
%28 = getelementptr inbounds %struct.dirent, %struct.dirent* %3, i64 0, i32 0
br label %32
29: ; preds = %35, %21
%30 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
%31 = call i32 @exit(i32 3)
unreachable
32: ; preds = %26, %35
%33 = load %struct.dirent*, %struct.dirent** %4
%34 = icmp eq %struct.dirent* %33, null
br i1 %34, label %41, label %35
35: ; preds = %32
%36 = load i64, i64* %27
%37 = load i32, i32* %28
%38 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0), i64 %36, i32 %37)
%39 = call i32 @readdir_r(i32* %16, %struct.dirent* %3, %struct.dirent** %4)
%40 = icmp eq i32 %39, 0
br i1 %40, label %32, label %29
41: ; preds = %32
ret i32 0
}