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

/* Variables and functions */
int /*<<< orphan*/ err(int, char *, ...);
scalar_t__ errno;
int /*<<< orphan*/ errx(int, char *);
char *malloc(int);
scalar_t__ mkdir(char const *, int);
scalar_t__ mknod(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ *opendir(char const *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ rewinddir(int /*<<< orphan*/ *);
scalar_t__ strcmp(int /*<<< orphan*/, char const *);
int /*<<< orphan*/ strcpy(char *, char const *);
int strlen(char const *);
int /*<<< orphan*/ warnx(char *, char const *, ...);
int main(int argc, char **argv) {
  const char kDirPath[] = "rewinddir_test_dir";
  const char kFileBasename[] = "rewinddir_test_file";
  if (mkdir(kDirPath, 0755) < 0) {
    err(1, "mkdir(%s)", kDirPath);
  }
  DIR *dir = opendir(kDirPath);
  if (!dir) {
    err(1, "opendir(%s)", kDirPath);
  }
  int failed = 0;
  while (1) {
    errno = 0;
    struct dirent *d = readdir(dir);
    if (!d) {
      if (errno != 0) {
        err(1, "readdir");
      }
      break;
    }
    if (strcmp(d->d_name, ".") != 0 && strcmp(d->d_name, "..") != 0) {
      warnx("unexpected file %s in new directory", d->d_name);
      failed = 1;
    }
  }
  char *file_path = malloc(strlen(kDirPath) + 1 + strlen(kFileBasename));
  if (!file_path) {
    errx(1, "malloc");
  }
  strcpy(file_path, kDirPath);
  file_path[strlen(kDirPath)] = '/';
  strcpy(file_path + strlen(kDirPath) + 1, kFileBasename);
  if (mknod(file_path, 0644, 0) < 0) {
    err(1, "mknod(%s)", file_path);
  }
  rewinddir(dir);
  size_t found_file = 0;
  while (1) {
    errno = 0;
    struct dirent *d = readdir(dir);
    if (!d) {
      if (errno != 0) {
        err(1, "readdir");
      }
      break;
    }
    if (strcmp(d->d_name, kFileBasename) == 0) {
      found_file++;
    } else if (strcmp(d->d_name, ".") != 0 && strcmp(d->d_name, "..") != 0) {
      warnx("unexpected file %s in new directory", d->d_name);
      failed = 1;
    }
  }
  if (found_file != 1) {
    warnx("readdir returned file %s %zu times, wanted 1", kFileBasename,
          found_file);
    failed = 1;
  }
  return failed;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xa0,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0xe20(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    0xe1d(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    0xe1a(%rip),%ax
mov    %ax,-0x20(%rbp)
mov    0xe12(%rip),%al
mov    %al,-0x1e(%rbp)
mov    0xe16(%rip),%rax
mov    %rax,-0x50(%rbp)
mov    0xe13(%rip),%rax
mov    %rax,-0x48(%rbp)
mov    0xe11(%rip),%eax
mov    %eax,-0x40(%rbp)
lea    -0x30(%rbp),%rdi
mov    $0x1ed,%esi
call   1040 <mkdir@plt>
cmp    $0x0,%rax
jge    1251 <main+0x91>
lea    -0x30(%rbp),%rdx
mov    $0x1,%edi
lea    0xdea(%rip),%rsi
mov    $0x0,%al
call   10d0 <err@plt>
lea    -0x30(%rbp),%rdi
call   1050 <opendir@plt>
mov    %rax,-0x58(%rbp)
cmpq   $0x0,-0x58(%rbp)
jne    1280 <main+0xc0>
lea    -0x30(%rbp),%rdx
mov    $0x1,%edi
lea    0xdc5(%rip),%rsi
mov    $0x0,%al
call   10d0 <err@plt>
movl   $0x0,-0x5c(%rbp)
mov    0x2d52(%rip),%rax
movq   $0x0,(%rax)
mov    -0x58(%rbp),%rdi
call   10c0 <readdir@plt>
mov    %rax,-0x68(%rbp)
cmpq   $0x0,-0x68(%rbp)
jne    12d6 <main+0x116>
mov    0x2d2c(%rip),%rax
cmpq   $0x0,(%rax)
je     12d1 <main+0x111>
mov    $0x1,%edi
lea    0xd80(%rip),%rsi
mov    $0x0,%al
call   10d0 <err@plt>
jmp    132f <main+0x16f>
mov    -0x68(%rbp),%rax
mov    (%rax),%edi
lea    0xd70(%rip),%rsi
call   10a0 <strcmp@plt>
cmp    $0x0,%rax
je     132a <main+0x16a>
mov    -0x68(%rbp),%rax
mov    (%rax),%edi
lea    0xd53(%rip),%rsi
call   10a0 <strcmp@plt>
cmp    $0x0,%rax
je     132a <main+0x16a>
mov    -0x68(%rbp),%rax
movslq (%rax),%rsi
lea    0xd39(%rip),%rdi
mov    $0x0,%al
call   1080 <warnx@plt>
movl   $0x1,-0x5c(%rbp)
jmp    1287 <main+0xc7>
lea    -0x30(%rbp),%rdi
call   1060 <strlen@plt>
add    $0x1,%eax
mov    %eax,-0x84(%rbp)
lea    -0x50(%rbp),%rdi
call   1060 <strlen@plt>
mov    -0x84(%rbp),%edi
add    %eax,%edi
call   10e0 <malloc@plt>
mov    %rax,-0x70(%rbp)
cmpq   $0x0,-0x70(%rbp)
jne    1377 <main+0x1b7>
mov    $0x1,%edi
lea    0xd07(%rip),%rsi
call   1070 <errx@plt>
mov    -0x70(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x70(%rbp),%rax
mov    %rax,-0x98(%rbp)
lea    -0x30(%rbp),%rdi
call   1060 <strlen@plt>
mov    %eax,%ecx
mov    -0x98(%rbp),%rax
movslq %ecx,%rcx
movb   $0x2f,(%rax,%rcx,1)
mov    -0x70(%rbp),%rax
mov    %rax,-0x90(%rbp)
lea    -0x30(%rbp),%rdi
call   1060 <strlen@plt>
mov    -0x90(%rbp),%rdi
cltq
add    %rax,%rdi
add    $0x1,%rdi
lea    -0x50(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x70(%rbp),%rdi
mov    $0x1a4,%esi
xor    %edx,%edx
call   10b0 <mknod@plt>
cmp    $0x0,%rax
jge    1406 <main+0x246>
mov    -0x70(%rbp),%rdx
mov    $0x1,%edi
lea    0xc81(%rip),%rsi
mov    $0x0,%al
call   10d0 <err@plt>
mov    -0x58(%rbp),%rdi
call   1090 <rewinddir@plt>
movq   $0x0,-0x78(%rbp)
mov    0x2bc2(%rip),%rax
movq   $0x0,(%rax)
mov    -0x58(%rbp),%rdi
call   10c0 <readdir@plt>
mov    %rax,-0x80(%rbp)
cmpq   $0x0,-0x80(%rbp)
jne    1466 <main+0x2a6>
mov    0x2b9c(%rip),%rax
cmpq   $0x0,(%rax)
je     1461 <main+0x2a1>
mov    $0x1,%edi
lea    0xbf0(%rip),%rsi
mov    $0x0,%al
call   10d0 <err@plt>
jmp    14ee <main+0x32e>
mov    -0x80(%rbp),%rax
mov    (%rax),%edi
lea    -0x50(%rbp),%rsi
call   10a0 <strcmp@plt>
cmp    $0x0,%rax
jne    1490 <main+0x2d0>
mov    -0x78(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x78(%rbp)
jmp    14e9 <main+0x329>
mov    -0x80(%rbp),%rax
mov    (%rax),%edi
lea    0xbb6(%rip),%rsi
call   10a0 <strcmp@plt>
cmp    $0x0,%rax
je     14e4 <main+0x324>
mov    -0x80(%rbp),%rax
mov    (%rax),%edi
lea    0xb99(%rip),%rsi
call   10a0 <strcmp@plt>
cmp    $0x0,%rax
je     14e4 <main+0x324>
mov    -0x80(%rbp),%rax
movslq (%rax),%rsi
lea    0xb7f(%rip),%rdi
mov    $0x0,%al
call   1080 <warnx@plt>
movl   $0x1,-0x5c(%rbp)
jmp    14e9 <main+0x329>
jmp    1417 <main+0x257>
cmpq   $0x1,-0x78(%rbp)
je     1516 <main+0x356>
lea    -0x50(%rbp),%rsi
mov    -0x78(%rbp),%rdx
lea    0xb82(%rip),%rdi
mov    $0x0,%al
call   1080 <warnx@plt>
movl   $0x1,-0x5c(%rbp)
mov    -0x5c(%rbp),%eax
add    $0xa0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x48,%rsp
movaps 0xe2b(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
movl   $0x726964,0x1f(%rsp)
movaps 0xe37(%rip),%xmm0
movaps %xmm0,0x30(%rsp)
movl   $0x656c69,0x40(%rsp)
lea    0x10(%rsp),%rdi
mov    $0x1ed,%esi
call   1040 <mkdir@plt>
test   %rax,%rax
jns    1222 <main+0x62>
lea    0xe23(%rip),%rsi
lea    0x10(%rsp),%rdx
mov    $0x1,%edi
xor    %eax,%eax
call   10d0 <err@plt>
lea    0x10(%rsp),%rdi
call   1050 <opendir@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    124c <main+0x8c>
lea    0xe03(%rip),%rsi
lea    0x10(%rsp),%rdx
mov    $0x1,%edi
xor    %eax,%eax
call   10d0 <err@plt>
mov    0x2d8d(%rip),%r14
movq   $0x0,(%r14)
mov    %rbx,%rdi
call   10c0 <readdir@plt>
test   %rax,%rax
je     12df <main+0x11f>
mov    %rax,%rbp
lea    0xde2(%rip),%r13
lea    0xdda(%rip),%r15
lea    0xdd6(%rip),%r12
movl   $0x0,0xc(%rsp)
jmp    12a7 <main+0xe7>
nopl   0x0(%rax)
movq   $0x0,(%r14)
mov    %rbx,%rdi
call   10c0 <readdir@plt>
mov    %rax,%rbp
test   %rax,%rax
je     12e7 <main+0x127>
mov    0x0(%rbp),%edi
mov    %r13,%rsi
call   10a0 <strcmp@plt>
test   %rax,%rax
je     1290 <main+0xd0>
mov    0x0(%rbp),%edi
mov    %r15,%rsi
call   10a0 <strcmp@plt>
test   %rax,%rax
je     1290 <main+0xd0>
movslq 0x0(%rbp),%rsi
mov    %r12,%rdi
xor    %eax,%eax
call   1080 <warnx@plt>
movl   $0x1,0xc(%rsp)
jmp    1290 <main+0xd0>
movl   $0x0,0xc(%rsp)
cmpq   $0x0,(%r14)
je     1300 <main+0x140>
lea    0xd56(%rip),%rsi
mov    $0x1,%edi
xor    %eax,%eax
call   10d0 <err@plt>
lea    0x10(%rsp),%rdi
call   1060 <strlen@plt>
mov    %eax,%ebp
lea    0x30(%rsp),%rdi
call   1060 <strlen@plt>
lea    (%rax,%rbp,1),%edi
add    $0x1,%edi
call   10e0 <malloc@plt>
mov    %rax,%rbp
test   %rax,%rax
jne    133a <main+0x17a>
lea    0xd49(%rip),%rsi
mov    $0x1,%edi
call   1070 <errx@plt>
lea    0x10(%rsp),%r15
mov    %rbp,%rdi
mov    %r15,%rsi
call   1030 <strcpy@plt>
mov    %r15,%rdi
call   1060 <strlen@plt>
cltq
movb   $0x2f,0x0(%rbp,%rax,1)
mov    %r15,%rdi
call   1060 <strlen@plt>
cltq
lea    (%rax,%rbp,1),%rdi
add    $0x1,%rdi
lea    0x30(%rsp),%rsi
call   1030 <strcpy@plt>
mov    %rbp,%rdi
mov    $0x1a4,%esi
xor    %edx,%edx
call   10b0 <mknod@plt>
test   %rax,%rax
jns    139f <main+0x1df>
lea    0xcf0(%rip),%rsi
mov    $0x1,%edi
mov    %rbp,%rdx
xor    %eax,%eax
call   10d0 <err@plt>
mov    %rbx,%rdi
call   1090 <rewinddir@plt>
movq   $0x0,(%r14)
mov    %rbx,%rdi
call   10c0 <readdir@plt>
test   %rax,%rax
je     144e <main+0x28e>
mov    %rax,%r13
lea    0x30(%rsp),%r15
lea    0xc85(%rip),%r12
xor    %ebp,%ebp
jmp    13fb <main+0x23b>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
add    $0x1,%rbp
movq   $0x0,(%r14)
mov    %rbx,%rdi
call   10c0 <readdir@plt>
mov    %rax,%r13
test   %rax,%rax
je     1450 <main+0x290>
mov    0x0(%r13),%edi
mov    %r15,%rsi
call   10a0 <strcmp@plt>
test   %rax,%rax
je     13e0 <main+0x220>
mov    0x0(%r13),%edi
mov    %r12,%rsi
call   10a0 <strcmp@plt>
test   %rax,%rax
je     13e4 <main+0x224>
mov    0x0(%r13),%edi
lea    0xc2a(%rip),%rsi
call   10a0 <strcmp@plt>
test   %rax,%rax
je     13e4 <main+0x224>
movslq 0x0(%r13),%rsi
lea    0xc18(%rip),%rdi
xor    %eax,%eax
call   1080 <warnx@plt>
movl   $0x1,0xc(%rsp)
jmp    13e4 <main+0x224>
xor    %ebp,%ebp
cmpq   $0x0,(%r14)
je     1469 <main+0x2a9>
lea    0xbed(%rip),%rsi
mov    $0x1,%edi
xor    %eax,%eax
call   10d0 <err@plt>
cmp    $0x1,%rbp
mov    0xc(%rsp),%eax
je     148e <main+0x2ce>
lea    0xc10(%rip),%rdi
lea    0x30(%rsp),%rsi
mov    %rbp,%rdx
xor    %eax,%eax
call   1080 <warnx@plt>
mov    $0x1,%eax
add    $0x48,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.dirent = type { i32 }
@__const.main.kDirPath = [19 x i8] c"rewinddir_test_dir\00"
@__const.main.kFileBasename = [20 x i8] c"rewinddir_test_file\00"
@.str = [10 x i8] c"mkdir(%s)\00"
@.str.1 = [12 x i8] c"opendir(%s)\00"
@errno = global i64 0
@.str.2 = [8 x i8] c"readdir\00"
@.str.3 = [2 x i8] c".\00"
@.str.4 = [3 x i8] c"..\00"
@.str.5 = [36 x i8] c"unexpected file %s in new directory\00"
@.str.6 = [7 x i8] c"malloc\00"
@.str.7 = [10 x i8] c"mknod(%s)\00"
@.str.8 = [45 x i8] c"readdir returned file %s %zu times, wanted 1\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [19 x i8]
%7 = alloca [20 x i8]
%8 = alloca i32*
%9 = alloca i32
%10 = alloca %struct.dirent*
%11 = alloca i8*
%12 = alloca i64
%13 = alloca %struct.dirent*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%14 = bitcast [19 x i8]* %6 to i8*
%15 = bitcast [20 x i8]* %7 to i8*
%16 = getelementptr inbounds [19 x i8], [19 x i8]* %6, i64 0, i64 0
%17 = call i64 @mkdir(i8* %16, i32 493)
%18 = icmp slt i64 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %2
%20 = getelementptr inbounds [19 x i8], [19 x i8]* %6, i64 0, i64 0
%21 = call i32 (i32, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* %20)
br label %22
22: ; preds = %19, %2
%23 = getelementptr inbounds [19 x i8], [19 x i8]* %6, i64 0, i64 0
%24 = call i32* @opendir(i8* %23)
store i32* %24, i32** %8
%25 = load i32*, i32** %8
%26 = icmp ne i32* %25, null
br i1 %26, label %30, label %27
27: ; preds = %22
%28 = getelementptr inbounds [19 x i8], [19 x i8]* %6, i64 0, i64 0
%29 = call i32 (i32, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i8* %28)
br label %30
30: ; preds = %27, %22
store i32 0, i32* %9
br label %31
31: ; preds = %30, %61
store i64 0, i64* @errno
%32 = load i32*, i32** %8
%33 = call %struct.dirent* @readdir(i32* %32)
store %struct.dirent* %33, %struct.dirent** %10
%34 = load %struct.dirent*, %struct.dirent** %10
%35 = icmp ne %struct.dirent* %34, null
br i1 %35, label %42, label %36
36: ; preds = %31
%37 = load i64, i64* @errno
%38 = icmp ne i64 %37, 0
br i1 %38, label %39, label %41
39: ; preds = %36
%40 = call i32 (i32, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
br label %41
41: ; preds = %39, %36
br label %62
42: ; preds = %31
%43 = load %struct.dirent*, %struct.dirent** %10
%44 = getelementptr inbounds %struct.dirent, %struct.dirent* %43, i32 0, i32 0
%45 = load i32, i32* %44
%46 = call i64 @strcmp(i32 %45, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%47 = icmp ne i64 %46, 0
br i1 %47, label %48, label %61
48: ; preds = %42
%49 = load %struct.dirent*, %struct.dirent** %10
%50 = getelementptr inbounds %struct.dirent, %struct.dirent* %49, i32 0, i32 0
%51 = load i32, i32* %50
%52 = call i64 @strcmp(i32 %51, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
%53 = icmp ne i64 %52, 0
br i1 %53, label %54, label %61
54: ; preds = %48
%55 = load %struct.dirent*, %struct.dirent** %10
%56 = getelementptr inbounds %struct.dirent, %struct.dirent* %55, i32 0, i32 0
%57 = load i32, i32* %56
%58 = sext i32 %57 to i64
%59 = inttoptr i64 %58 to i8*
%60 = call i32 (i8*, i8*, ...) @warnx(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.5, i64 0, i64 0), i8* %59)
store i32 1, i32* %9
br label %61
61: ; preds = %54, %48, %42
br label %31
62: ; preds = %41
%63 = getelementptr inbounds [19 x i8], [19 x i8]* %6, i64 0, i64 0
%64 = call i32 @strlen(i8* %63)
%65 = add nsw i32 %64, 1
%66 = getelementptr inbounds [20 x i8], [20 x i8]* %7, i64 0, i64 0
%67 = call i32 @strlen(i8* %66)
%68 = add nsw i32 %65, %67
%69 = call i8* @malloc(i32 %68)
store i8* %69, i8** %11
%70 = load i8*, i8** %11
%71 = icmp ne i8* %70, null
br i1 %71, label %74, label %72
72: ; preds = %62
%73 = call i32 @errx(i32 1, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.6, i64 0, i64 0))
br label %74
74: ; preds = %72, %62
%75 = load i8*, i8** %11
%76 = getelementptr inbounds [19 x i8], [19 x i8]* %6, i64 0, i64 0
%77 = call i32 @strcpy(i8* %75, i8* %76)
%78 = load i8*, i8** %11
%79 = getelementptr inbounds [19 x i8], [19 x i8]* %6, i64 0, i64 0
%80 = call i32 @strlen(i8* %79)
%81 = sext i32 %80 to i64
%82 = getelementptr inbounds i8, i8* %78, i64 %81
store i8 47, i8* %82
%83 = load i8*, i8** %11
%84 = getelementptr inbounds [19 x i8], [19 x i8]* %6, i64 0, i64 0
%85 = call i32 @strlen(i8* %84)
%86 = sext i32 %85 to i64
%87 = getelementptr inbounds i8, i8* %83, i64 %86
%88 = getelementptr inbounds i8, i8* %87, i64 1
%89 = getelementptr inbounds [20 x i8], [20 x i8]* %7, i64 0, i64 0
%90 = call i32 @strcpy(i8* %88, i8* %89)
%91 = load i8*, i8** %11
%92 = call i64 @mknod(i8* %91, i32 420, i32 0)
%93 = icmp slt i64 %92, 0
br i1 %93, label %94, label %97
94: ; preds = %74
%95 = load i8*, i8** %11
%96 = call i32 (i32, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.7, i64 0, i64 0), i8* %95)
br label %97
97: ; preds = %94, %74
%98 = load i32*, i32** %8
%99 = call i32 @rewinddir(i32* %98)
store i64 0, i64* %12
br label %100
100: ; preds = %97, %141
store i64 0, i64* @errno
%101 = load i32*, i32** %8
%102 = call %struct.dirent* @readdir(i32* %101)
store %struct.dirent* %102, %struct.dirent** %13
%103 = load %struct.dirent*, %struct.dirent** %13
%104 = icmp ne %struct.dirent* %103, null
br i1 %104, label %111, label %105
105: ; preds = %100
%106 = load i64, i64* @errno
%107 = icmp ne i64 %106, 0
br i1 %107, label %108, label %110
108: ; preds = %105
%109 = call i32 (i32, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
br label %110
110: ; preds = %108, %105
br label %142
111: ; preds = %100
%112 = load %struct.dirent*, %struct.dirent** %13
%113 = getelementptr inbounds %struct.dirent, %struct.dirent* %112, i32 0, i32 0
%114 = load i32, i32* %113
%115 = getelementptr inbounds [20 x i8], [20 x i8]* %7, i64 0, i64 0
%116 = call i64 @strcmp(i32 %114, i8* %115)
%117 = icmp eq i64 %116, 0
br i1 %117, label %118, label %121
118: ; preds = %111
%119 = load i64, i64* %12
%120 = add i64 %119, 1
store i64 %120, i64* %12
br label %141
121: ; preds = %111
%122 = load %struct.dirent*, %struct.dirent** %13
%123 = getelementptr inbounds %struct.dirent, %struct.dirent* %122, i32 0, i32 0
%124 = load i32, i32* %123
%125 = call i64 @strcmp(i32 %124, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%126 = icmp ne i64 %125, 0
br i1 %126, label %127, label %140
127: ; preds = %121
%128 = load %struct.dirent*, %struct.dirent** %13
%129 = getelementptr inbounds %struct.dirent, %struct.dirent* %128, i32 0, i32 0
%130 = load i32, i32* %129
%131 = call i64 @strcmp(i32 %130, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
%132 = icmp ne i64 %131, 0
br i1 %132, label %133, label %140
133: ; preds = %127
%134 = load %struct.dirent*, %struct.dirent** %13
%135 = getelementptr inbounds %struct.dirent, %struct.dirent* %134, i32 0, i32 0
%136 = load i32, i32* %135
%137 = sext i32 %136 to i64
%138 = inttoptr i64 %137 to i8*
%139 = call i32 (i8*, i8*, ...) @warnx(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.5, i64 0, i64 0), i8* %138)
store i32 1, i32* %9
br label %140
140: ; preds = %133, %127, %121
br label %141
141: ; preds = %140, %118
br label %100
142: ; preds = %110
%143 = load i64, i64* %12
%144 = icmp ne i64 %143, 1
br i1 %144, label %145, label %149
145: ; preds = %142
%146 = getelementptr inbounds [20 x i8], [20 x i8]* %7, i64 0, i64 0
%147 = load i64, i64* %12
%148 = call i32 (i8*, i8*, ...) @warnx(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.8, i64 0, i64 0), i8* %146, i64 %147)
store i32 1, i32* %9
br label %149
149: ; preds = %145, %142
%150 = load i32, i32* %9
ret i32 %150
}

LLVM IR (-O2):
%struct.dirent = type { i32 }
@__const.main.kDirPath = [19 x i8] c"rewinddir_test_dir\00"
@__const.main.kFileBasename = [20 x i8] c"rewinddir_test_file\00"
@.str = [10 x i8] c"mkdir(%s)\00"
@.str.1 = [12 x i8] c"opendir(%s)\00"
@errno = global i64 0
@.str.2 = [8 x i8] c"readdir\00"
@.str.3 = [2 x i8] c".\00"
@.str.4 = [3 x i8] c"..\00"
@.str.5 = [36 x i8] c"unexpected file %s in new directory\00"
@.str.6 = [7 x i8] c"malloc\00"
@.str.7 = [10 x i8] c"mknod(%s)\00"
@.str.8 = [45 x i8] c"readdir returned file %s %zu times, wanted 1\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [19 x i8]
%4 = alloca [20 x i8]
%5 = getelementptr inbounds [19 x i8], [19 x i8]* %3, i64 0, i64 0
%6 = getelementptr inbounds [20 x i8], [20 x i8]* %4, i64 0, i64 0
%7 = call i64 @mkdir(i8* %5, i32 493)
%8 = icmp slt i64 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %2
%10 = call i32 (i32, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* %5)
br label %11
11: ; preds = %9, %2
%12 = call i32* @opendir(i8* %5)
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %16
14: ; preds = %11
%15 = call i32 (i32, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i8* %5)
br label %16
16: ; preds = %14, %11
store i64 0, i64* @errno
%17 = call %struct.dirent* @readdir(i32* %12)
%18 = icmp eq %struct.dirent* %17, null
br i1 %18, label %19, label %25
19: ; preds = %41, %16
%20 = phi i32 [ 0, %16 ], [ %42, %41 ]
%21 = load i64, i64* @errno
%22 = icmp eq i64 %21, 0
br i1 %22, label %45, label %23
23: ; preds = %19
%24 = call i32 (i32, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
br label %45
25: ; preds = %16, %41
%26 = phi %struct.dirent* [ %43, %41 ], [ %17, %16 ]
%27 = phi i32 [ %42, %41 ], [ 0, %16 ]
%28 = getelementptr inbounds %struct.dirent, %struct.dirent* %26, i64 0, i32 0
%29 = load i32, i32* %28
%30 = call i64 @strcmp(i32 %29, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%31 = icmp eq i64 %30, 0
br i1 %31, label %41, label %32
32: ; preds = %25
%33 = load i32, i32* %28
%34 = call i64 @strcmp(i32 %33, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
%35 = icmp eq i64 %34, 0
br i1 %35, label %41, label %36
36: ; preds = %32
%37 = load i32, i32* %28
%38 = sext i32 %37 to i64
%39 = inttoptr i64 %38 to i8*
%40 = call i32 (i8*, i8*, ...) @warnx(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.5, i64 0, i64 0), i8* %39)
br label %41
41: ; preds = %25, %32, %36
%42 = phi i32 [ 1, %36 ], [ %27, %32 ], [ %27, %25 ]
store i64 0, i64* @errno
%43 = call %struct.dirent* @readdir(i32* %12)
%44 = icmp eq %struct.dirent* %43, null
br i1 %44, label %19, label %25
45: ; preds = %23, %19
%46 = call i32 @strlen(i8* %5)
%47 = add nsw i32 %46, 1
%48 = call i32 @strlen(i8* %6)
%49 = add nsw i32 %47, %48
%50 = call i8* @malloc(i32 %49)
%51 = icmp eq i8* %50, null
br i1 %51, label %52, label %54
52: ; preds = %45
%53 = call i32 @errx(i32 1, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.6, i64 0, i64 0))
br label %54
54: ; preds = %52, %45
%55 = call i32 @strcpy(i8* %50, i8* %5)
%56 = call i32 @strlen(i8* %5)
%57 = sext i32 %56 to i64
%58 = getelementptr inbounds i8, i8* %50, i64 %57
store i8 47, i8* %58
%59 = call i32 @strlen(i8* %5)
%60 = sext i32 %59 to i64
%61 = getelementptr inbounds i8, i8* %50, i64 %60
%62 = getelementptr inbounds i8, i8* %61, i64 1
%63 = call i32 @strcpy(i8* %62, i8* %6)
%64 = call i64 @mknod(i8* %50, i32 420, i32 0)
%65 = icmp slt i64 %64, 0
br i1 %65, label %66, label %68
66: ; preds = %54
%67 = call i32 (i32, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.7, i64 0, i64 0), i8* %50)
br label %68
68: ; preds = %66, %54
%69 = call i32 @rewinddir(i32* %12)
store i64 0, i64* @errno
%70 = call %struct.dirent* @readdir(i32* %12)
%71 = icmp eq %struct.dirent* %70, null
br i1 %71, label %72, label %79
72: ; preds = %102, %68
%73 = phi i32 [ %20, %68 ], [ %103, %102 ]
%74 = phi i64 [ 0, %68 ], [ %104, %102 ]
%75 = load i64, i64* @errno
%76 = icmp eq i64 %75, 0
br i1 %76, label %107, label %77
77: ; preds = %72
%78 = call i32 (i32, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
br label %107
79: ; preds = %68, %102
%80 = phi %struct.dirent* [ %105, %102 ], [ %70, %68 ]
%81 = phi i64 [ %104, %102 ], [ 0, %68 ]
%82 = phi i32 [ %103, %102 ], [ %20, %68 ]
%83 = getelementptr inbounds %struct.dirent, %struct.dirent* %80, i64 0, i32 0
%84 = load i32, i32* %83
%85 = call i64 @strcmp(i32 %84, i8* %6)
%86 = icmp eq i64 %85, 0
br i1 %86, label %87, label %89
87: ; preds = %79
%88 = add i64 %81, 1
br label %102
89: ; preds = %79
%90 = load i32, i32* %83
%91 = call i64 @strcmp(i32 %90, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%92 = icmp eq i64 %91, 0
br i1 %92, label %102, label %93
93: ; preds = %89
%94 = load i32, i32* %83
%95 = call i64 @strcmp(i32 %94, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
%96 = icmp eq i64 %95, 0
br i1 %96, label %102, label %97
97: ; preds = %93
%98 = load i32, i32* %83
%99 = sext i32 %98 to i64
%100 = inttoptr i64 %99 to i8*
%101 = call i32 (i8*, i8*, ...) @warnx(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.5, i64 0, i64 0), i8* %100)
br label %102
102: ; preds = %87, %97, %93, %89
%103 = phi i32 [ %82, %87 ], [ 1, %97 ], [ %82, %93 ], [ %82, %89 ]
%104 = phi i64 [ %88, %87 ], [ %81, %97 ], [ %81, %93 ], [ %81, %89 ]
store i64 0, i64* @errno
%105 = call %struct.dirent* @readdir(i32* %12)
%106 = icmp eq %struct.dirent* %105, null
br i1 %106, label %72, label %79
107: ; preds = %77, %72
%108 = icmp eq i64 %74, 1
br i1 %108, label %111, label %109
109: ; preds = %107
%110 = call i32 (i8*, i8*, ...) @warnx(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.8, i64 0, i64 0), i8* %6, i64 %74)
br label %111
111: ; preds = %109, %107
%112 = phi i32 [ 1, %109 ], [ %73, %107 ]
ret i32 %112
}