#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 ssize_t;

/* Variables and functions */
scalar_t__ EOF;
int STDOUT_FILENO;
int dup(int);
int dup2(int, int);
int /*<<< orphan*/ errno;
scalar_t__ fflush(int /*<<< orphan*/);
scalar_t__ pipe(int *);
int /*<<< orphan*/ printf(char *, ...);
int read(int, void *, size_t);
int /*<<< orphan*/ stdout;
char *strerror(int /*<<< orphan*/);
int strlen(void *);
int system_noblock(char **);
ssize_t system_noblock_with_result(char **argv, void *buf, size_t count) {
  int len = -1;
  int old_fd, new_fd;
  int fd[2];
  if (pipe(fd)) {
    printf("pipe failed\n");
    return -1;
  }
  int rfd = fd[0];
  int wfd = fd[1];
  if (EOF == fflush(stdout)) {
    printf("fflush failed: %s\n", strerror(errno));
    return -1;
  }
  if (-1 == (old_fd = dup(STDOUT_FILENO))) {
    printf("dup STDOUT_FILENO failed: %s\n", strerror(errno));
    return -1;
  }
  if (-1 == (new_fd = dup2(wfd, STDOUT_FILENO))) {
  }
  if (-1 == system_noblock(argv)) {
    printf("system call failed!\n");
    return -1;
  }
  if (-1 == read(rfd, buf, count - 1)) {
    printf("read buffer failed!\n");
    return -1;
  }
  len = strlen(buf);
  *((char *)buf + len - 1) = '\0';
  if (-1 == dup2(old_fd, new_fd)) {
    printf("dup2 failed: %s\n", strerror(errno));
    return -1;
  }
  return len;
}


x86 assembly (clang, -O0):
<system_noblock_with_result>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movl   $0xffffffff,-0x24(%rbp)
lea    -0x34(%rbp),%rdi
call   1080 <pipe@plt>
cmp    $0x0,%rax
je     11d8 <system_noblock_with_result+0x48>
lea    0xe3b(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1381 <system_noblock_with_result+0x1f1>
mov    -0x34(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    -0x30(%rbp),%eax
mov    %eax,-0x3c(%rbp)
mov    0x2df5(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x48(%rbp)
mov    0x2dcf(%rip),%rax
mov    (%rax),%edi
call   10a0 <fflush@plt>
mov    %rax,%rcx
mov    -0x48(%rbp),%rax
cmp    %rcx,%rax
jne    123b <system_noblock_with_result+0xab>
mov    0x2db9(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    %rax,%rsi
lea    0xde5(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1381 <system_noblock_with_result+0x1f1>
mov    0x2dae(%rip),%rax
mov    (%rax),%edi
call   1070 <dup@plt>
mov    %eax,%ecx
mov    %ecx,-0x28(%rbp)
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    1286 <system_noblock_with_result+0xf6>
mov    0x2d6e(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    %rax,%rsi
lea    0xdad(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1381 <system_noblock_with_result+0x1f1>
mov    -0x3c(%rbp),%edi
mov    0x2d60(%rip),%rax
mov    (%rax),%esi
call   1040 <dup2@plt>
mov    %eax,%ecx
mov    %ecx,-0x2c(%rbp)
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    12ae <system_noblock_with_result+0x11e>
jmp    12ae <system_noblock_with_result+0x11e>
mov    -0x10(%rbp),%rdi
call   1060 <system_noblock@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    12e0 <system_noblock_with_result+0x150>
lea    0xd71(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1381 <system_noblock_with_result+0x1f1>
mov    -0x38(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
sub    $0x1,%rdx
call   1090 <read@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    131d <system_noblock_with_result+0x18d>
lea    0xd49(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1381 <system_noblock_with_result+0x1f1>
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x24(%rbp)
mov    -0x18(%rbp),%rax
movslq -0x24(%rbp),%rcx
movb   $0x0,-0x1(%rax,%rcx,1)
mov    -0x28(%rbp),%edi
mov    -0x2c(%rbp),%esi
call   1040 <dup2@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    137b <system_noblock_with_result+0x1eb>
mov    0x2c79(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    %rax,%rsi
lea    0xd00(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1381 <system_noblock_with_result+0x1f1>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<system_noblock_with_result>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%r15
mov    %rsi,%r14
mov    %rdi,%rbp
lea    0x10(%rsp),%rdi
call   1090 <pipe@plt>
test   %rax,%rax
je     11d7 <system_noblock_with_result+0x37>
lea    0xe9d(%rip),%rdi
call   1030 <puts@plt>
jmp    12c3 <system_noblock_with_result+0x123>
mov    0x10(%rsp),%r12d
mov    0x14(%rsp),%r13d
mov    0x2df8(%rip),%rax
mov    (%rax),%rbx
mov    0x2dd6(%rip),%rax
mov    (%rax),%edi
call   10b0 <fflush@plt>
cmp    %rax,%rbx
jne    1218 <system_noblock_with_result+0x78>
mov    0x2dcb(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
lea    0xded(%rip),%rdi
jmp    12b9 <system_noblock_with_result+0x119>
mov    %r12d,%ebx
mov    0x2dce(%rip),%r12
mov    (%r12),%edi
call   1080 <dup@plt>
cmp    $0xffffffff,%eax
je     12a4 <system_noblock_with_result+0x104>
mov    %eax,0xc(%rsp)
mov    (%r12),%esi
mov    %r13d,%edi
call   1050 <dup2@plt>
mov    %eax,%r13d
mov    %rbp,%rdi
call   1070 <system_noblock@plt>
cmp    $0xffffffff,%eax
je     12d9 <system_noblock_with_result+0x139>
add    $0xffffffffffffffff,%r15
mov    %ebx,%edi
mov    %r14,%rsi
mov    %r15,%rdx
call   10a0 <read@plt>
cmp    $0xffffffff,%eax
je     12e7 <system_noblock_with_result+0x147>
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    %eax,%ebx
cltq
movb   $0x0,-0x1(%r14,%rax,1)
mov    0xc(%rsp),%edi
mov    %r13d,%esi
call   1050 <dup2@plt>
cmp    $0xffffffff,%eax
jne    12c8 <system_noblock_with_result+0x128>
mov    0x2d3c(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
lea    0xd8f(%rip),%rdi
jmp    12b9 <system_noblock_with_result+0x119>
mov    0x2d25(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
lea    0xd5a(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd76(%rip),%rdi
call   1030 <puts@plt>
jmp    12c3 <system_noblock_with_result+0x123>
lea    0xd54(%rip),%rdi
call   1030 <puts@plt>
jmp    12c3 <system_noblock_with_result+0x123>

LLVM IR (-O0):
@.str = [13 x i8] c"pipe failed\0A\00"
@EOF = global i64 0
@stdout = global i32 0
@.str.1 = [19 x i8] c"fflush failed: %s\0A\00"
@errno = global i32 0
@STDOUT_FILENO = global i32 0
@.str.2 = [30 x i8] c"dup STDOUT_FILENO failed: %s\0A\00"
@.str.3 = [21 x i8] c"system call failed!\0A\00"
@.str.4 = [21 x i8] c"read buffer failed!\0A\00"
@.str.5 = [17 x i8] c"dup2 failed: %s\0A\00"
define i32 @system_noblock_with_result(i8** %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca [2 x i32]
%12 = alloca i32
%13 = alloca i32
store i8** %0, i8*** %5
store i8* %1, i8** %6
store i64 %2, i64* %7
store i32 -1, i32* %8
%14 = getelementptr inbounds [2 x i32], [2 x i32]* %11, i64 0, i64 0
%15 = call i64 @pipe(i32* %14)
%16 = icmp ne i64 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %3
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %4
br label %79
19: ; preds = %3
%20 = getelementptr inbounds [2 x i32], [2 x i32]* %11, i64 0, i64 0
%21 = load i32, i32* %20
store i32 %21, i32* %12
%22 = getelementptr inbounds [2 x i32], [2 x i32]* %11, i64 0, i64 1
%23 = load i32, i32* %22
store i32 %23, i32* %13
%24 = load i64, i64* @EOF
%25 = load i32, i32* @stdout
%26 = call i64 @fflush(i32 %25)
%27 = icmp eq i64 %24, %26
br i1 %27, label %28, label %32
28: ; preds = %19
%29 = load i32, i32* @errno
%30 = call i8* @strerror(i32 %29)
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* %30)
store i32 -1, i32* %4
br label %79
32: ; preds = %19
%33 = load i32, i32* @STDOUT_FILENO
%34 = call i32 @dup(i32 %33)
store i32 %34, i32* %9
%35 = icmp eq i32 -1, %34
br i1 %35, label %36, label %40
36: ; preds = %32
%37 = load i32, i32* @errno
%38 = call i8* @strerror(i32 %37)
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0), i8* %38)
store i32 -1, i32* %4
br label %79
40: ; preds = %32
%41 = load i32, i32* %13
%42 = load i32, i32* @STDOUT_FILENO
%43 = call i32 @dup2(i32 %41, i32 %42)
store i32 %43, i32* %10
%44 = icmp eq i32 -1, %43
br i1 %44, label %45, label %46
45: ; preds = %40
br label %46
46: ; preds = %45, %40
%47 = load i8**, i8*** %5
%48 = call i32 @system_noblock(i8** %47)
%49 = icmp eq i32 -1, %48
br i1 %49, label %50, label %52
50: ; preds = %46
%51 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %4
br label %79
52: ; preds = %46
%53 = load i32, i32* %12
%54 = load i8*, i8** %6
%55 = load i64, i64* %7
%56 = sub i64 %55, 1
%57 = call i32 @read(i32 %53, i8* %54, i64 %56)
%58 = icmp eq i32 -1, %57
br i1 %58, label %59, label %61
59: ; preds = %52
%60 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0))
store i32 -1, i32* %4
br label %79
61: ; preds = %52
%62 = load i8*, i8** %6
%63 = call i32 @strlen(i8* %62)
store i32 %63, i32* %8
%64 = load i8*, i8** %6
%65 = load i32, i32* %8
%66 = sext i32 %65 to i64
%67 = getelementptr inbounds i8, i8* %64, i64 %66
%68 = getelementptr inbounds i8, i8* %67, i64 -1
store i8 0, i8* %68
%69 = load i32, i32* %9
%70 = load i32, i32* %10
%71 = call i32 @dup2(i32 %69, i32 %70)
%72 = icmp eq i32 -1, %71
br i1 %72, label %73, label %77
73: ; preds = %61
%74 = load i32, i32* @errno
%75 = call i8* @strerror(i32 %74)
%76 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.5, i64 0, i64 0), i8* %75)
store i32 -1, i32* %4
br label %79
77: ; preds = %61
%78 = load i32, i32* %8
store i32 %78, i32* %4
br label %79
79: ; preds = %77, %73, %59, %50, %36, %28, %17
%80 = load i32, i32* %4
ret i32 %80
}

LLVM IR (-O2):
@EOF = global i64 0
@stdout = global i32 0
@.str.1 = [19 x i8] c"fflush failed: %s\0A\00"
@errno = global i32 0
@STDOUT_FILENO = global i32 0
@.str.2 = [30 x i8] c"dup STDOUT_FILENO failed: %s\0A\00"
@.str.5 = [17 x i8] c"dup2 failed: %s\0A\00"
@str = [20 x i8] c"read buffer failed!\00"
@str.6 = [20 x i8] c"system call failed!\00"
@str.7 = [12 x i8] c"pipe failed\00"
define i32 @system_noblock_with_result(i8** %0, i8* %1, i64 %2) {
%4 = alloca [2 x i32]
%5 = bitcast [2 x i32]* %4 to i8*
%6 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 0
%7 = call i64 @pipe(i32* %6)
%8 = icmp eq i64 %7, 0
br i1 %8, label %11, label %9
9: ; preds = %3
%10 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str.7, i64 0, i64 0))
br label %55
11: ; preds = %3
%12 = load i32, i32* %6
%13 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%14 = load i32, i32* %13
%15 = load i64, i64* @EOF
%16 = load i32, i32* @stdout
%17 = call i64 @fflush(i32 %16)
%18 = icmp eq i64 %15, %17
br i1 %18, label %19, label %23
19: ; preds = %11
%20 = load i32, i32* @errno
%21 = call i8* @strerror(i32 %20)
%22 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* %21)
br label %55
23: ; preds = %11
%24 = load i32, i32* @STDOUT_FILENO
%25 = call i32 @dup(i32 %24)
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %31
27: ; preds = %23
%28 = load i32, i32* @errno
%29 = call i8* @strerror(i32 %28)
%30 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0), i8* %29)
br label %55
31: ; preds = %23
%32 = load i32, i32* @STDOUT_FILENO
%33 = call i32 @dup2(i32 %14, i32 %32)
%34 = call i32 @system_noblock(i8** %0)
%35 = icmp eq i32 %34, -1
br i1 %35, label %36, label %38
36: ; preds = %31
%37 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str.6, i64 0, i64 0))
br label %55
38: ; preds = %31
%39 = add i64 %2, -1
%40 = call i32 @read(i32 %12, i8* %1, i64 %39)
%41 = icmp eq i32 %40, -1
br i1 %41, label %42, label %44
42: ; preds = %38
%43 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str, i64 0, i64 0))
br label %55
44: ; preds = %38
%45 = call i32 @strlen(i8* %1)
%46 = sext i32 %45 to i64
%47 = getelementptr inbounds i8, i8* %1, i64 %46
%48 = getelementptr inbounds i8, i8* %47, i64 -1
store i8 0, i8* %48
%49 = call i32 @dup2(i32 %25, i32 %33)
%50 = icmp eq i32 %49, -1
br i1 %50, label %51, label %55
51: ; preds = %44
%52 = load i32, i32* @errno
%53 = call i8* @strerror(i32 %52)
%54 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.5, i64 0, i64 0), i8* %53)
br label %55
55: ; preds = %19, %27, %36, %42, %51, %44, %9
%56 = phi i32 [ -1, %9 ], [ -1, %19 ], [ -1, %27 ], [ -1, %36 ], [ -1, %42 ], [ -1, %51 ], [ %45, %44 ]
ret i32 %56
}