#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 */
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ print_Stat(char *);
int /*<<< orphan*/ printf(char *, ...);
struct dirent *readdir(int /*<<< orphan*/ *);
int print_DirContents(char *path, int opt_l) {
  DIR *dp;
  struct dirent *dent;
  if ((dp = opendir(path)) == NULL) {
    perror("opendir");
    return -1;
  }
  while ((dent = readdir(dp))) {
    if (opt_l)
      print_Stat(dent->d_name);
    printf("%s", dent->d_name);
    if (opt_l)
      printf("\n");
    else
      printf(" ");
  }
  closedir(dp);
  return 0;
}


x86 assembly (clang, -O0):
<print_DirContents>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    119e <print_DirContents+0x3e>
lea    0xe73(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1225 <print_DirContents+0xc5>
jmp    11a3 <print_DirContents+0x43>
mov    -0x20(%rbp),%rdi
call   1060 <readdir@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
je     1215 <print_DirContents+0xb5>
cmpl   $0x0,-0x14(%rbp)
je     11d0 <print_DirContents+0x70>
mov    -0x28(%rbp),%rax
mov    (%rax),%rdi
call   1070 <print_Stat@plt>
mov    -0x28(%rbp),%rax
mov    (%rax),%rsi
lea    0xe2a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
cmpl   $0x0,-0x14(%rbp)
je     1202 <print_DirContents+0xa2>
lea    0xe15(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1210 <print_DirContents+0xb0>
lea    0xe04(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    11a3 <print_DirContents+0x43>
mov    -0x20(%rbp),%rdi
call   1050 <closedir@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<print_DirContents>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%r15d
call   1040 <opendir@plt>
test   %rax,%rax
je     11f7 <print_DirContents+0x87>
mov    %rax,%rbx
mov    %rax,%rdi
call   1070 <readdir@plt>
test   %rax,%rax
je     11e2 <print_DirContents+0x72>
mov    %rax,%rbp
lea    0xe6a(%rip),%r14
jmp    11d2 <print_DirContents+0x62>
mov    %rsi,%rdi
call   1080 <print_Stat@plt>
mov    0x0(%rbp),%rsi
mov    $0xa,%ebp
mov    %r14,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %ebp,%edi
call   1030 <putchar@plt>
mov    %rbx,%rdi
call   1070 <readdir@plt>
mov    %rax,%rbp
test   %rax,%rax
je     11e2 <print_DirContents+0x72>
mov    0x0(%rbp),%rsi
test   %r15d,%r15d
jne    11a0 <print_DirContents+0x30>
mov    $0x20,%ebp
jmp    11b1 <print_DirContents+0x41>
mov    %rbx,%rdi
call   1060 <closedir@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe02(%rip),%rdi
call   1090 <perror@plt>
mov    $0xffffffff,%eax
jmp    11ec <print_DirContents+0x7c>

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

LLVM IR (-O2):
%struct.dirent = type { i8* }
@.str = [8 x i8] c"opendir\00"
@.str.1 = [3 x i8] c"%s\00"
define i32 @print_DirContents(i8* nocapture %0, i32 %1) {
%3 = tail call i32* @opendir(i8* %0)
%4 = icmp eq i32* %3, null
br i1 %4, label %10, label %5
5: ; preds = %2
%6 = tail call %struct.dirent* @readdir(i32* %3)
%7 = icmp eq %struct.dirent* %6, null
br i1 %7, label %26, label %8
8: ; preds = %5
%9 = icmp eq i32 %1, 0
br label %12
10: ; preds = %2
%11 = tail call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
br label %28
12: ; preds = %8, %19
%13 = phi %struct.dirent* [ %6, %8 ], [ %24, %19 ]
%14 = getelementptr inbounds %struct.dirent, %struct.dirent* %13, i64 0, i32 0
%15 = load i8*, i8** %14
br i1 %9, label %19, label %16
16: ; preds = %12
%17 = tail call i32 @print_Stat(i8* %15)
%18 = load i8*, i8** %14
br label %19
19: ; preds = %12, %16
%20 = phi i8* [ %18, %16 ], [ %15, %12 ]
%21 = phi i32 [ 10, %16 ], [ 32, %12 ]
%22 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %20)
%23 = tail call i32 @putchar(i32 %21)
%24 = tail call %struct.dirent* @readdir(i32* %3)
%25 = icmp eq %struct.dirent* %24, null
br i1 %25, label %26, label %12
26: ; preds = %19, %5
%27 = tail call i32 @closedir(i32* %3)
br label %28
28: ; preds = %26, %10
%29 = phi i32 [ -1, %10 ], [ 0, %26 ]
ret i32 %29
}