#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*/ exit(int);
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ rewinddir(int /*<<< orphan*/ *);
int main(void) {
  DIR *dir;
  struct dirent *d_ptr;
  dir = opendir("./");
  if (dir == NULL) {
    perror("fail to open dir");
    exit(1);
  }
  while ((d_ptr = readdir(dir)) != NULL)
    printf("dir name is :%s \n", d_ptr->d_name);
  printf("read again\n");
  rewinddir(dir);
  while ((d_ptr = readdir(dir)) != NULL)
    printf("dir name is :%s\n", d_ptr->d_name);
  closedir(dir);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe7a(%rip),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11b0 <main+0x40>
lea    0xe62(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
jmp    11b5 <main+0x45>
mov    -0x10(%rbp),%rdi
call   1070 <readdir@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     11e6 <main+0x76>
mov    -0x18(%rbp),%rax
mov    (%rax),%rsi
lea    0xe3a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    11b5 <main+0x45>
lea    0xe39(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%rdi
call   1050 <rewinddir@plt>
mov    -0x10(%rbp),%rdi
call   1070 <readdir@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     122e <main+0xbe>
mov    -0x18(%rbp),%rax
mov    (%rax),%rsi
lea    0xe10(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    11fd <main+0x8d>
mov    -0x10(%rbp),%rdi
call   1060 <closedir@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
lea    0xe75(%rip),%rdi
call   1040 <opendir@plt>
test   %rax,%rax
je     122c <main+0xac>
mov    %rax,%rbx
mov    %rax,%rdi
call   1080 <readdir@plt>
test   %rax,%rax
je     11ca <main+0x4a>
lea    0xe64(%rip),%r14
mov    (%rax),%rsi
mov    %r14,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rbx,%rdi
call   1080 <readdir@plt>
test   %rax,%rax
jne    11b0 <main+0x30>
lea    0xe66(%rip),%rdi
call   1030 <puts@plt>
mov    %rbx,%rdi
call   1060 <rewinddir@plt>
mov    %rbx,%rdi
call   1080 <readdir@plt>
test   %rax,%rax
je     121a <main+0x9a>
lea    0xe34(%rip),%r14
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    (%rax),%rsi
mov    %r14,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rbx,%rdi
call   1080 <readdir@plt>
test   %rax,%rax
jne    1200 <main+0x80>
mov    %rbx,%rdi
call   1070 <closedir@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xdd0(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>

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

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