#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_port;
  int /*<<< orphan*/ sin_addr;
  int /*<<< orphan*/ sin_family;
  int /*<<< orphan*/ sin_zero;
};
struct sockaddr {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
scalar_t__ inet_pton(int /*<<< orphan*/, char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ memset(int /*<<< orphan*/ *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stdout;
int /*<<< orphan*/ usage(char *);
int main(int argc, char **argv) {
  int sockfd;
  struct sockaddr_in cl_addr;
  int counter;
  if ((argc < 2) || (argc > 2))
    usage(argv[0]);
  memset(&(cl_addr.sin_zero), 0, sizeof(cl_addr.sin_zero));
  cl_addr.sin_family = AF_INET;
  if (inet_pton(AF_INET, argv[1], &cl_addr.sin_addr))
    printf("pton conversion successful\n");
  for (counter = 0; counter < 65536; counter++) {
    cl_addr.sin_port = htons(counter);
    if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
      perror("Error in socket()");
    if ((connect(sockfd, (struct sockaddr *)&cl_addr,
                 sizeof(struct sockaddr_in)) == 0))
      printf("Port[%d]: open\n", counter);
    else
      fflush(stdout);
    close(sockfd);
  }
  fflush(stdout);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jl     11ca <main+0x2a>
cmpl   $0x2,-0x8(%rbp)
jle    11d6 <main+0x36>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1090 <usage@plt>
lea    -0x28(%rbp),%rdi
add    $0xc,%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1050 <memset@plt>
mov    0x2df7(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2deb(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    -0x28(%rbp),%rdx
add    $0x4,%rdx
call   1070 <inet_pton@plt>
cmp    $0x0,%rax
je     122c <main+0x8c>
lea    0xddb(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x2c(%rbp)
cmpl   $0x10000,-0x2c(%rbp)
jge    12d1 <main+0x131>
mov    -0x2c(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x28(%rbp)
mov    0x2d96(%rip),%rax
mov    (%rax),%edi
mov    0x2d7d(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    127c <main+0xdc>
lea    0xda5(%rip),%rdi
call   10a0 <perror@plt>
mov    -0x14(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0x10,%edx
call   10b0 <connect@plt>
cmp    $0x0,%rax
jne    12ad <main+0x10d>
mov    -0x2c(%rbp),%esi
lea    0xd8d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    12bb <main+0x11b>
mov    0x2d1c(%rip),%rax
mov    (%rax),%edi
call   1080 <fflush@plt>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    1233 <main+0x93>
mov    0x2cf8(%rip),%rax
mov    (%rax),%edi
call   1080 <fflush@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%rbx
cmp    $0x2,%edi
je     11ce <main+0x1e>
mov    (%rbx),%rdi
call   10a0 <usage@plt>
lea    0x14(%rsp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1060 <memset@plt>
mov    0x2e02(%rip),%r13
mov    0x0(%r13),%edi
mov    %edi,0x10(%rsp)
mov    0x8(%rbx),%rsi
lea    0xc(%rsp),%rdx
call   1080 <inet_pton@plt>
test   %rax,%rax
je     120d <main+0x5d>
lea    0xe1a(%rip),%rdi
call   1030 <puts@plt>
mov    0x2dc4(%rip),%r14
lea    0x8(%rsp),%r12
mov    0x2db0(%rip),%r15
xor    %ebp,%ebp
jmp    124a <main+0x9a>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    (%r15),%edi
call   1090 <fflush@plt>
mov    %ebx,%edi
call   1070 <close@plt>
add    $0x1,%ebp
cmp    $0x10000,%ebp
je     129e <main+0xee>
mov    %ebp,%edi
call   1040 <htons@plt>
mov    %eax,0x8(%rsp)
mov    0x0(%r13),%edi
mov    (%r14),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
je     1290 <main+0xe0>
mov    %ebx,%edi
mov    %r12,%rsi
mov    $0x10,%edx
call   10c0 <connect@plt>
test   %rax,%rax
jne    1230 <main+0x80>
lea    0xd8d(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    1238 <main+0x88>
lea    0xd69(%rip),%rdi
call   10b0 <perror@plt>
jmp    126a <main+0xba>
mov    (%r15),%edi
call   1090 <fflush@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@.str = [28 x i8] c"pton conversion successful\0A\00"
@SOCK_STREAM = global i32 0
@.str.1 = [18 x i8] c"Error in socket()\00"
@.str.2 = [16 x i8] c"Port[%d]: open\0A\00"
@stdout = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca %struct.sockaddr_in
%8 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i32, i32* %4
%10 = icmp slt i32 %9, 2
br i1 %10, label %14, label %11
11: ; preds = %2
%12 = load i32, i32* %4
%13 = icmp sgt i32 %12, 2
br i1 %13, label %14, label %19
14: ; preds = %11, %2
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 0
%17 = load i8*, i8** %16
%18 = call i32 @usage(i8* %17)
br label %19
19: ; preds = %14, %11
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 3
%21 = call i32 @memset(i32* %20, i32 0, i32 4)
%22 = load i32, i32* @AF_INET
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 2
store i32 %22, i32* %23
%24 = load i32, i32* @AF_INET
%25 = load i8**, i8*** %5
%26 = getelementptr inbounds i8*, i8** %25, i64 1
%27 = load i8*, i8** %26
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 1
%29 = call i64 @inet_pton(i32 %24, i8* %27, i32* %28)
%30 = icmp ne i64 %29, 0
br i1 %30, label %31, label %33
31: ; preds = %19
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
br label %33
33: ; preds = %31, %19
store i32 0, i32* %8
br label %34
34: ; preds = %61, %33
%35 = load i32, i32* %8
%36 = icmp slt i32 %35, 65536
br i1 %36, label %37, label %64
37: ; preds = %34
%38 = load i32, i32* %8
%39 = call i32 @htons(i32 %38)
%40 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 0
store i32 %39, i32* %40
%41 = load i32, i32* @AF_INET
%42 = load i32, i32* @SOCK_STREAM
%43 = call i32 @socket(i32 %41, i32 %42, i32 0)
store i32 %43, i32* %6
%44 = icmp eq i32 %43, -1
br i1 %44, label %45, label %47
45: ; preds = %37
%46 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
br label %47
47: ; preds = %45, %37
%48 = load i32, i32* %6
%49 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%50 = call i64 @connect(i32 %48, %struct.sockaddr* %49, i32 16)
%51 = icmp eq i64 %50, 0
br i1 %51, label %52, label %55
52: ; preds = %47
%53 = load i32, i32* %8
%54 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i32 %53)
br label %58
55: ; preds = %47
%56 = load i32, i32* @stdout
%57 = call i32 @fflush(i32 %56)
br label %58
58: ; preds = %55, %52
%59 = load i32, i32* %6
%60 = call i32 @close(i32 %59)
br label %61
61: ; preds = %58
%62 = load i32, i32* %8
%63 = add nsw i32 %62, 1
store i32 %63, i32* %8
br label %34
64: ; preds = %34
%65 = load i32, i32* @stdout
%66 = call i32 @fflush(i32 %65)
ret i32 0
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [18 x i8] c"Error in socket()\00"
@.str.2 = [16 x i8] c"Port[%d]: open\0A\00"
@stdout = global i32 0
@str = [27 x i8] c"pton conversion successful\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.sockaddr_in
%4 = bitcast %struct.sockaddr_in* %3 to i8*
%5 = icmp eq i32 %0, 2
br i1 %5, label %9, label %6
6: ; preds = %2
%7 = load i8*, i8** %1
%8 = tail call i32 @usage(i8* %7)
br label %9
9: ; preds = %2, %6
%10 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 3
%11 = call i32 @memset(i32* %10, i32 0, i32 4)
%12 = load i32, i32* @AF_INET
%13 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %12, i32* %13
%14 = getelementptr inbounds i8*, i8** %1, i64 1
%15 = load i8*, i8** %14
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
%17 = call i64 @inet_pton(i32 %12, i8* %15, i32* %16)
%18 = icmp eq i64 %17, 0
br i1 %18, label %21, label %19
19: ; preds = %9
%20 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str, i64 0, i64 0))
br label %21
21: ; preds = %19, %9
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
%23 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
br label %24
24: ; preds = %21, %41
%25 = phi i32 [ 0, %21 ], [ %43, %41 ]
%26 = call i32 @htons(i32 %25)
store i32 %26, i32* %22
%27 = load i32, i32* @AF_INET
%28 = load i32, i32* @SOCK_STREAM
%29 = call i32 @socket(i32 %27, i32 %28, i32 0)
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %33
31: ; preds = %24
%32 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
br label %33
33: ; preds = %31, %24
%34 = call i64 @connect(i32 %29, %struct.sockaddr* %23, i32 16)
%35 = icmp eq i64 %34, 0
br i1 %35, label %36, label %38
36: ; preds = %33
%37 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i32 %25)
br label %41
38: ; preds = %33
%39 = load i32, i32* @stdout
%40 = call i32 @fflush(i32 %39)
br label %41
41: ; preds = %38, %36
%42 = call i32 @close(i32 %29)
%43 = add nuw nsw i32 %25, 1
%44 = icmp eq i32 %43, 65536
br i1 %44, label %45, label %24
45: ; preds = %41
%46 = load i32, i32* @stdout
%47 = call i32 @fflush(i32 %46)
ret i32 0
}