#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 client_posix_connect(char *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ die(char *, char *);
int /*<<< orphan*/ make_nonblocking(int);
int my_atoi(char *);
void *my_malloc(int);
int /*<<< orphan*/ print(char *);
int server_posix_accept(int);
int server_posix_listen(int);
int /*<<< orphan*/ try_relaying(int, int, void *);
int main(int argc, char **argv) {
  int servport;
  int listenfd;
  int serverfd;
  int clientfd;
  void *buf;
  if (argc != 4)
    die("usage: %s <listen port> <connect hostname> <connect port>\n", argv[0]);
  servport = my_atoi(argv[1]);
  listenfd = server_posix_listen(servport);
  serverfd = server_posix_accept(listenfd);
  print("got a connection\n");
  make_nonblocking(serverfd);
  clientfd = client_posix_connect(argv[2], my_atoi(argv[3]));
  make_nonblocking(clientfd);
  buf = my_malloc(4096);
  while (1) {
    if (!try_relaying(serverfd, clientfd, buf))
      break;
    if (!try_relaying(clientfd, serverfd, buf))
      break;
  }
  close(serverfd);
  close(clientfd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x4,-0x8(%rbp)
je     11d3 <main+0x33>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe32(%rip),%rdi
call   10a0 <die@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1030 <my_atoi@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edi
call   10c0 <server_posix_listen@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%edi
call   1060 <server_posix_accept@plt>
mov    %eax,-0x1c(%rbp)
lea    0xe3b(%rip),%rdi
call   1080 <print@plt>
mov    -0x1c(%rbp),%edi
call   10b0 <make_nonblocking@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
call   1030 <my_atoi@plt>
mov    -0x30(%rbp),%rdi
mov    %eax,%esi
call   1050 <client_posix_connect@plt>
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%edi
call   10b0 <make_nonblocking@plt>
mov    $0x1000,%edi
call   1040 <my_malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x1c(%rbp),%edi
mov    -0x20(%rbp),%esi
mov    -0x28(%rbp),%rdx
call   1090 <try_relaying@plt>
cmp    $0x0,%eax
jne    1267 <main+0xc7>
jmp    1289 <main+0xe9>
mov    -0x20(%rbp),%edi
mov    -0x1c(%rbp),%esi
mov    -0x28(%rbp),%rdx
call   1090 <try_relaying@plt>
cmp    $0x0,%eax
jne    1284 <main+0xe4>
jmp    1289 <main+0xe9>
jmp    124a <main+0xaa>
mov    -0x1c(%rbp),%edi
call   1070 <close@plt>
mov    -0x20(%rbp),%edi
call   1070 <close@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%rbp
cmp    $0x4,%edi
je     11bc <main+0x1c>
mov    0x0(%rbp),%rsi
lea    0xe49(%rip),%rdi
call   10a0 <die@plt>
mov    0x8(%rbp),%rdi
call   1030 <my_atoi@plt>
mov    %eax,%edi
call   10c0 <server_posix_listen@plt>
mov    %eax,%edi
call   1060 <server_posix_accept@plt>
mov    %eax,%r14d
lea    0xe5e(%rip),%rdi
call   1080 <print@plt>
mov    %r14d,%edi
call   10b0 <make_nonblocking@plt>
mov    0x10(%rbp),%rbx
mov    0x18(%rbp),%rdi
call   1030 <my_atoi@plt>
mov    %rbx,%rdi
mov    %eax,%esi
call   1050 <client_posix_connect@plt>
mov    %eax,%ebp
mov    %eax,%edi
call   10b0 <make_nonblocking@plt>
mov    $0x1000,%edi
call   1040 <my_malloc@plt>
mov    %rax,%rbx
nopw   0x0(%rax,%rax,1)
mov    %r14d,%edi
mov    %ebp,%esi
mov    %rbx,%rdx
call   1090 <try_relaying@plt>
test   %eax,%eax
je     1242 <main+0xa2>
mov    %ebp,%edi
mov    %r14d,%esi
mov    %rbx,%rdx
call   1090 <try_relaying@plt>
test   %eax,%eax
jne    1220 <main+0x80>
mov    %r14d,%edi
call   1070 <close@plt>
mov    %ebp,%edi
call   1070 <close@plt>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [59 x i8] c"usage: %s <listen port> <connect hostname> <connect port>\0A\00"
@.str.1 = [18 x i8] c"got a connection\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 i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i32, i32* %4
%12 = icmp ne i32 %11, 4
br i1 %12, label %13, label %18
13: ; preds = %2
%14 = load i8**, i8*** %5
%15 = getelementptr inbounds i8*, i8** %14, i64 0
%16 = load i8*, i8** %15
%17 = call i32 @die(i8* getelementptr inbounds ([59 x i8], [59 x i8]* @.str, i64 0, i64 0), i8* %16)
br label %18
18: ; preds = %13, %2
%19 = load i8**, i8*** %5
%20 = getelementptr inbounds i8*, i8** %19, i64 1
%21 = load i8*, i8** %20
%22 = call i32 @my_atoi(i8* %21)
store i32 %22, i32* %6
%23 = load i32, i32* %6
%24 = call i32 @server_posix_listen(i32 %23)
store i32 %24, i32* %7
%25 = load i32, i32* %7
%26 = call i32 @server_posix_accept(i32 %25)
store i32 %26, i32* %8
%27 = call i32 @print(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
%28 = load i32, i32* %8
%29 = call i32 @make_nonblocking(i32 %28)
%30 = load i8**, i8*** %5
%31 = getelementptr inbounds i8*, i8** %30, i64 2
%32 = load i8*, i8** %31
%33 = load i8**, i8*** %5
%34 = getelementptr inbounds i8*, i8** %33, i64 3
%35 = load i8*, i8** %34
%36 = call i32 @my_atoi(i8* %35)
%37 = call i32 @client_posix_connect(i8* %32, i32 %36)
store i32 %37, i32* %9
%38 = load i32, i32* %9
%39 = call i32 @make_nonblocking(i32 %38)
%40 = call i8* @my_malloc(i32 4096)
store i8* %40, i8** %10
br label %41
41: ; preds = %18, %55
%42 = load i32, i32* %8
%43 = load i32, i32* %9
%44 = load i8*, i8** %10
%45 = call i32 @try_relaying(i32 %42, i32 %43, i8* %44)
%46 = icmp ne i32 %45, 0
br i1 %46, label %48, label %47
47: ; preds = %41
br label %56
48: ; preds = %41
%49 = load i32, i32* %9
%50 = load i32, i32* %8
%51 = load i8*, i8** %10
%52 = call i32 @try_relaying(i32 %49, i32 %50, i8* %51)
%53 = icmp ne i32 %52, 0
br i1 %53, label %55, label %54
54: ; preds = %48
br label %56
55: ; preds = %48
br label %41
56: ; preds = %54, %47
%57 = load i32, i32* %8
%58 = call i32 @close(i32 %57)
%59 = load i32, i32* %9
%60 = call i32 @close(i32 %59)
ret i32 0
}

LLVM IR (-O2):
@.str = [59 x i8] c"usage: %s <listen port> <connect hostname> <connect port>\0A\00"
@.str.1 = [18 x i8] c"got a connection\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 4
br i1 %3, label %7, label %4
4: ; preds = %2
%5 = load i8*, i8** %1
%6 = tail call i32 @die(i8* getelementptr inbounds ([59 x i8], [59 x i8]* @.str, i64 0, i64 0), i8* %5)
br label %7
7: ; preds = %4, %2
%8 = getelementptr inbounds i8*, i8** %1, i64 1
%9 = load i8*, i8** %8
%10 = tail call i32 @my_atoi(i8* %9)
%11 = tail call i32 @server_posix_listen(i32 %10)
%12 = tail call i32 @server_posix_accept(i32 %11)
%13 = tail call i32 @print(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
%14 = tail call i32 @make_nonblocking(i32 %12)
%15 = getelementptr inbounds i8*, i8** %1, i64 2
%16 = load i8*, i8** %15
%17 = getelementptr inbounds i8*, i8** %1, i64 3
%18 = load i8*, i8** %17
%19 = tail call i32 @my_atoi(i8* %18)
%20 = tail call i32 @client_posix_connect(i8* %16, i32 %19)
%21 = tail call i32 @make_nonblocking(i32 %20)
%22 = tail call i8* @my_malloc(i32 4096)
br label %23
23: ; preds = %26, %7
%24 = tail call i32 @try_relaying(i32 %12, i32 %20, i8* %22)
%25 = icmp eq i32 %24, 0
br i1 %25, label %29, label %26
26: ; preds = %23
%27 = tail call i32 @try_relaying(i32 %20, i32 %12, i8* %22)
%28 = icmp eq i32 %27, 0
br i1 %28, label %29, label %23
29: ; preds = %26, %23
%30 = tail call i32 @close(i32 %12)
%31 = tail call i32 @close(i32 %20)
ret i32 0
}