#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*/ 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;
  dirp = opendir(".");
  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);
  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    0xe6d(%rip),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1060 <readdir@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
je     1213 <main+0xb3>
mov    -0x2c(%rbp),%eax
mov    %eax,%ecx
add    $0x1,%ecx
mov    %ecx,-0x2c(%rbp)
cmp    $0x3,%eax
jne    11d5 <main+0x75>
mov    -0x18(%rbp),%rdi
call   1080 <telldir@plt>
mov    %rax,-0x20(%rbp)
mov    -0x38(%rbp),%rax
mov    (%rax),%rsi
lea    0xe1f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
cmpl   $0x190,-0x2c(%rbp)
jne    11fc <main+0x9c>
jmp    1213 <main+0xb3>
jmp    1201 <main+0xa1>
mov    -0x18(%rbp),%rdi
call   1060 <readdir@plt>
mov    %rax,-0x38(%rbp)
jmp    11a9 <main+0x49>
lea    0xdec(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1070 <seekdir@plt>
mov    -0x18(%rbp),%rdi
call   1080 <telldir@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x20(%rbp),%rax
je     1266 <main+0x106>
mov    -0x20(%rbp),%rsi
mov    -0x28(%rbp),%rdx
lea    0xdcd(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x30(%rbp)
mov    -0x18(%rbp),%rdi
call   1060 <readdir@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
je     12a5 <main+0x145>
mov    -0x38(%rbp),%rax
mov    (%rax),%rsi
lea    0xd76(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1060 <readdir@plt>
mov    %rax,-0x38(%rbp)
jmp    1273 <main+0x113>
mov    -0x18(%rbp),%rdi
call   1050 <closedir@plt>
mov    -0x30(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
lea    0xec3(%rip),%rdi
call   1040 <opendir@plt>
mov    %rax,%r12
mov    %rax,%rdi
call   1070 <readdir@plt>
xor    %r14d,%r14d
test   %rax,%rax
je     11ec <main+0x7c>
mov    %rax,%rbx
mov    $0x18f,%ebp
xor    %r15d,%r15d
jmp    11d6 <main+0x66>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %r12,%rdi
call   1090 <telldir@plt>
mov    %rax,%r15
mov    (%rbx),%rdi
call   1030 <puts@plt>
mov    %r12,%rdi
call   1070 <readdir@plt>
mov    %rax,%rbx
add    $0xffffffff,%ebp
test   %rax,%rax
je     11ef <main+0x7f>
cmp    $0x18c,%ebp
je     11b0 <main+0x40>
mov    (%rbx),%rdi
call   1030 <puts@plt>
test   %ebp,%ebp
jne    11c3 <main+0x53>
jmp    11ef <main+0x7f>
xor    %r15d,%r15d
lea    0xe30(%rip),%rdi
call   1030 <puts@plt>
mov    %r12,%rdi
mov    %r15,%rsi
call   1080 <seekdir@plt>
mov    %r12,%rdi
call   1090 <telldir@plt>
cmp    %r15,%rax
je     1238 <main+0xc8>
lea    0xde6(%rip),%rdi
mov    %r15,%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%r14d
jmp    1238 <main+0xc8>
nop
mov    (%rax),%rdi
call   1030 <puts@plt>
mov    %r12,%rdi
call   1070 <readdir@plt>
test   %rax,%rax
jne    1230 <main+0xc0>
mov    %r12,%rdi
call   1060 <closedir@plt>
mov    %r14d,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.dirent = type { i8* }
@.str = [2 x i8] c".\00"
@.str.3 = [38 x i8] c"seekdir (d, %ld); telldir (d) == %ld\0A\00"
@str = [30 x i8] c"going back past 4-th entry...\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 = tail call %struct.dirent* @readdir(i32* %3)
%5 = icmp eq %struct.dirent* %4, null
br i1 %5, label %26, label %6
6: ; preds = %2, %22
%7 = phi %struct.dirent* [ %24, %22 ], [ %4, %2 ]
%8 = phi i32 [ %10, %22 ], [ 0, %2 ]
%9 = phi i64 [ %23, %22 ], [ 0, %2 ]
%10 = add nuw nsw i32 %8, 1
%11 = icmp eq i32 %8, 3
br i1 %11, label %12, label %17
12: ; preds = %6
%13 = tail call i64 @telldir(i32* %3)
%14 = getelementptr inbounds %struct.dirent, %struct.dirent* %7, i64 0, i32 0
%15 = load i8*, i8** %14
%16 = tail call i32 @puts(i8* dereferenceable(1) %15)
br label %22
17: ; preds = %6
%18 = getelementptr inbounds %struct.dirent, %struct.dirent* %7, i64 0, i32 0
%19 = load i8*, i8** %18
%20 = tail call i32 @puts(i8* dereferenceable(1) %19)
%21 = icmp eq i32 %10, 400
br i1 %21, label %26, label %22
22: ; preds = %12, %17
%23 = phi i64 [ %13, %12 ], [ %9, %17 ]
%24 = tail call %struct.dirent* @readdir(i32* %3)
%25 = icmp eq %struct.dirent* %24, null
br i1 %25, label %26, label %6
26: ; preds = %22, %17, %2
%27 = phi i64 [ 0, %2 ], [ %9, %17 ], [ %23, %22 ]
%28 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @str, i64 0, i64 0))
%29 = tail call i32 @seekdir(i32* %3, i64 %27)
%30 = tail call i64 @telldir(i32* %3)
%31 = icmp eq i64 %30, %27
br i1 %31, label %34, label %32
32: ; preds = %26
%33 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @.str.3, i64 0, i64 0), i64 %27, i64 %30)
br label %34
34: ; preds = %32, %26
%35 = phi i32 [ 1, %32 ], [ 0, %26 ]
%36 = tail call %struct.dirent* @readdir(i32* %3)
%37 = icmp eq %struct.dirent* %36, null
br i1 %37, label %45, label %38
38: ; preds = %34, %38
%39 = phi %struct.dirent* [ %43, %38 ], [ %36, %34 ]
%40 = getelementptr inbounds %struct.dirent, %struct.dirent* %39, i64 0, i32 0
%41 = load i8*, i8** %40
%42 = tail call i32 @puts(i8* dereferenceable(1) %41)
%43 = tail call %struct.dirent* @readdir(i32* %3)
%44 = icmp eq %struct.dirent* %43, null
br i1 %44, label %45, label %38
45: ; preds = %38, %34
%46 = tail call i32 @closedir(i32* %3)
ret i32 %35
}