#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 */
int /*<<< orphan*/ __set_errno(int /*<<< orphan*/);
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;
  }
  __set_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   1040 <puts@plt>
mov    -0x10(%rbp),%rdi
call   1050 <opendir@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11bc <test+0x4c>
lea    0xe55(%rip),%rdi
call   1090 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    124a <test+0xda>
xor    %edi,%edi
call   1030 <__set_errno@plt>
mov    -0x18(%rbp),%rdi
call   1080 <readdir@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     11fa <test+0x8a>
mov    -0x20(%rbp),%rax
mov    (%rax),%rsi
mov    -0x20(%rbp),%rax
mov    0x8(%rax),%rdx
lea    0xe18(%rip),%rdi
call   1060 <printf@plt>
jmp    11c3 <test+0x53>
mov    0x2ddf(%rip),%rax
cmpq   $0x0,(%rax)
je     121e <test+0xae>
lea    0xe0a(%rip),%rdi
call   1090 <perror@plt>
movl   $0x1,-0x24(%rbp)
mov    -0x18(%rbp),%rdi
call   1070 <closedir@plt>
cmp    $0x0,%rax
jge    1244 <test+0xd4>
lea    0xdec(%rip),%rdi
call   1090 <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   %r14
push   %rbx
mov    %rdi,%rbx
call   1040 <puts@plt>
mov    %rbx,%rdi
call   1050 <opendir@plt>
test   %rax,%rax
je     11ee <test+0x7e>
mov    %rax,%rbx
xor    %r14d,%r14d
xor    %edi,%edi
call   1030 <__set_errno@plt>
mov    %rbx,%rdi
call   1080 <readdir@plt>
test   %rax,%rax
je     11cc <test+0x5c>
lea    0xe5e(%rip),%rbp
nopw   0x0(%rax,%rax,1)
mov    (%rax),%rsi
mov    0x8(%rax),%rdx
mov    %rbp,%rdi
call   1060 <printf@plt>
mov    %rbx,%rdi
call   1080 <readdir@plt>
test   %rax,%rax
jne    11b0 <test+0x40>
mov    0x2e0d(%rip),%rax
cmpq   $0x0,(%rax)
jne    11f7 <test+0x87>
mov    %rbx,%rdi
call   1070 <closedir@plt>
test   %rax,%rax
js     120b <test+0x9b>
mov    %r14d,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe0b(%rip),%rdi
jmp    1212 <test+0xa2>
lea    0xe1e(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%r14d
jmp    11d9 <test+0x69>
lea    0xe12(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%r14d
jmp    11e6 <test+0x76>

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

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