#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 */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ STDOUT_FILENO;
int /*<<< orphan*/ _exit(int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup2(int, int /*<<< orphan*/);
int /*<<< orphan*/ execl(char const *, char const *, char *);
int fork();
int pipe(int *);
int read(int, char *, size_t);
int /*<<< orphan*/ wait(int *);
int /*<<< orphan*/ write(int, char const *, size_t);
ssize_t redirect(const char *path, const char *input, size_t input_size,
                 char *output, size_t output_size) {
  int infd[2], outfd[2], status;
  ssize_t size;
  if (pipe(infd) == -1) {
    goto error;
  }
  if (pipe(outfd) == -1) {
    goto error_close_infd;
  }
  switch (fork()) {
  case 0:
    close(outfd[0]);
    close(infd[1]);
    dup2(outfd[1], STDOUT_FILENO);
    dup2(infd[0], STDIN_FILENO);
    execl(path, path, (char *)NULL);
    _exit(EXIT_FAILURE);
  case -1:
    goto error_close_outfd;
    break;
  default:
    close(infd[0]);
    close(outfd[1]);
    write(infd[1], input, input_size);
    close(infd[1]);
    size = read(outfd[0], output, output_size);
    close(outfd[0]);
    wait(&status);
    if (status != 0) {
      goto error;
    }
    if (size == -1) {
      goto error;
    }
    output[size] = '\0';
    break;
  }
out:
  return size;
error_close_outfd:
  close(outfd[0]);
  close(outfd[1]);
error_close_infd:
  close(infd[0]);
  close(infd[1]);
error:
  size = -1;
  goto out;
}


x86 assembly (clang, -O0):
<redirect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
lea    -0x30(%rbp),%rdi
call   1070 <pipe@plt>
cmp    $0xffffffff,%eax
jne    11c3 <redirect+0x33>
jmp    12fe <redirect+0x16e>
lea    -0x38(%rbp),%rdi
call   1070 <pipe@plt>
cmp    $0xffffffff,%eax
jne    11da <redirect+0x4a>
jmp    12ee <redirect+0x15e>
xor    %eax,%eax
call   10b0 <fork@plt>
mov    %eax,%ecx
mov    %ecx,-0x44(%rbp)
sub    $0xffffffff,%eax
je     1255 <redirect+0xc5>
jmp    11f4 <redirect+0x64>
mov    -0x44(%rbp),%eax
test   %eax,%eax
jne    125a <redirect+0xca>
jmp    1204 <redirect+0x74>
mov    -0x38(%rbp),%edi
call   1060 <close@plt>
mov    -0x2c(%rbp),%edi
call   1060 <close@plt>
mov    -0x34(%rbp),%edi
mov    0x2dd2(%rip),%rax
mov    (%rax),%esi
call   1050 <dup2@plt>
mov    -0x30(%rbp),%edi
mov    0x2da1(%rip),%rax
mov    (%rax),%esi
call   1050 <dup2@plt>
mov    -0x8(%rbp),%rdi
mov    -0x8(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1090 <execl@plt>
mov    0x2d8a(%rip),%rax
mov    (%rax),%edi
call   1030 <_exit@plt>
jmp    12de <redirect+0x14e>
mov    -0x30(%rbp),%edi
call   1060 <close@plt>
mov    -0x34(%rbp),%edi
call   1060 <close@plt>
mov    -0x2c(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1040 <write@plt>
mov    -0x2c(%rbp),%edi
call   1060 <close@plt>
mov    -0x38(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1080 <read@plt>
mov    %eax,-0x40(%rbp)
mov    -0x38(%rbp),%edi
call   1060 <close@plt>
lea    -0x3c(%rbp),%rdi
call   10a0 <wait@plt>
cmpl   $0x0,-0x3c(%rbp)
je     12b5 <redirect+0x125>
jmp    12fe <redirect+0x16e>
cmpl   $0xffffffff,-0x40(%rbp)
jne    12c4 <redirect+0x134>
jmp    12fe <redirect+0x16e>
mov    -0x20(%rbp),%rax
movslq -0x40(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    12d5 <redirect+0x145>
mov    -0x40(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret
mov    -0x38(%rbp),%edi
call   1060 <close@plt>
mov    -0x34(%rbp),%edi
call   1060 <close@plt>
mov    -0x30(%rbp),%edi
call   1060 <close@plt>
mov    -0x2c(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x40(%rbp)
jmp    12d5 <redirect+0x145>

x86 assembly (clang, -O2):
<redirect>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %r8,%r12
mov    %rcx,%r14
mov    %rdx,%r13
mov    %rsi,%rbp
mov    %rdi,%r15
lea    0xc(%rsp),%rdi
call   1070 <pipe@plt>
mov    $0xffffffff,%ebx
cmp    $0xffffffff,%eax
je     1276 <redirect+0xe6>
lea    0x4(%rsp),%rdi
call   1070 <pipe@plt>
cmp    $0xffffffff,%eax
je     1264 <redirect+0xd4>
xor    %eax,%eax
call   10b0 <fork@plt>
cmp    $0xffffffff,%eax
je     1252 <redirect+0xc2>
test   %eax,%eax
je     1287 <redirect+0xf7>
mov    0xc(%rsp),%edi
call   1060 <close@plt>
mov    0x8(%rsp),%edi
call   1060 <close@plt>
mov    0x10(%rsp),%edi
mov    %rbp,%rsi
mov    %r13,%rdx
call   1040 <write@plt>
mov    0x10(%rsp),%edi
call   1060 <close@plt>
mov    0x4(%rsp),%edi
mov    %r14,%rsi
mov    %r12,%rdx
call   1080 <read@plt>
mov    %eax,%ebp
mov    0x4(%rsp),%edi
call   1060 <close@plt>
lea    0x14(%rsp),%rdi
call   10a0 <wait@plt>
cmp    $0xffffffff,%ebp
je     1276 <redirect+0xe6>
cmpl   $0x0,0x14(%rsp)
jne    1276 <redirect+0xe6>
movslq %ebp,%rax
movb   $0x0,(%r14,%rax,1)
mov    %ebp,%ebx
jmp    1276 <redirect+0xe6>
mov    0x4(%rsp),%edi
call   1060 <close@plt>
mov    0x8(%rsp),%edi
call   1060 <close@plt>
mov    0xc(%rsp),%edi
call   1060 <close@plt>
mov    0x10(%rsp),%edi
call   1060 <close@plt>
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x4(%rsp),%edi
call   1060 <close@plt>
mov    0x10(%rsp),%edi
call   1060 <close@plt>
mov    0x8(%rsp),%edi
mov    0x2d4c(%rip),%rax
mov    (%rax),%esi
call   1050 <dup2@plt>
mov    0xc(%rsp),%edi
mov    0x2d1a(%rip),%rax
mov    (%rax),%esi
call   1050 <dup2@plt>
mov    %r15,%rdi
mov    %r15,%rsi
xor    %edx,%edx
call   1090 <execl@plt>
mov    0x2d07(%rip),%rax
mov    (%rax),%edi
call   1030 <_exit@plt>

LLVM IR (-O0):
@STDOUT_FILENO = global i32 0
@STDIN_FILENO = global i32 0
@EXIT_FAILURE = global i32 0
define i32 @redirect(i8* %0, i8* %1, i64 %2, i8* %3, i64 %4) {
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i8*
%10 = alloca i64
%11 = alloca [2 x i32]
%12 = alloca [2 x i32]
%13 = alloca i32
%14 = alloca i32
store i8* %0, i8** %6
store i8* %1, i8** %7
store i64 %2, i64* %8
store i8* %3, i8** %9
store i64 %4, i64* %10
%15 = getelementptr inbounds [2 x i32], [2 x i32]* %11, i64 0, i64 0
%16 = call i32 @pipe(i32* %15)
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %19
18: ; preds = %5
br label %100
19: ; preds = %5
%20 = getelementptr inbounds [2 x i32], [2 x i32]* %12, i64 0, i64 0
%21 = call i32 @pipe(i32* %20)
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %24
23: ; preds = %19
br label %93
24: ; preds = %19
%25 = call i32 (...) @fork()
switch i32 %25, label %47 [
i32 0, label %26
i32 -1, label %46
]
26: ; preds = %24
%27 = getelementptr inbounds [2 x i32], [2 x i32]* %12, i64 0, i64 0
%28 = load i32, i32* %27
%29 = call i32 @close(i32 %28)
%30 = getelementptr inbounds [2 x i32], [2 x i32]* %11, i64 0, i64 1
%31 = load i32, i32* %30
%32 = call i32 @close(i32 %31)
%33 = getelementptr inbounds [2 x i32], [2 x i32]* %12, i64 0, i64 1
%34 = load i32, i32* %33
%35 = load i32, i32* @STDOUT_FILENO
%36 = call i32 @dup2(i32 %34, i32 %35)
%37 = getelementptr inbounds [2 x i32], [2 x i32]* %11, i64 0, i64 0
%38 = load i32, i32* %37
%39 = load i32, i32* @STDIN_FILENO
%40 = call i32 @dup2(i32 %38, i32 %39)
%41 = load i8*, i8** %6
%42 = load i8*, i8** %6
%43 = call i32 @execl(i8* %41, i8* %42, i8* null)
%44 = load i32, i32* @EXIT_FAILURE
%45 = call i32 @_exit(i32 %44)
unreachable
46: ; preds = %24
br label %86
47: ; preds = %24
%48 = getelementptr inbounds [2 x i32], [2 x i32]* %11, i64 0, i64 0
%49 = load i32, i32* %48
%50 = call i32 @close(i32 %49)
%51 = getelementptr inbounds [2 x i32], [2 x i32]* %12, i64 0, i64 1
%52 = load i32, i32* %51
%53 = call i32 @close(i32 %52)
%54 = getelementptr inbounds [2 x i32], [2 x i32]* %11, i64 0, i64 1
%55 = load i32, i32* %54
%56 = load i8*, i8** %7
%57 = load i64, i64* %8
%58 = call i32 @write(i32 %55, i8* %56, i64 %57)
%59 = getelementptr inbounds [2 x i32], [2 x i32]* %11, i64 0, i64 1
%60 = load i32, i32* %59
%61 = call i32 @close(i32 %60)
%62 = getelementptr inbounds [2 x i32], [2 x i32]* %12, i64 0, i64 0
%63 = load i32, i32* %62
%64 = load i8*, i8** %9
%65 = load i64, i64* %10
%66 = call i32 @read(i32 %63, i8* %64, i64 %65)
store i32 %66, i32* %14
%67 = getelementptr inbounds [2 x i32], [2 x i32]* %12, i64 0, i64 0
%68 = load i32, i32* %67
%69 = call i32 @close(i32 %68)
%70 = call i32 @wait(i32* %13)
%71 = load i32, i32* %13
%72 = icmp ne i32 %71, 0
br i1 %72, label %73, label %74
73: ; preds = %47
br label %100
74: ; preds = %47
%75 = load i32, i32* %14
%76 = icmp eq i32 %75, -1
br i1 %76, label %77, label %78
77: ; preds = %74
br label %100
78: ; preds = %74
%79 = load i8*, i8** %9
%80 = load i32, i32* %14
%81 = sext i32 %80 to i64
%82 = getelementptr inbounds i8, i8* %79, i64 %81
store i8 0, i8* %82
br label %83
83: ; preds = %78
br label %84
84: ; preds = %100, %83
%85 = load i32, i32* %14
ret i32 %85
86: ; preds = %46
%87 = getelementptr inbounds [2 x i32], [2 x i32]* %12, i64 0, i64 0
%88 = load i32, i32* %87
%89 = call i32 @close(i32 %88)
%90 = getelementptr inbounds [2 x i32], [2 x i32]* %12, i64 0, i64 1
%91 = load i32, i32* %90
%92 = call i32 @close(i32 %91)
br label %93
93: ; preds = %86, %23
%94 = getelementptr inbounds [2 x i32], [2 x i32]* %11, i64 0, i64 0
%95 = load i32, i32* %94
%96 = call i32 @close(i32 %95)
%97 = getelementptr inbounds [2 x i32], [2 x i32]* %11, i64 0, i64 1
%98 = load i32, i32* %97
%99 = call i32 @close(i32 %98)
br label %100
100: ; preds = %93, %77, %73, %18
store i32 -1, i32* %14
br label %84
}

LLVM IR (-O2):
@STDOUT_FILENO = global i32 0
@STDIN_FILENO = global i32 0
@EXIT_FAILURE = global i32 0
define i32 @redirect(i8* %0, i8* nocapture %1, i64 %2, i8* nocapture %3, i64 %4) {
%6 = alloca [2 x i32]
%7 = alloca [2 x i32]
%8 = alloca i32
%9 = bitcast [2 x i32]* %6 to i8*
%10 = bitcast [2 x i32]* %7 to i8*
%11 = bitcast i32* %8 to i8*
%12 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%13 = call i32 @pipe(i32* %12)
%14 = icmp eq i32 %13, -1
br i1 %14, label %60, label %15
15: ; preds = %5
%16 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 0
%17 = call i32 @pipe(i32* %16)
%18 = icmp eq i32 %17, -1
br i1 %18, label %68, label %19
19: ; preds = %15
%20 = call i32 (...) @fork()
switch i32 %20, label %37 [
i32 0, label %21
i32 -1, label %62
]
21: ; preds = %19
%22 = load i32, i32* %16
%23 = call i32 @close(i32 %22)
%24 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%25 = load i32, i32* %24
%26 = call i32 @close(i32 %25)
%27 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 1
%28 = load i32, i32* %27
%29 = load i32, i32* @STDOUT_FILENO
%30 = call i32 @dup2(i32 %28, i32 %29)
%31 = load i32, i32* %12
%32 = load i32, i32* @STDIN_FILENO
%33 = call i32 @dup2(i32 %31, i32 %32)
%34 = call i32 @execl(i8* %0, i8* %0, i8* null)
%35 = load i32, i32* @EXIT_FAILURE
%36 = call i32 @_exit(i32 %35)
unreachable
37: ; preds = %19
%38 = load i32, i32* %12
%39 = call i32 @close(i32 %38)
%40 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 1
%41 = load i32, i32* %40
%42 = call i32 @close(i32 %41)
%43 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%44 = load i32, i32* %43
%45 = call i32 @write(i32 %44, i8* %1, i64 %2)
%46 = load i32, i32* %43
%47 = call i32 @close(i32 %46)
%48 = load i32, i32* %16
%49 = call i32 @read(i32 %48, i8* %3, i64 %4)
%50 = load i32, i32* %16
%51 = call i32 @close(i32 %50)
%52 = call i32 @wait(i32* %8)
%53 = load i32, i32* %8
%54 = icmp ne i32 %53, 0
%55 = icmp eq i32 %49, -1
%56 = or i1 %55, %54
br i1 %56, label %60, label %57
57: ; preds = %37
%58 = sext i32 %49 to i64
%59 = getelementptr inbounds i8, i8* %3, i64 %58
store i8 0, i8* %59
br label %60
60: ; preds = %68, %5, %37, %57
%61 = phi i32 [ %49, %57 ], [ -1, %37 ], [ -1, %5 ], [ -1, %68 ]
ret i32 %61
62: ; preds = %19
%63 = load i32, i32* %16
%64 = call i32 @close(i32 %63)
%65 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 1
%66 = load i32, i32* %65
%67 = call i32 @close(i32 %66)
br label %68
68: ; preds = %15, %62
%69 = load i32, i32* %12
%70 = call i32 @close(i32 %69)
%71 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%72 = load i32, i32* %71
%73 = call i32 @close(i32 %72)
br label %60
}