#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 *);
int /*<<< orphan*/ printf(char *, ...);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ rewinddir(int /*<<< orphan*/ *);
int /*<<< orphan*/ seekdir(int /*<<< orphan*/ *, long);
long 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;
  long int save0;
  long int rewind;
  dirp = opendir(".");
  if (dirp == NULL) {
    printf("opendir failed: %m\n");
    return 1;
  }
  save0 = telldir(dirp);
  if (save0 == -1) {
    printf("telldir failed: %m\n");
    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 = telldir(dirp);
  if (rewind == -1) {
    printf("telldir failed: %m\n");
    result = 1;
  } else if (save0 != rewind) {
    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    $0x50,%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    0xe5d(%rip),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11d1 <main+0x61>
lea    0xe44(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1379 <main+0x209>
mov    -0x18(%rbp),%rdi
call   1090 <telldir@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0xffffffffffffffff,-0x40(%rbp)
jne    11fe <main+0x8e>
lea    0xe26(%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     1275 <main+0x105>
mov    -0x2c(%rbp),%eax
mov    %eax,%ecx
add    $0x1,%ecx
mov    %ecx,-0x2c(%rbp)
cmp    $0x3,%eax
jne    1237 <main+0xc7>
mov    -0x18(%rbp),%rdi
call   1090 <telldir@plt>
mov    %rax,-0x20(%rbp)
mov    -0x38(%rbp),%rax
mov    (%rax),%rsi
lea    0xde5(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
cmpl   $0x190,-0x2c(%rbp)
jne    125e <main+0xee>
jmp    1275 <main+0x105>
jmp    1263 <main+0xf3>
mov    -0x18(%rbp),%rdi
call   1070 <readdir@plt>
mov    %rax,-0x38(%rbp)
jmp    120b <main+0x9b>
lea    0xdb2(%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     12c8 <main+0x158>
mov    -0x20(%rbp),%rsi
mov    -0x28(%rbp),%rdx
lea    0xd93(%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     1307 <main+0x197>
mov    -0x38(%rbp),%rax
mov    (%rax),%rsi
lea    0xd3c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1070 <readdir@plt>
mov    %rax,-0x38(%rbp)
jmp    12d5 <main+0x165>
mov    -0x18(%rbp),%rdi
call   1050 <rewinddir@plt>
mov    -0x18(%rbp),%rdi
call   1090 <telldir@plt>
mov    %rax,-0x48(%rbp)
cmpq   $0xffffffffffffffff,-0x48(%rbp)
jne    1342 <main+0x1d2>
lea    0xce7(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x30(%rbp)
jmp    136a <main+0x1fa>
mov    -0x40(%rbp),%rax
cmp    -0x48(%rbp),%rax
je     1365 <main+0x1f5>
lea    0xd1c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x30(%rbp)
jmp    136a <main+0x1fa>
mov    -0x18(%rbp),%rdi
call   1060 <closedir@plt>
mov    -0x30(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
lea    0xed8(%rip),%rdi
call   1040 <opendir@plt>
test   %rax,%rax
je     122e <main+0xae>
mov    %rax,%r13
mov    %rax,%rdi
call   10a0 <telldir@plt>
mov    %rax,%r14
xor    %r15d,%r15d
cmp    $0xffffffffffffffff,%rax
jne    11cb <main+0x4b>
lea    0xe56(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%r15d
mov    %r13,%rdi
call   1080 <readdir@plt>
test   %rax,%rax
je     1247 <main+0xc7>
mov    %rax,%rbp
mov    $0x18f,%ebx
xor    %r12d,%r12d
jmp    1217 <main+0x97>
cs nopw 0x0(%rax,%rax,1)
nop
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     124a <main+0xca>
cmp    $0x18c,%ebx
je     11f0 <main+0x70>
mov    0x0(%rbp),%rdi
call   1030 <puts@plt>
test   %ebx,%ebx
jne    1204 <main+0x84>
jmp    124a <main+0xca>
lea    0xdcb(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%r15d
jmp    12ea <main+0x16a>
xor    %r12d,%r12d
lea    0xdfd(%rip),%rdi
call   1030 <puts@plt>
mov    %r13,%rdi
mov    %r12,%rsi
call   1090 <seekdir@plt>
mov    %r13,%rdi
call   10a0 <telldir@plt>
cmp    %r12,%rax
je     1298 <main+0x118>
lea    0xdb3(%rip),%rdi
mov    %r12,%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%r15d
jmp    1298 <main+0x118>
nopw   0x0(%rax,%rax,1)
mov    (%rax),%rdi
call   1030 <puts@plt>
mov    %r13,%rdi
call   1080 <readdir@plt>
test   %rax,%rax
jne    1290 <main+0x110>
mov    %r13,%rdi
call   1060 <rewinddir@plt>
mov    %r13,%rdi
call   10a0 <telldir@plt>
cmp    $0xffffffffffffffff,%rax
je     12ce <main+0x14e>
cmp    %rax,%r14
je     12e2 <main+0x162>
lea    0xda5(%rip),%rdi
call   1030 <puts@plt>
jmp    12dc <main+0x15c>
lea    0xd3f(%rip),%rdi
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: %m\0A\00"
@.str.2 = [20 x i8] c"telldir failed: %m\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 i64
%13 = alloca i64
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 %19
17: ; preds = %2
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %3
br label %99
19: ; preds = %2
%20 = load i32*, i32** %6
%21 = call i64 @telldir(i32* %20)
store i64 %21, i64* %12
%22 = load i64, i64* %12
%23 = icmp eq i64 %22, -1
br i1 %23, label %24, label %26
24: ; preds = %19
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %10
br label %26
26: ; preds = %24, %19
%27 = load i32*, i32** %6
%28 = call %struct.dirent* @readdir(i32* %27)
store %struct.dirent* %28, %struct.dirent** %11
br label %29
29: ; preds = %48, %26
%30 = load %struct.dirent*, %struct.dirent** %11
%31 = icmp ne %struct.dirent* %30, null
br i1 %31, label %32, label %51
32: ; preds = %29
%33 = load i32, i32* %9
%34 = add nsw i32 %33, 1
store i32 %34, i32* %9
%35 = icmp eq i32 %33, 3
br i1 %35, label %36, label %39
36: ; preds = %32
%37 = load i32*, i32** %6
%38 = call i64 @telldir(i32* %37)
store i64 %38, i64* %7
br label %39
39: ; preds = %36, %32
%40 = load %struct.dirent*, %struct.dirent** %11
%41 = getelementptr inbounds %struct.dirent, %struct.dirent* %40, i32 0, i32 0
%42 = load i8*, i8** %41
%43 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* %42)
%44 = load i32, i32* %9
%45 = icmp eq i32 %44, 400
br i1 %45, label %46, label %47
46: ; preds = %39
br label %51
47: ; preds = %39
br label %48
48: ; preds = %47
%49 = load i32*, i32** %6
%50 = call %struct.dirent* @readdir(i32* %49)
store %struct.dirent* %50, %struct.dirent** %11
br label %29
51: ; preds = %46, %29
%52 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.4, i64 0, i64 0))
%53 = load i32*, i32** %6
%54 = load i64, i64* %7
%55 = call i32 @seekdir(i32* %53, i64 %54)
%56 = load i32*, i32** %6
%57 = call i64 @telldir(i32* %56)
store i64 %57, i64* %8
%58 = load i64, i64* %8
%59 = load i64, i64* %7
%60 = icmp ne i64 %58, %59
br i1 %60, label %61, label %65
61: ; preds = %51
%62 = load i64, i64* %7
%63 = load i64, i64* %8
%64 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.5, i64 0, i64 0), i64 %62, i64 %63)
store i32 1, i32* %10
br label %65
65: ; preds = %61, %51
%66 = load i32*, i32** %6
%67 = call %struct.dirent* @readdir(i32* %66)
store %struct.dirent* %67, %struct.dirent** %11
br label %68
68: ; preds = %76, %65
%69 = load %struct.dirent*, %struct.dirent** %11
%70 = icmp ne %struct.dirent* %69, null
br i1 %70, label %71, label %79
71: ; preds = %68
%72 = load %struct.dirent*, %struct.dirent** %11
%73 = getelementptr inbounds %struct.dirent, %struct.dirent* %72, i32 0, i32 0
%74 = load i8*, i8** %73
%75 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* %74)
br label %76
76: ; preds = %71
%77 = load i32*, i32** %6
%78 = call %struct.dirent* @readdir(i32* %77)
store %struct.dirent* %78, %struct.dirent** %11
br label %68
79: ; preds = %68
%80 = load i32*, i32** %6
%81 = call i32 @rewinddir(i32* %80)
%82 = load i32*, i32** %6
%83 = call i64 @telldir(i32* %82)
store i64 %83, i64* %13
%84 = load i64, i64* %13
%85 = icmp eq i64 %84, -1
br i1 %85, label %86, label %88
86: ; preds = %79
%87 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %10
br label %95
88: ; preds = %79
%89 = load i64, i64* %12
%90 = load i64, i64* %13
%91 = icmp ne i64 %89, %90
br i1 %91, label %92, label %94
92: ; preds = %88
%93 = 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 %94
94: ; preds = %92, %88
br label %95
95: ; preds = %94, %86
%96 = load i32*, i32** %6
%97 = call i32 @closedir(i32* %96)
%98 = load i32, i32* %10
store i32 %98, i32* %3
br label %99
99: ; preds = %95, %17
%100 = load i32, i32* %3
ret i32 %100
}

LLVM IR (-O2):
%struct.dirent = type { i8* }
@.str = [2 x i8] c".\00"
@.str.1 = [20 x i8] c"opendir failed: %m\0A\00"
@.str.2 = [20 x i8] c"telldir failed: %m\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 %7
5: ; preds = %2
%6 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
br label %68
7: ; preds = %2
%8 = tail call i64 @telldir(i32* %3)
%9 = icmp eq i64 %8, -1
br i1 %9, label %10, label %12
10: ; preds = %7
%11 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
br label %12
12: ; preds = %10, %7
%13 = phi i32 [ 1, %10 ], [ 0, %7 ]
%14 = tail call %struct.dirent* @readdir(i32* %3)
%15 = icmp eq %struct.dirent* %14, null
br i1 %15, label %36, label %16
16: ; preds = %12, %32
%17 = phi i64 [ %33, %32 ], [ 0, %12 ]
%18 = phi %struct.dirent* [ %34, %32 ], [ %14, %12 ]
%19 = phi i32 [ %20, %32 ], [ 0, %12 ]
%20 = add nuw nsw i32 %19, 1
%21 = icmp eq i32 %19, 3
br i1 %21, label %22, label %27
22: ; preds = %16
%23 = tail call i64 @telldir(i32* %3)
%24 = getelementptr inbounds %struct.dirent, %struct.dirent* %18, i64 0, i32 0
%25 = load i8*, i8** %24
%26 = tail call i32 @puts(i8* dereferenceable(1) %25)
br label %32
27: ; preds = %16
%28 = getelementptr inbounds %struct.dirent, %struct.dirent* %18, i64 0, i32 0
%29 = load i8*, i8** %28
%30 = tail call i32 @puts(i8* dereferenceable(1) %29)
%31 = icmp eq i32 %20, 400
br i1 %31, label %36, label %32
32: ; preds = %22, %27
%33 = phi i64 [ %23, %22 ], [ %17, %27 ]
%34 = tail call %struct.dirent* @readdir(i32* %3)
%35 = icmp eq %struct.dirent* %34, null
br i1 %35, label %36, label %16
36: ; preds = %32, %27, %12
%37 = phi i64 [ 0, %12 ], [ %17, %27 ], [ %33, %32 ]
%38 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @str, i64 0, i64 0))
%39 = tail call i32 @seekdir(i32* %3, i64 %37)
%40 = tail call i64 @telldir(i32* %3)
%41 = icmp eq i64 %40, %37
br i1 %41, label %44, label %42
42: ; preds = %36
%43 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @.str.5, i64 0, i64 0), i64 %37, i64 %40)
br label %44
44: ; preds = %42, %36
%45 = phi i32 [ 1, %42 ], [ %13, %36 ]
%46 = tail call %struct.dirent* @readdir(i32* %3)
%47 = icmp eq %struct.dirent* %46, null
br i1 %47, label %55, label %48
48: ; preds = %44, %48
%49 = phi %struct.dirent* [ %53, %48 ], [ %46, %44 ]
%50 = getelementptr inbounds %struct.dirent, %struct.dirent* %49, i64 0, i32 0
%51 = load i8*, i8** %50
%52 = tail call i32 @puts(i8* dereferenceable(1) %51)
%53 = tail call %struct.dirent* @readdir(i32* %3)
%54 = icmp eq %struct.dirent* %53, null
br i1 %54, label %55, label %48
55: ; preds = %48, %44
%56 = tail call i32 @rewinddir(i32* %3)
%57 = tail call i64 @telldir(i32* %3)
%58 = icmp eq i64 %57, -1
br i1 %58, label %59, label %61
59: ; preds = %55
%60 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
br label %65
61: ; preds = %55
%62 = icmp eq i64 %8, %57
br i1 %62, label %65, label %63
63: ; preds = %61
%64 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([40 x i8], [40 x i8]* @str.7, i64 0, i64 0))
br label %65
65: ; preds = %61, %63, %59
%66 = phi i32 [ 1, %59 ], [ 1, %63 ], [ %45, %61 ]
%67 = tail call i32 @closedir(i32* %3)
br label %68
68: ; preds = %65, %5
%69 = phi i32 [ 1, %5 ], [ %66, %65 ]
ret i32 %69
}