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

/* Variables and functions */
scalar_t__ S_ISDIR(int /*<<< orphan*/);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ lstat(char *, struct stat *);
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ puts(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ strcat(int /*<<< orphan*/, char *);
int /*<<< orphan*/ strcpy(char *, char *);
int /*<<< orphan*/ system(char *);
int main(int argc, char *argv[]) {
  char *path;
  path = argc == 1 ? "." : argv[1];
  DIR *p = opendir(path);
  if (p == NULL) {
    perror("opendir");
    return 1;
  }
  char buf[1024], cmd[1024];
  struct stat s;
  struct dirent *q;
  while ((q = readdir(p)) != NULL) {
    if (q->d_name[0] == '.')
      continue;
    strcat(strcat(strcpy(buf, path), "/"), q->d_name);
    printf("%s ", buf);
    lstat(buf, &s);
    if (S_ISDIR(s.st_mode))
      puts("是目录");
    else {
      puts("是文件");
      strcat(strcpy(cmd, "head "), buf);
      system(cmd);
    }
  }
  puts("");
  closedir(p);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x840,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x1,-0x8(%rbp)
jne    11e6 <main+0x36>
lea    0xe26(%rip),%rax
mov    %rax,-0x838(%rbp)
jmp    11f5 <main+0x45>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x838(%rbp)
mov    -0x838(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <opendir@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1230 <main+0x80>
lea    0xde3(%rip),%rdi
call   10c0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1338 <main+0x188>
jmp    1235 <main+0x85>
mov    -0x20(%rbp),%rdi
call   10b0 <readdir@plt>
mov    %rax,-0x830(%rbp)
cmp    $0x0,%rax
je     1323 <main+0x173>
mov    -0x830(%rbp),%rax
mov    (%rax),%rax
movsbl (%rax),%eax
cmp    $0x2e,%eax
jne    126a <main+0xba>
jmp    1235 <main+0x85>
lea    -0x420(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1030 <strcpy@plt>
mov    %eax,%edi
lea    0xd87(%rip),%rsi
call   10d0 <strcat@plt>
mov    %eax,%edi
mov    -0x830(%rbp),%rax
mov    (%rax),%rsi
call   10d0 <strcat@plt>
lea    -0x420(%rbp),%rsi
lea    0xd65(%rip),%rdi
call   1070 <printf@plt>
lea    -0x420(%rbp),%rdi
lea    -0x828(%rbp),%rsi
call   10a0 <lstat@plt>
mov    -0x828(%rbp),%edi
call   1080 <S_ISDIR@plt>
cmp    $0x0,%rax
je     12e5 <main+0x135>
lea    0xd35(%rip),%rdi
call   1040 <puts@plt>
jmp    131e <main+0x16e>
lea    0xd2e(%rip),%rdi
call   1040 <puts@plt>
lea    -0x820(%rbp),%rdi
lea    0xd25(%rip),%rsi
call   1030 <strcpy@plt>
mov    %eax,%edi
lea    -0x420(%rbp),%rsi
call   10d0 <strcat@plt>
lea    -0x820(%rbp),%rdi
call   1060 <system@plt>
jmp    1235 <main+0x85>
lea    0xcff(%rip),%rdi
call   1040 <puts@plt>
mov    -0x20(%rbp),%rdi
call   1090 <closedir@plt>
mov    -0x4(%rbp),%eax
add    $0x840,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x818,%rsp
cmp    $0x1,%edi
jne    11df <main+0x1f>
lea    0xe23(%rip),%r14
jmp    11e3 <main+0x23>
mov    0x8(%rsi),%r14
mov    %r14,%rdi
call   1060 <opendir@plt>
test   %rax,%rax
je     12f9 <main+0x139>
mov    %rax,%r13
mov    %rax,%rdi
call   10c0 <readdir@plt>
test   %rax,%rax
je     12d3 <main+0x113>
mov    %rax,%r12
lea    0x410(%rsp),%r15
lea    0xdf2(%rip),%rbp
lea    0x8(%rsp),%rbx
jmp    1270 <main+0xb0>
lea    0xdf2(%rip),%rdi
call   1050 <puts@plt>
mov    %rbx,%r12
mov    %rbp,%rbx
lea    0x10(%rsp),%rbp
mov    %rbp,%rdi
lea    0xde2(%rip),%rsi
call   1040 <strcpy@plt>
mov    %eax,%edi
mov    %r15,%rsi
call   10e0 <strcat@plt>
mov    %rbp,%rdi
mov    %rbx,%rbp
mov    %r12,%rbx
call   1070 <system@plt>
nop
mov    %r13,%rdi
call   10c0 <readdir@plt>
mov    %rax,%r12
test   %rax,%rax
je     12d3 <main+0x113>
mov    (%r12),%rax
cmpb   $0x2e,(%rax)
je     1260 <main+0xa0>
mov    %r15,%rdi
mov    %r14,%rsi
call   1040 <strcpy@plt>
mov    %eax,%edi
lea    0xd7d(%rip),%rsi
call   10e0 <strcat@plt>
mov    (%r12),%rsi
mov    %eax,%edi
call   10e0 <strcat@plt>
mov    %rbp,%rdi
mov    %r15,%rsi
call   1080 <printf@plt>
mov    %r15,%rdi
mov    %rbx,%rsi
call   10b0 <lstat@plt>
mov    0x8(%rsp),%edi
call   1090 <S_ISDIR@plt>
test   %rax,%rax
je     1221 <main+0x61>
lea    0xd44(%rip),%rdi
call   1050 <puts@plt>
jmp    1260 <main+0xa0>
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    %r13,%rdi
call   10a0 <closedir@plt>
xor    %eax,%eax
add    $0x818,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd02(%rip),%rdi
call   10d0 <perror@plt>
mov    $0x1,%eax
jmp    12e7 <main+0x127>

LLVM IR (-O0):
%struct.stat = type { i32 }
%struct.dirent = type { i8* }
@.str = [2 x i8] c".\00"
@.str.1 = [8 x i8] c"opendir\00"
@.str.2 = [2 x i8] c"/\00"
@.str.3 = [4 x i8] c"%s \00"
@.str.4 = [10 x i8] c"\E6\98\AF\E7\9B\AE\E5\BD\95\00"
@.str.5 = [10 x i8] c"\E6\98\AF\E6\96\87\E4\BB\B6\00"
@.str.6 = [6 x i8] c"head \00"
@.str.7 = [1 x i8] zeroinitializer
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i32*
%8 = alloca [1024 x i8]
%9 = alloca [1024 x i8]
%10 = alloca %struct.stat
%11 = alloca %struct.dirent*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = load i32, i32* %4
%13 = icmp eq i32 %12, 1
br i1 %13, label %14, label %15
14: ; preds = %2
br label %19
15: ; preds = %2
%16 = load i8**, i8*** %5
%17 = getelementptr inbounds i8*, i8** %16, i64 1
%18 = load i8*, i8** %17
br label %19
19: ; preds = %15, %14
%20 = phi i8* [ getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), %14 ], [ %18, %15 ]
store i8* %20, i8** %6
%21 = load i8*, i8** %6
%22 = call i32* @opendir(i8* %21)
store i32* %22, i32** %7
%23 = load i32*, i32** %7
%24 = icmp eq i32* %23, null
br i1 %24, label %25, label %27
25: ; preds = %19
%26 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %3
br label %73
27: ; preds = %19
br label %28
28: ; preds = %68, %40, %27
%29 = load i32*, i32** %7
%30 = call %struct.dirent* @readdir(i32* %29)
store %struct.dirent* %30, %struct.dirent** %11
%31 = icmp ne %struct.dirent* %30, null
br i1 %31, label %32, label %69
32: ; preds = %28
%33 = load %struct.dirent*, %struct.dirent** %11
%34 = getelementptr inbounds %struct.dirent, %struct.dirent* %33, i32 0, i32 0
%35 = load i8*, i8** %34
%36 = getelementptr inbounds i8, i8* %35, i64 0
%37 = load i8, i8* %36
%38 = sext i8 %37 to i32
%39 = icmp eq i32 %38, 46
br i1 %39, label %40, label %41
40: ; preds = %32
br label %28
41: ; preds = %32
%42 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%43 = load i8*, i8** %6
%44 = call i32 @strcpy(i8* %42, i8* %43)
%45 = call i32 @strcat(i32 %44, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%46 = load %struct.dirent*, %struct.dirent** %11
%47 = getelementptr inbounds %struct.dirent, %struct.dirent* %46, i32 0, i32 0
%48 = load i8*, i8** %47
%49 = call i32 @strcat(i32 %45, i8* %48)
%50 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%51 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* %50)
%52 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%53 = call i32 @lstat(i8* %52, %struct.stat* %10)
%54 = getelementptr inbounds %struct.stat, %struct.stat* %10, i32 0, i32 0
%55 = load i32, i32* %54
%56 = call i64 @S_ISDIR(i32 %55)
%57 = icmp ne i64 %56, 0
br i1 %57, label %58, label %60
58: ; preds = %41
%59 = call i32 @puts(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0))
br label %68
60: ; preds = %41
%61 = call i32 @puts(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0))
%62 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%63 = call i32 @strcpy(i8* %62, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.6, i64 0, i64 0))
%64 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%65 = call i32 @strcat(i32 %63, i8* %64)
%66 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%67 = call i32 @system(i8* %66)
br label %68
68: ; preds = %60, %58
br label %28
69: ; preds = %28
%70 = call i32 @puts(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.7, i64 0, i64 0))
%71 = load i32*, i32** %7
%72 = call i32 @closedir(i32* %71)
br label %73
73: ; preds = %69, %25
%74 = load i32, i32* %3
ret i32 %74
}

LLVM IR (-O2):
%struct.stat = type { i32 }
%struct.dirent = type { i8* }
@.str = [2 x i8] c".\00"
@.str.1 = [8 x i8] c"opendir\00"
@.str.2 = [2 x i8] c"/\00"
@.str.3 = [4 x i8] c"%s \00"
@.str.4 = [10 x i8] c"\E6\98\AF\E7\9B\AE\E5\BD\95\00"
@.str.5 = [10 x i8] c"\E6\98\AF\E6\96\87\E4\BB\B6\00"
@.str.6 = [6 x i8] c"head \00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [1024 x i8]
%4 = alloca [1024 x i8]
%5 = alloca %struct.stat
%6 = icmp eq i32 %0, 1
br i1 %6, label %10, label %7
7: ; preds = %2
%8 = getelementptr inbounds i8*, i8** %1, i64 1
%9 = load i8*, i8** %8
br label %10
10: ; preds = %2, %7
%11 = phi i8* [ %9, %7 ], [ getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), %2 ]
%12 = tail call i32* @opendir(i8* %11)
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %16
14: ; preds = %10
%15 = tail call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
br label %53
16: ; preds = %10
%17 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%18 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%19 = bitcast %struct.stat* %5 to i8*
%20 = call %struct.dirent* @readdir(i32* %12)
%21 = icmp eq %struct.dirent* %20, null
br i1 %21, label %50, label %22
22: ; preds = %16
%23 = getelementptr inbounds %struct.stat, %struct.stat* %5, i64 0, i32 0
br label %24
24: ; preds = %22, %47
%25 = phi %struct.dirent* [ %20, %22 ], [ %48, %47 ]
%26 = getelementptr inbounds %struct.dirent, %struct.dirent* %25, i64 0, i32 0
%27 = load i8*, i8** %26
%28 = load i8, i8* %27
%29 = icmp eq i8 %28, 46
br i1 %29, label %47, label %30
30: ; preds = %24
%31 = call i32 @strcpy(i8* %17, i8* %11)
%32 = call i32 @strcat(i32 %31, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%33 = load i8*, i8** %26
%34 = call i32 @strcat(i32 %32, i8* %33)
%35 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* %17)
%36 = call i32 @lstat(i8* %17, %struct.stat* %5)
%37 = load i32, i32* %23
%38 = call i64 @S_ISDIR(i32 %37)
%39 = icmp eq i64 %38, 0
br i1 %39, label %42, label %40
40: ; preds = %30
%41 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0))
br label %47
42: ; preds = %30
%43 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0))
%44 = call i32 @strcpy(i8* %18, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.6, i64 0, i64 0))
%45 = call i32 @strcat(i32 %44, i8* %17)
%46 = call i32 @system(i8* %18)
br label %47
47: ; preds = %40, %42, %24
%48 = call %struct.dirent* @readdir(i32* %12)
%49 = icmp eq %struct.dirent* %48, null
br i1 %49, label %50, label %24
50: ; preds = %47, %16
%51 = call i32 @putchar(i32 10)
%52 = call i32 @closedir(i32* %12)
br label %53
53: ; preds = %50, %14
%54 = phi i32 [ 1, %14 ], [ 0, %50 ]
ret i32 %54
}