#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*/ printf(char *, ...);
struct dirent *readdir(int /*<<< orphan*/ *);
int showdir(char *path) {
  struct dirent *de;
  DIR *dir = opendir(path);
  if (!dir) {
    printf("Could not open directory `%s`\n", path);
    return 1;
  }
  printf("Contents of directory `%s`:\n", path);
  while ((de = readdir(dir)))
    printf("  %s\n", de->d_name);
  printf("\n");
  closedir(dir);
  return 0;
}


x86 assembly (clang, -O0):
<showdir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1182 <showdir+0x42>
mov    -0x10(%rbp),%rsi
lea    0xe91(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    11e3 <showdir+0xa3>
mov    -0x10(%rbp),%rsi
lea    0xe92(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x20(%rbp),%rdi
call   1060 <readdir@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     11c5 <showdir+0x85>
mov    -0x18(%rbp),%rax
mov    (%rax),%rsi
lea    0xe83(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1194 <showdir+0x54>
lea    0xe6e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x20(%rbp),%rdi
call   1050 <closedir@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<showdir>:
push   %rbp
push   %r14
push   %rbx
mov    %rdi,%rbp
call   1040 <opendir@plt>
test   %rax,%rax
je     11be <showdir+0x6e>
mov    %rax,%rbx
lea    0xeb4(%rip),%rdi
xor    %r14d,%r14d
mov    %rbp,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rbx,%rdi
call   1070 <readdir@plt>
test   %rax,%rax
je     11aa <showdir+0x5a>
lea    0xeb0(%rip),%rbp
nopl   0x0(%rax)
mov    (%rax),%rsi
mov    %rbp,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rbx,%rdi
call   1070 <readdir@plt>
test   %rax,%rax
jne    1190 <showdir+0x40>
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    %rbx,%rdi
call   1060 <closedir@plt>
jmp    11d5 <showdir+0x85>
lea    0xe3b(%rip),%rdi
mov    %rbp,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%r14d
mov    %r14d,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.dirent = type { i8* }
@.str = [31 x i8] c"Could not open directory `%s`\0A\00"
@.str.1 = [29 x i8] c"Contents of directory `%s`:\0A\00"
@.str.2 = [6 x i8] c" %s\0A\00"
@.str.3 = [2 x i8] c"\0A\00"
define i32 @showdir(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca %struct.dirent*
%5 = alloca i32*
store i8* %0, i8** %3
%6 = load i8*, i8** %3
%7 = call i32* @opendir(i8* %6)
store i32* %7, i32** %5
%8 = load i32*, i32** %5
%9 = icmp ne i32* %8, null
br i1 %9, label %13, label %10
10: ; preds = %1
%11 = load i8*, i8** %3
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i8* %11)
store i32 1, i32* %2
br label %29
13: ; preds = %1
%14 = load i8*, i8** %3
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i8* %14)
br label %16
16: ; preds = %20, %13
%17 = load i32*, i32** %5
%18 = call %struct.dirent* @readdir(i32* %17)
store %struct.dirent* %18, %struct.dirent** %4
%19 = icmp ne %struct.dirent* %18, null
br i1 %19, label %20, label %25
20: ; preds = %16
%21 = load %struct.dirent*, %struct.dirent** %4
%22 = getelementptr inbounds %struct.dirent, %struct.dirent* %21, i32 0, i32 0
%23 = load i8*, i8** %22
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* %23)
br label %16
25: ; preds = %16
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%27 = load i32*, i32** %5
%28 = call i32 @closedir(i32* %27)
store i32 0, i32* %2
br label %29
29: ; preds = %25, %10
%30 = load i32, i32* %2
ret i32 %30
}

LLVM IR (-O2):
%struct.dirent = type { i8* }
@.str = [31 x i8] c"Could not open directory `%s`\0A\00"
@.str.1 = [29 x i8] c"Contents of directory `%s`:\0A\00"
@.str.2 = [6 x i8] c" %s\0A\00"
define i32 @showdir(i8* %0) {
%2 = tail call i32* @opendir(i8* %0)
%3 = icmp eq i32* %2, null
br i1 %3, label %4, label %6
4: ; preds = %1
%5 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %20
6: ; preds = %1
%7 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%8 = tail call %struct.dirent* @readdir(i32* %2)
%9 = icmp eq %struct.dirent* %8, null
br i1 %9, label %17, label %10
10: ; preds = %6, %10
%11 = phi %struct.dirent* [ %15, %10 ], [ %8, %6 ]
%12 = getelementptr inbounds %struct.dirent, %struct.dirent* %11, i64 0, i32 0
%13 = load i8*, i8** %12
%14 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* %13)
%15 = tail call %struct.dirent* @readdir(i32* %2)
%16 = icmp eq %struct.dirent* %15, null
br i1 %16, label %17, label %10
17: ; preds = %10, %6
%18 = tail call i32 @putchar(i32 10)
%19 = tail call i32 @closedir(i32* %2)
br label %20
20: ; preds = %17, %4
%21 = phi i32 [ 0, %17 ], [ 1, %4 ]
ret i32 %21
}