#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_family;
  int /*<<< orphan*/ sin_addr;
};
struct sockaddr {
  int dummy;
};
struct hostent {
  int /*<<< orphan*/ h_length;
  scalar_t__ *h_addr_list;
};
typedef int /*<<< orphan*/ dest;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ atoi(char *);
int /*<<< orphan*/ close(int);
int connect(int, struct sockaddr *, int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
struct hostent *gethostbyname(char *);
int /*<<< orphan*/ htons(int /*<<< orphan*/);
int /*<<< orphan*/ memcpy(void *, void *, int /*<<< orphan*/);
int /*<<< orphan*/ memset(void *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ shutdown(int, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int main(int argc, char *argv[]) {
  struct sockaddr_in dest;
  struct hostent *hp;
  int sock;
  if (argc != 3) {
    fprintf(stderr, "Usage : %s ip port\n", argv[0]);
    exit(1);
  }
  if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
    perror("socket");
    exit(1);
  }
  if ((hp = gethostbyname(argv[1])) == NULL) {
    perror("gethostbyname");
    exit(1);
  }
  memset((void *)&dest, 0, sizeof(dest));
  memcpy((void *)hp->h_addr_list[0], (void *)&dest.sin_addr, hp->h_length);
  dest.sin_family = AF_INET;
  dest.sin_port = htons(atoi(argv[2]));
  if (connect(sock, (struct sockaddr *)&dest, sizeof(dest)) == -1) {
    perror("connect");
    exit(1);
  }
  printf("CX OK\n");
  shutdown(sock, 2);
  close(sock);
  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   $0x3,-0x8(%rbp)
je     1216 <main+0x46>
mov    0x2de1(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xdf9(%rip),%rsi
call   1090 <fprintf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    0x2dcb(%rip),%rax
mov    (%rax),%edi
mov    0x2daa(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10f0 <socket@plt>
mov    %eax,-0x2c(%rbp)
cmp    $0xffffffff,%eax
jne    1251 <main+0x81>
lea    0xdd2(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1080 <gethostbyname@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    1282 <main+0xb2>
lea    0xda8(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    -0x28(%rbp),%rax
mov    0x8(%rax),%rax
mov    (%rax),%rdi
lea    -0x20(%rbp),%rsi
add    $0x8,%rsi
mov    -0x28(%rbp),%rax
mov    (%rax),%edx
call   10a0 <memcpy@plt>
mov    0x2d31(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   10c0 <atoi@plt>
mov    %eax,%edi
call   1040 <htons@plt>
mov    %eax,-0x20(%rbp)
mov    -0x2c(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   10e0 <connect@plt>
cmp    $0xffffffff,%eax
jne    1303 <main+0x133>
lea    0xd35(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
lea    0xd27(%rip),%rdi
call   1050 <printf@plt>
mov    -0x2c(%rbp),%edi
mov    $0x2,%esi
call   1030 <shutdown@plt>
mov    -0x2c(%rbp),%edi
call   1070 <close@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
cmp    $0x3,%edi
jne    12a5 <main+0xd5>
mov    0x2df9(%rip),%r12
mov    (%r12),%edi
mov    0x2dd6(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10f0 <socket@plt>
cmp    $0xffffffff,%eax
je     12c7 <main+0xf7>
mov    %eax,%ebp
mov    0x8(%r14),%rdi
call   1080 <gethostbyname@plt>
test   %rax,%rax
je     12d0 <main+0x100>
mov    %rax,%rbx
mov    %rsp,%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    0x8(%rbx),%rax
mov    (%rax),%rdi
lea    0x8(%rsp),%rsi
mov    (%rbx),%edx
call   10a0 <memcpy@plt>
mov    (%r12),%eax
mov    %eax,0x4(%rsp)
mov    0x10(%r14),%rdi
call   10c0 <atoi@plt>
mov    %eax,%edi
call   1050 <htons@plt>
mov    %eax,(%rsp)
mov    %ebp,%edi
mov    %r15,%rsi
mov    $0xc,%edx
call   10e0 <connect@plt>
cmp    $0xffffffff,%eax
je     12d9 <main+0x109>
lea    0xdb3(%rip),%rdi
call   1030 <puts@plt>
mov    %ebp,%edi
mov    $0x2,%esi
call   1040 <shutdown@plt>
mov    %ebp,%edi
call   1070 <close@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d2c(%rip),%rax
mov    (%rax),%edi
mov    (%r14),%rdx
lea    0xd48(%rip),%rsi
call   1090 <fprintf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
lea    0xd46(%rip),%rdi
jmp    12e0 <main+0x110>
lea    0xd44(%rip),%rdi
jmp    12e0 <main+0x110>
lea    0xd49(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.hostent = type { i32, i64* }
%struct.sockaddr = type { i32 }
@stderr = global i32 0
@.str = [20 x i8] c"Usage : %s ip port\0A\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [7 x i8] c"socket\00"
@.str.2 = [14 x i8] c"gethostbyname\00"
@.str.3 = [8 x i8] c"connect\00"
@.str.4 = [7 x i8] c"CX OK\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.sockaddr_in
%7 = alloca %struct.hostent*
%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 ne i32 %9, 3
br i1 %10, label %11, label %18
11: ; preds = %2
%12 = load i32, i32* @stderr
%13 = load i8**, i8*** %5
%14 = getelementptr inbounds i8*, i8** %13, i64 0
%15 = load i8*, i8** %14
%16 = call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %15)
%17 = call i32 @exit(i32 1)
unreachable
18: ; preds = %2
%19 = load i32, i32* @AF_INET
%20 = load i32, i32* @SOCK_STREAM
%21 = call i32 @socket(i32 %19, i32 %20, i32 0)
store i32 %21, i32* %8
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %26
23: ; preds = %18
%24 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%25 = call i32 @exit(i32 1)
unreachable
26: ; preds = %18
%27 = load i8**, i8*** %5
%28 = getelementptr inbounds i8*, i8** %27, i64 1
%29 = load i8*, i8** %28
%30 = call %struct.hostent* @gethostbyname(i8* %29)
store %struct.hostent* %30, %struct.hostent** %7
%31 = icmp eq %struct.hostent* %30, null
br i1 %31, label %32, label %35
32: ; preds = %26
%33 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
%34 = call i32 @exit(i32 1)
unreachable
35: ; preds = %26
%36 = bitcast %struct.sockaddr_in* %6 to i8*
%37 = call i32 @memset(i8* %36, i32 0, i32 12)
%38 = load %struct.hostent*, %struct.hostent** %7
%39 = getelementptr inbounds %struct.hostent, %struct.hostent* %38, i32 0, i32 1
%40 = load i64*, i64** %39
%41 = getelementptr inbounds i64, i64* %40, i64 0
%42 = load i64, i64* %41
%43 = inttoptr i64 %42 to i8*
%44 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 2
%45 = bitcast i32* %44 to i8*
%46 = load %struct.hostent*, %struct.hostent** %7
%47 = getelementptr inbounds %struct.hostent, %struct.hostent* %46, i32 0, i32 0
%48 = load i32, i32* %47
%49 = call i32 @memcpy(i8* %43, i8* %45, i32 %48)
%50 = load i32, i32* @AF_INET
%51 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 1
store i32 %50, i32* %51
%52 = load i8**, i8*** %5
%53 = getelementptr inbounds i8*, i8** %52, i64 2
%54 = load i8*, i8** %53
%55 = call i32 @atoi(i8* %54)
%56 = call i32 @htons(i32 %55)
%57 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
store i32 %56, i32* %57
%58 = load i32, i32* %8
%59 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%60 = call i32 @connect(i32 %58, %struct.sockaddr* %59, i32 12)
%61 = icmp eq i32 %60, -1
br i1 %61, label %62, label %65
62: ; preds = %35
%63 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
%64 = call i32 @exit(i32 1)
unreachable
65: ; preds = %35
%66 = call i32 @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
%67 = load i32, i32* %8
%68 = call i32 @shutdown(i32 %67, i32 2)
%69 = load i32, i32* %8
%70 = call i32 @close(i32 %69)
ret i32 0
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.hostent = type { i32, i64* }
%struct.sockaddr = type { i32 }
@stderr = global i32 0
@.str = [20 x i8] c"Usage : %s ip port\0A\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [7 x i8] c"socket\00"
@.str.2 = [14 x i8] c"gethostbyname\00"
@.str.3 = [8 x i8] c"connect\00"
@str = [6 x i8] c"CX OK\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, 3
br i1 %5, label %11, label %6
6: ; preds = %2
%7 = load i32, i32* @stderr
%8 = load i8*, i8** %1
%9 = tail call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %8)
%10 = tail call i32 @exit(i32 1)
unreachable
11: ; preds = %2
%12 = load i32, i32* @AF_INET
%13 = load i32, i32* @SOCK_STREAM
%14 = tail call i32 @socket(i32 %12, i32 %13, i32 0)
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %19
16: ; preds = %11
%17 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%18 = tail call i32 @exit(i32 1)
unreachable
19: ; preds = %11
%20 = getelementptr inbounds i8*, i8** %1, i64 1
%21 = load i8*, i8** %20
%22 = tail call %struct.hostent* @gethostbyname(i8* %21)
%23 = icmp eq %struct.hostent* %22, null
br i1 %23, label %24, label %27
24: ; preds = %19
%25 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
%26 = tail call i32 @exit(i32 1)
unreachable
27: ; preds = %19
%28 = call i32 @memset(i8* %4, i32 0, i32 12)
%29 = getelementptr inbounds %struct.hostent, %struct.hostent* %22, i64 0, i32 1
%30 = load i64*, i64** %29
%31 = load i64, i64* %30
%32 = inttoptr i64 %31 to i8*
%33 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
%34 = bitcast i32* %33 to i8*
%35 = getelementptr inbounds %struct.hostent, %struct.hostent* %22, i64 0, i32 0
%36 = load i32, i32* %35
%37 = call i32 @memcpy(i8* %32, i8* %34, i32 %36)
%38 = load i32, i32* @AF_INET
%39 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i32 %38, i32* %39
%40 = getelementptr inbounds i8*, i8** %1, i64 2
%41 = load i8*, i8** %40
%42 = call i32 @atoi(i8* %41)
%43 = call i32 @htons(i32 %42)
%44 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %43, i32* %44
%45 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%46 = call i32 @connect(i32 %14, %struct.sockaddr* %45, i32 12)
%47 = icmp eq i32 %46, -1
br i1 %47, label %48, label %51
48: ; preds = %27
%49 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
%50 = call i32 @exit(i32 1)
unreachable
51: ; preds = %27
%52 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @str, i64 0, i64 0))
%53 = call i32 @shutdown(i32 %14, i32 2)
%54 = call i32 @close(i32 %14)
ret i32 0
}