#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 EOF;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int fgetc(int /*<<< orphan*/ *);
int fputc(int, int /*<<< orphan*/ *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ pclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ *popen(char *, char *);
int /*<<< orphan*/ putchar(int);
int /*<<< orphan*/ sleep(int);
int main(int argc, char *argv[]) {
  FILE *fr;
  FILE *fw;
  int ch;
  while (1) {
    fr = popen("ls *", "r");
    if (fr == NULL) {
      perror("popen");
      exit(1);
    }
    int fd1 = open("/dev/zero", O_RDONLY);
    fw = popen("sort > /dev/null", "w");
    if (fw == NULL) {
      perror("popen");
      exit(1);
    }
    int fd2 = open("/dev/null", O_RDONLY);
    while ((ch = fgetc(fr)) != EOF) {
      if (fputc(ch, fw) == EOF) {
        perror("fputc");
        exit(1);
      }
      putchar(ch);
    }
    pclose(fr);
    close(fd1);
    pclose(fw);
    sleep(2);
    close(fd2);
  }
  return 0;
}


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)
lea    0xe43(%rip),%rdi
lea    0xe41(%rip),%rsi
call   1090 <popen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11ee <main+0x4e>
lea    0xe28(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    0x2df3(%rip),%rax
mov    (%rax),%esi
lea    0xe0f(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x28(%rbp)
lea    0xe0a(%rip),%rdi
lea    0xe14(%rip),%rsi
call   1090 <popen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    123e <main+0x9e>
lea    0xdd8(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    0x2da3(%rip),%rax
mov    (%rax),%esi
lea    0xdd0(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <fgetc@plt>
mov    %eax,-0x24(%rbp)
mov    0x2d77(%rip),%rcx
cmp    (%rcx),%eax
je     12af <main+0x10f>
mov    -0x24(%rbp),%edi
mov    -0x20(%rbp),%rsi
call   1070 <fputc@plt>
mov    0x2d5c(%rip),%rcx
cmp    (%rcx),%eax
jne    12a2 <main+0x102>
lea    0xd97(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x24(%rbp),%edi
call   1030 <putchar@plt>
jmp    1256 <main+0xb6>
mov    -0x18(%rbp),%rdi
call   1040 <pclose@plt>
mov    -0x28(%rbp),%edi
call   1060 <close@plt>
mov    -0x20(%rbp),%rdi
call   1040 <pclose@plt>
mov    $0x2,%edi
call   10c0 <sleep@plt>
mov    -0x2c(%rbp),%edi
call   1060 <close@plt>
jmp    11b6 <main+0x16>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
lea    0xe4e(%rip),%rdi
lea    0xe4c(%rip),%rsi
call   1090 <popen@plt>
test   %rax,%rax
jne    11cf <main+0x2f>
lea    0xe3d(%rip),%rdi
jmp    12c1 <main+0x121>
mov    %rax,%r14
mov    0x2e0f(%rip),%rbx
mov    0x2e00(%rip),%rbp
jmp    123c <main+0x9c>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %r14,%rdi
call   1040 <pclose@plt>
mov    %r15d,%edi
call   1060 <close@plt>
mov    %r12,%rdi
call   1040 <pclose@plt>
mov    $0x2,%edi
call   10c0 <sleep@plt>
mov    %r13d,%edi
call   1060 <close@plt>
lea    0xddf(%rip),%rdi
lea    0xddd(%rip),%rsi
call   1090 <popen@plt>
mov    %rax,%r14
test   %rax,%rax
mov    0x2dae(%rip),%rbx
je     11c3 <main+0x23>
mov    (%rbx),%esi
lea    0xdc8(%rip),%rdi
call   1080 <open@plt>
mov    %eax,%r15d
lea    0xdc3(%rip),%rdi
lea    0xdcd(%rip),%rsi
call   1090 <popen@plt>
test   %rax,%rax
je     11c3 <main+0x23>
mov    %rax,%r12
mov    (%rbx),%esi
lea    0xda9(%rip),%rdi
call   1080 <open@plt>
mov    %eax,%r13d
mov    %r14,%rdi
call   1050 <fgetc@plt>
cmp    0x0(%rbp),%eax
je     11f0 <main+0x50>
mov    %eax,%ebx
mov    %ebx,%edi
mov    %r12,%rsi
call   1070 <fputc@plt>
cmp    0x0(%rbp),%eax
je     12ba <main+0x11a>
mov    %ebx,%edi
call   1030 <putchar@plt>
mov    %r14,%rdi
call   1050 <fgetc@plt>
mov    %eax,%ebx
cmp    0x0(%rbp),%eax
jne    1290 <main+0xf0>
jmp    11f0 <main+0x50>
lea    0xd69(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@.str = [5 x i8] c"ls *\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [6 x i8] c"popen\00"
@.str.3 = [10 x i8] c"/dev/zero\00"
@O_RDONLY = global i32 0
@.str.4 = [17 x i8] c"sort > /dev/null\00"
@.str.5 = [2 x i8] c"w\00"
@.str.6 = [10 x i8] c"/dev/null\00"
@EOF = global i32 0
@.str.7 = [6 x i8] c"fputc\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 i32
%9 = alloca i32
%10 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
br label %11
11: ; preds = %2, %47
%12 = call i32* @popen(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %12, i32** %6
%13 = load i32*, i32** %6
%14 = icmp eq i32* %13, null
br i1 %14, label %15, label %18
15: ; preds = %11
%16 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%17 = call i32 @exit(i32 1)
unreachable
18: ; preds = %11
%19 = load i32, i32* @O_RDONLY
%20 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i32 %19)
store i32 %20, i32* %9
%21 = call i32* @popen(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
store i32* %21, i32** %7
%22 = load i32*, i32** %7
%23 = icmp eq i32* %22, null
br i1 %23, label %24, label %27
24: ; preds = %18
%25 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%26 = call i32 @exit(i32 1)
unreachable
27: ; preds = %18
%28 = load i32, i32* @O_RDONLY
%29 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.6, i64 0, i64 0), i32 %28)
store i32 %29, i32* %10
br label %30
30: ; preds = %44, %27
%31 = load i32*, i32** %6
%32 = call i32 @fgetc(i32* %31)
store i32 %32, i32* %8
%33 = load i32, i32* @EOF
%34 = icmp ne i32 %32, %33
br i1 %34, label %35, label %47
35: ; preds = %30
%36 = load i32, i32* %8
%37 = load i32*, i32** %7
%38 = call i32 @fputc(i32 %36, i32* %37)
%39 = load i32, i32* @EOF
%40 = icmp eq i32 %38, %39
br i1 %40, label %41, label %44
41: ; preds = %35
%42 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.7, i64 0, i64 0))
%43 = call i32 @exit(i32 1)
unreachable
44: ; preds = %35
%45 = load i32, i32* %8
%46 = call i32 @putchar(i32 %45)
br label %30
47: ; preds = %30
%48 = load i32*, i32** %6
%49 = call i32 @pclose(i32* %48)
%50 = load i32, i32* %9
%51 = call i32 @close(i32 %50)
%52 = load i32*, i32** %7
%53 = call i32 @pclose(i32* %52)
%54 = call i32 @sleep(i32 2)
%55 = load i32, i32* %10
%56 = call i32 @close(i32 %55)
br label %11
}

LLVM IR (-O2):
@.str = [5 x i8] c"ls *\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [6 x i8] c"popen\00"
@.str.3 = [10 x i8] c"/dev/zero\00"
@O_RDONLY = global i32 0
@.str.4 = [17 x i8] c"sort > /dev/null\00"
@.str.5 = [2 x i8] c"w\00"
@.str.6 = [10 x i8] c"/dev/null\00"
@EOF = global i32 0
@.str.7 = [6 x i8] c"fputc\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i32* @popen(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %5, label %8
5: ; preds = %36, %2
%6 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%7 = tail call i32 @exit(i32 1)
unreachable
8: ; preds = %2, %36
%9 = phi i32* [ %42, %36 ], [ %3, %2 ]
%10 = load i32, i32* @O_RDONLY
%11 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i32 %10)
%12 = tail call i32* @popen(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %17
14: ; preds = %8
%15 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%16 = tail call i32 @exit(i32 1)
unreachable
17: ; preds = %8
%18 = load i32, i32* @O_RDONLY
%19 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.6, i64 0, i64 0), i32 %18)
%20 = tail call i32 @fgetc(i32* %9)
%21 = load i32, i32* @EOF
%22 = icmp eq i32 %20, %21
br i1 %22, label %36, label %23
23: ; preds = %17, %31
%24 = phi i32 [ %33, %31 ], [ %20, %17 ]
%25 = tail call i32 @fputc(i32 %24, i32* %12)
%26 = load i32, i32* @EOF
%27 = icmp eq i32 %25, %26
br i1 %27, label %28, label %31
28: ; preds = %23
%29 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.7, i64 0, i64 0))
%30 = tail call i32 @exit(i32 1)
unreachable
31: ; preds = %23
%32 = tail call i32 @putchar(i32 %24)
%33 = tail call i32 @fgetc(i32* %9)
%34 = load i32, i32* @EOF
%35 = icmp eq i32 %33, %34
br i1 %35, label %36, label %23
36: ; preds = %31, %17
%37 = tail call i32 @pclose(i32* %9)
%38 = tail call i32 @close(i32 %11)
%39 = tail call i32 @pclose(i32* %12)
%40 = tail call i32 @sleep(i32 2)
%41 = tail call i32 @close(i32 %19)
%42 = tail call i32* @popen(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%43 = icmp eq i32* %42, null
br i1 %43, label %5, label %8
}