#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;
};

/* Variables and functions */
int /*<<< orphan*/ closedir(void *);
scalar_t__ errno;
void *opendir(char *);
int /*<<< orphan*/ printf(char *, ...);
struct dirent *readdir(void *);
char *strerror(scalar_t__);
int main(int argc, char *argv[]) {
  int retval = 0;
  int prints = 0;
  struct dirent *dent = NULL;
  void *dir = opendir((argc == 1) ? (".") : (argv[1]));
  if (!dir) {
    printf("opendir failed: %s\n", strerror(errno));
    return 1;
  }
  for (;;) {
    errno = 0;
    dent = readdir(dir);
    if (!dent) {
      if (errno != 0) {
        printf("readdir failed: %s\n", strerror(errno));
        retval = 1;
        break;
      }
      break;
    }
    printf("%s\t\t", dent->d_name);
    if (++prints == 4) {
      prints = 0;
      printf("\n");
    }
  }
  printf("\n");
  closedir(dir);
  return retval;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
cmpl   $0x1,-0x8(%rbp)
jne    1196 <main+0x46>
lea    0xe73(%rip),%rax
mov    %rax,-0x30(%rbp)
jmp    11a2 <main+0x52>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11e6 <main+0x96>
mov    0x2e1f(%rip),%rax
mov    (%rax),%rdi
call   1070 <strerror@plt>
mov    %rax,%rsi
lea    0xe2f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12b1 <main+0x161>
jmp    11eb <main+0x9b>
mov    0x2dee(%rip),%rax
movq   $0x0,(%rax)
mov    -0x28(%rbp),%rdi
call   1060 <readdir@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1253 <main+0x103>
mov    0x2dc8(%rip),%rax
cmpq   $0x0,(%rax)
je     124e <main+0xfe>
mov    0x2db7(%rip),%rax
mov    (%rax),%rdi
call   1070 <strerror@plt>
mov    %rax,%rsi
lea    0xddb(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x14(%rbp)
jmp    1294 <main+0x144>
jmp    1294 <main+0x144>
mov    -0x20(%rbp),%rax
mov    (%rax),%rsi
lea    0xdc9(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
cmp    $0x4,%eax
jne    128f <main+0x13f>
movl   $0x0,-0x18(%rbp)
lea    0xda0(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    11eb <main+0x9b>
lea    0xd8d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x28(%rbp),%rdi
call   1050 <closedir@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
cmp    $0x1,%edi
jne    1175 <main+0x15>
lea    0xe8d(%rip),%rdi
jmp    1179 <main+0x19>
mov    0x8(%rsi),%rdi
call   1040 <opendir@plt>
test   %rax,%rax
je     120c <main+0xac>
mov    %rax,%rbx
mov    0x2e4f(%rip),%r15
movq   $0x0,(%r15)
mov    %rax,%rdi
call   1070 <readdir@plt>
test   %rax,%rax
je     11e7 <main+0x87>
lea    0xe7e(%rip),%r14
xor    %ebp,%ebp
jmp    11c4 <main+0x64>
movq   $0x0,(%r15)
mov    %rbx,%rdi
call   1070 <readdir@plt>
test   %rax,%rax
je     11e7 <main+0x87>
mov    (%rax),%rsi
mov    %r14,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
add    $0x1,%ebp
cmp    $0x4,%ebp
jne    11b0 <main+0x50>
mov    $0xa,%edi
call   1030 <putchar@plt>
xor    %ebp,%ebp
jmp    11b0 <main+0x50>
mov    (%r15),%rdi
test   %rdi,%rdi
je     1233 <main+0xd3>
call   1080 <strerror@plt>
lea    0xe1b(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%ebp
jmp    1235 <main+0xd5>
mov    0x2dcd(%rip),%rax
mov    (%rax),%rdi
call   1080 <strerror@plt>
lea    0xde0(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%ebp
jmp    1247 <main+0xe7>
xor    %ebp,%ebp
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    %rbx,%rdi
call   1060 <closedir@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

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