#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*/ handle;
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_in *, 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*/ fgets(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ htons(int);
scalar_t__ listen(int, int);
int /*<<< orphan*/ printf(char *);
int recvMSG(int);
int sendMSG(int, char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdin;
int strlen(char *);
int main(int argc, char *argv[]) {
  int newsockfd, sockserv, clilen, portno, quit;
  struct sockaddr_in serv_addr, cli_addr;
  char handle[256];
  if (argc < 2) {
    fprintf(stderr, "ERROR, no port provided\n");
    exit(1);
  }
  printf("What is the servers handle?\n");
  fgets(handle, sizeof(handle), stdin);
  handle[strlen(handle) - 1] = '\0';
  sockserv = socket(AF_INET, SOCK_STREAM, 0);
  if (sockserv < 0)
    error("ERROR opening socket");
  portno = atoi(argv[1]);
  bzero((char *)&serv_addr, sizeof(serv_addr));
  serv_addr.sin_family = AF_INET;
  serv_addr.sin_addr.s_addr = INADDR_ANY;
  serv_addr.sin_port = htons(portno);
  if (bind(sockserv, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
    error("ERROR on binding");
  if (listen(sockserv, 10) < 0)
    error("listen() failed");
  clilen = sizeof(cli_addr);
  while (1) {
    if ((newsockfd = accept(sockserv, &cli_addr, &clilen)) < 0)
      error("accept() failed");
    else {
      printf("Now connected\n");
      printf("Please enter the message: please type \"quit\" to exit chat.\n");
      quit = 0;
      while (quit != 1) {
        quit = recvMSG(newsockfd);
        if (quit != 1)
          quit = sendMSG(newsockfd, handle);
      }
    }
    printf("Client has disconnected\n");
  }
  close(newsockfd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x140,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    1232 <main+0x42>
mov    0x2dae(%rip),%rax
mov    (%rax),%edi
lea    0xddd(%rip),%rsi
call   1090 <fprintf@plt>
mov    $0x1,%edi
call   1100 <exit@plt>
lea    0xde0(%rip),%rdi
call   1070 <printf@plt>
lea    -0x140(%rbp),%rdi
mov    0x2d94(%rip),%rax
mov    (%rax),%edx
mov    $0x100,%esi
call   1080 <fgets@plt>
lea    -0x140(%rbp),%rdi
call   1050 <strlen@plt>
sub    $0x1,%eax
cltq
movb   $0x0,-0x140(%rbp,%rax,1)
mov    0x2d70(%rip),%rax
mov    (%rax),%edi
mov    0x2d3f(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1110 <socket@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    12a3 <main+0xb3>
lea    0xd98(%rip),%rdi
call   10c0 <error@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   10f0 <atoi@plt>
mov    %eax,-0x20(%rbp)
lea    -0x30(%rbp),%rdi
mov    $0xc,%esi
call   10d0 <bzero@plt>
mov    0x2d20(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    0x2d04(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x20(%rbp),%edi
call   1060 <htons@plt>
mov    %eax,-0x30(%rbp)
mov    -0x18(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0xc,%edx
call   10b0 <bind@plt>
cmp    $0x0,%rax
jge    130b <main+0x11b>
lea    0xd45(%rip),%rdi
call   10c0 <error@plt>
mov    -0x18(%rbp),%edi
mov    $0xa,%esi
call   10a0 <listen@plt>
cmp    $0x0,%rax
jge    132e <main+0x13e>
lea    0xd33(%rip),%rdi
call   10c0 <error@plt>
movl   $0xc,-0x1c(%rbp)
mov    -0x18(%rbp),%edi
lea    -0x40(%rbp),%rsi
lea    -0x1c(%rbp),%rdx
call   10e0 <accept@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    1362 <main+0x172>
lea    0xd14(%rip),%rdi
call   10c0 <error@plt>
jmp    13bc <main+0x1cc>
lea    0xd13(%rip),%rdi
call   1070 <printf@plt>
lea    0xd16(%rip),%rdi
call   1070 <printf@plt>
movl   $0x0,-0x24(%rbp)
cmpl   $0x1,-0x24(%rbp)
je     13b7 <main+0x1c7>
mov    -0x14(%rbp),%edi
call   1040 <recvMSG@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x1,-0x24(%rbp)
je     13b2 <main+0x1c2>
mov    -0x14(%rbp),%edi
lea    -0x140(%rbp),%rsi
call   1030 <sendMSG@plt>
mov    %eax,-0x24(%rbp)
jmp    1381 <main+0x191>
jmp    13bc <main+0x1cc>
lea    0xd04(%rip),%rdi
call   1070 <printf@plt>
jmp    1335 <main+0x145>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x138,%rsp
cmp    $0x1,%edi
jle    136f <main+0x17f>
mov    %rsi,%rbp
lea    0xe4b(%rip),%rdi
call   1040 <puts@plt>
mov    0x2dc0(%rip),%rax
mov    (%rax),%edx
lea    0x30(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x100,%esi
call   1080 <fgets@plt>
mov    %rbx,%rdi
call   1060 <strlen@plt>
cltq
movb   $0x0,-0x1(%rax,%rbx,1)
mov    0x2d9e(%rip),%r15
mov    (%r15),%edi
mov    0x2d6c(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1110 <socket@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    126f <main+0x7f>
lea    0xdaf(%rip),%rdi
call   10c0 <error@plt>
mov    0x8(%rbp),%rdi
call   10f0 <atoi@plt>
mov    %eax,%ebp
lea    0x10(%rsp),%r14
mov    %r14,%rdi
mov    $0xc,%esi
call   10d0 <bzero@plt>
mov    (%r15),%eax
mov    %eax,0x18(%rsp)
mov    0x2d3e(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x14(%rsp)
mov    %ebp,%edi
call   1070 <htons@plt>
mov    %eax,0x10(%rsp)
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0xc,%edx
call   10b0 <bind@plt>
test   %rax,%rax
jns    12cb <main+0xdb>
lea    0xd68(%rip),%rdi
call   10c0 <error@plt>
mov    %ebx,%edi
mov    $0xa,%esi
call   10a0 <listen@plt>
test   %rax,%rax
jns    12e8 <main+0xf8>
lea    0xd5c(%rip),%rdi
call   10c0 <error@plt>
movl   $0xc,0xc(%rsp)
lea    0x20(%rsp),%r14
lea    0xc(%rsp),%r15
lea    0xdc3(%rip),%r13
lea    0x30(%rsp),%rbp
jmp    1324 <main+0x134>
nopl   0x0(%rax,%rax,1)
lea    0xd38(%rip),%rdi
call   10c0 <error@plt>
mov    %r13,%rdi
call   1040 <puts@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    %r15,%rdx
call   10e0 <accept@plt>
test   %eax,%eax
js     1310 <main+0x120>
mov    %eax,%r12d
lea    0xd3c(%rip),%rdi
call   1040 <puts@plt>
lea    0xd3e(%rip),%rdi
call   1040 <puts@plt>
mov    %r12d,%edi
call   1050 <recvMSG@plt>
cmp    $0x1,%eax
je     131c <main+0x12c>
mov    %r12d,%edi
mov    %rbp,%rsi
call   1030 <sendMSG@plt>
cmp    $0x1,%eax
jne    1350 <main+0x160>
jmp    131c <main+0x12c>
mov    0x2c52(%rip),%rax
mov    (%rax),%edi
lea    0xc81(%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"
@.str.1 = [29 x i8] c"What is the servers handle?\0A\00"
@stdin = global i32 0
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.2 = [21 x i8] c"ERROR opening socket\00"
@INADDR_ANY = global i32 0
@.str.3 = [17 x i8] c"ERROR on binding\00"
@.str.4 = [16 x i8] c"listen() failed\00"
@.str.5 = [16 x i8] c"accept() failed\00"
@.str.6 = [15 x i8] c"Now connected\0A\00"
@.str.7 = [60 x i8] c"Please enter the message: please type \22quit\22 to exit chat.\0A\00"
@.str.8 = [25 x i8] c"Client has disconnected\0A\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 %struct.sockaddr_in
%12 = alloca %struct.sockaddr_in
%13 = alloca [256 x i8]
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 ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
%19 = call i32 @exit(i32 1)
unreachable
20: ; preds = %2
%21 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0))
%22 = getelementptr inbounds [256 x i8], [256 x i8]* %13, i64 0, i64 0
%23 = load i32, i32* @stdin
%24 = call i32 @fgets(i8* %22, i32 256, i32 %23)
%25 = getelementptr inbounds [256 x i8], [256 x i8]* %13, i64 0, i64 0
%26 = call i32 @strlen(i8* %25)
%27 = sub nsw i32 %26, 1
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds [256 x i8], [256 x i8]* %13, i64 0, i64 %28
store i8 0, i8* %29
%30 = load i32, i32* @AF_INET
%31 = load i32, i32* @SOCK_STREAM
%32 = call i32 @socket(i32 %30, i32 %31, i32 0)
store i32 %32, i32* %7
%33 = load i32, i32* %7
%34 = icmp slt i32 %33, 0
br i1 %34, label %35, label %37
35: ; preds = %20
%36 = call i32 @error(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
br label %37
37: ; preds = %35, %20
%38 = load i8**, i8*** %5
%39 = getelementptr inbounds i8*, i8** %38, i64 1
%40 = load i8*, i8** %39
%41 = call i32 @atoi(i8* %40)
store i32 %41, i32* %9
%42 = bitcast %struct.sockaddr_in* %11 to i8*
%43 = call i32 @bzero(i8* %42, i32 12)
%44 = load i32, i32* @AF_INET
%45 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 2
store i32 %44, i32* %45
%46 = load i32, i32* @INADDR_ANY
%47 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 1
%48 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %47, i32 0, i32 0
store i32 %46, i32* %48
%49 = load i32, i32* %9
%50 = call i32 @htons(i32 %49)
%51 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 0
store i32 %50, i32* %51
%52 = load i32, i32* %7
%53 = bitcast %struct.sockaddr_in* %11 to %struct.sockaddr*
%54 = call i64 @bind(i32 %52, %struct.sockaddr* %53, i32 12)
%55 = icmp slt i64 %54, 0
br i1 %55, label %56, label %58
56: ; preds = %37
%57 = call i32 @error(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0))
br label %58
58: ; preds = %56, %37
%59 = load i32, i32* %7
%60 = call i64 @listen(i32 %59, i32 10)
%61 = icmp slt i64 %60, 0
br i1 %61, label %62, label %64
62: ; preds = %58
%63 = call i32 @error(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0))
br label %64
64: ; preds = %62, %58
store i32 12, i32* %8
br label %65
65: ; preds = %64, %88
%66 = load i32, i32* %7
%67 = call i32 @accept(i32 %66, %struct.sockaddr_in* %12, i32* %8)
store i32 %67, i32* %6
%68 = icmp slt i32 %67, 0
br i1 %68, label %69, label %71
69: ; preds = %65
%70 = call i32 @error(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.5, i64 0, i64 0))
br label %88
71: ; preds = %65
%72 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.6, i64 0, i64 0))
%73 = call i32 @printf(i8* getelementptr inbounds ([60 x i8], [60 x i8]* @.str.7, i64 0, i64 0))
store i32 0, i32* %10
br label %74
74: ; preds = %86, %71
%75 = load i32, i32* %10
%76 = icmp ne i32 %75, 1
br i1 %76, label %77, label %87
77: ; preds = %74
%78 = load i32, i32* %6
%79 = call i32 @recvMSG(i32 %78)
store i32 %79, i32* %10
%80 = load i32, i32* %10
%81 = icmp ne i32 %80, 1
br i1 %81, label %82, label %86
82: ; preds = %77
%83 = load i32, i32* %6
%84 = getelementptr inbounds [256 x i8], [256 x i8]* %13, i64 0, i64 0
%85 = call i32 @sendMSG(i32 %83, i8* %84)
store i32 %85, i32* %10
br label %86
86: ; preds = %82, %77
br label %74
87: ; preds = %74
br label %88
88: ; preds = %87, %69
%89 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.8, i64 0, i64 0))
br label %65
}

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"
@stdin = global i32 0
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.2 = [21 x i8] c"ERROR opening socket\00"
@INADDR_ANY = global i32 0
@.str.3 = [17 x i8] c"ERROR on binding\00"
@.str.4 = [16 x i8] c"listen() failed\00"
@.str.5 = [16 x i8] c"accept() failed\00"
@str = [28 x i8] c"What is the servers handle?\00"
@str.9 = [14 x i8] c"Now connected\00"
@str.10 = [59 x i8] c"Please enter the message: please type \22quit\22 to exit chat.\00"
@str.11 = [24 x i8] c"Client has disconnected\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = alloca %struct.sockaddr_in
%5 = alloca %struct.sockaddr_in
%6 = alloca [256 x i8]
%7 = bitcast i32* %3 to i8*
%8 = bitcast %struct.sockaddr_in* %4 to i8*
%9 = bitcast %struct.sockaddr_in* %5 to i8*
%10 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%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 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @str, i64 0, i64 0))
%18 = load i32, i32* @stdin
%19 = call i32 @fgets(i8* %10, i32 256, i32 %18)
%20 = call i32 @strlen(i8* %10)
%21 = add nsw i32 %20, -1
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 %22
store i8 0, i8* %23
%24 = load i32, i32* @AF_INET
%25 = load i32, i32* @SOCK_STREAM
%26 = call i32 @socket(i32 %24, i32 %25, i32 0)
%27 = icmp slt i32 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %16
%29 = call i32 @error(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
br label %30
30: ; preds = %28, %16
%31 = getelementptr inbounds i8*, i8** %1, i64 1
%32 = load i8*, i8** %31
%33 = call i32 @atoi(i8* %32)
%34 = call i32 @bzero(i8* %8, i32 12)
%35 = load i32, i32* @AF_INET
%36 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 2
store i32 %35, i32* %36
%37 = load i32, i32* @INADDR_ANY
%38 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1, i32 0
store i32 %37, i32* %38
%39 = call i32 @htons(i32 %33)
%40 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0
store i32 %39, i32* %40
%41 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%42 = call i64 @bind(i32 %26, %struct.sockaddr* %41, i32 12)
%43 = icmp slt i64 %42, 0
br i1 %43, label %44, label %46
44: ; preds = %30
%45 = call i32 @error(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0))
br label %46
46: ; preds = %44, %30
%47 = call i64 @listen(i32 %26, i32 10)
%48 = icmp slt i64 %47, 0
br i1 %48, label %49, label %51
49: ; preds = %46
%50 = call i32 @error(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0))
br label %51
51: ; preds = %49, %46
store i32 12, i32* %3
br label %52
52: ; preds = %66, %51
%53 = call i32 @accept(i32 %26, %struct.sockaddr_in* %5, i32* %3)
%54 = icmp slt i32 %53, 0
br i1 %54, label %55, label %57
55: ; preds = %52
%56 = call i32 @error(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.5, i64 0, i64 0))
br label %66
57: ; preds = %52
%58 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str.9, i64 0, i64 0))
%59 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([59 x i8], [59 x i8]* @str.10, i64 0, i64 0))
br label %60
60: ; preds = %57, %63
%61 = call i32 @recvMSG(i32 %53)
%62 = icmp eq i32 %61, 1
br i1 %62, label %66, label %63
63: ; preds = %60
%64 = call i32 @sendMSG(i32 %53, i8* %10)
%65 = icmp eq i32 %64, 1
br i1 %65, label %66, label %60
66: ; preds = %60, %63, %55
%67 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @str.11, i64 0, i64 0))
br label %52
}