#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;
};
struct hostent {
  char *h_name;
};
typedef int /*<<< orphan*/ serveraddr;
typedef int /*<<< orphan*/ clientaddr;

/* 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*/ fgets(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
struct hostent *gethostbyaddr(char const *, int, int /*<<< orphan*/);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(unsigned short);
char *inet_ntoa(TYPE_1__);
scalar_t__ listen(int, int);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int /*<<< orphan*/ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              void const *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ strlen(char *);
int write(int, char *, int /*<<< orphan*/);
int main(int argc, char **argv) {
  int listenfd, connfd, portno, clientlen;
  struct sockaddr_in serveraddr;
  struct sockaddr_in clientaddr;
  struct hostent *hostp;
  char buf[1024];
  char *hostaddrp;
  int optval;
  int n;
  if (argc != 2) {
    fprintf(stderr, "usage: %s <port>\n", argv[0]);
    exit(1);
  }
  portno = atoi(argv[1]);
  listenfd = socket(AF_INET, SOCK_STREAM, 0);
  if (listenfd < 0)
    error("ERROR opening socket");
  optval = 1;
  setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, (const void *)&optval,
             sizeof(int));
  bzero((char *)&serveraddr, sizeof(serveraddr));
  serveraddr.sin_family = AF_INET;
  serveraddr.sin_addr.s_addr = htonl(INADDR_ANY);
  serveraddr.sin_port = htons((unsigned short)portno);
  if (bind(listenfd, (struct sockaddr *)&serveraddr, sizeof(serveraddr)) < 0)
    error("ERROR on binding");
  if (listen(listenfd, 5) < 0)
    error("ERROR on listen");
  clientlen = sizeof(clientaddr);
  while (1) {
    connfd = accept(listenfd, (struct sockaddr *)&clientaddr, &clientlen);
    if (connfd < 0)
      error("ERROR on accept");
    hostp = gethostbyaddr((const char *)&clientaddr.sin_addr.s_addr,
                          sizeof(clientaddr.sin_addr.s_addr), AF_INET);
    if (hostp == NULL)
      error("ERROR on gethostbyaddr");
    hostaddrp = inet_ntoa(clientaddr.sin_addr);
    if (hostaddrp == NULL)
      error("ERROR on inet_ntoa\n");
    printf("server established connection with %s (%s)\n", hostp->h_name,
           hostaddrp);
    bzero(buf, 1024);
    n = read(connfd, buf, 1024);
    if (n < 0)
      error("ERROR reading from socket");
    printf("server received %d bytes: %s", n, buf);
    printf("msg to client: ");
    bzero(buf, 1024);
    fgets(buf, 1024, stdin);
    n = write(connfd, buf, strlen(buf));
    if (n < 0)
      error("ERROR writing to socket");
  }
  close(connfd);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x470,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     1279 <main+0x49>
mov    0x2d66(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xd96(%rip),%rsi
call   10c0 <fprintf@plt>
mov    $0x1,%edi
call   1140 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1120 <atoi@plt>
mov    %eax,-0x1c(%rbp)
mov    0x2d50(%rip),%rax
mov    (%rax),%edi
mov    0x2d1f(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1150 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    12bb <main+0x8b>
lea    0xd5c(%rip),%rdi
call   10f0 <error@plt>
movl   $0x1,-0x45c(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2d19(%rip),%rax
mov    (%rax),%esi
mov    0x2cd8(%rip),%rax
mov    (%rax),%edx
lea    -0x45c(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
lea    -0x30(%rbp),%rdi
mov    $0xc,%esi
call   1100 <bzero@plt>
mov    0x2cdf(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    0x2cc3(%rip),%rax
mov    (%rax),%edi
call   1090 <htonl@plt>
mov    %eax,-0x30(%rbp)
mov    -0x1c(%rbp),%eax
movzwl %ax,%edi
call   1070 <htons@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0xc,%edx
call   10e0 <bind@plt>
cmp    $0x0,%rax
jge    134c <main+0x11c>
lea    0xce0(%rip),%rdi
call   10f0 <error@plt>
mov    -0x14(%rbp),%edi
mov    $0x5,%esi
call   10d0 <listen@plt>
cmp    $0x0,%rax
jge    136f <main+0x13f>
lea    0xcce(%rip),%rdi
call   10f0 <error@plt>
movl   $0xc,-0x20(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x40(%rbp),%rsi
lea    -0x20(%rbp),%rdx
call   1110 <accept@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    139f <main+0x16f>
lea    0xcae(%rip),%rdi
call   10f0 <error@plt>
lea    -0x40(%rbp),%rdi
mov    0x2c36(%rip),%rax
mov    (%rax),%edx
mov    $0x4,%esi
call   1130 <gethostbyaddr@plt>
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
jne    13d1 <main+0x1a1>
lea    0xc8c(%rip),%rdi
call   10f0 <error@plt>
mov    -0x40(%rbp),%edi
call   1050 <inet_ntoa@plt>
mov    %rax,-0x458(%rbp)
cmpq   $0x0,-0x458(%rbp)
jne    13fa <main+0x1ca>
lea    0xc7a(%rip),%rdi
call   10f0 <error@plt>
mov    -0x48(%rbp),%rax
mov    (%rax),%rsi
mov    -0x458(%rbp),%rdx
lea    0xc74(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
lea    -0x450(%rbp),%rdi
mov    $0x400,%esi
call   1100 <bzero@plt>
mov    -0x18(%rbp),%edi
lea    -0x450(%rbp),%rsi
mov    $0x400,%edx
call   10a0 <read@plt>
mov    %eax,-0x460(%rbp)
cmpl   $0x0,-0x460(%rbp)
jge    145a <main+0x22a>
lea    0xc5a(%rip),%rdi
call   10f0 <error@plt>
mov    -0x460(%rbp),%esi
lea    -0x450(%rbp),%rdx
lea    0xc5b(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
lea    0xc6a(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
lea    -0x450(%rbp),%rdi
mov    $0x400,%esi
call   1100 <bzero@plt>
lea    -0x450(%rbp),%rdi
mov    0x2b36(%rip),%rax
mov    (%rax),%edx
mov    $0x400,%esi
call   10b0 <fgets@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x46c(%rbp)
lea    -0x450(%rbp),%rax
mov    %rax,-0x468(%rbp)
lea    -0x450(%rbp),%rdi
call   1060 <strlen@plt>
mov    -0x46c(%rbp),%edi
mov    -0x468(%rbp),%rsi
mov    %eax,%edx
call   1040 <write@plt>
mov    %eax,-0x460(%rbp)
cmpl   $0x0,-0x460(%rbp)
jge    1504 <main+0x2d4>
lea    0xbf7(%rip),%rdi
call   10f0 <error@plt>
jmp    1376 <main+0x146>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x438,%rsp
cmp    $0x2,%edi
jne    1469 <main+0x239>
mov    0x8(%rsi),%rdi
call   1120 <atoi@plt>
mov    %eax,%r14d
mov    0x2d83(%rip),%r13
mov    0x0(%r13),%edi
mov    0x2d50(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1150 <socket@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    1283 <main+0x53>
lea    0xd94(%rip),%rdi
call   10f0 <error@plt>
movl   $0x1,0x8(%rsp)
mov    0x2d56(%rip),%rax
mov    (%rax),%esi
mov    0x2d15(%rip),%rax
mov    (%rax),%edx
lea    0x8(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
lea    0x10(%rsp),%r15
mov    %r15,%rdi
mov    $0xc,%esi
call   1100 <bzero@plt>
mov    0x0(%r13),%eax
mov    %eax,0x18(%rsp)
mov    0x2d00(%rip),%rax
mov    (%rax),%edi
call   1090 <htonl@plt>
mov    %eax,0x10(%rsp)
movzwl %r14w,%edi
call   1070 <htons@plt>
mov    %eax,0x14(%rsp)
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0xc,%edx
call   10e0 <bind@plt>
test   %rax,%rax
jns    1308 <main+0xd8>
lea    0xd24(%rip),%rdi
call   10f0 <error@plt>
mov    %ebx,%edi
mov    $0x5,%esi
call   10d0 <listen@plt>
test   %rax,%rax
jns    1325 <main+0xf5>
lea    0xd18(%rip),%rdi
call   10f0 <error@plt>
movl   $0xc,0xc(%rsp)
lea    0x20(%rsp),%rbp
lea    0x30(%rsp),%r12
mov    %ebx,0x4(%rsp)
nopl   0x0(%rax,%rax,1)
mov    %ebx,%edi
mov    %rbp,%rsi
lea    0xc(%rsp),%rdx
call   1110 <accept@plt>
mov    %eax,%r15d
test   %eax,%eax
jns    1362 <main+0x132>
lea    0xceb(%rip),%rdi
call   10f0 <error@plt>
mov    %r13,%rbx
mov    0x0(%r13),%edx
mov    %rbp,%rdi
mov    $0x4,%esi
call   1130 <gethostbyaddr@plt>
mov    %rax,%r13
test   %rax,%rax
jne    138a <main+0x15a>
lea    0xcd3(%rip),%rdi
call   10f0 <error@plt>
mov    0x20(%rsp),%edi
call   1050 <inet_ntoa@plt>
mov    %rax,%r14
test   %rax,%rax
jne    13a7 <main+0x177>
lea    0xccd(%rip),%rdi
call   10f0 <error@plt>
mov    0x0(%r13),%rsi
lea    0xcd1(%rip),%rdi
mov    %r14,%rdx
xor    %eax,%eax
call   1080 <printf@plt>
mov    %r12,%rdi
mov    $0x400,%esi
call   1100 <bzero@plt>
mov    %r15d,%edi
mov    %r12,%rsi
mov    $0x400,%edx
call   10a0 <read@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    13eb <main+0x1bb>
lea    0xcc9(%rip),%rdi
call   10f0 <error@plt>
lea    0xcd7(%rip),%rdi
mov    %ebp,%esi
mov    %r12,%rdx
xor    %eax,%eax
call   1080 <printf@plt>
lea    0xce1(%rip),%rdi
xor    %eax,%eax
call   1080 <printf@plt>
mov    %r12,%rdi
mov    $0x400,%esi
call   1100 <bzero@plt>
mov    0x2bb8(%rip),%rax
mov    (%rax),%edx
mov    %r12,%rdi
mov    $0x400,%esi
call   10b0 <fgets@plt>
mov    %r12,%rdi
call   1060 <strlen@plt>
mov    %r15d,%edi
mov    %r12,%rsi
mov    %eax,%edx
call   1040 <write@plt>
test   %eax,%eax
mov    %rbx,%r13
mov    0x4(%rsp),%ebx
lea    0x20(%rsp),%rbp
jns    1340 <main+0x110>
lea    0xc97(%rip),%rdi
call   10f0 <error@plt>
jmp    1340 <main+0x110>
mov    0x2b50(%rip),%rax
mov    (%rax),%edi
mov    (%rsi),%rdx
lea    0xb84(%rip),%rsi
call   10c0 <fprintf@plt>
mov    $0x1,%edi
call   1140 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.hostent = type { i8* }
%struct.sockaddr = type { i32 }
@stderr = global i32 0
@.str = [18 x i8] c"usage: %s <port>\0A\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [21 x i8] c"ERROR opening socket\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@INADDR_ANY = global i32 0
@.str.2 = [17 x i8] c"ERROR on binding\00"
@.str.3 = [16 x i8] c"ERROR on listen\00"
@.str.4 = [16 x i8] c"ERROR on accept\00"
@.str.5 = [23 x i8] c"ERROR on gethostbyaddr\00"
@.str.6 = [20 x i8] c"ERROR on inet_ntoa\0A\00"
@.str.7 = [44 x i8] c"server established connection with %s (%s)\0A\00"
@.str.8 = [26 x i8] c"ERROR reading from socket\00"
@.str.9 = [29 x i8] c"server received %d bytes: %s\00"
@.str.10 = [16 x i8] c"msg to client: \00"
@stdin = global i32 0
@.str.11 = [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 %struct.sockaddr_in
%11 = alloca %struct.sockaddr_in
%12 = alloca %struct.hostent*
%13 = alloca [1024 x i8]
%14 = alloca i8*
%15 = alloca i32
%16 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%17 = load i32, i32* %4
%18 = icmp ne i32 %17, 2
br i1 %18, label %19, label %26
19: ; preds = %2
%20 = load i32, i32* @stderr
%21 = load i8**, i8*** %5
%22 = getelementptr inbounds i8*, i8** %21, i64 0
%23 = load i8*, i8** %22
%24 = call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %23)
%25 = call i32 @exit(i32 1)
unreachable
26: ; preds = %2
%27 = load i8**, i8*** %5
%28 = getelementptr inbounds i8*, i8** %27, i64 1
%29 = load i8*, i8** %28
%30 = call i32 @atoi(i8* %29)
store i32 %30, i32* %8
%31 = load i32, i32* @AF_INET
%32 = load i32, i32* @SOCK_STREAM
%33 = call i32 @socket(i32 %31, i32 %32, i32 0)
store i32 %33, i32* %6
%34 = load i32, i32* %6
%35 = icmp slt i32 %34, 0
br i1 %35, label %36, label %38
36: ; preds = %26
%37 = call i32 @error(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
br label %38
38: ; preds = %36, %26
store i32 1, i32* %15
%39 = load i32, i32* %6
%40 = load i32, i32* @SOL_SOCKET
%41 = load i32, i32* @SO_REUSEADDR
%42 = bitcast i32* %15 to i8*
%43 = call i32 @setsockopt(i32 %39, i32 %40, i32 %41, i8* %42, i32 4)
%44 = bitcast %struct.sockaddr_in* %10 to i8*
%45 = call i32 @bzero(i8* %44, i32 12)
%46 = load i32, i32* @AF_INET
%47 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 2
store i32 %46, i32* %47
%48 = load i32, i32* @INADDR_ANY
%49 = call i32 @htonl(i32 %48)
%50 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 0
%51 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %50, i32 0, i32 0
store i32 %49, i32* %51
%52 = load i32, i32* %8
%53 = trunc i32 %52 to i16
%54 = call i32 @htons(i16 zeroext %53)
%55 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 1
store i32 %54, i32* %55
%56 = load i32, i32* %6
%57 = bitcast %struct.sockaddr_in* %10 to %struct.sockaddr*
%58 = call i64 @bind(i32 %56, %struct.sockaddr* %57, i32 12)
%59 = icmp slt i64 %58, 0
br i1 %59, label %60, label %62
60: ; preds = %38
%61 = call i32 @error(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
br label %62
62: ; preds = %60, %38
%63 = load i32, i32* %6
%64 = call i64 @listen(i32 %63, i32 5)
%65 = icmp slt i64 %64, 0
br i1 %65, label %66, label %68
66: ; preds = %62
%67 = call i32 @error(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
br label %68
68: ; preds = %66, %62
store i32 12, i32* %9
br label %69
69: ; preds = %68, %130
%70 = load i32, i32* %6
%71 = bitcast %struct.sockaddr_in* %11 to %struct.sockaddr*
%72 = call i32 @accept(i32 %70, %struct.sockaddr* %71, i32* %9)
store i32 %72, i32* %7
%73 = load i32, i32* %7
%74 = icmp slt i32 %73, 0
br i1 %74, label %75, label %77
75: ; preds = %69
%76 = call i32 @error(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0))
br label %77
77: ; preds = %75, %69
%78 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 0
%79 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %78, i32 0, i32 0
%80 = bitcast i32* %79 to i8*
%81 = load i32, i32* @AF_INET
%82 = call %struct.hostent* @gethostbyaddr(i8* %80, i32 4, i32 %81)
store %struct.hostent* %82, %struct.hostent** %12
%83 = load %struct.hostent*, %struct.hostent** %12
%84 = icmp eq %struct.hostent* %83, null
br i1 %84, label %85, label %87
85: ; preds = %77
%86 = call i32 @error(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.5, i64 0, i64 0))
br label %87
87: ; preds = %85, %77
%88 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 0
%89 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %88, i32 0, i32 0
%90 = load i32, i32* %89
%91 = call i8* @inet_ntoa(i32 %90)
store i8* %91, i8** %14
%92 = load i8*, i8** %14
%93 = icmp eq i8* %92, null
br i1 %93, label %94, label %96
94: ; preds = %87
%95 = call i32 @error(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.6, i64 0, i64 0))
br label %96
96: ; preds = %94, %87
%97 = load %struct.hostent*, %struct.hostent** %12
%98 = getelementptr inbounds %struct.hostent, %struct.hostent* %97, i32 0, i32 0
%99 = load i8*, i8** %98
%100 = load i8*, i8** %14
%101 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.7, i64 0, i64 0), i8* %99, i8* %100)
%102 = getelementptr inbounds [1024 x i8], [1024 x i8]* %13, i64 0, i64 0
%103 = call i32 @bzero(i8* %102, i32 1024)
%104 = load i32, i32* %7
%105 = getelementptr inbounds [1024 x i8], [1024 x i8]* %13, i64 0, i64 0
%106 = call i32 @read(i32 %104, i8* %105, i32 1024)
store i32 %106, i32* %16
%107 = load i32, i32* %16
%108 = icmp slt i32 %107, 0
br i1 %108, label %109, label %111
109: ; preds = %96
%110 = call i32 @error(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.8, i64 0, i64 0))
br label %111
111: ; preds = %109, %96
%112 = load i32, i32* %16
%113 = getelementptr inbounds [1024 x i8], [1024 x i8]* %13, i64 0, i64 0
%114 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.9, i64 0, i64 0), i32 %112, i8* %113)
%115 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.10, i64 0, i64 0))
%116 = getelementptr inbounds [1024 x i8], [1024 x i8]* %13, i64 0, i64 0
%117 = call i32 @bzero(i8* %116, i32 1024)
%118 = getelementptr inbounds [1024 x i8], [1024 x i8]* %13, i64 0, i64 0
%119 = load i32, i32* @stdin
%120 = call i32 @fgets(i8* %118, i32 1024, i32 %119)
%121 = load i32, i32* %7
%122 = getelementptr inbounds [1024 x i8], [1024 x i8]* %13, i64 0, i64 0
%123 = getelementptr inbounds [1024 x i8], [1024 x i8]* %13, i64 0, i64 0
%124 = call i32 @strlen(i8* %123)
%125 = call i32 @write(i32 %121, i8* %122, i32 %124)
store i32 %125, i32* %16
%126 = load i32, i32* %16
%127 = icmp slt i32 %126, 0
br i1 %127, label %128, label %130
128: ; preds = %111
%129 = call i32 @error(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.11, i64 0, i64 0))
br label %130
130: ; preds = %128, %111
br label %69
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
%struct.hostent = type { i8* }
@stderr = global i32 0
@.str = [18 x i8] c"usage: %s <port>\0A\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [21 x i8] c"ERROR opening socket\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@INADDR_ANY = global i32 0
@.str.2 = [17 x i8] c"ERROR on binding\00"
@.str.3 = [16 x i8] c"ERROR on listen\00"
@.str.4 = [16 x i8] c"ERROR on accept\00"
@.str.5 = [23 x i8] c"ERROR on gethostbyaddr\00"
@.str.6 = [20 x i8] c"ERROR on inet_ntoa\0A\00"
@.str.7 = [44 x i8] c"server established connection with %s (%s)\0A\00"
@.str.8 = [26 x i8] c"ERROR reading from socket\00"
@.str.9 = [29 x i8] c"server received %d bytes: %s\00"
@.str.10 = [16 x i8] c"msg to client: \00"
@stdin = global i32 0
@.str.11 = [24 x i8] c"ERROR writing to socket\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = alloca %struct.sockaddr_in
%5 = alloca %struct.sockaddr_in
%6 = alloca [1024 x i8]
%7 = alloca i32
%8 = bitcast i32* %3 to i8*
%9 = bitcast %struct.sockaddr_in* %4 to i8*
%10 = bitcast %struct.sockaddr_in* %5 to i8*
%11 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%12 = bitcast i32* %7 to i8*
%13 = icmp eq i32 %0, 2
br i1 %13, label %19, label %14
14: ; preds = %2
%15 = load i32, i32* @stderr
%16 = load i8*, i8** %1
%17 = tail call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %16)
%18 = tail call i32 @exit(i32 1)
unreachable
19: ; preds = %2
%20 = getelementptr inbounds i8*, i8** %1, i64 1
%21 = load i8*, i8** %20
%22 = tail call i32 @atoi(i8* %21)
%23 = load i32, i32* @AF_INET
%24 = load i32, i32* @SOCK_STREAM
%25 = tail call i32 @socket(i32 %23, i32 %24, i32 0)
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %19
%28 = tail call i32 @error(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
br label %29
29: ; preds = %27, %19
store i32 1, i32* %7
%30 = load i32, i32* @SOL_SOCKET
%31 = load i32, i32* @SO_REUSEADDR
%32 = call i32 @setsockopt(i32 %25, i32 %30, i32 %31, i8* %12, i32 4)
%33 = call i32 @bzero(i8* %9, i32 12)
%34 = load i32, i32* @AF_INET
%35 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 2
store i32 %34, i32* %35
%36 = load i32, i32* @INADDR_ANY
%37 = call i32 @htonl(i32 %36)
%38 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0, i32 0
store i32 %37, i32* %38
%39 = trunc i32 %22 to i16
%40 = call i32 @htons(i16 zeroext %39)
%41 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1
store i32 %40, i32* %41
%42 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%43 = call i64 @bind(i32 %25, %struct.sockaddr* %42, i32 12)
%44 = icmp slt i64 %43, 0
br i1 %44, label %45, label %47
45: ; preds = %29
%46 = call i32 @error(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
br label %47
47: ; preds = %45, %29
%48 = call i64 @listen(i32 %25, i32 5)
%49 = icmp slt i64 %48, 0
br i1 %49, label %50, label %52
50: ; preds = %47
%51 = call i32 @error(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
br label %52
52: ; preds = %50, %47
store i32 12, i32* %3
%53 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%54 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 0, i32 0
br label %55
55: ; preds = %92, %52
%56 = call i32 @accept(i32 %25, %struct.sockaddr* %53, i32* %3)
%57 = icmp slt i32 %56, 0
br i1 %57, label %58, label %60
58: ; preds = %55
%59 = call i32 @error(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0))
br label %60
60: ; preds = %58, %55
%61 = load i32, i32* @AF_INET
%62 = call %struct.hostent* @gethostbyaddr(i8* %10, i32 4, i32 %61)
%63 = icmp eq %struct.hostent* %62, null
br i1 %63, label %64, label %66
64: ; preds = %60
%65 = call i32 @error(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.5, i64 0, i64 0))
br label %66
66: ; preds = %64, %60
%67 = load i32, i32* %54
%68 = call i8* @inet_ntoa(i32 %67)
%69 = icmp eq i8* %68, null
br i1 %69, label %70, label %72
70: ; preds = %66
%71 = call i32 @error(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.6, i64 0, i64 0))
br label %72
72: ; preds = %70, %66
%73 = getelementptr inbounds %struct.hostent, %struct.hostent* %62, i64 0, i32 0
%74 = load i8*, i8** %73
%75 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([44 x i8], [44 x i8]* @.str.7, i64 0, i64 0), i8* %74, i8* %68)
%76 = call i32 @bzero(i8* %11, i32 1024)
%77 = call i32 @read(i32 %56, i8* %11, i32 1024)
%78 = icmp slt i32 %77, 0
br i1 %78, label %79, label %81
79: ; preds = %72
%80 = call i32 @error(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.8, i64 0, i64 0))
br label %81
81: ; preds = %79, %72
%82 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str.9, i64 0, i64 0), i32 %77, i8* %11)
%83 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.10, i64 0, i64 0))
%84 = call i32 @bzero(i8* %11, i32 1024)
%85 = load i32, i32* @stdin
%86 = call i32 @fgets(i8* %11, i32 1024, i32 %85)
%87 = call i32 @strlen(i8* %11)
%88 = call i32 @write(i32 %56, i8* %11, i32 %87)
%89 = icmp slt i32 %88, 0
br i1 %89, label %90, label %92
90: ; preds = %81
%91 = call i32 @error(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.11, i64 0, i64 0))
br label %92
92: ; preds = %90, %81
br label %55
}