#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_type;
};
typedef int /*<<< orphan*/ DIR;

/* Variables and functions */
scalar_t__ DT_DIR;
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ print_long_format(char *, int);
int /*<<< orphan*/ printf(char *, char *, char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ strlcat(char *, char *, int);
int /*<<< orphan*/ strlcpy(char *, char *, int);
int do_ls(char *pathname, int flag) {
  DIR *directory;
  struct dirent *dir;
  int ret;
  char *slash = "/";
  char *symbol;
  char path_buffer[(50)];
  if (pathname == NULL)
    pathname = ".";
  directory = opendir(pathname);
  if (!directory)
    return 1;
  while ((dir = readdir(directory)) != NULL) {
    if (*dir->d_name == '.' && !(flag & 1)) {
      continue;
    }
    if (flag & 2) {
      strlcpy(path_buffer, pathname, (50));
      strlcat(path_buffer, "/", (50));
      strlcat(path_buffer, (char *)dir->d_name, (50));
      print_long_format(path_buffer, flag);
    } else {
      symbol = (dir->d_type == DT_DIR && *dir->d_name != '.') ? slash : "";
      printf("%s%ls  ", symbol, dir->d_name);
    }
  }
  closedir(directory);
  return 0;
}


x86 assembly (clang, -O0):
<do_ls>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
lea    0xe77(%rip),%rax
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11a3 <do_ls+0x33>
lea    0xe63(%rip),%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <opendir@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11c7 <do_ls+0x57>
movl   $0x1,-0x4(%rbp)
jmp    12eb <do_ls+0x17b>
jmp    11cc <do_ls+0x5c>
mov    -0x20(%rbp),%rdi
call   1080 <readdir@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
je     12db <do_ls+0x16b>
mov    -0x28(%rbp),%rax
mov    (%rax),%rax
movsbl (%rax),%eax
cmp    $0x2e,%eax
jne    120a <do_ls+0x9a>
mov    -0x14(%rbp),%eax
and    $0x1,%eax
cmp    $0x0,%eax
jne    120a <do_ls+0x9a>
jmp    11cc <do_ls+0x5c>
mov    -0x14(%rbp),%eax
and    $0x2,%eax
cmp    $0x0,%eax
je     1266 <do_ls+0xf6>
lea    -0x80(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    $0x32,%edx
call   1090 <strlcpy@plt>
lea    -0x80(%rbp),%rdi
lea    0xdca(%rip),%rsi
mov    $0x32,%edx
call   1040 <strlcat@plt>
lea    -0x80(%rbp),%rdi
mov    -0x28(%rbp),%rax
mov    (%rax),%rsi
mov    $0x32,%edx
call   1040 <strlcat@plt>
lea    -0x80(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1030 <print_long_format@plt>
jmp    12d6 <do_ls+0x166>
mov    -0x28(%rbp),%rax
mov    0x8(%rax),%rax
mov    0x2d6b(%rip),%rcx
cmp    (%rcx),%rax
jne    12a1 <do_ls+0x131>
mov    -0x28(%rbp),%rax
mov    (%rax),%rax
movsbl (%rax),%eax
cmp    $0x2e,%eax
je     12a1 <do_ls+0x131>
mov    -0x38(%rbp),%rax
mov    %rax,-0x88(%rbp)
jmp    12b4 <do_ls+0x144>
lea    0xd63(%rip),%rax
mov    %rax,-0x88(%rbp)
jmp    12b4 <do_ls+0x144>
mov    -0x88(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rsi
mov    -0x28(%rbp),%rax
mov    (%rax),%rdx
lea    0xd33(%rip),%rdi
call   1060 <printf@plt>
jmp    11cc <do_ls+0x5c>
mov    -0x20(%rbp),%rdi
call   1070 <closedir@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x90,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_ls>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %esi,%ebx
test   %rdi,%rdi
lea    0xe78(%rip),%r14
cmovne %rdi,%r14
mov    %r14,%rdi
call   1050 <opendir@plt>
test   %rax,%rax
je     126c <do_ls+0xfc>
mov    %rax,%r15
mov    %rax,%rdi
call   1080 <readdir@plt>
test   %rax,%rax
je     1260 <do_ls+0xf0>
mov    %rax,%r13
lea    0xe43(%rip),%r12
mov    %rsp,%rbp
jmp    120e <do_ls+0x9e>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
cmp    $0x2e,%al
mov    0x8(%r13),%rax
mov    %r12,%rsi
lea    0xe2b(%rip),%rdi
cmove  %rdi,%rsi
mov    0x2df5(%rip),%rcx
cmp    (%rcx),%rax
cmovne %rdi,%rsi
lea    0xe0b(%rip),%rdi
call   1060 <printf@plt>
mov    %r15,%rdi
call   1080 <readdir@plt>
mov    %rax,%r13
test   %rax,%rax
je     1260 <do_ls+0xf0>
mov    0x0(%r13),%rdx
movzbl (%rdx),%eax
test   $0x1,%bl
jne    121e <do_ls+0xae>
cmp    $0x2e,%al
je     11fe <do_ls+0x8e>
test   $0x2,%bl
je     11d0 <do_ls+0x60>
mov    %rbp,%rdi
mov    %r14,%rsi
mov    $0x32,%edx
call   1090 <strlcpy@plt>
mov    %rbp,%rdi
mov    %r12,%rsi
mov    $0x32,%edx
call   1040 <strlcat@plt>
mov    0x0(%r13),%rsi
mov    %rbp,%rdi
mov    $0x32,%edx
call   1040 <strlcat@plt>
mov    %rbp,%rdi
mov    %ebx,%esi
call   1030 <print_long_format@plt>
jmp    11fe <do_ls+0x8e>
mov    %r15,%rdi
call   1070 <closedir@plt>
xor    %eax,%eax
jmp    1271 <do_ls+0x101>
mov    $0x1,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.dirent = type { i8*, i64 }
@.str = [2 x i8] c"/\00"
@.str.1 = [2 x i8] c".\00"
@DT_DIR = global i64 0
@.str.2 = [1 x i8] zeroinitializer
@.str.3 = [8 x i8] c"%s%ls \00"
define i32 @do_ls(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32*
%7 = alloca %struct.dirent*
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca [50 x i8]
store i8* %0, i8** %4
store i32 %1, i32* %5
store i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8** %9
%12 = load i8*, i8** %4
%13 = icmp eq i8* %12, null
br i1 %13, label %14, label %15
14: ; preds = %2
store i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i8** %4
br label %15
15: ; preds = %14, %2
%16 = load i8*, i8** %4
%17 = call i32* @opendir(i8* %16)
store i32* %17, i32** %6
%18 = load i32*, i32** %6
%19 = icmp ne i32* %18, null
br i1 %19, label %21, label %20
20: ; preds = %15
store i32 1, i32* %3
br label %83
21: ; preds = %15
br label %22
22: ; preds = %79, %37, %21
%23 = load i32*, i32** %6
%24 = call %struct.dirent* @readdir(i32* %23)
store %struct.dirent* %24, %struct.dirent** %7
%25 = icmp ne %struct.dirent* %24, null
br i1 %25, label %26, label %80
26: ; preds = %22
%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 = load i8, i8* %29
%31 = sext i8 %30 to i32
%32 = icmp eq i32 %31, 46
br i1 %32, label %33, label %38
33: ; preds = %26
%34 = load i32, i32* %5
%35 = and i32 %34, 1
%36 = icmp ne i32 %35, 0
br i1 %36, label %38, label %37
37: ; preds = %33
br label %22
38: ; preds = %33, %26
%39 = load i32, i32* %5
%40 = and i32 %39, 2
%41 = icmp ne i32 %40, 0
br i1 %41, label %42, label %56
42: ; preds = %38
%43 = getelementptr inbounds [50 x i8], [50 x i8]* %11, i64 0, i64 0
%44 = load i8*, i8** %4
%45 = call i32 @strlcpy(i8* %43, i8* %44, i32 50)
%46 = getelementptr inbounds [50 x i8], [50 x i8]* %11, i64 0, i64 0
%47 = call i32 @strlcat(i8* %46, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i32 50)
%48 = getelementptr inbounds [50 x i8], [50 x i8]* %11, i64 0, i64 0
%49 = load %struct.dirent*, %struct.dirent** %7
%50 = getelementptr inbounds %struct.dirent, %struct.dirent* %49, i32 0, i32 0
%51 = load i8*, i8** %50
%52 = call i32 @strlcat(i8* %48, i8* %51, i32 50)
%53 = getelementptr inbounds [50 x i8], [50 x i8]* %11, i64 0, i64 0
%54 = load i32, i32* %5
%55 = call i32 @print_long_format(i8* %53, i32 %54)
br label %79
56: ; preds = %38
%57 = load %struct.dirent*, %struct.dirent** %7
%58 = getelementptr inbounds %struct.dirent, %struct.dirent* %57, i32 0, i32 1
%59 = load i64, i64* %58
%60 = load i64, i64* @DT_DIR
%61 = icmp eq i64 %59, %60
br i1 %61, label %62, label %71
62: ; preds = %56
%63 = load %struct.dirent*, %struct.dirent** %7
%64 = getelementptr inbounds %struct.dirent, %struct.dirent* %63, i32 0, i32 0
%65 = load i8*, i8** %64
%66 = load i8, i8* %65
%67 = sext i8 %66 to i32
%68 = icmp ne i32 %67, 46
br i1 %68, label %69, label %71
69: ; preds = %62
%70 = load i8*, i8** %9
br label %72
71: ; preds = %62, %56
br label %72
72: ; preds = %71, %69
%73 = phi i8* [ %70, %69 ], [ getelementptr inbounds ([1 x i8], [1 x i8]* @.str.2, i64 0, i64 0), %71 ]
store i8* %73, i8** %10
%74 = load i8*, i8** %10
%75 = load %struct.dirent*, %struct.dirent** %7
%76 = getelementptr inbounds %struct.dirent, %struct.dirent* %75, i32 0, i32 0
%77 = load i8*, i8** %76
%78 = call i32 @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i8* %74, i8* %77)
br label %79
79: ; preds = %72, %42
br label %22
80: ; preds = %22
%81 = load i32*, i32** %6
%82 = call i32 @closedir(i32* %81)
store i32 0, i32* %3
br label %83
83: ; preds = %80, %20
%84 = load i32, i32* %3
ret i32 %84
}

LLVM IR (-O2):
%struct.dirent = type { i8*, i64 }
@.str = [2 x i8] c"/\00"
@.str.1 = [2 x i8] c".\00"
@DT_DIR = global i64 0
@.str.2 = [1 x i8] zeroinitializer
@.str.3 = [8 x i8] c"%s%ls \00"
define i32 @do_ls(i8* %0, i32 %1) {
%3 = alloca [50 x i8]
%4 = getelementptr inbounds [50 x i8], [50 x i8]* %3, i64 0, i64 0
%5 = icmp eq i8* %0, null
%6 = select i1 %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i8* %0
%7 = tail call i32* @opendir(i8* %6)
%8 = icmp eq i32* %7, null
br i1 %8, label %44, label %9
9: ; preds = %2
%10 = call %struct.dirent* @readdir(i32* %7)
%11 = icmp eq %struct.dirent* %10, null
br i1 %11, label %42, label %12
12: ; preds = %9
%13 = and i32 %1, 1
%14 = icmp eq i32 %13, 0
%15 = and i32 %1, 2
%16 = icmp eq i32 %15, 0
br label %17
17: ; preds = %12, %39
%18 = phi %struct.dirent* [ %10, %12 ], [ %40, %39 ]
%19 = getelementptr inbounds %struct.dirent, %struct.dirent* %18, i64 0, i32 0
%20 = load i8*, i8** %19
%21 = load i8, i8* %20
%22 = icmp eq i8 %21, 46
%23 = and i1 %14, %22
br i1 %23, label %39, label %24
24: ; preds = %17
br i1 %16, label %31, label %25
25: ; preds = %24
%26 = call i32 @strlcpy(i8* %4, i8* %6, i32 50)
%27 = call i32 @strlcat(i8* %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i32 50)
%28 = load i8*, i8** %19
%29 = call i32 @strlcat(i8* %4, i8* %28, i32 50)
%30 = call i32 @print_long_format(i8* %4, i32 %1)
br label %39
31: ; preds = %24
%32 = getelementptr inbounds %struct.dirent, %struct.dirent* %18, i64 0, i32 1
%33 = load i64, i64* %32
%34 = load i64, i64* @DT_DIR
%35 = icmp eq i64 %33, %34
%36 = select i1 %22, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0)
%37 = select i1 %35, i8* %36, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.2, i64 0, i64 0)
%38 = call i32 @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i8* %37, i8* %20)
br label %39
39: ; preds = %25, %31, %17
%40 = call %struct.dirent* @readdir(i32* %7)
%41 = icmp eq %struct.dirent* %40, null
br i1 %41, label %42, label %17
42: ; preds = %39, %9
%43 = call i32 @closedir(i32* %7)
br label %44
44: ; preds = %2, %42
%45 = phi i32 [ 0, %42 ], [ 1, %2 ]
ret i32 %45
}