#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 *, int *);
int atoi(char *);
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ bzero(char *, 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 write(int, char *, int);
int main(int argc, char *argv[]) {
  int sockfd, newsockfd, portno, clilen;
  char buffer[256];
  struct sockaddr_in serv_addr, cli_addr;
  int n;
  if (argc < 2) {
    fprintf(stderr, "Error: no port provided \n");
    exit(1);
  }
  sockfd = socket(AF_INET, SOCK_STREAM, 0);
  if (sockfd < 0) {
    error("Error while opening the 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 (bind(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) {
    error("Error while binding the socket ...");
  }
  listen(sockfd, 5);
  clilen = sizeof(cli_addr);
  newsockfd = accept(sockfd, (struct sockaddr *)&cli_addr, &clilen);
  if (newsockfd < 0) {
    error("Error on accepting a request ...");
  }
  bzero(buffer, 256);
  n = read(newsockfd, buffer, 255);
  if (n < 0) {
    error("Error while reading from the socket");
  }
  printf("Received an incoming message from the socket: %s \n", buffer);
  n = write(newsockfd, "The message was received by the tcp server", 43);
  if (n < 0) {
    error("Error while writing to the socket ...");
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x150,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    1212 <main+0x42>
mov    0x2dd6(%rip),%rax
mov    (%rax),%edi
lea    0xdfd(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
mov    0x2dcf(%rip),%rax
mov    (%rax),%edi
mov    0x2da6(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10f0 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1244 <main+0x74>
lea    0xddb(%rip),%rdi
call   10a0 <error@plt>
lea    -0x130(%rbp),%rdi
mov    $0xc,%esi
call   10b0 <bzero@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   10d0 <atoi@plt>
mov    %eax,-0x1c(%rbp)
mov    0x2d7c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x128(%rbp)
mov    0x2d65(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x12c(%rbp)
mov    -0x1c(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x130(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x130(%rbp),%rsi
mov    $0xc,%edx
call   1090 <bind@plt>
cmp    $0x0,%rax
jge    12bb <main+0xeb>
lea    0xd87(%rip),%rdi
call   10a0 <error@plt>
mov    -0x14(%rbp),%edi
mov    $0x5,%esi
call   1080 <listen@plt>
movl   $0xc,-0x20(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x140(%rbp),%rsi
lea    -0x20(%rbp),%rdx
call   10c0 <accept@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    12fb <main+0x12b>
lea    0xd6a(%rip),%rdi
call   10a0 <error@plt>
lea    -0x120(%rbp),%rdi
mov    $0x100,%esi
call   10b0 <bzero@plt>
mov    -0x18(%rbp),%edi
lea    -0x120(%rbp),%rsi
mov    $0xff,%edx
call   1060 <read@plt>
mov    %eax,-0x144(%rbp)
cmpl   $0x0,-0x144(%rbp)
jge    133f <main+0x16f>
lea    0xd47(%rip),%rdi
call   10a0 <error@plt>
lea    -0x120(%rbp),%rsi
lea    0xd58(%rip),%rdi
call   1050 <printf@plt>
mov    -0x18(%rbp),%edi
lea    0xd7c(%rip),%rsi
mov    $0x2b,%edx
call   1030 <write@plt>
mov    %eax,-0x144(%rbp)
cmpl   $0x0,-0x144(%rbp)
jge    1385 <main+0x1b5>
lea    0xd83(%rip),%rdi
call   10a0 <error@plt>
xor    %eax,%eax
add    $0x150,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x138,%rsp
cmp    $0x1,%edi
jle    131f <main+0x14f>
mov    %rsi,%r14
mov    0x2df7(%rip),%r12
mov    (%r12),%edi
mov    0x2dcc(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10f0 <socket@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    1217 <main+0x47>
lea    0xe08(%rip),%rdi
call   10a0 <error@plt>
lea    0x10(%rsp),%r15
mov    %r15,%rdi
mov    $0xc,%esi
call   10b0 <bzero@plt>
mov    0x8(%r14),%rdi
call   10d0 <atoi@plt>
mov    (%r12),%ecx
mov    %ecx,0x18(%rsp)
mov    0x2d9f(%rip),%rcx
mov    (%rcx),%ecx
mov    %ecx,0x14(%rsp)
mov    %eax,%edi
call   1040 <htons@plt>
mov    %eax,0x10(%rsp)
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0xc,%edx
call   1090 <bind@plt>
test   %rax,%rax
jns    1272 <main+0xa2>
lea    0xdd0(%rip),%rdi
call   10a0 <error@plt>
mov    %ebx,%edi
mov    $0x5,%esi
call   1080 <listen@plt>
movl   $0xc,0xc(%rsp)
lea    0x20(%rsp),%rsi
lea    0xc(%rsp),%rdx
mov    %ebx,%edi
call   10c0 <accept@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    12a9 <main+0xd9>
lea    0xdbc(%rip),%rdi
call   10a0 <error@plt>
lea    0x30(%rsp),%r14
mov    %r14,%rdi
mov    $0x100,%esi
call   10b0 <bzero@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0xff,%edx
call   1060 <read@plt>
test   %eax,%eax
jns    12da <main+0x10a>
lea    0xdac(%rip),%rdi
call   10a0 <error@plt>
lea    0xdc4(%rip),%rdi
lea    0x30(%rsp),%rsi
call   1050 <printf@plt>
lea    0xde6(%rip),%rsi
mov    %ebx,%edi
mov    $0x2b,%edx
call   1030 <write@plt>
test   %eax,%eax
jns    130e <main+0x13e>
lea    0xdfa(%rip),%rdi
call   10a0 <error@plt>
xor    %eax,%eax
add    $0x138,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
mov    0x2caa(%rip),%rax
mov    (%rax),%edi
lea    0xcd1(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10e0 <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 = [26 x i8] c"Error: no port provided \0A\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [35 x i8] c"Error while opening the socket ...\00"
@INADDR_ANY = global i32 0
@.str.2 = [35 x i8] c"Error while binding the socket ...\00"
@.str.3 = [33 x i8] c"Error on accepting a request ...\00"
@.str.4 = [36 x i8] c"Error while reading from the socket\00"
@.str.5 = [51 x i8] c"Received an incoming message from the socket: %s \0A\00"
@.str.6 = [43 x i8] c"The message was received by the tcp server\00"
@.str.7 = [38 x i8] c"Error while writing to the 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
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%14 = load i32, i32* %4
%15 = icmp slt i32 %14, 2
br i1 %15, label %16, label %20
16: ; preds = %2
%17 = load i32, i32* @stderr
%18 = call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
%19 = call i32 @exit(i32 1)
unreachable
20: ; preds = %2
%21 = load i32, i32* @AF_INET
%22 = load i32, i32* @SOCK_STREAM
%23 = call i32 @socket(i32 %21, i32 %22, i32 0)
store i32 %23, i32* %6
%24 = load i32, i32* %6
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %20
%27 = call i32 @error(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0))
br label %28
28: ; preds = %26, %20
%29 = bitcast %struct.sockaddr_in* %11 to i8*
%30 = call i32 @bzero(i8* %29, i32 12)
%31 = load i8**, i8*** %5
%32 = getelementptr inbounds i8*, i8** %31, i64 1
%33 = load i8*, i8** %32
%34 = call i32 @atoi(i8* %33)
store i32 %34, i32* %8
%35 = load i32, i32* @AF_INET
%36 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 2
store i32 %35, i32* %36
%37 = load i32, i32* @INADDR_ANY
%38 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 1
%39 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %38, i32 0, i32 0
store i32 %37, i32* %39
%40 = load i32, i32* %8
%41 = call i32 @htons(i32 %40)
%42 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 0
store i32 %41, i32* %42
%43 = load i32, i32* %6
%44 = bitcast %struct.sockaddr_in* %11 to %struct.sockaddr*
%45 = call i64 @bind(i32 %43, %struct.sockaddr* %44, i32 12)
%46 = icmp slt i64 %45, 0
br i1 %46, label %47, label %49
47: ; preds = %28
%48 = call i32 @error(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0))
br label %49
49: ; preds = %47, %28
%50 = load i32, i32* %6
%51 = call i32 @listen(i32 %50, i32 5)
store i32 12, i32* %9
%52 = load i32, i32* %6
%53 = bitcast %struct.sockaddr_in* %12 to %struct.sockaddr*
%54 = call i32 @accept(i32 %52, %struct.sockaddr* %53, i32* %9)
store i32 %54, i32* %7
%55 = load i32, i32* %7
%56 = icmp slt i32 %55, 0
br i1 %56, label %57, label %59
57: ; preds = %49
%58 = call i32 @error(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0))
br label %59
59: ; preds = %57, %49
%60 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%61 = call i32 @bzero(i8* %60, i32 256)
%62 = load i32, i32* %7
%63 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%64 = call i32 @read(i32 %62, i8* %63, i32 255)
store i32 %64, i32* %13
%65 = load i32, i32* %13
%66 = icmp slt i32 %65, 0
br i1 %66, label %67, label %69
67: ; preds = %59
%68 = call i32 @error(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.4, i64 0, i64 0))
br label %69
69: ; preds = %67, %59
%70 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%71 = call i32 @printf(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.5, i64 0, i64 0), i8* %70)
%72 = load i32, i32* %7
%73 = call i32 @write(i32 %72, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.6, i64 0, i64 0), i32 43)
store i32 %73, i32* %13
%74 = load i32, i32* %13
%75 = icmp slt i32 %74, 0
br i1 %75, label %76, label %78
76: ; preds = %69
%77 = call i32 @error(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.7, i64 0, i64 0))
br label %78
78: ; preds = %76, %69
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 = [26 x i8] c"Error: no port provided \0A\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [35 x i8] c"Error while opening the socket ...\00"
@INADDR_ANY = global i32 0
@.str.2 = [35 x i8] c"Error while binding the socket ...\00"
@.str.3 = [33 x i8] c"Error on accepting a request ...\00"
@.str.4 = [36 x i8] c"Error while reading from the socket\00"
@.str.5 = [51 x i8] c"Received an incoming message from the socket: %s \0A\00"
@.str.6 = [43 x i8] c"The message was received by the tcp server\00"
@.str.7 = [38 x i8] c"Error while writing to the 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 ([26 x i8], [26 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 ([35 x i8], [35 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 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%35 = call i64 @bind(i32 %19, %struct.sockaddr* %34, i32 12)
%36 = icmp slt i64 %35, 0
br i1 %36, label %37, label %39
37: ; preds = %23
%38 = call i32 @error(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0))
br label %39
39: ; preds = %37, %23
%40 = call i32 @listen(i32 %19, i32 5)
store i32 12, i32* %3
%41 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%42 = call i32 @accept(i32 %19, %struct.sockaddr* %41, i32* %3)
%43 = icmp slt i32 %42, 0
br i1 %43, label %44, label %46
44: ; preds = %39
%45 = call i32 @error(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0))
br label %46
46: ; preds = %44, %39
%47 = call i32 @bzero(i8* %8, i32 256)
%48 = call i32 @read(i32 %42, i8* %8, i32 255)
%49 = icmp slt i32 %48, 0
br i1 %49, label %50, label %52
50: ; preds = %46
%51 = call i32 @error(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.4, i64 0, i64 0))
br label %52
52: ; preds = %50, %46
%53 = call i32 @printf(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.5, i64 0, i64 0), i8* %8)
%54 = call i32 @write(i32 %42, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.6, i64 0, i64 0), i32 43)
%55 = icmp slt i32 %54, 0
br i1 %55, label %56, label %58
56: ; preds = %52
%57 = call i32 @error(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.7, i64 0, i64 0))
br label %58
58: ; preds = %56, %52
ret i32 0
}