#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*/ 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 /*<<< orphan*/ process_buffer(char *, int, char *, unsigned short *);
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 write(int, ...);
int main(int argc, char **argv) {
  int parentfd;
  int childfd;
  int portno;
  int clientlen;
  struct sockaddr_in serveraddr;
  struct sockaddr_in clientaddr;
  struct hostent *hostp;
  char buf[512];
  char *hostaddrp;
  int optval;
  int n;
  int i;
  char recv_buf[8192];
  unsigned short recv_size;
  if (argc != 2) {
    fprintf(stderr, "usage: %s <port>\n", argv[0]);
    exit(1);
  }
  portno = atoi(argv[1]);
  parentfd = socket(AF_INET, SOCK_STREAM, 0);
  if (parentfd < 0)
    error("ERROR opening socket");
  optval = 1;
  setsockopt(parentfd, 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(parentfd, (struct sockaddr *)&serveraddr, sizeof(serveraddr)) < 0)
    error("ERROR on binding");
  if (listen(parentfd, 5) < 0)
    error("ERROR on listen");
  clientlen = sizeof(clientaddr);
  while (1) {
    childfd = accept(parentfd, (struct sockaddr *)&clientaddr, &clientlen);
    if (childfd < 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, 512);
    n = read(childfd, buf, 512);
    if (n < 0)
      error("   ERROR reading from socket");
    printf("   Server received %d bytes: [", n);
    for (i = 0; i < n; i++)
      printf("%02x:", buf[i]);
    printf("]\n");
    recv_size = 0;
    process_buffer(buf, n, recv_buf, &recv_size);
    if (recv_size > 0) {
      n = write(childfd, recv_buf, recv_size);
      if (n < 0)
        error("   ERROR writing to socket");
      printf("   Server response (%d): [", recv_size);
      for (i = 0; i < recv_size; i++)
        printf("%02x:", recv_buf[i]);
      printf("]\n");
    } else {
      printf("    No response from sever.\n");
      write(childfd, '\0', 1);
    }
    close(childfd);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x2280,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     1279 <main+0x49>
mov    0x2d6e(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xd96(%rip),%rsi
call   10b0 <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    0x2d27(%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,-0x25c(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2d19(%rip),%rax
mov    (%rax),%esi
mov    0x2ce0(%rip),%rax
mov    (%rax),%edx
lea    -0x25c(%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    0x2ccb(%rip),%rax
mov    (%rax),%edi
call   1080 <htonl@plt>
mov    %eax,-0x30(%rbp)
mov    -0x1c(%rbp),%eax
movzwl %ax,%edi
call   1060 <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,-0x258(%rbp)
cmpq   $0x0,-0x258(%rbp)
jne    13fa <main+0x1ca>
lea    0xc7a(%rip),%rdi
call   10f0 <error@plt>
mov    -0x48(%rbp),%rax
mov    (%rax),%rsi
mov    -0x258(%rbp),%rdx
lea    0xc74(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
lea    -0x250(%rbp),%rdi
mov    $0x200,%esi
call   1100 <bzero@plt>
mov    -0x18(%rbp),%edi
lea    -0x250(%rbp),%rsi
mov    $0x200,%edx
call   10a0 <read@plt>
mov    %eax,-0x260(%rbp)
cmpl   $0x0,-0x260(%rbp)
jge    145a <main+0x22a>
lea    0xc5e(%rip),%rdi
call   10f0 <error@plt>
mov    -0x260(%rbp),%esi
lea    0xc69(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
movl   $0x0,-0x264(%rbp)
mov    -0x264(%rbp),%eax
cmp    -0x260(%rbp),%eax
jge    14bb <main+0x28b>
movslq -0x264(%rbp),%rax
movsbl -0x250(%rbp,%rax,1),%esi
ff
lea    0xc4f(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x264(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x264(%rbp)
jmp    1478 <main+0x248>
lea    0xc33(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
movw   $0x0,-0x2272(%rbp)
lea    -0x250(%rbp),%rdi
mov    -0x260(%rbp),%esi
lea    -0x2270(%rbp),%rdx
lea    -0x2272(%rbp),%rcx
call   10c0 <process_buffer@plt>
movzwl -0x2272(%rbp),%eax
cmp    $0x0,%eax
jle    15b1 <main+0x381>
mov    -0x18(%rbp),%edi
lea    -0x2270(%rbp),%rsi
movzwl -0x2272(%rbp),%edx
mov    $0x0,%al
call   1040 <write@plt>
mov    %eax,-0x260(%rbp)
cmpl   $0x0,-0x260(%rbp)
jge    1539 <main+0x309>
lea    0xbc4(%rip),%rdi
call   10f0 <error@plt>
movzwl -0x2272(%rbp),%esi
lea    0xbcc(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
movl   $0x0,-0x264(%rbp)
mov    -0x264(%rbp),%eax
movzwl -0x2272(%rbp),%ecx
cmp    %ecx,%eax
jge    159e <main+0x36e>
movslq -0x264(%rbp),%rax
movsbl -0x2270(%rbp,%rax,1),%esi
ff
lea    0xb6c(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x264(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x264(%rbp)
jmp    1558 <main+0x328>
lea    0xb50(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
jmp    15d0 <main+0x3a0>
lea    0xb76(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x18(%rbp),%edi
xor    %esi,%esi
mov    $0x1,%edx
mov    $0x0,%al
call   1040 <write@plt>
mov    -0x18(%rbp),%edi
call   1090 <close@plt>
jmp    1376 <main+0x146>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x2238,%rsp
cmp    $0x2,%edi
jne    1551 <main+0x311>
mov    0x8(%rsi),%rdi
call   1130 <atoi@plt>
mov    %eax,%r14d
mov    0x2d73(%rip),%r12
mov    (%r12),%edi
mov    0x2d48(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1160 <socket@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    1293 <main+0x53>
lea    0xd84(%rip),%rdi
call   1100 <error@plt>
movl   $0x1,0x8(%rsp)
mov    0x2d46(%rip),%rax
mov    (%rax),%esi
mov    0x2d0d(%rip),%rax
mov    (%rax),%edx
lea    0x8(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
lea    0x20(%rsp),%r15
mov    %r15,%rdi
mov    $0xc,%esi
call   1110 <bzero@plt>
mov    (%r12),%eax
mov    %eax,0x28(%rsp)
mov    0x2cf8(%rip),%rax
mov    (%rax),%edi
call   1090 <htonl@plt>
mov    %eax,0x20(%rsp)
movzwl %r14w,%edi
call   1070 <htons@plt>
mov    %eax,0x24(%rsp)
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0xc,%edx
call   10f0 <bind@plt>
test   %rax,%rax
jns    1318 <main+0xd8>
lea    0xd14(%rip),%rdi
call   1100 <error@plt>
mov    %ebx,%edi
mov    $0x5,%esi
call   10e0 <listen@plt>
test   %rax,%rax
jns    1335 <main+0xf5>
lea    0xd08(%rip),%rdi
call   1100 <error@plt>
movl   $0xc,0xc(%rsp)
lea    0x10(%rsp),%rbp
lea    0x230(%rsp),%r14
lea    0xd9e(%rip),%r13
mov    %ebx,0x4(%rsp)
jmp    1395 <main+0x155>
nopw   0x0(%rax,%rax,1)
lea    0xdc4(%rip),%rdi
call   1030 <puts@plt>
mov    %r15d,%edi
xor    %esi,%esi
mov    $0x1,%edx
xor    %eax,%eax
call   1050 <write@plt>
mov    0x2c5c(%rip),%r12
lea    0x10(%rsp),%rbp
mov    %r15d,%edi
call   10a0 <close@plt>
mov    0x4(%rsp),%ebx
mov    %ebx,%edi
mov    %rbp,%rsi
lea    0xc(%rsp),%rdx
call   1120 <accept@plt>
mov    %eax,%r15d
test   %eax,%eax
jns    13b7 <main+0x177>
lea    0xc96(%rip),%rdi
call   1100 <error@plt>
mov    (%r12),%edx
mov    %rbp,%rdi
mov    $0x4,%esi
call   1140 <gethostbyaddr@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    13dc <main+0x19c>
lea    0xc81(%rip),%rdi
call   1100 <error@plt>
mov    %r14,%r12
mov    0x10(%rsp),%edi
call   1060 <inet_ntoa@plt>
mov    %rax,%rbp
test   %rax,%rax
jne    13fc <main+0x1bc>
lea    0xc78(%rip),%rdi
call   1100 <error@plt>
mov    (%rbx),%rsi
lea    0xc7d(%rip),%rdi
mov    %rbp,%rdx
xor    %eax,%eax
call   1080 <printf@plt>
lea    0x30(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x200,%esi
call   1110 <bzero@plt>
mov    %r15d,%edi
mov    %rbx,%rsi
mov    $0x200,%edx
call   10b0 <read@plt>
mov    %eax,%r14d
test   %eax,%eax
js     1480 <main+0x240>
lea    0xc90(%rip),%rdi
mov    %r14d,%esi
xor    %eax,%eax
call   1080 <printf@plt>
test   %r14d,%r14d
je     149d <main+0x25d>
mov    %r14d,%ebx
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
movsbl 0x30(%rsp,%rbp,1),%esi
mov    %r13,%rdi
xor    %eax,%eax
call   1080 <printf@plt>
add    $0x1,%rbp
cmp    %rbp,%rbx
jne    1460 <main+0x220>
jmp    149d <main+0x25d>
nopw   0x0(%rax,%rax,1)
lea    0xc2c(%rip),%rdi
call   1100 <error@plt>
lea    0xc3d(%rip),%rdi
mov    %r14d,%esi
xor    %eax,%eax
call   1080 <printf@plt>
lea    0xca3(%rip),%rdi
call   1030 <puts@plt>
movw   $0x0,0x2(%rsp)
lea    0x30(%rsp),%rdi
mov    %r14d,%esi
mov    %r12,%r14
mov    %r12,%rdx
lea    0x2(%rsp),%rcx
call   10d0 <process_buffer@plt>
movzwl 0x2(%rsp),%edx
test   %edx,%edx
je     1360 <main+0x120>
mov    %r15d,%edi
mov    %r14,%rsi
xor    %eax,%eax
call   1050 <write@plt>
test   %eax,%eax
mov    0x2af5(%rip),%r12
lea    0x10(%rsp),%rbp
jns    14fe <main+0x2be>
lea    0xbfc(%rip),%rdi
call   1100 <error@plt>
movzwl 0x2(%rsp),%esi
lea    0xc06(%rip),%rdi
xor    %eax,%eax
call   1080 <printf@plt>
cmpw   $0x0,0x2(%rsp)
je     1540 <main+0x300>
xor    %ebx,%ebx
nopl   0x0(%rax,%rax,1)
movsbl 0x230(%rsp,%rbx,1),%esi
mov    %r13,%rdi
xor    %eax,%eax
call   1080 <printf@plt>
add    $0x1,%rbx
movzwl 0x2(%rsp),%eax
cmp    %rax,%rbx
jb     1520 <main+0x2e0>
lea    0xc00(%rip),%rdi
call   1030 <puts@plt>
jmp    1389 <main+0x149>
mov    0x2a70(%rip),%rax
mov    (%rax),%edi
mov    (%rsi),%rdx
lea    0xa9c(%rip),%rsi
call   10c0 <fprintf@plt>
mov    $0x1,%edi
call   1150 <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 = [48 x i8] c"--> Server established connection with %s (%s)\0A\00"
@.str.8 = [29 x i8] c" ERROR reading from socket\00"
@.str.9 = [31 x i8] c" Server received %d bytes: [\00"
@.str.10 = [6 x i8] c"%02x:\00"
@.str.11 = [3 x i8] c"]\0A\00"
@.str.12 = [27 x i8] c" ERROR writing to socket\00"
@.str.13 = [27 x i8] c" Server response (%d): [\00"
@.str.14 = [29 x i8] c" No response from sever.\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 %struct.sockaddr_in
%11 = alloca %struct.sockaddr_in
%12 = alloca %struct.hostent*
%13 = alloca [512 x i8]
%14 = alloca i8*
%15 = alloca i32
%16 = alloca i32
%17 = alloca i32
%18 = alloca [8192 x i8]
%19 = alloca i16
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%20 = load i32, i32* %4
%21 = icmp ne i32 %20, 2
br i1 %21, label %22, label %29
22: ; preds = %2
%23 = load i32, i32* @stderr
%24 = load i8**, i8*** %5
%25 = getelementptr inbounds i8*, i8** %24, i64 0
%26 = load i8*, i8** %25
%27 = call i32 @fprintf(i32 %23, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %26)
%28 = call i32 @exit(i32 1)
unreachable
29: ; preds = %2
%30 = load i8**, i8*** %5
%31 = getelementptr inbounds i8*, i8** %30, i64 1
%32 = load i8*, i8** %31
%33 = call i32 @atoi(i8* %32)
store i32 %33, i32* %8
%34 = load i32, i32* @AF_INET
%35 = load i32, i32* @SOCK_STREAM
%36 = call i32 @socket(i32 %34, i32 %35, i32 0)
store i32 %36, i32* %6
%37 = load i32, i32* %6
%38 = icmp slt i32 %37, 0
br i1 %38, label %39, label %41
39: ; preds = %29
%40 = call i32 @error(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
br label %41
41: ; preds = %39, %29
store i32 1, i32* %15
%42 = load i32, i32* %6
%43 = load i32, i32* @SOL_SOCKET
%44 = load i32, i32* @SO_REUSEADDR
%45 = bitcast i32* %15 to i8*
%46 = call i32 @setsockopt(i32 %42, i32 %43, i32 %44, i8* %45, i32 4)
%47 = bitcast %struct.sockaddr_in* %10 to i8*
%48 = call i32 @bzero(i8* %47, i32 12)
%49 = load i32, i32* @AF_INET
%50 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 2
store i32 %49, i32* %50
%51 = load i32, i32* @INADDR_ANY
%52 = call i32 @htonl(i32 %51)
%53 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 0
%54 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %53, i32 0, i32 0
store i32 %52, i32* %54
%55 = load i32, i32* %8
%56 = trunc i32 %55 to i16
%57 = call i32 @htons(i16 zeroext %56)
%58 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 1
store i32 %57, i32* %58
%59 = load i32, i32* %6
%60 = bitcast %struct.sockaddr_in* %10 to %struct.sockaddr*
%61 = call i64 @bind(i32 %59, %struct.sockaddr* %60, i32 12)
%62 = icmp slt i64 %61, 0
br i1 %62, label %63, label %65
63: ; preds = %41
%64 = call i32 @error(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
br label %65
65: ; preds = %63, %41
%66 = load i32, i32* %6
%67 = call i64 @listen(i32 %66, i32 5)
%68 = icmp slt i64 %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.3, i64 0, i64 0))
br label %71
71: ; preds = %69, %65
store i32 12, i32* %9
br label %72
72: ; preds = %71, %175
%73 = load i32, i32* %6
%74 = bitcast %struct.sockaddr_in* %11 to %struct.sockaddr*
%75 = call i32 @accept(i32 %73, %struct.sockaddr* %74, i32* %9)
store i32 %75, i32* %7
%76 = load i32, i32* %7
%77 = icmp slt i32 %76, 0
br i1 %77, label %78, label %80
78: ; preds = %72
%79 = call i32 @error(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0))
br label %80
80: ; preds = %78, %72
%81 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 0
%82 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %81, i32 0, i32 0
%83 = bitcast i32* %82 to i8*
%84 = load i32, i32* @AF_INET
%85 = call %struct.hostent* @gethostbyaddr(i8* %83, i32 4, i32 %84)
store %struct.hostent* %85, %struct.hostent** %12
%86 = load %struct.hostent*, %struct.hostent** %12
%87 = icmp eq %struct.hostent* %86, null
br i1 %87, label %88, label %90
88: ; preds = %80
%89 = call i32 @error(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.5, i64 0, i64 0))
br label %90
90: ; preds = %88, %80
%91 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 0
%92 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %91, i32 0, i32 0
%93 = load i32, i32* %92
%94 = call i8* @inet_ntoa(i32 %93)
store i8* %94, i8** %14
%95 = load i8*, i8** %14
%96 = icmp eq i8* %95, null
br i1 %96, label %97, label %99
97: ; preds = %90
%98 = call i32 @error(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.6, i64 0, i64 0))
br label %99
99: ; preds = %97, %90
%100 = load %struct.hostent*, %struct.hostent** %12
%101 = getelementptr inbounds %struct.hostent, %struct.hostent* %100, i32 0, i32 0
%102 = load i8*, i8** %101
%103 = load i8*, i8** %14
%104 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.7, i64 0, i64 0), i8* %102, i8* %103)
%105 = getelementptr inbounds [512 x i8], [512 x i8]* %13, i64 0, i64 0
%106 = call i32 @bzero(i8* %105, i32 512)
%107 = load i32, i32* %7
%108 = getelementptr inbounds [512 x i8], [512 x i8]* %13, i64 0, i64 0
%109 = call i32 @read(i32 %107, i8* %108, i32 512)
store i32 %109, i32* %16
%110 = load i32, i32* %16
%111 = icmp slt i32 %110, 0
br i1 %111, label %112, label %114
112: ; preds = %99
%113 = call i32 @error(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.8, i64 0, i64 0))
br label %114
114: ; preds = %112, %99
%115 = load i32, i32* %16
%116 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.9, i64 0, i64 0), i32 %115)
store i32 0, i32* %17
br label %117
117: ; preds = %128, %114
%118 = load i32, i32* %17
%119 = load i32, i32* %16
%120 = icmp slt i32 %118, %119
br i1 %120, label %121, label %131
121: ; preds = %117
%122 = load i32, i32* %17
%123 = sext i32 %122 to i64
%124 = getelementptr inbounds [512 x i8], [512 x i8]* %13, i64 0, i64 %123
%125 = load i8, i8* %124
%126 = sext i8 %125 to i32
%127 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.10, i64 0, i64 0), i32 %126)
br label %128
128: ; preds = %121
%129 = load i32, i32* %17
%130 = add nsw i32 %129, 1
store i32 %130, i32* %17
br label %117
131: ; preds = %117
%132 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.11, i64 0, i64 0))
store i16 0, i16* %19
%133 = getelementptr inbounds [512 x i8], [512 x i8]* %13, i64 0, i64 0
%134 = load i32, i32* %16
%135 = getelementptr inbounds [8192 x i8], [8192 x i8]* %18, i64 0, i64 0
%136 = call i32 @process_buffer(i8* %133, i32 %134, i8* %135, i16* %19)
%137 = load i16, i16* %19
%138 = zext i16 %137 to i32
%139 = icmp sgt i32 %138, 0
br i1 %139, label %140, label %171
140: ; preds = %131
%141 = load i32, i32* %7
%142 = getelementptr inbounds [8192 x i8], [8192 x i8]* %18, i64 0, i64 0
%143 = load i16, i16* %19
%144 = zext i16 %143 to i32
%145 = call i32 (i32, ...) @write(i32 %141, i8* %142, i32 %144)
store i32 %145, i32* %16
%146 = load i32, i32* %16
%147 = icmp slt i32 %146, 0
br i1 %147, label %148, label %150
148: ; preds = %140
%149 = call i32 @error(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.12, i64 0, i64 0))
br label %150
150: ; preds = %148, %140
%151 = load i16, i16* %19
%152 = zext i16 %151 to i32
%153 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.13, i64 0, i64 0), i32 %152)
store i32 0, i32* %17
br label %154
154: ; preds = %166, %150
%155 = load i32, i32* %17
%156 = load i16, i16* %19
%157 = zext i16 %156 to i32
%158 = icmp slt i32 %155, %157
br i1 %158, label %159, label %169
159: ; preds = %154
%160 = load i32, i32* %17
%161 = sext i32 %160 to i64
%162 = getelementptr inbounds [8192 x i8], [8192 x i8]* %18, i64 0, i64 %161
%163 = load i8, i8* %162
%164 = sext i8 %163 to i32
%165 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.10, i64 0, i64 0), i32 %164)
br label %166
166: ; preds = %159
%167 = load i32, i32* %17
%168 = add nsw i32 %167, 1
store i32 %168, i32* %17
br label %154
169: ; preds = %154
%170 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.11, i64 0, i64 0))
br label %175
171: ; preds = %131
%172 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.14, i64 0, i64 0))
%173 = load i32, i32* %7
%174 = call i32 (i32, ...) @write(i32 %173, i32 0, i32 1)
br label %175
175: ; preds = %171, %169
%176 = load i32, i32* %7
%177 = call i32 @close(i32 %176)
br label %72
}

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