#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*/ *);
scalar_t__ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
struct dirent *readdir(int /*<<< orphan*/ *);
scalar_t__ strcmp(char *, char *);
void my_ls(char *root) {
  DIR *dir;
  struct dirent *ptr;
  int count;
  dir = opendir(root);
  if (dir == NULL) {
    perror("fail to open dir");
    exit(1);
  }
  errno = 0;
  while ((ptr = readdir(dir)) != NULL) {
    if (strcmp(ptr->d_name, ".") == 0 || strcmp(ptr->d_name, "..") == 0)
      continue;
    printf("%s\t", ptr->d_name);
    count++;
    if (count % 5 == 0)
      printf("\n");
  }
  if (errno != 0) {
    perror("fail to read dir");
    exit(1);
  }
  closedir(dir);
}


x86 assembly (clang, -O0):
<my_ls>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11aa <my_ls+0x3a>
lea    0xe65(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    0x2e2f(%rip),%rax
movq   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
call   1070 <readdir@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     1253 <my_ls+0xe3>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
lea    0xe35(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
je     1209 <my_ls+0x99>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
lea    0xe17(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
jne    120e <my_ls+0x9e>
jmp    11b8 <my_ls+0x48>
mov    -0x18(%rbp),%rax
mov    (%rax),%rsi
lea    0xdf8(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    $0x5,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
jne    124e <my_ls+0xde>
lea    0xdd1(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    11b8 <my_ls+0x48>
mov    0x2d86(%rip),%rax
cmpq   $0x0,(%rax)
je     127a <my_ls+0x10a>
lea    0xdaf(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x10(%rbp),%rdi
call   1050 <closedir@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<my_ls>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
call   1040 <opendir@plt>
test   %rax,%rax
je     1251 <my_ls+0xd1>
mov    %rax,%r13
mov    0x2e3d(%rip),%rbp
movq   $0x0,0x0(%rbp)
mov    %rax,%rdi
call   1080 <readdir@plt>
test   %rax,%rax
je     1234 <my_ls+0xb4>
mov    %rax,%rbx
lea    0xe4c(%rip),%r14
lea    0xe44(%rip),%r15
jmp    11e0 <my_ls+0x60>
nop
mov    %r13,%rdi
call   1080 <readdir@plt>
mov    %rax,%rbx
test   %rax,%rax
je     1234 <my_ls+0xb4>
mov    (%rbx),%rdi
mov    %r14,%rsi
call   1070 <strcmp@plt>
test   %rax,%rax
je     11d0 <my_ls+0x50>
mov    (%rbx),%rdi
mov    %r15,%rsi
call   1070 <strcmp@plt>
test   %rax,%rax
je     11d0 <my_ls+0x50>
mov    (%rbx),%rsi
lea    0xe0a(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
add    $0x1,%r12d
imul   $0xcccccccd,%r12d,%eax
add    $0x19999999,%eax
cmp    $0x33333332,%eax
ja     11d0 <my_ls+0x50>
mov    $0xa,%edi
call   1030 <putchar@plt>
jmp    11d0 <my_ls+0x50>
cmpq   $0x0,0x0(%rbp)
jne    125a <my_ls+0xda>
mov    %r13,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1060 <closedir@plt>
lea    0xda8(%rip),%rdi
jmp    1261 <my_ls+0xe1>
lea    0xdb7(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
%struct.dirent = type { i8* }
@.str = [17 x i8] c"fail to open dir\00"
@errno = global i64 0
@.str.1 = [2 x i8] c".\00"
@.str.2 = [3 x i8] c"..\00"
@.str.3 = [4 x i8] c"%s\09\00"
@.str.4 = [2 x i8] c"\0A\00"
@.str.5 = [17 x i8] c"fail to read dir\00"
define void @my_ls(i8* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca %struct.dirent*
%5 = alloca i32
store i8* %0, i8** %2
%6 = load i8*, i8** %2
%7 = call i32* @opendir(i8* %6)
store i32* %7, i32** %3
%8 = load i32*, i32** %3
%9 = icmp eq i32* %8, null
br i1 %9, label %10, label %13
10: ; preds = %1
%11 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
%12 = call i32 @exit(i32 1)
unreachable
13: ; preds = %1
store i64 0, i64* @errno
br label %14
14: ; preds = %43, %30, %13
%15 = load i32*, i32** %3
%16 = call %struct.dirent* @readdir(i32* %15)
store %struct.dirent* %16, %struct.dirent** %4
%17 = icmp ne %struct.dirent* %16, null
br i1 %17, label %18, label %44
18: ; preds = %14
%19 = load %struct.dirent*, %struct.dirent** %4
%20 = getelementptr inbounds %struct.dirent, %struct.dirent* %19, i32 0, i32 0
%21 = load i8*, i8** %20
%22 = call i64 @strcmp(i8* %21, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%23 = icmp eq i64 %22, 0
br i1 %23, label %30, label %24
24: ; preds = %18
%25 = load %struct.dirent*, %struct.dirent** %4
%26 = getelementptr inbounds %struct.dirent, %struct.dirent* %25, i32 0, i32 0
%27 = load i8*, i8** %26
%28 = call i64 @strcmp(i8* %27, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%29 = icmp eq i64 %28, 0
br i1 %29, label %30, label %31
30: ; preds = %24, %18
br label %14
31: ; preds = %24
%32 = load %struct.dirent*, %struct.dirent** %4
%33 = getelementptr inbounds %struct.dirent, %struct.dirent* %32, i32 0, i32 0
%34 = load i8*, i8** %33
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* %34)
%36 = load i32, i32* %5
%37 = add nsw i32 %36, 1
store i32 %37, i32* %5
%38 = load i32, i32* %5
%39 = srem i32 %38, 5
%40 = icmp eq i32 %39, 0
br i1 %40, label %41, label %43
41: ; preds = %31
%42 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
br label %43
43: ; preds = %41, %31
br label %14
44: ; preds = %14
%45 = load i64, i64* @errno
%46 = icmp ne i64 %45, 0
br i1 %46, label %47, label %50
47: ; preds = %44
%48 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.5, i64 0, i64 0))
%49 = call i32 @exit(i32 1)
unreachable
50: ; preds = %44
%51 = load i32*, i32** %3
%52 = call i32 @closedir(i32* %51)
ret void
}

LLVM IR (-O2):
%struct.dirent = type { i8* }
@.str = [17 x i8] c"fail to open dir\00"
@errno = global i64 0
@.str.1 = [2 x i8] c".\00"
@.str.2 = [3 x i8] c"..\00"
@.str.3 = [4 x i8] c"%s\09\00"
@.str.5 = [17 x i8] c"fail to read dir\00"
define void @my_ls(i8* nocapture %0) {
%2 = tail call i32* @opendir(i8* %0)
%3 = icmp eq i32* %2, null
br i1 %3, label %4, label %7
4: ; preds = %1
%5 = tail call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
%6 = tail call i32 @exit(i32 1)
unreachable
7: ; preds = %1
store i64 0, i64* @errno
%8 = tail call %struct.dirent* @readdir(i32* %2)
%9 = icmp eq %struct.dirent* %8, null
br i1 %9, label %37, label %10
10: ; preds = %7, %34
%11 = phi %struct.dirent* [ %35, %34 ], [ %8, %7 ]
%12 = phi i32 [ %29, %34 ], [ undef, %7 ]
br label %13
13: ; preds = %10, %23
%14 = phi %struct.dirent* [ %11, %10 ], [ %24, %23 ]
%15 = getelementptr inbounds %struct.dirent, %struct.dirent* %14, i64 0, i32 0
%16 = load i8*, i8** %15
%17 = tail call i64 @strcmp(i8* %16, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%18 = icmp eq i64 %17, 0
br i1 %18, label %23, label %19
19: ; preds = %13
%20 = load i8*, i8** %15
%21 = tail call i64 @strcmp(i8* %20, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%22 = icmp eq i64 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %19, %13
%24 = tail call %struct.dirent* @readdir(i32* %2)
%25 = icmp eq %struct.dirent* %24, null
br i1 %25, label %37, label %13
26: ; preds = %19
%27 = load i8*, i8** %15
%28 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* %27)
%29 = add nsw i32 %12, 1
%30 = srem i32 %29, 5
%31 = icmp eq i32 %30, 0
br i1 %31, label %32, label %34
32: ; preds = %26
%33 = tail call i32 @putchar(i32 10)
br label %34
34: ; preds = %32, %26
%35 = tail call %struct.dirent* @readdir(i32* %2)
%36 = icmp eq %struct.dirent* %35, null
br i1 %36, label %37, label %10
37: ; preds = %34, %23, %7
%38 = load i64, i64* @errno
%39 = icmp eq i64 %38, 0
br i1 %39, label %43, label %40
40: ; preds = %37
%41 = tail call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.5, i64 0, i64 0))
%42 = tail call i32 @exit(i32 1)
unreachable
43: ; preds = %37
%44 = tail call i32 @closedir(i32* %2)
ret void
}