#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 /*<<< orphan*/ resp;
typedef int /*<<< orphan*/ req;

/* Variables and functions */
int accept_connection(int);
int /*<<< orphan*/ close(int);
int connect_to(char *, char *);
int listen_on_port(char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
int recv(int, char *, int, int /*<<< orphan*/);
int send(int, char *, int, int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  int socketfd, remotefd, targetfd;
  int received, sent;
  int target_sent, target_received;
  char req[4096], resp[4096];
  socketfd = listen_on_port(argv[1]);
  if (socketfd != -1) {
    remotefd = accept_connection(socketfd);
    if (remotefd != -1) {
      received = recv(remotefd, req, sizeof(req), 0);
      printf("received %d bytes\n", received);
      targetfd = connect_to(argv[2], argv[3]);
      if (targetfd != -1) {
        target_sent = send(targetfd, req, received, 0);
        printf("sent %d bytes to target\n", target_sent);
        target_received = recv(targetfd, resp, sizeof(resp), 0);
        printf("got %d bytes from target\n", target_received);
      } else {
        perror("ERROR: unable to connect to target");
      }
      sent = send(remotefd, resp, target_received, 0);
      printf("sent %d bytes\n", sent);
      close(remotefd);
      close(socketfd);
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x2030,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1090 <listen_on_port@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
je     12ac <main+0x12c>
mov    -0x14(%rbp),%edi
call   1080 <accept_connection@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
je     12a7 <main+0x127>
mov    -0x18(%rbp),%edi
lea    -0x1030(%rbp),%rsi
mov    $0x1000,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%esi
lea    0xe15(%rip),%rdi
call   1060 <printf@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rsi
call   1040 <connect_to@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
je     1265 <main+0xe5>
mov    -0x1c(%rbp),%edi
lea    -0x1030(%rbp),%rsi
mov    -0x20(%rbp),%edx
xor    %ecx,%ecx
call   1050 <send@plt>
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%esi
lea    0xde0(%rip),%rdi
call   1060 <printf@plt>
mov    -0x1c(%rbp),%edi
lea    -0x2030(%rbp),%rsi
mov    $0x1000,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%esi
lea    0xdd1(%rip),%rdi
call   1060 <printf@plt>
jmp    1271 <main+0xf1>
lea    0xdda(%rip),%rdi
call   10a0 <perror@plt>
mov    -0x18(%rbp),%edi
lea    -0x2030(%rbp),%rsi
mov    -0x2c(%rbp),%edx
xor    %ecx,%ecx
call   1050 <send@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%esi
lea    0xdd7(%rip),%rdi
call   1060 <printf@plt>
mov    -0x18(%rbp),%edi
call   1070 <close@plt>
mov    -0x14(%rbp),%edi
call   1070 <close@plt>
jmp    12ac <main+0x12c>
xor    %eax,%eax
add    $0x2030,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x2008,%rsp
mov    %rsi,%rbp
mov    0x8(%rsi),%rdi
call   1090 <listen_on_port@plt>
cmp    $0xffffffff,%eax
je     1263 <main+0xe3>
mov    %eax,%ebx
mov    %eax,%edi
call   1080 <accept_connection@plt>
cmp    $0xffffffff,%eax
je     1263 <main+0xe3>
mov    %eax,%r14d
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0x1000,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,%r15d
lea    0xe2e(%rip),%rdi
mov    %eax,%esi
call   1060 <printf@plt>
mov    0x10(%rbp),%rdi
mov    0x18(%rbp),%rsi
call   1040 <connect_to@plt>
cmp    $0xffffffff,%eax
je     1273 <main+0xf3>
mov    %eax,%ebp
mov    %rsp,%rsi
mov    %eax,%edi
mov    %r15d,%edx
xor    %ecx,%ecx
call   1050 <send@plt>
lea    0xe0c(%rip),%rdi
mov    %eax,%esi
call   1060 <printf@plt>
lea    0x1000(%rsp),%r15
mov    %ebp,%edi
mov    %r15,%rsi
mov    $0x1000,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,%ebp
lea    0xdfc(%rip),%rdi
mov    %eax,%esi
call   1060 <printf@plt>
mov    %r14d,%edi
mov    %r15,%rsi
mov    %ebp,%edx
xor    %ecx,%ecx
call   1050 <send@plt>
lea    0xe1c(%rip),%rdi
mov    %eax,%esi
call   1060 <printf@plt>
mov    %r14d,%edi
call   1070 <close@plt>
mov    %ebx,%edi
call   1070 <close@plt>
xor    %eax,%eax
add    $0x2008,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdcc(%rip),%rdi
call   10a0 <perror@plt>

LLVM IR (-O0):
@.str = [19 x i8] c"received %d bytes\0A\00"
@.str.1 = [25 x i8] c"sent %d bytes to target\0A\00"
@.str.2 = [26 x i8] c"got %d bytes from target\0A\00"
@.str.3 = [35 x i8] c"ERROR: unable to connect to target\00"
@.str.4 = [15 x i8] c"sent %d bytes\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca [4096 x i8]
%14 = alloca [4096 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 1
%17 = load i8*, i8** %16
%18 = call i32 @listen_on_port(i8* %17)
store i32 %18, i32* %6
%19 = load i32, i32* %6
%20 = icmp ne i32 %19, -1
br i1 %20, label %21, label %67
21: ; preds = %2
%22 = load i32, i32* %6
%23 = call i32 @accept_connection(i32 %22)
store i32 %23, i32* %7
%24 = load i32, i32* %7
%25 = icmp ne i32 %24, -1
br i1 %25, label %26, label %66
26: ; preds = %21
%27 = load i32, i32* %7
%28 = getelementptr inbounds [4096 x i8], [4096 x i8]* %13, i64 0, i64 0
%29 = call i32 @recv(i32 %27, i8* %28, i32 4096, i32 0)
store i32 %29, i32* %9
%30 = load i32, i32* %9
%31 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i32 %30)
%32 = load i8**, i8*** %5
%33 = getelementptr inbounds i8*, i8** %32, i64 2
%34 = load i8*, i8** %33
%35 = load i8**, i8*** %5
%36 = getelementptr inbounds i8*, i8** %35, i64 3
%37 = load i8*, i8** %36
%38 = call i32 @connect_to(i8* %34, i8* %37)
store i32 %38, i32* %8
%39 = load i32, i32* %8
%40 = icmp ne i32 %39, -1
br i1 %40, label %41, label %53
41: ; preds = %26
%42 = load i32, i32* %8
%43 = getelementptr inbounds [4096 x i8], [4096 x i8]* %13, i64 0, i64 0
%44 = load i32, i32* %9
%45 = call i32 @send(i32 %42, i8* %43, i32 %44, i32 0)
store i32 %45, i32* %11
%46 = load i32, i32* %11
%47 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i32 %46)
%48 = load i32, i32* %8
%49 = getelementptr inbounds [4096 x i8], [4096 x i8]* %14, i64 0, i64 0
%50 = call i32 @recv(i32 %48, i8* %49, i32 4096, i32 0)
store i32 %50, i32* %12
%51 = load i32, i32* %12
%52 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i32 %51)
br label %55
53: ; preds = %26
%54 = call i32 @perror(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.3, i64 0, i64 0))
br label %55
55: ; preds = %53, %41
%56 = load i32, i32* %7
%57 = getelementptr inbounds [4096 x i8], [4096 x i8]* %14, i64 0, i64 0
%58 = load i32, i32* %12
%59 = call i32 @send(i32 %56, i8* %57, i32 %58, i32 0)
store i32 %59, i32* %10
%60 = load i32, i32* %10
%61 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0), i32 %60)
%62 = load i32, i32* %7
%63 = call i32 @close(i32 %62)
%64 = load i32, i32* %6
%65 = call i32 @close(i32 %64)
br label %66
66: ; preds = %55, %21
br label %67
67: ; preds = %66, %2
ret i32 0
}

LLVM IR (-O2):
@.str = [19 x i8] c"received %d bytes\0A\00"
@.str.1 = [25 x i8] c"sent %d bytes to target\0A\00"
@.str.2 = [26 x i8] c"got %d bytes from target\0A\00"
@.str.3 = [35 x i8] c"ERROR: unable to connect to target\00"
@.str.4 = [15 x i8] c"sent %d bytes\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [4096 x i8]
%4 = alloca [4096 x i8]
%5 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%6 = getelementptr inbounds [4096 x i8], [4096 x i8]* %4, i64 0, i64 0
%7 = getelementptr inbounds i8*, i8** %1, i64 1
%8 = load i8*, i8** %7
%9 = tail call i32 @listen_on_port(i8* %8)
%10 = icmp eq i32 %9, -1
br i1 %10, label %34, label %11
11: ; preds = %2
%12 = tail call i32 @accept_connection(i32 %9)
%13 = icmp eq i32 %12, -1
br i1 %13, label %34, label %14
14: ; preds = %11
%15 = call i32 @recv(i32 %12, i8* %5, i32 4096, i32 0)
%16 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i32 %15)
%17 = getelementptr inbounds i8*, i8** %1, i64 2
%18 = load i8*, i8** %17
%19 = getelementptr inbounds i8*, i8** %1, i64 3
%20 = load i8*, i8** %19
%21 = call i32 @connect_to(i8* %18, i8* %20)
%22 = icmp eq i32 %21, -1
br i1 %22, label %32, label %23
23: ; preds = %14
%24 = call i32 @send(i32 %21, i8* %5, i32 %15, i32 0)
%25 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i32 %24)
%26 = call i32 @recv(i32 %21, i8* %6, i32 4096, i32 0)
%27 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i32 %26)
%28 = call i32 @send(i32 %12, i8* %6, i32 %26, i32 0)
%29 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0), i32 %28)
%30 = call i32 @close(i32 %12)
%31 = call i32 @close(i32 %9)
br label %34
32: ; preds = %14
%33 = call i32 @perror(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.3, i64 0, i64 0))
unreachable
34: ; preds = %11, %23, %2
ret i32 0
}