#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 {
  scalar_t__ d_type;
  char *d_name;
};
typedef int /*<<< orphan*/ DIR;

/* Variables and functions */
scalar_t__ DT_DIR;
scalar_t__ DT_REG;
int /*<<< orphan*/ alphasort;
int /*<<< orphan*/ clear_screen();
int /*<<< orphan*/ free(char *);
char *get_dist_path(char *, int /*<<< orphan*/);
char *get_workdir();
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ printf(char *, ...);
struct dirent *readdir(int /*<<< orphan*/ *);
int scandir(char *, struct dirent ***, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strlen(char *);
void print_distilleries(void) {
  clear_screen();
  char *workdir = get_workdir();
  struct dirent **dirlist, *filelist;
  int i, offset = 2, n = scandir(workdir, &dirlist, 0, alphasort);
  if (n <= 0)
    printf("No distilleries found in %s\n", workdir);
  else {
    printf("%3s %12s %12s\n", "No", "Distillery", "Reviews");
    for (i = offset; i < n; i++) {
      if (DT_DIR == dirlist[i]->d_type) {
        char *dirname = dirlist[i]->d_name;
        char *distillery_path = get_dist_path(dirname, strlen(dirname));
        DIR *dir = opendir(distillery_path);
        int reviews = 0;
        while ((filelist = readdir(dir)) != NULL) {
          if (DT_REG == filelist->d_type) {
            reviews++;
          }
        }
        printf("\033[32m[\033[39m %d \033[32m]\033[39m %-15s (%d)\n", i - 1,
               dirname, reviews);
        free(distillery_path);
      }
    }
  }
  free(workdir);
}


x86 assembly (clang, -O0):
<print_distilleries>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    $0x0,%al
call   10a0 <clear_screen@plt>
mov    $0x0,%al
call   1040 <get_workdir@plt>
mov    %rax,-0x8(%rbp)
movl   $0x2,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e14(%rip),%rax
mov    (%rax),%ecx
lea    -0x10(%rbp),%rsi
xor    %edx,%edx
call   1080 <scandir@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jg     11ed <print_distilleries+0x5d>
mov    -0x8(%rbp),%rsi
lea    0xe1f(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
jmp    1301 <print_distilleries+0x171>
lea    0xe29(%rip),%rdi
lea    0xe31(%rip),%rsi
lea    0xe2d(%rip),%rdx
lea    0xe31(%rip),%rcx
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x24(%rbp),%eax
jge    12fc <print_distilleries+0x16c>
mov    0x2daf(%rip),%rax
mov    (%rax),%rax
mov    -0x10(%rbp),%rcx
movslq -0x1c(%rbp),%rdx
mov    (%rcx,%rdx,8),%rcx
cmp    (%rcx),%rax
jne    12e9 <print_distilleries+0x159>
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x30(%rbp),%rdi
call   1060 <strlen@plt>
mov    -0x50(%rbp),%rdi
mov    %eax,%esi
call   10b0 <get_dist_path@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
call   1050 <opendir@plt>
mov    %rax,-0x40(%rbp)
movl   $0x0,-0x44(%rbp)
mov    -0x40(%rbp),%rdi
call   1090 <readdir@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     12c5 <print_distilleries+0x135>
mov    0x2d39(%rip),%rax
mov    (%rax),%rax
mov    -0x18(%rbp),%rcx
cmp    (%rcx),%rax
jne    12c0 <print_distilleries+0x130>
mov    -0x44(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x44(%rbp)
jmp    1289 <print_distilleries+0xf9>
mov    -0x1c(%rbp),%esi
sub    $0x1,%esi
mov    -0x30(%rbp),%rdx
mov    -0x44(%rbp),%ecx
lea    0xd69(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
jmp    12ee <print_distilleries+0x15e>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1216 <print_distilleries+0x86>
jmp    1301 <print_distilleries+0x171>
mov    -0x8(%rbp),%rdi
call   1030 <free@plt>
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<print_distilleries>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
xor    %eax,%eax
call   10a0 <clear_screen@plt>
xor    %eax,%eax
call   1040 <get_workdir@plt>
mov    %rax,%rbp
mov    0x2e1a(%rip),%rax
mov    (%rax),%ecx
lea    0x10(%rsp),%rsi
mov    %rbp,%rdi
xor    %edx,%edx
call   1080 <scandir@plt>
test   %eax,%eax
mov    %rbp,(%rsp)
jle    12cf <print_distilleries+0x13f>
mov    %eax,%ebx
lea    0xe41(%rip),%rdi
lea    0xe49(%rip),%rsi
lea    0xe45(%rip),%rdx
lea    0xe49(%rip),%rcx
xor    %eax,%eax
call   1070 <printf@plt>
cmp    $0x3,%ebx
jb     12e0 <print_distilleries+0x150>
mov    %ebx,%edx
mov    0x2dce(%rip),%rax
mov    (%rax),%rax
mov    $0x2,%r15d
mov    0x2dc6(%rip),%r14
mov    %rdx,0x8(%rsp)
jmp    1267 <print_distilleries+0xd7>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
lea    -0x1(%r15),%esi
lea    0xe07(%rip),%rdi
mov    %r13,%rdx
mov    %ebp,%ecx
xor    %eax,%eax
call   1070 <printf@plt>
mov    %r12,%rdi
call   1030 <free@plt>
mov    0x2d82(%rip),%rax
mov    (%rax),%rax
mov    0x8(%rsp),%rdx
add    $0x1,%r15
cmp    %rdx,%r15
je     12e0 <print_distilleries+0x150>
mov    0x10(%rsp),%rcx
mov    (%rcx,%r15,8),%rcx
cmp    (%rcx),%rax
jne    125e <print_distilleries+0xce>
mov    0x8(%rcx),%r13
mov    %r13,%rdi
call   1060 <strlen@plt>
mov    %r13,%rdi
mov    %eax,%esi
call   10b0 <get_dist_path@plt>
mov    %rax,%r12
mov    %rax,%rdi
call   1050 <opendir@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1090 <readdir@plt>
xor    %ebp,%ebp
test   %rax,%rax
je     1230 <print_distilleries+0xa0>
nopl   0x0(%rax,%rax,1)
mov    (%r14),%rcx
xor    %edx,%edx
cmp    (%rax),%rcx
sete   %dl
add    %edx,%ebp
mov    %rbx,%rdi
call   1090 <readdir@plt>
test   %rax,%rax
jne    12b0 <print_distilleries+0x120>
jmp    1230 <print_distilleries+0xa0>
lea    0xd2a(%rip),%rdi
mov    %rbp,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
mov    (%rsp),%rdi
call   1030 <free@plt>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.dirent = type { i64, i8* }
@alphasort = global i32 0
@.str = [29 x i8] c"No distilleries found in %s\0A\00"
@.str.1 = [15 x i8] c"%3s %12s %12s\0A\00"
@.str.2 = [3 x i8] c"No\00"
@.str.3 = [11 x i8] c"Distillery\00"
@.str.4 = [8 x i8] c"Reviews\00"
@DT_DIR = global i64 0
@DT_REG = global i64 0
@.str.5 = [39 x i8] c"\1B[32m[\1B[39m %d \1B[32m]\1B[39m %-15s (%d)\0A\00"
define void @print_distilleries() {
%1 = alloca i8*
%2 = alloca %struct.dirent**
%3 = alloca %struct.dirent*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32*
%10 = alloca i32
%11 = call i32 (...) @clear_screen()
%12 = call i8* (...) @get_workdir()
store i8* %12, i8** %1
store i32 2, i32* %5
%13 = load i8*, i8** %1
%14 = load i32, i32* @alphasort
%15 = call i32 @scandir(i8* %13, %struct.dirent*** %2, i32 0, i32 %14)
store i32 %15, i32* %6
%16 = load i32, i32* %6
%17 = icmp sle i32 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %0
%19 = load i8*, i8** %1
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i8* %19)
br label %79
21: ; preds = %0
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0))
%23 = load i32, i32* %5
store i32 %23, i32* %4
br label %24
24: ; preds = %75, %21
%25 = load i32, i32* %4
%26 = load i32, i32* %6
%27 = icmp slt i32 %25, %26
br i1 %27, label %28, label %78
28: ; preds = %24
%29 = load i64, i64* @DT_DIR
%30 = load %struct.dirent**, %struct.dirent*** %2
%31 = load i32, i32* %4
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds %struct.dirent*, %struct.dirent** %30, i64 %32
%34 = load %struct.dirent*, %struct.dirent** %33
%35 = getelementptr inbounds %struct.dirent, %struct.dirent* %34, i32 0, i32 0
%36 = load i64, i64* %35
%37 = icmp eq i64 %29, %36
br i1 %37, label %38, label %74
38: ; preds = %28
%39 = load %struct.dirent**, %struct.dirent*** %2
%40 = load i32, i32* %4
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds %struct.dirent*, %struct.dirent** %39, i64 %41
%43 = load %struct.dirent*, %struct.dirent** %42
%44 = getelementptr inbounds %struct.dirent, %struct.dirent* %43, i32 0, i32 1
%45 = load i8*, i8** %44
store i8* %45, i8** %7
%46 = load i8*, i8** %7
%47 = load i8*, i8** %7
%48 = call i32 @strlen(i8* %47)
%49 = call i8* @get_dist_path(i8* %46, i32 %48)
store i8* %49, i8** %8
%50 = load i8*, i8** %8
%51 = call i32* @opendir(i8* %50)
store i32* %51, i32** %9
store i32 0, i32* %10
br label %52
52: ; preds = %65, %38
%53 = load i32*, i32** %9
%54 = call %struct.dirent* @readdir(i32* %53)
store %struct.dirent* %54, %struct.dirent** %3
%55 = icmp ne %struct.dirent* %54, null
br i1 %55, label %56, label %66
56: ; preds = %52
%57 = load i64, i64* @DT_REG
%58 = load %struct.dirent*, %struct.dirent** %3
%59 = getelementptr inbounds %struct.dirent, %struct.dirent* %58, i32 0, i32 0
%60 = load i64, i64* %59
%61 = icmp eq i64 %57, %60
br i1 %61, label %62, label %65
62: ; preds = %56
%63 = load i32, i32* %10
%64 = add nsw i32 %63, 1
store i32 %64, i32* %10
br label %65
65: ; preds = %62, %56
br label %52
66: ; preds = %52
%67 = load i32, i32* %4
%68 = sub nsw i32 %67, 1
%69 = load i8*, i8** %7
%70 = load i32, i32* %10
%71 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.5, i64 0, i64 0), i32 %68, i8* %69, i32 %70)
%72 = load i8*, i8** %8
%73 = call i32 @free(i8* %72)
br label %74
74: ; preds = %66, %28
br label %75
75: ; preds = %74
%76 = load i32, i32* %4
%77 = add nsw i32 %76, 1
store i32 %77, i32* %4
br label %24
78: ; preds = %24
br label %79
79: ; preds = %78, %18
%80 = load i8*, i8** %1
%81 = call i32 @free(i8* %80)
ret void
}

LLVM IR (-O2):
%struct.dirent = type { i64, i8* }
@alphasort = global i32 0
@.str = [29 x i8] c"No distilleries found in %s\0A\00"
@.str.1 = [15 x i8] c"%3s %12s %12s\0A\00"
@.str.2 = [3 x i8] c"No\00"
@.str.3 = [11 x i8] c"Distillery\00"
@.str.4 = [8 x i8] c"Reviews\00"
@DT_DIR = global i64 0
@DT_REG = global i64 0
@.str.5 = [39 x i8] c"\1B[32m[\1B[39m %d \1B[32m]\1B[39m %-15s (%d)\0A\00"
define void @print_distilleries() {
%1 = alloca %struct.dirent**
%2 = tail call i32 (...) @clear_screen()
%3 = tail call i8* (...) @get_workdir()
%4 = bitcast %struct.dirent*** %1 to i8*
%5 = load i32, i32* @alphasort
%6 = call i32 @scandir(i8* %3, %struct.dirent*** %1, i32 0, i32 %5)
%7 = icmp slt i32 %6, 1
br i1 %7, label %8, label %10
8: ; preds = %0
%9 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i8* %3)
br label %55
10: ; preds = %0
%11 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0))
%12 = icmp ugt i32 %6, 2
br i1 %12, label %13, label %55
13: ; preds = %10
%14 = zext i32 %6 to i64
%15 = load i64, i64* @DT_DIR
br label %16
16: ; preds = %13, %51
%17 = phi i64 [ %15, %13 ], [ %52, %51 ]
%18 = phi i64 [ 2, %13 ], [ %53, %51 ]
%19 = load %struct.dirent**, %struct.dirent*** %1
%20 = getelementptr inbounds %struct.dirent*, %struct.dirent** %19, i64 %18
%21 = load %struct.dirent*, %struct.dirent** %20
%22 = getelementptr inbounds %struct.dirent, %struct.dirent* %21, i64 0, i32 0
%23 = load i64, i64* %22
%24 = icmp eq i64 %17, %23
br i1 %24, label %25, label %51
25: ; preds = %16
%26 = getelementptr inbounds %struct.dirent, %struct.dirent* %21, i64 0, i32 1
%27 = load i8*, i8** %26
%28 = call i32 @strlen(i8* %27)
%29 = call i8* @get_dist_path(i8* %27, i32 %28)
%30 = call i32* @opendir(i8* %29)
%31 = call %struct.dirent* @readdir(i32* %30)
%32 = icmp eq %struct.dirent* %31, null
br i1 %32, label %44, label %33
33: ; preds = %25, %33
%34 = phi %struct.dirent* [ %42, %33 ], [ %31, %25 ]
%35 = phi i32 [ %41, %33 ], [ 0, %25 ]
%36 = load i64, i64* @DT_REG
%37 = getelementptr inbounds %struct.dirent, %struct.dirent* %34, i64 0, i32 0
%38 = load i64, i64* %37
%39 = icmp eq i64 %36, %38
%40 = zext i1 %39 to i32
%41 = add nuw nsw i32 %35, %40
%42 = call %struct.dirent* @readdir(i32* %30)
%43 = icmp eq %struct.dirent* %42, null
br i1 %43, label %44, label %33
44: ; preds = %33, %25
%45 = phi i32 [ 0, %25 ], [ %41, %33 ]
%46 = trunc i64 %18 to i32
%47 = add i32 %46, -1
%48 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([39 x i8], [39 x i8]* @.str.5, i64 0, i64 0), i32 %47, i8* %27, i32 %45)
%49 = call i32 @free(i8* %29)
%50 = load i64, i64* @DT_DIR
br label %51
51: ; preds = %16, %44
%52 = phi i64 [ %17, %16 ], [ %50, %44 ]
%53 = add nuw nsw i64 %18, 1
%54 = icmp eq i64 %53, %14
br i1 %54, label %55, label %16
55: ; preds = %51, %10, %8
%56 = call i32 @free(i8* %3)
ret void
}