#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 /*<<< orphan*/ pclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *popen(char const *, char *);
char *qfile_read(int /*<<< orphan*/ *, int /*<<< orphan*/ *);
char *strdup(char *);
char *qsyscmd(const char *cmd) {
  FILE *fp = popen(cmd, "r");
  if (fp == NULL)
    return NULL;
  char *str = qfile_read(fp, NULL);
  pclose(fp);
  if (str == NULL)
    str = strdup("");
  return str;
}


x86 assembly (clang, -O0):
<qsyscmd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xea9(%rip),%rsi
call   1050 <popen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1178 <qsyscmd+0x38>
movq   $0x0,-0x8(%rbp)
jmp    11b5 <qsyscmd+0x75>
mov    -0x18(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1040 <qfile_read@plt>
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <pclose@plt>
cmpq   $0x0,-0x20(%rbp)
jne    11ad <qsyscmd+0x6d>
lea    0xe5d(%rip),%rdi
call   1060 <strdup@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<qsyscmd>:
push   %r14
push   %rbx
push   %rax
lea    0xeb5(%rip),%rsi
call   1050 <popen@plt>
test   %rax,%rax
je     1185 <qsyscmd+0x45>
mov    %rax,%rbx
mov    %rax,%rdi
xor    %esi,%esi
call   1040 <qfile_read@plt>
mov    %rax,%r14
mov    %rbx,%rdi
call   1030 <pclose@plt>
test   %r14,%r14
jne    1188 <qsyscmd+0x48>
lea    0xe88(%rip),%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1060 <strdup@plt>
xor    %r14d,%r14d
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@.str.1 = [1 x i8] zeroinitializer
define i8* @qsyscmd(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i8*
store i8* %0, i8** %3
%6 = load i8*, i8** %3
%7 = call i32* @popen(i8* %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %7, i32** %4
%8 = load i32*, i32** %4
%9 = icmp eq i32* %8, null
br i1 %9, label %10, label %11
10: ; preds = %1
store i8* null, i8** %2
br label %22
11: ; preds = %1
%12 = load i32*, i32** %4
%13 = call i8* @qfile_read(i32* %12, i32* null)
store i8* %13, i8** %5
%14 = load i32*, i32** %4
%15 = call i32 @pclose(i32* %14)
%16 = load i8*, i8** %5
%17 = icmp eq i8* %16, null
br i1 %17, label %18, label %20
18: ; preds = %11
%19 = call i8* @strdup(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i64 0, i64 0))
store i8* %19, i8** %5
br label %20
20: ; preds = %18, %11
%21 = load i8*, i8** %5
store i8* %21, i8** %2
br label %22
22: ; preds = %20, %10
%23 = load i8*, i8** %2
ret i8* %23
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@.str.1 = [1 x i8] zeroinitializer
define i8* @qsyscmd(i8* nocapture %0) {
%2 = tail call i32* @popen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%3 = icmp eq i32* %2, null
br i1 %3, label %10, label %4
4: ; preds = %1
%5 = tail call i8* @qfile_read(i32* %2, i32* null)
%6 = tail call i32 @pclose(i32* %2)
%7 = icmp eq i8* %5, null
br i1 %7, label %8, label %10
8: ; preds = %4
%9 = tail call dereferenceable_or_null(1) i8* @strdup(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i64 0, i64 0))
br label %10
10: ; preds = %4, %8, %1
%11 = phi i8* [ null, %1 ], [ %9, %8 ], [ %5, %4 ]
ret i8* %11
}