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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
int accept(int, struct sockaddr *, 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*/ htons(int);
int /*<<< orphan*/ listen(int, int);
int /*<<< orphan*/ printf(char *, char *);
int read(int, char *, int);
int /*<<< orphan*/ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              char const *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
scalar_t__ strlen(char *);
int write(int, char *, scalar_t__);
int main(int argc, char *argv[]) {
  int sockfd, newsockfd, portno;
  socklen_t clilen;
  char buffer[256];
  struct sockaddr_in serv_addr, cli_addr;
  int n;
  int opt = 1;
  if (argc < 2) {
    error("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);
  setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (const char *)&opt, sizeof(int));
  if (bind(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
    error("ERROR on binding");
  listen(sockfd, 5);
  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("Server Buffer: %s\n", buffer);
  char *str = "I got your message!";
  n = write(newsockfd, str, strlen(str) + 1);
  if (n < 0)
    error("ERROR writing to socket");
  close(newsockfd);
  close(sockfd);
  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)
movl   $0x1,-0x148(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    1233 <main+0x43>
lea    0xddc(%rip),%rdi
call   10c0 <error@plt>
mov    $0x1,%edi
call   1100 <exit@plt>
mov    0x2da6(%rip),%rax
mov    (%rax),%edi
mov    0x2d85(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1110 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1265 <main+0x75>
lea    0xdb9(%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    0x2d53(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x128(%rbp)
mov    0x2d3c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x12c(%rbp)
mov    -0x1c(%rbp),%edi
call   1060 <htons@plt>
mov    %eax,-0x130(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2d2c(%rip),%rax
mov    (%rax),%esi
mov    0x2cfb(%rip),%rax
mov    (%rax),%edx
lea    -0x148(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    -0x14(%rbp),%edi
lea    -0x130(%rbp),%rsi
mov    $0xc,%edx
call   10b0 <bind@plt>
cmp    $0x0,%rax
jge    1303 <main+0x113>
lea    0xd30(%rip),%rdi
call   10c0 <error@plt>
mov    -0x14(%rbp),%edi
mov    $0x5,%esi
call   10a0 <listen@plt>
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    1343 <main+0x153>
lea    0xd01(%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   1090 <read@plt>
mov    %eax,-0x144(%rbp)
cmpl   $0x0,-0x144(%rbp)
jge    1387 <main+0x197>
lea    0xccd(%rip),%rdi
call   10c0 <error@plt>
lea    -0x120(%rbp),%rsi
lea    0xcd4(%rip),%rdi
call   1070 <printf@plt>
lea    0xcdb(%rip),%rax
mov    %rax,-0x150(%rbp)
mov    -0x18(%rbp),%eax
mov    %eax,-0x15c(%rbp)
mov    -0x150(%rbp),%rax
mov    %rax,-0x158(%rbp)
mov    -0x150(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x15c(%rbp),%edi
mov    -0x158(%rbp),%rsi
mov    %rax,%rdx
add    $0x1,%rdx
call   1040 <write@plt>
mov    %eax,-0x144(%rbp)
cmpl   $0x0,-0x144(%rbp)
jge    1403 <main+0x213>
lea    0xc92(%rip),%rdi
call   10c0 <error@plt>
mov    -0x18(%rbp),%edi
call   1080 <close@plt>
mov    -0x14(%rbp),%edi
call   1080 <close@plt>
xor    %eax,%eax
add    $0x160,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x138,%rsp
movl   $0x1,0x8(%rsp)
cmp    $0x1,%edi
jle    1369 <main+0x189>
mov    %rsi,%rbp
mov    0x2dd8(%rip),%rbx
mov    (%rbx),%edi
mov    0x2db7(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1100 <socket@plt>
mov    %eax,%r15d
test   %eax,%eax
jns    122d <main+0x4d>
lea    0xdf1(%rip),%rdi
call   10b0 <error@plt>
lea    0x10(%rsp),%r14
mov    %r14,%rdi
mov    $0xc,%esi
call   10c0 <bzero@plt>
mov    0x8(%rbp),%rdi
call   10e0 <atoi@plt>
mov    (%rbx),%ecx
mov    %ecx,0x18(%rsp)
mov    0x2d83(%rip),%rcx
mov    (%rcx),%ecx
mov    %ecx,0x14(%rsp)
mov    %eax,%edi
call   1050 <htons@plt>
mov    %eax,0x10(%rsp)
mov    0x2d7b(%rip),%rax
mov    (%rax),%esi
mov    0x2d4a(%rip),%rax
mov    (%rax),%edx
lea    0x8(%rsp),%rcx
mov    %r15d,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    %r15d,%edi
mov    %r14,%rsi
mov    $0xc,%edx
call   10a0 <bind@plt>
test   %rax,%rax
jns    12ac <main+0xcc>
lea    0xd87(%rip),%rdi
call   10b0 <error@plt>
mov    %r15d,%edi
mov    $0x5,%esi
call   1090 <listen@plt>
movl   $0xc,0xc(%rsp)
lea    0x20(%rsp),%rsi
lea    0xc(%rsp),%rdx
mov    %r15d,%edi
call   10d0 <accept@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    12e5 <main+0x105>
lea    0xd5f(%rip),%rdi
call   10b0 <error@plt>
lea    0x30(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x100,%esi
call   10c0 <bzero@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0xff,%edx
call   1080 <read@plt>
test   %eax,%eax
jns    1316 <main+0x136>
lea    0xd3e(%rip),%rdi
call   10b0 <error@plt>
lea    0xd4c(%rip),%rdi
lea    0x30(%rsp),%rsi
call   1060 <printf@plt>
lea    0xd4e(%rip),%rsi
mov    $0x14,%edx
mov    %ebp,%edi
call   1040 <write@plt>
test   %eax,%eax
jns    134a <main+0x16a>
lea    0xd4b(%rip),%rdi
call   10b0 <error@plt>
mov    %ebp,%edi
call   1070 <close@plt>
mov    %r15d,%edi
call   1070 <close@plt>
xor    %eax,%eax
add    $0x138,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xc90(%rip),%rdi
call   10b0 <error@plt>
mov    $0x1,%edi
call   10f0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@.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
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.2 = [17 x i8] c"ERROR on binding\00"
@.str.3 = [16 x i8] c"ERROR on accept\00"
@.str.4 = [26 x i8] c"ERROR reading from socket\00"
@.str.5 = [19 x i8] c"Server Buffer: %s\0A\00"
@.str.6 = [20 x i8] c"I got your message!\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
%15 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 1, i32* %14
%16 = load i32, i32* %4
%17 = icmp slt i32 %16, 2
br i1 %17, label %18, label %21
18: ; preds = %2
%19 = call i32 @error(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* %6
%45 = load i32, i32* @SOL_SOCKET
%46 = load i32, i32* @SO_REUSEADDR
%47 = bitcast i32* %14 to i8*
%48 = call i32 @setsockopt(i32 %44, i32 %45, i32 %46, i8* %47, i32 4)
%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 = %29
%54 = call i32 @error(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
br label %55
55: ; preds = %53, %29
%56 = load i32, i32* %6
%57 = call i32 @listen(i32 %56, i32 5)
store i32 12, i32* %9
%58 = load i32, i32* %6
%59 = bitcast %struct.sockaddr_in* %12 to %struct.sockaddr*
%60 = call i32 @accept(i32 %58, %struct.sockaddr* %59, i32* %9)
store i32 %60, i32* %7
%61 = load i32, i32* %7
%62 = icmp slt i32 %61, 0
br i1 %62, label %63, label %65
63: ; preds = %55
%64 = call i32 @error(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
br label %65
65: ; preds = %63, %55
%66 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%67 = call i32 @bzero(i8* %66, i32 256)
%68 = load i32, i32* %7
%69 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%70 = call i32 @read(i32 %68, i8* %69, i32 255)
store i32 %70, i32* %13
%71 = load i32, i32* %13
%72 = icmp slt i32 %71, 0
br i1 %72, label %73, label %75
73: ; preds = %65
%74 = call i32 @error(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0))
br label %75
75: ; preds = %73, %65
%76 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%77 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.5, i64 0, i64 0), i8* %76)
store i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.6, i64 0, i64 0), i8** %15
%78 = load i32, i32* %7
%79 = load i8*, i8** %15
%80 = load i8*, i8** %15
%81 = call i64 @strlen(i8* %80)
%82 = add nsw i64 %81, 1
%83 = call i32 @write(i32 %78, i8* %79, i64 %82)
store i32 %83, i32* %13
%84 = load i32, i32* %13
%85 = icmp slt i32 %84, 0
br i1 %85, label %86, label %88
86: ; preds = %75
%87 = call i32 @error(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.7, i64 0, i64 0))
br label %88
88: ; preds = %86, %75
%89 = load i32, i32* %7
%90 = call i32 @close(i32 %89)
%91 = load i32, i32* %6
%92 = call i32 @close(i32 %91)
ret i32 0
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@.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
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.2 = [17 x i8] c"ERROR on binding\00"
@.str.3 = [16 x i8] c"ERROR on accept\00"
@.str.4 = [26 x i8] c"ERROR reading from socket\00"
@.str.5 = [19 x i8] c"Server Buffer: %s\0A\00"
@.str.6 = [20 x i8] c"I got your message!\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 = alloca i32
%8 = bitcast i32* %3 to i8*
%9 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%10 = bitcast %struct.sockaddr_in* %5 to i8*
%11 = bitcast %struct.sockaddr_in* %6 to i8*
%12 = bitcast i32* %7 to i8*
store i32 1, i32* %7
%13 = icmp slt i32 %0, 2
br i1 %13, label %14, label %17
14: ; preds = %2
%15 = tail call i32 @error(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
%16 = tail call i32 @exit(i32 1)
unreachable
17: ; preds = %2
%18 = load i32, i32* @AF_INET
%19 = load i32, i32* @SOCK_STREAM
%20 = tail call i32 @socket(i32 %18, i32 %19, i32 0)
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %17
%23 = tail call i32 @error(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
br label %24
24: ; preds = %22, %17
%25 = call i32 @bzero(i8* %10, i32 12)
%26 = getelementptr inbounds i8*, i8** %1, i64 1
%27 = load i8*, i8** %26
%28 = tail call i32 @atoi(i8* %27)
%29 = load i32, i32* @AF_INET
%30 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 2
store i32 %29, i32* %30
%31 = load i32, i32* @INADDR_ANY
%32 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 1, i32 0
store i32 %31, i32* %32
%33 = tail call i32 @htons(i32 %28)
%34 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 0
store i32 %33, i32* %34
%35 = load i32, i32* @SOL_SOCKET
%36 = load i32, i32* @SO_REUSEADDR
%37 = call i32 @setsockopt(i32 %20, i32 %35, i32 %36, i8* %12, i32 4)
%38 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%39 = call i64 @bind(i32 %20, %struct.sockaddr* %38, i32 12)
%40 = icmp slt i64 %39, 0
br i1 %40, label %41, label %43
41: ; preds = %24
%42 = call i32 @error(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
br label %43
43: ; preds = %41, %24
%44 = call i32 @listen(i32 %20, i32 5)
store i32 12, i32* %3
%45 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%46 = call i32 @accept(i32 %20, %struct.sockaddr* %45, i32* %3)
%47 = icmp slt i32 %46, 0
br i1 %47, label %48, label %50
48: ; preds = %43
%49 = call i32 @error(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
br label %50
50: ; preds = %48, %43
%51 = call i32 @bzero(i8* %9, i32 256)
%52 = call i32 @read(i32 %46, i8* %9, i32 255)
%53 = icmp slt i32 %52, 0
br i1 %53, label %54, label %56
54: ; preds = %50
%55 = call i32 @error(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0))
br label %56
56: ; preds = %54, %50
%57 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.5, i64 0, i64 0), i8* %9)
%58 = call i32 @write(i32 %46, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.6, i64 0, i64 0), i64 20)
%59 = icmp slt i32 %58, 0
br i1 %59, label %60, label %62
60: ; preds = %56
%61 = call i32 @error(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.7, i64 0, i64 0))
br label %62
62: ; preds = %60, %56
%63 = call i32 @close(i32 %46)
%64 = call i32 @close(i32 %20)
ret i32 0
}