#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 /*<<< orphan*/ serv_addr;
typedef int /*<<< orphan*/ cli_addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ SOCK_STREAM;
int accept(int, struct sockaddr *, unsigned int *);
int atoi(char *);
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ bzero(char *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ error(char *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, 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*/);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strlen(char *);
int write(int, char *, int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  int sockfd, newsockfd, portno;
  unsigned int clilen;
  char buffer[256];
  struct sockaddr_in serv_addr, cli_addr;
  int n, x;
  if (argc < 2) {
    fprintf(stderr, "ERROR, no port provided\n");
    exit(1);
  }
  sockfd = socket(AF_INET, SOCK_STREAM, 0);
  if (sockfd < 0)
    error("ERROR opening socket");
  bzero((char *)&serv_addr, sizeof(serv_addr));
  portno = atoi(argv[1]);
  serv_addr.sin_family = AF_INET;
  serv_addr.sin_addr.s_addr = INADDR_ANY;
  serv_addr.sin_port = htons(portno);
  if (portno == 0) {
    error("Conversion of port failed");
  }
  if (bind(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
    error("ERROR on binding");
  listen(sockfd, 5);
  for (x = 0; x < 5; x++) {
    clilen = sizeof(cli_addr);
    newsockfd = accept(sockfd, (struct sockaddr *)&cli_addr, &clilen);
    if (newsockfd < 0)
      error("ERROR on accept");
    bzero(buffer, 256);
    n = read(newsockfd, buffer, 255);
    if (n < 0)
      error("ERROR reading from socket");
    printf("Here is the message: %s\n", buffer);
    n = write(newsockfd, buffer, strlen(buffer));
    if (n < 0)
      error("ERROR writing to socket");
    close(newsockfd);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x160,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    1232 <main+0x42>
mov    0x2db6(%rip),%rax
mov    (%rax),%edi
lea    0xddd(%rip),%rsi
call   1090 <fprintf@plt>
mov    $0x1,%edi
call   1100 <exit@plt>
mov    0x2daf(%rip),%rax
mov    (%rax),%edi
mov    0x2d86(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1110 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1264 <main+0x74>
lea    0xdba(%rip),%rdi
call   10c0 <error@plt>
lea    -0x130(%rbp),%rdi
mov    $0xc,%esi
call   10d0 <bzero@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   10f0 <atoi@plt>
mov    %eax,-0x1c(%rbp)
mov    0x2d5c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x128(%rbp)
mov    0x2d45(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x12c(%rbp)
mov    -0x1c(%rbp),%edi
call   1050 <htons@plt>
mov    %eax,-0x130(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jne    12c7 <main+0xd7>
lea    0xd6c(%rip),%rdi
call   10c0 <error@plt>
mov    -0x14(%rbp),%edi
lea    -0x130(%rbp),%rsi
mov    $0xc,%edx
call   10b0 <bind@plt>
cmp    $0x0,%rax
jge    12f1 <main+0x101>
lea    0xd5c(%rip),%rdi
call   10c0 <error@plt>
mov    -0x14(%rbp),%edi
mov    $0x5,%esi
call   10a0 <listen@plt>
movl   $0x0,-0x148(%rbp)
cmpl   $0x5,-0x148(%rbp)
jge    1411 <main+0x221>
movl   $0xc,-0x20(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x140(%rbp),%rsi
lea    -0x20(%rbp),%rdx
call   10e0 <accept@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    1348 <main+0x158>
lea    0xd16(%rip),%rdi
call   10c0 <error@plt>
lea    -0x120(%rbp),%rdi
mov    $0x100,%esi
call   10d0 <bzero@plt>
mov    -0x18(%rbp),%edi
lea    -0x120(%rbp),%rsi
mov    $0xff,%edx
call   1080 <read@plt>
mov    %eax,-0x144(%rbp)
cmpl   $0x0,-0x144(%rbp)
jge    138c <main+0x19c>
lea    0xce2(%rip),%rdi
call   10c0 <error@plt>
lea    -0x120(%rbp),%rsi
lea    0xce9(%rip),%rdi
call   1060 <printf@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x154(%rbp)
lea    -0x120(%rbp),%rax
mov    %rax,-0x150(%rbp)
lea    -0x120(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x154(%rbp),%edi
mov    -0x150(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %eax,-0x144(%rbp)
cmpl   $0x0,-0x144(%rbp)
jge    13f5 <main+0x205>
lea    0xcac(%rip),%rdi
call   10c0 <error@plt>
mov    -0x18(%rbp),%edi
call   1070 <close@plt>
mov    -0x148(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x148(%rbp)
jmp    1308 <main+0x118>
xor    %eax,%eax
add    $0x160,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x138,%rsp
cmp    $0x1,%edi
jle    1386 <main+0x196>
mov    %rsi,%rbx
mov    0x2dd4(%rip),%r14
mov    (%r14),%edi
mov    0x2daa(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1110 <socket@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    1239 <main+0x49>
lea    0xde5(%rip),%rdi
call   10c0 <error@plt>
lea    0x10(%rsp),%rdi
mov    $0xc,%esi
call   10d0 <bzero@plt>
mov    0x8(%rbx),%rdi
call   10f0 <atoi@plt>
mov    %eax,%ebx
mov    (%r14),%eax
mov    %eax,0x18(%rsp)
mov    0x2d7f(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x14(%rsp)
mov    %ebx,%edi
call   1050 <htons@plt>
mov    %eax,0x10(%rsp)
test   %ebx,%ebx
jne    1282 <main+0x92>
lea    0xdb1(%rip),%rdi
call   10c0 <error@plt>
lea    0x10(%rsp),%rsi
mov    %ebp,%edi
mov    $0xc,%edx
call   10b0 <bind@plt>
test   %rax,%rax
jns    12a4 <main+0xb4>
lea    0xda9(%rip),%rdi
call   10c0 <error@plt>
mov    $0x5,%r14d
mov    %ebp,%edi
mov    $0x5,%esi
call   10a0 <listen@plt>
lea    0xc(%rsp),%r15
lea    0x30(%rsp),%rbx
lea    0xdbc(%rip),%r12
jmp    12e2 <main+0xf2>
nopl   0x0(%rax)
mov    %r13d,%edi
call   1070 <close@plt>
add    $0xffffffff,%r14d
je     1372 <main+0x182>
movl   $0xc,0xc(%rsp)
mov    %ebp,%edi
lea    0x20(%rsp),%rsi
mov    %r15,%rdx
call   10e0 <accept@plt>
mov    %eax,%r13d
test   %eax,%eax
jns    130c <main+0x11c>
lea    0xd52(%rip),%rdi
call   10c0 <error@plt>
mov    %rbx,%rdi
mov    $0x100,%esi
call   10d0 <bzero@plt>
mov    %r13d,%edi
mov    %rbx,%rsi
mov    $0xff,%edx
call   1080 <read@plt>
test   %eax,%eax
jns    1339 <main+0x149>
lea    0xd35(%rip),%rdi
call   10c0 <error@plt>
mov    %r12,%rdi
mov    %rbx,%rsi
call   1060 <printf@plt>
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %r13d,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
test   %eax,%eax
jns    12d0 <main+0xe0>
lea    0xd34(%rip),%rdi
call   10c0 <error@plt>
jmp    12d0 <main+0xe0>
xor    %eax,%eax
add    $0x138,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2c43(%rip),%rax
mov    (%rax),%edi
lea    0xc6a(%rip),%rsi
call   1090 <fprintf@plt>
mov    $0x1,%edi
call   1100 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@stderr = global i32 0
@.str = [25 x i8] c"ERROR, no port provided\0A\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [21 x i8] c"ERROR opening socket\00"
@INADDR_ANY = global i32 0
@.str.2 = [26 x i8] c"Conversion of port failed\00"
@.str.3 = [17 x i8] c"ERROR on binding\00"
@.str.4 = [16 x i8] c"ERROR on accept\00"
@.str.5 = [26 x i8] c"ERROR reading from socket\00"
@.str.6 = [25 x i8] c"Here is the message: %s\0A\00"
@.str.7 = [24 x i8] c"ERROR writing to socket\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 [256 x i8]
%11 = alloca %struct.sockaddr_in
%12 = alloca %struct.sockaddr_in
%13 = alloca i32
%14 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%15 = load i32, i32* %4
%16 = icmp slt i32 %15, 2
br i1 %16, label %17, label %21
17: ; preds = %2
%18 = load i32, i32* @stderr
%19 = call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
%20 = call i32 @exit(i32 1)
unreachable
21: ; preds = %2
%22 = load i32, i32* @AF_INET
%23 = load i32, i32* @SOCK_STREAM
%24 = call i32 @socket(i32 %22, i32 %23, i32 0)
store i32 %24, i32* %6
%25 = load i32, i32* %6
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %21
%28 = call i32 @error(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
br label %29
29: ; preds = %27, %21
%30 = bitcast %struct.sockaddr_in* %11 to i8*
%31 = call i32 @bzero(i8* %30, i32 12)
%32 = load i8**, i8*** %5
%33 = getelementptr inbounds i8*, i8** %32, i64 1
%34 = load i8*, i8** %33
%35 = call i32 @atoi(i8* %34)
store i32 %35, i32* %8
%36 = load i32, i32* @AF_INET
%37 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 2
store i32 %36, i32* %37
%38 = load i32, i32* @INADDR_ANY
%39 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 1
%40 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %39, i32 0, i32 0
store i32 %38, i32* %40
%41 = load i32, i32* %8
%42 = call i32 @htons(i32 %41)
%43 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 0
store i32 %42, i32* %43
%44 = load i32, i32* %8
%45 = icmp eq i32 %44, 0
br i1 %45, label %46, label %48
46: ; preds = %29
%47 = call i32 @error(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
br label %48
48: ; preds = %46, %29
%49 = load i32, i32* %6
%50 = bitcast %struct.sockaddr_in* %11 to %struct.sockaddr*
%51 = call i64 @bind(i32 %49, %struct.sockaddr* %50, i32 12)
%52 = icmp slt i64 %51, 0
br i1 %52, label %53, label %55
53: ; preds = %48
%54 = call i32 @error(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0))
br label %55
55: ; preds = %53, %48
%56 = load i32, i32* %6
%57 = call i32 @listen(i32 %56, i32 5)
store i32 0, i32* %14
br label %58
58: ; preds = %94, %55
%59 = load i32, i32* %14
%60 = icmp slt i32 %59, 5
br i1 %60, label %61, label %97
61: ; preds = %58
store i32 12, i32* %9
%62 = load i32, i32* %6
%63 = bitcast %struct.sockaddr_in* %12 to %struct.sockaddr*
%64 = call i32 @accept(i32 %62, %struct.sockaddr* %63, i32* %9)
store i32 %64, i32* %7
%65 = load i32, i32* %7
%66 = icmp slt i32 %65, 0
br i1 %66, label %67, label %69
67: ; preds = %61
%68 = call i32 @error(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0))
br label %69
69: ; preds = %67, %61
%70 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%71 = call i32 @bzero(i8* %70, i32 256)
%72 = load i32, i32* %7
%73 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%74 = call i32 @read(i32 %72, i8* %73, i32 255)
store i32 %74, i32* %13
%75 = load i32, i32* %13
%76 = icmp slt i32 %75, 0
br i1 %76, label %77, label %79
77: ; preds = %69
%78 = call i32 @error(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.5, i64 0, i64 0))
br label %79
79: ; preds = %77, %69
%80 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%81 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.6, i64 0, i64 0), i8* %80)
%82 = load i32, i32* %7
%83 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%84 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%85 = call i32 @strlen(i8* %84)
%86 = call i32 @write(i32 %82, i8* %83, i32 %85)
store i32 %86, i32* %13
%87 = load i32, i32* %13
%88 = icmp slt i32 %87, 0
br i1 %88, label %89, label %91
89: ; preds = %79
%90 = call i32 @error(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.7, i64 0, i64 0))
br label %91
91: ; preds = %89, %79
%92 = load i32, i32* %7
%93 = call i32 @close(i32 %92)
br label %94
94: ; preds = %91
%95 = load i32, i32* %14
%96 = add nsw i32 %95, 1
store i32 %96, i32* %14
br label %58
97: ; preds = %58
ret i32 0
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@stderr = global i32 0
@.str = [25 x i8] c"ERROR, no port provided\0A\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [21 x i8] c"ERROR opening socket\00"
@INADDR_ANY = global i32 0
@.str.2 = [26 x i8] c"Conversion of port failed\00"
@.str.3 = [17 x i8] c"ERROR on binding\00"
@.str.4 = [16 x i8] c"ERROR on accept\00"
@.str.5 = [26 x i8] c"ERROR reading from socket\00"
@.str.6 = [25 x i8] c"Here is the message: %s\0A\00"
@.str.7 = [24 x i8] c"ERROR writing to socket\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = alloca [256 x i8]
%5 = alloca %struct.sockaddr_in
%6 = alloca %struct.sockaddr_in
%7 = bitcast i32* %3 to i8*
%8 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%9 = bitcast %struct.sockaddr_in* %5 to i8*
%10 = bitcast %struct.sockaddr_in* %6 to i8*
%11 = icmp slt i32 %0, 2
br i1 %11, label %12, label %16
12: ; preds = %2
%13 = load i32, i32* @stderr
%14 = tail call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
%15 = tail call i32 @exit(i32 1)
unreachable
16: ; preds = %2
%17 = load i32, i32* @AF_INET
%18 = load i32, i32* @SOCK_STREAM
%19 = tail call i32 @socket(i32 %17, i32 %18, i32 0)
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %16
%22 = tail call i32 @error(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
br label %23
23: ; preds = %21, %16
%24 = call i32 @bzero(i8* %9, i32 12)
%25 = getelementptr inbounds i8*, i8** %1, i64 1
%26 = load i8*, i8** %25
%27 = tail call i32 @atoi(i8* %26)
%28 = load i32, i32* @AF_INET
%29 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 2
store i32 %28, i32* %29
%30 = load i32, i32* @INADDR_ANY
%31 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 1, i32 0
store i32 %30, i32* %31
%32 = tail call i32 @htons(i32 %27)
%33 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 0
store i32 %32, i32* %33
%34 = icmp eq i32 %27, 0
br i1 %34, label %35, label %37
35: ; preds = %23
%36 = tail call i32 @error(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
br label %37
37: ; preds = %35, %23
%38 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%39 = call i64 @bind(i32 %19, %struct.sockaddr* %38, i32 12)
%40 = icmp slt i64 %39, 0
br i1 %40, label %41, label %43
41: ; preds = %37
%42 = call i32 @error(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0))
br label %43
43: ; preds = %41, %37
%44 = call i32 @listen(i32 %19, i32 5)
%45 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
br label %46
46: ; preds = %43, %65
%47 = phi i32 [ 0, %43 ], [ %67, %65 ]
store i32 12, i32* %3
%48 = call i32 @accept(i32 %19, %struct.sockaddr* %45, i32* %3)
%49 = icmp slt i32 %48, 0
br i1 %49, label %50, label %52
50: ; preds = %46
%51 = call i32 @error(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0))
br label %52
52: ; preds = %50, %46
%53 = call i32 @bzero(i8* %8, i32 256)
%54 = call i32 @read(i32 %48, i8* %8, i32 255)
%55 = icmp slt i32 %54, 0
br i1 %55, label %56, label %58
56: ; preds = %52
%57 = call i32 @error(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.5, i64 0, i64 0))
br label %58
58: ; preds = %56, %52
%59 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.6, i64 0, i64 0), i8* %8)
%60 = call i32 @strlen(i8* %8)
%61 = call i32 @write(i32 %48, i8* %8, i32 %60)
%62 = icmp slt i32 %61, 0
br i1 %62, label %63, label %65
63: ; preds = %58
%64 = call i32 @error(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.7, i64 0, i64 0))
br label %65
65: ; preds = %63, %58
%66 = call i32 @close(i32 %48)
%67 = add nuw nsw i32 %47, 1
%68 = icmp eq i32 %67, 5
br i1 %68, label %69, label %46
69: ; preds = %65
ret i32 0
}