#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*/ abort();
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ *opendir(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
scalar_t__ strcmp(char *, char *);
void test_retrieval(void) {
  DIR *dir = opendir("tests/1");
  if (dir == NULL) {
    fprintf(stderr, "Directory tests/1 not found\n");
    abort();
  }
  struct dirent *ent;
  int found = 0;
  while ((ent = readdir(dir)) != NULL) {
    if (strcmp(ent->d_name, ".") == 0) {
      found += 1;
    } else if (strcmp(ent->d_name, "..") == 0) {
      found += 2;
    } else if (strcmp(ent->d_name, "file") == 0) {
      found += 4;
    } else if (strcmp(ent->d_name, "dir") == 0) {
      found += 8;
    } else {
      fprintf(stderr, "Unexpected file %s\n", ent->d_name);
      abort();
    }
  }
  assert(found == 0xf);
  closedir(dir);
}


x86 assembly (clang, -O0):
<test_retrieval>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
lea    0xe81(%rip),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    11b1 <test_retrieval+0x41>
mov    0x2e46(%rip),%rax
mov    (%rax),%edi
lea    0xe65(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    $0x0,%al
call   1030 <abort@plt>
movl   $0x0,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
call   1090 <readdir@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
je     12b4 <test_retrieval+0x144>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
lea    0xe49(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
jne    11fa <test_retrieval+0x8a>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    12af <test_retrieval+0x13f>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
lea    0xe1d(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
jne    1225 <test_retrieval+0xb5>
mov    -0x14(%rbp),%eax
add    $0x2,%eax
mov    %eax,-0x14(%rbp)
jmp    12aa <test_retrieval+0x13a>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
lea    0xdf5(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
jne    1250 <test_retrieval+0xe0>
mov    -0x14(%rbp),%eax
add    $0x4,%eax
mov    %eax,-0x14(%rbp)
jmp    12a5 <test_retrieval+0x135>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
lea    0xdcf(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
jne    127b <test_retrieval+0x10b>
mov    -0x14(%rbp),%eax
add    $0x8,%eax
mov    %eax,-0x14(%rbp)
jmp    12a0 <test_retrieval+0x130>
mov    0x2d5e(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xd9f(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    $0x0,%al
call   1030 <abort@plt>
jmp    12a5 <test_retrieval+0x135>
jmp    12aa <test_retrieval+0x13a>
jmp    12af <test_retrieval+0x13f>
jmp    11b8 <test_retrieval+0x48>
cmpl   $0xf,-0x14(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
mov    -0x8(%rbp),%rdi
call   1050 <closedir@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_retrieval>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
lea    0xe7e(%rip),%rdi
call   1040 <opendir@plt>
test   %rax,%rax
je     1299 <test_retrieval+0x129>
mov    %rax,%rbx
mov    %rax,%rdi
call   1090 <readdir@plt>
test   %rax,%rax
je     125a <test_retrieval+0xea>
mov    %rax,%rbp
xor    %r13d,%r13d
lea    0xe75(%rip),%r14
lea    0xe6d(%rip),%r15
lea    0xe69(%rip),%r12
jmp    11e8 <test_retrieval+0x78>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    $0x1,%ecx
add    %ecx,%r13d
mov    %rbx,%rdi
call   1090 <readdir@plt>
mov    %rax,%rbp
test   %rax,%rax
je     124e <test_retrieval+0xde>
mov    0x0(%rbp),%rdi
mov    %r14,%rsi
call   1060 <strcmp@plt>
test   %rax,%rax
je     11d0 <test_retrieval+0x60>
mov    0x0(%rbp),%rdi
mov    %r15,%rsi
call   1060 <strcmp@plt>
test   %rax,%rax
je     1240 <test_retrieval+0xd0>
mov    0x0(%rbp),%rdi
mov    %r12,%rsi
call   1060 <strcmp@plt>
test   %rax,%rax
je     1247 <test_retrieval+0xd7>
mov    0x0(%rbp),%rdi
lea    0xe07(%rip),%rsi
call   1060 <strcmp@plt>
mov    $0x8,%ecx
test   %rax,%rax
je     11d5 <test_retrieval+0x65>
jmp    1277 <test_retrieval+0x107>
nopw   0x0(%rax,%rax,1)
mov    $0x2,%ecx
jmp    11d5 <test_retrieval+0x65>
mov    $0x4,%ecx
jmp    11d5 <test_retrieval+0x65>
xor    %edi,%edi
cmp    $0xf,%r13d
sete   %dil
jmp    125c <test_retrieval+0xec>
xor    %edi,%edi
call   1070 <assert@plt>
mov    %rbx,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <closedir@plt>
mov    0x2d62(%rip),%rax
mov    (%rax),%edi
mov    0x0(%rbp),%rdx
lea    0xda6(%rip),%rsi
xor    %eax,%eax
call   1080 <fprintf@plt>
xor    %eax,%eax
call   1030 <abort@plt>
mov    0x2d40(%rip),%rax
mov    (%rax),%edi
lea    0xd5f(%rip),%rsi
xor    %eax,%eax
call   1080 <fprintf@plt>
xor    %eax,%eax
call   1030 <abort@plt>

LLVM IR (-O0):
%struct.dirent = type { i8* }
@.str = [8 x i8] c"tests/1\00"
@stderr = global i32 0
@.str.1 = [29 x i8] c"Directory tests/1 not found\0A\00"
@.str.2 = [2 x i8] c".\00"
@.str.3 = [3 x i8] c"..\00"
@.str.4 = [5 x i8] c"file\00"
@.str.5 = [4 x i8] c"dir\00"
@.str.6 = [20 x i8] c"Unexpected file %s\0A\00"
define void @test_retrieval() {
%1 = alloca i32*
%2 = alloca %struct.dirent*
%3 = alloca i32
%4 = call i32* @opendir(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
store i32* %4, i32** %1
%5 = load i32*, i32** %1
%6 = icmp eq i32* %5, null
br i1 %6, label %7, label %11
7: ; preds = %0
%8 = load i32, i32* @stderr
%9 = call i32 (i32, i8*, ...) @fprintf(i32 %8, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0))
%10 = call i32 (...) @abort()
unreachable
11: ; preds = %0
store i32 0, i32* %3
br label %12
12: ; preds = %62, %11
%13 = load i32*, i32** %1
%14 = call %struct.dirent* @readdir(i32* %13)
store %struct.dirent* %14, %struct.dirent** %2
%15 = icmp ne %struct.dirent* %14, null
br i1 %15, label %16, label %63
16: ; preds = %12
%17 = load %struct.dirent*, %struct.dirent** %2
%18 = getelementptr inbounds %struct.dirent, %struct.dirent* %17, i32 0, i32 0
%19 = load i8*, i8** %18
%20 = call i64 @strcmp(i8* %19, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%21 = icmp eq i64 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %16
%23 = load i32, i32* %3
%24 = add nsw i32 %23, 1
store i32 %24, i32* %3
br label %62
25: ; preds = %16
%26 = load %struct.dirent*, %struct.dirent** %2
%27 = getelementptr inbounds %struct.dirent, %struct.dirent* %26, i32 0, i32 0
%28 = load i8*, i8** %27
%29 = call i64 @strcmp(i8* %28, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%30 = icmp eq i64 %29, 0
br i1 %30, label %31, label %34
31: ; preds = %25
%32 = load i32, i32* %3
%33 = add nsw i32 %32, 2
store i32 %33, i32* %3
br label %61
34: ; preds = %25
%35 = load %struct.dirent*, %struct.dirent** %2
%36 = getelementptr inbounds %struct.dirent, %struct.dirent* %35, i32 0, i32 0
%37 = load i8*, i8** %36
%38 = call i64 @strcmp(i8* %37, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
%39 = icmp eq i64 %38, 0
br i1 %39, label %40, label %43
40: ; preds = %34
%41 = load i32, i32* %3
%42 = add nsw i32 %41, 4
store i32 %42, i32* %3
br label %60
43: ; preds = %34
%44 = load %struct.dirent*, %struct.dirent** %2
%45 = getelementptr inbounds %struct.dirent, %struct.dirent* %44, i32 0, i32 0
%46 = load i8*, i8** %45
%47 = call i64 @strcmp(i8* %46, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0))
%48 = icmp eq i64 %47, 0
br i1 %48, label %49, label %52
49: ; preds = %43
%50 = load i32, i32* %3
%51 = add nsw i32 %50, 8
store i32 %51, i32* %3
br label %59
52: ; preds = %43
%53 = load i32, i32* @stderr
%54 = load %struct.dirent*, %struct.dirent** %2
%55 = getelementptr inbounds %struct.dirent, %struct.dirent* %54, i32 0, i32 0
%56 = load i8*, i8** %55
%57 = call i32 (i32, i8*, ...) @fprintf(i32 %53, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.6, i64 0, i64 0), i8* %56)
%58 = call i32 (...) @abort()
unreachable
59: ; preds = %49
br label %60
60: ; preds = %59, %40
br label %61
61: ; preds = %60, %31
br label %62
62: ; preds = %61, %22
br label %12
63: ; preds = %12
%64 = load i32, i32* %3
%65 = icmp eq i32 %64, 15
%66 = zext i1 %65 to i32
%67 = call i32 @assert(i32 %66)
%68 = load i32*, i32** %1
%69 = call i32 @closedir(i32* %68)
ret void
}

LLVM IR (-O2):
%struct.dirent = type { i8* }
@.str = [8 x i8] c"tests/1\00"
@stderr = global i32 0
@.str.1 = [29 x i8] c"Directory tests/1 not found\0A\00"
@.str.2 = [2 x i8] c".\00"
@.str.3 = [3 x i8] c"..\00"
@.str.4 = [5 x i8] c"file\00"
@.str.5 = [4 x i8] c"dir\00"
@.str.6 = [20 x i8] c"Unexpected file %s\0A\00"
define void @test_retrieval() {
%1 = tail call i32* @opendir(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%2 = icmp eq i32* %1, null
br i1 %2, label %6, label %3
3: ; preds = %0
%4 = tail call %struct.dirent* @readdir(i32* %1)
%5 = icmp eq %struct.dirent* %4, null
br i1 %5, label %42, label %10
6: ; preds = %0
%7 = load i32, i32* @stderr
%8 = tail call i32 (i32, i8*, ...) @fprintf(i32 %7, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0))
%9 = tail call i32 (...) @abort()
unreachable
10: ; preds = %3, %34
%11 = phi %struct.dirent* [ %37, %34 ], [ %4, %3 ]
%12 = phi i32 [ %36, %34 ], [ 0, %3 ]
%13 = getelementptr inbounds %struct.dirent, %struct.dirent* %11, i64 0, i32 0
%14 = load i8*, i8** %13
%15 = tail call i64 @strcmp(i8* %14, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%16 = icmp eq i64 %15, 0
br i1 %16, label %34, label %17
17: ; preds = %10
%18 = load i8*, i8** %13
%19 = tail call i64 @strcmp(i8* %18, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%20 = icmp eq i64 %19, 0
br i1 %20, label %34, label %21
21: ; preds = %17
%22 = load i8*, i8** %13
%23 = tail call i64 @strcmp(i8* %22, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
%24 = icmp eq i64 %23, 0
br i1 %24, label %34, label %25
25: ; preds = %21
%26 = load i8*, i8** %13
%27 = tail call i64 @strcmp(i8* %26, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0))
%28 = icmp eq i64 %27, 0
br i1 %28, label %34, label %29
29: ; preds = %25
%30 = load i32, i32* @stderr
%31 = load i8*, i8** %13
%32 = tail call i32 (i32, i8*, ...) @fprintf(i32 %30, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.6, i64 0, i64 0), i8* %31)
%33 = tail call i32 (...) @abort()
unreachable
34: ; preds = %25, %21, %17, %10
%35 = phi i32 [ 1, %10 ], [ 2, %17 ], [ 4, %21 ], [ 8, %25 ]
%36 = add nuw nsw i32 %12, %35
%37 = tail call %struct.dirent* @readdir(i32* %1)
%38 = icmp eq %struct.dirent* %37, null
br i1 %38, label %39, label %10
39: ; preds = %34
%40 = icmp eq i32 %36, 15
%41 = zext i1 %40 to i32
br label %42
42: ; preds = %39, %3
%43 = phi i32 [ 0, %3 ], [ %41, %39 ]
%44 = tail call i32 @assert(i32 %43)
%45 = tail call i32 @closedir(i32* %1)
ret void
}