#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 EMFILE;
int ENOSPC;
scalar_t__ errno;
int fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ pclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *popen(char const *, char *);
int getExecResult(const char *command, char *output, const int buff_size) {
  FILE *fp;
  char *pCurrent;
  int bytes_read;
  int remain_bytes;
  if ((fp = popen(command, "r")) == NULL) {
    return errno != 0 ? errno : EMFILE;
  }
  pCurrent = output;
  remain_bytes = buff_size;
  while (remain_bytes > 0 &&
         (bytes_read = fread(pCurrent, 1, remain_bytes, fp)) > 0) {
    pCurrent += bytes_read;
    remain_bytes -= bytes_read;
  }
  pclose(fp);
  if (remain_bytes <= 0) {
    return ENOSPC;
  }
  *pCurrent = '\0';
  return 0;
}


x86 assembly (clang, -O0):
<getExecResult>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xeb2(%rip),%rsi
call   1050 <popen@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    119f <getExecResult+0x6f>
mov    0x2e78(%rip),%rax
cmpq   $0x0,(%rax)
je     1185 <getExecResult+0x55>
mov    0x2e67(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x40(%rbp)
jmp    1193 <getExecResult+0x63>
mov    0x2e3c(%rip),%rax
movslq (%rax),%rax
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rax
mov    %eax,-0x4(%rbp)
jmp    1240 <getExecResult+0x110>
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x38(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0x38(%rbp)
mov    %al,-0x41(%rbp)
jle    11dd <getExecResult+0xad>
mov    -0x30(%rbp),%rdi
mov    -0x38(%rbp),%edx
mov    -0x28(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
mov    %eax,-0x34(%rbp)
cmp    $0x0,%eax
setg   %al
mov    %al,-0x41(%rbp)
mov    -0x41(%rbp),%al
test   $0x1,%al
jne    11ed <getExecResult+0xbd>
jmp    120e <getExecResult+0xde>
mov    -0x34(%rbp),%ecx
mov    -0x30(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x30(%rbp)
mov    -0x34(%rbp),%ecx
mov    -0x38(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x38(%rbp)
jmp    11ad <getExecResult+0x7d>
mov    -0x28(%rbp),%rdi
call   1040 <pclose@plt>
cmpl   $0x0,-0x38(%rbp)
jg     1232 <getExecResult+0x102>
mov    0x2db0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1240 <getExecResult+0x110>
mov    -0x30(%rbp),%rax
movb   $0x0,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getExecResult>:
push   %rbp
push   %r14
push   %rbx
mov    %edx,%ebp
mov    %rsi,%rbx
lea    0xec0(%rip),%rsi
call   1050 <popen@plt>
test   %rax,%rax
je     1192 <getExecResult+0x62>
mov    %rax,%r14
test   %ebp,%ebp
jle    117f <getExecResult+0x4f>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    $0x1,%esi
mov    %ebp,%edx
mov    %r14,%rcx
call   1030 <fread@plt>
test   %eax,%eax
jle    11ac <getExecResult+0x7c>
mov    %eax,%ecx
add    %rcx,%rbx
sub    %eax,%ebp
jg     1160 <getExecResult+0x30>
mov    %r14,%rdi
call   1040 <pclose@plt>
mov    0x2e4a(%rip),%rax
mov    (%rax),%eax
jmp    11b9 <getExecResult+0x89>
mov    0x2e47(%rip),%rax
mov    (%rax),%rax
test   %rax,%rax
jne    11b9 <getExecResult+0x89>
mov    0x2e20(%rip),%rax
mov    (%rax),%eax
jmp    11b9 <getExecResult+0x89>
mov    %r14,%rdi
call   1040 <pclose@plt>
movb   $0x0,(%rbx)
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@errno = global i64 0
@EMFILE = global i32 0
@ENOSPC = global i32 0
define i32 @getExecResult(i8* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32*
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%12 = load i8*, i8** %5
%13 = call i32* @popen(i8* %12, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %13, i32** %8
%14 = icmp eq i32* %13, null
br i1 %14, label %15, label %26
15: ; preds = %3
%16 = load i64, i64* @errno
%17 = icmp ne i64 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %15
%19 = load i64, i64* @errno
br label %23
20: ; preds = %15
%21 = load i32, i32* @EMFILE
%22 = sext i32 %21 to i64
br label %23
23: ; preds = %20, %18
%24 = phi i64 [ %19, %18 ], [ %22, %20 ]
%25 = trunc i64 %24 to i32
store i32 %25, i32* %4
br label %57
26: ; preds = %3
%27 = load i8*, i8** %6
store i8* %27, i8** %9
%28 = load i32, i32* %7
store i32 %28, i32* %11
br label %29
29: ; preds = %40, %26
%30 = load i32, i32* %11
%31 = icmp sgt i32 %30, 0
br i1 %31, label %32, label %38
32: ; preds = %29
%33 = load i8*, i8** %9
%34 = load i32, i32* %11
%35 = load i32*, i32** %8
%36 = call i32 @fread(i8* %33, i32 1, i32 %34, i32* %35)
store i32 %36, i32* %10
%37 = icmp sgt i32 %36, 0
br label %38
38: ; preds = %32, %29
%39 = phi i1 [ false, %29 ], [ %37, %32 ]
br i1 %39, label %40, label %48
40: ; preds = %38
%41 = load i32, i32* %10
%42 = load i8*, i8** %9
%43 = sext i32 %41 to i64
%44 = getelementptr inbounds i8, i8* %42, i64 %43
store i8* %44, i8** %9
%45 = load i32, i32* %10
%46 = load i32, i32* %11
%47 = sub nsw i32 %46, %45
store i32 %47, i32* %11
br label %29
48: ; preds = %38
%49 = load i32*, i32** %8
%50 = call i32 @pclose(i32* %49)
%51 = load i32, i32* %11
%52 = icmp sle i32 %51, 0
br i1 %52, label %53, label %55
53: ; preds = %48
%54 = load i32, i32* @ENOSPC
store i32 %54, i32* %4
br label %57
55: ; preds = %48
%56 = load i8*, i8** %9
store i8 0, i8* %56
store i32 0, i32* %4
br label %57
57: ; preds = %55, %53, %23
%58 = load i32, i32* %4
ret i32 %58
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@errno = global i64 0
@EMFILE = global i32 0
@ENOSPC = global i32 0
define i32 @getExecResult(i8* nocapture %0, i8* nocapture %1, i32 %2) {
%4 = tail call i32* @popen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %8, label %6
6: ; preds = %3
%7 = icmp sgt i32 %2, 0
br i1 %7, label %15, label %25
8: ; preds = %3
%9 = load i64, i64* @errno
%10 = icmp eq i64 %9, 0
%11 = load i32, i32* @EMFILE
%12 = zext i32 %11 to i64
%13 = select i1 %10, i64 %12, i64 %9
%14 = trunc i64 %13 to i32
br label %30
15: ; preds = %6, %20
%16 = phi i32 [ %23, %20 ], [ %2, %6 ]
%17 = phi i8* [ %22, %20 ], [ %1, %6 ]
%18 = tail call i32 @fread(i8* %17, i32 1, i32 %16, i32* %4)
%19 = icmp sgt i32 %18, 0
br i1 %19, label %20, label %28
20: ; preds = %15
%21 = zext i32 %18 to i64
%22 = getelementptr inbounds i8, i8* %17, i64 %21
%23 = sub nsw i32 %16, %18
%24 = icmp sgt i32 %23, 0
br i1 %24, label %15, label %25
25: ; preds = %20, %6
%26 = tail call i32 @pclose(i32* %4)
%27 = load i32, i32* @ENOSPC
br label %30
28: ; preds = %15
%29 = tail call i32 @pclose(i32* %4)
store i8 0, i8* %17
br label %30
30: ; preds = %28, %25, %8
%31 = phi i32 [ %14, %8 ], [ %27, %25 ], [ 0, %28 ]
ret i32 %31
}