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

/* Variables and functions */
scalar_t__ closedir(int /*<<< orphan*/ *);
scalar_t__ errno;
int /*<<< orphan*/ *opendir(char const *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *, unsigned long);
int /*<<< orphan*/ puts(char const *);
struct dirent *readdir(int /*<<< orphan*/ *);
int test(const char *name) {
  DIR *dirp;
  struct dirent *entp;
  int retval = 0;
  puts(name);
  dirp = opendir(name);
  if (dirp == NULL) {
    perror("opendir");
    return 1;
  }
  errno = 0;
  while ((entp = readdir(dirp)) != NULL)
    printf("%s\tfile number %lu\n", entp->d_name,
           (unsigned long int)entp->d_fileno);
  if (errno) {
    perror("readdir");
    retval = 1;
  }
  if (closedir(dirp) < 0) {
    perror("closedir");
    retval = 1;
  }
  return retval;
}


x86 assembly (clang, -O0):
<test>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <puts@plt>
mov    -0x10(%rbp),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11ac <test+0x4c>
lea    0xe65(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1241 <test+0xe1>
mov    0x2e2d(%rip),%rax
movq   $0x0,(%rax)
mov    -0x18(%rbp),%rdi
call   1070 <readdir@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     11f1 <test+0x91>
mov    -0x20(%rbp),%rax
mov    (%rax),%rsi
mov    -0x20(%rbp),%rax
mov    0x8(%rax),%rdx
lea    0xe21(%rip),%rdi
call   1050 <printf@plt>
jmp    11ba <test+0x5a>
mov    0x2de8(%rip),%rax
cmpq   $0x0,(%rax)
je     1215 <test+0xb5>
lea    0xe13(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x24(%rbp)
mov    -0x18(%rbp),%rdi
call   1060 <closedir@plt>
cmp    $0x0,%rax
jge    123b <test+0xdb>
lea    0xdf5(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
call   1030 <puts@plt>
mov    %rbx,%rdi
call   1040 <opendir@plt>
test   %rax,%rax
je     11ee <test+0x8e>
mov    %rax,%rbx
mov    0x2e5a(%rip),%r15
movq   $0x0,(%r15)
mov    %rax,%rdi
call   1070 <readdir@plt>
test   %rax,%rax
je     11cc <test+0x6c>
lea    0xe67(%rip),%r14
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    (%rax),%rsi
mov    0x8(%rax),%rdx
mov    %r14,%rdi
call   1050 <printf@plt>
mov    %rbx,%rdi
call   1070 <readdir@plt>
test   %rax,%rax
jne    11b0 <test+0x50>
xor    %ebp,%ebp
cmpq   $0x0,(%r15)
jne    11f7 <test+0x97>
mov    %rbx,%rdi
call   1060 <closedir@plt>
test   %rax,%rax
js     120a <test+0xaa>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe0b(%rip),%rdi
jmp    1211 <test+0xb1>
lea    0xe1e(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%ebp
jmp    11d4 <test+0x74>
lea    0xe13(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%ebp
jmp    11e1 <test+0x81>

LLVM IR (-O0):
%struct.dirent = type { i8*, i64 }
@.str = [8 x i8] c"opendir\00"
@errno = global i64 0
@.str.1 = [20 x i8] c"%s\09file number %lu\0A\00"
@.str.2 = [8 x i8] c"readdir\00"
@.str.3 = [9 x i8] c"closedir\00"
define i32 @test(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca %struct.dirent*
%6 = alloca i32
store i8* %0, i8** %3
store i32 0, i32* %6
%7 = load i8*, i8** %3
%8 = call i32 @puts(i8* %7)
%9 = load i8*, i8** %3
%10 = call i32* @opendir(i8* %9)
store i32* %10, i32** %4
%11 = load i32*, i32** %4
%12 = icmp eq i32* %11, null
br i1 %12, label %13, label %15
13: ; preds = %1
%14 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %2
br label %41
15: ; preds = %1
store i64 0, i64* @errno
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 %28
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 1
%26 = load i64, i64* %25
%27 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i8* %23, i64 %26)
br label %16
28: ; preds = %16
%29 = load i64, i64* @errno
%30 = icmp ne i64 %29, 0
br i1 %30, label %31, label %33
31: ; preds = %28
%32 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %6
br label %33
33: ; preds = %31, %28
%34 = load i32*, i32** %4
%35 = call i64 @closedir(i32* %34)
%36 = icmp slt i64 %35, 0
br i1 %36, label %37, label %39
37: ; preds = %33
%38 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %6
br label %39
39: ; preds = %37, %33
%40 = load i32, i32* %6
store i32 %40, i32* %2
br label %41
41: ; preds = %39, %13
%42 = load i32, i32* %2
ret i32 %42
}

LLVM IR (-O2):
%struct.dirent = type { i8*, i64 }
@.str = [8 x i8] c"opendir\00"
@errno = global i64 0
@.str.1 = [20 x i8] c"%s\09file number %lu\0A\00"
@.str.2 = [8 x i8] c"readdir\00"
@.str.3 = [9 x i8] c"closedir\00"
define i32 @test(i8* nocapture %0) {
%2 = tail call i32 @puts(i8* dereferenceable(1) %0)
%3 = tail call i32* @opendir(i8* %0)
%4 = icmp eq i32* %3, null
br i1 %4, label %26, label %5
5: ; preds = %1
store i64 0, i64* @errno
%6 = tail call %struct.dirent* @readdir(i32* %3)
%7 = icmp eq %struct.dirent* %6, null
br i1 %7, label %17, label %8
8: ; preds = %5, %8
%9 = phi %struct.dirent* [ %15, %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 1
%13 = load i64, i64* %12
%14 = tail call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i8* %11, i64 %13)
%15 = tail call %struct.dirent* @readdir(i32* %3)
%16 = icmp eq %struct.dirent* %15, null
br i1 %16, label %17, label %8
17: ; preds = %8, %5
%18 = load i64, i64* @errno
%19 = icmp eq i64 %18, 0
br i1 %19, label %22, label %20
20: ; preds = %17
%21 = tail call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
br label %22
22: ; preds = %20, %17
%23 = phi i32 [ 1, %20 ], [ 0, %17 ]
%24 = tail call i64 @closedir(i32* %3)
%25 = icmp slt i64 %24, 0
br i1 %25, label %26, label %29
26: ; preds = %22, %1
%27 = phi i8* [ getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), %1 ], [ getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), %22 ]
%28 = tail call i32 @perror(i8* %27)
br label %29
29: ; preds = %26, %22
%30 = phi i32 [ %23, %22 ], [ 1, %26 ]
ret i32 %30
}