#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 */
typedef struct TYPE_2__ TYPE_1__;

/* Type definitions */
struct TYPE_2__ {
  int /*<<< orphan*/ s_addr;
};
struct sockaddr_in {
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ echoserver;
typedef int /*<<< orphan*/ echoclient;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ Die(char *);
int /*<<< orphan*/ HandleClient(int);
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ IPPROTO_TCP;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int accept(int, struct sockaddr *, unsigned int *);
int /*<<< orphan*/ atoi(char *);
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int /*<<< orphan*/);
char *inet_ntoa(TYPE_1__);
scalar_t__ listen(int, int);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
int main(int argc, char *argv[]) {
  int serversock, clientsock;
  struct sockaddr_in echoserver, echoclient;
  if (argc != 2) {
    fprintf(stderr, "USAGE: echoserver <port>\n");
    exit(1);
  }
  fprintf(stdout, "using zero copy\n");
  if ((serversock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {
    Die("Failed to create socket");
  }
  memset(&echoserver, 0, sizeof(echoserver));
  echoserver.sin_family = AF_INET;
  echoserver.sin_addr.s_addr = htonl(INADDR_ANY);
  echoserver.sin_port = htons(atoi(argv[1]));
  if (bind(serversock, (struct sockaddr *)&echoserver, sizeof(echoserver)) <
      0) {
    Die("Failed to bind the server socket");
  }
  if (listen(serversock, 5) < 0) {
    Die("Failed to listen on server socket");
  }
  while (1) {
    unsigned int clientlen = sizeof(echoclient);
    if ((clientsock = accept(serversock, (struct sockaddr *)&echoclient,
                             &clientlen)) < 0) {
      Die("Failed to accept client connection");
    }
    fprintf(stdout, "Client connected: %s\n", inet_ntoa(echoclient.sin_addr));
    HandleClient(clientsock);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     1211 <main+0x41>
mov    0x2dd1(%rip),%rax
mov    (%rax),%edi
lea    0xe00(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
mov    0x2d98(%rip),%rax
mov    (%rax),%edi
lea    0xdf9(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    0x2d89(%rip),%rax
mov    (%rax),%edi
mov    0x2d88(%rip),%rax
mov    (%rax),%esi
mov    0x2d97(%rip),%rax
mov    (%rax),%edx
call   10f0 <socket@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    1260 <main+0x90>
lea    0xdd0(%rip),%rdi
call   1090 <Die@plt>
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    0x2d71(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2d5d(%rip),%rax
mov    (%rax),%edi
call   1050 <htonl@plt>
mov    %eax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   10d0 <atoi@plt>
mov    %eax,%edi
call   1040 <htons@plt>
mov    %eax,-0x24(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0xc,%edx
call   10a0 <bind@plt>
cmp    $0x0,%rax
jge    12cb <main+0xfb>
lea    0xd7d(%rip),%rdi
call   1090 <Die@plt>
mov    -0x14(%rbp),%edi
mov    $0x5,%esi
call   1080 <listen@plt>
cmp    $0x0,%rax
jge    12ee <main+0x11e>
lea    0xd7b(%rip),%rdi
call   1090 <Die@plt>
jmp    12f3 <main+0x123>
movl   $0xc,-0x3c(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x38(%rbp),%rsi
lea    -0x3c(%rbp),%rdx
call   10c0 <accept@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jge    1322 <main+0x152>
lea    0xd69(%rip),%rdi
call   1090 <Die@plt>
mov    0x2c87(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x40(%rbp)
mov    -0x38(%rbp),%edi
call   1030 <inet_ntoa@plt>
mov    -0x40(%rbp),%edi
mov    %rax,%rdx
lea    0xd66(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    -0x18(%rbp),%edi
call   10b0 <HandleClient@plt>
jmp    12f3 <main+0x123>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
cmp    $0x2,%edi
jne    1324 <main+0x154>
mov    %rsi,%r14
mov    0x2dbf(%rip),%rax
mov    (%rax),%edi
lea    0xe20(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    0x2db0(%rip),%rax
mov    (%rax),%edi
mov    0x2daf(%rip),%rax
mov    (%rax),%esi
mov    0x2dbe(%rip),%rax
mov    (%rax),%edx
call   10f0 <socket@plt>
mov    %eax,%r15d
test   %eax,%eax
jns    1234 <main+0x64>
lea    0xdfc(%rip),%rdi
call   1090 <Die@plt>
lea    0x8(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    0x2d99(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
mov    0x2d84(%rip),%rax
mov    (%rax),%edi
call   1050 <htonl@plt>
mov    %eax,0x8(%rsp)
mov    0x8(%r14),%rdi
call   10d0 <atoi@plt>
mov    %eax,%edi
call   1040 <htons@plt>
mov    %eax,0xc(%rsp)
mov    %r15d,%edi
mov    %rbx,%rsi
mov    $0xc,%edx
call   10a0 <bind@plt>
test   %rax,%rax
jns    129c <main+0xcc>
lea    0xdac(%rip),%rdi
call   1090 <Die@plt>
mov    %r15d,%edi
mov    $0x5,%esi
call   1080 <listen@plt>
test   %rax,%rax
jns    12ba <main+0xea>
lea    0xdaf(%rip),%rdi
call   1090 <Die@plt>
lea    0x18(%rsp),%r12
lea    0x4(%rsp),%r13
lea    0xdde(%rip),%rbx
jmp    12fa <main+0x12a>
nopl   (%rax)
mov    0x2cd9(%rip),%rax
mov    (%rax),%r14d
mov    0x18(%rsp),%edi
call   1030 <inet_ntoa@plt>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    %ebp,%edi
call   10b0 <HandleClient@plt>
movl   $0xc,0x4(%rsp)
mov    %r15d,%edi
mov    %r12,%rsi
mov    %r13,%rdx
call   10c0 <accept@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    12d0 <main+0x100>
lea    0xd69(%rip),%rdi
call   1090 <Die@plt>
jmp    12d0 <main+0x100>
mov    0x2c9d(%rip),%rax
mov    (%rax),%edi
lea    0xccc(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@stderr = global i32 0
@.str = [26 x i8] c"USAGE: echoserver <port>\0A\00"
@stdout = global i32 0
@.str.1 = [17 x i8] c"using zero copy\0A\00"
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@.str.2 = [24 x i8] c"Failed to create socket\00"
@AF_INET = global i32 0
@INADDR_ANY = global i32 0
@.str.3 = [33 x i8] c"Failed to bind the server socket\00"
@.str.4 = [34 x i8] c"Failed to listen on server socket\00"
@.str.5 = [35 x i8] c"Failed to accept client connection\00"
@.str.6 = [22 x i8] c"Client connected: %s\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 %struct.sockaddr_in
%9 = alloca %struct.sockaddr_in
%10 = alloca i32
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, 2
br i1 %12, label %13, label %17
13: ; preds = %2
%14 = load i32, i32* @stderr
%15 = call i32 (i32, i8*, ...) @fprintf(i32 %14, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
%16 = call i32 @exit(i32 1)
unreachable
17: ; preds = %2
%18 = load i32, i32* @stdout
%19 = call i32 (i32, i8*, ...) @fprintf(i32 %18, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
%20 = load i32, i32* @PF_INET
%21 = load i32, i32* @SOCK_STREAM
%22 = load i32, i32* @IPPROTO_TCP
%23 = call i32 @socket(i32 %20, i32 %21, i32 %22)
store i32 %23, i32* %6
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %17
%26 = call i32 @Die(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
br label %27
27: ; preds = %25, %17
%28 = call i32 @memset(%struct.sockaddr_in* %8, i32 0, i32 12)
%29 = load i32, i32* @AF_INET
%30 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 2
store i32 %29, i32* %30
%31 = load i32, i32* @INADDR_ANY
%32 = call i32 @htonl(i32 %31)
%33 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 0
%34 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %33, i32 0, i32 0
store i32 %32, i32* %34
%35 = load i8**, i8*** %5
%36 = getelementptr inbounds i8*, i8** %35, i64 1
%37 = load i8*, i8** %36
%38 = call i32 @atoi(i8* %37)
%39 = call i32 @htons(i32 %38)
%40 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 1
store i32 %39, i32* %40
%41 = load i32, i32* %6
%42 = bitcast %struct.sockaddr_in* %8 to %struct.sockaddr*
%43 = call i64 @bind(i32 %41, %struct.sockaddr* %42, i32 12)
%44 = icmp slt i64 %43, 0
br i1 %44, label %45, label %47
45: ; preds = %27
%46 = call i32 @Die(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0))
br label %47
47: ; preds = %45, %27
%48 = load i32, i32* %6
%49 = call i64 @listen(i32 %48, i32 5)
%50 = icmp slt i64 %49, 0
br i1 %50, label %51, label %53
51: ; preds = %47
%52 = call i32 @Die(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.4, i64 0, i64 0))
br label %53
53: ; preds = %51, %47
br label %54
54: ; preds = %53, %61
store i32 12, i32* %10
%55 = load i32, i32* %6
%56 = bitcast %struct.sockaddr_in* %9 to %struct.sockaddr*
%57 = call i32 @accept(i32 %55, %struct.sockaddr* %56, i32* %10)
store i32 %57, i32* %7
%58 = icmp slt i32 %57, 0
br i1 %58, label %59, label %61
59: ; preds = %54
%60 = call i32 @Die(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.5, i64 0, i64 0))
br label %61
61: ; preds = %59, %54
%62 = load i32, i32* @stdout
%63 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 0
%64 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %63, i32 0, i32 0
%65 = load i32, i32* %64
%66 = call i8* @inet_ntoa(i32 %65)
%67 = call i32 (i32, i8*, ...) @fprintf(i32 %62, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.6, i64 0, i64 0), i8* %66)
%68 = load i32, i32* %7
%69 = call i32 @HandleClient(i32 %68)
br label %54
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@stderr = global i32 0
@.str = [26 x i8] c"USAGE: echoserver <port>\0A\00"
@stdout = global i32 0
@.str.1 = [17 x i8] c"using zero copy\0A\00"
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@.str.2 = [24 x i8] c"Failed to create socket\00"
@AF_INET = global i32 0
@INADDR_ANY = global i32 0
@.str.3 = [33 x i8] c"Failed to bind the server socket\00"
@.str.4 = [34 x i8] c"Failed to listen on server socket\00"
@.str.5 = [35 x i8] c"Failed to accept client connection\00"
@.str.6 = [22 x i8] c"Client connected: %s\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca %struct.sockaddr_in
%5 = alloca i32
%6 = bitcast %struct.sockaddr_in* %3 to i8*
%7 = bitcast %struct.sockaddr_in* %4 to i8*
%8 = icmp eq i32 %0, 2
br i1 %8, label %13, label %9
9: ; preds = %2
%10 = load i32, i32* @stderr
%11 = tail call i32 (i32, i8*, ...) @fprintf(i32 %10, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
%12 = tail call i32 @exit(i32 1)
unreachable
13: ; preds = %2
%14 = load i32, i32* @stdout
%15 = tail call i32 (i32, i8*, ...) @fprintf(i32 %14, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
%16 = load i32, i32* @PF_INET
%17 = load i32, i32* @SOCK_STREAM
%18 = load i32, i32* @IPPROTO_TCP
%19 = tail call i32 @socket(i32 %16, i32 %17, i32 %18)
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %13
%22 = tail call i32 @Die(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
br label %23
23: ; preds = %21, %13
%24 = call i32 @memset(%struct.sockaddr_in* %3, i32 0, i32 12)
%25 = load i32, i32* @AF_INET
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %25, i32* %26
%27 = load i32, i32* @INADDR_ANY
%28 = call i32 @htonl(i32 %27)
%29 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0, i32 0
store i32 %28, i32* %29
%30 = getelementptr inbounds i8*, i8** %1, i64 1
%31 = load i8*, i8** %30
%32 = call i32 @atoi(i8* %31)
%33 = call i32 @htons(i32 %32)
%34 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i32 %33, i32* %34
%35 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%36 = call i64 @bind(i32 %19, %struct.sockaddr* %35, i32 12)
%37 = icmp slt i64 %36, 0
br i1 %37, label %38, label %40
38: ; preds = %23
%39 = call i32 @Die(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0))
br label %40
40: ; preds = %38, %23
%41 = call i64 @listen(i32 %19, i32 5)
%42 = icmp slt i64 %41, 0
br i1 %42, label %43, label %45
43: ; preds = %40
%44 = call i32 @Die(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.4, i64 0, i64 0))
br label %45
45: ; preds = %43, %40
%46 = bitcast i32* %5 to i8*
%47 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%48 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0, i32 0
br label %49
49: ; preds = %54, %45
store i32 12, i32* %5
%50 = call i32 @accept(i32 %19, %struct.sockaddr* %47, i32* %5)
%51 = icmp slt i32 %50, 0
br i1 %51, label %52, label %54
52: ; preds = %49
%53 = call i32 @Die(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.5, i64 0, i64 0))
br label %54
54: ; preds = %52, %49
%55 = load i32, i32* @stdout
%56 = load i32, i32* %48
%57 = call i8* @inet_ntoa(i32 %56)
%58 = call i32 (i32, i8*, ...) @fprintf(i32 %55, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.6, i64 0, i64 0), i8* %57)
%59 = call i32 @HandleClient(i32 %50)
br label %49
}