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

/* Variables and functions */
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ pclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *popen(char const *, char *);
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
int processCmd(const char *cmd, char *cmd_output) {
  FILE *fp;
  char line[1024];
  cmd_output[0] = '\0';
  fp = popen(cmd, "r");
  if (fp == NULL) {
    strcpy(cmd_output, "Failed to run command\n");
    return -1;
  }
  while (fgets(line, sizeof(line), fp) != NULL) {
    if ((strlen(cmd_output) + strlen(line)) < 32768 - 1)
      strcat(cmd_output, line);
    else
      return -1;
  }
  pclose(fp);
  return 1;
}


x86 assembly (clang, -O0):
<processCmd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
lea    0xe7b(%rip),%rsi
call   1070 <popen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11b5 <processCmd+0x55>
mov    -0x18(%rbp),%rdi
lea    0xe5e(%rip),%rsi
call   1030 <strcpy@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    123f <processCmd+0xdf>
jmp    11ba <processCmd+0x5a>
lea    -0x420(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    $0x400,%esi
call   1060 <fgets@plt>
cmp    $0x0,%rax
je     122f <processCmd+0xcf>
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x424(%rbp)
lea    -0x420(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x424(%rbp),%eax
add    %ecx,%eax
cmp    $0x7fff,%eax
jge    121e <processCmd+0xbe>
mov    -0x18(%rbp),%rdi
lea    -0x420(%rbp),%rsi
call   1080 <strcat@plt>
jmp    122a <processCmd+0xca>
movl   $0xffffffff,-0x4(%rbp)
jmp    123f <processCmd+0xdf>
jmp    11ba <processCmd+0x5a>
mov    -0x20(%rbp),%rdi
call   1050 <pclose@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<processCmd>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x408,%rsp
mov    %rsi,%rbx
movb   $0x0,(%rsi)
lea    0xe86(%rip),%rsi
call   1070 <popen@plt>
test   %rax,%rax
je     11ea <processCmd+0x8a>
mov    %rax,%r14
mov    %rsp,%rdi
mov    $0x400,%esi
mov    %rax,%rdx
call   1060 <fgets@plt>
test   %rax,%rax
je     11db <processCmd+0x7b>
mov    %rsp,%r15
nop
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %eax,%ebp
mov    %r15,%rdi
call   1040 <strlen@plt>
add    %ebp,%eax
cmp    $0x7ffe,%eax
jg     11f9 <processCmd+0x99>
mov    %rbx,%rdi
mov    %r15,%rsi
call   1080 <strcat@plt>
mov    %r15,%rdi
mov    $0x400,%esi
mov    %r14,%rdx
call   1060 <fgets@plt>
test   %rax,%rax
jne    11a0 <processCmd+0x40>
mov    %r14,%rdi
call   1050 <pclose@plt>
mov    $0x1,%eax
jmp    11fe <processCmd+0x9e>
lea    0xe11(%rip),%rsi
mov    %rbx,%rdi
call   1030 <strcpy@plt>
mov    $0xffffffff,%eax
add    $0x408,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@.str.1 = [23 x i8] c"Failed to run command\0A\00"
define i32 @processCmd(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca [1024 x i8]
store i8* %0, i8** %4
store i8* %1, i8** %5
%8 = load i8*, i8** %5
%9 = getelementptr inbounds i8, i8* %8, i64 0
store i8 0, i8* %9
%10 = load i8*, i8** %4
%11 = call i32* @popen(i8* %10, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %11, i32** %6
%12 = load i32*, i32** %6
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %17
14: ; preds = %2
%15 = load i8*, i8** %5
%16 = call i32 @strcpy(i8* %15, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %39
17: ; preds = %2
br label %18
18: ; preds = %35, %17
%19 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%20 = load i32*, i32** %6
%21 = call i32* @fgets(i8* %19, i32 1024, i32* %20)
%22 = icmp ne i32* %21, null
br i1 %22, label %23, label %36
23: ; preds = %18
%24 = load i8*, i8** %5
%25 = call i32 @strlen(i8* %24)
%26 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%27 = call i32 @strlen(i8* %26)
%28 = add nsw i32 %25, %27
%29 = icmp slt i32 %28, 32767
br i1 %29, label %30, label %34
30: ; preds = %23
%31 = load i8*, i8** %5
%32 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%33 = call i32 @strcat(i8* %31, i8* %32)
br label %35
34: ; preds = %23
store i32 -1, i32* %3
br label %39
35: ; preds = %30
br label %18
36: ; preds = %18
%37 = load i32*, i32** %6
%38 = call i32 @pclose(i32* %37)
store i32 1, i32* %3
br label %39
39: ; preds = %36, %34, %14
%40 = load i32, i32* %3
ret i32 %40
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@.str.1 = [23 x i8] c"Failed to run command\0A\00"
define i32 @processCmd(i8* nocapture %0, i8* %1) {
%3 = alloca [1024 x i8]
%4 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
store i8 0, i8* %1
%5 = tail call i32* @popen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %10, label %7
7: ; preds = %2
%8 = call i32* @fgets(i8* %4, i32 1024, i32* %5)
%9 = icmp eq i32* %8, null
br i1 %9, label %21, label %12
10: ; preds = %2
%11 = tail call i32 @strcpy(i8* %1, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
br label %23
12: ; preds = %7, %17
%13 = call i32 @strlen(i8* %1)
%14 = call i32 @strlen(i8* %4)
%15 = add nsw i32 %14, %13
%16 = icmp slt i32 %15, 32767
br i1 %16, label %17, label %23
17: ; preds = %12
%18 = call i32 @strcat(i8* %1, i8* %4)
%19 = call i32* @fgets(i8* %4, i32 1024, i32* %5)
%20 = icmp eq i32* %19, null
br i1 %20, label %21, label %12
21: ; preds = %17, %7
%22 = call i32 @pclose(i32* %5)
br label %23
23: ; preds = %12, %21, %10
%24 = phi i32 [ -1, %10 ], [ 1, %21 ], [ -1, %12 ]
ret i32 %24
}