#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*/ close(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ perror(char *);
int recv(int, char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ send(int, char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ shutdown(int, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
scalar_t__ write(int, char *, int);
void forward_data_to_dest_and_pipe(int source_sock, int destination_sock,
                                   int pipe) {
  fprintf(stderr, "FORWARD DATA TO DEST AND PIPE \n");
  char buffer[1024];
  int n;
  while ((n = recv(source_sock, buffer, 1024, 0)) > 0) {
    send(destination_sock, buffer, n, 0);
    if (write(pipe, buffer, n) < 0) {
      perror("Buffer Overflow \n");
    }
  }
  shutdown(destination_sock, SHUT_RDWR);
  close(destination_sock);
  shutdown(source_sock, SHUT_RDWR);
  close(source_sock);
}


x86 assembly (clang, -O0):
<forward_data_to_dest_and_pipe>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %edx,-0xc(%rbp)
mov    0x2e4d(%rip),%rax
mov    (%rax),%edi
lea    0xe6c(%rip),%rsi
call   1080 <fprintf@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    1205 <forward_data_to_dest_and_pipe+0x95>
mov    -0x8(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    -0x414(%rbp),%edx
xor    %ecx,%ecx
call   1060 <send@plt>
mov    -0xc(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    -0x414(%rbp),%edx
call   1040 <write@plt>
cmp    $0x0,%rax
jge    1200 <forward_data_to_dest_and_pipe+0x90>
lea    0xe25(%rip),%rdi
call   1090 <perror@plt>
jmp    1199 <forward_data_to_dest_and_pipe+0x29>
mov    -0x8(%rbp),%edi
mov    0x2de1(%rip),%rax
mov    (%rax),%esi
call   1050 <shutdown@plt>
mov    -0x8(%rbp),%edi
call   1070 <close@plt>
mov    -0x4(%rbp),%edi
mov    0x2dc8(%rip),%rax
mov    (%rax),%esi
call   1050 <shutdown@plt>
mov    -0x4(%rbp),%edi
call   1070 <close@plt>
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<forward_data_to_dest_and_pipe>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x408,%rsp
mov    %edx,%r15d
mov    %esi,%r13d
mov    %edi,%r14d
mov    0x2e47(%rip),%rax
mov    (%rax),%edi
lea    0xe66(%rip),%rsi
call   1080 <fprintf@plt>
mov    %rsp,%rsi
mov    %r14d,%edi
mov    $0x400,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
test   %eax,%eax
jle    1213 <forward_data_to_dest_and_pipe+0xa3>
mov    %eax,%ebp
mov    %rsp,%rbx
lea    0xe5f(%rip),%r12
jmp    11e8 <forward_data_to_dest_and_pipe+0x78>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %r14d,%edi
mov    %rbx,%rsi
mov    $0x400,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,%ebp
test   %eax,%eax
jle    1213 <forward_data_to_dest_and_pipe+0xa3>
mov    %r13d,%edi
mov    %rbx,%rsi
mov    %ebp,%edx
xor    %ecx,%ecx
call   1060 <send@plt>
mov    %r15d,%edi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1040 <write@plt>
test   %rax,%rax
jns    11d0 <forward_data_to_dest_and_pipe+0x60>
mov    %r12,%rdi
call   1090 <perror@plt>
jmp    11d0 <forward_data_to_dest_and_pipe+0x60>
mov    0x2dd6(%rip),%rbx
mov    (%rbx),%esi
mov    %r13d,%edi
call   1050 <shutdown@plt>
mov    %r13d,%edi
call   1070 <close@plt>
mov    (%rbx),%esi
mov    %r14d,%edi
call   1050 <shutdown@plt>
mov    %r14d,%edi
call   1070 <close@plt>
add    $0x408,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [32 x i8] c"FORWARD DATA TO DEST AND PIPE \0A\00"
@.str.1 = [18 x i8] c"Buffer Overflow \0A\00"
@SHUT_RDWR = global i32 0
define void @forward_data_to_dest_and_pipe(i32 %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca [1024 x i8]
%8 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
store i32 %2, i32* %6
%9 = load i32, i32* @stderr
%10 = call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
br label %11
11: ; preds = %28, %3
%12 = load i32, i32* %4
%13 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%14 = call i32 @recv(i32 %12, i8* %13, i32 1024, i32 0)
store i32 %14, i32* %8
%15 = icmp sgt i32 %14, 0
br i1 %15, label %16, label %29
16: ; preds = %11
%17 = load i32, i32* %5
%18 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%19 = load i32, i32* %8
%20 = call i32 @send(i32 %17, i8* %18, i32 %19, i32 0)
%21 = load i32, i32* %6
%22 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%23 = load i32, i32* %8
%24 = call i64 @write(i32 %21, i8* %22, i32 %23)
%25 = icmp slt i64 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %16
%27 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
br label %28
28: ; preds = %26, %16
br label %11
29: ; preds = %11
%30 = load i32, i32* %5
%31 = load i32, i32* @SHUT_RDWR
%32 = call i32 @shutdown(i32 %30, i32 %31)
%33 = load i32, i32* %5
%34 = call i32 @close(i32 %33)
%35 = load i32, i32* %4
%36 = load i32, i32* @SHUT_RDWR
%37 = call i32 @shutdown(i32 %35, i32 %36)
%38 = load i32, i32* %4
%39 = call i32 @close(i32 %38)
ret void
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [32 x i8] c"FORWARD DATA TO DEST AND PIPE \0A\00"
@.str.1 = [18 x i8] c"Buffer Overflow \0A\00"
@SHUT_RDWR = global i32 0
define void @forward_data_to_dest_and_pipe(i32 %0, i32 %1, i32 %2) {
%4 = alloca [1024 x i8]
%5 = load i32, i32* @stderr
%6 = tail call i32 @fprintf(i32 %5, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
%7 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%8 = call i32 @recv(i32 %0, i8* %7, i32 1024, i32 0)
%9 = icmp sgt i32 %8, 0
br i1 %9, label %10, label %20
10: ; preds = %3, %17
%11 = phi i32 [ %18, %17 ], [ %8, %3 ]
%12 = call i32 @send(i32 %1, i8* %7, i32 %11, i32 0)
%13 = call i64 @write(i32 %2, i8* %7, i32 %11)
%14 = icmp slt i64 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %10
%16 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
br label %17
17: ; preds = %15, %10
%18 = call i32 @recv(i32 %0, i8* %7, i32 1024, i32 0)
%19 = icmp sgt i32 %18, 0
br i1 %19, label %10, label %20
20: ; preds = %17, %3
%21 = load i32, i32* @SHUT_RDWR
%22 = call i32 @shutdown(i32 %1, i32 %21)
%23 = call i32 @close(i32 %1)
%24 = load i32, i32* @SHUT_RDWR
%25 = call i32 @shutdown(i32 %0, i32 %24)
%26 = call i32 @close(i32 %0)
ret void
}