#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 Rio_readp(int, char *, int);
int Rio_writen(int, char *, int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ free(void *);
int /*<<< orphan*/ shutdown(int, int);
int /*<<< orphan*/ stderr;
void *forwarder(void *args) {
  int numBytes, lineNum, serverfd, clientfd;
  int byteCount = 0;
  char buf1[8192];
  clientfd = ((int *)args)[0];
  serverfd = ((int *)args)[1];
  free(args);
  while (1) {
    numBytes = Rio_readp(serverfd, buf1, 8192);
    if (numBytes <= 0) {
      break;
    }
    byteCount = Rio_writen(clientfd, buf1, numBytes);
    if (numBytes != byteCount) {
      fprintf(stderr, "Did not send correct number of bytes to client.\n");
      break;
    }
  }
  shutdown(clientfd, 1);
  return NULL;
}


x86 assembly (clang, -O0):
<forwarder>:
push   %rbp
mov    %rsp,%rbp
sub    $0x2020,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%rax
mov    0x4(%rax),%eax
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <free@plt>
mov    -0x14(%rbp),%edi
lea    -0x2020(%rbp),%rsi
mov    $0x2000,%edx
call   1070 <Rio_readp@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jg     11a8 <forwarder+0x58>
jmp    11e8 <forwarder+0x98>
mov    -0x18(%rbp),%edi
lea    -0x2020(%rbp),%rsi
mov    -0xc(%rbp),%edx
call   1060 <Rio_writen@plt>
mov    %eax,-0x1c(%rbp)
mov    -0xc(%rbp),%eax
cmp    -0x1c(%rbp),%eax
je     11e3 <forwarder+0x93>
mov    0x2e10(%rip),%rax
mov    (%rax),%edi
lea    0xe27(%rip),%rsi
call   1050 <fprintf@plt>
jmp    11e8 <forwarder+0x98>
jmp    1182 <forwarder+0x32>
mov    -0x18(%rbp),%edi
mov    $0x1,%esi
call   1040 <shutdown@plt>
xor    %eax,%eax
add    $0x2020,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<forwarder>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x2008,%rsp
mov    (%rdi),%r14d
mov    0x4(%rdi),%r15d
call   1030 <free@plt>
mov    %rsp,%rbx
nopl   0x0(%rax)
mov    %r15d,%edi
mov    %rbx,%rsi
mov    $0x2000,%edx
call   1070 <Rio_readp@plt>
test   %eax,%eax
jle    11ac <forwarder+0x5c>
mov    %eax,%ebp
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1060 <Rio_writen@plt>
cmp    %eax,%ebp
je     1170 <forwarder+0x20>
mov    0x2e42(%rip),%rax
mov    (%rax),%edi
lea    0xe59(%rip),%rsi
call   1050 <fprintf@plt>
mov    %r14d,%edi
mov    $0x1,%esi
call   1040 <shutdown@plt>
xor    %eax,%eax
add    $0x2008,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [49 x i8] c"Did not send correct number of bytes to client.\0A\00"
define i8* @forwarder(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca [8192 x i8]
store i8* %0, i8** %2
store i32 0, i32* %7
%9 = load i8*, i8** %2
%10 = bitcast i8* %9 to i32*
%11 = getelementptr inbounds i32, i32* %10, i64 0
%12 = load i32, i32* %11
store i32 %12, i32* %6
%13 = load i8*, i8** %2
%14 = bitcast i8* %13 to i32*
%15 = getelementptr inbounds i32, i32* %14, i64 1
%16 = load i32, i32* %15
store i32 %16, i32* %5
%17 = load i8*, i8** %2
%18 = call i32 @free(i8* %17)
br label %19
19: ; preds = %1, %37
%20 = load i32, i32* %5
%21 = getelementptr inbounds [8192 x i8], [8192 x i8]* %8, i64 0, i64 0
%22 = call i32 @Rio_readp(i32 %20, i8* %21, i32 8192)
store i32 %22, i32* %3
%23 = load i32, i32* %3
%24 = icmp sle i32 %23, 0
br i1 %24, label %25, label %26
25: ; preds = %19
br label %38
26: ; preds = %19
%27 = load i32, i32* %6
%28 = getelementptr inbounds [8192 x i8], [8192 x i8]* %8, i64 0, i64 0
%29 = load i32, i32* %3
%30 = call i32 @Rio_writen(i32 %27, i8* %28, i32 %29)
store i32 %30, i32* %7
%31 = load i32, i32* %3
%32 = load i32, i32* %7
%33 = icmp ne i32 %31, %32
br i1 %33, label %34, label %37
34: ; preds = %26
%35 = load i32, i32* @stderr
%36 = call i32 @fprintf(i32 %35, i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str, i64 0, i64 0))
br label %38
37: ; preds = %26
br label %19
38: ; preds = %34, %25
%39 = load i32, i32* %6
%40 = call i32 @shutdown(i32 %39, i32 1)
ret i8* null
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [49 x i8] c"Did not send correct number of bytes to client.\0A\00"
define noalias i8* @forwarder(i8* nocapture %0) {
%2 = alloca [8192 x i8]
%3 = getelementptr inbounds [8192 x i8], [8192 x i8]* %2, i64 0, i64 0
%4 = bitcast i8* %0 to i32*
%5 = load i32, i32* %4
%6 = getelementptr inbounds i8, i8* %0, i64 4
%7 = bitcast i8* %6 to i32*
%8 = load i32, i32* %7
%9 = tail call i32 @free(i8* %0)
br label %10
10: ; preds = %13, %1
%11 = call i32 @Rio_readp(i32 %8, i8* %3, i32 8192)
%12 = icmp slt i32 %11, 1
br i1 %12, label %19, label %13
13: ; preds = %10
%14 = call i32 @Rio_writen(i32 %5, i8* %3, i32 %11)
%15 = icmp eq i32 %11, %14
br i1 %15, label %10, label %16
16: ; preds = %13
%17 = load i32, i32* @stderr
%18 = call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str, i64 0, i64 0))
br label %19
19: ; preds = %10, %16
%20 = call i32 @shutdown(i32 %5, i32 1)
ret i8* null
}