#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 d_reclen;
  int d_ino;
  char *d_name;
};
typedef int off_t;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
char *calloc(int, int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int getdirentries(int, char *, int, int *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
void main(int argc, char **argv) {
  char *buf;
  struct dirent *p;
  int i, cnt, dir, nbytes;
  off_t base = 0;
  if (argc != 2) {
    fprintf(stderr, "Usage: test-dirent <path>\n");
    exit(1);
  }
  buf = calloc(1, 8192);
  if (!buf) {
    fprintf(stderr, "error: out of virtual memory\n");
    exit(1);
  }
  fprintf(stdout, "test-dirent: opening `%s'...\n", argv[1]);
  dir = open(argv[1], O_RDONLY);
  if (dir == -1) {
    fprintf(stderr, "error: cannot open directory `%s'\n", argv[1]);
    exit(1);
  }
  nbytes = getdirentries(dir, buf, 8192, &base);
  fprintf(stdout, "%d = getdirentries(%d, %p, %d, %d)\n", nbytes, dir, buf,
          8192, base);
  for (i = 0, cnt = 0, p = (struct dirent *)buf;
       cnt < nbytes && p->d_reclen > 0;
       i++, cnt += p->d_reclen, p = (struct dirent *)(buf + cnt)) {
    fprintf(stdout, "rec #%2d:  d_ino: %7d,  d_reclen: %2d,  d_name: %s\n", i,
            p->d_ino, p->d_reclen, p->d_name);
  }
  if (cnt != nbytes)
    fprintf(stderr, "warn: cnt != nbytes, cnt == %d, nbytes == %d", cnt,
            nbytes);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x34(%rbp)
cmpl   $0x2,-0x4(%rbp)
je     1191 <main+0x41>
mov    0x2e61(%rip),%rax
mov    (%rax),%edi
lea    0xe80(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    $0x1,%edi
mov    $0x2000,%esi
call   1040 <calloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11d0 <main+0x80>
mov    0x2e22(%rip),%rax
mov    (%rax),%edi
lea    0xe5c(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    0x2df9(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
lea    0xe51(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2dea(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0xffffffff,-0x2c(%rbp)
jne    123b <main+0xeb>
mov    0x2dbf(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
lea    0xe2d(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x2c(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    $0x2000,%edx
lea    -0x34(%rbp),%rcx
call   1030 <getdirentries@plt>
mov    %eax,-0x30(%rbp)
mov    0x2d76(%rip),%rax
mov    (%rax),%edi
mov    -0x30(%rbp),%edx
mov    -0x2c(%rbp),%ecx
mov    -0x18(%rbp),%r8
mov    -0x34(%rbp),%eax
lea    0xe0a(%rip),%rsi
mov    $0x2000,%r9d
mov    %eax,(%rsp)
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x0,-0x24(%rbp)
movl   $0x0,-0x28(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x28(%rbp),%ecx
xor    %eax,%eax
cmp    -0x30(%rbp),%ecx
mov    %al,-0x35(%rbp)
jge    12b4 <main+0x164>
mov    -0x20(%rbp),%rax
cmpl   $0x0,(%rax)
setg   %al
mov    %al,-0x35(%rbp)
mov    -0x35(%rbp),%al
test   $0x1,%al
jne    12c4 <main+0x174>
jmp    131d <main+0x1cd>
mov    0x2d05(%rip),%rax
mov    (%rax),%edi
mov    -0x24(%rbp),%edx
mov    -0x20(%rbp),%rax
mov    0x4(%rax),%ecx
mov    -0x20(%rbp),%rax
mov    (%rax),%r8d
mov    -0x20(%rbp),%rax
mov    0x8(%rax),%r9
lea    0xdb1(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%rax
mov    (%rax),%eax
add    -0x28(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x18(%rbp),%rax
movslq -0x28(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x20(%rbp)
jmp    1296 <main+0x146>
mov    -0x28(%rbp),%eax
cmp    -0x30(%rbp),%eax
je     1346 <main+0x1f6>
mov    0x2ca8(%rip),%rax
mov    (%rax),%edi
mov    -0x28(%rbp),%edx
mov    -0x30(%rbp),%ecx
lea    0xd93(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
movl   $0x0,0x14(%rsp)
cmp    $0x2,%edi
jne    128c <main+0x13c>
mov    %rsi,%r15
mov    $0x1,%edi
mov    $0x2000,%esi
call   1040 <calloc@plt>
test   %rax,%rax
je     129e <main+0x14e>
mov    %rax,%r14
mov    0x2e3c(%rip),%rbx
mov    (%rbx),%edi
mov    0x8(%r15),%rdx
lea    0xe98(%rip),%rsi
xor    %r13d,%r13d
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    0x8(%r15),%rdi
mov    0x2e32(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
cmp    $0xffffffff,%eax
je     12bf <main+0x16f>
mov    %eax,%ebp
lea    0x14(%rsp),%rcx
mov    %eax,%edi
mov    %r14,%rsi
mov    $0x2000,%edx
call   1030 <getdirentries@plt>
mov    %eax,%r12d
mov    (%rbx),%edi
mov    0x14(%rsp),%eax
mov    %eax,(%rsp)
lea    0xe8b(%rip),%rsi
mov    %r12d,%edx
mov    %ebp,%ecx
mov    %r14,%r8
mov    $0x2000,%r9d
xor    %eax,%eax
call   1050 <fprintf@plt>
test   %r12d,%r12d
jle    125b <main+0x10b>
lea    0xe8e(%rip),%r15
mov    %r14,%rbx
xor    %ebp,%ebp
xor    %r13d,%r13d
nopl   0x0(%rax,%rax,1)
mov    (%rbx),%r8d
test   %r8d,%r8d
jle    125b <main+0x10b>
mov    0x2da1(%rip),%rax
mov    (%rax),%edi
mov    0x4(%rbx),%ecx
mov    0x8(%rbx),%r9
mov    %r15,%rsi
mov    %ebp,%edx
xor    %eax,%eax
call   1050 <fprintf@plt>
add    $0x1,%ebp
movslq (%rbx),%rax
movslq %r13d,%r13
add    %rax,%r13
mov    %r13,%rbx
add    %r14,%rbx
cmp    %r12d,%r13d
jl     1220 <main+0xd0>
cmp    %r12d,%r13d
je     127d <main+0x12d>
mov    0x2d71(%rip),%rax
mov    (%rax),%edi
lea    0xe62(%rip),%rsi
mov    %r13d,%edx
mov    %r12d,%ecx
xor    %eax,%eax
call   1050 <fprintf@plt>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d45(%rip),%rax
mov    (%rax),%edi
lea    0xd64(%rip),%rsi
jmp    12ae <main+0x15e>
mov    0x2d33(%rip),%rax
mov    (%rax),%edi
lea    0xd6d(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    0x2d12(%rip),%rax
mov    (%rax),%edi
mov    0x8(%r15),%rdx
lea    0xd84(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
%struct.dirent = type { i32, i32, i8* }
@stderr = global i32 0
@.str = [27 x i8] c"Usage: test-dirent <path>\0A\00"
@.str.1 = [30 x i8] c"error: out of virtual memory\0A\00"
@stdout = global i32 0
@.str.2 = [30 x i8] c"test-dirent: opening `%s'...\0A\00"
@O_RDONLY = global i32 0
@.str.3 = [35 x i8] c"error: cannot open directory `%s'\0A\00"
@.str.4 = [36 x i8] c"%d = getdirentries(%d, %p, %d, %d)\0A\00"
@.str.5 = [52 x i8] c"rec #%2d: d_ino: %7d, d_reclen: %2d, d_name: %s\0A\00"
@.str.6 = [45 x i8] c"warn: cnt != nbytes, cnt == %d, nbytes == %d\00"
define void @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i8**
%5 = alloca i8*
%6 = alloca %struct.dirent*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i32 %0, i32* %3
store i8** %1, i8*** %4
store i32 0, i32* %11
%12 = load i32, i32* %3
%13 = icmp ne i32 %12, 2
br i1 %13, label %14, label %18
14: ; preds = %2
%15 = load i32, i32* @stderr
%16 = call i32 (i32, i8*, ...) @fprintf(i32 %15, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
%17 = call i32 @exit(i32 1)
unreachable
18: ; preds = %2
%19 = call i8* @calloc(i32 1, i32 8192)
store i8* %19, i8** %5
%20 = load i8*, i8** %5
%21 = icmp ne i8* %20, null
br i1 %21, label %26, label %22
22: ; preds = %18
%23 = load i32, i32* @stderr
%24 = call i32 (i32, i8*, ...) @fprintf(i32 %23, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0))
%25 = call i32 @exit(i32 1)
unreachable
26: ; preds = %18
%27 = load i32, i32* @stdout
%28 = load i8**, i8*** %4
%29 = getelementptr inbounds i8*, i8** %28, i64 1
%30 = load i8*, i8** %29
%31 = call i32 (i32, i8*, ...) @fprintf(i32 %27, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0), i8* %30)
%32 = load i8**, i8*** %4
%33 = getelementptr inbounds i8*, i8** %32, i64 1
%34 = load i8*, i8** %33
%35 = load i32, i32* @O_RDONLY
%36 = call i32 @open(i8* %34, i32 %35)
store i32 %36, i32* %9
%37 = load i32, i32* %9
%38 = icmp eq i32 %37, -1
br i1 %38, label %39, label %46
39: ; preds = %26
%40 = load i32, i32* @stderr
%41 = load i8**, i8*** %4
%42 = getelementptr inbounds i8*, i8** %41, i64 1
%43 = load i8*, i8** %42
%44 = call i32 (i32, i8*, ...) @fprintf(i32 %40, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.3, i64 0, i64 0), i8* %43)
%45 = call i32 @exit(i32 1)
unreachable
46: ; preds = %26
%47 = load i32, i32* %9
%48 = load i8*, i8** %5
%49 = call i32 @getdirentries(i32 %47, i8* %48, i32 8192, i32* %11)
store i32 %49, i32* %10
%50 = load i32, i32* @stdout
%51 = load i32, i32* %10
%52 = load i32, i32* %9
%53 = load i8*, i8** %5
%54 = load i32, i32* %11
%55 = call i32 (i32, i8*, ...) @fprintf(i32 %50, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.4, i64 0, i64 0), i32 %51, i32 %52, i8* %53, i32 8192, i32 %54)
store i32 0, i32* %7
store i32 0, i32* %8
%56 = load i8*, i8** %5
%57 = bitcast i8* %56 to %struct.dirent*
store %struct.dirent* %57, %struct.dirent** %6
br label %58
58: ; preds = %82, %46
%59 = load i32, i32* %8
%60 = load i32, i32* %10
%61 = icmp slt i32 %59, %60
br i1 %61, label %62, label %67
62: ; preds = %58
%63 = load %struct.dirent*, %struct.dirent** %6
%64 = getelementptr inbounds %struct.dirent, %struct.dirent* %63, i32 0, i32 0
%65 = load i32, i32* %64
%66 = icmp sgt i32 %65, 0
br label %67
67: ; preds = %62, %58
%68 = phi i1 [ false, %58 ], [ %66, %62 ]
br i1 %68, label %69, label %95
69: ; preds = %67
%70 = load i32, i32* @stdout
%71 = load i32, i32* %7
%72 = load %struct.dirent*, %struct.dirent** %6
%73 = getelementptr inbounds %struct.dirent, %struct.dirent* %72, i32 0, i32 1
%74 = load i32, i32* %73
%75 = load %struct.dirent*, %struct.dirent** %6
%76 = getelementptr inbounds %struct.dirent, %struct.dirent* %75, i32 0, i32 0
%77 = load i32, i32* %76
%78 = load %struct.dirent*, %struct.dirent** %6
%79 = getelementptr inbounds %struct.dirent, %struct.dirent* %78, i32 0, i32 2
%80 = load i8*, i8** %79
%81 = call i32 (i32, i8*, ...) @fprintf(i32 %70, i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.5, i64 0, i64 0), i32 %71, i32 %74, i32 %77, i8* %80)
br label %82
82: ; preds = %69
%83 = load i32, i32* %7
%84 = add nsw i32 %83, 1
store i32 %84, i32* %7
%85 = load %struct.dirent*, %struct.dirent** %6
%86 = getelementptr inbounds %struct.dirent, %struct.dirent* %85, i32 0, i32 0
%87 = load i32, i32* %86
%88 = load i32, i32* %8
%89 = add nsw i32 %88, %87
store i32 %89, i32* %8
%90 = load i8*, i8** %5
%91 = load i32, i32* %8
%92 = sext i32 %91 to i64
%93 = getelementptr inbounds i8, i8* %90, i64 %92
%94 = bitcast i8* %93 to %struct.dirent*
store %struct.dirent* %94, %struct.dirent** %6
br label %58
95: ; preds = %67
%96 = load i32, i32* %8
%97 = load i32, i32* %10
%98 = icmp ne i32 %96, %97
br i1 %98, label %99, label %104
99: ; preds = %95
%100 = load i32, i32* @stderr
%101 = load i32, i32* %8
%102 = load i32, i32* %10
%103 = call i32 (i32, i8*, ...) @fprintf(i32 %100, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.6, i64 0, i64 0), i32 %101, i32 %102)
br label %104
104: ; preds = %99, %95
ret void
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [27 x i8] c"Usage: test-dirent <path>\0A\00"
@.str.1 = [30 x i8] c"error: out of virtual memory\0A\00"
@stdout = global i32 0
@.str.2 = [30 x i8] c"test-dirent: opening `%s'...\0A\00"
@O_RDONLY = global i32 0
@.str.3 = [35 x i8] c"error: cannot open directory `%s'\0A\00"
@.str.4 = [36 x i8] c"%d = getdirentries(%d, %p, %d, %d)\0A\00"
@.str.5 = [52 x i8] c"rec #%2d: d_ino: %7d, d_reclen: %2d, d_name: %s\0A\00"
@.str.6 = [45 x i8] c"warn: cnt != nbytes, cnt == %d, nbytes == %d\00"
define void @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
store i32 0, i32* %3
%5 = icmp eq i32 %0, 2
br i1 %5, label %10, label %6
6: ; preds = %2
%7 = load i32, i32* @stderr
%8 = tail call i32 (i32, i8*, ...) @fprintf(i32 %7, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
%9 = tail call i32 @exit(i32 1)
unreachable
10: ; preds = %2
%11 = tail call dereferenceable_or_null(8192) i8* @calloc(i32 1, i32 8192)
%12 = icmp eq i8* %11, null
br i1 %12, label %13, label %17
13: ; preds = %10
%14 = load i32, i32* @stderr
%15 = tail call i32 (i32, i8*, ...) @fprintf(i32 %14, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0))
%16 = tail call i32 @exit(i32 1)
unreachable
17: ; preds = %10
%18 = load i32, i32* @stdout
%19 = getelementptr inbounds i8*, i8** %1, i64 1
%20 = load i8*, i8** %19
%21 = tail call i32 (i32, i8*, ...) @fprintf(i32 %18, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0), i8* %20)
%22 = load i8*, i8** %19
%23 = load i32, i32* @O_RDONLY
%24 = tail call i32 @open(i8* %22, i32 %23)
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %31
26: ; preds = %17
%27 = load i32, i32* @stderr
%28 = load i8*, i8** %19
%29 = tail call i32 (i32, i8*, ...) @fprintf(i32 %27, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.3, i64 0, i64 0), i8* %28)
%30 = tail call i32 @exit(i32 1)
unreachable
31: ; preds = %17
%32 = call i32 @getdirentries(i32 %24, i8* %11, i32 8192, i32* %3)
%33 = load i32, i32* @stdout
%34 = load i32, i32* %3
%35 = call i32 (i32, i8*, ...) @fprintf(i32 %33, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.4, i64 0, i64 0), i32 %32, i32 %24, i8* %11, i32 8192, i32 %34)
%36 = icmp sgt i32 %32, 0
br i1 %36, label %37, label %59
37: ; preds = %31, %44
%38 = phi i8* [ %57, %44 ], [ %11, %31 ]
%39 = phi i32 [ %53, %44 ], [ 0, %31 ]
%40 = phi i32 [ %55, %44 ], [ 0, %31 ]
%41 = bitcast i8* %38 to i32*
%42 = load i32, i32* %41
%43 = icmp sgt i32 %42, 0
br i1 %43, label %44, label %59
44: ; preds = %37
%45 = load i32, i32* @stdout
%46 = getelementptr inbounds i8, i8* %38, i64 4
%47 = bitcast i8* %46 to i32*
%48 = load i32, i32* %47
%49 = getelementptr inbounds i8, i8* %38, i64 8
%50 = bitcast i8* %49 to i8**
%51 = load i8*, i8** %50
%52 = call i32 (i32, i8*, ...) @fprintf(i32 %45, i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.5, i64 0, i64 0), i32 %39, i32 %48, i32 %42, i8* %51)
%53 = add nuw nsw i32 %39, 1
%54 = load i32, i32* %41
%55 = add nsw i32 %54, %40
%56 = sext i32 %55 to i64
%57 = getelementptr inbounds i8, i8* %11, i64 %56
%58 = icmp slt i32 %55, %32
br i1 %58, label %37, label %59
59: ; preds = %37, %44, %31
%60 = phi i32 [ 0, %31 ], [ %55, %44 ], [ %40, %37 ]
%61 = icmp eq i32 %60, %32
br i1 %61, label %65, label %62
62: ; preds = %59
%63 = load i32, i32* @stderr
%64 = call i32 (i32, i8*, ...) @fprintf(i32 %63, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.6, i64 0, i64 0), i32 %60, i32 %32)
br label %65
65: ; preds = %62, %59
ret void
}