#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 {
  int /*<<< orphan*/ sin_port;
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int socklen_t;
typedef int /*<<< orphan*/ servAddr;
typedef int /*<<< orphan*/ clntAddr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int INET_ADDRSTRLEN;
int /*<<< orphan*/ IPPROTO_TCP;
int /*<<< orphan*/ SOCK_STREAM;
int accept(int, struct sockaddr *, int *);
int atoi(char *);
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ echoService(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, int);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ *inet_ntop(int /*<<< orphan*/, int /*<<< orphan*/ *, char *,
                              int);
scalar_t__ listen(int, int);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int ntohs(int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stdout;
int /*<<< orphan*/ sysError(char *);
int /*<<< orphan*/ usrError(char *);
int main(int argc, char **argv) {
  if (argc != 2)
    usrError("Usage:\tServer <Port>");
  int servPort = atoi(argv[1]);
  int servSock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  if (servSock < 0)
    sysError("failed to create socket");
  struct sockaddr_in servAddr;
  memset(&servAddr, 0, sizeof(servAddr));
  servAddr.sin_family = AF_INET;
  servAddr.sin_addr.s_addr = htonl(INADDR_ANY);
  servAddr.sin_port = htons(servPort);
  if (bind(servSock, (struct sockaddr *)&servAddr, sizeof(servAddr)) < 0)
    sysError("failed to connect to socket");
  if (listen(servSock, 5) < 0)
    sysError("failed to listen to socket stream");
  for (;;) {
    struct sockaddr_in clntAddr;
    memset(&clntAddr, 0, sizeof(clntAddr));
    socklen_t clntAddrLen = sizeof(clntAddr);
    int clntSock = accept(servSock, (struct sockaddr *)&clntAddr, &clntAddrLen);
    if (clntSock < 0)
      sysError("failed to accept incoming connection");
    char clntIP[INET_ADDRSTRLEN];
    if (inet_ntop(AF_INET, &clntAddr.sin_addr.s_addr, clntIP, clntAddrLen) !=
        NULL)
      fprintf(stdout, "\nincoming connection from %s:%d\n", clntIP,
              ntohs(clntAddr.sin_port));
    else
      sysError("failed to retrieve client IP address");
    echoService(clntSock);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     120c <main+0x2c>
lea    0xdf9(%rip),%rdi
call   1050 <usrError@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   10f0 <atoi@plt>
mov    %eax,-0x14(%rbp)
mov    0x2dc5(%rip),%rax
mov    (%rax),%edi
mov    0x2d9c(%rip),%rax
mov    (%rax),%esi
mov    0x2da3(%rip),%rax
mov    (%rax),%edx
call   1100 <socket@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    1255 <main+0x75>
lea    0xdc5(%rip),%rdi
call   1040 <sysError@plt>
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1080 <memset@plt>
mov    0x2d7c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2d68(%rip),%rax
mov    (%rax),%edi
call   1070 <htonl@plt>
mov    %eax,-0x24(%rbp)
mov    -0x14(%rbp),%edi
call   1060 <htons@plt>
mov    %eax,-0x28(%rbp)
mov    -0x18(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0xc,%edx
call   10d0 <bind@plt>
cmp    $0x0,%rax
jge    12b4 <main+0xd4>
lea    0xd7e(%rip),%rdi
call   1040 <sysError@plt>
mov    -0x18(%rbp),%edi
mov    $0x5,%esi
call   10b0 <listen@plt>
cmp    $0x0,%rax
jge    12d7 <main+0xf7>
lea    0xd77(%rip),%rdi
call   1040 <sysError@plt>
jmp    12dc <main+0xfc>
lea    -0x38(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1080 <memset@plt>
movl   $0xc,-0x3c(%rbp)
mov    -0x18(%rbp),%edi
lea    -0x38(%rbp),%rsi
lea    -0x3c(%rbp),%rdx
call   10e0 <accept@plt>
mov    %eax,-0x40(%rbp)
cmpl   $0x0,-0x40(%rbp)
jge    131c <main+0x13c>
lea    0xd54(%rip),%rdi
call   1040 <sysError@plt>
mov    0x2c9d(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x48(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdx
sub    %rcx,%rdx
mov    %rdx,-0x58(%rbp)
mov    %rdx,%rsp
mov    %rax,-0x50(%rbp)
mov    0x2c99(%rip),%rax
mov    (%rax),%edi
lea    -0x38(%rbp),%rsi
add    $0x4,%rsi
mov    -0x3c(%rbp),%ecx
call   1030 <inet_ntop@plt>
cmp    $0x0,%rax
je     1399 <main+0x1b9>
mov    0x2c46(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x5c(%rbp)
mov    -0x38(%rbp),%edi
call   10c0 <ntohs@plt>
mov    -0x5c(%rbp),%edi
mov    -0x58(%rbp),%rdx
mov    %eax,%ecx
lea    0xd01(%rip),%rsi
call   10a0 <fprintf@plt>
jmp    13a5 <main+0x1c5>
lea    0xd11(%rip),%rdi
call   1040 <sysError@plt>
mov    -0x40(%rbp),%edi
call   1090 <echoService@plt>
mov    -0x48(%rbp),%rax
mov    %rax,%rsp
jmp    12dc <main+0xfc>

x86 assembly (clang, -O2):
<main>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rsi,%rbx
cmp    $0x2,%edi
je     1205 <main+0x25>
lea    0xe00(%rip),%rdi
call   1050 <usrError@plt>
mov    0x8(%rbx),%rdi
call   10f0 <atoi@plt>
mov    %eax,%r14d
mov    0x2dd0(%rip),%rbx
mov    (%rbx),%edi
mov    0x2da7(%rip),%rax
mov    (%rax),%esi
mov    0x2dae(%rip),%rax
mov    (%rax),%edx
call   1100 <socket@plt>
mov    %eax,%r12d
test   %eax,%eax
jns    1244 <main+0x64>
lea    0xdd6(%rip),%rdi
call   1040 <sysError@plt>
lea    -0x40(%rbp),%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1080 <memset@plt>
mov    (%rbx),%eax
mov    %eax,-0x38(%rbp)
mov    0x2d7d(%rip),%rax
mov    (%rax),%edi
call   1070 <htonl@plt>
mov    %eax,-0x3c(%rbp)
mov    %r14d,%edi
call   1060 <htons@plt>
mov    %eax,-0x40(%rbp)
mov    %r12d,%edi
mov    %r15,%rsi
mov    $0xc,%edx
call   10d0 <bind@plt>
test   %rax,%rax
jns    1299 <main+0xb9>
lea    0xd99(%rip),%rdi
call   1040 <sysError@plt>
mov    %r12d,%edi
mov    $0x5,%esi
call   10b0 <listen@plt>
test   %rax,%rax
jns    12d7 <main+0xf7>
lea    0xd97(%rip),%rdi
call   1040 <sysError@plt>
jmp    12d7 <main+0xf7>
nopl   0x0(%rax)
lea    0xdea(%rip),%rdi
call   1040 <sysError@plt>
mov    %r13d,%edi
call   1090 <echoService@plt>
mov    %r15,%rsp
lea    -0x50(%rbp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1080 <memset@plt>
movl   $0xc,-0x2c(%rbp)
mov    %r12d,%edi
mov    %r14,%rsi
lea    -0x2c(%rbp),%rdx
call   10e0 <accept@plt>
mov    %eax,%r13d
test   %eax,%eax
jns    1313 <main+0x133>
lea    0xd5d(%rip),%rdi
call   1040 <sysError@plt>
mov    %rsp,%r15
mov    0x2ca3(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%r14
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r14
mov    %r14,%rsp
mov    (%rbx),%edi
mov    -0x2c(%rbp),%ecx
lea    -0x4c(%rbp),%rsi
mov    %r14,%rdx
call   1030 <inet_ntop@plt>
test   %rax,%rax
je     12c0 <main+0xe0>
mov    %r13d,-0x30(%rbp)
mov    %r15,%r13
mov    %r12d,%r15d
mov    0x2c5d(%rip),%rax
mov    (%rax),%r12d
mov    -0x50(%rbp),%edi
call   10c0 <ntohs@plt>
mov    %r12d,%edi
mov    %r15d,%r12d
mov    %r13,%r15
mov    -0x30(%rbp),%r13d
lea    0xd16(%rip),%rsi
mov    %r14,%rdx
mov    %eax,%ecx
call   10a0 <fprintf@plt>
jmp    12cc <main+0xec>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [21 x i8] c"Usage:\09Server <Port>\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@.str.1 = [24 x i8] c"failed to create socket\00"
@INADDR_ANY = global i32 0
@.str.2 = [28 x i8] c"failed to connect to socket\00"
@.str.3 = [34 x i8] c"failed to listen to socket stream\00"
@.str.4 = [37 x i8] c"failed to accept incoming connection\00"
@INET_ADDRSTRLEN = global i32 0
@stdout = global i32 0
@.str.5 = [33 x i8] c"\0Aincoming connection from %s:%d\0A\00"
@.str.6 = [37 x i8] c"failed to retrieve client IP address\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
%11 = alloca i32
%12 = alloca i8*
%13 = alloca i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%14 = load i32, i32* %4
%15 = icmp ne i32 %14, 2
br i1 %15, label %16, label %18
16: ; preds = %2
%17 = call i32 @usrError(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
br label %18
18: ; preds = %16, %2
%19 = load i8**, i8*** %5
%20 = getelementptr inbounds i8*, i8** %19, i64 1
%21 = load i8*, i8** %20
%22 = call i32 @atoi(i8* %21)
store i32 %22, i32* %6
%23 = load i32, i32* @AF_INET
%24 = load i32, i32* @SOCK_STREAM
%25 = load i32, i32* @IPPROTO_TCP
%26 = call i32 @socket(i32 %23, i32 %24, i32 %25)
store i32 %26, i32* %7
%27 = load i32, i32* %7
%28 = icmp slt i32 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %18
%30 = call i32 @sysError(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
br label %31
31: ; preds = %29, %18
%32 = call i32 @memset(%struct.sockaddr_in* %8, i32 0, i32 12)
%33 = load i32, i32* @AF_INET
%34 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 2
store i32 %33, i32* %34
%35 = load i32, i32* @INADDR_ANY
%36 = call i32 @htonl(i32 %35)
%37 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 1
%38 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %37, i32 0, i32 0
store i32 %36, i32* %38
%39 = load i32, i32* %6
%40 = call i32 @htons(i32 %39)
%41 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 0
store i32 %40, i32* %41
%42 = load i32, i32* %7
%43 = bitcast %struct.sockaddr_in* %8 to %struct.sockaddr*
%44 = call i64 @bind(i32 %42, %struct.sockaddr* %43, i32 12)
%45 = icmp slt i64 %44, 0
br i1 %45, label %46, label %48
46: ; preds = %31
%47 = call i32 @sysError(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0))
br label %48
48: ; preds = %46, %31
%49 = load i32, i32* %7
%50 = call i64 @listen(i32 %49, i32 5)
%51 = icmp slt i64 %50, 0
br i1 %51, label %52, label %54
52: ; preds = %48
%53 = call i32 @sysError(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.3, i64 0, i64 0))
br label %54
54: ; preds = %52, %48
br label %55
55: ; preds = %83, %54
%56 = call i32 @memset(%struct.sockaddr_in* %9, i32 0, i32 12)
store i32 12, i32* %10
%57 = load i32, i32* %7
%58 = bitcast %struct.sockaddr_in* %9 to %struct.sockaddr*
%59 = call i32 @accept(i32 %57, %struct.sockaddr* %58, i32* %10)
store i32 %59, i32* %11
%60 = load i32, i32* %11
%61 = icmp slt i32 %60, 0
br i1 %61, label %62, label %64
62: ; preds = %55
%63 = call i32 @sysError(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.4, i64 0, i64 0))
br label %64
64: ; preds = %62, %55
%65 = load i32, i32* @INET_ADDRSTRLEN
%66 = zext i32 %65 to i64
%67 = call i8* @llvm.stacksave()
store i8* %67, i8** %12
%68 = alloca i8, i64 %66
store i64 %66, i64* %13
%69 = load i32, i32* @AF_INET
%70 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 1
%71 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %70, i32 0, i32 0
%72 = load i32, i32* %10
%73 = call i32* @inet_ntop(i32 %69, i32* %71, i8* %68, i32 %72)
%74 = icmp ne i32* %73, null
br i1 %74, label %75, label %81
75: ; preds = %64
%76 = load i32, i32* @stdout
%77 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 0
%78 = load i32, i32* %77
%79 = call i32 @ntohs(i32 %78)
%80 = call i32 @fprintf(i32 %76, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.5, i64 0, i64 0), i8* %68, i32 %79)
br label %83
81: ; preds = %64
%82 = call i32 @sysError(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.6, i64 0, i64 0))
br label %83
83: ; preds = %81, %75
%84 = load i32, i32* %11
%85 = call i32 @echoService(i32 %84)
%86 = load i8*, i8** %12
br label %55
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [21 x i8] c"Usage:\09Server <Port>\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@.str.1 = [24 x i8] c"failed to create socket\00"
@INADDR_ANY = global i32 0
@.str.2 = [28 x i8] c"failed to connect to socket\00"
@.str.3 = [34 x i8] c"failed to listen to socket stream\00"
@.str.4 = [37 x i8] c"failed to accept incoming connection\00"
@INET_ADDRSTRLEN = global i32 0
@stdout = global i32 0
@.str.5 = [33 x i8] c"\0Aincoming connection from %s:%d\0A\00"
@.str.6 = [37 x i8] c"failed to retrieve client IP address\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca %struct.sockaddr_in
%5 = alloca i32
%6 = icmp eq i32 %0, 2
br i1 %6, label %9, label %7
7: ; preds = %2
%8 = tail call i32 @usrError(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
br label %9
9: ; preds = %7, %2
%10 = getelementptr inbounds i8*, i8** %1, i64 1
%11 = load i8*, i8** %10
%12 = tail call i32 @atoi(i8* %11)
%13 = load i32, i32* @AF_INET
%14 = load i32, i32* @SOCK_STREAM
%15 = load i32, i32* @IPPROTO_TCP
%16 = tail call i32 @socket(i32 %13, i32 %14, i32 %15)
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %9
%19 = tail call i32 @sysError(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
br label %20
20: ; preds = %18, %9
%21 = bitcast %struct.sockaddr_in* %3 to i8*
%22 = call i32 @memset(%struct.sockaddr_in* %3, i32 0, i32 12)
%23 = load i32, i32* @AF_INET
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %23, i32* %24
%25 = load i32, i32* @INADDR_ANY
%26 = call i32 @htonl(i32 %25)
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1, i32 0
store i32 %26, i32* %27
%28 = call i32 @htons(i32 %12)
%29 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %28, i32* %29
%30 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%31 = call i64 @bind(i32 %16, %struct.sockaddr* %30, i32 12)
%32 = icmp slt i64 %31, 0
br i1 %32, label %33, label %35
33: ; preds = %20
%34 = call i32 @sysError(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0))
br label %35
35: ; preds = %33, %20
%36 = call i64 @listen(i32 %16, i32 5)
%37 = icmp slt i64 %36, 0
br i1 %37, label %38, label %40
38: ; preds = %35
%39 = call i32 @sysError(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.3, i64 0, i64 0))
br label %40
40: ; preds = %38, %35
%41 = bitcast %struct.sockaddr_in* %4 to i8*
%42 = bitcast i32* %5 to i8*
%43 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%44 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1, i32 0
%45 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0
br label %46
46: ; preds = %68, %40
%47 = call i32 @memset(%struct.sockaddr_in* %4, i32 0, i32 12)
store i32 12, i32* %5
%48 = call i32 @accept(i32 %16, %struct.sockaddr* %43, i32* %5)
%49 = icmp slt i32 %48, 0
br i1 %49, label %50, label %52
50: ; preds = %46
%51 = call i32 @sysError(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.4, i64 0, i64 0))
br label %52
52: ; preds = %50, %46
%53 = load i32, i32* @INET_ADDRSTRLEN
%54 = zext i32 %53 to i64
%55 = call i8* @llvm.stacksave()
%56 = alloca i8, i64 %54
%57 = load i32, i32* @AF_INET
%58 = load i32, i32* %5
%59 = call i32* @inet_ntop(i32 %57, i32* %44, i8* %56, i32 %58)
%60 = icmp eq i32* %59, null
br i1 %60, label %66, label %61
61: ; preds = %52
%62 = load i32, i32* @stdout
%63 = load i32, i32* %45
%64 = call i32 @ntohs(i32 %63)
%65 = call i32 @fprintf(i32 %62, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.5, i64 0, i64 0), i8* %56, i32 %64)
br label %68
66: ; preds = %52
%67 = call i32 @sysError(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.6, i64 0, i64 0))
br label %68
68: ; preds = %66, %61
%69 = call i32 @echoService(i32 %48)
br label %46
}