#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 EXIT_SUCCESS;
int errno;
scalar_t__ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int, char *);
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/);
scalar_t__ pclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *popen(char *, char *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
char *strerror(int);
int main(int argc, char *argv[]) {
  char *cmd = "ps ajf";
  char buf[512];
  FILE *fpipe;
  if (!(fpipe = popen(cmd, "r"))) {
    fprintf(stderr, "Err.: %d popen() - %s\n", errno, strerror(errno));
    return 13;
  }
  while (fgets(buf, 512, fpipe) != 0)
    fputs(buf, stdout);
  if (pclose(fpipe)) {
    fprintf(stderr, "Err.: %d - pclose() %s\n", errno, strerror(errno));
    return (EXIT_SUCCESS);
  }
  return (EXIT_SUCCESS);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x240,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe80(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xe78(%rip),%rsi
call   1070 <popen@plt>
mov    %rax,-0x228(%rbp)
cmp    $0x0,%rax
jne    11f8 <main+0x98>
mov    0x2e34(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x230(%rbp)
mov    0x2e1d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x22c(%rbp)
mov    0x2e0e(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x230(%rbp),%edi
mov    -0x22c(%rbp),%edx
mov    %rax,%rcx
lea    0xe22(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0xd,-0x4(%rbp)
jmp    12b3 <main+0x153>
jmp    11fd <main+0x9d>
lea    -0x220(%rbp),%rdi
mov    -0x228(%rbp),%rdx
mov    $0x200,%esi
call   1050 <fgets@plt>
cmp    $0x0,%rax
je     1239 <main+0xd9>
lea    -0x220(%rbp),%rdi
mov    0x2da3(%rip),%rax
mov    (%rax),%esi
call   1040 <fputs@plt>
jmp    11fd <main+0x9d>
mov    -0x228(%rbp),%rdi
call   1030 <pclose@plt>
cmp    $0x0,%rax
je     12a7 <main+0x147>
mov    0x2d8a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x238(%rbp)
mov    0x2d73(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x234(%rbp)
mov    0x2d64(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x238(%rbp),%edi
mov    -0x234(%rbp),%edx
mov    %rax,%rcx
lea    0xd8f(%rip),%rsi
call   1060 <fprintf@plt>
mov    0x2d2b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    12b3 <main+0x153>
mov    0x2d1a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x240,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x200,%rsp
lea    0xe8e(%rip),%rdi
lea    0xe8e(%rip),%rsi
call   1070 <popen@plt>
test   %rax,%rax
je     1214 <main+0xb4>
mov    %rax,%r14
mov    %rsp,%rdi
mov    $0x200,%esi
mov    %rax,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
je     11d0 <main+0x70>
mov    0x2e2a(%rip),%rbp
mov    %rsp,%rbx
nopl   0x0(%rax)
mov    0x0(%rbp),%esi
mov    %rbx,%rdi
call   1040 <fputs@plt>
mov    %rbx,%rdi
mov    $0x200,%esi
mov    %r14,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
jne    11b0 <main+0x50>
mov    %r14,%rdi
call   1030 <pclose@plt>
test   %rax,%rax
je     1209 <main+0xa9>
mov    0x2dfc(%rip),%rax
mov    (%rax),%ebx
mov    0x2deb(%rip),%rax
mov    (%rax),%ebp
mov    %ebp,%edi
call   1080 <strerror@plt>
lea    0xe23(%rip),%rsi
mov    %ebx,%edi
mov    %ebp,%edx
mov    %rax,%rcx
call   1060 <fprintf@plt>
mov    0x2db8(%rip),%rax
mov    (%rax),%eax
jmp    1245 <main+0xe5>
mov    0x2dc5(%rip),%rax
mov    (%rax),%ebx
mov    0x2db4(%rip),%rax
mov    (%rax),%ebp
mov    %ebp,%edi
call   1080 <strerror@plt>
lea    0xdd5(%rip),%rsi
mov    %ebx,%edi
mov    %ebp,%edx
mov    %rax,%rcx
call   1060 <fprintf@plt>
mov    $0xd,%eax
add    $0x200,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [7 x i8] c"ps ajf\00"
@.str.1 = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.2 = [23 x i8] c"Err.: %d popen() - %s\0A\00"
@errno = global i32 0
@stdout = global i32 0
@.str.3 = [24 x i8] c"Err.: %d - pclose() %s\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca [512 x i8]
%8 = alloca i32*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8** %6
%9 = load i8*, i8** %6
%10 = call i32* @popen(i8* %9, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %10, i32** %8
%11 = icmp ne i32* %10, null
br i1 %11, label %18, label %12
12: ; preds = %2
%13 = load i32, i32* @stderr
%14 = load i32, i32* @errno
%15 = load i32, i32* @errno
%16 = call i8* @strerror(i32 %15)
%17 = call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i32 %14, i8* %16)
store i32 13, i32* %3
br label %41
18: ; preds = %2
br label %19
19: ; preds = %24, %18
%20 = getelementptr inbounds [512 x i8], [512 x i8]* %7, i64 0, i64 0
%21 = load i32*, i32** %8
%22 = call i64 @fgets(i8* %20, i32 512, i32* %21)
%23 = icmp ne i64 %22, 0
br i1 %23, label %24, label %28
24: ; preds = %19
%25 = getelementptr inbounds [512 x i8], [512 x i8]* %7, i64 0, i64 0
%26 = load i32, i32* @stdout
%27 = call i32 @fputs(i8* %25, i32 %26)
br label %19
28: ; preds = %19
%29 = load i32*, i32** %8
%30 = call i64 @pclose(i32* %29)
%31 = icmp ne i64 %30, 0
br i1 %31, label %32, label %39
32: ; preds = %28
%33 = load i32, i32* @stderr
%34 = load i32, i32* @errno
%35 = load i32, i32* @errno
%36 = call i8* @strerror(i32 %35)
%37 = call i32 @fprintf(i32 %33, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), i32 %34, i8* %36)
%38 = load i32, i32* @EXIT_SUCCESS
store i32 %38, i32* %3
br label %41
39: ; preds = %28
%40 = load i32, i32* @EXIT_SUCCESS
store i32 %40, i32* %3
br label %41
41: ; preds = %39, %32, %12
%42 = load i32, i32* %3
ret i32 %42
}

LLVM IR (-O2):
@.str = [7 x i8] c"ps ajf\00"
@.str.1 = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.2 = [23 x i8] c"Err.: %d popen() - %s\0A\00"
@errno = global i32 0
@stdout = global i32 0
@.str.3 = [24 x i8] c"Err.: %d - pclose() %s\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [512 x i8]
%4 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 0
%5 = tail call i32* @popen(i8* getelementptr inbounds ([7 x i8], [7 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 i64 @fgets(i8* %4, i32 512, i32* %5)
%9 = icmp eq i64 %8, 0
br i1 %9, label %20, label %15
10: ; preds = %2
%11 = load i32, i32* @stderr
%12 = load i32, i32* @errno
%13 = tail call i8* @strerror(i32 %12)
%14 = tail call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i32 %12, i8* %13)
br label %31
15: ; preds = %7, %15
%16 = load i32, i32* @stdout
%17 = call i32 @fputs(i8* %4, i32 %16)
%18 = call i64 @fgets(i8* %4, i32 512, i32* %5)
%19 = icmp eq i64 %18, 0
br i1 %19, label %20, label %15
20: ; preds = %15, %7
%21 = call i64 @pclose(i32* %5)
%22 = icmp eq i64 %21, 0
br i1 %22, label %29, label %23
23: ; preds = %20
%24 = load i32, i32* @stderr
%25 = load i32, i32* @errno
%26 = call i8* @strerror(i32 %25)
%27 = call i32 @fprintf(i32 %24, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), i32 %25, i8* %26)
%28 = load i32, i32* @EXIT_SUCCESS
br label %31
29: ; preds = %20
%30 = load i32, i32* @EXIT_SUCCESS
br label %31
31: ; preds = %29, %23, %10
%32 = phi i32 [ %28, %23 ], [ %30, %29 ], [ 13, %10 ]
ret i32 %32
}