#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 */

/* Type definitions */
struct sockaddr_storage {
  int /*<<< orphan*/ ss_family;
};
struct sockaddr {
  int dummy;
};
struct addrinfo {
  int /*<<< orphan*/ ai_addrlen;
  int /*<<< orphan*/ ai_addr;
  int /*<<< orphan*/ ai_protocol;
  int /*<<< orphan*/ ai_socktype;
  int /*<<< orphan*/ ai_family;
  int /*<<< orphan*/ ai_flags;
};
typedef int socklen_t;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ AI_PASSIVE;
int INET6_ADDRSTRLEN;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
int accept(int, struct sockaddr *, int *);
int atoi(int /*<<< orphan*/);
int bind(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
char *gai_strerror(int);
int /*<<< orphan*/ get_in_addr(struct sockaddr *);
int getaddrinfo(int /*<<< orphan*/ *, char *, struct addrinfo *,
                struct addrinfo **);
int getopt(int, char **, char *);
int /*<<< orphan*/ inet_ntop(int /*<<< orphan*/, int /*<<< orphan*/, char *,
                             int);
int listen(int, int);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int /*<<< orphan*/ optarg;
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int recv(int, char *, int, int /*<<< orphan*/);
int send(int, char *, int, int /*<<< orphan*/);
int setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int *, int);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
int main(int argc, char **argv) {
  int sockfd, new_fd;
  int option_char;
  int portno = 8888;
  char port_char[15];
  int maxnpending = 5;
  struct addrinfo hints, *servinfo;
  struct sockaddr_storage their_addr;
  socklen_t sin_size;
  char message_buff[16];
  int num_bytes;
  int res;
  int yes;
  char ip_info[INET6_ADDRSTRLEN];
  while ((option_char = getopt(argc, argv, "p:n:h")) != -1) {
    switch (option_char) {
    case 'p':
      portno = atoi(optarg);
      break;
    case 'n':
      maxnpending = atoi(optarg);
      break;
    case 'h':
      fprintf(stdout, "%s",
              "usage:\n"
              "  echoserver [options]\n"
              "options:\n"
              "  -p                  Port (Default: 8888)\n"
              "  -n                  Maximum pending connections\n"
              "  -h                  Show this help message\n");
      exit(0);
      break;
    default:
      fprintf(stderr, "%s",
              "usage:\n"
              "  echoserver [options]\n"
              "options:\n"
              "  -p                  Port (Default: 8888)\n"
              "  -n                  Maximum pending connections\n"
              "  -h                  Show this help message\n");
      exit(1);
    }
  }
  memset(&hints, 0, sizeof hints);
  hints.ai_family = AF_INET;
  hints.ai_socktype = SOCK_STREAM;
  hints.ai_flags = AI_PASSIVE;
  snprintf(port_char, sizeof port_char, "%d", portno);
  if ((res = getaddrinfo(NULL, port_char, &hints, &servinfo)) != 0) {
    fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(res));
    return 1;
  }
  printf("getaddrinfo is executed\n");
  if ((sockfd = socket(servinfo->ai_family, servinfo->ai_socktype,
                       servinfo->ai_protocol)) == -1) {
    perror("server: socket");
    exit(1);
  }
  printf("socket is created\n");
  if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1) {
    perror("setsockopt");
    exit(1);
  }
  printf("zombies are killed\n");
  if (bind(sockfd, servinfo->ai_addr, servinfo->ai_addrlen) == -1) {
    close(sockfd);
    perror("server:bind");
    exit(1);
  }
  printf("bind to socket\n");
  if (listen(sockfd, maxnpending) == -1) {
    perror("listen");
    exit(1);
  }
  printf("server: waiting for connections...\n");
  while (1) {
    sin_size = sizeof their_addr;
    if ((new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size)) ==
        -1) {
      perror("accept");
      exit(1);
    }
    inet_ntop(their_addr.ss_family, get_in_addr((struct sockaddr *)&their_addr),
              ip_info, sizeof ip_info);
    printf("server: got connection from %s\n", ip_info);
    if ((num_bytes = recv(new_fd, message_buff, 16 - 1, 0)) == -1) {
      perror("recv");
      exit(1);
    }
    message_buff[num_bytes] = '\0';
    printf("server: i've got this message from client: %s\n", message_buff);
    if (send(new_fd, message_buff, num_bytes, 0) == -1) {
      perror("send");
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xc0,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x22b8,-0x20(%rbp)
movl   $0x5,-0x34(%rbp)
mov    0x2d6a(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0xb0(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x98(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0xa8(%rbp)
mov    %rcx,%rsp
mov    %rax,-0xa0(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
lea    0xd4f(%rip),%rdx
call   1110 <getopt@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0xffffffff,%eax
je     137b <main+0x13b>
mov    -0x1c(%rbp),%eax
mov    %eax,-0xb4(%rbp)
sub    $0x68,%eax
je     132d <main+0xed>
jmp    12d9 <main+0x99>
mov    -0xb4(%rbp),%eax
sub    $0x6e,%eax
je     1317 <main+0xd7>
jmp    12ed <main+0xad>
mov    -0xb4(%rbp),%eax
sub    $0x70,%eax
jne    1350 <main+0x110>
jmp    1301 <main+0xc1>
mov    0x2cf0(%rip),%rax
mov    (%rax),%edi
call   1130 <atoi@plt>
mov    %eax,-0x20(%rbp)
jmp    1376 <main+0x136>
mov    0x2cda(%rip),%rax
mov    (%rax),%edi
call   1130 <atoi@plt>
mov    %eax,-0x34(%rbp)
jmp    1376 <main+0x136>
mov    0x2c74(%rip),%rax
mov    (%rax),%edi
lea    0xcc9(%rip),%rsi
lea    0xcc5(%rip),%rdx
call   10d0 <fprintf@plt>
xor    %edi,%edi
call   1140 <exit@plt>
mov    0x2c61(%rip),%rax
mov    (%rax),%edi
lea    0xca6(%rip),%rsi
lea    0xca2(%rip),%rdx
call   10d0 <fprintf@plt>
mov    $0x1,%edi
call   1140 <exit@plt>
jmp    12a3 <main+0x63>
lea    -0x50(%rbp),%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   10b0 <memset@plt>
mov    0x2c36(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x40(%rbp)
mov    0x2c12(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
mov    0x2c26(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
lea    -0x2f(%rbp),%rdi
mov    -0x20(%rbp),%ecx
mov    $0xf,%esi
lea    0xcf9(%rip),%rdx
call   1090 <snprintf@plt>
lea    -0x2f(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edi
lea    -0x50(%rbp),%rdx
lea    -0x58(%rbp),%rcx
call   1150 <getaddrinfo@plt>
mov    %eax,-0x88(%rbp)
cmp    $0x0,%eax
je     1433 <main+0x1f3>
mov    0x2bc6(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xb8(%rbp)
mov    -0x88(%rbp),%edi
call   10a0 <gai_strerror@plt>
mov    -0xb8(%rbp),%edi
mov    %rax,%rdx
lea    0xca9(%rip),%rsi
call   10d0 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x98(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret
lea    0xc95(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
mov    -0x58(%rbp),%rax
mov    0x10(%rax),%edi
mov    -0x58(%rbp),%rax
mov    0xc(%rax),%esi
mov    -0x58(%rbp),%rax
mov    0x8(%rax),%edx
call   1160 <socket@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    147d <main+0x23d>
lea    0xc7a(%rip),%rdi
call   1100 <perror@plt>
mov    $0x1,%edi
call   1140 <exit@plt>
lea    0xc73(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
mov    -0x14(%rbp),%edi
mov    0x2b4b(%rip),%rax
mov    (%rax),%esi
mov    0x2b02(%rip),%rax
mov    (%rax),%edx
lea    -0x8c(%rbp),%rcx
mov    $0x4,%r8d
call   1060 <setsockopt@plt>
cmp    $0xffffffff,%eax
jne    14d1 <main+0x291>
lea    0xc48(%rip),%rdi
call   1100 <perror@plt>
mov    $0x1,%edi
call   1140 <exit@plt>
lea    0xc3d(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
mov    -0x14(%rbp),%edi
mov    -0x58(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x58(%rbp),%rax
mov    (%rax),%edx
call   10f0 <bind@plt>
cmp    $0xffffffff,%eax
jne    151b <main+0x2db>
mov    -0x14(%rbp),%edi
call   10c0 <close@plt>
lea    0xc1d(%rip),%rdi
call   1100 <perror@plt>
mov    $0x1,%edi
call   1140 <exit@plt>
lea    0xc13(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
mov    -0x14(%rbp),%edi
mov    -0x34(%rbp),%esi
call   10e0 <listen@plt>
cmp    $0xffffffff,%eax
jne    1553 <main+0x313>
lea    0xc01(%rip),%rdi
call   1100 <perror@plt>
mov    $0x1,%edi
call   1140 <exit@plt>
lea    0xbf2(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
jmp    1566 <main+0x326>
movl   $0x4,-0x64(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x60(%rbp),%rsi
lea    -0x64(%rbp),%rdx
call   1120 <accept@plt>
mov    %eax,-0x18(%rbp)
cmp    $0xffffffff,%eax
jne    159f <main+0x35f>
lea    0xbe0(%rip),%rdi
call   1100 <perror@plt>
mov    $0x1,%edi
call   1140 <exit@plt>
mov    -0x60(%rbp),%eax
mov    %eax,-0xbc(%rbp)
lea    -0x60(%rbp),%rdi
call   1050 <get_in_addr@plt>
mov    -0xbc(%rbp),%edi
mov    -0xa8(%rbp),%rdx
mov    %eax,%esi
mov    -0xb0(%rbp),%rax
mov    %eax,%ecx
call   1030 <inet_ntop@plt>
mov    -0xa8(%rbp),%rsi
lea    0xb9b(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
mov    -0x18(%rbp),%edi
lea    -0x80(%rbp),%rsi
mov    $0xf,%edx
xor    %ecx,%ecx
call   1040 <recv@plt>
mov    %eax,-0x84(%rbp)
cmp    $0xffffffff,%eax
jne    161b <main+0x3db>
lea    0xb8b(%rip),%rdi
call   1100 <perror@plt>
mov    $0x1,%edi
call   1140 <exit@plt>
movslq -0x84(%rbp),%rax
movb   $0x0,-0x80(%rbp,%rax,1)
lea    -0x80(%rbp),%rsi
lea    0xb6a(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
mov    -0x18(%rbp),%edi
lea    -0x80(%rbp),%rsi
mov    -0x84(%rbp),%edx
xor    %ecx,%ecx
call   1070 <send@plt>
cmp    $0xffffffff,%eax
jne    1662 <main+0x422>
lea    0xb6e(%rip),%rdi
call   1100 <perror@plt>
jmp    1561 <main+0x321>

x86 assembly (clang, -O2):
<main>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x78,%rsp
mov    %rsi,%rbx
mov    %edi,%r14d
mov    %rsp,-0x30(%rbp)
mov    0x2d66(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rax,-0x58(%rbp)
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rcx
mov    %rcx,-0x50(%rbp)
mov    %rcx,%rsp
mov    $0x22b8,%r13d
movl   $0x5,-0x3c(%rbp)
lea    0xd5f(%rip),%r12
mov    0x2d50(%rip),%r15
jmp    12bb <main+0x6b>
nopw   0x0(%rax,%rax,1)
mov    (%r15),%edi
call   1140 <atoi@plt>
mov    %eax,%r13d
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %r12,%rdx
call   1120 <getopt@plt>
cmp    $0x70,%eax
je     12b0 <main+0x60>
cmp    $0xffffffff,%eax
je     12e9 <main+0x99>
cmp    $0x6e,%eax
jne    1390 <main+0x140>
mov    (%r15),%edi
call   1140 <atoi@plt>
mov    %eax,-0x3c(%rbp)
jmp    12bb <main+0x6b>
lea    -0x78(%rbp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   10c0 <memset@plt>
mov    0x2cc5(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x68(%rbp)
mov    0x2ca1(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x6c(%rbp)
mov    0x2cb5(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x64(%rbp)
lea    0xd94(%rip),%rdx
lea    -0x9f(%rbp),%rbx
mov    %rbx,%rdi
mov    $0xf,%esi
mov    %r13d,%ecx
call   10a0 <snprintf@plt>
lea    -0x48(%rbp),%rcx
xor    %edi,%edi
mov    %rbx,%rsi
mov    %r14,%rdx
call   1160 <getaddrinfo@plt>
test   %eax,%eax
je     13de <main+0x18e>
mov    0x2c5a(%rip),%rcx
mov    (%rcx),%ebx
mov    %eax,%edi
call   10b0 <gai_strerror@plt>
lea    0xd50(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   10e0 <fprintf@plt>
mov    -0x30(%rbp),%rsp
mov    $0x1,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
cmp    $0x68,%eax
jne    13b8 <main+0x168>
mov    0x2c0c(%rip),%rax
mov    (%rax),%edi
lea    0xc61(%rip),%rsi
lea    0xc5d(%rip),%rdx
call   10e0 <fprintf@plt>
xor    %edi,%edi
call   1150 <exit@plt>
mov    0x2bf9(%rip),%rax
mov    (%rax),%edi
lea    0xc3e(%rip),%rsi
lea    0xc3a(%rip),%rdx
call   10e0 <fprintf@plt>
mov    $0x1,%edi
call   1150 <exit@plt>
lea    0xd77(%rip),%rdi
call   1060 <puts@plt>
mov    -0x48(%rbp),%rax
mov    0x10(%rax),%edi
mov    0x8(%rax),%edx
mov    0xc(%rax),%esi
call   1170 <socket@plt>
mov    %eax,-0x30(%rbp)
cmp    $0xffffffff,%eax
jne    1410 <main+0x1c0>
lea    0xcc4(%rip),%rdi
jmp    15a9 <main+0x359>
lea    0xd5d(%rip),%rdi
call   1060 <puts@plt>
mov    0x2bbd(%rip),%rax
mov    (%rax),%esi
mov    0x2b74(%rip),%rax
mov    (%rax),%edx
lea    -0x5c(%rbp),%rcx
mov    -0x30(%rbp),%edi
mov    $0x4,%r8d
call   1070 <setsockopt@plt>
cmp    $0xffffffff,%eax
jne    1451 <main+0x201>
lea    0xc92(%rip),%rdi
jmp    15a9 <main+0x359>
lea    0xd2e(%rip),%rdi
call   1060 <puts@plt>
mov    -0x48(%rbp),%rax
mov    (%rax),%edx
mov    0x4(%rax),%esi
mov    -0x30(%rbp),%edi
call   1100 <bind@plt>
cmp    $0xffffffff,%eax
jne    1487 <main+0x237>
mov    -0x30(%rbp),%edi
call   10d0 <close@plt>
lea    0xc67(%rip),%rdi
jmp    15a9 <main+0x359>
lea    0xd0b(%rip),%rdi
call   1060 <puts@plt>
mov    -0x30(%rbp),%edi
mov    -0x3c(%rbp),%esi
call   10f0 <listen@plt>
cmp    $0xffffffff,%eax
jne    14af <main+0x25f>
lea    0xc4b(%rip),%rdi
jmp    15a9 <main+0x359>
lea    0xcf2(%rip),%rdi
call   1060 <puts@plt>
movl   $0x4,-0x34(%rbp)
lea    -0x38(%rbp),%rsi
lea    -0x34(%rbp),%rdx
mov    -0x30(%rbp),%edi
call   1130 <accept@plt>
cmp    $0xffffffff,%eax
jne    14e3 <main+0x293>
lea    0xc1e(%rip),%rdi
jmp    15a9 <main+0x359>
mov    %eax,%r15d
lea    -0x38(%rbp),%r12
lea    -0x90(%rbp),%r13
mov    -0x50(%rbp),%rbx
jmp    1515 <main+0x2c5>
movl   $0x4,-0x34(%rbp)
mov    -0x30(%rbp),%edi
mov    %r12,%rsi
lea    -0x34(%rbp),%rdx
call   1130 <accept@plt>
mov    %eax,%r15d
cmp    $0xffffffff,%eax
je     14d7 <main+0x287>
mov    -0x38(%rbp),%r14d
mov    %r12,%rdi
call   1050 <get_in_addr@plt>
mov    %r14d,%edi
mov    %eax,%esi
mov    %rbx,%rdx
mov    -0x58(%rbp),%rcx
call   1030 <inet_ntop@plt>
lea    0xbca(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1090 <printf@plt>
mov    %r15d,%edi
mov    %r13,%rsi
mov    $0xf,%edx
xor    %ecx,%ecx
call   1040 <recv@plt>
cmp    $0xffffffff,%eax
je     15a2 <main+0x352>
mov    %eax,%r14d
cltq
movb   $0x0,-0x90(%rbp,%rax,1)
lea    0xbba(%rip),%rdi
mov    %r13,%rsi
xor    %eax,%eax
call   1090 <printf@plt>
mov    %r15d,%edi
mov    %r13,%rsi
mov    %r14d,%edx
xor    %ecx,%ecx
call   1080 <send@plt>
cmp    $0xffffffff,%eax
jne    14f7 <main+0x2a7>
lea    0xbbf(%rip),%rdi
call   1110 <perror@plt>
jmp    14f7 <main+0x2a7>
lea    0xb7a(%rip),%rdi
call   1110 <perror@plt>
mov    $0x1,%edi
call   1150 <exit@plt>

LLVM IR (-O0):
%struct.addrinfo = type { i32, i32, i32, i32, i32, i32 }
%struct.sockaddr_storage = type { i32 }
%struct.sockaddr = type { i32 }
@INET6_ADDRSTRLEN = global i32 0
@.str = [6 x i8] c"p:n:h\00"
@optarg = global i32 0
@stdout = global i32 0
@.str.1 = [3 x i8] c"%s\00"
@.str.2 = [178 x i8] c"usage:\0A echoserver [options]\0Aoptions:\0A -p Port (Default: 8888)\0A -n Maximum pending connections\0A -h Show this help message\0A\00"
@stderr = global i32 0
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@AI_PASSIVE = global i32 0
@.str.3 = [3 x i8] c"%d\00"
@.str.4 = [17 x i8] c"getaddrinfo: %s\0A\00"
@.str.5 = [25 x i8] c"getaddrinfo is executed\0A\00"
@.str.6 = [15 x i8] c"server: socket\00"
@.str.7 = [19 x i8] c"socket is created\0A\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.8 = [11 x i8] c"setsockopt\00"
@.str.9 = [20 x i8] c"zombies are killed\0A\00"
@.str.10 = [12 x i8] c"server:bind\00"
@.str.11 = [16 x i8] c"bind to socket\0A\00"
@.str.12 = [7 x i8] c"listen\00"
@.str.13 = [36 x i8] c"server: waiting for connections...\0A\00"
@.str.14 = [7 x i8] c"accept\00"
@.str.15 = [32 x i8] c"server: got connection from %s\0A\00"
@.str.16 = [5 x i8] c"recv\00"
@.str.17 = [47 x i8] c"server: i've got this message from client: %s\0A\00"
@.str.18 = [5 x i8] c"send\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 [15 x i8]
%11 = alloca i32
%12 = alloca %struct.addrinfo
%13 = alloca %struct.addrinfo*
%14 = alloca %struct.sockaddr_storage
%15 = alloca i32
%16 = alloca [16 x i8]
%17 = alloca i32
%18 = alloca i32
%19 = alloca i32
%20 = alloca i8*
%21 = alloca i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 8888, i32* %9
store i32 5, i32* %11
%22 = load i32, i32* @INET6_ADDRSTRLEN
%23 = zext i32 %22 to i64
%24 = call i8* @llvm.stacksave()
store i8* %24, i8** %20
%25 = alloca i8, i64 %23
store i64 %23, i64* %21
br label %26
26: ; preds = %47, %2
%27 = load i32, i32* %4
%28 = load i8**, i8*** %5
%29 = call i32 @getopt(i32 %27, i8** %28, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
store i32 %29, i32* %8
%30 = icmp ne i32 %29, -1
br i1 %30, label %31, label %48
31: ; preds = %26
%32 = load i32, i32* %8
switch i32 %32, label %43 [
i32 112, label %33
i32 110, label %36
i32 104, label %39
]
33: ; preds = %31
%34 = load i32, i32* @optarg
%35 = call i32 @atoi(i32 %34)
store i32 %35, i32* %9
br label %47
36: ; preds = %31
%37 = load i32, i32* @optarg
%38 = call i32 @atoi(i32 %37)
store i32 %38, i32* %11
br label %47
39: ; preds = %31
%40 = load i32, i32* @stdout
%41 = call i32 @fprintf(i32 %40, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([178 x i8], [178 x i8]* @.str.2, i64 0, i64 0))
%42 = call i32 @exit(i32 0)
unreachable
43: ; preds = %31
%44 = load i32, i32* @stderr
%45 = call i32 @fprintf(i32 %44, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([178 x i8], [178 x i8]* @.str.2, i64 0, i64 0))
%46 = call i32 @exit(i32 1)
unreachable
47: ; preds = %36, %33
br label %26
48: ; preds = %26
%49 = call i32 @memset(%struct.addrinfo* %12, i32 0, i32 24)
%50 = load i32, i32* @AF_INET
%51 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %12, i32 0, i32 4
store i32 %50, i32* %51
%52 = load i32, i32* @SOCK_STREAM
%53 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %12, i32 0, i32 3
store i32 %52, i32* %53
%54 = load i32, i32* @AI_PASSIVE
%55 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %12, i32 0, i32 5
store i32 %54, i32* %55
%56 = getelementptr inbounds [15 x i8], [15 x i8]* %10, i64 0, i64 0
%57 = load i32, i32* %9
%58 = call i32 @snprintf(i8* %56, i32 15, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32 %57)
%59 = getelementptr inbounds [15 x i8], [15 x i8]* %10, i64 0, i64 0
%60 = call i32 @getaddrinfo(i32* null, i8* %59, %struct.addrinfo* %12, %struct.addrinfo** %13)
store i32 %60, i32* %18
%61 = icmp ne i32 %60, 0
br i1 %61, label %62, label %69
62: ; preds = %48
%63 = load i32, i32* @stderr
%64 = load i32, i32* %18
%65 = call i8* @gai_strerror(i32 %64)
%66 = call i32 @fprintf(i32 %63, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0), i8* %65)
store i32 1, i32* %3
%67 = load i8*, i8** %20
%68 = load i32, i32* %3
ret i32 %68
69: ; preds = %48
%70 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.5, i64 0, i64 0))
%71 = load %struct.addrinfo*, %struct.addrinfo** %13
%72 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %71, i32 0, i32 4
%73 = load i32, i32* %72
%74 = load %struct.addrinfo*, %struct.addrinfo** %13
%75 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %74, i32 0, i32 3
%76 = load i32, i32* %75
%77 = load %struct.addrinfo*, %struct.addrinfo** %13
%78 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %77, i32 0, i32 2
%79 = load i32, i32* %78
%80 = call i32 @socket(i32 %73, i32 %76, i32 %79)
store i32 %80, i32* %6
%81 = icmp eq i32 %80, -1
br i1 %81, label %82, label %85
82: ; preds = %69
%83 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.6, i64 0, i64 0))
%84 = call i32 @exit(i32 1)
unreachable
85: ; preds = %69
%86 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.7, i64 0, i64 0))
%87 = load i32, i32* %6
%88 = load i32, i32* @SOL_SOCKET
%89 = load i32, i32* @SO_REUSEADDR
%90 = call i32 @setsockopt(i32 %87, i32 %88, i32 %89, i32* %19, i32 4)
%91 = icmp eq i32 %90, -1
br i1 %91, label %92, label %95
92: ; preds = %85
%93 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.8, i64 0, i64 0))
%94 = call i32 @exit(i32 1)
unreachable
95: ; preds = %85
%96 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.9, i64 0, i64 0))
%97 = load i32, i32* %6
%98 = load %struct.addrinfo*, %struct.addrinfo** %13
%99 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %98, i32 0, i32 1
%100 = load i32, i32* %99
%101 = load %struct.addrinfo*, %struct.addrinfo** %13
%102 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %101, i32 0, i32 0
%103 = load i32, i32* %102
%104 = call i32 @bind(i32 %97, i32 %100, i32 %103)
%105 = icmp eq i32 %104, -1
br i1 %105, label %106, label %111
106: ; preds = %95
%107 = load i32, i32* %6
%108 = call i32 @close(i32 %107)
%109 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.10, i64 0, i64 0))
%110 = call i32 @exit(i32 1)
unreachable
111: ; preds = %95
%112 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.11, i64 0, i64 0))
%113 = load i32, i32* %6
%114 = load i32, i32* %11
%115 = call i32 @listen(i32 %113, i32 %114)
%116 = icmp eq i32 %115, -1
br i1 %116, label %117, label %120
117: ; preds = %111
%118 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.12, i64 0, i64 0))
%119 = call i32 @exit(i32 1)
unreachable
120: ; preds = %111
%121 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.13, i64 0, i64 0))
br label %122
122: ; preds = %159, %120
br label %123
123: ; preds = %122
store i32 4, i32* %15
%124 = load i32, i32* %6
%125 = bitcast %struct.sockaddr_storage* %14 to %struct.sockaddr*
%126 = call i32 @accept(i32 %124, %struct.sockaddr* %125, i32* %15)
store i32 %126, i32* %7
%127 = icmp eq i32 %126, -1
br i1 %127, label %128, label %131
128: ; preds = %123
%129 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.14, i64 0, i64 0))
%130 = call i32 @exit(i32 1)
unreachable
131: ; preds = %123
%132 = getelementptr inbounds %struct.sockaddr_storage, %struct.sockaddr_storage* %14, i32 0, i32 0
%133 = load i32, i32* %132
%134 = bitcast %struct.sockaddr_storage* %14 to %struct.sockaddr*
%135 = call i32 @get_in_addr(%struct.sockaddr* %134)
%136 = trunc i64 %23 to i32
%137 = call i32 @inet_ntop(i32 %133, i32 %135, i8* %25, i32 %136)
%138 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.15, i64 0, i64 0), i8* %25)
%139 = load i32, i32* %7
%140 = getelementptr inbounds [16 x i8], [16 x i8]* %16, i64 0, i64 0
%141 = call i32 @recv(i32 %139, i8* %140, i32 15, i32 0)
store i32 %141, i32* %17
%142 = icmp eq i32 %141, -1
br i1 %142, label %143, label %146
143: ; preds = %131
%144 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.16, i64 0, i64 0))
%145 = call i32 @exit(i32 1)
unreachable
146: ; preds = %131
%147 = load i32, i32* %17
%148 = sext i32 %147 to i64
%149 = getelementptr inbounds [16 x i8], [16 x i8]* %16, i64 0, i64 %148
store i8 0, i8* %149
%150 = getelementptr inbounds [16 x i8], [16 x i8]* %16, i64 0, i64 0
%151 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.17, i64 0, i64 0), i8* %150)
%152 = load i32, i32* %7
%153 = getelementptr inbounds [16 x i8], [16 x i8]* %16, i64 0, i64 0
%154 = load i32, i32* %17
%155 = call i32 @send(i32 %152, i8* %153, i32 %154, i32 0)
%156 = icmp eq i32 %155, -1
br i1 %156, label %157, label %159
157: ; preds = %146
%158 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.18, i64 0, i64 0))
br label %159
159: ; preds = %157, %146
br label %122
}

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32, i32, i32, i32, i32 }
%struct.sockaddr_storage = type { i32 }
%struct.sockaddr = type { i32 }
@INET6_ADDRSTRLEN = global i32 0
@.str = [6 x i8] c"p:n:h\00"
@optarg = global i32 0
@stdout = global i32 0
@.str.1 = [3 x i8] c"%s\00"
@.str.2 = [178 x i8] c"usage:\0A echoserver [options]\0Aoptions:\0A -p Port (Default: 8888)\0A -n Maximum pending connections\0A -h Show this help message\0A\00"
@stderr = global i32 0
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@AI_PASSIVE = global i32 0
@.str.3 = [3 x i8] c"%d\00"
@.str.4 = [17 x i8] c"getaddrinfo: %s\0A\00"
@.str.6 = [15 x i8] c"server: socket\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.8 = [11 x i8] c"setsockopt\00"
@.str.10 = [12 x i8] c"server:bind\00"
@.str.12 = [7 x i8] c"listen\00"
@.str.14 = [7 x i8] c"accept\00"
@.str.15 = [32 x i8] c"server: got connection from %s\0A\00"
@.str.16 = [5 x i8] c"recv\00"
@.str.17 = [47 x i8] c"server: i've got this message from client: %s\0A\00"
@.str.18 = [5 x i8] c"send\00"
@str = [24 x i8] c"getaddrinfo is executed\00"
@str.19 = [18 x i8] c"socket is created\00"
@str.20 = [19 x i8] c"zombies are killed\00"
@str.21 = [15 x i8] c"bind to socket\00"
@str.22 = [35 x i8] c"server: waiting for connections...\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca [15 x i8]
%4 = alloca %struct.addrinfo
%5 = alloca %struct.addrinfo*
%6 = alloca %struct.sockaddr_storage
%7 = alloca i32
%8 = alloca [16 x i8]
%9 = alloca i32
%10 = getelementptr inbounds [15 x i8], [15 x i8]* %3, i64 0, i64 0
%11 = bitcast %struct.addrinfo* %4 to i8*
%12 = bitcast %struct.addrinfo** %5 to i8*
%13 = bitcast %struct.sockaddr_storage* %6 to i8*
%14 = bitcast i32* %7 to i8*
%15 = getelementptr inbounds [16 x i8], [16 x i8]* %8, i64 0, i64 0
%16 = bitcast i32* %9 to i8*
%17 = load i32, i32* @INET6_ADDRSTRLEN
%18 = zext i32 %17 to i64
%19 = tail call i8* @llvm.stacksave()
%20 = alloca i8, i64 %18
br label %21
21: ; preds = %30, %2
%22 = phi i32 [ %32, %30 ], [ 5, %2 ]
%23 = phi i32 [ %25, %30 ], [ 8888, %2 ]
br label %24
24: ; preds = %21, %27
%25 = phi i32 [ %29, %27 ], [ %23, %21 ]
%26 = tail call i32 @getopt(i32 %0, i8** %1, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
switch i32 %26, label %37 [
i32 -1, label %41
i32 112, label %27
i32 110, label %30
i32 104, label %33
]
27: ; preds = %24
%28 = load i32, i32* @optarg
%29 = tail call i32 @atoi(i32 %28)
br label %24
30: ; preds = %24
%31 = load i32, i32* @optarg
%32 = tail call i32 @atoi(i32 %31)
br label %21
33: ; preds = %24
%34 = load i32, i32* @stdout
%35 = tail call i32 @fprintf(i32 %34, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([178 x i8], [178 x i8]* @.str.2, i64 0, i64 0))
%36 = tail call i32 @exit(i32 0)
unreachable
37: ; preds = %24
%38 = load i32, i32* @stderr
%39 = tail call i32 @fprintf(i32 %38, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([178 x i8], [178 x i8]* @.str.2, i64 0, i64 0))
%40 = tail call i32 @exit(i32 1)
unreachable
41: ; preds = %24
%42 = call i32 @memset(%struct.addrinfo* %4, i32 0, i32 24)
%43 = load i32, i32* @AF_INET
%44 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 4
store i32 %43, i32* %44
%45 = load i32, i32* @SOCK_STREAM
%46 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 3
store i32 %45, i32* %46
%47 = load i32, i32* @AI_PASSIVE
%48 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 5
store i32 %47, i32* %48
%49 = call i32 @snprintf(i8* %10, i32 15, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32 %25)
%50 = call i32 @getaddrinfo(i32* null, i8* %10, %struct.addrinfo* %4, %struct.addrinfo** %5)
%51 = icmp eq i32 %50, 0
br i1 %51, label %56, label %52
52: ; preds = %41
%53 = load i32, i32* @stderr
%54 = call i8* @gai_strerror(i32 %50)
%55 = call i32 @fprintf(i32 %53, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0), i8* %54)
ret i32 1
56: ; preds = %41
%57 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @str, i64 0, i64 0))
%58 = load %struct.addrinfo*, %struct.addrinfo** %5
%59 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %58, i64 0, i32 4
%60 = load i32, i32* %59
%61 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %58, i64 0, i32 3
%62 = load i32, i32* %61
%63 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %58, i64 0, i32 2
%64 = load i32, i32* %63
%65 = call i32 @socket(i32 %60, i32 %62, i32 %64)
%66 = icmp eq i32 %65, -1
br i1 %66, label %67, label %70
67: ; preds = %56
%68 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.6, i64 0, i64 0))
%69 = call i32 @exit(i32 1)
unreachable
70: ; preds = %56
%71 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str.19, i64 0, i64 0))
%72 = load i32, i32* @SOL_SOCKET
%73 = load i32, i32* @SO_REUSEADDR
%74 = call i32 @setsockopt(i32 %65, i32 %72, i32 %73, i32* %9, i32 4)
%75 = icmp eq i32 %74, -1
br i1 %75, label %76, label %79
76: ; preds = %70
%77 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.8, i64 0, i64 0))
%78 = call i32 @exit(i32 1)
unreachable
79: ; preds = %70
%80 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str.20, i64 0, i64 0))
%81 = load %struct.addrinfo*, %struct.addrinfo** %5
%82 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %81, i64 0, i32 1
%83 = load i32, i32* %82
%84 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %81, i64 0, i32 0
%85 = load i32, i32* %84
%86 = call i32 @bind(i32 %65, i32 %83, i32 %85)
%87 = icmp eq i32 %86, -1
br i1 %87, label %88, label %92
88: ; preds = %79
%89 = call i32 @close(i32 %65)
%90 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.10, i64 0, i64 0))
%91 = call i32 @exit(i32 1)
unreachable
92: ; preds = %79
%93 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str.21, i64 0, i64 0))
%94 = call i32 @listen(i32 %65, i32 %22)
%95 = icmp eq i32 %94, -1
br i1 %95, label %96, label %99
96: ; preds = %92
%97 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.12, i64 0, i64 0))
%98 = call i32 @exit(i32 1)
unreachable
99: ; preds = %92
%100 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @str.22, i64 0, i64 0))
%101 = bitcast %struct.sockaddr_storage* %6 to %struct.sockaddr*
store i32 4, i32* %7
%102 = call i32 @accept(i32 %65, %struct.sockaddr* %101, i32* %7)
%103 = icmp eq i32 %102, -1
br i1 %103, label %106, label %104
104: ; preds = %99
%105 = getelementptr inbounds %struct.sockaddr_storage, %struct.sockaddr_storage* %6, i64 0, i32 0
br label %109
106: ; preds = %128, %99
%107 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.14, i64 0, i64 0))
%108 = call i32 @exit(i32 1)
unreachable
109: ; preds = %104, %128
%110 = phi i32 [ %102, %104 ], [ %129, %128 ]
%111 = load i32, i32* %105
%112 = call i32 @get_in_addr(%struct.sockaddr* %101)
%113 = call i32 @inet_ntop(i32 %111, i32 %112, i8* %20, i32 %17)
%114 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @.str.15, i64 0, i64 0), i8* %20)
%115 = call i32 @recv(i32 %110, i8* %15, i32 15, i32 0)
%116 = icmp eq i32 %115, -1
br i1 %116, label %117, label %120
117: ; preds = %109
%118 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.16, i64 0, i64 0))
%119 = call i32 @exit(i32 1)
unreachable
120: ; preds = %109
%121 = sext i32 %115 to i64
%122 = getelementptr inbounds [16 x i8], [16 x i8]* %8, i64 0, i64 %121
store i8 0, i8* %122
%123 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([47 x i8], [47 x i8]* @.str.17, i64 0, i64 0), i8* %15)
%124 = call i32 @send(i32 %110, i8* %15, i32 %115, i32 0)
%125 = icmp eq i32 %124, -1
br i1 %125, label %126, label %128
126: ; preds = %120
%127 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.18, i64 0, i64 0))
br label %128
128: ; preds = %126, %120
store i32 4, i32* %7
%129 = call i32 @accept(i32 %65, %struct.sockaddr* %101, i32* %7)
%130 = icmp eq i32 %129, -1
br i1 %130, label %106, label %109
}