#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;
  int /*<<< orphan*/ d_type;
  int /*<<< orphan*/ d_reclen;
  scalar_t__ d_off;
  scalar_t__ d_ino;
};
typedef int /*<<< orphan*/ DIR;

/* Variables and functions */
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ *opendir(char const *);
int /*<<< orphan*/ printf(char *, ...);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ show_size(struct dirent *);
scalar_t__ strcmp(char const *, int /*<<< orphan*/);
int read_dir(const char *path, const char *my) {
  if (NULL == path) {
    return -1;
  }
  DIR *pDir = opendir(path);
  if (NULL == pDir) {
    printf("open dir [%s] faild", path);
    return -1;
  }
  printf("%6s %20s %6s %4s %8s\n", "inode", "off", "length", "type",
         "filename");
  while (1) {
    struct dirent *pFile = readdir(pDir);
    if (NULL == pFile) {
      break;
    }
    if (strcmp(my, pFile->d_name) == 0) {
      show_size(pFile);
    }
    printf("%6lu %20lu %6d %4d %8s\n", (long unsigned)pFile->d_ino,
           (long unsigned)pFile->d_off, pFile->d_reclen, pFile->d_type,
           pFile->d_name);
  }
  closedir(pDir);
  return 0;
}


x86 assembly (clang, -O0):
<read_dir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
xor    %eax,%eax
cmp    -0x10(%rbp),%rax
jne    1188 <read_dir+0x28>
movl   $0xffffffff,-0x4(%rbp)
jmp    1279 <read_dir+0x119>
mov    -0x10(%rbp),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x20(%rbp)
xor    %eax,%eax
cmp    -0x20(%rbp),%rax
jne    11bf <read_dir+0x5f>
mov    -0x10(%rbp),%rsi
lea    0xe54(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1279 <read_dir+0x119>
lea    0xe4e(%rip),%rdi
lea    0xe5d(%rip),%rsi
lea    0xe5c(%rip),%rdx
lea    0xe59(%rip),%rcx
lea    0xe59(%rip),%r8
lea    0xe57(%rip),%r9
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x20(%rbp),%rdi
call   1080 <readdir@plt>
mov    %rax,-0x28(%rbp)
xor    %eax,%eax
cmp    -0x28(%rbp),%rax
jne    120e <read_dir+0xae>
jmp    1269 <read_dir+0x109>
mov    -0x18(%rbp),%rdi
mov    -0x28(%rbp),%rax
mov    (%rax),%esi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
jne    1230 <read_dir+0xd0>
mov    -0x28(%rbp),%rdi
call   1070 <show_size@plt>
mov    -0x28(%rbp),%rax
mov    0x18(%rax),%rsi
mov    -0x28(%rbp),%rax
mov    0x10(%rax),%rdx
mov    -0x28(%rbp),%rax
mov    0x8(%rax),%ecx
mov    -0x28(%rbp),%rax
mov    0x4(%rax),%r8d
mov    -0x28(%rbp),%rax
mov    (%rax),%r9d
lea    0xdec(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    11f0 <read_dir+0x90>
mov    -0x20(%rbp),%rdi
call   1050 <closedir@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_dir>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    $0xffffffff,%r14d
test   %rdi,%rdi
je     1240 <read_dir+0xe0>
mov    %rsi,%r12
mov    %rdi,%rbx
call   1030 <opendir@plt>
test   %rax,%rax
je     122f <read_dir+0xcf>
mov    %rax,%rbp
lea    0xe7f(%rip),%rdi
lea    0xe8e(%rip),%rsi
lea    0xe8d(%rip),%rdx
lea    0xe8a(%rip),%rcx
lea    0xe8a(%rip),%r8
lea    0xe88(%rip),%r9
xor    %r14d,%r14d
xor    %eax,%eax
call   1040 <printf@plt>
mov    %rbp,%rdi
call   1080 <readdir@plt>
test   %rax,%rax
je     1225 <read_dir+0xc5>
mov    %rax,%rbx
lea    0xe70(%rip),%r15
jmp    120c <read_dir+0xac>
nopl   0x0(%rax,%rax,1)
mov    0x18(%rbx),%rsi
mov    0x10(%rbx),%rdx
mov    0x8(%rbx),%ecx
mov    (%rbx),%r9d
mov    0x4(%rbx),%r8d
mov    %r15,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %rbp,%rdi
call   1080 <readdir@plt>
mov    %rax,%rbx
test   %rax,%rax
je     1225 <read_dir+0xc5>
mov    (%rbx),%esi
mov    %r12,%rdi
call   1060 <strcmp@plt>
test   %rax,%rax
jne    11e0 <read_dir+0x80>
mov    %rbx,%rdi
call   1070 <show_size@plt>
jmp    11e0 <read_dir+0x80>
mov    %rbp,%rdi
call   1050 <closedir@plt>
jmp    1240 <read_dir+0xe0>
lea    0xdca(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %r14d,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.dirent = type { i32, i32, i32, i64, i64 }
@.str = [20 x i8] c"open dir [%s] faild\00"
@.str.1 = [22 x i8] c"%6s %20s %6s %4s %8s\0A\00"
@.str.2 = [6 x i8] c"inode\00"
@.str.3 = [4 x i8] c"off\00"
@.str.4 = [7 x i8] c"length\00"
@.str.5 = [5 x i8] c"type\00"
@.str.6 = [9 x i8] c"filename\00"
@.str.7 = [24 x i8] c"%6lu %20lu %6d %4d %8s\0A\00"
define i32 @read_dir(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca %struct.dirent*
store i8* %0, i8** %4
store i8* %1, i8** %5
%8 = load i8*, i8** %4
%9 = icmp eq i8* null, %8
br i1 %9, label %10, label %11
10: ; preds = %2
store i32 -1, i32* %3
br label %57
11: ; preds = %2
%12 = load i8*, i8** %4
%13 = call i32* @opendir(i8* %12)
store i32* %13, i32** %6
%14 = load i32*, i32** %6
%15 = icmp eq i32* null, %14
br i1 %15, label %16, label %19
16: ; preds = %11
%17 = load i8*, i8** %4
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %17)
store i32 -1, i32* %3
br label %57
19: ; preds = %11
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.6, i64 0, i64 0))
br label %21
21: ; preds = %19, %37
%22 = load i32*, i32** %6
%23 = call %struct.dirent* @readdir(i32* %22)
store %struct.dirent* %23, %struct.dirent** %7
%24 = load %struct.dirent*, %struct.dirent** %7
%25 = icmp eq %struct.dirent* null, %24
br i1 %25, label %26, label %27
26: ; preds = %21
br label %54
27: ; preds = %21
%28 = load i8*, i8** %5
%29 = load %struct.dirent*, %struct.dirent** %7
%30 = getelementptr inbounds %struct.dirent, %struct.dirent* %29, i32 0, i32 0
%31 = load i32, i32* %30
%32 = call i64 @strcmp(i8* %28, i32 %31)
%33 = icmp eq i64 %32, 0
br i1 %33, label %34, label %37
34: ; preds = %27
%35 = load %struct.dirent*, %struct.dirent** %7
%36 = call i32 @show_size(%struct.dirent* %35)
br label %37
37: ; preds = %34, %27
%38 = load %struct.dirent*, %struct.dirent** %7
%39 = getelementptr inbounds %struct.dirent, %struct.dirent* %38, i32 0, i32 4
%40 = load i64, i64* %39
%41 = load %struct.dirent*, %struct.dirent** %7
%42 = getelementptr inbounds %struct.dirent, %struct.dirent* %41, i32 0, i32 3
%43 = load i64, i64* %42
%44 = load %struct.dirent*, %struct.dirent** %7
%45 = getelementptr inbounds %struct.dirent, %struct.dirent* %44, i32 0, i32 2
%46 = load i32, i32* %45
%47 = load %struct.dirent*, %struct.dirent** %7
%48 = getelementptr inbounds %struct.dirent, %struct.dirent* %47, i32 0, i32 1
%49 = load i32, i32* %48
%50 = load %struct.dirent*, %struct.dirent** %7
%51 = getelementptr inbounds %struct.dirent, %struct.dirent* %50, i32 0, i32 0
%52 = load i32, i32* %51
%53 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.7, i64 0, i64 0), i64 %40, i64 %43, i32 %46, i32 %49, i32 %52)
br label %21
54: ; preds = %26
%55 = load i32*, i32** %6
%56 = call i32 @closedir(i32* %55)
store i32 0, i32* %3
br label %57
57: ; preds = %54, %16, %10
%58 = load i32, i32* %3
ret i32 %58
}

LLVM IR (-O2):
%struct.dirent = type { i32, i32, i32, i64, i64 }
@.str = [20 x i8] c"open dir [%s] faild\00"
@.str.1 = [22 x i8] c"%6s %20s %6s %4s %8s\0A\00"
@.str.2 = [6 x i8] c"inode\00"
@.str.3 = [4 x i8] c"off\00"
@.str.4 = [7 x i8] c"length\00"
@.str.5 = [5 x i8] c"type\00"
@.str.6 = [9 x i8] c"filename\00"
@.str.7 = [24 x i8] c"%6lu %20lu %6d %4d %8s\0A\00"
define i32 @read_dir(i8* %0, i8* %1) {
%3 = icmp eq i8* %0, null
br i1 %3, label %36, label %4
4: ; preds = %2
%5 = tail call i32* @opendir(i8* %0)
%6 = icmp eq i32* %5, null
br i1 %6, label %7, label %9
7: ; preds = %4
%8 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %36
9: ; preds = %4
%10 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.6, i64 0, i64 0))
%11 = tail call %struct.dirent* @readdir(i32* %5)
%12 = icmp eq %struct.dirent* %11, null
br i1 %12, label %34, label %13
13: ; preds = %9, %21
%14 = phi %struct.dirent* [ %32, %21 ], [ %11, %9 ]
%15 = getelementptr inbounds %struct.dirent, %struct.dirent* %14, i64 0, i32 0
%16 = load i32, i32* %15
%17 = tail call i64 @strcmp(i8* %1, i32 %16)
%18 = icmp eq i64 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %13
%20 = tail call i32 @show_size(%struct.dirent* %14)
br label %21
21: ; preds = %13, %19
%22 = getelementptr inbounds %struct.dirent, %struct.dirent* %14, i64 0, i32 4
%23 = load i64, i64* %22
%24 = getelementptr inbounds %struct.dirent, %struct.dirent* %14, i64 0, i32 3
%25 = load i64, i64* %24
%26 = getelementptr inbounds %struct.dirent, %struct.dirent* %14, i64 0, i32 2
%27 = load i32, i32* %26
%28 = getelementptr inbounds %struct.dirent, %struct.dirent* %14, i64 0, i32 1
%29 = load i32, i32* %28
%30 = load i32, i32* %15
%31 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.7, i64 0, i64 0), i64 %23, i64 %25, i32 %27, i32 %29, i32 %30)
%32 = tail call %struct.dirent* @readdir(i32* %5)
%33 = icmp eq %struct.dirent* %32, null
br i1 %33, label %34, label %13
34: ; preds = %21, %9
%35 = tail call i32 @closedir(i32* %5)
br label %36
36: ; preds = %7, %34, %2
%37 = phi i32 [ -1, %2 ], [ -1, %7 ], [ 0, %34 ]
ret i32 %37
}