#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 */
typedef struct TYPE_2__ TYPE_1__;

/* Type definitions */
struct TYPE_2__ {
  int /*<<< orphan*/ s_addr;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_port;
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ dest;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
#define EADDRNOTAVAIL 131
#define EALREADY 130
#define ECONNREFUSED 129
#define ENETUNREACH 128
int EXIT_SUCCESS;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
int connect(int, struct sockaddr *, int);
int errno;
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ inet_addr(char *);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ send(int, char *, int, int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int main(int argc, char **argv) {
  if (argc < 2) {
    printf("Usage: ./servkill [IP_ADDR]\n");
    return -1;
  }
  int len, mysocket;
  struct sockaddr_in dest;
  mysocket = socket(AF_INET, SOCK_STREAM, 0);
  memset(&dest, 0, sizeof(dest));
  dest.sin_family = AF_INET;
  dest.sin_addr.s_addr = inet_addr(argv[1]);
  dest.sin_port = htons(8100);
  int conn_res =
      connect(mysocket, (struct sockaddr *)&dest, sizeof(struct sockaddr));
  switch (errno) {
  case ECONNREFUSED:
    printf("Connection refused! Aborting!!\n");
    return -1;
  case EADDRNOTAVAIL:
    printf("Address not available from this machine, aborting!!\n");
  case EALREADY:
    printf("Connection already in progress on socket, aborting!!\n");
  case ENETUNREACH:
    printf("No route to address present on network, aborting!!\n");
  default:
    if (conn_res < 0) {
      printf("Unknown error, aborting!!\n");
      return -1;
    }
  }
  send(mysocket, "e", 1, 0);
  close(mysocket);
  return EXIT_SUCCESS;
}


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    11b8 <main+0x38>
lea    0xe69(%rip),%rdi
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12e6 <main+0x166>
mov    0x2e29(%rip),%rax
mov    %rax,-0x48(%rbp)
mov    (%rax),%edi
mov    0x2e0c(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
mov    %edx,-0x4c(%rbp)
call   10a0 <socket@plt>
mov    -0x4c(%rbp),%esi
mov    %eax,-0x18(%rbp)
lea    -0x28(%rbp),%rdi
mov    %rdi,-0x40(%rbp)
mov    $0xc,%edx
call   1060 <memset@plt>
mov    -0x48(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1080 <inet_addr@plt>
mov    %eax,-0x24(%rbp)
mov    $0x1fa4,%edi
call   1030 <htons@plt>
mov    -0x40(%rbp),%rsi
mov    %eax,-0x28(%rbp)
mov    -0x18(%rbp),%edi
mov    $0x4,%edx
call   1090 <connect@plt>
mov    %eax,-0x2c(%rbp)
mov    0x2d9f(%rip),%rax
mov    (%rax),%eax
add    $0xffffff80,%eax
mov    %eax,%ecx
mov    %rcx,-0x38(%rbp)
sub    $0x3,%eax
ja     1295 <main+0x115>
mov    -0x38(%rbp),%rax
lea    0xdb0(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
lea    0xdcd(%rip),%rdi
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12e6 <main+0x166>
lea    0xdd5(%rip),%rdi
call   1050 <printf@plt>
lea    0xdfe(%rip),%rdi
call   1050 <printf@plt>
lea    0xe28(%rip),%rdi
call   1050 <printf@plt>
cmpl   $0x0,-0x2c(%rbp)
jge    12b7 <main+0x137>
lea    0xe46(%rip),%rdi
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12e6 <main+0x166>
jmp    12bc <main+0x13c>
mov    -0x18(%rbp),%edi
lea    0xe41(%rip),%rsi
mov    $0x1,%edx
xor    %ecx,%ecx
call   1040 <send@plt>
mov    -0x18(%rbp),%edi
call   1070 <close@plt>
mov    0x2ce7(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
cmp    $0x1,%edi
jg     119b <main+0x1b>
lea    0xf51(%rip),%rdi
jmp    1283 <main+0x103>
mov    %rsi,%rbp
mov    0x2e43(%rip),%rbx
mov    (%rbx),%edi
mov    0x2e2a(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,%r14d
lea    0x8(%rsp),%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    (%rbx),%eax
mov    %eax,0x10(%rsp)
mov    0x8(%rbp),%rdi
call   1080 <inet_addr@plt>
mov    %eax,0xc(%rsp)
mov    $0x1fa4,%edi
call   1040 <htons@plt>
mov    %eax,0x8(%rsp)
mov    %r14d,%edi
mov    %r15,%rsi
mov    $0x4,%edx
call   1090 <connect@plt>
mov    %eax,%ebp
mov    0x2dc8(%rip),%rax
mov    (%rax),%eax
add    $0xffffff80,%eax
cmp    $0x3,%eax
ja     124f <main+0xcf>
lea    0xde7(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
lea    0xe9f(%rip),%rdi
jmp    1283 <main+0x103>
lea    0xe62(%rip),%rdi
call   1030 <puts@plt>
lea    0xe21(%rip),%rdi
call   1030 <puts@plt>
lea    0xdc8(%rip),%rdi
call   1030 <puts@plt>
test   %ebp,%ebp
js     127c <main+0xfc>
lea    0xdb6(%rip),%rsi
mov    %r14d,%edi
mov    $0x1,%edx
xor    %ecx,%ecx
call   1050 <send@plt>
mov    %r14d,%edi
call   1070 <close@plt>
mov    0x2d50(%rip),%rax
mov    (%rax),%eax
jmp    128d <main+0x10d>
lea    0xdc2(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [29 x i8] c"Usage: ./servkill [IP_ADDR]\0A\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@errno = global i32 0
@.str.1 = [32 x i8] c"Connection refused! Aborting!!\0A\00"
@.str.2 = [53 x i8] c"Address not available from this machine, aborting!!\0A\00"
@.str.3 = [54 x i8] c"Connection already in progress on socket, aborting!!\0A\00"
@.str.4 = [52 x i8] c"No route to address present on network, aborting!!\0A\00"
@.str.5 = [27 x i8] c"Unknown error, aborting!!\0A\00"
@.str.6 = [2 x i8] c"e\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.sockaddr_in
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* %4
%11 = icmp slt i32 %10, 2
br i1 %11, label %12, label %14
12: ; preds = %2
%13 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %53
14: ; preds = %2
%15 = load i32, i32* @AF_INET
%16 = load i32, i32* @SOCK_STREAM
%17 = call i32 @socket(i32 %15, i32 %16, i32 0)
store i32 %17, i32* %7
%18 = call i32 @memset(%struct.sockaddr_in* %8, i32 0, i32 12)
%19 = load i32, i32* @AF_INET
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 2
store i32 %19, i32* %20
%21 = load i8**, i8*** %5
%22 = getelementptr inbounds i8*, i8** %21, i64 1
%23 = load i8*, i8** %22
%24 = call i32 @inet_addr(i8* %23)
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 1
%26 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %25, i32 0, i32 0
store i32 %24, i32* %26
%27 = call i32 @htons(i32 8100)
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 0
store i32 %27, i32* %28
%29 = load i32, i32* %7
%30 = bitcast %struct.sockaddr_in* %8 to %struct.sockaddr*
%31 = call i32 @connect(i32 %29, %struct.sockaddr* %30, i32 4)
store i32 %31, i32* %9
%32 = load i32, i32* @errno
switch i32 %32, label %41 [
i32 129, label %33
i32 131, label %35
i32 130, label %37
i32 128, label %39
]
33: ; preds = %14
%34 = call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %53
35: ; preds = %14
%36 = call i32 @printf(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.2, i64 0, i64 0))
br label %37
37: ; preds = %14, %35
%38 = call i32 @printf(i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.3, i64 0, i64 0))
br label %39
39: ; preds = %14, %37
%40 = call i32 @printf(i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.4, i64 0, i64 0))
br label %41
41: ; preds = %14, %39
%42 = load i32, i32* %9
%43 = icmp slt i32 %42, 0
br i1 %43, label %44, label %46
44: ; preds = %41
%45 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.5, i64 0, i64 0))
store i32 -1, i32* %3
br label %53
46: ; preds = %41
br label %47
47: ; preds = %46
%48 = load i32, i32* %7
%49 = call i32 @send(i32 %48, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0), i32 1, i32 0)
%50 = load i32, i32* %7
%51 = call i32 @close(i32 %50)
%52 = load i32, i32* @EXIT_SUCCESS
store i32 %52, i32* %3
br label %53
53: ; preds = %47, %44, %33, %12
%54 = load i32, i32* %3
ret i32 %54
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@errno = global i32 0
@.str.6 = [2 x i8] c"e\00"
@EXIT_SUCCESS = global i32 0
@str = [51 x i8] c"No route to address present on network, aborting!!\00"
@str.7 = [26 x i8] c"Unknown error, aborting!!\00"
@str.8 = [53 x i8] c"Connection already in progress on socket, aborting!!\00"
@str.9 = [52 x i8] c"Address not available from this machine, aborting!!\00"
@str.10 = [31 x i8] c"Connection refused! Aborting!!\00"
@str.11 = [28 x i8] c"Usage: ./servkill [IP_ADDR]\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.sockaddr_in
%4 = icmp slt i32 %0, 2
br i1 %4, label %5, label %7
5: ; preds = %2
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @str.11, i64 0, i64 0))
br label %42
7: ; preds = %2
%8 = bitcast %struct.sockaddr_in* %3 to i8*
%9 = load i32, i32* @AF_INET
%10 = load i32, i32* @SOCK_STREAM
%11 = tail call i32 @socket(i32 %9, i32 %10, i32 0)
%12 = call i32 @memset(%struct.sockaddr_in* %3, i32 0, i32 12)
%13 = load i32, i32* @AF_INET
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %13, i32* %14
%15 = getelementptr inbounds i8*, i8** %1, i64 1
%16 = load i8*, i8** %15
%17 = call i32 @inet_addr(i8* %16)
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1, i32 0
store i32 %17, i32* %18
%19 = call i32 @htons(i32 8100)
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %19, i32* %20
%21 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%22 = call i32 @connect(i32 %11, %struct.sockaddr* %21, i32 4)
%23 = load i32, i32* @errno
switch i32 %23, label %32 [
i32 129, label %24
i32 131, label %26
i32 130, label %28
i32 128, label %30
]
24: ; preds = %7
%25 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @str.10, i64 0, i64 0))
br label %40
26: ; preds = %7
%27 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([52 x i8], [52 x i8]* @str.9, i64 0, i64 0))
br label %28
28: ; preds = %7, %26
%29 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([53 x i8], [53 x i8]* @str.8, i64 0, i64 0))
br label %30
30: ; preds = %7, %28
%31 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([51 x i8], [51 x i8]* @str, i64 0, i64 0))
br label %32
32: ; preds = %7, %30
%33 = icmp slt i32 %22, 0
br i1 %33, label %34, label %36
34: ; preds = %32
%35 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @str.7, i64 0, i64 0))
br label %40
36: ; preds = %32
%37 = call i32 @send(i32 %11, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0), i32 1, i32 0)
%38 = call i32 @close(i32 %11)
%39 = load i32, i32* @EXIT_SUCCESS
br label %40
40: ; preds = %36, %34, %24
%41 = phi i32 [ -1, %34 ], [ %39, %36 ], [ -1, %24 ]
br label %42
42: ; preds = %40, %5
%43 = phi i32 [ -1, %5 ], [ %41, %40 ]
ret i32 %43
}