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

/* Variables and functions */
int WEXITSTATUS(int);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int pclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ *popen(char *, char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ snprintf(char *, int, char *);
int main() {
  FILE *fp = NULL;
  char buffer[1024] = {0};
  char result_buffer[2048] = {0};
  snprintf(buffer, 1024, "./test.sh &");
  fp = popen(buffer, "r");
  if (fp == NULL) {
    perror("popen error");
    return -1;
  }
  printf("fgets start\n");
  while (fgets(result_buffer, sizeof(result_buffer), fp) != NULL) {
    printf("result is %s\n", result_buffer);
  }
  printf("fgets end\n");
  int rc = pclose(fp);
  if (rc == -1) {
    perror("pclose fp error");
    return -1;
  } else {
    printf("进程结束状态【%d】命令返回值【%d】\r\n", rc, WEXITSTATUS(rc));
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xc20,%rsp
movl   $0x0,-0x4(%rbp)
movq   $0x0,-0x10(%rbp)
lea    -0x410(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1060 <memset@plt>
lea    -0xc10(%rbp),%rdi
xor    %esi,%esi
mov    $0x800,%edx
call   1060 <memset@plt>
lea    -0x410(%rbp),%rdi
mov    $0x400,%esi
lea    0xe2d(%rip),%rdx
call   1050 <snprintf@plt>
lea    -0x410(%rbp),%rdi
lea    0xe30(%rip),%rsi
call   1090 <popen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1212 <main+0x92>
lea    0xe0b(%rip),%rdi
call   10a0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12cf <main+0x14f>
lea    0xdff(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    -0xc10(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    $0x800,%esi
call   1070 <fgets@plt>
cmp    $0x0,%rax
je     1259 <main+0xd9>
lea    -0xc10(%rbp),%rsi
lea    0xdd8(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1220 <main+0xa0>
lea    0xdd3(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x10(%rbp),%rdi
call   1040 <pclose@plt>
mov    %eax,-0xc14(%rbp)
cmpl   $0xffffffff,-0xc14(%rbp)
jne    129b <main+0x11b>
lea    0xdb4(%rip),%rdi
call   10a0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12cf <main+0x14f>
mov    -0xc14(%rbp),%eax
mov    %eax,-0xc18(%rbp)
mov    -0xc14(%rbp),%edi
call   1080 <WEXITSTATUS@plt>
mov    -0xc18(%rbp),%esi
mov    %eax,%edx
lea    0xd8d(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xc20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0xc08,%rsp
lea    0x800(%rsp),%rbx
mov    $0x400,%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1070 <memset@plt>
mov    %rsp,%rdi
mov    $0x800,%edx
xor    %esi,%esi
call   1070 <memset@plt>
lea    0xe36(%rip),%rdx
mov    %rbx,%rdi
mov    $0x400,%esi
call   1060 <snprintf@plt>
lea    0xe38(%rip),%rsi
mov    %rbx,%rdi
call   10a0 <popen@plt>
test   %rax,%rax
je     1288 <main+0xf8>
mov    %rax,%r15
lea    0xe71(%rip),%rdi
call   1030 <puts@plt>
mov    %rsp,%rdi
mov    $0x800,%esi
mov    %r15,%rdx
call   1080 <fgets@plt>
test   %rax,%rax
je     1242 <main+0xb2>
lea    0xdfe(%rip),%r14
mov    %rsp,%rbx
nopl   (%rax)
mov    %r14,%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %rbx,%rdi
mov    $0x800,%esi
mov    %r15,%rdx
call   1080 <fgets@plt>
test   %rax,%rax
jne    1220 <main+0x90>
lea    0xe2d(%rip),%rdi
call   1030 <puts@plt>
mov    %r15,%rdi
call   1050 <pclose@plt>
cmp    $0xffffffff,%eax
je     1291 <main+0x101>
mov    %eax,%ebp
mov    %eax,%edi
call   1090 <WEXITSTATUS@plt>
lea    0xdcb(%rip),%rdi
xor    %ebx,%ebx
mov    %ebp,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1040 <printf@plt>
mov    %ebx,%eax
add    $0xc08,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd7d(%rip),%rdi
jmp    1298 <main+0x108>
lea    0xd8e(%rip),%rdi
call   10b0 <perror@plt>
mov    $0xffffffff,%ebx
jmp    1278 <main+0xe8>

LLVM IR (-O0):
@.str = [12 x i8] c"./test.sh &\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [12 x i8] c"popen error\00"
@.str.3 = [13 x i8] c"fgets start\0A\00"
@.str.4 = [14 x i8] c"result is %s\0A\00"
@.str.5 = [11 x i8] c"fgets end\0A\00"
@.str.6 = [16 x i8] c"pclose fp error\00"
@.str.7 = [52 x i8] c"\E8\BF\9B\E7\A8\8B\E7\BB\93\E6\9D\9F\E7\8A\B6\E6\80\81\E3\80\90%d\E3\80\91\E5\91\BD\E4\BB\A4\E8\BF\94\E5\9B\9E\E5\80\BC\E3\80\90%d\E3\80\91\0D\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca [1024 x i8]
%4 = alloca [2048 x i8]
%5 = alloca i32
store i32 0, i32* %1
store i32* null, i32** %2
%6 = bitcast [1024 x i8]* %3 to i8*
%7 = bitcast [2048 x i8]* %4 to i8*
%8 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%9 = call i32 @snprintf(i8* %8, i32 1024, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%10 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%11 = call i32* @popen(i8* %10, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %11, i32** %2
%12 = load i32*, i32** %2
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %16
14: ; preds = %0
%15 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %1
br label %40
16: ; preds = %0
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
br label %18
18: ; preds = %23, %16
%19 = getelementptr inbounds [2048 x i8], [2048 x i8]* %4, i64 0, i64 0
%20 = load i32*, i32** %2
%21 = call i32* @fgets(i8* %19, i32 2048, i32* %20)
%22 = icmp ne i32* %21, null
br i1 %22, label %23, label %26
23: ; preds = %18
%24 = getelementptr inbounds [2048 x i8], [2048 x i8]* %4, i64 0, i64 0
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0), i8* %24)
br label %18
26: ; preds = %18
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.5, i64 0, i64 0))
%28 = load i32*, i32** %2
%29 = call i32 @pclose(i32* %28)
store i32 %29, i32* %5
%30 = load i32, i32* %5
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %34
32: ; preds = %26
%33 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.6, i64 0, i64 0))
store i32 -1, i32* %1
br label %40
34: ; preds = %26
%35 = load i32, i32* %5
%36 = load i32, i32* %5
%37 = call i32 @WEXITSTATUS(i32 %36)
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.7, i64 0, i64 0), i32 %35, i32 %37)
br label %39
39: ; preds = %34
store i32 0, i32* %1
br label %40
40: ; preds = %39, %32, %14
%41 = load i32, i32* %1
ret i32 %41
}

LLVM IR (-O2):
@.str = [12 x i8] c"./test.sh &\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [12 x i8] c"popen error\00"
@.str.4 = [14 x i8] c"result is %s\0A\00"
@.str.6 = [16 x i8] c"pclose fp error\00"
@.str.7 = [52 x i8] c"\E8\BF\9B\E7\A8\8B\E7\BB\93\E6\9D\9F\E7\8A\B6\E6\80\81\E3\80\90%d\E3\80\91\E5\91\BD\E4\BB\A4\E8\BF\94\E5\9B\9E\E5\80\BC\E3\80\90%d\E3\80\91\0D\0A\00"
@str = [12 x i8] c"fgets start\00"
@str.8 = [10 x i8] c"fgets end\00"
define i32 @main() {
%1 = alloca [1024 x i8]
%2 = alloca [2048 x i8]
%3 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 0
%4 = getelementptr inbounds [2048 x i8], [2048 x i8]* %2, i64 0, i64 0
%5 = call i32 @snprintf(i8* %3, i32 1024, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%6 = call i32* @popen(i8* %3, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%7 = icmp eq i32* %6, null
br i1 %7, label %8, label %10
8: ; preds = %0
%9 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
br label %27
10: ; preds = %0
%11 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str, i64 0, i64 0))
%12 = call i32* @fgets(i8* %4, i32 2048, i32* %6)
%13 = icmp eq i32* %12, null
br i1 %13, label %18, label %14
14: ; preds = %10, %14
%15 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0), i8* %4)
%16 = call i32* @fgets(i8* %4, i32 2048, i32* %6)
%17 = icmp eq i32* %16, null
br i1 %17, label %18, label %14
18: ; preds = %14, %10
%19 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @str.8, i64 0, i64 0))
%20 = call i32 @pclose(i32* %6)
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %24
22: ; preds = %18
%23 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.6, i64 0, i64 0))
br label %27
24: ; preds = %18
%25 = call i32 @WEXITSTATUS(i32 %20)
%26 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([52 x i8], [52 x i8]* @.str.7, i64 0, i64 0), i32 %20, i32 %25)
br label %27
27: ; preds = %22, %24, %8
%28 = phi i32 [ -1, %8 ], [ -1, %22 ], [ 0, %24 ]
ret i32 %28
}