#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;
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ printf(char *, char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int ls(char *s) {
  DIR *dir = opendir(s);
  if (dir) {
    struct dirent *s_dir;
    while ((s_dir = readdir(dir))) {
      if (s_dir->d_type == DT_DIR) {
        printf("/%s\n", s_dir->d_name);
      } else {
        printf("%s\n", s_dir->d_name);
      }
    }
    return 0;
  } else {
    return -1;
  }
}


x86 assembly (clang, -O0):
<ls>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     11c5 <ls+0x95>
jmp    1159 <ls+0x29>
mov    -0x18(%rbp),%rdi
call   1050 <readdir@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     11b9 <ls+0x89>
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
mov    0x2e62(%rip),%rcx
cmp    (%rcx),%rax
jne    11a0 <ls+0x70>
mov    -0x20(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xe6a(%rip),%rdi
call   1040 <printf@plt>
jmp    11b4 <ls+0x84>
mov    -0x20(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xe52(%rip),%rdi
call   1040 <printf@plt>
jmp    1159 <ls+0x29>
movl   $0x0,-0x4(%rbp)
jmp    11cc <ls+0x9c>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ls>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
call   1030 <opendir@plt>
test   %rax,%rax
je     1195 <ls+0x65>
mov    %rax,%rbx
mov    %rax,%rdi
call   1050 <readdir@plt>
xor    %r14d,%r14d
test   %rax,%rax
je     119b <ls+0x6b>
mov    0x2e84(%rip),%r15
lea    0xe9d(%rip),%r12
lea    0xe97(%rip),%rbp
nopw   0x0(%rax,%rax,1)
mov    (%rax),%rcx
mov    0x8(%rax),%rsi
cmp    (%r15),%rcx
mov    %rbp,%rdi
cmove  %r12,%rdi
call   1040 <printf@plt>
mov    %rbx,%rdi
call   1050 <readdir@plt>
test   %rax,%rax
jne    1170 <ls+0x40>
jmp    119b <ls+0x6b>
mov    $0xffffffff,%r14d
mov    %r14d,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

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