#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 */
scalar_t__ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
struct dirent *readdir(int /*<<< orphan*/ *);
void showDirectoryInfo() {
  DIR *dir;
  dir = opendir("./");
  if (dir == NULL) {
    perror("error occurred while opendir()");
    return;
  }
  struct dirent *dirent;
  while ((dirent = readdir(dir)) != NULL) {
    printf("%s\n", dirent->d_name);
  }
  if (closedir(dir)) {
    perror("error occurred while closedir()");
    return;
  }
}


x86 assembly (clang, -O0):
<showDirectoryInfo>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
lea    0xea1(%rip),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    1184 <showDirectoryInfo+0x34>
lea    0xe89(%rip),%rdi
call   1070 <perror@plt>
jmp    11d7 <showDirectoryInfo+0x87>
jmp    1189 <showDirectoryInfo+0x39>
mov    -0x8(%rbp),%rdi
call   1060 <readdir@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
je     11b8 <showDirectoryInfo+0x68>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe74(%rip),%rdi
call   1040 <printf@plt>
jmp    1189 <showDirectoryInfo+0x39>
mov    -0x8(%rbp),%rdi
call   1050 <closedir@plt>
cmp    $0x0,%rax
je     11d7 <showDirectoryInfo+0x87>
lea    0xe54(%rip),%rdi
call   1070 <perror@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<showDirectoryInfo>:
push   %r14
push   %rbx
push   %rax
lea    0xea5(%rip),%rdi
call   1030 <opendir@plt>
test   %rax,%rax
je     11ad <showDirectoryInfo+0x5d>
mov    %rax,%rbx
mov    %rax,%rdi
call   1060 <readdir@plt>
test   %rax,%rax
je     1198 <showDirectoryInfo+0x48>
lea    0xea6(%rip),%r14
nopl   0x0(%rax)
mov    (%rax),%rsi
mov    %r14,%rdi
call   1040 <printf@plt>
mov    %rbx,%rdi
call   1060 <readdir@plt>
test   %rax,%rax
jne    1180 <showDirectoryInfo+0x30>
mov    %rbx,%rdi
call   1050 <closedir@plt>
test   %rax,%rax
jne    11b6 <showDirectoryInfo+0x66>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe4f(%rip),%rdi
jmp    11bd <showDirectoryInfo+0x6d>
lea    0xe69(%rip),%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1070 <perror@plt>

LLVM IR (-O0):
%struct.dirent = type { i8* }
@.str = [3 x i8] c"./\00"
@.str.1 = [31 x i8] c"error occurred while opendir()\00"
@.str.2 = [4 x i8] c"%s\0A\00"
@.str.3 = [32 x i8] c"error occurred while closedir()\00"
define void @showDirectoryInfo() {
%1 = alloca i32*
%2 = alloca %struct.dirent*
%3 = call i32* @opendir(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %3, i32** %1
%4 = load i32*, i32** %1
%5 = icmp eq i32* %4, null
br i1 %5, label %6, label %8
6: ; preds = %0
%7 = call i32 @perror(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0))
br label %24
8: ; preds = %0
br label %9
9: ; preds = %13, %8
%10 = load i32*, i32** %1
%11 = call %struct.dirent* @readdir(i32* %10)
store %struct.dirent* %11, %struct.dirent** %2
%12 = icmp ne %struct.dirent* %11, null
br i1 %12, label %13, label %18
13: ; preds = %9
%14 = load %struct.dirent*, %struct.dirent** %2
%15 = getelementptr inbounds %struct.dirent, %struct.dirent* %14, i32 0, i32 0
%16 = load i8*, i8** %15
%17 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* %16)
br label %9
18: ; preds = %9
%19 = load i32*, i32** %1
%20 = call i64 @closedir(i32* %19)
%21 = icmp ne i64 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %18
%23 = call i32 @perror(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0))
br label %24
24: ; preds = %6, %22, %18
ret void
}

LLVM IR (-O2):
%struct.dirent = type { i8* }
@.str = [3 x i8] c"./\00"
@.str.1 = [31 x i8] c"error occurred while opendir()\00"
@.str.2 = [4 x i8] c"%s\0A\00"
@.str.3 = [32 x i8] c"error occurred while closedir()\00"
define void @showDirectoryInfo() {
%1 = tail call i32* @opendir(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%2 = icmp eq i32* %1, null
br i1 %2, label %16, label %3
3: ; preds = %0
%4 = tail call %struct.dirent* @readdir(i32* %1)
%5 = icmp eq %struct.dirent* %4, null
br i1 %5, label %13, label %6
6: ; preds = %3, %6
%7 = phi %struct.dirent* [ %11, %6 ], [ %4, %3 ]
%8 = getelementptr inbounds %struct.dirent, %struct.dirent* %7, i64 0, i32 0
%9 = load i8*, i8** %8
%10 = tail call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* %9)
%11 = tail call %struct.dirent* @readdir(i32* %1)
%12 = icmp eq %struct.dirent* %11, null
br i1 %12, label %13, label %6
13: ; preds = %6, %3
%14 = tail call i64 @closedir(i32* %1)
%15 = icmp eq i64 %14, 0
br i1 %15, label %19, label %16
16: ; preds = %13, %0
%17 = phi i8* [ getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), %0 ], [ getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0), %13 ]
%18 = tail call i32 @perror(i8* %17)
br label %19
19: ; preds = %16, %13
ret void
}