#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 */
scalar_t__ fgets(char *, int const, int /*<<< orphan*/ *);
int /*<<< orphan*/ pclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *popen(char const *, char *);
int /*<<< orphan*/ printf(char *);
int get_shell_stdout(const char *shell_cmd, char *shell_stdout, const int len) {
  FILE *pp = NULL;
  if (shell_cmd == NULL)
    return -1;
  if ((pp = popen(shell_cmd, "r")) == NULL) {
    printf("###ERR: Open shell Cmd Err.\r\n");
    return -2;
  }
  while (fgets(shell_stdout, len, pp))
    ;
  pclose(pp);
  return 0;
}


x86 assembly (clang, -O0):
<get_shell_stdout>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
movq   $0x0,-0x28(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1172 <get_shell_stdout+0x32>
movl   $0xffffffff,-0x4(%rbp)
jmp    11dc <get_shell_stdout+0x9c>
mov    -0x10(%rbp),%rdi
lea    0xe83(%rip),%rsi
call   1060 <popen@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    11a8 <get_shell_stdout+0x68>
lea    0xe6b(%rip),%rdi
call   1030 <printf@plt>
movl   $0xfffffffe,-0x4(%rbp)
jmp    11dc <get_shell_stdout+0x9c>
jmp    11ad <get_shell_stdout+0x6d>
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%esi
mov    -0x28(%rbp),%rdx
call   1050 <fgets@plt>
cmp    $0x0,%rax
je     11cc <get_shell_stdout+0x8c>
jmp    11ad <get_shell_stdout+0x6d>
mov    -0x28(%rbp),%rdi
call   1040 <pclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_shell_stdout>:
push   %rbp
push   %r14
push   %rbx
test   %rdi,%rdi
je     118f <get_shell_stdout+0x4f>
mov    %edx,%r14d
mov    %rsi,%rbx
lea    0xeaa(%rip),%rsi
call   1060 <popen@plt>
test   %rax,%rax
je     1196 <get_shell_stdout+0x56>
mov    %rax,%rbp
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %rbx,%rdi
mov    %r14d,%esi
mov    %rbp,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
jne    1170 <get_shell_stdout+0x30>
mov    %rbp,%rdi
call   1040 <pclose@plt>
xor    %eax,%eax
jmp    11a7 <get_shell_stdout+0x67>
mov    $0xffffffff,%eax
jmp    11a7 <get_shell_stdout+0x67>
lea    0xe65(%rip),%rdi
call   1030 <puts@plt>
mov    $0xfffffffe,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@.str.1 = [30 x i8] c"###ERR: Open shell Cmd Err.\0D\0A\00"
define i32 @get_shell_stdout(i8* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32*
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
store i32* null, i32** %8
%9 = load i8*, i8** %5
%10 = icmp eq i8* %9, null
br i1 %10, label %11, label %12
11: ; preds = %3
store i32 -1, i32* %4
br label %29
12: ; preds = %3
%13 = load i8*, i8** %5
%14 = call i32* @popen(i8* %13, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %14, i32** %8
%15 = icmp eq i32* %14, null
br i1 %15, label %16, label %18
16: ; preds = %12
%17 = call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0))
store i32 -2, i32* %4
br label %29
18: ; preds = %12
br label %19
19: ; preds = %25, %18
%20 = load i8*, i8** %6
%21 = load i32, i32* %7
%22 = load i32*, i32** %8
%23 = call i64 @fgets(i8* %20, i32 %21, i32* %22)
%24 = icmp ne i64 %23, 0
br i1 %24, label %25, label %26
25: ; preds = %19
br label %19
26: ; preds = %19
%27 = load i32*, i32** %8
%28 = call i32 @pclose(i32* %27)
store i32 0, i32* %4
br label %29
29: ; preds = %26, %16, %11
%30 = load i32, i32* %4
ret i32 %30
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@str = [29 x i8] c"###ERR: Open shell Cmd Err.\0D\00"
define i32 @get_shell_stdout(i8* %0, i8* %1, i32 %2) {
%4 = icmp eq i8* %0, null
br i1 %4, label %15, label %5
5: ; preds = %3
%6 = tail call i32* @popen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%7 = icmp eq i32* %6, null
br i1 %7, label %8, label %10
8: ; preds = %5
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @str, i64 0, i64 0))
br label %15
10: ; preds = %5, %10
%11 = tail call i64 @fgets(i8* %1, i32 %2, i32* %6)
%12 = icmp eq i64 %11, 0
br i1 %12, label %13, label %10
13: ; preds = %10
%14 = tail call i32 @pclose(i32* %6)
br label %15
15: ; preds = %3, %13, %8
%16 = phi i32 [ -2, %8 ], [ 0, %13 ], [ -1, %3 ]
ret i32 %16
}