#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 /*<<< orphan*/ timeout;
struct timeval {
  int tv_sec;
  scalar_t__ tv_usec;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_zero;
  int /*<<< orphan*/ sin_family;
  int /*<<< orphan*/ sin_addr;
};
struct sockaddr {
  int dummy;
};
struct hostent {
  int /*<<< orphan*/ h_addr;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
scalar_t__ ECONNREFUSED;
scalar_t__ EINPROGRESS;
scalar_t__ ETIMEDOUT;
int /*<<< orphan*/ IPPROTO_TCP;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_SNDTIMEO;
int atoi(char *);
int /*<<< orphan*/ close(int);
scalar_t__ connect(int, struct sockaddr *, int);
scalar_t__ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
struct hostent *gethostbyname(char *);
int /*<<< orphan*/ htons(int);
scalar_t__ inet_aton(char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ memcpy(int /*<<< orphan*/ *, int /*<<< orphan*/, int);
int /*<<< orphan*/ memset(int /*<<< orphan*/, int /*<<< orphan*/, int);
int ntohs(int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                      struct timeval *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stdout;
int main(int argc, char **argv) {
  int sockfd, pstart, pend;
  char mode;
  struct timeval timeout;
  struct sockaddr_in sain;
  struct hostent *host;
  if (argc < 2) {
    printf("usage: %s <IP/host> [<mode> <port start> <port end>]\n"
           "-modes:"
           "\t%#.2x display OPEN\n"
           "\t%#.2x display CLOSE\n"
           "\t%#.2x display TIMEOUT\n"
           "\t(default is OPEN)\n",
           argv[0], 0x1, 0x2, 0x4);
    exit(1);
  }
  if ((inet_aton(argv[1], &(sain.sin_addr))) == 0) {
    if ((host = gethostbyname(argv[1])) == 0) {
      perror("gethostbyname");
      exit(1);
    } else {
      memcpy(&(sain.sin_addr), host->h_addr, sizeof(sain.sin_addr));
    }
  }
  if (argc > 2) {
    mode = (char)atoi(argv[2]);
    if (!(mode & 0x1) && !(mode & 0x2) && !(mode & 0x4)) {
      printf("error: wrong mode\n");
      exit(1);
    }
  } else {
    mode = 0x1;
  }
  sain.sin_family = AF_INET;
  memset(sain.sin_zero, 0, sizeof(sain.sin_zero));
  timeout.tv_sec = 1;
  timeout.tv_usec = 0;
  if (argc > 3)
    pstart = atoi(argv[3]);
  else
    pstart = 0;
  if (argc > 4)
    pend = atoi(argv[4]);
  else
    pend = 65535;
  if (pstart > pend) {
    sockfd = pend;
    pend = pstart;
    pstart = sockfd;
  }
  for (; pstart <= pend; pstart++) {
    sain.sin_port = htons(pstart);
    if ((sockfd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) <= 0)
      perror("socket");
    if (setsockopt(sockfd, SOL_SOCKET, SO_SNDTIMEO, &timeout, sizeof(timeout)))
      perror("setsockopt");
    if (connect(sockfd, (struct sockaddr *)&sain, sizeof(struct sockaddr))) {
      if (errno == ECONNREFUSED) {
        if (mode & 0x2)
          printf("\015%hu CLOSE\n", ntohs(sain.sin_port));
        else {
          printf("\015%u", pstart);
          fflush(stdout);
        }
      } else if ((errno == ETIMEDOUT) || (errno == EINPROGRESS)) {
        if (mode & 0x4)
          printf("\015%hu TIMEOUT\n", ntohs(sain.sin_port));
        else {
          printf("\015%u", pstart);
          fflush(stdout);
        }
      } else {
        perror("connect");
      }
    } else {
      if (mode & 0x1)
        printf("\015%hu OPEN\n", ntohs(sain.sin_port));
      else {
        printf("\015%u", pstart);
        fflush(stdout);
      }
    }
    close(sockfd);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    123f <main+0x4f>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xde2(%rip),%rdi
mov    $0x1,%edx
mov    $0x2,%ecx
mov    $0x4,%r8d
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x1,%edi
call   10f0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    -0x40(%rbp),%rsi
add    $0xc,%rsi
call   10a0 <inet_aton@plt>
cmp    $0x0,%rax
jne    12ac <main+0xbc>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1080 <gethostbyname@plt>
mov    %rax,-0x48(%rbp)
cmp    $0x0,%rax
jne    128f <main+0x9f>
lea    0xe10(%rip),%rdi
call   10d0 <perror@plt>
mov    $0x1,%edi
call   10f0 <exit@plt>
lea    -0x40(%rbp),%rdi
add    $0xc,%rdi
mov    -0x48(%rbp),%rax
mov    (%rax),%esi
mov    $0x4,%edx
call   1090 <memcpy@plt>
jmp    12ac <main+0xbc>
cmpl   $0x2,-0x8(%rbp)
jle    1313 <main+0x123>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   10e0 <atoi@plt>
mov    %al,-0x1d(%rbp)
movsbl -0x1d(%rbp),%eax
and    $0x1,%eax
cmp    $0x0,%eax
jne    130e <main+0x11e>
movsbl -0x1d(%rbp),%eax
and    $0x2,%eax
cmp    $0x0,%eax
jne    130e <main+0x11e>
movsbl -0x1d(%rbp),%eax
and    $0x4,%eax
cmp    $0x0,%eax
jne    130e <main+0x11e>
lea    0xda1(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x1,%edi
call   10f0 <exit@plt>
jmp    1317 <main+0x127>
movb   $0x1,-0x1d(%rbp)
mov    0x2cba(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
mov    -0x3c(%rbp),%edi
xor    %esi,%esi
mov    $0x4,%edx
call   1060 <memset@plt>
movl   $0x1,-0x30(%rbp)
movq   $0x0,-0x28(%rbp)
cmpl   $0x3,-0x8(%rbp)
jle    1360 <main+0x170>
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
call   10e0 <atoi@plt>
mov    %eax,-0x18(%rbp)
jmp    1367 <main+0x177>
movl   $0x0,-0x18(%rbp)
cmpl   $0x4,-0x8(%rbp)
jle    1386 <main+0x196>
mov    -0x10(%rbp),%rax
mov    0x20(%rax),%rdi
call   10e0 <atoi@plt>
mov    %eax,-0x1c(%rbp)
jmp    138d <main+0x19d>
movl   $0xffff,-0x1c(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jle    13ab <main+0x1bb>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0x18(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x18(%rbp)
jmp    13b0 <main+0x1c0>
mov    -0x18(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jg     15c1 <main+0x3d1>
mov    -0x18(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x40(%rbp)
mov    0x2bd2(%rip),%rax
mov    (%rax),%edi
mov    0x2bd9(%rip),%rax
mov    (%rax),%esi
mov    0x2be0(%rip),%rax
mov    (%rax),%edx
call   1110 <socket@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jg     13ff <main+0x20f>
lea    0xcb7(%rip),%rdi
call   10d0 <perror@plt>
mov    -0x14(%rbp),%edi
mov    0x2bd7(%rip),%rax
mov    (%rax),%esi
mov    0x2bde(%rip),%rax
mov    (%rax),%edx
lea    -0x30(%rbp),%rcx
mov    $0x10,%r8d
call   1030 <setsockopt@plt>
cmp    $0x0,%rax
je     1439 <main+0x249>
lea    0xc84(%rip),%rdi
call   10d0 <perror@plt>
mov    -0x14(%rbp),%edi
lea    -0x40(%rbp),%rsi
mov    $0x4,%edx
call   1100 <connect@plt>
cmp    $0x0,%rax
je     155a <main+0x36a>
mov    0x2b4d(%rip),%rax
mov    (%rax),%rax
mov    0x2b63(%rip),%rcx
cmp    (%rcx),%rax
jne    14bf <main+0x2cf>
movsbl -0x1d(%rbp),%eax
and    $0x2,%eax
cmp    $0x0,%eax
je     149b <main+0x2ab>
mov    -0x40(%rbp),%edi
call   10c0 <ntohs@plt>
mov    %eax,%esi
lea    0xc34(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    14ba <main+0x2ca>
mov    -0x18(%rbp),%esi
lea    0xc2a(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    0x2add(%rip),%rax
mov    (%rax),%edi
call   10b0 <fflush@plt>
jmp    1555 <main+0x365>
mov    0x2ae2(%rip),%rax
mov    (%rax),%rax
mov    0x2ac8(%rip),%rcx
cmp    (%rcx),%rax
je     14f3 <main+0x303>
mov    0x2ac8(%rip),%rax
mov    (%rax),%rax
mov    0x2ae6(%rip),%rcx
cmp    (%rcx),%rax
jne    1544 <main+0x354>
movsbl -0x1d(%rbp),%eax
and    $0x4,%eax
cmp    $0x0,%eax
je     1520 <main+0x330>
mov    -0x40(%rbp),%edi
call   10c0 <ntohs@plt>
mov    %eax,%esi
lea    0xbbf(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    153f <main+0x34f>
mov    -0x18(%rbp),%esi
lea    0xba5(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    0x2a58(%rip),%rax
mov    (%rax),%edi
call   10b0 <fflush@plt>
jmp    1550 <main+0x360>
lea    0xb96(%rip),%rdi
call   10d0 <perror@plt>
jmp    1555 <main+0x365>
jmp    15ab <main+0x3bb>
movsbl -0x1d(%rbp),%eax
and    $0x1,%eax
cmp    $0x0,%eax
je     1587 <main+0x397>
mov    -0x40(%rbp),%edi
call   10c0 <ntohs@plt>
mov    %eax,%esi
lea    0xb6e(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    15a6 <main+0x3b6>
mov    -0x18(%rbp),%esi
lea    0xb3e(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    0x29f1(%rip),%rax
mov    (%rax),%edi
call   10b0 <fflush@plt>
jmp    15ab <main+0x3bb>
mov    -0x14(%rbp),%edi
call   1070 <close@plt>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    13b0 <main+0x1c0>
xor    %eax,%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rsi,%r15
cmp    $0x1,%edi
jle    1474 <main+0x274>
mov    %edi,%ebx
mov    0x8(%r15),%rdi
lea    0x14(%rsp),%rbp
mov    %rbp,%rsi
call   10b0 <inet_aton@plt>
test   %rax,%rax
jne    1253 <main+0x53>
mov    0x8(%r15),%rdi
call   1090 <gethostbyname@plt>
test   %rax,%rax
je     14b5 <main+0x2b5>
mov    (%rax),%esi
mov    %rbp,%rdi
mov    $0x4,%edx
call   10a0 <memcpy@plt>
mov    $0x1,%r14b
cmp    $0x3,%ebx
jb     1271 <main+0x71>
mov    0x10(%r15),%rdi
call   10f0 <atoi@plt>
mov    %eax,%r14d
test   $0x7,%r14b
je     149f <main+0x29f>
mov    0x2d60(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
mov    0xc(%rsp),%edi
xor    %r12d,%r12d
xor    %esi,%esi
mov    $0x4,%edx
call   1070 <memset@plt>
movl   $0x1,0x18(%rsp)
movq   $0x0,0x20(%rsp)
mov    $0xffff,%ebp
cmp    $0x4,%ebx
jb     12c8 <main+0xc8>
mov    0x18(%r15),%rdi
call   10f0 <atoi@plt>
mov    %eax,%r12d
cmp    $0x4,%ebx
je     12c8 <main+0xc8>
mov    0x20(%r15),%rdi
call   10f0 <atoi@plt>
mov    %eax,%ebp
cmp    %ebp,%r12d
mov    %r12d,%ebx
cmovg  %ebp,%ebx
cmovg  %r12d,%ebp
add    $0x1,%ebp
lea    0x18(%rsp),%r12
lea    0x8(%rsp),%r13
jmp    1321 <main+0x121>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
lea    0xdc5(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x2c89(%rip),%rax
mov    (%rax),%edi
call   10c0 <fflush@plt>
mov    %r15d,%edi
call   1080 <close@plt>
add    $0x1,%ebx
cmp    %ebx,%ebp
je     1463 <main+0x263>
mov    %ebx,%edi
call   1050 <htons@plt>
mov    %eax,0x8(%rsp)
mov    0x2c6d(%rip),%rax
mov    (%rax),%edi
mov    0x2c74(%rip),%rax
mov    (%rax),%esi
mov    0x2c7b(%rip),%rax
mov    (%rax),%edx
call   1120 <socket@plt>
mov    %eax,%r15d
test   %eax,%eax
jle    1430 <main+0x230>
mov    0x2c82(%rip),%rax
mov    (%rax),%esi
mov    0x2c89(%rip),%rax
mov    (%rax),%edx
mov    %r15d,%edi
mov    %r12,%rcx
mov    $0x10,%r8d
call   1040 <setsockopt@plt>
test   %rax,%rax
jne    1441 <main+0x241>
mov    %r15d,%edi
mov    %r13,%rsi
mov    $0x4,%edx
call   1110 <connect@plt>
test   %rax,%rax
je     13d0 <main+0x1d0>
mov    0x2c09(%rip),%rax
mov    (%rax),%rax
mov    0x2c1f(%rip),%rcx
cmp    (%rcx),%rax
jne    13f0 <main+0x1f0>
test   $0x2,%r14b
je     12f0 <main+0xf0>
mov    0x8(%rsp),%edi
call   10d0 <ntohs@plt>
lea    0xce8(%rip),%rdi
jmp    1422 <main+0x222>
nopw   0x0(%rax,%rax,1)
test   $0x1,%r14b
je     12f0 <main+0xf0>
mov    0x8(%rsp),%edi
call   10d0 <ntohs@plt>
lea    0xcec(%rip),%rdi
jmp    1422 <main+0x222>
nopl   0x0(%rax)
mov    0x2ba1(%rip),%rcx
cmp    (%rcx),%rax
je     1408 <main+0x208>
mov    0x2bcd(%rip),%rcx
cmp    (%rcx),%rax
jne    1452 <main+0x252>
test   $0x4,%r14b
je     12f0 <main+0xf0>
mov    0x8(%rsp),%edi
call   10d0 <ntohs@plt>
lea    0xc9e(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
jmp    130e <main+0x10e>
lea    0xc67(%rip),%rdi
call   10e0 <perror@plt>
jmp    1357 <main+0x157>
lea    0xc5d(%rip),%rdi
call   10e0 <perror@plt>
jmp    1383 <main+0x183>
lea    0xc75(%rip),%rdi
call   10e0 <perror@plt>
jmp    130e <main+0x10e>
xor    %eax,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    (%r15),%rsi
lea    0xb82(%rip),%rdi
mov    $0x1,%edx
mov    $0x2,%ecx
mov    $0x4,%r8d
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0x1,%edi
call   1100 <exit@plt>
lea    0xc3b(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1100 <exit@plt>
lea    0xbd4(%rip),%rdi
call   10e0 <perror@plt>
mov    $0x1,%edi
call   1100 <exit@plt>

LLVM IR (-O0):
%struct.timeval = type { i32, i64 }
%struct.sockaddr_in = type { i32, i32, i32, i32 }
%struct.hostent = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [144 x i8] c"usage: %s <IP/host> [<mode> <port start> <port end>]\0A-modes:\09%#.2x display OPEN\0A\09%#.2x display CLOSE\0A\09%#.2x display TIMEOUT\0A\09(default is OPEN)\0A\00"
@.str.1 = [14 x i8] c"gethostbyname\00"
@.str.2 = [19 x i8] c"error: wrong mode\0A\00"
@AF_INET = global i32 0
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@.str.3 = [7 x i8] c"socket\00"
@SOL_SOCKET = global i32 0
@SO_SNDTIMEO = global i32 0
@.str.4 = [11 x i8] c"setsockopt\00"
@errno = global i64 0
@ECONNREFUSED = global i64 0
@.str.5 = [12 x i8] c"\0D%hu CLOSE\0A\00"
@.str.6 = [4 x i8] c"\0D%u\00"
@stdout = global i32 0
@ETIMEDOUT = global i64 0
@EINPROGRESS = global i64 0
@.str.7 = [14 x i8] c"\0D%hu TIMEOUT\0A\00"
@.str.8 = [8 x i8] c"connect\00"
@.str.9 = [11 x i8] c"\0D%hu OPEN\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 i8
%10 = alloca %struct.timeval
%11 = alloca %struct.sockaddr_in
%12 = alloca %struct.hostent*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%13 = load i32, i32* %4
%14 = icmp slt i32 %13, 2
br i1 %14, label %15, label %21
15: ; preds = %2
%16 = load i8**, i8*** %5
%17 = getelementptr inbounds i8*, i8** %16, i64 0
%18 = load i8*, i8** %17
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([144 x i8], [144 x i8]* @.str, i64 0, i64 0), i8* %18, i32 1, i32 2, i32 4)
%20 = call i32 @exit(i32 1)
unreachable
21: ; preds = %2
%22 = load i8**, i8*** %5
%23 = getelementptr inbounds i8*, i8** %22, i64 1
%24 = load i8*, i8** %23
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 3
%26 = call i64 @inet_aton(i8* %24, i32* %25)
%27 = icmp eq i64 %26, 0
br i1 %27, label %28, label %44
28: ; preds = %21
%29 = load i8**, i8*** %5
%30 = getelementptr inbounds i8*, i8** %29, i64 1
%31 = load i8*, i8** %30
%32 = call %struct.hostent* @gethostbyname(i8* %31)
store %struct.hostent* %32, %struct.hostent** %12
%33 = icmp eq %struct.hostent* %32, null
br i1 %33, label %34, label %37
34: ; preds = %28
%35 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
%36 = call i32 @exit(i32 1)
unreachable
37: ; preds = %28
%38 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 3
%39 = load %struct.hostent*, %struct.hostent** %12
%40 = getelementptr inbounds %struct.hostent, %struct.hostent* %39, i32 0, i32 0
%41 = load i32, i32* %40
%42 = call i32 @memcpy(i32* %38, i32 %41, i32 4)
br label %43
43: ; preds = %37
br label %44
44: ; preds = %43, %21
%45 = load i32, i32* %4
%46 = icmp sgt i32 %45, 2
br i1 %46, label %47, label %71
47: ; preds = %44
%48 = load i8**, i8*** %5
%49 = getelementptr inbounds i8*, i8** %48, i64 2
%50 = load i8*, i8** %49
%51 = call i32 @atoi(i8* %50)
%52 = trunc i32 %51 to i8
store i8 %52, i8* %9
%53 = load i8, i8* %9
%54 = sext i8 %53 to i32
%55 = and i32 %54, 1
%56 = icmp ne i32 %55, 0
br i1 %56, label %70, label %57
57: ; preds = %47
%58 = load i8, i8* %9
%59 = sext i8 %58 to i32
%60 = and i32 %59, 2
%61 = icmp ne i32 %60, 0
br i1 %61, label %70, label %62
62: ; preds = %57
%63 = load i8, i8* %9
%64 = sext i8 %63 to i32
%65 = and i32 %64, 4
%66 = icmp ne i32 %65, 0
br i1 %66, label %70, label %67
67: ; preds = %62
%68 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
%69 = call i32 @exit(i32 1)
unreachable
70: ; preds = %62, %57, %47
br label %72
71: ; preds = %44
store i8 1, i8* %9
br label %72
72: ; preds = %71, %70
%73 = load i32, i32* @AF_INET
%74 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 2
store i32 %73, i32* %74
%75 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 1
%76 = load i32, i32* %75
%77 = call i32 @memset(i32 %76, i32 0, i32 4)
%78 = getelementptr inbounds %struct.timeval, %struct.timeval* %10, i32 0, i32 0
store i32 1, i32* %78
%79 = getelementptr inbounds %struct.timeval, %struct.timeval* %10, i32 0, i32 1
store i64 0, i64* %79
%80 = load i32, i32* %4
%81 = icmp sgt i32 %80, 3
br i1 %81, label %82, label %87
82: ; preds = %72
%83 = load i8**, i8*** %5
%84 = getelementptr inbounds i8*, i8** %83, i64 3
%85 = load i8*, i8** %84
%86 = call i32 @atoi(i8* %85)
store i32 %86, i32* %7
br label %88
87: ; preds = %72
store i32 0, i32* %7
br label %88
88: ; preds = %87, %82
%89 = load i32, i32* %4
%90 = icmp sgt i32 %89, 4
br i1 %90, label %91, label %96
91: ; preds = %88
%92 = load i8**, i8*** %5
%93 = getelementptr inbounds i8*, i8** %92, i64 4
%94 = load i8*, i8** %93
%95 = call i32 @atoi(i8* %94)
store i32 %95, i32* %8
br label %97
96: ; preds = %88
store i32 65535, i32* %8
br label %97
97: ; preds = %96, %91
%98 = load i32, i32* %7
%99 = load i32, i32* %8
%100 = icmp sgt i32 %98, %99
br i1 %100, label %101, label %105
101: ; preds = %97
%102 = load i32, i32* %8
store i32 %102, i32* %6
%103 = load i32, i32* %7
store i32 %103, i32* %8
%104 = load i32, i32* %6
store i32 %104, i32* %7
br label %105
105: ; preds = %101, %97
br label %106
106: ; preds = %201, %105
%107 = load i32, i32* %7
%108 = load i32, i32* %8
%109 = icmp sle i32 %107, %108
br i1 %109, label %110, label %204
110: ; preds = %106
%111 = load i32, i32* %7
%112 = call i32 @htons(i32 %111)
%113 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 0
store i32 %112, i32* %113
%114 = load i32, i32* @PF_INET
%115 = load i32, i32* @SOCK_STREAM
%116 = load i32, i32* @IPPROTO_TCP
%117 = call i32 @socket(i32 %114, i32 %115, i32 %116)
store i32 %117, i32* %6
%118 = icmp sle i32 %117, 0
br i1 %118, label %119, label %121
119: ; preds = %110
%120 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
br label %121
121: ; preds = %119, %110
%122 = load i32, i32* %6
%123 = load i32, i32* @SOL_SOCKET
%124 = load i32, i32* @SO_SNDTIMEO
%125 = call i64 @setsockopt(i32 %122, i32 %123, i32 %124, %struct.timeval* %10, i32 16)
%126 = icmp ne i64 %125, 0
br i1 %126, label %127, label %129
127: ; preds = %121
%128 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0))
br label %129
129: ; preds = %127, %121
%130 = load i32, i32* %6
%131 = bitcast %struct.sockaddr_in* %11 to %struct.sockaddr*
%132 = call i64 @connect(i32 %130, %struct.sockaddr* %131, i32 4)
%133 = icmp ne i64 %132, 0
br i1 %133, label %134, label %182
134: ; preds = %129
%135 = load i64, i64* @errno
%136 = load i64, i64* @ECONNREFUSED
%137 = icmp eq i64 %135, %136
br i1 %137, label %138, label %154
138: ; preds = %134
%139 = load i8, i8* %9
%140 = sext i8 %139 to i32
%141 = and i32 %140, 2
%142 = icmp ne i32 %141, 0
br i1 %142, label %143, label %148
143: ; preds = %138
%144 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 0
%145 = load i32, i32* %144
%146 = call i32 @ntohs(i32 %145)
%147 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0), i32 %146)
br label %153
148: ; preds = %138
%149 = load i32, i32* %7
%150 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0), i32 %149)
%151 = load i32, i32* @stdout
%152 = call i32 @fflush(i32 %151)
br label %153
153: ; preds = %148, %143
br label %181
154: ; preds = %134
%155 = load i64, i64* @errno
%156 = load i64, i64* @ETIMEDOUT
%157 = icmp eq i64 %155, %156
br i1 %157, label %162, label %158
158: ; preds = %154
%159 = load i64, i64* @errno
%160 = load i64, i64* @EINPROGRESS
%161 = icmp eq i64 %159, %160
br i1 %161, label %162, label %178
162: ; preds = %158, %154
%163 = load i8, i8* %9
%164 = sext i8 %163 to i32
%165 = and i32 %164, 4
%166 = icmp ne i32 %165, 0
br i1 %166, label %167, label %172
167: ; preds = %162
%168 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 0
%169 = load i32, i32* %168
%170 = call i32 @ntohs(i32 %169)
%171 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.7, i64 0, i64 0), i32 %170)
br label %177
172: ; preds = %162
%173 = load i32, i32* %7
%174 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0), i32 %173)
%175 = load i32, i32* @stdout
%176 = call i32 @fflush(i32 %175)
br label %177
177: ; preds = %172, %167
br label %180
178: ; preds = %158
%179 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.8, i64 0, i64 0))
br label %180
180: ; preds = %178, %177
br label %181
181: ; preds = %180, %153
br label %198
182: ; preds = %129
%183 = load i8, i8* %9
%184 = sext i8 %183 to i32
%185 = and i32 %184, 1
%186 = icmp ne i32 %185, 0
br i1 %186, label %187, label %192
187: ; preds = %182
%188 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 0
%189 = load i32, i32* %188
%190 = call i32 @ntohs(i32 %189)
%191 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.9, i64 0, i64 0), i32 %190)
br label %197
192: ; preds = %182
%193 = load i32, i32* %7
%194 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0), i32 %193)
%195 = load i32, i32* @stdout
%196 = call i32 @fflush(i32 %195)
br label %197
197: ; preds = %192, %187
br label %198
198: ; preds = %197, %181
%199 = load i32, i32* %6
%200 = call i32 @close(i32 %199)
br label %201
201: ; preds = %198
%202 = load i32, i32* %7
%203 = add nsw i32 %202, 1
store i32 %203, i32* %7
br label %106
204: ; preds = %106
ret i32 0
}

LLVM IR (-O2):
%struct.timeval = type { i32, i64 }
%struct.sockaddr_in = type { i32, i32, i32, i32 }
%struct.hostent = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [144 x i8] c"usage: %s <IP/host> [<mode> <port start> <port end>]\0A-modes:\09%#.2x display OPEN\0A\09%#.2x display CLOSE\0A\09%#.2x display TIMEOUT\0A\09(default is OPEN)\0A\00"
@.str.1 = [14 x i8] c"gethostbyname\00"
@AF_INET = global i32 0
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@.str.3 = [7 x i8] c"socket\00"
@SOL_SOCKET = global i32 0
@SO_SNDTIMEO = global i32 0
@.str.4 = [11 x i8] c"setsockopt\00"
@errno = global i64 0
@ECONNREFUSED = global i64 0
@.str.5 = [12 x i8] c"\0D%hu CLOSE\0A\00"
@.str.6 = [4 x i8] c"\0D%u\00"
@stdout = global i32 0
@ETIMEDOUT = global i64 0
@EINPROGRESS = global i64 0
@.str.7 = [14 x i8] c"\0D%hu TIMEOUT\0A\00"
@.str.8 = [8 x i8] c"connect\00"
@.str.9 = [11 x i8] c"\0D%hu OPEN\0A\00"
@str = [18 x i8] c"error: wrong mode\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.timeval
%4 = alloca %struct.sockaddr_in
%5 = bitcast %struct.timeval* %3 to i8*
%6 = bitcast %struct.sockaddr_in* %4 to i8*
%7 = icmp slt i32 %0, 2
br i1 %7, label %8, label %12
8: ; preds = %2
%9 = load i8*, i8** %1
%10 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([144 x i8], [144 x i8]* @.str, i64 0, i64 0), i8* %9, i32 1, i32 2, i32 4)
%11 = tail call i32 @exit(i32 1)
unreachable
12: ; preds = %2
%13 = getelementptr inbounds i8*, i8** %1, i64 1
%14 = load i8*, i8** %13
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 3
%16 = call i64 @inet_aton(i8* %14, i32* %15)
%17 = icmp eq i64 %16, 0
br i1 %17, label %18, label %29
18: ; preds = %12
%19 = load i8*, i8** %13
%20 = call %struct.hostent* @gethostbyname(i8* %19)
%21 = icmp eq %struct.hostent* %20, null
br i1 %21, label %22, label %25
22: ; preds = %18
%23 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
%24 = call i32 @exit(i32 1)
unreachable
25: ; preds = %18
%26 = getelementptr inbounds %struct.hostent, %struct.hostent* %20, i64 0, i32 0
%27 = load i32, i32* %26
%28 = call i32 @memcpy(i32* %15, i32 %27, i32 4)
br label %29
29: ; preds = %25, %12
%30 = icmp ugt i32 %0, 2
br i1 %30, label %31, label %41
31: ; preds = %29
%32 = getelementptr inbounds i8*, i8** %1, i64 2
%33 = load i8*, i8** %32
%34 = call i32 @atoi(i8* %33)
%35 = trunc i32 %34 to i8
%36 = and i32 %34, 7
%37 = icmp eq i32 %36, 0
br i1 %37, label %38, label %41
38: ; preds = %31
%39 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str, i64 0, i64 0))
%40 = call i32 @exit(i32 1)
unreachable
41: ; preds = %29, %31
%42 = phi i8 [ %35, %31 ], [ 1, %29 ]
%43 = load i32, i32* @AF_INET
%44 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 2
store i32 %43, i32* %44
%45 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1
%46 = load i32, i32* %45
%47 = call i32 @memset(i32 %46, i32 0, i32 4)
%48 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 0
store i32 1, i32* %48
%49 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 1
store i64 0, i64* %49
%50 = icmp ugt i32 %0, 3
br i1 %50, label %51, label %60
51: ; preds = %41
%52 = getelementptr inbounds i8*, i8** %1, i64 3
%53 = load i8*, i8** %52
%54 = call i32 @atoi(i8* %53)
%55 = icmp eq i32 %0, 4
br i1 %55, label %60, label %56
56: ; preds = %51
%57 = getelementptr inbounds i8*, i8** %1, i64 4
%58 = load i8*, i8** %57
%59 = call i32 @atoi(i8* %58)
br label %60
60: ; preds = %41, %51, %56
%61 = phi i32 [ %54, %56 ], [ %54, %51 ], [ 0, %41 ]
%62 = phi i32 [ %59, %56 ], [ 65535, %51 ], [ 65535, %41 ]
%63 = icmp sgt i32 %61, %62
%64 = select i1 %63, i32 %62, i32 %61
%65 = select i1 %63, i32 %61, i32 %62
%66 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0
%67 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%68 = and i8 %42, 4
%69 = icmp eq i8 %68, 0
%70 = and i8 %42, 2
%71 = icmp eq i8 %70, 0
%72 = and i8 %42, 1
%73 = icmp eq i8 %72, 0
br label %74
74: ; preds = %60, %133
%75 = phi i32 [ %64, %60 ], [ %135, %133 ]
%76 = call i32 @htons(i32 %75)
store i32 %76, i32* %66
%77 = load i32, i32* @PF_INET
%78 = load i32, i32* @SOCK_STREAM
%79 = load i32, i32* @IPPROTO_TCP
%80 = call i32 @socket(i32 %77, i32 %78, i32 %79)
%81 = icmp slt i32 %80, 1
br i1 %81, label %82, label %84
82: ; preds = %74
%83 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
br label %84
84: ; preds = %82, %74
%85 = load i32, i32* @SOL_SOCKET
%86 = load i32, i32* @SO_SNDTIMEO
%87 = call i64 @setsockopt(i32 %80, i32 %85, i32 %86, %struct.timeval* %3, i32 16)
%88 = icmp eq i64 %87, 0
br i1 %88, label %91, label %89
89: ; preds = %84
%90 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0))
br label %91
91: ; preds = %89, %84
%92 = call i64 @connect(i32 %80, %struct.sockaddr* %67, i32 4)
%93 = icmp eq i64 %92, 0
br i1 %93, label %124, label %94
94: ; preds = %91
%95 = load i64, i64* @errno
%96 = load i64, i64* @ECONNREFUSED
%97 = icmp eq i64 %95, %96
br i1 %97, label %98, label %107
98: ; preds = %94
br i1 %71, label %103, label %99
99: ; preds = %98
%100 = load i32, i32* %66
%101 = call i32 @ntohs(i32 %100)
%102 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0), i32 %101)
br label %133
103: ; preds = %98
%104 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0), i32 %75)
%105 = load i32, i32* @stdout
%106 = call i32 @fflush(i32 %105)
br label %133
107: ; preds = %94
%108 = load i64, i64* @ETIMEDOUT
%109 = icmp eq i64 %95, %108
%110 = load i64, i64* @EINPROGRESS
%111 = icmp eq i64 %95, %110
%112 = select i1 %109, i1 true, i1 %111
br i1 %112, label %113, label %122
113: ; preds = %107
br i1 %69, label %118, label %114
114: ; preds = %113
%115 = load i32, i32* %66
%116 = call i32 @ntohs(i32 %115)
%117 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.7, i64 0, i64 0), i32 %116)
br label %133
118: ; preds = %113
%119 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0), i32 %75)
%120 = load i32, i32* @stdout
%121 = call i32 @fflush(i32 %120)
br label %133
122: ; preds = %107
%123 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.8, i64 0, i64 0))
br label %133
124: ; preds = %91
br i1 %73, label %129, label %125
125: ; preds = %124
%126 = load i32, i32* %66
%127 = call i32 @ntohs(i32 %126)
%128 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.9, i64 0, i64 0), i32 %127)
br label %133
129: ; preds = %124
%130 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0), i32 %75)
%131 = load i32, i32* @stdout
%132 = call i32 @fflush(i32 %131)
br label %133
133: ; preds = %125, %129, %103, %99, %114, %118, %122
%134 = call i32 @close(i32 %80)
%135 = add i32 %75, 1
%136 = icmp eq i32 %75, %65
br i1 %136, label %137, label %74
137: ; preds = %133
ret i32 0
}