#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 O_CREAT;
int O_WRONLY;
int /*<<< orphan*/ close(int /*<<< orphan*/);
int /*<<< orphan*/ mkdir(char *, int);
int /*<<< orphan*/ open(char *, int, int);
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ puts(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ rmdir(char *);
scalar_t__ strcmp(int /*<<< orphan*/, char *);
int /*<<< orphan*/ unlink(char *);
int main() {
  DIR *D;
  mkdir("/tmp/foo", 0777);
  close(open("/tmp/foo/bar", O_WRONLY | O_CREAT, 0666));
  close(open("/tmp/foo/baz", O_WRONLY | O_CREAT, 0666));
  D = opendir("/tmp/foo");
  struct dirent *d;
  if (!D) {
    perror("opendir");
    return 1;
  }
  if (!(d = readdir(D)) || strcmp(d->d_name, "."))
    puts("did not get \".\"");
  if (!(d = readdir(D)) || strcmp(d->d_name, ".."))
    puts("did not get \"..\"");
  if (!(d = readdir(D)) || strcmp(d->d_name, "bar"))
    puts("did not get \"bar\"");
  if (!(d = readdir(D)) || strcmp(d->d_name, "baz"))
    puts("did not get \"baz\"");
  if ((d = readdir(D)))
    puts("got additional file");
  unlink("/tmp/foo/bar");
  unlink("/tmp/foo/baz");
  rmdir("/tmp/foo");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe4a(%rip),%rdi
mov    $0x1ff,%esi
call   1040 <mkdir@plt>
mov    0x2e09(%rip),%rax
mov    (%rax),%esi
mov    0x2e28(%rip),%rax
or     (%rax),%esi
lea    0xe30(%rip),%rdi
mov    $0x1b6,%edx
call   10b0 <open@plt>
mov    %eax,%edi
call   1080 <close@plt>
mov    0x2ddf(%rip),%rax
mov    (%rax),%esi
mov    0x2dfe(%rip),%rax
or     (%rax),%esi
lea    0xe13(%rip),%rdi
mov    $0x1b6,%edx
call   10b0 <open@plt>
mov    %eax,%edi
call   1080 <close@plt>
lea    0xde5(%rip),%rdi
call   1060 <opendir@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1247 <main+0xa7>
lea    0xded(%rip),%rdi
call   10c0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1391 <main+0x1f1>
mov    -0x10(%rbp),%rdi
call   10a0 <readdir@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     127a <main+0xda>
mov    -0x18(%rbp),%rax
mov    (%rax),%edi
lea    0xdd1(%rip),%rsi
call   1090 <strcmp@plt>
cmp    $0x0,%rax
je     1286 <main+0xe6>
lea    0xdaa(%rip),%rdi
call   1050 <puts@plt>
mov    -0x10(%rbp),%rdi
call   10a0 <readdir@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     12b9 <main+0x119>
mov    -0x18(%rbp),%rax
mov    (%rax),%edi
lea    0xd91(%rip),%rsi
call   1090 <strcmp@plt>
cmp    $0x0,%rax
je     12c5 <main+0x125>
lea    0xd7e(%rip),%rdi
call   1050 <puts@plt>
mov    -0x10(%rbp),%rdi
call   10a0 <readdir@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     12f8 <main+0x158>
mov    -0x18(%rbp),%rax
mov    (%rax),%edi
lea    0xd29(%rip),%rsi
call   1090 <strcmp@plt>
cmp    $0x0,%rax
je     1304 <main+0x164>
lea    0xd50(%rip),%rdi
call   1050 <puts@plt>
mov    -0x10(%rbp),%rdi
call   10a0 <readdir@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     1337 <main+0x197>
mov    -0x18(%rbp),%rax
mov    (%rax),%edi
lea    0xcf7(%rip),%rsi
call   1090 <strcmp@plt>
cmp    $0x0,%rax
je     1343 <main+0x1a3>
lea    0xd23(%rip),%rdi
call   1050 <puts@plt>
mov    -0x10(%rbp),%rdi
call   10a0 <readdir@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     1366 <main+0x1c6>
lea    0xd12(%rip),%rdi
call   1050 <puts@plt>
lea    0xc9c(%rip),%rdi
call   1030 <unlink@plt>
lea    0xc9d(%rip),%rdi
call   1030 <unlink@plt>
lea    0xc7b(%rip),%rdi
call   1070 <rmdir@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
lea    0xe54(%rip),%r14
mov    %r14,%rdi
mov    $0x1ff,%esi
call   1040 <mkdir@plt>
mov    0x2e10(%rip),%r15
mov    0x2e31(%rip),%rbx
mov    (%rbx),%esi
or     (%r15),%esi
lea    0xe36(%rip),%rdi
mov    $0x1b6,%edx
call   10b0 <open@plt>
mov    %eax,%edi
call   1080 <close@plt>
mov    (%rbx),%esi
or     (%r15),%esi
lea    0xe26(%rip),%rdi
mov    $0x1b6,%edx
call   10b0 <open@plt>
mov    %eax,%edi
call   1080 <close@plt>
mov    %r14,%rdi
call   1060 <opendir@plt>
test   %rax,%rax
je     130a <main+0x16a>
mov    %rax,%rbx
mov    %rax,%rdi
call   10a0 <readdir@plt>
test   %rax,%rax
je     1235 <main+0x95>
mov    (%rax),%edi
lea    0xe11(%rip),%rsi
call   1090 <strcmp@plt>
test   %rax,%rax
je     1241 <main+0xa1>
lea    0xdef(%rip),%rdi
call   1050 <puts@plt>
mov    %rbx,%rdi
call   10a0 <readdir@plt>
test   %rax,%rax
je     1261 <main+0xc1>
mov    (%rax),%edi
lea    0xde4(%rip),%rsi
call   1090 <strcmp@plt>
test   %rax,%rax
je     126d <main+0xcd>
lea    0xdd6(%rip),%rdi
call   1050 <puts@plt>
mov    %rbx,%rdi
call   10a0 <readdir@plt>
test   %rax,%rax
je     128d <main+0xed>
mov    (%rax),%edi
lea    0xd8f(%rip),%rsi
call   1090 <strcmp@plt>
test   %rax,%rax
je     1299 <main+0xf9>
lea    0xdbb(%rip),%rdi
call   1050 <puts@plt>
mov    %rbx,%rdi
call   10a0 <readdir@plt>
test   %rax,%rax
je     12b9 <main+0x119>
mov    (%rax),%edi
lea    0xd70(%rip),%rsi
call   1090 <strcmp@plt>
test   %rax,%rax
je     12c5 <main+0x125>
lea    0xda1(%rip),%rdi
call   1050 <puts@plt>
mov    %rbx,%rdi
call   10a0 <readdir@plt>
test   %rax,%rax
je     12de <main+0x13e>
lea    0xd9a(%rip),%rdi
call   1050 <puts@plt>
lea    0xd24(%rip),%rdi
call   1030 <unlink@plt>
lea    0xd25(%rip),%rdi
call   1030 <unlink@plt>
lea    0xd03(%rip),%rdi
call   1070 <rmdir@plt>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xd12(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x1,%eax
jmp    1304 <main+0x164>

LLVM IR (-O0):
%struct.dirent = type { i32 }
@.str = [9 x i8] c"/tmp/foo\00"
@.str.1 = [13 x i8] c"/tmp/foo/bar\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@.str.2 = [13 x i8] c"/tmp/foo/baz\00"
@.str.3 = [8 x i8] c"opendir\00"
@.str.4 = [2 x i8] c".\00"
@.str.5 = [16 x i8] c"did not get \22.\22\00"
@.str.6 = [3 x i8] c"..\00"
@.str.7 = [17 x i8] c"did not get \22..\22\00"
@.str.8 = [4 x i8] c"bar\00"
@.str.9 = [18 x i8] c"did not get \22bar\22\00"
@.str.10 = [4 x i8] c"baz\00"
@.str.11 = [18 x i8] c"did not get \22baz\22\00"
@.str.12 = [20 x i8] c"got additional file\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca %struct.dirent*
store i32 0, i32* %1
%4 = call i32 @mkdir(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 511)
%5 = load i32, i32* @O_WRONLY
%6 = load i32, i32* @O_CREAT
%7 = or i32 %5, %6
%8 = call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i32 %7, i32 438)
%9 = call i32 @close(i32 %8)
%10 = load i32, i32* @O_WRONLY
%11 = load i32, i32* @O_CREAT
%12 = or i32 %10, %11
%13 = call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i32 %12, i32 438)
%14 = call i32 @close(i32 %13)
%15 = call i32* @opendir(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
store i32* %15, i32** %2
%16 = load i32*, i32** %2
%17 = icmp ne i32* %16, null
br i1 %17, label %20, label %18
18: ; preds = %0
%19 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %1
br label %78
20: ; preds = %0
%21 = load i32*, i32** %2
%22 = call %struct.dirent* @readdir(i32* %21)
store %struct.dirent* %22, %struct.dirent** %3
%23 = icmp ne %struct.dirent* %22, null
br i1 %23, label %24, label %30
24: ; preds = %20
%25 = load %struct.dirent*, %struct.dirent** %3
%26 = getelementptr inbounds %struct.dirent, %struct.dirent* %25, i32 0, i32 0
%27 = load i32, i32* %26
%28 = call i64 @strcmp(i32 %27, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%29 = icmp ne i64 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %24, %20
%31 = call i32 @puts(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.5, i64 0, i64 0))
br label %32
32: ; preds = %30, %24
%33 = load i32*, i32** %2
%34 = call %struct.dirent* @readdir(i32* %33)
store %struct.dirent* %34, %struct.dirent** %3
%35 = icmp ne %struct.dirent* %34, null
br i1 %35, label %36, label %42
36: ; preds = %32
%37 = load %struct.dirent*, %struct.dirent** %3
%38 = getelementptr inbounds %struct.dirent, %struct.dirent* %37, i32 0, i32 0
%39 = load i32, i32* %38
%40 = call i64 @strcmp(i32 %39, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0))
%41 = icmp ne i64 %40, 0
br i1 %41, label %42, label %44
42: ; preds = %36, %32
%43 = call i32 @puts(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.7, i64 0, i64 0))
br label %44
44: ; preds = %42, %36
%45 = load i32*, i32** %2
%46 = call %struct.dirent* @readdir(i32* %45)
store %struct.dirent* %46, %struct.dirent** %3
%47 = icmp ne %struct.dirent* %46, null
br i1 %47, label %48, label %54
48: ; preds = %44
%49 = load %struct.dirent*, %struct.dirent** %3
%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 ([4 x i8], [4 x i8]* @.str.8, i64 0, i64 0))
%53 = icmp ne i64 %52, 0
br i1 %53, label %54, label %56
54: ; preds = %48, %44
%55 = call i32 @puts(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.9, i64 0, i64 0))
br label %56
56: ; preds = %54, %48
%57 = load i32*, i32** %2
%58 = call %struct.dirent* @readdir(i32* %57)
store %struct.dirent* %58, %struct.dirent** %3
%59 = icmp ne %struct.dirent* %58, null
br i1 %59, label %60, label %66
60: ; preds = %56
%61 = load %struct.dirent*, %struct.dirent** %3
%62 = getelementptr inbounds %struct.dirent, %struct.dirent* %61, i32 0, i32 0
%63 = load i32, i32* %62
%64 = call i64 @strcmp(i32 %63, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.10, i64 0, i64 0))
%65 = icmp ne i64 %64, 0
br i1 %65, label %66, label %68
66: ; preds = %60, %56
%67 = call i32 @puts(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.11, i64 0, i64 0))
br label %68
68: ; preds = %66, %60
%69 = load i32*, i32** %2
%70 = call %struct.dirent* @readdir(i32* %69)
store %struct.dirent* %70, %struct.dirent** %3
%71 = icmp ne %struct.dirent* %70, null
br i1 %71, label %72, label %74
72: ; preds = %68
%73 = call i32 @puts(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.12, i64 0, i64 0))
br label %74
74: ; preds = %72, %68
%75 = call i32 @unlink(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
%76 = call i32 @unlink(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%77 = call i32 @rmdir(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %1
br label %78
78: ; preds = %74, %18
%79 = load i32, i32* %1
ret i32 %79
}

LLVM IR (-O2):
%struct.dirent = type { i32 }
@.str = [9 x i8] c"/tmp/foo\00"
@.str.1 = [13 x i8] c"/tmp/foo/bar\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@.str.2 = [13 x i8] c"/tmp/foo/baz\00"
@.str.3 = [8 x i8] c"opendir\00"
@.str.4 = [2 x i8] c".\00"
@.str.5 = [16 x i8] c"did not get \22.\22\00"
@.str.6 = [3 x i8] c"..\00"
@.str.7 = [17 x i8] c"did not get \22..\22\00"
@.str.8 = [4 x i8] c"bar\00"
@.str.9 = [18 x i8] c"did not get \22bar\22\00"
@.str.10 = [4 x i8] c"baz\00"
@.str.11 = [18 x i8] c"did not get \22baz\22\00"
@.str.12 = [20 x i8] c"got additional file\00"
define i32 @main() {
%1 = tail call i32 @mkdir(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 511)
%2 = load i32, i32* @O_WRONLY
%3 = load i32, i32* @O_CREAT
%4 = or i32 %3, %2
%5 = tail call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i32 %4, i32 438)
%6 = tail call i32 @close(i32 %5)
%7 = load i32, i32* @O_WRONLY
%8 = load i32, i32* @O_CREAT
%9 = or i32 %8, %7
%10 = tail call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i32 %9, i32 438)
%11 = tail call i32 @close(i32 %10)
%12 = tail call i32* @opendir(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %16
14: ; preds = %0
%15 = tail call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
br label %65
16: ; preds = %0
%17 = tail call %struct.dirent* @readdir(i32* %12)
%18 = icmp eq %struct.dirent* %17, null
br i1 %18, label %24, label %19
19: ; preds = %16
%20 = getelementptr inbounds %struct.dirent, %struct.dirent* %17, i64 0, i32 0
%21 = load i32, i32* %20
%22 = tail call i64 @strcmp(i32 %21, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%23 = icmp eq i64 %22, 0
br i1 %23, label %26, label %24
24: ; preds = %19, %16
%25 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.5, i64 0, i64 0))
br label %26
26: ; preds = %24, %19
%27 = tail call %struct.dirent* @readdir(i32* %12)
%28 = icmp eq %struct.dirent* %27, null
br i1 %28, label %34, label %29
29: ; preds = %26
%30 = getelementptr inbounds %struct.dirent, %struct.dirent* %27, i64 0, i32 0
%31 = load i32, i32* %30
%32 = tail call i64 @strcmp(i32 %31, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0))
%33 = icmp eq i64 %32, 0
br i1 %33, label %36, label %34
34: ; preds = %29, %26
%35 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.7, i64 0, i64 0))
br label %36
36: ; preds = %34, %29
%37 = tail call %struct.dirent* @readdir(i32* %12)
%38 = icmp eq %struct.dirent* %37, null
br i1 %38, label %44, label %39
39: ; preds = %36
%40 = getelementptr inbounds %struct.dirent, %struct.dirent* %37, i64 0, i32 0
%41 = load i32, i32* %40
%42 = tail call i64 @strcmp(i32 %41, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.8, i64 0, i64 0))
%43 = icmp eq i64 %42, 0
br i1 %43, label %46, label %44
44: ; preds = %39, %36
%45 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.9, i64 0, i64 0))
br label %46
46: ; preds = %44, %39
%47 = tail call %struct.dirent* @readdir(i32* %12)
%48 = icmp eq %struct.dirent* %47, null
br i1 %48, label %54, label %49
49: ; preds = %46
%50 = getelementptr inbounds %struct.dirent, %struct.dirent* %47, i64 0, i32 0
%51 = load i32, i32* %50
%52 = tail call i64 @strcmp(i32 %51, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.10, i64 0, i64 0))
%53 = icmp eq i64 %52, 0
br i1 %53, label %56, label %54
54: ; preds = %49, %46
%55 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.11, i64 0, i64 0))
br label %56
56: ; preds = %54, %49
%57 = tail call %struct.dirent* @readdir(i32* %12)
%58 = icmp eq %struct.dirent* %57, null
br i1 %58, label %61, label %59
59: ; preds = %56
%60 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.12, i64 0, i64 0))
br label %61
61: ; preds = %59, %56
%62 = tail call i32 @unlink(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
%63 = tail call i32 @unlink(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%64 = tail call i32 @rmdir(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
br label %65
65: ; preds = %61, %14
%66 = phi i32 [ 0, %61 ], [ 1, %14 ]
ret i32 %66
}