#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 */
struct sockaddr_in6 {
  int /*<<< orphan*/ sin6_port;
  int /*<<< orphan*/ sin6_addr;
  int /*<<< orphan*/ sin6_family;
};
struct sockaddr {
  int dummy;
};
typedef int socklen_t;
typedef int /*<<< orphan*/ servAddr;
typedef int /*<<< orphan*/ in_port_t;
typedef int /*<<< orphan*/ clntAddr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET6;
int /*<<< orphan*/ IPPROTO_TCP;
int /*<<< orphan*/ SOCK_STREAM;
int accept(int, struct sockaddr *, int *);
int /*<<< orphan*/ atoi(char *);
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ getSockName(int);
int /*<<< orphan*/ htons(int /*<<< orphan*/);
int /*<<< orphan*/ in6addr_any;
scalar_t__ listen(int, int);
int /*<<< orphan*/ memset(struct sockaddr_in6 *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printSocketAddress(struct sockaddr_in6);
int /*<<< orphan*/ serverStream(int);
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>");
  in_port_t port = atoi(argv[1]);
  int servSock = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP);
  if (servSock < 0)
    sysError("failed to create socket");
  struct sockaddr_in6 servAddr;
  memset(&servAddr, 0, sizeof(servAddr));
  servAddr.sin6_family = AF_INET6;
  servAddr.sin6_addr = in6addr_any;
  servAddr.sin6_port = htons(port);
  if (bind(servSock, (struct sockaddr *)&servAddr, sizeof(servAddr)) < 0)
    sysError("failed to bind to port");
  if (listen(servSock, 5) < 0)
    sysError("failed to listen to socket");
  getSockName(servSock);
  for (;;) {
    struct sockaddr_in6 clntAddr;
    socklen_t clntSockLen = sizeof(clntAddr);
    memset(&clntAddr, 0, clntSockLen);
    int clntSock = accept(servSock, (struct sockaddr *)&clntAddr, &clntSockLen);
    if (clntSock < 0)
      sysError("failed to get client address");
    fprintf(stdout, "\nClient ");
    printSocketAddress(clntAddr);
    serverStream(clntSock);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11fc <main+0x2c>
lea    0xe09(%rip),%rdi
call   1040 <usrError@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   10c0 <atoi@plt>
mov    %eax,-0x14(%rbp)
mov    0x2dc5(%rip),%rax
mov    (%rax),%edi
mov    0x2dac(%rip),%rax
mov    (%rax),%esi
mov    0x2dbb(%rip),%rax
mov    (%rax),%edx
call   10f0 <socket@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    1245 <main+0x75>
lea    0xdd5(%rip),%rdi
call   1030 <sysError@plt>
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    0x2d7c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2d88(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x14(%rbp),%edi
call   1050 <htons@plt>
mov    %eax,-0x28(%rbp)
mov    -0x18(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0xc,%edx
call   10a0 <bind@plt>
cmp    $0x0,%rax
jge    129f <main+0xcf>
lea    0xd93(%rip),%rdi
call   1030 <sysError@plt>
mov    -0x18(%rbp),%edi
mov    $0x5,%esi
call   1090 <listen@plt>
cmp    $0x0,%rax
jge    12c2 <main+0xf2>
lea    0xd87(%rip),%rdi
call   1030 <sysError@plt>
mov    -0x18(%rbp),%edi
call   1080 <getSockName@plt>
movl   $0xc,-0x3c(%rbp)
mov    -0x3c(%rbp),%edx
lea    -0x38(%rbp),%rdi
xor    %esi,%esi
call   1060 <memset@plt>
mov    -0x18(%rbp),%edi
lea    -0x38(%rbp),%rsi
lea    -0x3c(%rbp),%rdx
call   10b0 <accept@plt>
mov    %eax,-0x40(%rbp)
cmpl   $0x0,-0x40(%rbp)
jge    1308 <main+0x138>
lea    0xd5c(%rip),%rdi
call   1030 <sysError@plt>
mov    0x2cb1(%rip),%rax
mov    (%rax),%edi
lea    0xd64(%rip),%rsi
call   1070 <fprintf@plt>
mov    -0x38(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x30(%rbp),%eax
mov    %eax,-0x48(%rbp)
mov    -0x50(%rbp),%rdi
mov    -0x48(%rbp),%esi
call   10e0 <printSocketAddress@plt>
mov    -0x40(%rbp),%edi
call   10d0 <serverStream@plt>
jmp    12ca <main+0xfa>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rsi,%rbx
cmp    $0x2,%edi
je     11f2 <main+0x22>
lea    0xe13(%rip),%rdi
call   1040 <usrError@plt>
mov    0x8(%rbx),%rdi
call   10c0 <atoi@plt>
mov    %eax,%ebx
mov    0x2dd4(%rip),%r15
mov    (%r15),%edi
mov    0x2dba(%rip),%rax
mov    (%rax),%esi
mov    0x2dc9(%rip),%rax
mov    (%rax),%edx
call   10f0 <socket@plt>
mov    %eax,%r14d
test   %eax,%eax
jns    1231 <main+0x61>
lea    0xde9(%rip),%rdi
call   1030 <sysError@plt>
lea    0x8(%rsp),%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    (%r15),%eax
mov    %eax,0x10(%rsp)
mov    0x2d9d(%rip),%rax
mov    (%rax),%eax
mov    %eax,0xc(%rsp)
mov    %ebx,%edi
call   1050 <htons@plt>
mov    %eax,0x8(%rsp)
mov    %r14d,%edi
mov    %rbp,%rsi
mov    $0xc,%edx
call   10a0 <bind@plt>
test   %rax,%rax
jns    1285 <main+0xb5>
lea    0xdad(%rip),%rdi
call   1030 <sysError@plt>
mov    %r14d,%edi
mov    $0x5,%esi
call   1090 <listen@plt>
test   %rax,%rax
jns    12a3 <main+0xd3>
lea    0xda6(%rip),%rdi
call   1030 <sysError@plt>
mov    %r14d,%edi
call   1080 <getSockName@plt>
lea    0x18(%rsp),%rbx
lea    0x4(%rsp),%r12
mov    0x2d04(%rip),%r15
lea    0xdb9(%rip),%r13
jmp    12f0 <main+0x120>
cs nopw 0x0(%rax,%rax,1)
nop
mov    (%r15),%edi
mov    %r13,%rsi
call   1070 <fprintf@plt>
mov    0x18(%rsp),%rdi
mov    0x20(%rsp),%esi
call   10e0 <printSocketAddress@plt>
mov    %ebp,%edi
call   10d0 <serverStream@plt>
movl   $0xc,0x4(%rsp)
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %r12,%rdx
call   10b0 <accept@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    12d0 <main+0x100>
lea    0xd3d(%rip),%rdi
call   1030 <sysError@plt>
jmp    12d0 <main+0x100>

LLVM IR (-O0):
%struct.sockaddr_in6 = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@.str = [21 x i8] c"Usage:\09Server <port>\00"
@AF_INET6 = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@.str.1 = [24 x i8] c"failed to create socket\00"
@in6addr_any = global i32 0
@.str.2 = [23 x i8] c"failed to bind to port\00"
@.str.3 = [27 x i8] c"failed to listen to socket\00"
@.str.4 = [29 x i8] c"failed to get client address\00"
@stdout = global i32 0
@.str.5 = [9 x i8] c"\0AClient \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_in6
%9 = alloca %struct.sockaddr_in6
%10 = alloca i32
%11 = alloca i32
%12 = alloca { i64, i32 }
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%13 = load i32, i32* %4
%14 = icmp ne i32 %13, 2
br i1 %14, label %15, label %17
15: ; preds = %2
%16 = call i32 @usrError(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
br label %17
17: ; preds = %15, %2
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 1
%20 = load i8*, i8** %19
%21 = call i32 @atoi(i8* %20)
store i32 %21, i32* %6
%22 = load i32, i32* @AF_INET6
%23 = load i32, i32* @SOCK_STREAM
%24 = load i32, i32* @IPPROTO_TCP
%25 = call i32 @socket(i32 %22, i32 %23, i32 %24)
store i32 %25, i32* %7
%26 = load i32, i32* %7
%27 = icmp slt i32 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %17
%29 = call i32 @sysError(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
br label %30
30: ; preds = %28, %17
%31 = call i32 @memset(%struct.sockaddr_in6* %8, i32 0, i32 12)
%32 = load i32, i32* @AF_INET6
%33 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %8, i32 0, i32 2
store i32 %32, i32* %33
%34 = load i32, i32* @in6addr_any
%35 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %8, i32 0, i32 1
store i32 %34, i32* %35
%36 = load i32, i32* %6
%37 = call i32 @htons(i32 %36)
%38 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %8, i32 0, i32 0
store i32 %37, i32* %38
%39 = load i32, i32* %7
%40 = bitcast %struct.sockaddr_in6* %8 to %struct.sockaddr*
%41 = call i64 @bind(i32 %39, %struct.sockaddr* %40, i32 12)
%42 = icmp slt i64 %41, 0
br i1 %42, label %43, label %45
43: ; preds = %30
%44 = call i32 @sysError(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
br label %45
45: ; preds = %43, %30
%46 = load i32, i32* %7
%47 = call i64 @listen(i32 %46, i32 5)
%48 = icmp slt i64 %47, 0
br i1 %48, label %49, label %51
49: ; preds = %45
%50 = call i32 @sysError(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0))
br label %51
51: ; preds = %49, %45
%52 = load i32, i32* %7
%53 = call i32 @getSockName(i32 %52)
br label %54
54: ; preds = %64, %51
store i32 12, i32* %10
%55 = load i32, i32* %10
%56 = call i32 @memset(%struct.sockaddr_in6* %9, i32 0, i32 %55)
%57 = load i32, i32* %7
%58 = bitcast %struct.sockaddr_in6* %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 = %54
%63 = call i32 @sysError(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0))
br label %64
64: ; preds = %62, %54
%65 = load i32, i32* @stdout
%66 = call i32 @fprintf(i32 %65, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.5, i64 0, i64 0))
%67 = bitcast { i64, i32 }* %12 to i8*
%68 = bitcast %struct.sockaddr_in6* %9 to i8*
%69 = getelementptr inbounds { i64, i32 }, { i64, i32 }* %12, i32 0, i32 0
%70 = load i64, i64* %69
%71 = getelementptr inbounds { i64, i32 }, { i64, i32 }* %12, i32 0, i32 1
%72 = load i32, i32* %71
%73 = call i32 @printSocketAddress(i64 %70, i32 %72)
%74 = load i32, i32* %11
%75 = call i32 @serverStream(i32 %74)
br label %54
}

LLVM IR (-O2):
%struct.sockaddr_in6 = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@.str = [21 x i8] c"Usage:\09Server <port>\00"
@AF_INET6 = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@.str.1 = [24 x i8] c"failed to create socket\00"
@in6addr_any = global i32 0
@.str.2 = [23 x i8] c"failed to bind to port\00"
@.str.3 = [27 x i8] c"failed to listen to socket\00"
@.str.4 = [29 x i8] c"failed to get client address\00"
@stdout = global i32 0
@.str.5 = [9 x i8] c"\0AClient \00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.sockaddr_in6
%4 = alloca %struct.sockaddr_in6
%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_INET6
%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_in6* %3 to i8*
%22 = call i32 @memset(%struct.sockaddr_in6* %3, i32 0, i32 12)
%23 = load i32, i32* @AF_INET6
%24 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %3, i64 0, i32 2
store i32 %23, i32* %24
%25 = load i32, i32* @in6addr_any
%26 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %3, i64 0, i32 1
store i32 %25, i32* %26
%27 = call i32 @htons(i32 %12)
%28 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %3, i64 0, i32 0
store i32 %27, i32* %28
%29 = bitcast %struct.sockaddr_in6* %3 to %struct.sockaddr*
%30 = call i64 @bind(i32 %16, %struct.sockaddr* %29, i32 12)
%31 = icmp slt i64 %30, 0
br i1 %31, label %32, label %34
32: ; preds = %20
%33 = call i32 @sysError(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
br label %34
34: ; preds = %32, %20
%35 = call i64 @listen(i32 %16, i32 5)
%36 = icmp slt i64 %35, 0
br i1 %36, label %37, label %39
37: ; preds = %34
%38 = call i32 @sysError(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0))
br label %39
39: ; preds = %37, %34
%40 = call i32 @getSockName(i32 %16)
%41 = bitcast %struct.sockaddr_in6* %4 to i8*
%42 = bitcast i32* %5 to i8*
%43 = bitcast %struct.sockaddr_in6* %4 to %struct.sockaddr*
%44 = bitcast %struct.sockaddr_in6* %4 to i64*
%45 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %4, i64 0, i32 2
br label %46
46: ; preds = %52, %39
store i32 12, i32* %5
%47 = call i32 @memset(%struct.sockaddr_in6* %4, i32 0, i32 12)
%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 ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0))
br label %52
52: ; preds = %50, %46
%53 = load i32, i32* @stdout
%54 = call i32 @fprintf(i32 %53, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.5, i64 0, i64 0))
%55 = load i64, i64* %44
%56 = load i32, i32* %45
%57 = call i32 @printSocketAddress(i64 %55, i32 %56)
%58 = call i32 @serverStream(i32 %48)
br label %46
}