#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*/ *opendir(char *);
int /*<<< orphan*/ printf(char *, ...);
struct dirent *readdir(int /*<<< orphan*/ *);
int main(int argc, char **argv) {
  DIR *dirptr = NULL;
  struct dirent *entry;
  if (argc < 2) {
    printf("the command need a dirname\n");
    return 1;
  }
  if (argc > 2) {
    printf("the program can only deal with one dir at once\n");
    return 1;
  }
  if ((dirptr = opendir(argv[1])) == NULL) {
    printf("open dir error !\n");
    return 1;
  } else {
    while (entry = readdir(dirptr)) {
      printf("%s\n", entry->d_name);
    }
    closedir(dirptr);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    1182 <main+0x42>
lea    0xe91(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1221 <main+0xe1>
cmpl   $0x2,-0x8(%rbp)
jle    11a6 <main+0x66>
lea    0xe89(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1221 <main+0xe1>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    11db <main+0x9b>
lea    0xe84(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1221 <main+0xe1>
jmp    11e0 <main+0xa0>
mov    -0x18(%rbp),%rdi
call   1060 <readdir@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     1211 <main+0xd1>
mov    -0x20(%rbp),%rax
mov    (%rax),%rsi
lea    0xe59(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    11e0 <main+0xa0>
mov    -0x18(%rbp),%rdi
call   1050 <closedir@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
cmp    $0x1,%edi
jg     114f <main+0xf>
lea    0xef3(%rip),%rdi
jmp    11a3 <main+0x63>
cmp    $0x2,%edi
jne    1187 <main+0x47>
mov    0x8(%rsi),%rdi
call   1040 <opendir@plt>
test   %rax,%rax
je     119c <main+0x5c>
mov    %rax,%rbx
cs nopw 0x0(%rax,%rax,1)
nop
mov    %rbx,%rdi
call   1060 <readdir@plt>
test   %rax,%rax
je     1190 <main+0x50>
mov    (%rax),%rdi
call   1030 <puts@plt>
jmp    1170 <main+0x30>
lea    0xe83(%rip),%rdi
jmp    11a3 <main+0x63>
mov    %rbx,%rdi
call   1050 <closedir@plt>
xor    %eax,%eax
pop    %rbx
ret
lea    0xe5d(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%eax
pop    %rbx
ret

LLVM IR (-O0):
%struct.dirent = type { i8* }
@.str = [28 x i8] c"the command need a dirname\0A\00"
@.str.1 = [48 x i8] c"the program can only deal with one dir at once\0A\00"
@.str.2 = [18 x i8] c"open dir error !\0A\00"
@.str.3 = [4 x i8] c"%s\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca %struct.dirent*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32* null, i32** %6
%8 = load i32, i32* %4
%9 = icmp slt i32 %8, 2
br i1 %9, label %10, label %12
10: ; preds = %2
%11 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %39
12: ; preds = %2
%13 = load i32, i32* %4
%14 = icmp sgt i32 %13, 2
br i1 %14, label %15, label %17
15: ; preds = %12
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %3
br label %39
17: ; preds = %12
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 1
%20 = load i8*, i8** %19
%21 = call i32* @opendir(i8* %20)
store i32* %21, i32** %6
%22 = icmp eq i32* %21, null
br i1 %22, label %23, label %25
23: ; preds = %17
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %3
br label %39
25: ; preds = %17
br label %26
26: ; preds = %30, %25
%27 = load i32*, i32** %6
%28 = call %struct.dirent* @readdir(i32* %27)
store %struct.dirent* %28, %struct.dirent** %7
%29 = icmp ne %struct.dirent* %28, null
br i1 %29, label %30, label %35
30: ; preds = %26
%31 = load %struct.dirent*, %struct.dirent** %7
%32 = getelementptr inbounds %struct.dirent, %struct.dirent* %31, i32 0, i32 0
%33 = load i8*, i8** %32
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* %33)
br label %26
35: ; preds = %26
%36 = load i32*, i32** %6
%37 = call i32 @closedir(i32* %36)
br label %38
38: ; preds = %35
store i32 0, i32* %3
br label %39
39: ; preds = %38, %23, %15, %10
%40 = load i32, i32* %3
ret i32 %40
}

LLVM IR (-O2):
%struct.dirent = type { i8* }
@str = [17 x i8] c"open dir error !\00"
@str.4 = [47 x i8] c"the program can only deal with one dir at once\00"
@str.5 = [27 x i8] c"the command need a dirname\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp slt i32 %0, 2
br i1 %3, label %4, label %6
4: ; preds = %2
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str.5, i64 0, i64 0))
br label %29
6: ; preds = %2
%7 = icmp eq i32 %0, 2
br i1 %7, label %10, label %8
8: ; preds = %6
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([47 x i8], [47 x i8]* @str.4, i64 0, i64 0))
br label %29
10: ; preds = %6
%11 = getelementptr inbounds i8*, i8** %1, i64 1
%12 = load i8*, i8** %11
%13 = tail call i32* @opendir(i8* %12)
%14 = icmp eq i32* %13, null
br i1 %14, label %18, label %15
15: ; preds = %10
%16 = tail call %struct.dirent* @readdir(i32* %13)
%17 = icmp eq %struct.dirent* %16, null
br i1 %17, label %27, label %20
18: ; preds = %10
%19 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str, i64 0, i64 0))
br label %29
20: ; preds = %15, %20
%21 = phi %struct.dirent* [ %25, %20 ], [ %16, %15 ]
%22 = getelementptr inbounds %struct.dirent, %struct.dirent* %21, i64 0, i32 0
%23 = load i8*, i8** %22
%24 = tail call i32 @puts(i8* dereferenceable(1) %23)
%25 = tail call %struct.dirent* @readdir(i32* %13)
%26 = icmp eq %struct.dirent* %25, null
br i1 %26, label %27, label %20
27: ; preds = %20, %15
%28 = tail call i32 @closedir(i32* %13)
br label %29
29: ; preds = %27, %18, %8, %4
%30 = phi i32 [ 1, %4 ], [ 1, %8 ], [ 1, %18 ], [ 0, %27 ]
ret i32 %30
}