#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 off_t;
typedef int /*<<< orphan*/ DIR;

/* Variables and functions */
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ printf(char *, ...);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ rewinddir(int /*<<< orphan*/ *);
int /*<<< orphan*/ seekdir(int /*<<< orphan*/ *, long);
char *strerror(int /*<<< orphan*/);
void *telldir(int /*<<< orphan*/ *);
int main(int argc, char *argv[]) {
  DIR *dirp;
  long int save3 = 0;
  long int cur;
  int i = 0;
  int result = 0;
  struct dirent *dp;
  off_t save0, rewind_ret;
  dirp = opendir(".");
  if (dirp == NULL) {
    printf("opendir failed: %s\n", strerror(errno));
    return 1;
  }
  save0 = telldir(dirp);
  if (save0 == -1) {
    printf("telldir failed: %s\n", strerror(errno));
    result = 1;
  }
  for (dp = readdir(dirp); dp != NULL; dp = readdir(dirp)) {
    if (i++ == 3)
      save3 = telldir(dirp);
    printf("%s\n", dp->d_name);
    if (i == 400)
      break;
  }
  printf("going back past 4-th entry...\n");
  seekdir(dirp, save3);
  cur = telldir(dirp);
  if (cur != save3) {
    printf("seekdir (d, %ld); telldir (d) == %ld\n", save3, cur);
    result = 1;
  }
  for (dp = readdir(dirp); dp != NULL; dp = readdir(dirp))
    printf("%s\n", dp->d_name);
  rewinddir(dirp);
  rewind_ret = telldir(dirp);
  if (rewind_ret == -1) {
    printf("telldir failed: %s\n", strerror(errno));
    result = 1;
  } else if (save0 != rewind_ret) {
    printf("rewinddir didn't reset directory stream\n");
    result = 1;
  }
  closedir(dirp);
  return result;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x20(%rbp)
movl   $0x0,-0x2c(%rbp)
movl   $0x0,-0x30(%rbp)
lea    0xe4d(%rip),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11f2 <main+0x72>
mov    0x2e12(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    %rax,%rsi
lea    0xe23(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    13b6 <main+0x236>
mov    -0x18(%rbp),%rdi
call   1090 <telldir@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0xffffffff,-0x3c(%rbp)
jne    122e <main+0xae>
mov    0x2dd1(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    %rax,%rsi
lea    0xdf6(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x30(%rbp)
mov    -0x18(%rbp),%rdi
call   1070 <readdir@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
je     12a5 <main+0x125>
mov    -0x2c(%rbp),%eax
mov    %eax,%ecx
add    $0x1,%ecx
mov    %ecx,-0x2c(%rbp)
cmp    $0x3,%eax
jne    1267 <main+0xe7>
mov    -0x18(%rbp),%rdi
call   1090 <telldir@plt>
mov    %rax,-0x20(%rbp)
mov    -0x38(%rbp),%rax
mov    (%rax),%rsi
lea    0xdb1(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
cmpl   $0x190,-0x2c(%rbp)
jne    128e <main+0x10e>
jmp    12a5 <main+0x125>
jmp    1293 <main+0x113>
mov    -0x18(%rbp),%rdi
call   1070 <readdir@plt>
mov    %rax,-0x38(%rbp)
jmp    123b <main+0xbb>
lea    0xd7e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1080 <seekdir@plt>
mov    -0x18(%rbp),%rdi
call   1090 <telldir@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x20(%rbp),%rax
je     12f8 <main+0x178>
mov    -0x20(%rbp),%rsi
mov    -0x28(%rbp),%rdx
lea    0xd5f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x30(%rbp)
mov    -0x18(%rbp),%rdi
call   1070 <readdir@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
je     1337 <main+0x1b7>
mov    -0x38(%rbp),%rax
mov    (%rax),%rsi
lea    0xd08(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1070 <readdir@plt>
mov    %rax,-0x38(%rbp)
jmp    1305 <main+0x185>
mov    -0x18(%rbp),%rdi
call   1050 <rewinddir@plt>
mov    -0x18(%rbp),%rdi
call   1090 <telldir@plt>
mov    %eax,-0x40(%rbp)
cmpl   $0xffffffff,-0x40(%rbp)
jne    1381 <main+0x201>
mov    0x2c83(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    %rax,%rsi
lea    0xca8(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x30(%rbp)
jmp    13a7 <main+0x227>
mov    -0x3c(%rbp),%eax
cmp    -0x40(%rbp),%eax
je     13a2 <main+0x222>
lea    0xcdb(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x30(%rbp)
jmp    13a7 <main+0x227>
mov    -0x18(%rbp),%rdi
call   1060 <closedir@plt>
mov    -0x30(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
lea    0xec8(%rip),%rdi
call   1040 <opendir@plt>
test   %rax,%rax
je     124e <main+0xbe>
mov    %rax,%r13
mov    %rax,%rdi
call   10a0 <telldir@plt>
mov    %rax,%r14
xor    %r15d,%r15d
cmp    $0xffffffff,%r14d
jne    11ec <main+0x5c>
mov    0x2e12(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xe38(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%r15d
mov    %r13,%rdi
call   1080 <readdir@plt>
test   %rax,%rax
je     1278 <main+0xe8>
mov    %rax,%rbp
mov    $0x18f,%ebx
xor    %r12d,%r12d
jmp    1237 <main+0xa7>
nopw   0x0(%rax,%rax,1)
mov    %r13,%rdi
call   10a0 <telldir@plt>
mov    %rax,%r12
mov    0x0(%rbp),%rdi
call   1030 <puts@plt>
mov    %r13,%rdi
call   1080 <readdir@plt>
mov    %rax,%rbp
add    $0xffffffff,%ebx
test   %rax,%rax
je     127b <main+0xeb>
cmp    $0x18c,%ebx
je     1210 <main+0x80>
mov    0x0(%rbp),%rdi
call   1030 <puts@plt>
test   %ebx,%ebx
jne    1224 <main+0x94>
jmp    127b <main+0xeb>
mov    0x2d8b(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xd9d(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%r15d
jmp    132a <main+0x19a>
xor    %r12d,%r12d
lea    0xdcc(%rip),%rdi
call   1030 <puts@plt>
mov    %r13,%rdi
mov    %r12,%rsi
call   1090 <seekdir@plt>
mov    %r13,%rdi
call   10a0 <telldir@plt>
cmp    %rax,%r12
je     12c8 <main+0x138>
lea    0xd82(%rip),%rdi
mov    %r12,%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%r15d
jmp    12c8 <main+0x138>
nopl   0x0(%rax,%rax,1)
mov    (%rax),%rdi
call   1030 <puts@plt>
mov    %r13,%rdi
call   1080 <readdir@plt>
test   %rax,%rax
jne    12c0 <main+0x130>
mov    %r13,%rdi
call   1060 <rewinddir@plt>
mov    %r13,%rdi
call   10a0 <telldir@plt>
cmp    $0xffffffff,%eax
je     12fd <main+0x16d>
cmp    %eax,%r14d
je     1322 <main+0x192>
lea    0xd76(%rip),%rdi
call   1030 <puts@plt>
jmp    131c <main+0x18c>
mov    0x2cdc(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xd02(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%r15d
mov    %r13,%rdi
call   1070 <closedir@plt>
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.dirent = type { i8* }
@.str = [2 x i8] c".\00"
@.str.1 = [20 x i8] c"opendir failed: %s\0A\00"
@errno = global i32 0
@.str.2 = [20 x i8] c"telldir failed: %s\0A\00"
@.str.3 = [4 x i8] c"%s\0A\00"
@.str.4 = [31 x i8] c"going back past 4-th entry...\0A\00"
@.str.5 = [38 x i8] c"seekdir (d, %ld); telldir (d) == %ld\0A\00"
@.str.6 = [41 x i8] c"rewinddir didn't reset directory stream\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca i64
%8 = alloca i64
%9 = alloca i32
%10 = alloca i32
%11 = alloca %struct.dirent*
%12 = alloca i32
%13 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i64 0, i64* %7
store i32 0, i32* %9
store i32 0, i32* %10
%14 = call i32* @opendir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %14, i32** %6
%15 = load i32*, i32** %6
%16 = icmp eq i32* %15, null
br i1 %16, label %17, label %21
17: ; preds = %2
%18 = load i32, i32* @errno
%19 = call i8* @strerror(i32 %18)
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i8* %19)
store i32 1, i32* %3
br label %109
21: ; preds = %2
%22 = load i32*, i32** %6
%23 = call i8* @telldir(i32* %22)
%24 = ptrtoint i8* %23 to i32
store i32 %24, i32* %12
%25 = load i32, i32* %12
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %31
27: ; preds = %21
%28 = load i32, i32* @errno
%29 = call i8* @strerror(i32 %28)
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8* %29)
store i32 1, i32* %10
br label %31
31: ; preds = %27, %21
%32 = load i32*, i32** %6
%33 = call %struct.dirent* @readdir(i32* %32)
store %struct.dirent* %33, %struct.dirent** %11
br label %34
34: ; preds = %54, %31
%35 = load %struct.dirent*, %struct.dirent** %11
%36 = icmp ne %struct.dirent* %35, null
br i1 %36, label %37, label %57
37: ; preds = %34
%38 = load i32, i32* %9
%39 = add nsw i32 %38, 1
store i32 %39, i32* %9
%40 = icmp eq i32 %38, 3
br i1 %40, label %41, label %45
41: ; preds = %37
%42 = load i32*, i32** %6
%43 = call i8* @telldir(i32* %42)
%44 = ptrtoint i8* %43 to i64
store i64 %44, i64* %7
br label %45
45: ; preds = %41, %37
%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 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* %48)
%50 = load i32, i32* %9
%51 = icmp eq i32 %50, 400
br i1 %51, label %52, label %53
52: ; preds = %45
br label %57
53: ; preds = %45
br label %54
54: ; preds = %53
%55 = load i32*, i32** %6
%56 = call %struct.dirent* @readdir(i32* %55)
store %struct.dirent* %56, %struct.dirent** %11
br label %34
57: ; preds = %52, %34
%58 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.4, i64 0, i64 0))
%59 = load i32*, i32** %6
%60 = load i64, i64* %7
%61 = call i32 @seekdir(i32* %59, i64 %60)
%62 = load i32*, i32** %6
%63 = call i8* @telldir(i32* %62)
%64 = ptrtoint i8* %63 to i64
store i64 %64, i64* %8
%65 = load i64, i64* %8
%66 = load i64, i64* %7
%67 = icmp ne i64 %65, %66
br i1 %67, label %68, label %72
68: ; preds = %57
%69 = load i64, i64* %7
%70 = load i64, i64* %8
%71 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.5, i64 0, i64 0), i64 %69, i64 %70)
store i32 1, i32* %10
br label %72
72: ; preds = %68, %57
%73 = load i32*, i32** %6
%74 = call %struct.dirent* @readdir(i32* %73)
store %struct.dirent* %74, %struct.dirent** %11
br label %75
75: ; preds = %83, %72
%76 = load %struct.dirent*, %struct.dirent** %11
%77 = icmp ne %struct.dirent* %76, null
br i1 %77, label %78, label %86
78: ; preds = %75
%79 = load %struct.dirent*, %struct.dirent** %11
%80 = getelementptr inbounds %struct.dirent, %struct.dirent* %79, i32 0, i32 0
%81 = load i8*, i8** %80
%82 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* %81)
br label %83
83: ; preds = %78
%84 = load i32*, i32** %6
%85 = call %struct.dirent* @readdir(i32* %84)
store %struct.dirent* %85, %struct.dirent** %11
br label %75
86: ; preds = %75
%87 = load i32*, i32** %6
%88 = call i32 @rewinddir(i32* %87)
%89 = load i32*, i32** %6
%90 = call i8* @telldir(i32* %89)
%91 = ptrtoint i8* %90 to i32
store i32 %91, i32* %13
%92 = load i32, i32* %13
%93 = icmp eq i32 %92, -1
br i1 %93, label %94, label %98
94: ; preds = %86
%95 = load i32, i32* @errno
%96 = call i8* @strerror(i32 %95)
%97 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8* %96)
store i32 1, i32* %10
br label %105
98: ; preds = %86
%99 = load i32, i32* %12
%100 = load i32, i32* %13
%101 = icmp ne i32 %99, %100
br i1 %101, label %102, label %104
102: ; preds = %98
%103 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.6, i64 0, i64 0))
store i32 1, i32* %10
br label %104
104: ; preds = %102, %98
br label %105
105: ; preds = %104, %94
%106 = load i32*, i32** %6
%107 = call i32 @closedir(i32* %106)
%108 = load i32, i32* %10
store i32 %108, i32* %3
br label %109
109: ; preds = %105, %17
%110 = load i32, i32* %3
ret i32 %110
}

LLVM IR (-O2):
%struct.dirent = type { i8* }
@.str = [2 x i8] c".\00"
@.str.1 = [20 x i8] c"opendir failed: %s\0A\00"
@errno = global i32 0
@.str.2 = [20 x i8] c"telldir failed: %s\0A\00"
@.str.5 = [38 x i8] c"seekdir (d, %ld); telldir (d) == %ld\0A\00"
@str = [30 x i8] c"going back past 4-th entry...\00"
@str.7 = [40 x i8] c"rewinddir didn't reset directory stream\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i32* @opendir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %5, label %9
5: ; preds = %2
%6 = load i32, i32* @errno
%7 = tail call i8* @strerror(i32 %6)
%8 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i8* %7)
br label %80
9: ; preds = %2
%10 = tail call i8* @telldir(i32* %3)
%11 = ptrtoint i8* %10 to i64
%12 = trunc i64 %11 to i32
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %18
14: ; preds = %9
%15 = load i32, i32* @errno
%16 = tail call i8* @strerror(i32 %15)
%17 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8* %16)
br label %18
18: ; preds = %14, %9
%19 = phi i32 [ 1, %14 ], [ 0, %9 ]
%20 = tail call %struct.dirent* @readdir(i32* %3)
%21 = icmp eq %struct.dirent* %20, null
br i1 %21, label %43, label %22
22: ; preds = %18, %39
%23 = phi i64 [ %40, %39 ], [ 0, %18 ]
%24 = phi %struct.dirent* [ %41, %39 ], [ %20, %18 ]
%25 = phi i32 [ %26, %39 ], [ 0, %18 ]
%26 = add nuw nsw i32 %25, 1
%27 = icmp eq i32 %25, 3
br i1 %27, label %28, label %34
28: ; preds = %22
%29 = tail call i8* @telldir(i32* %3)
%30 = ptrtoint i8* %29 to i64
%31 = getelementptr inbounds %struct.dirent, %struct.dirent* %24, i64 0, i32 0
%32 = load i8*, i8** %31
%33 = tail call i32 @puts(i8* dereferenceable(1) %32)
br label %39
34: ; preds = %22
%35 = getelementptr inbounds %struct.dirent, %struct.dirent* %24, i64 0, i32 0
%36 = load i8*, i8** %35
%37 = tail call i32 @puts(i8* dereferenceable(1) %36)
%38 = icmp eq i32 %26, 400
br i1 %38, label %43, label %39
39: ; preds = %28, %34
%40 = phi i64 [ %30, %28 ], [ %23, %34 ]
%41 = tail call %struct.dirent* @readdir(i32* %3)
%42 = icmp eq %struct.dirent* %41, null
br i1 %42, label %43, label %22
43: ; preds = %39, %34, %18
%44 = phi i64 [ 0, %18 ], [ %23, %34 ], [ %40, %39 ]
%45 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @str, i64 0, i64 0))
%46 = tail call i32 @seekdir(i32* %3, i64 %44)
%47 = tail call i8* @telldir(i32* %3)
%48 = ptrtoint i8* %47 to i64
%49 = icmp eq i64 %44, %48
br i1 %49, label %52, label %50
50: ; preds = %43
%51 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @.str.5, i64 0, i64 0), i64 %44, i64 %48)
br label %52
52: ; preds = %50, %43
%53 = phi i32 [ 1, %50 ], [ %19, %43 ]
%54 = tail call %struct.dirent* @readdir(i32* %3)
%55 = icmp eq %struct.dirent* %54, null
br i1 %55, label %63, label %56
56: ; preds = %52, %56
%57 = phi %struct.dirent* [ %61, %56 ], [ %54, %52 ]
%58 = getelementptr inbounds %struct.dirent, %struct.dirent* %57, i64 0, i32 0
%59 = load i8*, i8** %58
%60 = tail call i32 @puts(i8* dereferenceable(1) %59)
%61 = tail call %struct.dirent* @readdir(i32* %3)
%62 = icmp eq %struct.dirent* %61, null
br i1 %62, label %63, label %56
63: ; preds = %56, %52
%64 = tail call i32 @rewinddir(i32* %3)
%65 = tail call i8* @telldir(i32* %3)
%66 = ptrtoint i8* %65 to i64
%67 = trunc i64 %66 to i32
%68 = icmp eq i32 %67, -1
br i1 %68, label %69, label %73
69: ; preds = %63
%70 = load i32, i32* @errno
%71 = tail call i8* @strerror(i32 %70)
%72 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8* %71)
br label %77
73: ; preds = %63
%74 = icmp eq i32 %12, %67
br i1 %74, label %77, label %75
75: ; preds = %73
%76 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([40 x i8], [40 x i8]* @str.7, i64 0, i64 0))
br label %77
77: ; preds = %73, %75, %69
%78 = phi i32 [ 1, %69 ], [ 1, %75 ], [ %53, %73 ]
%79 = tail call i32 @closedir(i32* %3)
br label %80
80: ; preds = %77, %5
%81 = phi i32 [ 1, %5 ], [ %78, %77 ]
ret i32 %81
}