#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 */
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int EXIT_SUCCESS;
scalar_t__ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ free(char *);
int getline(char **, size_t *, int /*<<< orphan*/ *);
int pclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ *popen(char *, char *);
int /*<<< orphan*/ stderr;
int main(int argc, char **argv) {
  if (argc != 2) {
    fprintf(stderr, "Line %d\n", 27);
    perror("Wrong number of arguments"
           "\n");
    if (1) {
      exit(EXIT_FAILURE);
    }
    errno = 0;
  }
  FILE *p_file = popen("ls -l | grep ^d", "r");
  if (p_file == NULL) {
    fprintf(stderr, "Line %d\n", 30);
    perror("Bad popen"
           "\n");
    if (1) {
      exit(EXIT_FAILURE);
    }
    errno = 0;
  }
  FILE *output = fopen(argv[1], "w");
  if (output == NULL) {
    fprintf(stderr, "Line %d\n", 33);
    perror("Bad open output file"
           "\n");
    if (0) {
      exit(EXIT_FAILURE);
    }
    errno = 0;
  }
  char *line = NULL;
  size_t size = 0;
  while (getline(&line, &size, p_file) != -1) {
    fprintf(output, "%s", line);
  }
  free(line);
  if (pclose(p_file) == -1) {
    fprintf(stderr, "Line %d\n", 43);
    perror("Error, when closing pipe"
           "\n");
    if (1) {
      exit(EXIT_FAILURE);
    }
    errno = 0;
  }
  if (fclose(output) == -1) {
    fprintf(stderr, "Line %d\n", 44);
    perror("Error, when closing output file"
           "\n");
    if (1) {
      exit(EXIT_FAILURE);
    }
    errno = 0;
  }
  return EXIT_SUCCESS;
}


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)
cmpl   $0x2,-0x8(%rbp)
je     11e6 <main+0x56>
mov    0x2e29(%rip),%rax
mov    (%rax),%edi
lea    0xe40(%rip),%rsi
mov    $0x1b,%edx
mov    $0x0,%al
call   1060 <fprintf@plt>
lea    0xe36(%rip),%rdi
call   1090 <perror@plt>
mov    0x2df1(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
lea    0xe37(%rip),%rdi
lea    0xe40(%rip),%rsi
call   1070 <popen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    123e <main+0xae>
mov    0x2dd1(%rip),%rax
mov    (%rax),%edi
lea    0xde8(%rip),%rsi
mov    $0x1e,%edx
mov    $0x0,%al
call   1060 <fprintf@plt>
lea    0xe0b(%rip),%rdi
call   1090 <perror@plt>
mov    0x2d99(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xdf4(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1297 <main+0x107>
mov    0x2d78(%rip),%rax
mov    (%rax),%edi
lea    0xd8f(%rip),%rsi
mov    $0x21,%edx
mov    $0x0,%al
call   1060 <fprintf@plt>
lea    0xdbf(%rip),%rdi
call   1090 <perror@plt>
mov    0x2d48(%rip),%rax
movq   $0x0,(%rax)
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x18(%rbp),%rdx
lea    -0x28(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   10a0 <getline@plt>
cmp    $0xffffffff,%eax
je     12de <main+0x14e>
mov    -0x20(%rbp),%rax
mov    %eax,%edi
mov    -0x28(%rbp),%rdx
lea    0xd87(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
jmp    12a7 <main+0x117>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x18(%rbp),%rdi
call   1050 <pclose@plt>
cmp    $0xffffffff,%eax
jne    132f <main+0x19f>
mov    0x2ce0(%rip),%rax
mov    (%rax),%edi
lea    0xcf7(%rip),%rsi
mov    $0x2b,%edx
mov    $0x0,%al
call   1060 <fprintf@plt>
lea    0xd40(%rip),%rdi
call   1090 <perror@plt>
mov    0x2ca8(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
cmp    $0xffffffff,%eax
jne    1377 <main+0x1e7>
mov    0x2c98(%rip),%rax
mov    (%rax),%edi
lea    0xcaf(%rip),%rsi
mov    $0x2c,%edx
mov    $0x0,%al
call   1060 <fprintf@plt>
lea    0xd12(%rip),%rdi
call   1090 <perror@plt>
mov    0x2c60(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    0x2c4a(%rip),%rax
mov    (%rax),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x10,%rsp
cmp    $0x2,%edi
jne    12c1 <main+0x131>
mov    %rsi,%rbx
lea    0xe74(%rip),%rdi
lea    0xe7d(%rip),%rsi
call   1070 <popen@plt>
test   %rax,%rax
je     12e6 <main+0x156>
mov    %rax,%r12
mov    0x8(%rbx),%rdi
lea    0xe6e(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,%r14
test   %rax,%rax
je     1286 <main+0xf6>
movq   $0x0,(%rsp)
movq   $0x0,0x8(%rsp)
mov    %rsp,%rdi
lea    0x8(%rsp),%rsi
mov    %r12,%rdx
call   10a0 <getline@plt>
cmp    $0xffffffff,%eax
je     1244 <main+0xb4>
lea    0xe48(%rip),%r15
mov    %rsp,%r13
lea    0x8(%rsp),%rbx
nopl   0x0(%rax)
mov    (%rsp),%rdx
mov    %r14d,%edi
mov    %r15,%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    %r13,%rdi
mov    %rbx,%rsi
mov    %r12,%rdx
call   10a0 <getline@plt>
cmp    $0xffffffff,%eax
jne    1220 <main+0x90>
mov    (%rsp),%rdi
call   1030 <free@plt>
mov    %r12,%rdi
call   1050 <pclose@plt>
cmp    $0xffffffff,%eax
je     130b <main+0x17b>
mov    %r14,%rdi
call   1040 <fclose@plt>
cmp    $0xffffffff,%eax
je     1330 <main+0x1a0>
mov    0x2d52(%rip),%rax
mov    (%rax),%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret
mov    0x2d53(%rip),%rax
mov    (%rax),%edi
lea    0xd6a(%rip),%rsi
mov    $0x21,%edx
xor    %eax,%eax
call   1060 <fprintf@plt>
lea    0xd9a(%rip),%rdi
call   1090 <perror@plt>
mov    0x2d23(%rip),%rax
movq   $0x0,(%rax)
jmp    11e4 <main+0x54>
mov    0x2d18(%rip),%rax
mov    (%rax),%edi
lea    0xd2f(%rip),%rsi
mov    $0x1b,%edx
xor    %eax,%eax
call   1060 <fprintf@plt>
lea    0xd25(%rip),%rdi
jmp    1353 <main+0x1c3>
mov    0x2cf3(%rip),%rax
mov    (%rax),%edi
lea    0xd0a(%rip),%rsi
mov    $0x1e,%edx
xor    %eax,%eax
call   1060 <fprintf@plt>
lea    0xd2d(%rip),%rdi
jmp    1353 <main+0x1c3>
mov    0x2cce(%rip),%rax
mov    (%rax),%edi
lea    0xce5(%rip),%rsi
mov    $0x2b,%edx
xor    %eax,%eax
call   1060 <fprintf@plt>
lea    0xd2e(%rip),%rdi
jmp    1353 <main+0x1c3>
mov    0x2ca9(%rip),%rax
mov    (%rax),%edi
lea    0xcc0(%rip),%rsi
mov    $0x2c,%edx
xor    %eax,%eax
call   1060 <fprintf@plt>
lea    0xd23(%rip),%rdi
call   1090 <perror@plt>
mov    0x2c71(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [9 x i8] c"Line %d\0A\00"
@.str.1 = [27 x i8] c"Wrong number of arguments\0A\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [16 x i8] c"ls -l | grep ^d\00"
@.str.3 = [2 x i8] c"r\00"
@.str.4 = [11 x i8] c"Bad popen\0A\00"
@.str.5 = [2 x i8] c"w\00"
@.str.6 = [22 x i8] c"Bad open output file\0A\00"
@errno = global i64 0
@.str.7 = [3 x i8] c"%s\00"
@.str.8 = [26 x i8] c"Error, when closing pipe\0A\00"
@.str.9 = [33 x i8] c"Error, when closing output file\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca i8*
%9 = alloca i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* %4
%11 = icmp ne i32 %10, 2
br i1 %11, label %12, label %18
12: ; preds = %2
%13 = load i32, i32* @stderr
%14 = call i32 (i32, i8*, ...) @fprintf(i32 %13, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 27)
%15 = call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0))
%16 = load i32, i32* @EXIT_FAILURE
%17 = call i32 @exit(i32 %16)
unreachable
18: ; preds = %2
%19 = call i32* @popen(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32* %19, i32** %6
%20 = load i32*, i32** %6
%21 = icmp eq i32* %20, null
br i1 %21, label %22, label %28
22: ; preds = %18
%23 = load i32, i32* @stderr
%24 = call i32 (i32, i8*, ...) @fprintf(i32 %23, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 30)
%25 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0))
%26 = load i32, i32* @EXIT_FAILURE
%27 = call i32 @exit(i32 %26)
unreachable
28: ; preds = %18
%29 = load i8**, i8*** %5
%30 = getelementptr inbounds i8*, i8** %29, i64 1
%31 = load i8*, i8** %30
%32 = call i32* @fopen(i8* %31, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
store i32* %32, i32** %7
%33 = load i32*, i32** %7
%34 = icmp eq i32* %33, null
br i1 %34, label %35, label %39
35: ; preds = %28
%36 = load i32, i32* @stderr
%37 = call i32 (i32, i8*, ...) @fprintf(i32 %36, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 33)
%38 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.6, i64 0, i64 0))
store i64 0, i64* @errno
br label %39
39: ; preds = %35, %28
store i8* null, i8** %8
store i64 0, i64* %9
br label %40
40: ; preds = %44, %39
%41 = load i32*, i32** %6
%42 = call i32 @getline(i8** %8, i64* %9, i32* %41)
%43 = icmp ne i32 %42, -1
br i1 %43, label %44, label %49
44: ; preds = %40
%45 = load i32*, i32** %7
%46 = ptrtoint i32* %45 to i32
%47 = load i8*, i8** %8
%48 = call i32 (i32, i8*, ...) @fprintf(i32 %46, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.7, i64 0, i64 0), i8* %47)
br label %40
49: ; preds = %40
%50 = load i8*, i8** %8
%51 = call i32 @free(i8* %50)
%52 = load i32*, i32** %6
%53 = call i32 @pclose(i32* %52)
%54 = icmp eq i32 %53, -1
br i1 %54, label %55, label %61
55: ; preds = %49
%56 = load i32, i32* @stderr
%57 = call i32 (i32, i8*, ...) @fprintf(i32 %56, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 43)
%58 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.8, i64 0, i64 0))
%59 = load i32, i32* @EXIT_FAILURE
%60 = call i32 @exit(i32 %59)
unreachable
61: ; preds = %49
%62 = load i32*, i32** %7
%63 = call i32 @fclose(i32* %62)
%64 = icmp eq i32 %63, -1
br i1 %64, label %65, label %71
65: ; preds = %61
%66 = load i32, i32* @stderr
%67 = call i32 (i32, i8*, ...) @fprintf(i32 %66, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 44)
%68 = call i32 @perror(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.9, i64 0, i64 0))
%69 = load i32, i32* @EXIT_FAILURE
%70 = call i32 @exit(i32 %69)
unreachable
71: ; preds = %61
%72 = load i32, i32* @EXIT_SUCCESS
ret i32 %72
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [9 x i8] c"Line %d\0A\00"
@.str.1 = [27 x i8] c"Wrong number of arguments\0A\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [16 x i8] c"ls -l | grep ^d\00"
@.str.3 = [2 x i8] c"r\00"
@.str.4 = [11 x i8] c"Bad popen\0A\00"
@.str.5 = [2 x i8] c"w\00"
@.str.6 = [22 x i8] c"Bad open output file\0A\00"
@errno = global i64 0
@.str.7 = [3 x i8] c"%s\00"
@.str.8 = [26 x i8] c"Error, when closing pipe\0A\00"
@.str.9 = [33 x i8] c"Error, when closing output file\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i8*
%4 = alloca i64
%5 = icmp eq i32 %0, 2
br i1 %5, label %12, label %6
6: ; preds = %2
%7 = load i32, i32* @stderr
%8 = tail call i32 (i32, i8*, ...) @fprintf(i32 %7, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 27)
%9 = tail call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0))
%10 = load i32, i32* @EXIT_FAILURE
%11 = tail call i32 @exit(i32 %10)
unreachable
12: ; preds = %2
%13 = tail call i32* @popen(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%14 = icmp eq i32* %13, null
br i1 %14, label %15, label %21
15: ; preds = %12
%16 = load i32, i32* @stderr
%17 = tail call i32 (i32, i8*, ...) @fprintf(i32 %16, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 30)
%18 = tail call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0))
%19 = load i32, i32* @EXIT_FAILURE
%20 = tail call i32 @exit(i32 %19)
unreachable
21: ; preds = %12
%22 = getelementptr inbounds i8*, i8** %1, i64 1
%23 = load i8*, i8** %22
%24 = tail call i32* @fopen(i8* %23, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
%25 = icmp eq i32* %24, null
br i1 %25, label %26, label %30
26: ; preds = %21
%27 = load i32, i32* @stderr
%28 = tail call i32 (i32, i8*, ...) @fprintf(i32 %27, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 33)
%29 = tail call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.6, i64 0, i64 0))
store i64 0, i64* @errno
br label %30
30: ; preds = %26, %21
%31 = bitcast i8** %3 to i8*
store i8* null, i8** %3
%32 = bitcast i64* %4 to i8*
store i64 0, i64* %4
%33 = call i32 @getline(i8** %3, i64* %4, i32* %13)
%34 = icmp eq i32 %33, -1
br i1 %34, label %43, label %35
35: ; preds = %30
%36 = ptrtoint i32* %24 to i64
%37 = trunc i64 %36 to i32
br label %38
38: ; preds = %35, %38
%39 = load i8*, i8** %3
%40 = call i32 (i32, i8*, ...) @fprintf(i32 %37, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.7, i64 0, i64 0), i8* %39)
%41 = call i32 @getline(i8** %3, i64* %4, i32* %13)
%42 = icmp eq i32 %41, -1
br i1 %42, label %43, label %38
43: ; preds = %38, %30
%44 = load i8*, i8** %3
%45 = call i32 @free(i8* %44)
%46 = call i32 @pclose(i32* %13)
%47 = icmp eq i32 %46, -1
br i1 %47, label %48, label %54
48: ; preds = %43
%49 = load i32, i32* @stderr
%50 = call i32 (i32, i8*, ...) @fprintf(i32 %49, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 43)
%51 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.8, i64 0, i64 0))
%52 = load i32, i32* @EXIT_FAILURE
%53 = call i32 @exit(i32 %52)
unreachable
54: ; preds = %43
%55 = call i32 @fclose(i32* %24)
%56 = icmp eq i32 %55, -1
br i1 %56, label %57, label %63
57: ; preds = %54
%58 = load i32, i32* @stderr
%59 = call i32 (i32, i8*, ...) @fprintf(i32 %58, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 44)
%60 = call i32 @perror(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.9, i64 0, i64 0))
%61 = load i32, i32* @EXIT_FAILURE
%62 = call i32 @exit(i32 %61)
unreachable
63: ; preds = %54
%64 = load i32, i32* @EXIT_SUCCESS
ret i32 %64
}