#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 O_CREAT;
int O_RDWR;
int O_TRUNC;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int open(char *, int, int);
int /*<<< orphan*/ *opendir(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
scalar_t__ strlen(char *);
int write(int, char *, scalar_t__);
int main(int argc, char **argv) {
  DIR *d;
  struct dirent *dir;
  int out_fd, ec = 0;
  if (argc < 3) {
    return -1;
  }
  out_fd = open(argv[2], O_CREAT | O_TRUNC | O_RDWR, 0644);
  if (out_fd < 0) {
    return -2;
  }
  d = opendir(argv[1]);
  if (d) {
    while ((dir = readdir(d)) != NULL) {
      if ((write(out_fd, dir->d_name, strlen(dir->d_name)) !=
           (int)strlen(dir->d_name)) ||
          (write(out_fd, "\n", strlen("\n")) != (int)strlen("\n"))) {
        ec = -3;
        break;
      }
    }
    closedir(d);
  }
  close(out_fd);
  return ec;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x28(%rbp)
cmpl   $0x3,-0x8(%rbp)
jge    11a3 <main+0x33>
movl   $0xffffffff,-0x4(%rbp)
jmp    12d3 <main+0x163>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
mov    0x2e46(%rip),%rax
mov    (%rax),%esi
mov    0x2e35(%rip),%rax
or     (%rax),%esi
mov    0x2e14(%rip),%rax
or     (%rax),%esi
mov    $0x1a4,%edx
call   1090 <open@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    11e9 <main+0x79>
movl   $0xfffffffe,-0x4(%rbp)
jmp    12d3 <main+0x163>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     12c5 <main+0x155>
jmp    120a <main+0x9a>
mov    -0x18(%rbp),%rdi
call   1080 <readdir@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     12bc <main+0x14c>
mov    -0x24(%rbp),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x38(%rbp)
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
call   1050 <strlen@plt>
mov    -0x3c(%rbp),%edi
mov    -0x38(%rbp),%rsi
mov    %rax,%rdx
call   1030 <write@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
call   1050 <strlen@plt>
mov    %rax,%rcx
mov    -0x2c(%rbp),%eax
cmp    %ecx,%eax
jne    12ab <main+0x13b>
mov    -0x24(%rbp),%eax
mov    %eax,-0x44(%rbp)
lea    0xd89(%rip),%rdi
call   1050 <strlen@plt>
mov    -0x44(%rbp),%edi
mov    %rax,%rdx
lea    0xd77(%rip),%rsi
call   1030 <write@plt>
mov    %eax,-0x40(%rbp)
lea    0xd68(%rip),%rdi
call   1050 <strlen@plt>
mov    %rax,%rcx
mov    -0x40(%rbp),%eax
cmp    %ecx,%eax
je     12b7 <main+0x147>
movl   $0xfffffffd,-0x28(%rbp)
jmp    12bc <main+0x14c>
jmp    120a <main+0x9a>
mov    -0x18(%rbp),%rdi
call   1070 <closedir@plt>
mov    -0x24(%rbp),%edi
call   1060 <close@plt>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    $0xffffffff,%ebp
cmp    $0x3,%edi
jl     1251 <main+0xe1>
mov    %rsi,%r14
mov    0x2e68(%rip),%rax
mov    0x2e59(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x10(%r14),%rdi
mov    0x2e32(%rip),%rax
or     (%rax),%esi
mov    $0x1a4,%edx
call   1090 <open@plt>
test   %eax,%eax
js     1234 <main+0xc4>
mov    %eax,%r12d
mov    0x8(%r14),%rdi
call   1040 <opendir@plt>
test   %rax,%rax
je     123b <main+0xcb>
mov    %rax,%r14
lea    0xe2f(%rip),%r15
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %r14,%rdi
call   1080 <readdir@plt>
test   %rax,%rax
je     123f <main+0xcf>
mov    %rax,%rbp
mov    (%rax),%rbx
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %r12d,%edi
mov    %rbx,%rsi
mov    %rax,%rdx
call   1030 <write@plt>
mov    %eax,%ebx
mov    0x0(%rbp),%rdi
call   1050 <strlen@plt>
mov    $0xfffffffd,%ebp
cmp    %eax,%ebx
jne    1241 <main+0xd1>
mov    $0x1,%edx
mov    %r12d,%edi
mov    %r15,%rsi
call   1030 <write@plt>
cmp    $0x1,%eax
je     11e0 <main+0x70>
jmp    1241 <main+0xd1>
mov    $0xfffffffe,%ebp
jmp    1251 <main+0xe1>
xor    %ebp,%ebp
jmp    1249 <main+0xd9>
xor    %ebp,%ebp
mov    %r14,%rdi
call   1070 <closedir@plt>
mov    %r12d,%edi
call   1060 <close@plt>
mov    %ebp,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.dirent = type { i8* }
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@O_RDWR = global i32 0
@.str = [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 %struct.dirent*
%8 = alloca i32
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %9
%10 = load i32, i32* %4
%11 = icmp slt i32 %10, 3
br i1 %11, label %12, label %13
12: ; preds = %2
store i32 -1, i32* %3
br label %70
13: ; preds = %2
%14 = load i8**, i8*** %5
%15 = getelementptr inbounds i8*, i8** %14, i64 2
%16 = load i8*, i8** %15
%17 = load i32, i32* @O_CREAT
%18 = load i32, i32* @O_TRUNC
%19 = or i32 %17, %18
%20 = load i32, i32* @O_RDWR
%21 = or i32 %19, %20
%22 = call i32 @open(i8* %16, i32 %21, i32 420)
store i32 %22, i32* %8
%23 = load i32, i32* %8
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %26
25: ; preds = %13
store i32 -2, i32* %3
br label %70
26: ; preds = %13
%27 = load i8**, i8*** %5
%28 = getelementptr inbounds i8*, i8** %27, i64 1
%29 = load i8*, i8** %28
%30 = call i32* @opendir(i8* %29)
store i32* %30, i32** %6
%31 = load i32*, i32** %6
%32 = icmp ne i32* %31, null
br i1 %32, label %33, label %66
33: ; preds = %26
br label %34
34: ; preds = %62, %33
%35 = load i32*, i32** %6
%36 = call %struct.dirent* @readdir(i32* %35)
store %struct.dirent* %36, %struct.dirent** %7
%37 = icmp ne %struct.dirent* %36, null
br i1 %37, label %38, label %63
38: ; preds = %34
%39 = load i32, i32* %8
%40 = load %struct.dirent*, %struct.dirent** %7
%41 = getelementptr inbounds %struct.dirent, %struct.dirent* %40, i32 0, i32 0
%42 = load i8*, i8** %41
%43 = load %struct.dirent*, %struct.dirent** %7
%44 = getelementptr inbounds %struct.dirent, %struct.dirent* %43, i32 0, i32 0
%45 = load i8*, i8** %44
%46 = call i64 @strlen(i8* %45)
%47 = call i32 @write(i32 %39, i8* %42, i64 %46)
%48 = load %struct.dirent*, %struct.dirent** %7
%49 = getelementptr inbounds %struct.dirent, %struct.dirent* %48, i32 0, i32 0
%50 = load i8*, i8** %49
%51 = call i64 @strlen(i8* %50)
%52 = trunc i64 %51 to i32
%53 = icmp ne i32 %47, %52
br i1 %53, label %61, label %54
54: ; preds = %38
%55 = load i32, i32* %8
%56 = call i64 @strlen(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%57 = call i32 @write(i32 %55, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i64 %56)
%58 = call i64 @strlen(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%59 = trunc i64 %58 to i32
%60 = icmp ne i32 %57, %59
br i1 %60, label %61, label %62
61: ; preds = %54, %38
store i32 -3, i32* %9
br label %63
62: ; preds = %54
br label %34
63: ; preds = %61, %34
%64 = load i32*, i32** %6
%65 = call i32 @closedir(i32* %64)
br label %66
66: ; preds = %63, %26
%67 = load i32, i32* %8
%68 = call i32 @close(i32 %67)
%69 = load i32, i32* %9
store i32 %69, i32* %3
br label %70
70: ; preds = %66, %25, %12
%71 = load i32, i32* %3
ret i32 %71
}

LLVM IR (-O2):
%struct.dirent = type { i8* }
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@O_RDWR = global i32 0
@.str = [2 x i8] c"\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp slt i32 %0, 3
br i1 %3, label %40, label %4
4: ; preds = %2
%5 = getelementptr inbounds i8*, i8** %1, i64 2
%6 = load i8*, i8** %5
%7 = load i32, i32* @O_CREAT
%8 = load i32, i32* @O_TRUNC
%9 = or i32 %8, %7
%10 = load i32, i32* @O_RDWR
%11 = or i32 %9, %10
%12 = tail call i32 @open(i8* %6, i32 %11, i32 420)
%13 = icmp slt i32 %12, 0
br i1 %13, label %40, label %14
14: ; preds = %4
%15 = getelementptr inbounds i8*, i8** %1, i64 1
%16 = load i8*, i8** %15
%17 = tail call i32* @opendir(i8* %16)
%18 = icmp eq i32* %17, null
br i1 %18, label %37, label %19
19: ; preds = %14, %31
%20 = tail call %struct.dirent* @readdir(i32* %17)
%21 = icmp eq %struct.dirent* %20, null
br i1 %21, label %34, label %22
22: ; preds = %19
%23 = getelementptr inbounds %struct.dirent, %struct.dirent* %20, i64 0, i32 0
%24 = load i8*, i8** %23
%25 = tail call i64 @strlen(i8* dereferenceable(1) %24)
%26 = tail call i32 @write(i32 %12, i8* %24, i64 %25)
%27 = load i8*, i8** %23
%28 = tail call i64 @strlen(i8* dereferenceable(1) %27)
%29 = trunc i64 %28 to i32
%30 = icmp eq i32 %26, %29
br i1 %30, label %31, label %34
31: ; preds = %22
%32 = tail call i32 @write(i32 %12, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i64 1)
%33 = icmp eq i32 %32, 1
br i1 %33, label %19, label %34
34: ; preds = %22, %31, %19
%35 = phi i32 [ 0, %19 ], [ -3, %31 ], [ -3, %22 ]
%36 = tail call i32 @closedir(i32* %17)
br label %37
37: ; preds = %34, %14
%38 = phi i32 [ %35, %34 ], [ 0, %14 ]
%39 = tail call i32 @close(i32 %12)
br label %40
40: ; preds = %4, %2, %37
%41 = phi i32 [ %38, %37 ], [ -1, %2 ], [ -2, %4 ]
ret i32 %41
}