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

/* Variables and functions */
int EXIT_FAILURE;
scalar_t__ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
scalar_t__ pclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *popen(char *, char *);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ stderr;
scalar_t__ strlen(char *);
int main() {
  FILE *ps_pipe;
  FILE *grep_pipe;
  int bytes_read;
  char buffer[100];
  ps_pipe = popen("/bin/ls -a", "r");
  grep_pipe = popen("/bin/grep 'pipe*'", "w");
  if ((!ps_pipe) || (!grep_pipe)) {
    fprintf(stderr, "One or both pipes failed.\n");
    return EXIT_FAILURE;
  }
  bytes_read = 0;
  while (fgets(buffer, sizeof(buffer), ps_pipe)) {
    fprintf(grep_pipe, "%s", buffer);
    bytes_read += strlen(buffer);
  }
  printf("Total bytes read = %d\n", bytes_read);
  if (pclose(ps_pipe) != 0) {
    fprintf(stderr, "Could not run 'ls', or other error.\n");
  }
  if (pclose(grep_pipe) != 0) {
    fprintf(stderr, "Could not run 'grep', or other error.\n");
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe87(%rip),%rdi
lea    0xe8b(%rip),%rsi
call   1080 <popen@plt>
mov    %rax,-0x10(%rbp)
lea    0xe7d(%rip),%rdi
lea    0xe88(%rip),%rsi
call   1080 <popen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11b6 <main+0x56>
cmpq   $0x0,-0x18(%rbp)
jne    11de <main+0x7e>
mov    0x2e23(%rip),%rax
mov    (%rax),%edi
lea    0xe5b(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    0x2e04(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    129e <main+0x13e>
movl   $0x0,-0x1c(%rbp)
lea    -0x80(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    $0x64,%esi
call   1060 <fgets@plt>
cmp    $0x0,%rax
je     1234 <main+0xd4>
mov    -0x18(%rbp),%rax
mov    %eax,%edi
lea    -0x80(%rbp),%rdx
lea    0xe2a(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
lea    -0x80(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,%rcx
movslq -0x1c(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x1c(%rbp)
jmp    11e5 <main+0x85>
mov    -0x1c(%rbp),%esi
lea    0xe01(%rip),%rdi
call   1040 <printf@plt>
mov    -0x10(%rbp),%rdi
call   1050 <pclose@plt>
cmp    $0x0,%rax
je     126d <main+0x10d>
mov    0x2d83(%rip),%rax
mov    (%rax),%edi
lea    0xdf0(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    -0x18(%rbp),%rdi
call   1050 <pclose@plt>
cmp    $0x0,%rax
je     1297 <main+0x137>
mov    0x2d59(%rip),%rax
mov    (%rax),%edi
lea    0xdeb(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x70,%rsp
lea    0xe8d(%rip),%rdi
lea    0xe91(%rip),%rsi
call   1080 <popen@plt>
mov    %rax,%r12
lea    0xe84(%rip),%rdi
lea    0xe8f(%rip),%rsi
call   1080 <popen@plt>
test   %r12,%r12
je     1201 <main+0xa1>
mov    %rax,%r14
test   %rax,%rax
je     1201 <main+0xa1>
mov    %rsp,%rdi
mov    $0x64,%esi
mov    %r12,%rdx
call   1060 <fgets@plt>
test   %rax,%rax
je     1223 <main+0xc3>
lea    0xe7e(%rip),%r15
mov    %rsp,%rbx
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %r14d,%edi
mov    %r15,%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    %rbx,%rdi
call   1030 <strlen@plt>
add    %eax,%ebp
mov    %rbx,%rdi
mov    $0x64,%esi
mov    %r12,%rdx
call   1060 <fgets@plt>
test   %rax,%rax
jne    11d0 <main+0x70>
jmp    1225 <main+0xc5>
mov    0x2dd8(%rip),%rax
mov    (%rax),%edi
lea    0xe10(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    0x2db9(%rip),%rax
mov    (%rax),%ebx
jmp    1281 <main+0x121>
xor    %ebp,%ebp
lea    0xe13(%rip),%rdi
mov    %ebp,%esi
call   1040 <printf@plt>
mov    %r12,%rdi
call   1050 <pclose@plt>
test   %rax,%rax
je     1257 <main+0xf7>
mov    0x2d99(%rip),%rax
mov    (%rax),%edi
lea    0xe06(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    %r14,%rdi
call   1050 <pclose@plt>
test   %rax,%rax
je     127f <main+0x11f>
mov    0x2d75(%rip),%rax
mov    (%rax),%edi
lea    0xe07(%rip),%rsi
xor    %ebx,%ebx
xor    %eax,%eax
call   1070 <fprintf@plt>
jmp    1281 <main+0x121>
xor    %ebx,%ebx
mov    %ebx,%eax
add    $0x70,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [11 x i8] c"/bin/ls -a\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [18 x i8] c"/bin/grep 'pipe*'\00"
@.str.3 = [2 x i8] c"w\00"
@stderr = global i32 0
@.str.4 = [27 x i8] c"One or both pipes failed.\0A\00"
@EXIT_FAILURE = global i32 0
@.str.5 = [3 x i8] c"%s\00"
@.str.6 = [23 x i8] c"Total bytes read = %d\0A\00"
@.str.7 = [37 x i8] c"Could not run 'ls', or other error.\0A\00"
@.str.8 = [39 x i8] c"Could not run 'grep', or other error.\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca i32*
%4 = alloca i32
%5 = alloca [100 x i8]
store i32 0, i32* %1
%6 = call i32* @popen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %6, i32** %2
%7 = call i32* @popen(i8* getelementptr inbounds ([18 x i8], [18 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* %7, i32** %3
%8 = load i32*, i32** %2
%9 = icmp ne i32* %8, null
br i1 %9, label %10, label %13
10: ; preds = %0
%11 = load i32*, i32** %3
%12 = icmp ne i32* %11, null
br i1 %12, label %17, label %13
13: ; preds = %10, %0
%14 = load i32, i32* @stderr
%15 = call i32 (i32, i8*, ...) @fprintf(i32 %14, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.4, i64 0, i64 0))
%16 = load i32, i32* @EXIT_FAILURE
store i32 %16, i32* %1
br label %51
17: ; preds = %10
store i32 0, i32* %4
br label %18
18: ; preds = %23, %17
%19 = getelementptr inbounds [100 x i8], [100 x i8]* %5, i64 0, i64 0
%20 = load i32*, i32** %2
%21 = call i64 @fgets(i8* %19, i32 100, i32* %20)
%22 = icmp ne i64 %21, 0
br i1 %22, label %23, label %34
23: ; preds = %18
%24 = load i32*, i32** %3
%25 = ptrtoint i32* %24 to i32
%26 = getelementptr inbounds [100 x i8], [100 x i8]* %5, i64 0, i64 0
%27 = call i32 (i32, i8*, ...) @fprintf(i32 %25, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i8* %26)
%28 = getelementptr inbounds [100 x i8], [100 x i8]* %5, i64 0, i64 0
%29 = call i64 @strlen(i8* %28)
%30 = load i32, i32* %4
%31 = sext i32 %30 to i64
%32 = add nsw i64 %31, %29
%33 = trunc i64 %32 to i32
store i32 %33, i32* %4
br label %18
34: ; preds = %18
%35 = load i32, i32* %4
%36 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.6, i64 0, i64 0), i32 %35)
%37 = load i32*, i32** %2
%38 = call i64 @pclose(i32* %37)
%39 = icmp ne i64 %38, 0
br i1 %39, label %40, label %43
40: ; preds = %34
%41 = load i32, i32* @stderr
%42 = call i32 (i32, i8*, ...) @fprintf(i32 %41, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.7, i64 0, i64 0))
br label %43
43: ; preds = %40, %34
%44 = load i32*, i32** %3
%45 = call i64 @pclose(i32* %44)
%46 = icmp ne i64 %45, 0
br i1 %46, label %47, label %50
47: ; preds = %43
%48 = load i32, i32* @stderr
%49 = call i32 (i32, i8*, ...) @fprintf(i32 %48, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.8, i64 0, i64 0))
br label %50
50: ; preds = %47, %43
store i32 0, i32* %1
br label %51
51: ; preds = %50, %13
%52 = load i32, i32* %1
ret i32 %52
}

LLVM IR (-O2):
@.str = [11 x i8] c"/bin/ls -a\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [18 x i8] c"/bin/grep 'pipe*'\00"
@.str.3 = [2 x i8] c"w\00"
@stderr = global i32 0
@.str.4 = [27 x i8] c"One or both pipes failed.\0A\00"
@EXIT_FAILURE = global i32 0
@.str.5 = [3 x i8] c"%s\00"
@.str.6 = [23 x i8] c"Total bytes read = %d\0A\00"
@.str.7 = [37 x i8] c"Could not run 'ls', or other error.\0A\00"
@.str.8 = [39 x i8] c"Could not run 'grep', or other error.\0A\00"
define i32 @main() {
%1 = alloca [100 x i8]
%2 = getelementptr inbounds [100 x i8], [100 x i8]* %1, i64 0, i64 0
%3 = tail call i32* @popen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%4 = tail call i32* @popen(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%5 = icmp ne i32* %3, null
%6 = icmp ne i32* %4, null
%7 = and i1 %5, %6
br i1 %7, label %8, label %14
8: ; preds = %0
%9 = call i64 @fgets(i8* %2, i32 100, i32* %3)
%10 = icmp eq i64 %9, 0
br i1 %10, label %26, label %11
11: ; preds = %8
%12 = ptrtoint i32* %4 to i64
%13 = trunc i64 %12 to i32
br label %18
14: ; preds = %0
%15 = load i32, i32* @stderr
%16 = tail call i32 (i32, i8*, ...) @fprintf(i32 %15, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.4, i64 0, i64 0))
%17 = load i32, i32* @EXIT_FAILURE
br label %40
18: ; preds = %11, %18
%19 = phi i32 [ 0, %11 ], [ %23, %18 ]
%20 = call i32 (i32, i8*, ...) @fprintf(i32 %13, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i8* %2)
%21 = call i64 @strlen(i8* %2)
%22 = trunc i64 %21 to i32
%23 = add i32 %19, %22
%24 = call i64 @fgets(i8* %2, i32 100, i32* %3)
%25 = icmp eq i64 %24, 0
br i1 %25, label %26, label %18
26: ; preds = %18, %8
%27 = phi i32 [ 0, %8 ], [ %23, %18 ]
%28 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.6, i64 0, i64 0), i32 %27)
%29 = call i64 @pclose(i32* %3)
%30 = icmp eq i64 %29, 0
br i1 %30, label %34, label %31
31: ; preds = %26
%32 = load i32, i32* @stderr
%33 = call i32 (i32, i8*, ...) @fprintf(i32 %32, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.7, i64 0, i64 0))
br label %34
34: ; preds = %31, %26
%35 = call i64 @pclose(i32* %4)
%36 = icmp eq i64 %35, 0
br i1 %36, label %40, label %37
37: ; preds = %34
%38 = load i32, i32* @stderr
%39 = call i32 (i32, i8*, ...) @fprintf(i32 %38, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.8, i64 0, i64 0))
br label %40
40: ; preds = %34, %37, %14
%41 = phi i32 [ %17, %14 ], [ 0, %37 ], [ 0, %34 ]
ret i32 %41
}