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


x86 assembly (clang, -O0):
<my_readir>:
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    118b <my_readir+0x3b>
lea    0xe86(%rip),%rdi
call   1070 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11cf <my_readir+0x7f>
jmp    1190 <my_readir+0x40>
mov    -0x18(%rbp),%rdi
call   1060 <readdir@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     11bf <my_readir+0x6f>
mov    -0x20(%rbp),%rax
mov    (%rax),%rsi
lea    0xe53(%rip),%rdi
call   1040 <printf@plt>
jmp    1190 <my_readir+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_readir>:
push   %r14
push   %rbx
push   %rax
call   1030 <opendir@plt>
test   %rax,%rax
je     11aa <my_readir+0x5a>
mov    %rax,%rbx
mov    %rax,%rdi
call   1060 <readdir@plt>
test   %rax,%rax
je     1198 <my_readir+0x48>
lea    0xe93(%rip),%r14
cs nopw 0x0(%rax,%rax,1)
nop
mov    (%rax),%rsi
mov    %r14,%rdi
call   1040 <printf@plt>
mov    %rbx,%rdi
call   1060 <readdir@plt>
test   %rax,%rax
jne    1180 <my_readir+0x30>
mov    %rbx,%rdi
call   1050 <closedir@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe4f(%rip),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%eax
jmp    11a2 <my_readir+0x52>

LLVM IR (-O0):
%struct.dirent = type { i8* }
@.str = [8 x i8] c"opendir\00"
@.str.1 = [14 x i8] c"file name:%s\0A\00"
define i32 @my_readir(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 ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %2
br label %24
11: ; preds = %1
br label %12
12: ; preds = %16, %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 %21
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 i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i8* %19)
br label %12
21: ; preds = %12
%22 = load i32*, i32** %4
%23 = call i32 @closedir(i32* %22)
store i32 0, i32* %2
br label %24
24: ; preds = %21, %9
%25 = load i32, i32* %2
ret i32 %25
}

LLVM IR (-O2):
%struct.dirent = type { i8* }
@.str = [8 x i8] c"opendir\00"
@.str.1 = [14 x i8] c"file name:%s\0A\00"
define i32 @my_readir(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 %16, label %9
7: ; preds = %1
%8 = tail call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
br label %18
9: ; preds = %4, %9
%10 = phi %struct.dirent* [ %14, %9 ], [ %5, %4 ]
%11 = getelementptr inbounds %struct.dirent, %struct.dirent* %10, i64 0, i32 0
%12 = load i8*, i8** %11
%13 = tail call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i8* %12)
%14 = tail call %struct.dirent* @readdir(i32* %2)
%15 = icmp eq %struct.dirent* %14, null
br i1 %15, label %16, label %9
16: ; preds = %9, %4
%17 = tail call i32 @closedir(i32* %2)
br label %18
18: ; preds = %16, %7
%19 = phi i32 [ -1, %7 ], [ 0, %16 ]
ret i32 %19
}