#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 addrinfo {
  int /*<<< orphan*/ ai_addrlen;
  int /*<<< orphan*/ ai_addr;
  int /*<<< orphan*/ ai_protocol;
  int /*<<< orphan*/ ai_socktype;
  int /*<<< orphan*/ ai_family;
  struct addrinfo *ai_next;
};
typedef int /*<<< orphan*/ hints;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int atoi(char *);
int /*<<< orphan*/ close(int);
char connect(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
scalar_t__ getaddrinfo(char *, char *, struct addrinfo *, struct addrinfo **);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ sprintf(char *, char *, int);
void port_scanner(char *tIP, char *start_port, char *end_port) {
  int s_port = atoi(start_port);
  int e_port = atoi(end_port);
  int port = 0;
  for (port = s_port; port <= e_port; port++) {
    struct addrinfo hints;
    memset(&hints, 0, sizeof(hints));
    hints.ai_family = AF_INET;
    hints.ai_socktype = SOCK_STREAM;
    struct addrinfo *serv_addr = NULL;
    char tport[6] = {0};
    sprintf(tport, "%d", port);
    if (getaddrinfo(tIP, tport, &hints, &serv_addr) == 0) {
      struct addrinfo *temp = NULL;
      int sockfd = 0;
      char status = 0;
      for (temp = serv_addr; temp != NULL; temp = temp->ai_next) {
        sockfd = socket(temp->ai_family, temp->ai_socktype, temp->ai_protocol);
        if (sockfd < 0) {
          printf("Port %d is NOT open.\n", port);
          continue;
        }
        status = connect(sockfd, temp->ai_addr, temp->ai_addrlen);
        if (status < 0) {
          printf("Port %d is NOT open.\n", port);
          close(sockfd);
          continue;
        }
        printf("Port %d is open.\n", port);
        close(sockfd);
      }
      freeaddrinfo(serv_addr);
    } else {
      freeaddrinfo(serv_addr);
      printf("Port %d is NOT open.\n", port);
    }
  }
}


x86 assembly (clang, -O0):
<port_scanner>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <atoi@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%rdi
call   1060 <atoi@plt>
mov    %eax,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x20(%rbp),%eax
jg     134a <port_scanner+0x1ba>
lea    -0x48(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1040 <memset@plt>
mov    0x2dfc(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
mov    0x2de0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
movq   $0x0,-0x50(%rbp)
lea    -0x56(%rbp),%rdi
xor    %esi,%esi
mov    $0x6,%edx
call   1040 <memset@plt>
lea    -0x56(%rbp),%rdi
mov    -0x24(%rbp),%edx
lea    0xddd(%rip),%rsi
call   1070 <sprintf@plt>
mov    -0x8(%rbp),%rdi
lea    -0x56(%rbp),%rsi
lea    -0x48(%rbp),%rdx
lea    -0x50(%rbp),%rcx
call   1090 <getaddrinfo@plt>
cmp    $0x0,%rax
jne    131f <port_scanner+0x18f>
movq   $0x0,-0x60(%rbp)
movl   $0x0,-0x64(%rbp)
movb   $0x0,-0x65(%rbp)
mov    -0x50(%rbp),%rax
mov    %rax,-0x60(%rbp)
cmpq   $0x0,-0x60(%rbp)
je     1311 <port_scanner+0x181>
mov    -0x60(%rbp),%rax
mov    0x10(%rax),%edi
mov    -0x60(%rbp),%rax
mov    0xc(%rax),%esi
mov    -0x60(%rbp),%rax
mov    0x8(%rax),%edx
call   10b0 <socket@plt>
mov    %eax,-0x64(%rbp)
cmpl   $0x0,-0x64(%rbp)
jge    12a8 <port_scanner+0x118>
mov    -0x24(%rbp),%esi
lea    0xd65(%rip),%rdi
call   1030 <printf@plt>
jmp    1300 <port_scanner+0x170>
mov    -0x64(%rbp),%edi
mov    -0x60(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x60(%rbp),%rax
mov    (%rax),%edx
call   1080 <connect@plt>
mov    %al,-0x65(%rbp)
movsbl -0x65(%rbp),%eax
cmp    $0x0,%eax
jge    12e9 <port_scanner+0x159>
mov    -0x24(%rbp),%esi
lea    0xd2c(%rip),%rdi
call   1030 <printf@plt>
mov    -0x64(%rbp),%edi
call   1050 <close@plt>
jmp    1300 <port_scanner+0x170>
mov    -0x24(%rbp),%esi
lea    0xd26(%rip),%rdi
call   1030 <printf@plt>
mov    -0x64(%rbp),%edi
call   1050 <close@plt>
mov    -0x60(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,-0x60(%rbp)
jmp    1262 <port_scanner+0xd2>
mov    -0x50(%rbp),%rdi
call   10a0 <freeaddrinfo@plt>
jmp    1337 <port_scanner+0x1a7>
mov    -0x50(%rbp),%rdi
call   10a0 <freeaddrinfo@plt>
mov    -0x24(%rbp),%esi
lea    0xcd1(%rip),%rdi
call   1030 <printf@plt>
jmp    133c <port_scanner+0x1ac>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    11c9 <port_scanner+0x39>
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<port_scanner>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x48,%rsp
mov    %rdx,%rbp
mov    %rdi,0x20(%rsp)
mov    %rsi,%rdi
call   1060 <atoi@plt>
mov    %eax,%ebx
mov    %rbp,%rdi
call   1060 <atoi@plt>
mov    %eax,0x14(%rsp)
cmp    %eax,%ebx
jle    11cf <port_scanner+0x3f>
add    $0x48,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe2d(%rip),%r12
lea    0xe3c(%rip),%r14
jmp    11fd <port_scanner+0x6d>
nop
mov    %r15,%rdi
call   10a0 <freeaddrinfo@plt>
mov    %r12,%rdi
mov    %ebx,%esi
call   1030 <printf@plt>
lea    0x1(%rbx),%eax
cmp    0x14(%rsp),%ebx
mov    %eax,%ebx
je     11c0 <port_scanner+0x30>
lea    0x28(%rsp),%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1040 <memset@plt>
mov    0x2dd0(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x38(%rsp)
mov    0x2db3(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x34(%rsp)
movq   $0x0,0x8(%rsp)
movw   $0x0,0x1c(%rsp)
movl   $0x0,0x18(%rsp)
lea    0x18(%rsp),%rbp
mov    %rbp,%rdi
lea    0xdae(%rip),%rsi
mov    %ebx,%edx
call   1070 <sprintf@plt>
mov    0x20(%rsp),%rdi
mov    %rbp,%rsi
mov    %r15,%rdx
lea    0x8(%rsp),%rcx
call   1090 <getaddrinfo@plt>
mov    0x8(%rsp),%r15
test   %rax,%rax
jne    11e0 <port_scanner+0x50>
test   %r15,%r15
jne    12a3 <port_scanner+0x113>
xor    %edi,%edi
call   10a0 <freeaddrinfo@plt>
jmp    11f2 <port_scanner+0x62>
nopl   (%rax)
mov    %r12,%rdi
mov    %ebx,%esi
call   1030 <printf@plt>
mov    0x18(%r15),%r15
test   %r15,%r15
je     1300 <port_scanner+0x170>
mov    0x10(%r15),%edi
mov    0x8(%r15),%edx
mov    0xc(%r15),%esi
call   10b0 <socket@plt>
test   %eax,%eax
js     1290 <port_scanner+0x100>
mov    %eax,%r13d
mov    (%r15),%edx
mov    0x4(%r15),%esi
mov    %eax,%edi
call   1080 <connect@plt>
test   %al,%al
js     12e0 <port_scanner+0x150>
mov    %r14,%rdi
jmp    12e3 <port_scanner+0x153>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %r12,%rdi
mov    %ebx,%esi
call   1030 <printf@plt>
mov    %r13d,%edi
call   1050 <close@plt>
jmp    129a <port_scanner+0x10a>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    0x8(%rsp),%rdi
call   10a0 <freeaddrinfo@plt>
jmp    11f2 <port_scanner+0x62>

LLVM IR (-O0):
%struct.addrinfo = type { i32, i32, i32, i32, i32, %struct.addrinfo* }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [3 x i8] c"%d\00"
@.str.1 = [22 x i8] c"Port %d is NOT open.\0A\00"
@.str.2 = [18 x i8] c"Port %d is open.\0A\00"
define void @port_scanner(i8* %0, i8* %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca %struct.addrinfo
%11 = alloca %struct.addrinfo*
%12 = alloca [6 x i8]
%13 = alloca %struct.addrinfo*
%14 = alloca i32
%15 = alloca i8
store i8* %0, i8** %4
store i8* %1, i8** %5
store i8* %2, i8** %6
%16 = load i8*, i8** %5
%17 = call i32 @atoi(i8* %16)
store i32 %17, i32* %7
%18 = load i8*, i8** %6
%19 = call i32 @atoi(i8* %18)
store i32 %19, i32* %8
store i32 0, i32* %9
%20 = load i32, i32* %7
store i32 %20, i32* %9
br label %21
21: ; preds = %95, %3
%22 = load i32, i32* %9
%23 = load i32, i32* %8
%24 = icmp sle i32 %22, %23
br i1 %24, label %25, label %98
25: ; preds = %21
%26 = call i32 @memset(%struct.addrinfo* %10, i32 0, i32 32)
%27 = load i32, i32* @AF_INET
%28 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %10, i32 0, i32 4
store i32 %27, i32* %28
%29 = load i32, i32* @SOCK_STREAM
%30 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %10, i32 0, i32 3
store i32 %29, i32* %30
store %struct.addrinfo* null, %struct.addrinfo** %11
%31 = bitcast [6 x i8]* %12 to i8*
%32 = getelementptr inbounds [6 x i8], [6 x i8]* %12, i64 0, i64 0
%33 = load i32, i32* %9
%34 = call i32 @sprintf(i8* %32, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %33)
%35 = load i8*, i8** %4
%36 = getelementptr inbounds [6 x i8], [6 x i8]* %12, i64 0, i64 0
%37 = call i64 @getaddrinfo(i8* %35, i8* %36, %struct.addrinfo* %10, %struct.addrinfo** %11)
%38 = icmp eq i64 %37, 0
br i1 %38, label %39, label %89
39: ; preds = %25
store %struct.addrinfo* null, %struct.addrinfo** %13
store i32 0, i32* %14
store i8 0, i8* %15
%40 = load %struct.addrinfo*, %struct.addrinfo** %11
store %struct.addrinfo* %40, %struct.addrinfo** %13
br label %41
41: ; preds = %82, %39
%42 = load %struct.addrinfo*, %struct.addrinfo** %13
%43 = icmp ne %struct.addrinfo* %42, null
br i1 %43, label %44, label %86
44: ; preds = %41
%45 = load %struct.addrinfo*, %struct.addrinfo** %13
%46 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %45, i32 0, i32 4
%47 = load i32, i32* %46
%48 = load %struct.addrinfo*, %struct.addrinfo** %13
%49 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %48, i32 0, i32 3
%50 = load i32, i32* %49
%51 = load %struct.addrinfo*, %struct.addrinfo** %13
%52 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %51, i32 0, i32 2
%53 = load i32, i32* %52
%54 = call i32 @socket(i32 %47, i32 %50, i32 %53)
store i32 %54, i32* %14
%55 = load i32, i32* %14
%56 = icmp slt i32 %55, 0
br i1 %56, label %57, label %60
57: ; preds = %44
%58 = load i32, i32* %9
%59 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i32 %58)
br label %82
60: ; preds = %44
%61 = load i32, i32* %14
%62 = load %struct.addrinfo*, %struct.addrinfo** %13
%63 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %62, i32 0, i32 1
%64 = load i32, i32* %63
%65 = load %struct.addrinfo*, %struct.addrinfo** %13
%66 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %65, i32 0, i32 0
%67 = load i32, i32* %66
%68 = call signext i8 @connect(i32 %61, i32 %64, i32 %67)
store i8 %68, i8* %15
%69 = load i8, i8* %15
%70 = sext i8 %69 to i32
%71 = icmp slt i32 %70, 0
br i1 %71, label %72, label %77
72: ; preds = %60
%73 = load i32, i32* %9
%74 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i32 %73)
%75 = load i32, i32* %14
%76 = call i32 @close(i32 %75)
br label %82
77: ; preds = %60
%78 = load i32, i32* %9
%79 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i32 %78)
%80 = load i32, i32* %14
%81 = call i32 @close(i32 %80)
br label %82
82: ; preds = %77, %72, %57
%83 = load %struct.addrinfo*, %struct.addrinfo** %13
%84 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %83, i32 0, i32 5
%85 = load %struct.addrinfo*, %struct.addrinfo** %84
store %struct.addrinfo* %85, %struct.addrinfo** %13
br label %41
86: ; preds = %41
%87 = load %struct.addrinfo*, %struct.addrinfo** %11
%88 = call i32 @freeaddrinfo(%struct.addrinfo* %87)
br label %94
89: ; preds = %25
%90 = load %struct.addrinfo*, %struct.addrinfo** %11
%91 = call i32 @freeaddrinfo(%struct.addrinfo* %90)
%92 = load i32, i32* %9
%93 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i32 %92)
br label %94
94: ; preds = %89, %86
br label %95
95: ; preds = %94
%96 = load i32, i32* %9
%97 = add nsw i32 %96, 1
store i32 %97, i32* %9
br label %21
98: ; preds = %21
ret void
}

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32, i32, i32, i32, %struct.addrinfo* }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [3 x i8] c"%d\00"
@.str.1 = [22 x i8] c"Port %d is NOT open.\0A\00"
@.str.2 = [18 x i8] c"Port %d is open.\0A\00"
define void @port_scanner(i8* %0, i8* nocapture %1, i8* nocapture %2) {
%4 = alloca %struct.addrinfo
%5 = alloca %struct.addrinfo*
%6 = alloca [6 x i8]
%7 = tail call i32 @atoi(i8* %1)
%8 = tail call i32 @atoi(i8* %2)
%9 = icmp sgt i32 %7, %8
br i1 %9, label %67, label %10
10: ; preds = %3
%11 = bitcast %struct.addrinfo* %4 to i8*
%12 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 4
%13 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 3
%14 = bitcast %struct.addrinfo** %5 to i8*
%15 = getelementptr inbounds [6 x i8], [6 x i8]* %6, i64 0, i64 0
br label %16
16: ; preds = %10, %64
%17 = phi i32 [ %7, %10 ], [ %65, %64 ]
%18 = call i32 @memset(%struct.addrinfo* %4, i32 0, i32 32)
%19 = load i32, i32* @AF_INET
store i32 %19, i32* %12
%20 = load i32, i32* @SOCK_STREAM
store i32 %20, i32* %13
store %struct.addrinfo* null, %struct.addrinfo** %5
%21 = call i32 @sprintf(i8* %15, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %17)
%22 = call i64 @getaddrinfo(i8* %0, i8* %15, %struct.addrinfo* %4, %struct.addrinfo** %5)
%23 = icmp eq i64 %22, 0
%24 = load %struct.addrinfo*, %struct.addrinfo** %5
br i1 %23, label %25, label %61
25: ; preds = %16
%26 = icmp eq %struct.addrinfo* %24, null
br i1 %26, label %58, label %27
27: ; preds = %25, %52
%28 = phi %struct.addrinfo* [ %54, %52 ], [ %24, %25 ]
%29 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %28, i64 0, i32 4
%30 = load i32, i32* %29
%31 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %28, i64 0, i32 3
%32 = load i32, i32* %31
%33 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %28, i64 0, i32 2
%34 = load i32, i32* %33
%35 = call i32 @socket(i32 %30, i32 %32, i32 %34)
%36 = icmp slt i32 %35, 0
br i1 %36, label %37, label %39
37: ; preds = %27
%38 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i32 %17)
br label %52
39: ; preds = %27
%40 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %28, i64 0, i32 1
%41 = load i32, i32* %40
%42 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %28, i64 0, i32 0
%43 = load i32, i32* %42
%44 = call signext i8 @connect(i32 %35, i32 %41, i32 %43)
%45 = icmp slt i8 %44, 0
br i1 %45, label %46, label %49
46: ; preds = %39
%47 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i32 %17)
%48 = call i32 @close(i32 %35)
br label %52
49: ; preds = %39
%50 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i32 %17)
%51 = call i32 @close(i32 %35)
br label %52
52: ; preds = %49, %46, %37
%53 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %28, i64 0, i32 5
%54 = load %struct.addrinfo*, %struct.addrinfo** %53
%55 = icmp eq %struct.addrinfo* %54, null
br i1 %55, label %56, label %27
56: ; preds = %52
%57 = load %struct.addrinfo*, %struct.addrinfo** %5
br label %58
58: ; preds = %56, %25
%59 = phi %struct.addrinfo* [ %57, %56 ], [ null, %25 ]
%60 = call i32 @freeaddrinfo(%struct.addrinfo* %59)
br label %64
61: ; preds = %16
%62 = call i32 @freeaddrinfo(%struct.addrinfo* %24)
%63 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i32 %17)
br label %64
64: ; preds = %61, %58
%65 = add i32 %17, 1
%66 = icmp eq i32 %17, %8
br i1 %66, label %67, label %16
67: ; preds = %64, %3
ret void
}