#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 socklen_t;
typedef int /*<<< orphan*/ serverAddress;
typedef int /*<<< orphan*/ clientAddress;

/* 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*/ close(int);
int /*<<< orphan*/ error(char *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ listen(int, int);
int /*<<< orphan*/ memset(char *, char, int);
int /*<<< orphan*/ printf(char *, char *);
int recv(int, char *, int, int /*<<< orphan*/);
int send(int, char *, int, int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int main(int argc, char *argv[]) {
  int listenSocketFD, establishedConnectionFD, portNumber, charsRead;
  socklen_t sizeOfClientInfo;
  char buffer[256];
  struct sockaddr_in serverAddress, clientAddress;
  if (argc < 2) {
    fprintf(stderr, "USAGE: %s port\n", argv[0]);
    exit(1);
  }
  memset((char *)&serverAddress, '\0', sizeof(serverAddress));
  portNumber = atoi(argv[1]);
  serverAddress.sin_family = AF_INET;
  serverAddress.sin_port = htons(portNumber);
  serverAddress.sin_addr.s_addr = INADDR_ANY;
  listenSocketFD = socket(AF_INET, SOCK_STREAM, 0);
  if (listenSocketFD < 0)
    error("ERROR opening socket");
  if (bind(listenSocketFD, (struct sockaddr *)&serverAddress,
           sizeof(serverAddress)) < 0)
    error("ERROR on binding");
  listen(listenSocketFD, 5);
  sizeOfClientInfo = sizeof(clientAddress);
  establishedConnectionFD = accept(
      listenSocketFD, (struct sockaddr *)&clientAddress, &sizeOfClientInfo);
  if (establishedConnectionFD < 0)
    error("ERROR on accept");
  memset(buffer, '\0', 256);
  charsRead = recv(establishedConnectionFD, buffer, 255, 0);
  if (charsRead < 0)
    error("ERROR reading from socket");
  printf("SERVER: I received this from the client: \"%s\"\n", buffer);
  charsRead = send(establishedConnectionFD,
                   "I am the server, and I got your message", 39, 0);
  if (charsRead < 0)
    error("ERROR writing to socket");
  close(establishedConnectionFD);
  close(listenSocketFD);
  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    1229 <main+0x49>
mov    0x2dc6(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xde6(%rip),%rsi
call   1090 <fprintf@plt>
mov    $0x1,%edi
call   10f0 <exit@plt>
lea    -0x140(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1070 <memset@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   10e0 <atoi@plt>
mov    %eax,-0x1c(%rbp)
mov    0x2d95(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x138(%rbp)
mov    -0x1c(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x13c(%rbp)
mov    0x2d70(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x140(%rbp)
mov    0x2d69(%rip),%rax
mov    (%rax),%edi
mov    0x2d40(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1100 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    12aa <main+0xca>
lea    0xd6b(%rip),%rdi
call   10c0 <error@plt>
mov    -0x14(%rbp),%edi
lea    -0x140(%rbp),%rsi
mov    $0xc,%edx
call   10b0 <bind@plt>
cmp    $0x0,%rax
jge    12d4 <main+0xf4>
lea    0xd56(%rip),%rdi
call   10c0 <error@plt>
mov    -0x14(%rbp),%edi
mov    $0x5,%esi
call   10a0 <listen@plt>
movl   $0xc,-0x24(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x150(%rbp),%rsi
lea    -0x24(%rbp),%rdx
call   10d0 <accept@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    1314 <main+0x134>
lea    0xd27(%rip),%rdi
call   10c0 <error@plt>
lea    -0x130(%rbp),%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1070 <memset@plt>
mov    -0x18(%rbp),%edi
lea    -0x130(%rbp),%rsi
mov    $0xff,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    1356 <main+0x176>
lea    0xcf5(%rip),%rdi
call   10c0 <error@plt>
lea    -0x130(%rbp),%rsi
lea    0xcfc(%rip),%rdi
call   1060 <printf@plt>
mov    -0x18(%rbp),%edi
lea    0xd1c(%rip),%rsi
mov    $0x27,%edx
xor    %ecx,%ecx
call   1050 <send@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    1398 <main+0x1b8>
lea    0xd24(%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    $0x150,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x130,%rsp
mov    %rsi,%rbx
cmp    $0x1,%edi
jle    133a <main+0x15a>
lea    0x10(%rsp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1070 <memset@plt>
mov    0x8(%rbx),%rdi
call   10e0 <atoi@plt>
mov    0x2dd0(%rip),%rbx
mov    (%rbx),%ecx
mov    %ecx,0x18(%rsp)
mov    %eax,%edi
call   1040 <htons@plt>
mov    %eax,0x14(%rsp)
mov    0x2db0(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
mov    (%rbx),%edi
mov    0x2d89(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1100 <socket@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    125a <main+0x7a>
lea    0xdbb(%rip),%rdi
call   10c0 <error@plt>
lea    0x10(%rsp),%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   10b0 <bind@plt>
test   %rax,%rax
jns    127c <main+0x9c>
lea    0xdae(%rip),%rdi
call   10c0 <error@plt>
mov    %ebx,%edi
mov    $0x5,%esi
call   10a0 <listen@plt>
movl   $0xc,0xc(%rsp)
lea    0x20(%rsp),%rsi
lea    0xc(%rsp),%rdx
mov    %ebx,%edi
call   10d0 <accept@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    12b3 <main+0xd3>
lea    0xd88(%rip),%rdi
call   10c0 <error@plt>
lea    0x30(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1070 <memset@plt>
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0xff,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
test   %eax,%eax
jns    12e8 <main+0x108>
lea    0xd63(%rip),%rdi
call   10c0 <error@plt>
lea    0xd71(%rip),%rdi
lea    0x30(%rsp),%rsi
call   1060 <printf@plt>
lea    0xd8f(%rip),%rsi
mov    %ebp,%edi
mov    $0x27,%edx
xor    %ecx,%ecx
call   1050 <send@plt>
test   %eax,%eax
jns    131e <main+0x13e>
lea    0xd9e(%rip),%rdi
call   10c0 <error@plt>
mov    %ebp,%edi
call   1080 <close@plt>
mov    %ebx,%edi
call   1080 <close@plt>
xor    %eax,%eax
add    $0x130,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x2c8f(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%rdx
lea    0xcb3(%rip),%rsi
call   1090 <fprintf@plt>
mov    $0x1,%edi
call   10f0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@stderr = global i32 0
@.str = [16 x i8] c"USAGE: %s port\0A\00"
@AF_INET = global i32 0
@INADDR_ANY = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [21 x i8] c"ERROR opening socket\00"
@.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 = [47 x i8] c"SERVER: I received this from the client: \22%s\22\0A\00"
@.str.6 = [40 x i8] c"I am the server, and 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 i32
%11 = alloca [256 x i8]
%12 = alloca %struct.sockaddr_in
%13 = alloca %struct.sockaddr_in
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 %23
16: ; preds = %2
%17 = load i32, i32* @stderr
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 0
%20 = load i8*, i8** %19
%21 = call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* %20)
%22 = call i32 @exit(i32 1)
unreachable
23: ; preds = %2
%24 = bitcast %struct.sockaddr_in* %12 to i8*
%25 = call i32 @memset(i8* %24, i8 signext 0, i32 12)
%26 = load i8**, i8*** %5
%27 = getelementptr inbounds i8*, i8** %26, i64 1
%28 = load i8*, i8** %27
%29 = call i32 @atoi(i8* %28)
store i32 %29, i32* %8
%30 = load i32, i32* @AF_INET
%31 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %12, i32 0, i32 2
store i32 %30, i32* %31
%32 = load i32, i32* %8
%33 = call i32 @htons(i32 %32)
%34 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %12, i32 0, i32 1
store i32 %33, i32* %34
%35 = load i32, i32* @INADDR_ANY
%36 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %12, i32 0, i32 0
%37 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %36, i32 0, i32 0
store i32 %35, i32* %37
%38 = load i32, i32* @AF_INET
%39 = load i32, i32* @SOCK_STREAM
%40 = call i32 @socket(i32 %38, i32 %39, i32 0)
store i32 %40, i32* %6
%41 = load i32, i32* %6
%42 = icmp slt i32 %41, 0
br i1 %42, label %43, label %45
43: ; preds = %23
%44 = call i32 @error(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
br label %45
45: ; preds = %43, %23
%46 = load i32, i32* %6
%47 = bitcast %struct.sockaddr_in* %12 to %struct.sockaddr*
%48 = call i64 @bind(i32 %46, %struct.sockaddr* %47, i32 12)
%49 = icmp slt i64 %48, 0
br i1 %49, label %50, label %52
50: ; preds = %45
%51 = call i32 @error(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
br label %52
52: ; preds = %50, %45
%53 = load i32, i32* %6
%54 = call i32 @listen(i32 %53, i32 5)
store i32 12, i32* %10
%55 = load i32, i32* %6
%56 = bitcast %struct.sockaddr_in* %13 to %struct.sockaddr*
%57 = call i32 @accept(i32 %55, %struct.sockaddr* %56, i32* %10)
store i32 %57, i32* %7
%58 = load i32, i32* %7
%59 = icmp slt i32 %58, 0
br i1 %59, label %60, label %62
60: ; preds = %52
%61 = call i32 @error(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
br label %62
62: ; preds = %60, %52
%63 = getelementptr inbounds [256 x i8], [256 x i8]* %11, i64 0, i64 0
%64 = call i32 @memset(i8* %63, i8 signext 0, i32 256)
%65 = load i32, i32* %7
%66 = getelementptr inbounds [256 x i8], [256 x i8]* %11, i64 0, i64 0
%67 = call i32 @recv(i32 %65, i8* %66, i32 255, i32 0)
store i32 %67, i32* %9
%68 = load i32, i32* %9
%69 = icmp slt i32 %68, 0
br i1 %69, label %70, label %72
70: ; preds = %62
%71 = call i32 @error(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0))
br label %72
72: ; preds = %70, %62
%73 = getelementptr inbounds [256 x i8], [256 x i8]* %11, i64 0, i64 0
%74 = call i32 @printf(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.5, i64 0, i64 0), i8* %73)
%75 = load i32, i32* %7
%76 = call i32 @send(i32 %75, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.6, i64 0, i64 0), i32 39, i32 0)
store i32 %76, i32* %9
%77 = load i32, i32* %9
%78 = icmp slt i32 %77, 0
br i1 %78, label %79, label %81
79: ; preds = %72
%80 = call i32 @error(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.7, i64 0, i64 0))
br label %81
81: ; preds = %79, %72
%82 = load i32, i32* %7
%83 = call i32 @close(i32 %82)
%84 = load i32, i32* %6
%85 = call i32 @close(i32 %84)
ret i32 0
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@stderr = global i32 0
@.str = [16 x i8] c"USAGE: %s port\0A\00"
@AF_INET = global i32 0
@INADDR_ANY = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [21 x i8] c"ERROR opening socket\00"
@.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 = [47 x i8] c"SERVER: I received this from the client: \22%s\22\0A\00"
@.str.6 = [40 x i8] c"I am the server, and 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 = 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 %17
12: ; preds = %2
%13 = load i32, i32* @stderr
%14 = load i8*, i8** %1
%15 = tail call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* %14)
%16 = tail call i32 @exit(i32 1)
unreachable
17: ; preds = %2
%18 = call i32 @memset(i8* %9, i8 signext 0, i32 12)
%19 = getelementptr inbounds i8*, i8** %1, i64 1
%20 = load i8*, i8** %19
%21 = call i32 @atoi(i8* %20)
%22 = load i32, i32* @AF_INET
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 2
store i32 %22, i32* %23
%24 = call i32 @htons(i32 %21)
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 1
store i32 %24, i32* %25
%26 = load i32, i32* @INADDR_ANY
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 0, i32 0
store i32 %26, i32* %27
%28 = load i32, i32* @AF_INET
%29 = load i32, i32* @SOCK_STREAM
%30 = call i32 @socket(i32 %28, i32 %29, i32 0)
%31 = icmp slt i32 %30, 0
br i1 %31, label %32, label %34
32: ; preds = %17
%33 = call i32 @error(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
br label %34
34: ; preds = %32, %17
%35 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%36 = call i64 @bind(i32 %30, %struct.sockaddr* %35, i32 12)
%37 = icmp slt i64 %36, 0
br i1 %37, label %38, label %40
38: ; preds = %34
%39 = call i32 @error(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
br label %40
40: ; preds = %38, %34
%41 = call i32 @listen(i32 %30, i32 5)
store i32 12, i32* %3
%42 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%43 = call i32 @accept(i32 %30, %struct.sockaddr* %42, i32* %3)
%44 = icmp slt i32 %43, 0
br i1 %44, label %45, label %47
45: ; preds = %40
%46 = call i32 @error(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
br label %47
47: ; preds = %45, %40
%48 = call i32 @memset(i8* %8, i8 signext 0, i32 256)
%49 = call i32 @recv(i32 %43, i8* %8, i32 255, i32 0)
%50 = icmp slt i32 %49, 0
br i1 %50, label %51, label %53
51: ; preds = %47
%52 = call i32 @error(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0))
br label %53
53: ; preds = %51, %47
%54 = call i32 @printf(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.5, i64 0, i64 0), i8* %8)
%55 = call i32 @send(i32 %43, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.6, i64 0, i64 0), i32 39, i32 0)
%56 = icmp slt i32 %55, 0
br i1 %56, label %57, label %59
57: ; preds = %53
%58 = call i32 @error(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.7, i64 0, i64 0))
br label %59
59: ; preds = %57, %53
%60 = call i32 @close(i32 %43)
%61 = call i32 @close(i32 %30)
ret i32 0
}