#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 */
typedef int u_short;
struct in_addr {
  int /*<<< orphan*/ s_addr;
};
struct sockaddr_in {
  char sin_zero;
  int /*<<< orphan*/ sin_port;
  struct in_addr sin_addr;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
struct servent {
  char *s_name;
};
struct pollfd {
  int fd;
  scalar_t__ events;
  scalar_t__ revents;
};
struct hostent {
  char *h_name;
  scalar_t__ *h_addr_list;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
#define EACCES 132
#define ECONNREFUSED 131
#define ENETUNREACH 130
#define EPERM 129
#define ETIMEDOUT 128
int /*<<< orphan*/ INADDR_NONE;
scalar_t__ POLLIN;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
scalar_t__ connect(int, struct sockaddr *, int);
int errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
struct hostent *gethostbyaddr(char *, int, int /*<<< orphan*/);
struct hostent *gethostbyname(char *);
struct servent *getservbyport(int /*<<< orphan*/, int /*<<< orphan*/ *);
int /*<<< orphan*/ herror(char *);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ inet_addr(char *);
scalar_t__ inet_ntoa(struct in_addr);
int /*<<< orphan*/ memset(char *, char, int);
int /*<<< orphan*/ perror(char *);
scalar_t__ poll(struct pollfd *, int, int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ recv(int, char *, int, int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strncpy(char *, char *, int);
int do_scan(char *ahost, u_short startp, u_short stopp) {
  int sockfd;
  struct sockaddr_in dest_addr;
  u_short curr_port;
  struct servent *serv_struct;
  struct hostent *htoscan;
  char buf[64];
  u_short minport = startp;
  u_short maxport = stopp;
  struct pollfd stopoll;
  char hn_string[255 + 1];
  char ip_string[30];
  int timedout = 0;
  int accessperm = 0;
  memset(hn_string, '\0', 255 + 1);
  memset(ip_string, '\0', 30);
  dest_addr.sin_family = AF_INET;
  if ((dest_addr.sin_addr.s_addr = inet_addr(ahost)) == INADDR_NONE) {
    if ((htoscan = gethostbyname(ahost)) == NULL) {
      herror("gethostbyname() failed");
      return -1;
    } else {
      dest_addr.sin_addr = *(struct in_addr *)*htoscan->h_addr_list;
      strncpy(hn_string, ahost, 255 + 1);
      strncpy(ip_string, (char *)inet_ntoa(dest_addr.sin_addr), 30);
    }
  } else {
    if ((htoscan = gethostbyaddr(ahost, 4, AF_INET)) == NULL) {
      strncpy(hn_string, "hostname unknown", 255 + 1);
      strncpy(ip_string, ahost, 30);
    } else {
      strncpy(hn_string, htoscan->h_name, 255 + 1);
      strncpy(ip_string, ahost, 30);
    }
  }
  hn_string[255 + 1 - 1] = '\0';
  ip_string[30 - 1] = '\0';
  printf("%s\t(%s)\n", hn_string, ip_string);
  memset(&(dest_addr.sin_zero), 0, 8);
  for (curr_port = minport; curr_port <= maxport && curr_port != 0;
       curr_port++) {
    if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
      perror("socket() call failed");
      return -1;
    }
    stopoll.fd = sockfd;
    stopoll.events = POLLIN;
    dest_addr.sin_port = htons(curr_port);
    if (!((connect(sockfd, (struct sockaddr *)&dest_addr,
                   sizeof(struct sockaddr))) < 0)) {
      serv_struct = getservbyport(htons(curr_port), NULL);
      printf("%d\t", curr_port);
      if (serv_struct != NULL)
        printf("%s \t", serv_struct->s_name);
      if ((poll(&stopoll, 1, 1000) > 0) &&
          (stopoll.events == stopoll.revents)) {
        memset(buf, 0, 64);
        recv(sockfd, buf, (64 - 1), 0);
        printf("%s\n", buf);
      } else {
        printf("\n");
      }
    } else {
      switch (errno) {
      case ECONNREFUSED:
        break;
      case ETIMEDOUT:
      case ENETUNREACH:
        if (!timedout) {
          timedout = 1;
          perror("socket error");
          fprintf(stderr, "Host may be down or firewalled\n");
          fprintf(stderr, "I'll try to continue, but this may take a while\n");
        }
        break;
      case EACCES:
      case EPERM:
        if (!accessperm) {
          accessperm = 1;
          perror("socket error");
          fprintf(stderr, "We might be blocked by a local firewall rule\n");
          fprintf(stderr,
                  "I'll try to continue, but this is likely to end in tears.");
        }
        break;
      default:
        perror("socket error");
        return -1;
      }
    }
    close(sockfd);
  }
  return 0;
}


x86 assembly (clang, -O0):
<do_scan>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1f0,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x94(%rbp)
mov    -0x18(%rbp),%eax
mov    %eax,-0x98(%rbp)
movl   $0x0,-0x1d4(%rbp)
movl   $0x0,-0x1d8(%rbp)
lea    -0x1b0(%rbp),%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1080 <memset@plt>
lea    -0x1d0(%rbp),%rdi
xor    %esi,%esi
mov    $0x1e,%edx
call   1080 <memset@plt>
mov    0x2d70(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
call   10a0 <inet_addr@plt>
mov    %eax,-0x28(%rbp)
mov    0x2d50(%rip),%rcx
cmp    (%rcx),%eax
jne    131b <do_scan+0x10b>
mov    -0x10(%rbp),%rdi
call   10b0 <gethostbyname@plt>
mov    %rax,-0x48(%rbp)
cmp    $0x0,%rax
jne    12c7 <do_scan+0xb7>
lea    0xd5e(%rip),%rdi
call   10d0 <herror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1685 <do_scan+0x475>
mov    -0x48(%rbp),%rax
mov    0x8(%rax),%rax
mov    (%rax),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
lea    -0x1b0(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    $0x100,%edx
call   1040 <strncpy@plt>
lea    -0x1d0(%rbp),%rax
mov    %rax,-0x1e0(%rbp)
mov    -0x28(%rbp),%edi
call   1050 <inet_ntoa@plt>
mov    -0x1e0(%rbp),%rdi
mov    %rax,%rsi
mov    $0x1e,%edx
call   1040 <strncpy@plt>
jmp    13a4 <do_scan+0x194>
mov    -0x10(%rbp),%rdi
mov    0x2cc2(%rip),%rax
mov    (%rax),%edx
mov    $0x4,%esi
call   1100 <gethostbyaddr@plt>
mov    %rax,-0x48(%rbp)
cmp    $0x0,%rax
jne    1372 <do_scan+0x162>
lea    -0x1b0(%rbp),%rdi
lea    0xcdd(%rip),%rsi
mov    $0x100,%edx
call   1040 <strncpy@plt>
lea    -0x1d0(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    $0x1e,%edx
call   1040 <strncpy@plt>
jmp    139f <do_scan+0x18f>
lea    -0x1b0(%rbp),%rdi
mov    -0x48(%rbp),%rax
mov    (%rax),%rsi
mov    $0x100,%edx
call   1040 <strncpy@plt>
lea    -0x1d0(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    $0x1e,%edx
call   1040 <strncpy@plt>
jmp    13a4 <do_scan+0x194>
movb   $0x0,-0xb1(%rbp)
movb   $0x0,-0x1b3(%rbp)
lea    -0x1b0(%rbp),%rsi
lea    -0x1d0(%rbp),%rdx
lea    0xc75(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1080 <memset@plt>
mov    -0x94(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    -0x34(%rbp),%ecx
xor    %eax,%eax
cmp    -0x98(%rbp),%ecx
mov    %al,-0x1e1(%rbp)
jg     140b <do_scan+0x1fb>
cmpl   $0x0,-0x34(%rbp)
setne  %al
mov    %al,-0x1e1(%rbp)
mov    -0x1e1(%rbp),%al
test   $0x1,%al
jne    141e <do_scan+0x20e>
jmp    167e <do_scan+0x46e>
mov    0x2bc3(%rip),%rax
mov    (%rax),%edi
mov    0x2b92(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1130 <socket@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jge    145b <do_scan+0x24b>
lea    0xbfb(%rip),%rdi
call   10f0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1685 <do_scan+0x475>
mov    -0x1c(%rbp),%eax
mov    %eax,-0xb0(%rbp)
mov    0x2b6d(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0xa8(%rbp)
mov    -0x34(%rbp),%edi
call   1060 <htons@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x1c(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0x4,%edx
call   1110 <connect@plt>
cmp    $0x0,%rax
jl     156d <do_scan+0x35d>
mov    -0x34(%rbp),%edi
call   1060 <htons@plt>
mov    %eax,%edi
xor    %eax,%eax
mov    %eax,%esi
call   1120 <getservbyport@plt>
mov    %rax,-0x40(%rbp)
mov    -0x34(%rbp),%esi
lea    0xb9e(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
cmpq   $0x0,-0x40(%rbp)
je     14e3 <do_scan+0x2d3>
mov    -0x40(%rbp),%rax
mov    (%rax),%rsi
lea    0xb82(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
lea    -0xb0(%rbp),%rdi
mov    $0x1,%esi
mov    $0x3e8,%edx
call   10e0 <poll@plt>
cmp    $0x0,%rax
jle    155a <do_scan+0x34a>
mov    -0xa8(%rbp),%rax
cmp    -0xa0(%rbp),%rax
jne    155a <do_scan+0x34a>
lea    -0x90(%rbp),%rdi
xor    %esi,%esi
mov    $0x40,%edx
call   1080 <memset@plt>
mov    -0x1c(%rbp),%edi
lea    -0x90(%rbp),%rsi
mov    $0x3f,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
lea    -0x90(%rbp),%rsi
lea    0xb15(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
jmp    1568 <do_scan+0x358>
lea    0xae2(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
jmp    1668 <do_scan+0x458>
mov    0x2a44(%rip),%rax
mov    (%rax),%eax
add    $0xffffff80,%eax
mov    %eax,%ecx
mov    %rcx,-0x1f0(%rbp)
sub    $0x4,%eax
ja     164b <do_scan+0x43b>
mov    -0x1f0(%rbp),%rax
lea    0xa67(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
jmp    1663 <do_scan+0x453>
cmpl   $0x0,-0x1d4(%rbp)
jne    15f4 <do_scan+0x3e4>
movl   $0x1,-0x1d4(%rbp)
lea    0xaa2(%rip),%rdi
call   10f0 <perror@plt>
mov    0x29f7(%rip),%rax
mov    (%rax),%edi
lea    0xa9a(%rip),%rsi
call   10c0 <fprintf@plt>
mov    0x29e2(%rip),%rax
mov    (%rax),%edi
lea    0xaa5(%rip),%rsi
call   10c0 <fprintf@plt>
jmp    1663 <do_scan+0x453>
cmpl   $0x0,-0x1d8(%rbp)
jne    1646 <do_scan+0x436>
movl   $0x1,-0x1d8(%rbp)
lea    0xa50(%rip),%rdi
call   10f0 <perror@plt>
mov    0x29a5(%rip),%rax
mov    (%rax),%edi
lea    0xa99(%rip),%rsi
call   10c0 <fprintf@plt>
mov    0x2990(%rip),%rax
mov    (%rax),%edi
lea    0xab2(%rip),%rsi
call   10c0 <fprintf@plt>
jmp    1663 <do_scan+0x453>
lea    0xa15(%rip),%rdi
call   10f0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1685 <do_scan+0x475>
jmp    1668 <do_scan+0x458>
mov    -0x1c(%rbp),%edi
call   1090 <close@plt>
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    13e7 <do_scan+0x1d7>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x1f0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_scan>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x198,%rsp
mov    %edx,%r12d
mov    %esi,%r14d
mov    %rdi,%rbp
lea    0x50(%rsp),%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   10a0 <memset@plt>
lea    0x30(%rsp),%rdi
xor    %esi,%esi
mov    $0x1e,%edx
call   10a0 <memset@plt>
mov    0x2d75(%rip),%rbx
mov    (%rbx),%eax
mov    %eax,0x14(%rsp)
mov    %rbp,%rdi
call   10c0 <inet_addr@plt>
mov    %eax,0x10(%rsp)
mov    0x2d54(%rip),%rcx
cmp    (%rcx),%eax
jne    12cb <do_scan+0x9b>
mov    %rbp,%rdi
call   10d0 <gethostbyname@plt>
test   %rax,%rax
je     12e4 <do_scan+0xb4>
mov    0x8(%rax),%rax
mov    (%rax),%rax
mov    (%rax),%r15d
mov    %r15d,0x10(%rsp)
lea    0x50(%rsp),%rdi
mov    %rbp,%rsi
mov    $0x100,%edx
call   1050 <strncpy@plt>
mov    %r15d,%edi
call   1070 <inet_ntoa@plt>
mov    %rax,%rbp
jmp    1310 <do_scan+0xe0>
mov    (%rbx),%edx
mov    %rbp,%rdi
mov    $0x4,%esi
call   1120 <gethostbyaddr@plt>
test   %rax,%rax
je     12fa <do_scan+0xca>
mov    (%rax),%rsi
jmp    1301 <do_scan+0xd1>
lea    0xd29(%rip),%rdi
call   10f0 <herror@plt>
mov    $0xffffffff,%ebp
jmp    1566 <do_scan+0x336>
lea    0xd2a(%rip),%rsi
lea    0x50(%rsp),%rdi
mov    $0x100,%edx
call   1050 <strncpy@plt>
lea    0x30(%rsp),%r15
mov    %r15,%rdi
mov    %rbp,%rsi
mov    $0x1e,%edx
call   1050 <strncpy@plt>
movb   $0x0,0x14f(%rsp)
movb   $0x0,0x4d(%rsp)
lea    0xd03(%rip),%rdi
xor    %ebp,%ebp
lea    0x50(%rsp),%rsi
mov    %r15,%rdx
xor    %eax,%eax
call   1090 <printf@plt>
lea    0x8(%rsp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   10a0 <memset@plt>
cmp    %r12d,%r14d
jg     1566 <do_scan+0x336>
test   %r14d,%r14d
je     1566 <do_scan+0x336>
movl   $0x0,(%rsp)
movl   $0x0,0x4(%rsp)
nopl   0x0(%rax)
mov    %rbx,%r13
mov    (%rbx),%edi
mov    0x2c34(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1150 <socket@plt>
test   %eax,%eax
js     157a <do_scan+0x34a>
mov    %eax,%r15d
mov    %eax,0x18(%rsp)
mov    0x2c2d(%rip),%rax
mov    (%rax),%rax
mov    %rax,0x20(%rsp)
mov    %r14d,%edi
call   1080 <htons@plt>
mov    %eax,0xc(%rsp)
mov    %r15d,%edi
lea    0x8(%rsp),%rsi
mov    $0x4,%edx
call   1130 <connect@plt>
test   %rax,%rax
js     1480 <do_scan+0x250>
mov    %r14d,%edi
call   1080 <htons@plt>
mov    %eax,%edi
xor    %esi,%esi
call   1140 <getservbyport@plt>
mov    %rax,%rbp
lea    0xc65(%rip),%rdi
mov    %r14d,%esi
xor    %eax,%eax
call   1090 <printf@plt>
test   %rbp,%rbp
je     1416 <do_scan+0x1e6>
mov    0x0(%rbp),%rsi
lea    0xc4f(%rip),%rdi
xor    %eax,%eax
call   1090 <printf@plt>
lea    0x18(%rsp),%rdi
mov    $0x1,%esi
mov    $0x3e8,%edx
call   1100 <poll@plt>
test   %rax,%rax
jle    1470 <do_scan+0x240>
mov    0x20(%rsp),%rax
cmp    0x28(%rsp),%rax
jne    1470 <do_scan+0x240>
lea    0x150(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x40,%edx
call   10a0 <memset@plt>
mov    %r15d,%edi
mov    %rbx,%rsi
mov    $0x3f,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %rbx,%rdi
call   1060 <puts@plt>
jmp    14d0 <do_scan+0x2a0>
xchg   %ax,%ax
mov    $0xa,%edi
call   1040 <putchar@plt>
jmp    14d0 <do_scan+0x2a0>
nopl   0x0(%rax)
mov    0x2b31(%rip),%rax
mov    (%rax),%eax
add    $0xffffff80,%eax
cmp    $0x4,%eax
ja     158b <do_scan+0x35b>
lea    0xb64(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
cmpl   $0x0,0x4(%rsp)
je     14f9 <do_scan+0x2c9>
movl   $0x1,0x4(%rsp)
jmp    14d0 <do_scan+0x2a0>
cmpl   $0x0,(%rsp)
je     1532 <do_scan+0x302>
movl   $0x1,(%rsp)
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %r15d,%edi
call   10b0 <close@plt>
xor    %ebp,%ebp
cmp    %r12d,%r14d
jge    1566 <do_scan+0x336>
lea    0x1(%r14),%eax
cmp    $0xffffffff,%r14d
mov    %eax,%r14d
mov    %r13,%rbx
jne    1380 <do_scan+0x150>
jmp    1566 <do_scan+0x336>
lea    0xb63(%rip),%rdi
call   1110 <perror@plt>
mov    0x2abc(%rip),%rbx
mov    (%rbx),%edi
lea    0xb5b(%rip),%rsi
call   10e0 <fprintf@plt>
mov    (%rbx),%edi
lea    0xb6d(%rip),%rsi
call   10e0 <fprintf@plt>
movl   $0x1,0x4(%rsp)
jmp    14d0 <do_scan+0x2a0>
lea    0xb2a(%rip),%rdi
call   1110 <perror@plt>
mov    0x2a83(%rip),%rbx
mov    (%rbx),%edi
lea    0xb73(%rip),%rsi
call   10e0 <fprintf@plt>
mov    (%rbx),%edi
lea    0xb93(%rip),%rsi
call   10e0 <fprintf@plt>
jmp    14bc <do_scan+0x28c>
mov    %ebp,%eax
add    $0x198,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xac4(%rip),%rdi
call   1110 <perror@plt>
jmp    12f0 <do_scan+0xc0>
lea    0xad1(%rip),%rdi
jmp    1581 <do_scan+0x351>

LLVM IR (-O0):
%struct.sockaddr_in = type { i8, i32, %struct.in_addr, i32 }
%struct.in_addr = type { i32 }
%struct.servent = type { i8* }
%struct.hostent = type { i8*, i64* }
%struct.pollfd = type { i32, i64, i64 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@INADDR_NONE = global i32 0
@.str = [23 x i8] c"gethostbyname() failed\00"
@.str.1 = [17 x i8] c"hostname unknown\00"
@.str.2 = [9 x i8] c"%s\09(%s)\0A\00"
@SOCK_STREAM = global i32 0
@.str.3 = [21 x i8] c"socket() call failed\00"
@POLLIN = global i64 0
@.str.4 = [4 x i8] c"%d\09\00"
@.str.5 = [5 x i8] c"%s \09\00"
@.str.6 = [4 x i8] c"%s\0A\00"
@.str.7 = [2 x i8] c"\0A\00"
@errno = global i32 0
@.str.8 = [13 x i8] c"socket error\00"
@stderr = global i32 0
@.str.9 = [32 x i8] c"Host may be down or firewalled\0A\00"
@.str.10 = [49 x i8] c"I'll try to continue, but this may take a while\0A\00"
@.str.11 = [46 x i8] c"We might be blocked by a local firewall rule\0A\00"
@.str.12 = [58 x i8] c"I'll try to continue, but this is likely to end in tears.\00"
define i32 @do_scan(i8* %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca %struct.sockaddr_in
%10 = alloca i32
%11 = alloca %struct.servent*
%12 = alloca %struct.hostent*
%13 = alloca [64 x i8]
%14 = alloca i32
%15 = alloca i32
%16 = alloca %struct.pollfd
%17 = alloca [256 x i8]
%18 = alloca [30 x i8]
%19 = alloca i32
%20 = alloca i32
store i8* %0, i8** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
%21 = load i32, i32* %6
store i32 %21, i32* %14
%22 = load i32, i32* %7
store i32 %22, i32* %15
store i32 0, i32* %19
store i32 0, i32* %20
%23 = getelementptr inbounds [256 x i8], [256 x i8]* %17, i64 0, i64 0
%24 = call i32 @memset(i8* %23, i8 signext 0, i32 256)
%25 = getelementptr inbounds [30 x i8], [30 x i8]* %18, i64 0, i64 0
%26 = call i32 @memset(i8* %25, i8 signext 0, i32 30)
%27 = load i32, i32* @AF_INET
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 3
store i32 %27, i32* %28
%29 = load i8*, i8** %5
%30 = call i32 @inet_addr(i8* %29)
%31 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 2
%32 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %31, i32 0, i32 0
store i32 %30, i32* %32
%33 = load i32, i32* @INADDR_NONE
%34 = icmp eq i32 %30, %33
br i1 %34, label %35, label %61
35: ; preds = %3
%36 = load i8*, i8** %5
%37 = call %struct.hostent* @gethostbyname(i8* %36)
store %struct.hostent* %37, %struct.hostent** %12
%38 = icmp eq %struct.hostent* %37, null
br i1 %38, label %39, label %41
39: ; preds = %35
%40 = call i32 @herror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %4
br label %185
41: ; preds = %35
%42 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 2
%43 = load %struct.hostent*, %struct.hostent** %12
%44 = getelementptr inbounds %struct.hostent, %struct.hostent* %43, i32 0, i32 1
%45 = load i64*, i64** %44
%46 = load i64, i64* %45
%47 = inttoptr i64 %46 to %struct.in_addr*
%48 = bitcast %struct.in_addr* %42 to i8*
%49 = bitcast %struct.in_addr* %47 to i8*
%50 = getelementptr inbounds [256 x i8], [256 x i8]* %17, i64 0, i64 0
%51 = load i8*, i8** %5
%52 = call i32 @strncpy(i8* %50, i8* %51, i32 256)
%53 = getelementptr inbounds [30 x i8], [30 x i8]* %18, i64 0, i64 0
%54 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 2
%55 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %54, i32 0, i32 0
%56 = load i32, i32* %55
%57 = call i64 @inet_ntoa(i32 %56)
%58 = inttoptr i64 %57 to i8*
%59 = call i32 @strncpy(i8* %53, i8* %58, i32 30)
br label %60
60: ; preds = %41
br label %82
61: ; preds = %3
%62 = load i8*, i8** %5
%63 = load i32, i32* @AF_INET
%64 = call %struct.hostent* @gethostbyaddr(i8* %62, i32 4, i32 %63)
store %struct.hostent* %64, %struct.hostent** %12
%65 = icmp eq %struct.hostent* %64, null
br i1 %65, label %66, label %72
66: ; preds = %61
%67 = getelementptr inbounds [256 x i8], [256 x i8]* %17, i64 0, i64 0
%68 = call i32 @strncpy(i8* %67, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i32 256)
%69 = getelementptr inbounds [30 x i8], [30 x i8]* %18, i64 0, i64 0
%70 = load i8*, i8** %5
%71 = call i32 @strncpy(i8* %69, i8* %70, i32 30)
br label %81
72: ; preds = %61
%73 = getelementptr inbounds [256 x i8], [256 x i8]* %17, i64 0, i64 0
%74 = load %struct.hostent*, %struct.hostent** %12
%75 = getelementptr inbounds %struct.hostent, %struct.hostent* %74, i32 0, i32 0
%76 = load i8*, i8** %75
%77 = call i32 @strncpy(i8* %73, i8* %76, i32 256)
%78 = getelementptr inbounds [30 x i8], [30 x i8]* %18, i64 0, i64 0
%79 = load i8*, i8** %5
%80 = call i32 @strncpy(i8* %78, i8* %79, i32 30)
br label %81
81: ; preds = %72, %66
br label %82
82: ; preds = %81, %60
%83 = getelementptr inbounds [256 x i8], [256 x i8]* %17, i64 0, i64 255
store i8 0, i8* %83
%84 = getelementptr inbounds [30 x i8], [30 x i8]* %18, i64 0, i64 29
store i8 0, i8* %84
%85 = getelementptr inbounds [256 x i8], [256 x i8]* %17, i64 0, i64 0
%86 = getelementptr inbounds [30 x i8], [30 x i8]* %18, i64 0, i64 0
%87 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i8* %85, i8* %86)
%88 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 0
%89 = call i32 @memset(i8* %88, i8 signext 0, i32 8)
%90 = load i32, i32* %14
store i32 %90, i32* %10
br label %91
91: ; preds = %181, %82
%92 = load i32, i32* %10
%93 = load i32, i32* %15
%94 = icmp sle i32 %92, %93
br i1 %94, label %95, label %98
95: ; preds = %91
%96 = load i32, i32* %10
%97 = icmp ne i32 %96, 0
br label %98
98: ; preds = %95, %91
%99 = phi i1 [ false, %91 ], [ %97, %95 ]
br i1 %99, label %100, label %184
100: ; preds = %98
%101 = load i32, i32* @AF_INET
%102 = load i32, i32* @SOCK_STREAM
%103 = call i32 @socket(i32 %101, i32 %102, i32 0)
store i32 %103, i32* %8
%104 = icmp slt i32 %103, 0
br i1 %104, label %105, label %107
105: ; preds = %100
%106 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %4
br label %185
107: ; preds = %100
%108 = load i32, i32* %8
%109 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %16, i32 0, i32 0
store i32 %108, i32* %109
%110 = load i64, i64* @POLLIN
%111 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %16, i32 0, i32 1
store i64 %110, i64* %111
%112 = load i32, i32* %10
%113 = call i32 @htons(i32 %112)
%114 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 1
store i32 %113, i32* %114
%115 = load i32, i32* %8
%116 = bitcast %struct.sockaddr_in* %9 to %struct.sockaddr*
%117 = call i64 @connect(i32 %115, %struct.sockaddr* %116, i32 4)
%118 = icmp slt i64 %117, 0
br i1 %118, label %152, label %119
119: ; preds = %107
%120 = load i32, i32* %10
%121 = call i32 @htons(i32 %120)
%122 = call %struct.servent* @getservbyport(i32 %121, i32* null)
store %struct.servent* %122, %struct.servent** %11
%123 = load i32, i32* %10
%124 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i32 %123)
%125 = load %struct.servent*, %struct.servent** %11
%126 = icmp ne %struct.servent* %125, null
br i1 %126, label %127, label %132
127: ; preds = %119
%128 = load %struct.servent*, %struct.servent** %11
%129 = getelementptr inbounds %struct.servent, %struct.servent* %128, i32 0, i32 0
%130 = load i8*, i8** %129
%131 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0), i8* %130)
br label %132
132: ; preds = %127, %119
%133 = call i64 @poll(%struct.pollfd* %16, i32 1, i32 1000)
%134 = icmp sgt i64 %133, 0
br i1 %134, label %135, label %149
135: ; preds = %132
%136 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %16, i32 0, i32 1
%137 = load i64, i64* %136
%138 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %16, i32 0, i32 2
%139 = load i64, i64* %138
%140 = icmp eq i64 %137, %139
br i1 %140, label %141, label %149
141: ; preds = %135
%142 = getelementptr inbounds [64 x i8], [64 x i8]* %13, i64 0, i64 0
%143 = call i32 @memset(i8* %142, i8 signext 0, i32 64)
%144 = load i32, i32* %8
%145 = getelementptr inbounds [64 x i8], [64 x i8]* %13, i64 0, i64 0
%146 = call i32 @recv(i32 %144, i8* %145, i32 63, i32 0)
%147 = getelementptr inbounds [64 x i8], [64 x i8]* %13, i64 0, i64 0
%148 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0), i8* %147)
br label %151
149: ; preds = %135, %132
%150 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.7, i64 0, i64 0))
br label %151
151: ; preds = %149, %141
br label %178
152: ; preds = %107
%153 = load i32, i32* @errno
switch i32 %153, label %175 [
i32 131, label %154
i32 128, label %155
i32 130, label %155
i32 132, label %165
i32 129, label %165
]
154: ; preds = %152
br label %177
155: ; preds = %152, %152
%156 = load i32, i32* %19
%157 = icmp ne i32 %156, 0
br i1 %157, label %164, label %158
158: ; preds = %155
store i32 1, i32* %19
%159 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.8, i64 0, i64 0))
%160 = load i32, i32* @stderr
%161 = call i32 @fprintf(i32 %160, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.9, i64 0, i64 0))
%162 = load i32, i32* @stderr
%163 = call i32 @fprintf(i32 %162, i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.10, i64 0, i64 0))
br label %164
164: ; preds = %158, %155
br label %177
165: ; preds = %152, %152
%166 = load i32, i32* %20
%167 = icmp ne i32 %166, 0
br i1 %167, label %174, label %168
168: ; preds = %165
store i32 1, i32* %20
%169 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.8, i64 0, i64 0))
%170 = load i32, i32* @stderr
%171 = call i32 @fprintf(i32 %170, i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.11, i64 0, i64 0))
%172 = load i32, i32* @stderr
%173 = call i32 @fprintf(i32 %172, i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str.12, i64 0, i64 0))
br label %174
174: ; preds = %168, %165
br label %177
175: ; preds = %152
%176 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.8, i64 0, i64 0))
store i32 -1, i32* %4
br label %185
177: ; preds = %174, %164, %154
br label %178
178: ; preds = %177, %151
%179 = load i32, i32* %8
%180 = call i32 @close(i32 %179)
br label %181
181: ; preds = %178
%182 = load i32, i32* %10
%183 = add nsw i32 %182, 1
store i32 %183, i32* %10
br label %91
184: ; preds = %98
store i32 0, i32* %4
br label %185
185: ; preds = %184, %175, %105, %39
%186 = load i32, i32* %4
ret i32 %186
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i8, i32, %struct.in_addr, i32 }
%struct.in_addr = type { i32 }
%struct.pollfd = type { i32, i64, i64 }
%struct.hostent = type { i8*, i64* }
%struct.sockaddr = type { i32 }
%struct.servent = type { i8* }
@AF_INET = global i32 0
@INADDR_NONE = global i32 0
@.str = [23 x i8] c"gethostbyname() failed\00"
@.str.1 = [17 x i8] c"hostname unknown\00"
@.str.2 = [9 x i8] c"%s\09(%s)\0A\00"
@SOCK_STREAM = global i32 0
@.str.3 = [21 x i8] c"socket() call failed\00"
@POLLIN = global i64 0
@.str.4 = [4 x i8] c"%d\09\00"
@.str.5 = [5 x i8] c"%s \09\00"
@errno = global i32 0
@.str.8 = [13 x i8] c"socket error\00"
@stderr = global i32 0
@.str.9 = [32 x i8] c"Host may be down or firewalled\0A\00"
@.str.10 = [49 x i8] c"I'll try to continue, but this may take a while\0A\00"
@.str.11 = [46 x i8] c"We might be blocked by a local firewall rule\0A\00"
@.str.12 = [58 x i8] c"I'll try to continue, but this is likely to end in tears.\00"
define i32 @do_scan(i8* %0, i32 %1, i32 %2) {
%4 = alloca %struct.sockaddr_in
%5 = alloca [64 x i8]
%6 = alloca %struct.pollfd
%7 = alloca [256 x i8]
%8 = alloca [30 x i8]
%9 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0
%10 = getelementptr inbounds [64 x i8], [64 x i8]* %5, i64 0, i64 0
%11 = bitcast %struct.pollfd* %6 to i8*
%12 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%13 = getelementptr inbounds [30 x i8], [30 x i8]* %8, i64 0, i64 0
%14 = call i32 @memset(i8* %12, i8 signext 0, i32 256)
%15 = call i32 @memset(i8* %13, i8 signext 0, i32 30)
%16 = load i32, i32* @AF_INET
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 3
store i32 %16, i32* %17
%18 = call i32 @inet_addr(i8* %0)
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 2, i32 0
store i32 %18, i32* %19
%20 = load i32, i32* @INADDR_NONE
%21 = icmp eq i32 %18, %20
br i1 %21, label %22, label %36
22: ; preds = %3
%23 = call %struct.hostent* @gethostbyname(i8* %0)
%24 = icmp eq %struct.hostent* %23, null
br i1 %24, label %25, label %27
25: ; preds = %22
%26 = call i32 @herror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
br label %127
27: ; preds = %22
%28 = getelementptr inbounds %struct.hostent, %struct.hostent* %23, i64 0, i32 1
%29 = load i64*, i64** %28
%30 = load i64, i64* %29
%31 = inttoptr i64 %30 to i32*
%32 = load i32, i32* %31
store i32 %32, i32* %19
%33 = call i32 @strncpy(i8* %12, i8* %0, i32 256)
%34 = call i64 @inet_ntoa(i32 %32)
%35 = inttoptr i64 %34 to i8*
br label %46
36: ; preds = %3
%37 = load i32, i32* @AF_INET
%38 = call %struct.hostent* @gethostbyaddr(i8* %0, i32 4, i32 %37)
%39 = icmp eq %struct.hostent* %38, null
br i1 %39, label %40, label %42
40: ; preds = %36
%41 = call i32 @strncpy(i8* %12, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i32 256)
br label %46
42: ; preds = %36
%43 = getelementptr inbounds %struct.hostent, %struct.hostent* %38, i64 0, i32 0
%44 = load i8*, i8** %43
%45 = call i32 @strncpy(i8* %12, i8* %44, i32 256)
br label %46
46: ; preds = %40, %42, %27
%47 = phi i8* [ %0, %40 ], [ %0, %42 ], [ %35, %27 ]
%48 = call i32 @strncpy(i8* %13, i8* %47, i32 30)
%49 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 255
store i8 0, i8* %49
%50 = getelementptr inbounds [30 x i8], [30 x i8]* %8, i64 0, i64 29
store i8 0, i8* %50
%51 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i8* %12, i8* %13)
%52 = call i32 @memset(i8* %9, i8 signext 0, i32 8)
%53 = icmp sle i32 %1, %2
%54 = icmp ne i32 %1, 0
%55 = and i1 %53, %54
br i1 %55, label %56, label %127
56: ; preds = %46
%57 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %6, i64 0, i32 0
%58 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %6, i64 0, i32 1
%59 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1
%60 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%61 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %6, i64 0, i32 2
br label %62
62: ; preds = %56, %119
%63 = phi i32 [ 0, %56 ], [ %121, %119 ]
%64 = phi i32 [ 0, %56 ], [ %120, %119 ]
%65 = phi i32 [ %1, %56 ], [ %123, %119 ]
%66 = load i32, i32* @AF_INET
%67 = load i32, i32* @SOCK_STREAM
%68 = call i32 @socket(i32 %66, i32 %67, i32 0)
%69 = icmp slt i32 %68, 0
br i1 %69, label %70, label %72
70: ; preds = %62
%71 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
br label %127
72: ; preds = %62
store i32 %68, i32* %57
%73 = load i64, i64* @POLLIN
store i64 %73, i64* %58
%74 = call i32 @htons(i32 %65)
store i32 %74, i32* %59
%75 = call i64 @connect(i32 %68, %struct.sockaddr* %60, i32 4)
%76 = icmp slt i64 %75, 0
br i1 %76, label %99, label %77
77: ; preds = %72
%78 = call i32 @htons(i32 %65)
%79 = call %struct.servent* @getservbyport(i32 %78, i32* null)
%80 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i32 %65)
%81 = icmp eq %struct.servent* %79, null
br i1 %81, label %86, label %82
82: ; preds = %77
%83 = getelementptr inbounds %struct.servent, %struct.servent* %79, i64 0, i32 0
%84 = load i8*, i8** %83
%85 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0), i8* %84)
br label %86
86: ; preds = %82, %77
%87 = call i64 @poll(%struct.pollfd* %6, i32 1, i32 1000)
%88 = icmp sgt i64 %87, 0
br i1 %88, label %89, label %97
89: ; preds = %86
%90 = load i64, i64* %58
%91 = load i64, i64* %61
%92 = icmp eq i64 %90, %91
br i1 %92, label %93, label %97
93: ; preds = %89
%94 = call i32 @memset(i8* %10, i8 signext 0, i32 64)
%95 = call i32 @recv(i32 %68, i8* %10, i32 63, i32 0)
%96 = call i32 @puts(i8* %10)
br label %119
97: ; preds = %89, %86
%98 = call i32 @putchar(i32 10)
br label %119
99: ; preds = %72
%100 = load i32, i32* @errno
switch i32 %100, label %117 [
i32 131, label %119
i32 128, label %101
i32 130, label %101
i32 132, label %109
i32 129, label %109
]
101: ; preds = %99, %99
%102 = icmp eq i32 %64, 0
br i1 %102, label %103, label %119
103: ; preds = %101
%104 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.8, i64 0, i64 0))
%105 = load i32, i32* @stderr
%106 = call i32 @fprintf(i32 %105, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.9, i64 0, i64 0))
%107 = load i32, i32* @stderr
%108 = call i32 @fprintf(i32 %107, i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.10, i64 0, i64 0))
br label %119
109: ; preds = %99, %99
%110 = icmp eq i32 %63, 0
br i1 %110, label %111, label %119
111: ; preds = %109
%112 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.8, i64 0, i64 0))
%113 = load i32, i32* @stderr
%114 = call i32 @fprintf(i32 %113, i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.11, i64 0, i64 0))
%115 = load i32, i32* @stderr
%116 = call i32 @fprintf(i32 %115, i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str.12, i64 0, i64 0))
br label %119
117: ; preds = %99
%118 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.8, i64 0, i64 0))
br label %127
119: ; preds = %99, %103, %101, %111, %109, %93, %97
%120 = phi i32 [ %64, %109 ], [ %64, %111 ], [ 1, %101 ], [ 1, %103 ], [ %64, %99 ], [ %64, %93 ], [ %64, %97 ]
%121 = phi i32 [ 1, %109 ], [ 1, %111 ], [ %63, %101 ], [ %63, %103 ], [ %63, %99 ], [ %63, %93 ], [ %63, %97 ]
%122 = call i32 @close(i32 %68)
%123 = add nsw i32 %65, 1
%124 = icmp slt i32 %65, %2
%125 = icmp ne i32 %123, 0
%126 = and i1 %124, %125
br i1 %126, label %62, label %127
127: ; preds = %119, %46, %117, %70, %25
%128 = phi i32 [ -1, %25 ], [ -1, %70 ], [ -1, %117 ], [ 0, %46 ], [ 0, %119 ]
ret i32 %128
}