#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 */
scalar_t__ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ pclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *popen(char *, char *);
int main(void) {
  FILE *pipein_fp, *pipeout_fp;
  char readbuf[80];
  pipein_fp = popen("ls", "r");
  pipeout_fp = popen("sort", "w");
  while (fgets(readbuf, 80, pipein_fp))
    fputs(readbuf, pipeout_fp);
  pclose(pipein_fp);
  pclose(pipeout_fp);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xeaa(%rip),%rdi
lea    0xea6(%rip),%rsi
call   1060 <popen@plt>
mov    %rax,-0x10(%rbp)
lea    0xe98(%rip),%rdi
lea    0xe96(%rip),%rsi
call   1060 <popen@plt>
mov    %rax,-0x18(%rbp)
lea    -0x70(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    $0x50,%esi
call   1050 <fgets@plt>
cmp    $0x0,%rax
je     11ab <main+0x6b>
lea    -0x70(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1040 <fputs@plt>
jmp    117d <main+0x3d>
mov    -0x10(%rbp),%rdi
call   1030 <pclose@plt>
mov    -0x18(%rbp),%rdi
call   1030 <pclose@plt>
xor    %eax,%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x50,%rsp
lea    0xeb0(%rip),%rdi
lea    0xeac(%rip),%rsi
call   1060 <popen@plt>
mov    %rax,%r14
lea    0xe9f(%rip),%rdi
lea    0xe9d(%rip),%rsi
call   1060 <popen@plt>
mov    %rax,%r15
mov    %rsp,%rdi
mov    $0x50,%esi
mov    %r14,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
je     11b0 <main+0x70>
mov    %rsp,%rbx
nopl   (%rax)
mov    %rbx,%rdi
mov    %r15,%rsi
call   1040 <fputs@plt>
mov    %rbx,%rdi
mov    $0x50,%esi
mov    %r14,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
jne    1190 <main+0x50>
mov    %r14,%rdi
call   1030 <pclose@plt>
mov    %r15,%rdi
call   1030 <pclose@plt>
xor    %eax,%eax
add    $0x50,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [3 x i8] c"ls\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [5 x i8] c"sort\00"
@.str.3 = [2 x i8] c"w\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca i32*
%4 = alloca [80 x i8]
store i32 0, i32* %1
%5 = call i32* @popen(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %5, i32** %2
%6 = call i32* @popen(i8* getelementptr inbounds ([5 x i8], [5 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* %6, i32** %3
br label %7
7: ; preds = %12, %0
%8 = getelementptr inbounds [80 x i8], [80 x i8]* %4, i64 0, i64 0
%9 = load i32*, i32** %2
%10 = call i64 @fgets(i8* %8, i32 80, i32* %9)
%11 = icmp ne i64 %10, 0
br i1 %11, label %12, label %16
12: ; preds = %7
%13 = getelementptr inbounds [80 x i8], [80 x i8]* %4, i64 0, i64 0
%14 = load i32*, i32** %3
%15 = call i32 @fputs(i8* %13, i32* %14)
br label %7
16: ; preds = %7
%17 = load i32*, i32** %2
%18 = call i32 @pclose(i32* %17)
%19 = load i32*, i32** %3
%20 = call i32 @pclose(i32* %19)
ret i32 0
}

LLVM IR (-O2):
@.str = [3 x i8] c"ls\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [5 x i8] c"sort\00"
@.str.3 = [2 x i8] c"w\00"
define i32 @main() {
%1 = alloca [80 x i8]
%2 = getelementptr inbounds [80 x i8], [80 x i8]* %1, i64 0, i64 0
%3 = tail call i32* @popen(i8* getelementptr inbounds ([3 x i8], [3 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 ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%5 = call i64 @fgets(i8* %2, i32 80, i32* %3)
%6 = icmp eq i64 %5, 0
br i1 %6, label %11, label %7
7: ; preds = %0, %7
%8 = call i32 @fputs(i8* %2, i32* %4)
%9 = call i64 @fgets(i8* %2, i32 80, i32* %3)
%10 = icmp eq i64 %9, 0
br i1 %10, label %11, label %7
11: ; preds = %7, %0
%12 = call i32 @pclose(i32* %3)
%13 = call i32 @pclose(i32* %4)
ret i32 0
}