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

/* Variables and functions */
int EXIT_SUCCESS;
int WEXITSTATUS(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int pclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *popen(char *, char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ snprintf(char *, int, char *, ...);
int /*<<< orphan*/ stderr;
int strlen(char *);
int /*<<< orphan*/ strncat(char *, char *, int);
int main(int argc, char *argv[]) {
  int rc = 0;
  FILE *fp;
  char line_buf[1024];
  char cmd_buf[1024];
  char output_buf[102400];
  output_buf[0] = '\0';
  if (argc > 1) {
    snprintf(cmd_buf, 1024, "%s", argv[1]);
  } else {
    snprintf(cmd_buf, 1024, "cd ~/ && ls -la");
  }
  fp = popen(cmd_buf, "r");
  if (NULL == fp) {
    fprintf(stderr, "Can NOT popen().\n");
    exit(110);
  }
  while (strlen(output_buf) < sizeof(output_buf) - sizeof(line_buf) &&
         NULL != fgets(line_buf, sizeof(line_buf) - 1, fp)) {
    strncat(output_buf, line_buf, strlen(line_buf));
  }
  rc = pclose(fp);
  int status_child = WEXITSTATUS(rc);
  if (EXIT_SUCCESS == status_child) {
    printf("子进程执行成功! <-> ");
  } else {
    printf("子进程执行失败! <-> ");
  }
  printf("子进程的执行结果: \n\n%s\n---end--\n", output_buf);
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x19840,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movb   $0x0,-0x19820(%rbp)
cmpl   $0x1,-0x8(%rbp)
jle    11f8 <main+0x58>
lea    -0x820(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rcx
mov    $0x400,%esi
lea    0xe14(%rip),%rdx
mov    $0x0,%al
call   1060 <snprintf@plt>
jmp    1212 <main+0x72>
lea    -0x820(%rbp),%rdi
mov    $0x400,%esi
lea    0xdf8(%rip),%rdx
mov    $0x0,%al
call   1060 <snprintf@plt>
lea    -0x820(%rbp),%rdi
lea    0xdf3(%rip),%rsi
call   10b0 <popen@plt>
mov    %rax,-0x20(%rbp)
xor    %eax,%eax
cmp    -0x20(%rbp),%rax
jne    1254 <main+0xb4>
mov    0x2da4(%rip),%rax
mov    (%rax),%edi
lea    0xdd0(%rip),%rsi
call   1090 <fprintf@plt>
mov    $0x6e,%edi
call   10c0 <exit@plt>
jmp    1259 <main+0xb9>
lea    -0x19820(%rbp),%rdi
call   1030 <strlen@plt>
movslq %eax,%rcx
xor    %eax,%eax
cmp    $0x18c00,%rcx
mov    %al,-0x19825(%rbp)
jae    12a3 <main+0x103>
lea    -0x420(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    $0x3ff,%esi
call   1080 <fgets@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
setne  %al
mov    %al,-0x19825(%rbp)
mov    -0x19825(%rbp),%al
test   $0x1,%al
jne    12b6 <main+0x116>
jmp    12f8 <main+0x158>
lea    -0x19820(%rbp),%rax
mov    %rax,-0x19838(%rbp)
lea    -0x420(%rbp),%rax
mov    %rax,-0x19830(%rbp)
lea    -0x420(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x19838(%rbp),%rdi
mov    -0x19830(%rbp),%rsi
mov    %eax,%edx
call   1070 <strncat@plt>
jmp    1259 <main+0xb9>
mov    -0x20(%rbp),%rdi
call   1050 <pclose@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edi
call   10a0 <WEXITSTATUS@plt>
mov    %eax,-0x19824(%rbp)
mov    0x2cbf(%rip),%rax
mov    (%rax),%eax
cmp    -0x19824(%rbp),%eax
jne    133a <main+0x19a>
lea    0xcf9(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1348 <main+0x1a8>
lea    0xd02(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x19820(%rbp),%rsi
lea    0xd09(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2c74(%rip),%rax
mov    (%rax),%eax
add    $0x19840,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x19800,%rsp
movb   $0x0,0x800(%rsp)
cmp    $0x2,%edi
jl     11d5 <main+0x35>
mov    0x8(%rsi),%rcx
lea    0xe3c(%rip),%rdx
mov    %rsp,%rdi
mov    $0x400,%esi
xor    %eax,%eax
call   1060 <snprintf@plt>
jmp    11eb <main+0x4b>
lea    0xe27(%rip),%rdx
mov    %rsp,%rdi
mov    $0x400,%esi
xor    %eax,%eax
call   1060 <snprintf@plt>
lea    0xe21(%rip),%rsi
mov    %rsp,%rdi
call   10b0 <popen@plt>
test   %rax,%rax
je     12bf <main+0x11f>
mov    %rax,%r14
lea    0x800(%rsp),%rdi
call   1030 <strlen@plt>
cmp    $0x18bff,%eax
ja     1269 <main+0xc9>
lea    0x400(%rsp),%rbx
lea    0x800(%rsp),%r15
nopw   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    $0x3ff,%esi
mov    %r14,%rdx
call   1080 <fgets@plt>
test   %rax,%rax
je     1269 <main+0xc9>
mov    %rbx,%rdi
call   1030 <strlen@plt>
mov    %r15,%rdi
mov    %rbx,%rsi
mov    %eax,%edx
call   1070 <strncat@plt>
mov    %r15,%rdi
call   1030 <strlen@plt>
cmp    $0x18c00,%eax
jb     1230 <main+0x90>
mov    %r14,%rdi
call   1050 <pclose@plt>
mov    %eax,%edi
call   10a0 <WEXITSTATUS@plt>
mov    0x2d59(%rip),%rbx
cmp    %eax,(%rbx)
lea    0xd9f(%rip),%rax
lea    0xdb4(%rip),%rdi
cmove  %rax,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xdbe(%rip),%rdi
lea    0x800(%rsp),%rsi
xor    %eax,%eax
call   1040 <printf@plt>
mov    (%rbx),%eax
add    $0x19800,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
mov    0x2d1a(%rip),%rax
mov    (%rax),%edi
lea    0xd46(%rip),%rsi
call   1090 <fprintf@plt>
mov    $0x6e,%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
@.str = [3 x i8] c"%s\00"
@.str.1 = [16 x i8] c"cd ~/ && ls -la\00"
@.str.2 = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.3 = [18 x i8] c"Can NOT popen().\0A\00"
@EXIT_SUCCESS = global i32 0
@.str.4 = [28 x i8] c"\E5\AD\90\E8\BF\9B\E7\A8\8B\E6\89\A7\E8\A1\8C\E6\88\90\E5\8A\9F! <-> \00"
@.str.5 = [28 x i8] c"\E5\AD\90\E8\BF\9B\E7\A8\8B\E6\89\A7\E8\A1\8C\E5\A4\B1\E8\B4\A5! <-> \00"
@.str.6 = [41 x i8] c"\E5\AD\90\E8\BF\9B\E7\A8\8B\E7\9A\84\E6\89\A7\E8\A1\8C\E7\BB\93\E6\9E\9C: \0A\0A%s\0A---end--\0A\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 [1024 x i8]
%9 = alloca [1024 x i8]
%10 = alloca [102400 x i8]
%11 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %6
%12 = getelementptr inbounds [102400 x i8], [102400 x i8]* %10, i64 0, i64 0
store i8 0, i8* %12
%13 = load i32, i32* %4
%14 = icmp sgt i32 %13, 1
br i1 %14, label %15, label %21
15: ; preds = %2
%16 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%17 = load i8**, i8*** %5
%18 = getelementptr inbounds i8*, i8** %17, i64 1
%19 = load i8*, i8** %18
%20 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %16, i32 1024, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %19)
br label %24
21: ; preds = %2
%22 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%23 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %22, i32 1024, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
br label %24
24: ; preds = %21, %15
%25 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%26 = call i32* @popen(i8* %25, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %26, i32** %7
%27 = load i32*, i32** %7
%28 = icmp eq i32* null, %27
br i1 %28, label %29, label %33
29: ; preds = %24
%30 = load i32, i32* @stderr
%31 = call i32 @fprintf(i32 %30, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0))
%32 = call i32 @exit(i32 110)
unreachable
33: ; preds = %24
br label %34
34: ; preds = %46, %33
%35 = getelementptr inbounds [102400 x i8], [102400 x i8]* %10, i64 0, i64 0
%36 = call i32 @strlen(i8* %35)
%37 = sext i32 %36 to i64
%38 = icmp ult i64 %37, 101376
br i1 %38, label %39, label %44
39: ; preds = %34
%40 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%41 = load i32*, i32** %7
%42 = call i32* @fgets(i8* %40, i32 1023, i32* %41)
%43 = icmp ne i32* null, %42
br label %44
44: ; preds = %39, %34
%45 = phi i1 [ false, %34 ], [ %43, %39 ]
br i1 %45, label %46, label %52
46: ; preds = %44
%47 = getelementptr inbounds [102400 x i8], [102400 x i8]* %10, i64 0, i64 0
%48 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%49 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%50 = call i32 @strlen(i8* %49)
%51 = call i32 @strncat(i8* %47, i8* %48, i32 %50)
br label %34
52: ; preds = %44
%53 = load i32*, i32** %7
%54 = call i32 @pclose(i32* %53)
store i32 %54, i32* %6
%55 = load i32, i32* %6
%56 = call i32 @WEXITSTATUS(i32 %55)
store i32 %56, i32* %11
%57 = load i32, i32* @EXIT_SUCCESS
%58 = load i32, i32* %11
%59 = icmp eq i32 %57, %58
br i1 %59, label %60, label %62
60: ; preds = %52
%61 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.4, i64 0, i64 0))
br label %64
62: ; preds = %52
%63 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.5, i64 0, i64 0))
br label %64
64: ; preds = %62, %60
%65 = getelementptr inbounds [102400 x i8], [102400 x i8]* %10, i64 0, i64 0
%66 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.6, i64 0, i64 0), i8* %65)
%67 = load i32, i32* @EXIT_SUCCESS
ret i32 %67
}

LLVM IR (-O2):
@.str = [3 x i8] c"%s\00"
@.str.1 = [16 x i8] c"cd ~/ && ls -la\00"
@.str.2 = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.3 = [18 x i8] c"Can NOT popen().\0A\00"
@EXIT_SUCCESS = global i32 0
@.str.4 = [28 x i8] c"\E5\AD\90\E8\BF\9B\E7\A8\8B\E6\89\A7\E8\A1\8C\E6\88\90\E5\8A\9F! <-> \00"
@.str.5 = [28 x i8] c"\E5\AD\90\E8\BF\9B\E7\A8\8B\E6\89\A7\E8\A1\8C\E5\A4\B1\E8\B4\A5! <-> \00"
@.str.6 = [41 x i8] c"\E5\AD\90\E8\BF\9B\E7\A8\8B\E7\9A\84\E6\89\A7\E8\A1\8C\E7\BB\93\E6\9E\9C: \0A\0A%s\0A---end--\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [1024 x i8]
%4 = alloca [1024 x i8]
%5 = alloca [102400 x i8]
%6 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%7 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%8 = getelementptr inbounds [102400 x i8], [102400 x i8]* %5, i64 0, i64 0
store i8 0, i8* %8
%9 = icmp sgt i32 %0, 1
br i1 %9, label %10, label %14
10: ; preds = %2
%11 = getelementptr inbounds i8*, i8** %1, i64 1
%12 = load i8*, i8** %11
%13 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %7, i32 1024, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %12)
br label %16
14: ; preds = %2
%15 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %7, i32 1024, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
br label %16
16: ; preds = %14, %10
%17 = call i32* @popen(i8* %7, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%18 = icmp eq i32* %17, null
br i1 %18, label %22, label %19
19: ; preds = %16
%20 = call i32 @strlen(i8* %8)
%21 = icmp ult i32 %20, 101376
br i1 %21, label %26, label %34
22: ; preds = %16
%23 = load i32, i32* @stderr
%24 = call i32 @fprintf(i32 %23, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0))
%25 = call i32 @exit(i32 110)
unreachable
26: ; preds = %19, %29
%27 = call i32* @fgets(i8* %6, i32 1023, i32* %17)
%28 = icmp eq i32* %27, null
br i1 %28, label %34, label %29
29: ; preds = %26
%30 = call i32 @strlen(i8* %6)
%31 = call i32 @strncat(i8* %8, i8* %6, i32 %30)
%32 = call i32 @strlen(i8* %8)
%33 = icmp ult i32 %32, 101376
br i1 %33, label %26, label %34
34: ; preds = %26, %29, %19
%35 = call i32 @pclose(i32* %17)
%36 = call i32 @WEXITSTATUS(i32 %35)
%37 = load i32, i32* @EXIT_SUCCESS
%38 = icmp eq i32 %37, %36
%39 = select i1 %38, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.5, i64 0, i64 0)
%40 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) %39)
%41 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([41 x i8], [41 x i8]* @.str.6, i64 0, i64 0), i8* %8)
%42 = load i32, i32* @EXIT_SUCCESS
ret i32 %42
}