#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;
  scalar_t__ d_type;
  scalar_t__ d_off;
  scalar_t__ d_ino;
};
typedef int /*<<< orphan*/ DIR;

/* Variables and functions */
scalar_t__ closedir(int /*<<< orphan*/ *);
int errno;
int /*<<< orphan*/ *opendir(char const *);
int /*<<< orphan*/ perror(char const *);
int /*<<< orphan*/ printf(char *, char const *, ...);
struct dirent *readdir(int /*<<< orphan*/ *);
int testit(const char *path) {
  DIR *d;
  struct dirent *de;
  printf("testing directory functions on %s\n", path);
  if ((d = opendir(path)) == NULL) {
    perror(path);
    return errno;
  }
  while ((de = readdir(d)) != NULL)
    printf("\t %s: ino %lu off %lu type %u\n", de->d_name,
           (unsigned long)de->d_ino, (unsigned long)de->d_off, (int)de->d_type);
  if (closedir(d)) {
    perror("closedir");
    return errno;
  }
  return 0;
}


x86 assembly (clang, -O0):
<testit>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rsi
lea    0xe99(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    119f <testit+0x4f>
mov    -0x10(%rbp),%rdi
call   1070 <perror@plt>
mov    0x2e4b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1227 <testit+0xd7>
jmp    11a4 <testit+0x54>
mov    -0x18(%rbp),%rdi
call   1060 <readdir@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     11f0 <testit+0xa0>
mov    -0x20(%rbp),%rax
mov    (%rax),%rsi
mov    -0x20(%rbp),%rax
mov    0x18(%rax),%rdx
mov    -0x20(%rbp),%rax
mov    0x10(%rax),%rcx
mov    -0x20(%rbp),%rax
mov    0x8(%rax),%rax
mov    %eax,%r8d
lea    0xe3f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    11a4 <testit+0x54>
mov    -0x18(%rbp),%rdi
call   1050 <closedir@plt>
cmp    $0x0,%rax
je     1220 <testit+0xd0>
lea    0xe38(%rip),%rdi
call   1070 <perror@plt>
mov    0x2dca(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1227 <testit+0xd7>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<testit>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
lea    0xea2(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %r14,%rdi
call   1030 <opendir@plt>
test   %rax,%rax
je     11d7 <testit+0x87>
mov    %rax,%rbx
mov    %rax,%rdi
call   1060 <readdir@plt>
test   %rax,%rax
je     11b6 <testit+0x66>
lea    0xe97(%rip),%r14
nopl   0x0(%rax)
mov    (%rax),%rsi
mov    0x18(%rax),%rdx
mov    0x10(%rax),%rcx
mov    0x8(%rax),%r8d
mov    %r14,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %rbx,%rdi
call   1060 <readdir@plt>
test   %rax,%rax
jne    1190 <testit+0x40>
mov    %rbx,%rdi
call   1050 <closedir@plt>
mov    %rax,%rcx
xor    %eax,%eax
test   %rcx,%rcx
jne    11d0 <testit+0x80>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe6b(%rip),%r14
mov    %r14,%rdi
call   1070 <perror@plt>
mov    0x2dfa(%rip),%rax
mov    (%rax),%eax
jmp    11c8 <testit+0x78>

LLVM IR (-O0):
%struct.dirent = type { i8*, i64, i64, i64 }
@.str = [35 x i8] c"testing directory functions on %s\0A\00"
@errno = global i32 0
@.str.1 = [31 x i8] c"\09 %s: ino %lu off %lu type %u\0A\00"
@.str.2 = [9 x i8] c"closedir\00"
define i32 @testit(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca %struct.dirent*
store i8* %0, i8** %3
%6 = load i8*, i8** %3
%7 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0), i8* %6)
%8 = load i8*, i8** %3
%9 = call i32* @opendir(i8* %8)
store i32* %9, i32** %4
%10 = icmp eq i32* %9, null
br i1 %10, label %11, label %15
11: ; preds = %1
%12 = load i8*, i8** %3
%13 = call i32 @perror(i8* %12)
%14 = load i32, i32* @errno
store i32 %14, i32* %2
br label %43
15: ; preds = %1
br label %16
16: ; preds = %20, %15
%17 = load i32*, i32** %4
%18 = call %struct.dirent* @readdir(i32* %17)
store %struct.dirent* %18, %struct.dirent** %5
%19 = icmp ne %struct.dirent* %18, null
br i1 %19, label %20, label %35
20: ; preds = %16
%21 = load %struct.dirent*, %struct.dirent** %5
%22 = getelementptr inbounds %struct.dirent, %struct.dirent* %21, i32 0, i32 0
%23 = load i8*, i8** %22
%24 = load %struct.dirent*, %struct.dirent** %5
%25 = getelementptr inbounds %struct.dirent, %struct.dirent* %24, i32 0, i32 3
%26 = load i64, i64* %25
%27 = load %struct.dirent*, %struct.dirent** %5
%28 = getelementptr inbounds %struct.dirent, %struct.dirent* %27, i32 0, i32 2
%29 = load i64, i64* %28
%30 = load %struct.dirent*, %struct.dirent** %5
%31 = getelementptr inbounds %struct.dirent, %struct.dirent* %30, i32 0, i32 1
%32 = load i64, i64* %31
%33 = trunc i64 %32 to i32
%34 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i8* %23, i64 %26, i64 %29, i32 %33)
br label %16
35: ; preds = %16
%36 = load i32*, i32** %4
%37 = call i64 @closedir(i32* %36)
%38 = icmp ne i64 %37, 0
br i1 %38, label %39, label %42
39: ; preds = %35
%40 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
%41 = load i32, i32* @errno
store i32 %41, i32* %2
br label %43
42: ; preds = %35
store i32 0, i32* %2
br label %43
43: ; preds = %42, %39, %11
%44 = load i32, i32* %2
ret i32 %44
}

LLVM IR (-O2):
%struct.dirent = type { i8*, i64, i64, i64 }
@.str = [35 x i8] c"testing directory functions on %s\0A\00"
@errno = global i32 0
@.str.1 = [31 x i8] c"\09 %s: ino %lu off %lu type %u\0A\00"
@.str.2 = [9 x i8] c"closedir\00"
define i32 @testit(i8* %0) {
%2 = tail call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0), i8* %0)
%3 = tail call i32* @opendir(i8* %0)
%4 = icmp eq i32* %3, null
br i1 %4, label %25, label %5
5: ; preds = %1
%6 = tail call %struct.dirent* @readdir(i32* %3)
%7 = icmp eq %struct.dirent* %6, null
br i1 %7, label %22, label %8
8: ; preds = %5, %8
%9 = phi %struct.dirent* [ %20, %8 ], [ %6, %5 ]
%10 = getelementptr inbounds %struct.dirent, %struct.dirent* %9, i64 0, i32 0
%11 = load i8*, i8** %10
%12 = getelementptr inbounds %struct.dirent, %struct.dirent* %9, i64 0, i32 3
%13 = load i64, i64* %12
%14 = getelementptr inbounds %struct.dirent, %struct.dirent* %9, i64 0, i32 2
%15 = load i64, i64* %14
%16 = getelementptr inbounds %struct.dirent, %struct.dirent* %9, i64 0, i32 1
%17 = load i64, i64* %16
%18 = trunc i64 %17 to i32
%19 = tail call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i8* %11, i64 %13, i64 %15, i32 %18)
%20 = tail call %struct.dirent* @readdir(i32* %3)
%21 = icmp eq %struct.dirent* %20, null
br i1 %21, label %22, label %8
22: ; preds = %8, %5
%23 = tail call i64 @closedir(i32* %3)
%24 = icmp eq i64 %23, 0
br i1 %24, label %29, label %25
25: ; preds = %22, %1
%26 = phi i8* [ %0, %1 ], [ getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), %22 ]
%27 = tail call i32 @perror(i8* %26)
%28 = load i32, i32* @errno
br label %29
29: ; preds = %25, %22
%30 = phi i32 [ 0, %22 ], [ %28, %25 ]
ret i32 %30
}