#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_in {
  int /*<<< orphan*/ sin_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
struct hostent {
  int /*<<< orphan*/ h_length;
  int /*<<< orphan*/ h_addr;
};
typedef int /*<<< orphan*/ servaddr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ IPPROTO_TCP;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int atoi(char *);
int /*<<< orphan*/ close(int);
int connect(int, struct sockaddr *, int);
int errno;
struct hostent *gethostbyname(char *);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ memcpy(int /*<<< orphan*/ *, int /*<<< orphan*/,
                          int /*<<< orphan*/);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int main(int argc, char **argv) {
  int sd;
  int port;
  int start;
  int end;
  int rval;
  char response[1024];
  char *message = "shell";
  struct hostent *hostaddr;
  struct sockaddr_in servaddr;
  if (argc < 4) {
    printf("Usage: ./main <IPaddress> <Start Port> <End Port>\n");
    return 0;
  }
  start = atoi(argv[2]);
  end = atoi(argv[3]);
  for (port = start; port <= end; port++) {
    sd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
    if (sd == -1) {
      perror("Socket()\n");
      return (errno);
    }
    memset(&servaddr, 0, sizeof(servaddr));
    servaddr.sin_family = AF_INET;
    servaddr.sin_port = htons(port);
    hostaddr = gethostbyname(argv[1]);
    memcpy(&servaddr.sin_addr, hostaddr->h_addr, hostaddr->h_length);
    rval = connect(sd, (struct sockaddr *)&servaddr, sizeof(servaddr));
    if (rval == -1) {
      printf("Port %d is open\n", port);
      close(sd);
    } else
      printf("Port %d is closed\n", port);
    close(sd);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x450,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe40(%rip),%rax
mov    %rax,-0x438(%rbp)
cmpl   $0x4,-0x8(%rbp)
jge    11eb <main+0x4b>
lea    0xe2e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1330 <main+0x190>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   10a0 <atoi@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
call   10a0 <atoi@plt>
mov    %eax,-0x20(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x20(%rbp),%eax
jg     1330 <main+0x190>
mov    0x2d9c(%rip),%rax
mov    (%rax),%edi
mov    0x2da3(%rip),%rax
mov    (%rax),%esi
mov    0x2daa(%rip),%rax
mov    (%rax),%edx
call   10c0 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    1267 <main+0xc7>
lea    0xde8(%rip),%rdi
call   1090 <perror@plt>
mov    0x2d6b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1330 <main+0x190>
lea    -0x450(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    0x2d67(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x448(%rbp)
mov    -0x18(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x44c(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1070 <gethostbyname@plt>
mov    %rax,-0x440(%rbp)
lea    -0x450(%rbp),%rdi
mov    -0x440(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x440(%rbp),%rax
mov    (%rax),%edx
call   1080 <memcpy@plt>
mov    -0x14(%rbp),%edi
lea    -0x450(%rbp),%rsi
mov    $0xc,%edx
call   10b0 <connect@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    1309 <main+0x169>
mov    -0x18(%rbp),%esi
lea    0xd4e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
jmp    131a <main+0x17a>
mov    -0x18(%rbp),%esi
lea    0xd41(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1211 <main+0x71>
mov    -0x4(%rbp),%eax
add    $0x450,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
cmp    $0x3,%edi
jg     11d3 <main+0x23>
lea    0xe64(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
jmp    11f4 <main+0x44>
mov    %rsi,%r14
mov    0x10(%rsi),%rdi
call   10b0 <atoi@plt>
mov    %eax,%ebp
mov    0x18(%r14),%rdi
call   10b0 <atoi@plt>
mov    %eax,%r15d
xor    %eax,%eax
cmp    %r15d,%ebp
jle    1203 <main+0x53>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
add    $0x1,%r15d
lea    0x8(%rsp),%rbx
mov    0x2dd5(%rip),%r13
jmp    1240 <main+0x90>
cs nopw 0x0(%rax,%rax,1)
nop
lea    0xdf4(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %r12d,%edi
call   1070 <close@plt>
add    $0x1,%ebp
cmp    %ebp,%r15d
je     11cf <main+0x1f>
mov    0x2d79(%rip),%rax
mov    (%rax),%edi
mov    0x2d80(%rip),%rax
mov    (%rax),%esi
mov    0x2d87(%rip),%rax
mov    (%rax),%edx
call   10d0 <socket@plt>
cmp    $0xffffffff,%eax
je     12d6 <main+0x126>
mov    %eax,%r12d
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    0x0(%r13),%eax
mov    %eax,0x10(%rsp)
mov    %ebp,%edi
call   1040 <htons@plt>
mov    %eax,0xc(%rsp)
mov    0x8(%r14),%rdi
call   1080 <gethostbyname@plt>
mov    (%rax),%edx
mov    0x4(%rax),%esi
mov    %rbx,%rdi
call   1090 <memcpy@plt>
mov    %r12d,%edi
mov    %rbx,%rsi
mov    $0xc,%edx
call   10c0 <connect@plt>
cmp    $0xffffffff,%eax
jne    1220 <main+0x70>
lea    0xd4a(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %r12d,%edi
call   1070 <close@plt>
jmp    1230 <main+0x80>
lea    0xd23(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2cdf(%rip),%rax
mov    (%rax),%eax
jmp    11f4 <main+0x44>

LLVM IR (-O0):
%struct.hostent = type { i32, i32 }
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@.str = [6 x i8] c"shell\00"
@.str.1 = [51 x i8] c"Usage: ./main <IPaddress> <Start Port> <End Port>\0A\00"
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@.str.2 = [10 x i8] c"Socket()\0A\00"
@errno = global i32 0
@AF_INET = global i32 0
@.str.3 = [17 x i8] c"Port %d is open\0A\00"
@.str.4 = [19 x i8] c"Port %d is closed\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca [1024 x i8]
%12 = alloca i8*
%13 = alloca %struct.hostent*
%14 = alloca %struct.sockaddr_in
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8** %12
%15 = load i32, i32* %4
%16 = icmp slt i32 %15, 4
br i1 %16, label %17, label %19
17: ; preds = %2
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %3
br label %81
19: ; preds = %2
%20 = load i8**, i8*** %5
%21 = getelementptr inbounds i8*, i8** %20, i64 2
%22 = load i8*, i8** %21
%23 = call i32 @atoi(i8* %22)
store i32 %23, i32* %8
%24 = load i8**, i8*** %5
%25 = getelementptr inbounds i8*, i8** %24, i64 3
%26 = load i8*, i8** %25
%27 = call i32 @atoi(i8* %26)
store i32 %27, i32* %9
%28 = load i32, i32* %8
store i32 %28, i32* %7
br label %29
29: ; preds = %78, %19
%30 = load i32, i32* %7
%31 = load i32, i32* %9
%32 = icmp sle i32 %30, %31
br i1 %32, label %33, label %81
33: ; preds = %29
%34 = load i32, i32* @PF_INET
%35 = load i32, i32* @SOCK_STREAM
%36 = load i32, i32* @IPPROTO_TCP
%37 = call i32 @socket(i32 %34, i32 %35, i32 %36)
store i32 %37, i32* %6
%38 = load i32, i32* %6
%39 = icmp eq i32 %38, -1
br i1 %39, label %40, label %43
40: ; preds = %33
%41 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
%42 = load i32, i32* @errno
store i32 %42, i32* %3
br label %81
43: ; preds = %33
%44 = call i32 @memset(%struct.sockaddr_in* %14, i32 0, i32 12)
%45 = load i32, i32* @AF_INET
%46 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %14, i32 0, i32 2
store i32 %45, i32* %46
%47 = load i32, i32* %7
%48 = call i32 @htons(i32 %47)
%49 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %14, i32 0, i32 1
store i32 %48, i32* %49
%50 = load i8**, i8*** %5
%51 = getelementptr inbounds i8*, i8** %50, i64 1
%52 = load i8*, i8** %51
%53 = call %struct.hostent* @gethostbyname(i8* %52)
store %struct.hostent* %53, %struct.hostent** %13
%54 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %14, i32 0, i32 0
%55 = load %struct.hostent*, %struct.hostent** %13
%56 = getelementptr inbounds %struct.hostent, %struct.hostent* %55, i32 0, i32 1
%57 = load i32, i32* %56
%58 = load %struct.hostent*, %struct.hostent** %13
%59 = getelementptr inbounds %struct.hostent, %struct.hostent* %58, i32 0, i32 0
%60 = load i32, i32* %59
%61 = call i32 @memcpy(i32* %54, i32 %57, i32 %60)
%62 = load i32, i32* %6
%63 = bitcast %struct.sockaddr_in* %14 to %struct.sockaddr*
%64 = call i32 @connect(i32 %62, %struct.sockaddr* %63, i32 12)
store i32 %64, i32* %10
%65 = load i32, i32* %10
%66 = icmp eq i32 %65, -1
br i1 %66, label %67, label %72
67: ; preds = %43
%68 = load i32, i32* %7
%69 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), i32 %68)
%70 = load i32, i32* %6
%71 = call i32 @close(i32 %70)
br label %75
72: ; preds = %43
%73 = load i32, i32* %7
%74 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0), i32 %73)
br label %75
75: ; preds = %72, %67
%76 = load i32, i32* %6
%77 = call i32 @close(i32 %76)
br label %78
78: ; preds = %75
%79 = load i32, i32* %7
%80 = add nsw i32 %79, 1
store i32 %80, i32* %7
br label %29
81: ; preds = %17, %40, %29
%82 = load i32, i32* %3
ret i32 %82
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
%struct.hostent = type { i32, i32 }
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@.str.2 = [10 x i8] c"Socket()\0A\00"
@errno = global i32 0
@AF_INET = global i32 0
@.str.3 = [17 x i8] c"Port %d is open\0A\00"
@.str.4 = [19 x i8] c"Port %d is closed\0A\00"
@str = [50 x i8] c"Usage: ./main <IPaddress> <Start Port> <End Port>\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.sockaddr_in
%4 = bitcast %struct.sockaddr_in* %3 to i8*
%5 = icmp slt i32 %0, 4
br i1 %5, label %6, label %8
6: ; preds = %2
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([50 x i8], [50 x i8]* @str, i64 0, i64 0))
br label %54
8: ; preds = %2
%9 = getelementptr inbounds i8*, i8** %1, i64 2
%10 = load i8*, i8** %9
%11 = tail call i32 @atoi(i8* %10)
%12 = getelementptr inbounds i8*, i8** %1, i64 3
%13 = load i8*, i8** %12
%14 = tail call i32 @atoi(i8* %13)
%15 = icmp sgt i32 %11, %14
br i1 %15, label %54, label %16
16: ; preds = %8
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
%19 = getelementptr inbounds i8*, i8** %1, i64 1
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
%21 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
br label %22
22: ; preds = %16, %50
%23 = phi i32 [ %11, %16 ], [ %52, %50 ]
%24 = load i32, i32* @PF_INET
%25 = load i32, i32* @SOCK_STREAM
%26 = load i32, i32* @IPPROTO_TCP
%27 = call i32 @socket(i32 %24, i32 %25, i32 %26)
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %32
29: ; preds = %22
%30 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
%31 = load i32, i32* @errno
br label %54
32: ; preds = %22
%33 = call i32 @memset(%struct.sockaddr_in* %3, i32 0, i32 12)
%34 = load i32, i32* @AF_INET
store i32 %34, i32* %17
%35 = call i32 @htons(i32 %23)
store i32 %35, i32* %18
%36 = load i8*, i8** %19
%37 = call %struct.hostent* @gethostbyname(i8* %36)
%38 = getelementptr inbounds %struct.hostent, %struct.hostent* %37, i64 0, i32 1
%39 = load i32, i32* %38
%40 = getelementptr inbounds %struct.hostent, %struct.hostent* %37, i64 0, i32 0
%41 = load i32, i32* %40
%42 = call i32 @memcpy(i32* %20, i32 %39, i32 %41)
%43 = call i32 @connect(i32 %27, %struct.sockaddr* %21, i32 12)
%44 = icmp eq i32 %43, -1
br i1 %44, label %45, label %48
45: ; preds = %32
%46 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), i32 %23)
%47 = call i32 @close(i32 %27)
br label %50
48: ; preds = %32
%49 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0), i32 %23)
br label %50
50: ; preds = %48, %45
%51 = call i32 @close(i32 %27)
%52 = add i32 %23, 1
%53 = icmp eq i32 %23, %14
br i1 %53, label %54, label %22
54: ; preds = %50, %8, %29, %6
%55 = phi i32 [ 0, %6 ], [ %31, %29 ], [ 0, %8 ], [ 0, %50 ]
ret i32 %55
}