#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 const *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
struct dirent *readdir(int /*<<< orphan*/ *);
scalar_t__ strcmp(char *, char *);
int my_readdir(const char *path) {
  DIR *dir;
  struct dirent *ptr;
  if ((dir = opendir(path)) == NULL) {
    perror("opendir error");
    return -1;
  }
  while ((ptr = readdir(dir)) != NULL) {
    if (strcmp(ptr->d_name, ".") == 0 || strcmp(ptr->d_name, "..") == 0) {
      continue;
    } else {
      printf("file name: %s\n", ptr->d_name);
    }
  }
  closedir(dir);
  return 0;
}


x86 assembly (clang, -O0):
<my_readdir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    119b <my_readdir+0x3b>
lea    0xe76(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    121e <my_readdir+0xbe>
jmp    11a0 <my_readdir+0x40>
mov    -0x18(%rbp),%rdi
call   1070 <readdir@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     120e <my_readdir+0xae>
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
lea    0xe4a(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
je     11f1 <my_readdir+0x91>
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
lea    0xe2c(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
jne    11f6 <my_readdir+0x96>
jmp    11a0 <my_readdir+0x40>
mov    -0x20(%rbp),%rax
mov    (%rax),%rsi
lea    0xe0d(%rip),%rdi
call   1040 <printf@plt>
jmp    11a0 <my_readdir+0x40>
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):
<my_readdir>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
call   1030 <opendir@plt>
test   %rax,%rax
je     11f1 <my_readdir+0x91>
mov    %rax,%r13
mov    %rax,%rdi
call   1070 <readdir@plt>
test   %rax,%rax
je     11dd <my_readdir+0x7d>
mov    %rax,%rbx
lea    0xe82(%rip),%r14
lea    0xe7a(%rip),%r15
lea    0xe76(%rip),%r12
jmp    11b0 <my_readdir+0x50>
nopl   (%rax)
mov    %r13,%rdi
call   1070 <readdir@plt>
mov    %rax,%rbx
test   %rax,%rax
je     11dd <my_readdir+0x7d>
mov    (%rbx),%rdi
mov    %r14,%rsi
call   1060 <strcmp@plt>
test   %rax,%rax
je     11a0 <my_readdir+0x40>
mov    (%rbx),%rdi
mov    %r15,%rsi
call   1060 <strcmp@plt>
test   %rax,%rax
je     11a0 <my_readdir+0x40>
mov    (%rbx),%rsi
mov    %r12,%rdi
call   1040 <printf@plt>
jmp    11a0 <my_readdir+0x40>
mov    %r13,%rdi
call   1050 <closedir@plt>
xor    %eax,%eax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret
lea    0xe08(%rip),%rdi
call   1080 <perror@plt>
mov    $0xffffffff,%eax
jmp    11e7 <my_readdir+0x87>

LLVM IR (-O0):
%struct.dirent = type { i8* }
@.str = [14 x i8] c"opendir error\00"
@.str.1 = [2 x i8] c".\00"
@.str.2 = [3 x i8] c"..\00"
@.str.3 = [15 x i8] c"file name: %s\0A\00"
define i32 @my_readdir(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca %struct.dirent*
store i8* %0, i8** %3
%6 = load i8*, i8** %3
%7 = call i32* @opendir(i8* %6)
store i32* %7, i32** %4
%8 = icmp eq i32* %7, null
br i1 %8, label %9, label %11
9: ; preds = %1
%10 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %2
br label %38
11: ; preds = %1
br label %12
12: ; preds = %34, %28, %11
%13 = load i32*, i32** %4
%14 = call %struct.dirent* @readdir(i32* %13)
store %struct.dirent* %14, %struct.dirent** %5
%15 = icmp ne %struct.dirent* %14, null
br i1 %15, label %16, label %35
16: ; preds = %12
%17 = load %struct.dirent*, %struct.dirent** %5
%18 = getelementptr inbounds %struct.dirent, %struct.dirent* %17, i32 0, i32 0
%19 = load i8*, i8** %18
%20 = call i64 @strcmp(i8* %19, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%21 = icmp eq i64 %20, 0
br i1 %21, label %28, label %22
22: ; preds = %16
%23 = load %struct.dirent*, %struct.dirent** %5
%24 = getelementptr inbounds %struct.dirent, %struct.dirent* %23, i32 0, i32 0
%25 = load i8*, i8** %24
%26 = call i64 @strcmp(i8* %25, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%27 = icmp eq i64 %26, 0
br i1 %27, label %28, label %29
28: ; preds = %22, %16
br label %12
29: ; preds = %22
%30 = load %struct.dirent*, %struct.dirent** %5
%31 = getelementptr inbounds %struct.dirent, %struct.dirent* %30, i32 0, i32 0
%32 = load i8*, i8** %31
%33 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), i8* %32)
br label %34
34: ; preds = %29
br label %12
35: ; preds = %12
%36 = load i32*, i32** %4
%37 = call i32 @closedir(i32* %36)
store i32 0, i32* %2
br label %38
38: ; preds = %35, %9
%39 = load i32, i32* %2
ret i32 %39
}

LLVM IR (-O2):
%struct.dirent = type { i8* }
@.str = [14 x i8] c"opendir error\00"
@.str.1 = [2 x i8] c".\00"
@.str.2 = [3 x i8] c"..\00"
@.str.3 = [15 x i8] c"file name: %s\0A\00"
define i32 @my_readdir(i8* nocapture %0) {
%2 = tail call i32* @opendir(i8* %0)
%3 = icmp eq i32* %2, null
br i1 %3, label %7, label %4
4: ; preds = %1
%5 = tail call %struct.dirent* @readdir(i32* %2)
%6 = icmp eq %struct.dirent* %5, null
br i1 %6, label %25, label %9
7: ; preds = %1
%8 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %27
9: ; preds = %4, %19
%10 = phi %struct.dirent* [ %20, %19 ], [ %5, %4 ]
%11 = getelementptr inbounds %struct.dirent, %struct.dirent* %10, i64 0, i32 0
%12 = load i8*, i8** %11
%13 = tail call i64 @strcmp(i8* %12, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%14 = icmp eq i64 %13, 0
br i1 %14, label %19, label %15
15: ; preds = %9
%16 = load i8*, i8** %11
%17 = tail call i64 @strcmp(i8* %16, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%18 = icmp eq i64 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %9, %15, %22
%20 = tail call %struct.dirent* @readdir(i32* %2)
%21 = icmp eq %struct.dirent* %20, null
br i1 %21, label %25, label %9
22: ; preds = %15
%23 = load i8*, i8** %11
%24 = tail call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), i8* %23)
br label %19
25: ; preds = %19, %4
%26 = tail call i32 @closedir(i32* %2)
br label %27
27: ; preds = %25, %7
%28 = phi i32 [ -1, %7 ], [ 0, %25 ]
ret i32 %28
}