#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*/ server_addr;
typedef int /*<<< orphan*/ client_addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ SOCK_STREAM;
int accept(int, struct sockaddr *, int *);
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ bzero(char *, int);
int /*<<< orphan*/ error(char *);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ listen(int, int);
int /*<<< orphan*/ printf(char *, char *, ...);
int read(int, char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
scalar_t__ strcmp(char *, char *);
int /*<<< orphan*/ strtok(char *, char *);
void server(int port) {
  int sockfd, newsockfd;
  int client_length;
  int n;
  char buffer[256];
  struct sockaddr_in server_addr, client_addr;
  sockfd = socket(AF_INET, SOCK_STREAM, 0);
  if (sockfd < 0) {
    error("ERROR opening socket");
  }
  bzero((char *)&server_addr, sizeof(server_addr));
  server_addr.sin_family = AF_INET;
  server_addr.sin_port = htons(port);
  server_addr.sin_addr.s_addr = INADDR_ANY;
  if (bind(sockfd, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) {
    error("ERROR binding");
  }
  listen(sockfd, 5);
  while (1) {
    bzero(buffer, 256);
    client_length = sizeof(client_addr);
    char client_nickname[256];
    newsockfd = accept(sockfd, (struct sockaddr *)&client_addr, &client_length);
    if (newsockfd < 0) {
      error("ERROR accepting");
    }
    n = read(newsockfd, client_nickname, 255);
    if (n < 0) {
      error("ERROR reading from socket while getting nickname");
    }
    strtok(client_nickname, "\n");
    printf("Connection established with %s on port %d\n", client_nickname,
           port);
    bzero(buffer, 256);
    while (1) {
      bzero(buffer, 256);
      n = read(newsockfd, buffer, 255);
      if (n < 0) {
        error("ERROR reading from socket");
      }
      printf("<%s> %s", client_nickname, buffer);
      if (strcmp(buffer, "--EXIT--\n") == 0) {
        break;
      }
    }
    printf("Connection with %s on port %d closed.\n", client_nickname, port);
  }
}


x86 assembly (clang, -O0):
<server>:
push   %rbp
mov    %rsp,%rbp
sub    $0x240,%rsp
mov    %edi,-0x4(%rbp)
mov    0x2e23(%rip),%rax
mov    (%rax),%edi
mov    0x2e02(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    11f0 <server+0x40>
lea    0xe15(%rip),%rdi
call   1090 <error@plt>
lea    -0x130(%rbp),%rdi
mov    $0xc,%esi
call   10b0 <bzero@plt>
mov    0x2de0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x128(%rbp)
mov    -0x4(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x12c(%rbp)
mov    0x2dbb(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x130(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x130(%rbp),%rsi
mov    $0xc,%edx
call   1080 <bind@plt>
cmp    $0x0,%rax
jge    1257 <server+0xa7>
lea    0xdc3(%rip),%rdi
call   1090 <error@plt>
mov    -0x8(%rbp),%edi
mov    $0x5,%esi
call   1070 <listen@plt>
lea    -0x120(%rbp),%rdi
mov    $0x100,%esi
call   10b0 <bzero@plt>
movl   $0xc,-0x10(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x140(%rbp),%rsi
lea    -0x10(%rbp),%rdx
call   10c0 <accept@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    12a8 <server+0xf8>
lea    0xd80(%rip),%rdi
call   1090 <error@plt>
mov    -0xc(%rbp),%edi
lea    -0x240(%rbp),%rsi
mov    $0xff,%edx
call   1050 <read@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    12d5 <server+0x125>
lea    0xd63(%rip),%rdi
call   1090 <error@plt>
lea    -0x240(%rbp),%rdi
lea    0xdd6(%rip),%rsi
call   10a0 <strtok@plt>
lea    -0x240(%rbp),%rsi
mov    -0x4(%rbp),%edx
lea    0xd6b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x120(%rbp),%rdi
mov    $0x100,%esi
call   10b0 <bzero@plt>
lea    -0x120(%rbp),%rdi
mov    $0x100,%esi
call   10b0 <bzero@plt>
mov    -0xc(%rbp),%edi
lea    -0x120(%rbp),%rsi
mov    $0xff,%edx
call   1050 <read@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    134f <server+0x19f>
lea    0xd45(%rip),%rdi
call   1090 <error@plt>
lea    -0x240(%rbp),%rsi
lea    -0x120(%rbp),%rdx
lea    0xd45(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x120(%rbp),%rdi
lea    0xd38(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
jne    138d <server+0x1dd>
jmp    1392 <server+0x1e2>
jmp    1311 <server+0x161>
lea    -0x240(%rbp),%rsi
mov    -0x4(%rbp),%edx
lea    0xd18(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1264 <server+0xb4>

x86 assembly (clang, -O2):
<server>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x238,%rsp
mov    %edi,%ebp
mov    0x2e1e(%rip),%r14
mov    (%r14),%edi
mov    0x2dfc(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    11ef <server+0x3f>
lea    0xe16(%rip),%rdi
call   1090 <error@plt>
lea    0x10(%rsp),%r15
mov    %r15,%rdi
mov    $0xc,%esi
call   10b0 <bzero@plt>
mov    (%r14),%eax
mov    %eax,0x18(%rsp)
mov    %ebp,0x4(%rsp)
mov    %ebp,%edi
call   1030 <htons@plt>
mov    %eax,0x14(%rsp)
mov    0x2dc2(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0xc,%edx
call   1080 <bind@plt>
test   %rax,%rax
jns    1244 <server+0x94>
lea    0xdd6(%rip),%rdi
call   1090 <error@plt>
mov    %ebx,0x8(%rsp)
mov    %ebx,%edi
mov    $0x5,%esi
call   1070 <listen@plt>
lea    0x130(%rsp),%rbp
lea    0x30(%rsp),%r13
lea    0xe27(%rip),%r15
lea    0xe3a(%rip),%r12
lea    0xe3b(%rip),%r14
jmp    1295 <server+0xe5>
nopl   0x0(%rax,%rax,1)
lea    0xe34(%rip),%rdi
mov    %r13,%rsi
mov    0x4(%rsp),%edx
xor    %eax,%eax
call   1040 <printf@plt>
mov    %rbp,%rdi
mov    $0x100,%esi
call   10b0 <bzero@plt>
movl   $0xc,0xc(%rsp)
mov    0x8(%rsp),%edi
lea    0x20(%rsp),%rsi
lea    0xc(%rsp),%rdx
call   10c0 <accept@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    12cf <server+0x11f>
lea    0xd59(%rip),%rdi
call   1090 <error@plt>
mov    %ebx,%edi
mov    %r13,%rsi
mov    $0xff,%edx
call   1050 <read@plt>
test   %eax,%eax
jns    12ee <server+0x13e>
lea    0xd4a(%rip),%rdi
call   1090 <error@plt>
mov    %r13,%rdi
lea    0xdc1(%rip),%rsi
call   10a0 <strtok@plt>
lea    0xd60(%rip),%rdi
mov    %r13,%rsi
mov    0x4(%rsp),%edx
xor    %eax,%eax
call   1040 <printf@plt>
mov    %rbp,%rdi
mov    $0x100,%esi
call   10b0 <bzero@plt>
jmp    1354 <server+0x1a4>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %r12,%rdi
mov    %r13,%rsi
mov    %rbp,%rdx
xor    %eax,%eax
call   1040 <printf@plt>
mov    %rbp,%rdi
mov    %r14,%rsi
call   1060 <strcmp@plt>
test   %rax,%rax
je     1280 <server+0xd0>
mov    %rbp,%rdi
mov    $0x100,%esi
call   10b0 <bzero@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0xff,%edx
call   1050 <read@plt>
test   %eax,%eax
jns    1330 <server+0x180>
mov    %r15,%rdi
call   1090 <error@plt>
jmp    1330 <server+0x180>

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [21 x i8] c"ERROR opening socket\00"
@INADDR_ANY = global i32 0
@.str.1 = [14 x i8] c"ERROR binding\00"
@.str.2 = [16 x i8] c"ERROR accepting\00"
@.str.3 = [49 x i8] c"ERROR reading from socket while getting nickname\00"
@.str.4 = [2 x i8] c"\0A\00"
@.str.5 = [43 x i8] c"Connection established with %s on port %d\0A\00"
@.str.6 = [26 x i8] c"ERROR reading from socket\00"
@.str.7 = [8 x i8] c"<%s> %s\00"
@.str.8 = [10 x i8] c"--EXIT--\0A\00"
@.str.9 = [39 x i8] c"Connection with %s on port %d closed.\0A\00"
define void @server(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca [256 x i8]
%8 = alloca %struct.sockaddr_in
%9 = alloca %struct.sockaddr_in
%10 = alloca [256 x i8]
store i32 %0, i32* %2
%11 = load i32, i32* @AF_INET
%12 = load i32, i32* @SOCK_STREAM
%13 = call i32 @socket(i32 %11, i32 %12, i32 0)
store i32 %13, i32* %3
%14 = load i32, i32* %3
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %1
%17 = call i32 @error(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
br label %18
18: ; preds = %16, %1
%19 = bitcast %struct.sockaddr_in* %8 to i8*
%20 = call i32 @bzero(i8* %19, i32 12)
%21 = load i32, i32* @AF_INET
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 2
store i32 %21, i32* %22
%23 = load i32, i32* %2
%24 = call i32 @htons(i32 %23)
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 1
store i32 %24, i32* %25
%26 = load i32, i32* @INADDR_ANY
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 0
%28 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %27, i32 0, i32 0
store i32 %26, i32* %28
%29 = load i32, i32* %3
%30 = bitcast %struct.sockaddr_in* %8 to %struct.sockaddr*
%31 = call i64 @bind(i32 %29, %struct.sockaddr* %30, i32 12)
%32 = icmp slt i64 %31, 0
br i1 %32, label %33, label %35
33: ; preds = %18
%34 = call i32 @error(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
br label %35
35: ; preds = %33, %18
%36 = load i32, i32* %3
%37 = call i32 @listen(i32 %36, i32 5)
br label %38
38: ; preds = %35, %83
%39 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%40 = call i32 @bzero(i8* %39, i32 256)
store i32 12, i32* %5
%41 = load i32, i32* %3
%42 = bitcast %struct.sockaddr_in* %9 to %struct.sockaddr*
%43 = call i32 @accept(i32 %41, %struct.sockaddr* %42, i32* %5)
store i32 %43, i32* %4
%44 = load i32, i32* %4
%45 = icmp slt i32 %44, 0
br i1 %45, label %46, label %48
46: ; preds = %38
%47 = call i32 @error(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
br label %48
48: ; preds = %46, %38
%49 = load i32, i32* %4
%50 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%51 = call i32 @read(i32 %49, i8* %50, i32 255)
store i32 %51, i32* %6
%52 = load i32, i32* %6
%53 = icmp slt i32 %52, 0
br i1 %53, label %54, label %56
54: ; preds = %48
%55 = call i32 @error(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.3, i64 0, i64 0))
br label %56
56: ; preds = %54, %48
%57 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%58 = call i32 @strtok(i8* %57, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%59 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%60 = load i32, i32* %2
%61 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.5, i64 0, i64 0), i8* %59, i32 %60)
%62 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%63 = call i32 @bzero(i8* %62, i32 256)
br label %64
64: ; preds = %56, %82
%65 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%66 = call i32 @bzero(i8* %65, i32 256)
%67 = load i32, i32* %4
%68 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%69 = call i32 @read(i32 %67, i8* %68, i32 255)
store i32 %69, i32* %6
%70 = load i32, i32* %6
%71 = icmp slt i32 %70, 0
br i1 %71, label %72, label %74
72: ; preds = %64
%73 = call i32 @error(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.6, i64 0, i64 0))
br label %74
74: ; preds = %72, %64
%75 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%76 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%77 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.7, i64 0, i64 0), i8* %75, i8* %76)
%78 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%79 = call i64 @strcmp(i8* %78, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.8, i64 0, i64 0))
%80 = icmp eq i64 %79, 0
br i1 %80, label %81, label %82
81: ; preds = %74
br label %83
82: ; preds = %74
br label %64
83: ; preds = %81
%84 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%85 = load i32, i32* %2
%86 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.9, i64 0, i64 0), i8* %84, i32 %85)
br label %38
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [21 x i8] c"ERROR opening socket\00"
@INADDR_ANY = global i32 0
@.str.1 = [14 x i8] c"ERROR binding\00"
@.str.2 = [16 x i8] c"ERROR accepting\00"
@.str.3 = [49 x i8] c"ERROR reading from socket while getting nickname\00"
@.str.4 = [2 x i8] c"\0A\00"
@.str.5 = [43 x i8] c"Connection established with %s on port %d\0A\00"
@.str.6 = [26 x i8] c"ERROR reading from socket\00"
@.str.7 = [8 x i8] c"<%s> %s\00"
@.str.8 = [10 x i8] c"--EXIT--\0A\00"
@.str.9 = [39 x i8] c"Connection with %s on port %d closed.\0A\00"
define void @server(i32 %0) {
%2 = alloca i32
%3 = alloca [256 x i8]
%4 = alloca %struct.sockaddr_in
%5 = alloca %struct.sockaddr_in
%6 = alloca [256 x i8]
%7 = bitcast i32* %2 to i8*
%8 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%9 = bitcast %struct.sockaddr_in* %4 to i8*
%10 = bitcast %struct.sockaddr_in* %5 to i8*
%11 = load i32, i32* @AF_INET
%12 = load i32, i32* @SOCK_STREAM
%13 = tail call i32 @socket(i32 %11, i32 %12, i32 0)
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %1
%16 = tail call i32 @error(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
br label %17
17: ; preds = %15, %1
%18 = call i32 @bzero(i8* %9, i32 12)
%19 = load i32, i32* @AF_INET
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 2
store i32 %19, i32* %20
%21 = tail call i32 @htons(i32 %0)
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1
store i32 %21, i32* %22
%23 = load i32, i32* @INADDR_ANY
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0, i32 0
store i32 %23, i32* %24
%25 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%26 = call i64 @bind(i32 %13, %struct.sockaddr* %25, i32 12)
%27 = icmp slt i64 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %17
%29 = call i32 @error(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
br label %30
30: ; preds = %28, %17
%31 = call i32 @listen(i32 %13, i32 5)
%32 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%33 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
br label %34
34: ; preds = %59, %30
%35 = call i32 @bzero(i8* %8, i32 256)
store i32 12, i32* %2
%36 = call i32 @accept(i32 %13, %struct.sockaddr* %33, i32* %2)
%37 = icmp slt i32 %36, 0
br i1 %37, label %38, label %40
38: ; preds = %34
%39 = call i32 @error(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
br label %40
40: ; preds = %38, %34
%41 = call i32 @read(i32 %36, i8* %32, i32 255)
%42 = icmp slt i32 %41, 0
br i1 %42, label %43, label %45
43: ; preds = %40
%44 = call i32 @error(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.3, i64 0, i64 0))
br label %45
45: ; preds = %43, %40
%46 = call i32 @strtok(i8* %32, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%47 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.5, i64 0, i64 0), i8* %32, i32 %0)
%48 = call i32 @bzero(i8* %8, i32 256)
br label %49
49: ; preds = %55, %45
%50 = call i32 @bzero(i8* %8, i32 256)
%51 = call i32 @read(i32 %36, i8* %8, i32 255)
%52 = icmp slt i32 %51, 0
br i1 %52, label %53, label %55
53: ; preds = %49
%54 = call i32 @error(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.6, i64 0, i64 0))
br label %55
55: ; preds = %53, %49
%56 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.7, i64 0, i64 0), i8* %32, i8* %8)
%57 = call i64 @strcmp(i8* %8, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.8, i64 0, i64 0))
%58 = icmp eq i64 %57, 0
br i1 %58, label %59, label %49
59: ; preds = %55
%60 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.9, i64 0, i64 0), i8* %32, i32 %0)
br label %34
}