#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 */

/* Variables and functions */
int /*<<< orphan*/ SHUT_RDWR;
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ STDOUT_FILENO;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup2(int, int /*<<< orphan*/);
int /*<<< orphan*/ exit(int);
scalar_t__ fork();
int /*<<< orphan*/ perror(char *);
scalar_t__ pipe(int *);
int read(int, char *, int);
int recv(int, char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ send(int, char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ shutdown(int, int /*<<< orphan*/);
int system(char *);
scalar_t__ write(int, char *, int);
void forward_data_ext(int source_sock, int destination_sock, char *cmd) {
  char buffer[1024];
  int n, i, pipe_in[2], pipe_out[2];
  if (pipe(pipe_in) < 0 || pipe(pipe_out) < 0) {
    perror("Cannot create pipe");
    exit(-8);
  }
  if (fork() == 0) {
    dup2(pipe_in[0], STDIN_FILENO);
    dup2(pipe_out[1], STDOUT_FILENO);
    close(pipe_in[1]);
    close(pipe_out[0]);
    n = system(cmd);
    exit(n);
  } else {
    close(pipe_in[0]);
    close(pipe_out[1]);
    while ((n = recv(source_sock, buffer, 1024, 0)) > 0) {
      if (write(pipe_in[1], buffer, n) < 0) {
        perror("Cannot write to pipe");
        exit(-9);
      }
      if ((i = read(pipe_out[0], buffer, 1024)) > 0) {
        send(destination_sock, buffer, i, 0);
      }
    }
    shutdown(destination_sock, SHUT_RDWR);
    close(destination_sock);
    shutdown(source_sock, SHUT_RDWR);
    close(source_sock);
  }
}


x86 assembly (clang, -O0):
<forward_data_ext>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %rdx,-0x10(%rbp)
lea    -0x420(%rbp),%rdi
call   10a0 <pipe@plt>
cmp    $0x0,%rax
jl     1201 <forward_data_ext+0x41>
lea    -0x428(%rbp),%rdi
call   10a0 <pipe@plt>
cmp    $0x0,%rax
jge    1217 <forward_data_ext+0x57>
lea    0xdf8(%rip),%rdi
call   10c0 <perror@plt>
mov    $0xfffffff8,%edi
call   10d0 <exit@plt>
mov    $0x0,%al
call   10e0 <fork@plt>
cmp    $0x0,%rax
jne    1280 <forward_data_ext+0xc0>
mov    -0x420(%rbp),%edi
mov    0x2d9b(%rip),%rax
mov    (%rax),%esi
call   1070 <dup2@plt>
mov    -0x424(%rbp),%edi
mov    0x2da7(%rip),%rax
mov    (%rax),%esi
call   1070 <dup2@plt>
mov    -0x41c(%rbp),%edi
call   1090 <close@plt>
mov    -0x428(%rbp),%edi
call   1090 <close@plt>
mov    -0x10(%rbp),%rdi
call   1060 <system@plt>
mov    %eax,-0x414(%rbp)
mov    -0x414(%rbp),%edi
call   10d0 <exit@plt>
mov    -0x420(%rbp),%edi
call   1090 <close@plt>
mov    -0x424(%rbp),%edi
call   1090 <close@plt>
mov    -0x4(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    $0x400,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x414(%rbp)
cmp    $0x0,%eax
jle    1335 <forward_data_ext+0x175>
mov    -0x41c(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    -0x414(%rbp),%edx
call   1040 <write@plt>
cmp    $0x0,%rax
jge    12f3 <forward_data_ext+0x133>
lea    0xd2f(%rip),%rdi
call   10c0 <perror@plt>
mov    $0xfffffff7,%edi
call   10d0 <exit@plt>
mov    -0x428(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    $0x400,%edx
call   10b0 <read@plt>
mov    %eax,-0x418(%rbp)
cmp    $0x0,%eax
jle    1330 <forward_data_ext+0x170>
mov    -0x8(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    -0x418(%rbp),%edx
xor    %ecx,%ecx
call   1080 <send@plt>
jmp    1296 <forward_data_ext+0xd6>
mov    -0x8(%rbp),%edi
mov    0x2ca9(%rip),%rax
mov    (%rax),%esi
call   1050 <shutdown@plt>
mov    -0x8(%rbp),%edi
call   1090 <close@plt>
mov    -0x4(%rbp),%edi
mov    0x2c90(%rip),%rax
mov    (%rax),%esi
call   1050 <shutdown@plt>
mov    -0x4(%rbp),%edi
call   1090 <close@plt>
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<forward_data_ext>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x418,%rsp
mov    %rdx,%r14
mov    %esi,%r15d
mov    %edi,%ebx
lea    0x8(%rsp),%rdi
call   10a0 <pipe@plt>
test   %rax,%rax
js     12dc <forward_data_ext+0x11c>
mov    %rsp,%rdi
call   10a0 <pipe@plt>
test   %rax,%rax
js     12dc <forward_data_ext+0x11c>
xor    %eax,%eax
call   10e0 <fork@plt>
mov    0x8(%rsp),%edi
test   %rax,%rax
je     12f2 <forward_data_ext+0x132>
call   1090 <close@plt>
mov    0x4(%rsp),%edi
call   1090 <close@plt>
lea    0x10(%rsp),%rsi
mov    %ebx,%edi
mov    $0x400,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
test   %eax,%eax
jle    128d <forward_data_ext+0xcd>
lea    0x10(%rsp),%rbp
jmp    1255 <forward_data_ext+0x95>
nopl   0x0(%rax)
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0x400,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
test   %eax,%eax
jle    128d <forward_data_ext+0xcd>
mov    0xc(%rsp),%edi
mov    %rbp,%rsi
mov    %eax,%edx
call   1040 <write@plt>
test   %rax,%rax
js     12c6 <forward_data_ext+0x106>
mov    (%rsp),%edi
mov    %rbp,%rsi
mov    $0x400,%edx
call   10b0 <read@plt>
test   %eax,%eax
jle    1240 <forward_data_ext+0x80>
mov    %r15d,%edi
mov    %rbp,%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   1080 <send@plt>
jmp    1240 <forward_data_ext+0x80>
mov    0x2d54(%rip),%rbp
mov    0x0(%rbp),%esi
mov    %r15d,%edi
call   1050 <shutdown@plt>
mov    %r15d,%edi
call   1090 <close@plt>
mov    0x0(%rbp),%esi
mov    %ebx,%edi
call   1050 <shutdown@plt>
mov    %ebx,%edi
call   1090 <close@plt>
add    $0x418,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd46(%rip),%rdi
call   10c0 <perror@plt>
mov    $0xfffffff7,%edi
call   10d0 <exit@plt>
lea    0xd1d(%rip),%rdi
call   10c0 <perror@plt>
mov    $0xfffffff8,%edi
call   10d0 <exit@plt>
mov    0x2cd7(%rip),%rax
mov    (%rax),%esi
call   1070 <dup2@plt>
mov    0x4(%rsp),%edi
mov    0x2ce5(%rip),%rax
mov    (%rax),%esi
call   1070 <dup2@plt>
mov    0xc(%rsp),%edi
call   1090 <close@plt>
mov    (%rsp),%edi
call   1090 <close@plt>
mov    %r14,%rdi
call   1060 <system@plt>
mov    %eax,%edi
call   10d0 <exit@plt>

LLVM IR (-O0):
@.str = [19 x i8] c"Cannot create pipe\00"
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@.str.1 = [21 x i8] c"Cannot write to pipe\00"
@SHUT_RDWR = global i32 0
define void @forward_data_ext(i32 %0, i32 %1, i8* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca [1024 x i8]
%8 = alloca i32
%9 = alloca i32
%10 = alloca [2 x i32]
%11 = alloca [2 x i32]
store i32 %0, i32* %4
store i32 %1, i32* %5
store i8* %2, i8** %6
%12 = getelementptr inbounds [2 x i32], [2 x i32]* %10, i64 0, i64 0
%13 = call i64 @pipe(i32* %12)
%14 = icmp slt i64 %13, 0
br i1 %14, label %19, label %15
15: ; preds = %3
%16 = getelementptr inbounds [2 x i32], [2 x i32]* %11, i64 0, i64 0
%17 = call i64 @pipe(i32* %16)
%18 = icmp slt i64 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %15, %3
%20 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
%21 = call i32 @exit(i32 -8)
unreachable
22: ; preds = %15
%23 = call i64 (...) @fork()
%24 = icmp eq i64 %23, 0
br i1 %24, label %25, label %44
25: ; preds = %22
%26 = getelementptr inbounds [2 x i32], [2 x i32]* %10, i64 0, i64 0
%27 = load i32, i32* %26
%28 = load i32, i32* @STDIN_FILENO
%29 = call i32 @dup2(i32 %27, i32 %28)
%30 = getelementptr inbounds [2 x i32], [2 x i32]* %11, i64 0, i64 1
%31 = load i32, i32* %30
%32 = load i32, i32* @STDOUT_FILENO
%33 = call i32 @dup2(i32 %31, i32 %32)
%34 = getelementptr inbounds [2 x i32], [2 x i32]* %10, i64 0, i64 1
%35 = load i32, i32* %34
%36 = call i32 @close(i32 %35)
%37 = getelementptr inbounds [2 x i32], [2 x i32]* %11, i64 0, i64 0
%38 = load i32, i32* %37
%39 = call i32 @close(i32 %38)
%40 = load i8*, i8** %6
%41 = call i32 @system(i8* %40)
store i32 %41, i32* %8
%42 = load i32, i32* %8
%43 = call i32 @exit(i32 %42)
unreachable
44: ; preds = %22
%45 = getelementptr inbounds [2 x i32], [2 x i32]* %10, i64 0, i64 0
%46 = load i32, i32* %45
%47 = call i32 @close(i32 %46)
%48 = getelementptr inbounds [2 x i32], [2 x i32]* %11, i64 0, i64 1
%49 = load i32, i32* %48
%50 = call i32 @close(i32 %49)
br label %51
51: ; preds = %77, %44
%52 = load i32, i32* %4
%53 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%54 = call i32 @recv(i32 %52, i8* %53, i32 1024, i32 0)
store i32 %54, i32* %8
%55 = icmp sgt i32 %54, 0
br i1 %55, label %56, label %78
56: ; preds = %51
%57 = getelementptr inbounds [2 x i32], [2 x i32]* %10, i64 0, i64 1
%58 = load i32, i32* %57
%59 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%60 = load i32, i32* %8
%61 = call i64 @write(i32 %58, i8* %59, i32 %60)
%62 = icmp slt i64 %61, 0
br i1 %62, label %63, label %66
63: ; preds = %56
%64 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
%65 = call i32 @exit(i32 -9)
unreachable
66: ; preds = %56
%67 = getelementptr inbounds [2 x i32], [2 x i32]* %11, i64 0, i64 0
%68 = load i32, i32* %67
%69 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%70 = call i32 @read(i32 %68, i8* %69, i32 1024)
store i32 %70, i32* %9
%71 = icmp sgt i32 %70, 0
br i1 %71, label %72, label %77
72: ; preds = %66
%73 = load i32, i32* %5
%74 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%75 = load i32, i32* %9
%76 = call i32 @send(i32 %73, i8* %74, i32 %75, i32 0)
br label %77
77: ; preds = %72, %66
br label %51
78: ; preds = %51
%79 = load i32, i32* %5
%80 = load i32, i32* @SHUT_RDWR
%81 = call i32 @shutdown(i32 %79, i32 %80)
%82 = load i32, i32* %5
%83 = call i32 @close(i32 %82)
%84 = load i32, i32* %4
%85 = load i32, i32* @SHUT_RDWR
%86 = call i32 @shutdown(i32 %84, i32 %85)
%87 = load i32, i32* %4
%88 = call i32 @close(i32 %87)
br label %89
89: ; preds = %78
ret void
}

LLVM IR (-O2):
@.str = [19 x i8] c"Cannot create pipe\00"
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@.str.1 = [21 x i8] c"Cannot write to pipe\00"
@SHUT_RDWR = global i32 0
define void @forward_data_ext(i32 %0, i32 %1, i8* nocapture %2) {
%4 = alloca [1024 x i8]
%5 = alloca [2 x i32]
%6 = alloca [2 x i32]
%7 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%8 = bitcast [2 x i32]* %5 to i8*
%9 = bitcast [2 x i32]* %6 to i8*
%10 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 0
%11 = call i64 @pipe(i32* %10)
%12 = icmp slt i64 %11, 0
br i1 %12, label %17, label %13
13: ; preds = %3
%14 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%15 = call i64 @pipe(i32* %14)
%16 = icmp slt i64 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %13, %3
%18 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
%19 = call i32 @exit(i32 -8)
unreachable
20: ; preds = %13
%21 = call i64 (...) @fork()
%22 = icmp eq i64 %21, 0
%23 = load i32, i32* %10
br i1 %22, label %24, label %38
24: ; preds = %20
%25 = load i32, i32* @STDIN_FILENO
%26 = call i32 @dup2(i32 %23, i32 %25)
%27 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%28 = load i32, i32* %27
%29 = load i32, i32* @STDOUT_FILENO
%30 = call i32 @dup2(i32 %28, i32 %29)
%31 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 1
%32 = load i32, i32* %31
%33 = call i32 @close(i32 %32)
%34 = load i32, i32* %14
%35 = call i32 @close(i32 %34)
%36 = call i32 @system(i8* %2)
%37 = call i32 @exit(i32 %36)
unreachable
38: ; preds = %20
%39 = call i32 @close(i32 %23)
%40 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%41 = load i32, i32* %40
%42 = call i32 @close(i32 %41)
%43 = call i32 @recv(i32 %0, i8* %7, i32 1024, i32 0)
%44 = icmp sgt i32 %43, 0
br i1 %44, label %45, label %64
45: ; preds = %38
%46 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 1
br label %47
47: ; preds = %45, %61
%48 = phi i32 [ %43, %45 ], [ %62, %61 ]
%49 = load i32, i32* %46
%50 = call i64 @write(i32 %49, i8* %7, i32 %48)
%51 = icmp slt i64 %50, 0
br i1 %51, label %52, label %55
52: ; preds = %47
%53 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
%54 = call i32 @exit(i32 -9)
unreachable
55: ; preds = %47
%56 = load i32, i32* %14
%57 = call i32 @read(i32 %56, i8* %7, i32 1024)
%58 = icmp sgt i32 %57, 0
br i1 %58, label %59, label %61
59: ; preds = %55
%60 = call i32 @send(i32 %1, i8* %7, i32 %57, i32 0)
br label %61
61: ; preds = %59, %55
%62 = call i32 @recv(i32 %0, i8* %7, i32 1024, i32 0)
%63 = icmp sgt i32 %62, 0
br i1 %63, label %47, label %64
64: ; preds = %61, %38
%65 = load i32, i32* @SHUT_RDWR
%66 = call i32 @shutdown(i32 %1, i32 %65)
%67 = call i32 @close(i32 %1)
%68 = load i32, i32* @SHUT_RDWR
%69 = call i32 @shutdown(i32 %0, i32 %68)
%70 = call i32 @close(i32 %0)
ret void
}