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

/* Variables and functions */
scalar_t__ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ pclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *popen(char *, char *);
int /*<<< orphan*/ stderr;
int main() {
  FILE *out, *in;
  char buf[255];
  if ((out = popen("grep init", "w")) == NULL) {
    fprintf(stderr, "error!\n");
    return 0;
  }
  if ((in = popen("ps -ef", "r")) == NULL) {
    fprintf(stderr, "error!\n");
    return 0;
  }
  while (fgets(buf, sizeof(buf), in))
    fputs(buf, out);
  pclose(out);
  pclose(in);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x120,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe97(%rip),%rdi
lea    0xe9a(%rip),%rsi
call   1070 <popen@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jne    11a4 <main+0x54>
mov    0x2e56(%rip),%rax
mov    (%rax),%edi
lea    0xe79(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1238 <main+0xe8>
lea    0xe69(%rip),%rdi
lea    0xe69(%rip),%rsi
call   1070 <popen@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    11e6 <main+0x96>
mov    0x2e14(%rip),%rax
mov    (%rax),%edi
lea    0xe37(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1238 <main+0xe8>
jmp    11eb <main+0x9b>
lea    -0x120(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    $0xff,%esi
call   1050 <fgets@plt>
cmp    $0x0,%rax
je     121f <main+0xcf>
lea    -0x120(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1040 <fputs@plt>
jmp    11eb <main+0x9b>
mov    -0x10(%rbp),%rdi
call   1030 <pclose@plt>
mov    -0x18(%rbp),%rdi
call   1030 <pclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x120,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x100,%rsp
lea    0xe9d(%rip),%rdi
lea    0xea0(%rip),%rsi
call   1070 <popen@plt>
test   %rax,%rax
je     11e2 <main+0x92>
mov    %rax,%r14
lea    0xe96(%rip),%rdi
lea    0xe96(%rip),%rsi
call   1070 <popen@plt>
test   %rax,%rax
je     11e2 <main+0x92>
mov    %rax,%r15
mov    %rsp,%rdi
mov    $0xff,%esi
mov    %rax,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
je     11d0 <main+0x80>
mov    %rsp,%rbx
nopw   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    %r14,%rsi
call   1040 <fputs@plt>
mov    %rbx,%rdi
mov    $0xff,%esi
mov    %r15,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
jne    11b0 <main+0x60>
mov    %r14,%rdi
call   1030 <pclose@plt>
mov    %r15,%rdi
call   1030 <pclose@plt>
jmp    11f7 <main+0xa7>
mov    0x2df7(%rip),%rax
mov    (%rax),%edi
lea    0xe1a(%rip),%rsi
call   1060 <fprintf@plt>
xor    %eax,%eax
add    $0x100,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [10 x i8] c"grep init\00"
@.str.1 = [2 x i8] c"w\00"
@stderr = global i32 0
@.str.2 = [8 x i8] c"error!\0A\00"
@.str.3 = [7 x i8] c"ps -ef\00"
@.str.4 = [2 x i8] c"r\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca i32*
%4 = alloca [255 x i8]
store i32 0, i32* %1
%5 = call i32* @popen(i8* getelementptr inbounds ([10 x i8], [10 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 = icmp eq i32* %5, null
br i1 %6, label %7, label %10
7: ; preds = %0
%8 = load i32, i32* @stderr
%9 = call i32 @fprintf(i32 %8, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
store i32 0, i32* %1
br label %31
10: ; preds = %0
%11 = call i32* @popen(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
store i32* %11, i32** %3
%12 = icmp eq i32* %11, null
br i1 %12, label %13, label %16
13: ; preds = %10
%14 = load i32, i32* @stderr
%15 = call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
store i32 0, i32* %1
br label %31
16: ; preds = %10
br label %17
17: ; preds = %22, %16
%18 = getelementptr inbounds [255 x i8], [255 x i8]* %4, i64 0, i64 0
%19 = load i32*, i32** %3
%20 = call i64 @fgets(i8* %18, i32 255, i32* %19)
%21 = icmp ne i64 %20, 0
br i1 %21, label %22, label %26
22: ; preds = %17
%23 = getelementptr inbounds [255 x i8], [255 x i8]* %4, i64 0, i64 0
%24 = load i32*, i32** %2
%25 = call i32 @fputs(i8* %23, i32* %24)
br label %17
26: ; preds = %17
%27 = load i32*, i32** %2
%28 = call i32 @pclose(i32* %27)
%29 = load i32*, i32** %3
%30 = call i32 @pclose(i32* %29)
store i32 0, i32* %1
br label %31
31: ; preds = %26, %13, %7
%32 = load i32, i32* %1
ret i32 %32
}

LLVM IR (-O2):
@.str = [10 x i8] c"grep init\00"
@.str.1 = [2 x i8] c"w\00"
@stderr = global i32 0
@.str.2 = [8 x i8] c"error!\0A\00"
@.str.3 = [7 x i8] c"ps -ef\00"
@.str.4 = [2 x i8] c"r\00"
define i32 @main() {
%1 = alloca [255 x i8]
%2 = getelementptr inbounds [255 x i8], [255 x i8]* %1, i64 0, i64 0
%3 = tail call i32* @popen(i8* getelementptr inbounds ([10 x i8], [10 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 = %0
%6 = load i32, i32* @stderr
%7 = tail call i32 @fprintf(i32 %6, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
br label %24
8: ; preds = %0
%9 = tail call i32* @popen(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%10 = icmp eq i32* %9, null
br i1 %10, label %14, label %11
11: ; preds = %8
%12 = call i64 @fgets(i8* %2, i32 255, i32* %9)
%13 = icmp eq i64 %12, 0
br i1 %13, label %21, label %17
14: ; preds = %8
%15 = load i32, i32* @stderr
%16 = tail call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
br label %24
17: ; preds = %11, %17
%18 = call i32 @fputs(i8* %2, i32* %3)
%19 = call i64 @fgets(i8* %2, i32 255, i32* %9)
%20 = icmp eq i64 %19, 0
br i1 %20, label %21, label %17
21: ; preds = %17, %11
%22 = call i32 @pclose(i32* %3)
%23 = call i32 @pclose(i32* %9)
br label %24
24: ; preds = %21, %14, %5
ret i32 0
}