#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 */
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/);
scalar_t__ pclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ *popen(char *, char *);
int /*<<< orphan*/ stdout;
int main(int argc, char *argv[]) {
  char buf[256];
  FILE *p = popen("ps f", "r");
  if (p == NULL) {
    perror("open pipe 'ps f'");
    return 1;
  }
  while (fgets(buf, sizeof(buf), p) != NULL) {
    fputs(buf, stdout);
  }
  if (pclose(p) != 0) {
    perror("pclose()");
    return 1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x120,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe90(%rip),%rdi
lea    0xe8e(%rip),%rsi
call   1060 <popen@plt>
mov    %rax,-0x118(%rbp)
cmpq   $0x0,-0x118(%rbp)
jne    11a9 <main+0x59>
lea    0xe6f(%rip),%rdi
call   1070 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    121f <main+0xcf>
jmp    11ae <main+0x5e>
lea    -0x110(%rbp),%rdi
mov    -0x118(%rbp),%rdx
mov    $0x100,%esi
call   1050 <fgets@plt>
cmp    $0x0,%rax
je     11ea <main+0x9a>
lea    -0x110(%rbp),%rdi
mov    0x2e02(%rip),%rax
mov    (%rax),%esi
call   1040 <fputs@plt>
jmp    11ae <main+0x5e>
mov    -0x118(%rbp),%rdi
call   1030 <pclose@plt>
cmp    $0x0,%rax
je     1218 <main+0xc8>
lea    0xe11(%rip),%rdi
call   1070 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    121f <main+0xcf>
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    0xe9b(%rip),%rsi
call   1060 <popen@plt>
test   %rax,%rax
je     11df <main+0x8f>
mov    %rax,%r14
mov    %rsp,%rdi
mov    $0x100,%esi
mov    %rax,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
je     11c0 <main+0x70>
mov    0x2e4d(%rip),%r15
mov    %rsp,%rbx
cs nopw 0x0(%rax,%rax,1)
mov    (%r15),%esi
mov    %rbx,%rdi
call   1040 <fputs@plt>
mov    %rbx,%rdi
mov    $0x100,%esi
mov    %r14,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
jne    11a0 <main+0x50>
mov    %r14,%rdi
call   1030 <pclose@plt>
mov    %rax,%rcx
xor    %eax,%eax
test   %rcx,%rcx
jne    11e8 <main+0x98>
add    $0x100,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xe21(%rip),%rdi
jmp    11ef <main+0x9f>
lea    0xe29(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%eax
jmp    11d2 <main+0x82>

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

LLVM IR (-O2):
@.str = [5 x i8] c"ps f\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [17 x i8] c"open pipe 'ps f'\00"
@stdout = global i32 0
@.str.3 = [9 x i8] c"pclose()\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [256 x i8]
%4 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%5 = 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))
%6 = icmp eq i32* %5, null
br i1 %6, label %10, label %7
7: ; preds = %2
%8 = call i32* @fgets(i8* %4, i32 256, i32* %5)
%9 = icmp eq i32* %8, null
br i1 %9, label %17, label %12
10: ; preds = %2
%11 = tail call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
br label %22
12: ; preds = %7, %12
%13 = load i32, i32* @stdout
%14 = call i32 @fputs(i8* %4, i32 %13)
%15 = call i32* @fgets(i8* %4, i32 256, i32* %5)
%16 = icmp eq i32* %15, null
br i1 %16, label %17, label %12
17: ; preds = %12, %7
%18 = call i64 @pclose(i32* %5)
%19 = icmp eq i64 %18, 0
br i1 %19, label %22, label %20
20: ; preds = %17
%21 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
br label %22
22: ; preds = %17, %20, %10
%23 = phi i32 [ 1, %10 ], [ 1, %20 ], [ 0, %17 ]
ret i32 %23
}