#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;
};
struct sockaddr {
  int dummy;
};
struct hostent {
  int /*<<< orphan*/ h_length;
  int /*<<< orphan*/ h_addr;
};
typedef int /*<<< orphan*/ remote_server;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ atoi(char *);
int /*<<< orphan*/ bcopy(int /*<<< orphan*/, struct sockaddr_in *,
                         int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/);
struct hostent *gethostbyname(char *);
int /*<<< orphan*/ htons(int /*<<< orphan*/);
int /*<<< orphan*/ puts(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stdout;
int main(int argc, char **argv) {
  int out_socket;
  struct sockaddr_in remote_server;
  struct hostent *hp;
  char buf[1024];
  if (argv[1][0] == '\0') {
    puts("Usage:  hostcheck <hostname> <port>\n\n");
    exit(1);
  }
  out_socket = socket(AF_INET, SOCK_STREAM, 0);
  remote_server.sin_family = AF_INET;
  hp = gethostbyname(argv[1]);
  if (hp == NULL) {
    fputs("BADHOST", stdout);
    exit(1);
  }
  remote_server.sin_port = htons(atoi(argv[2]));
  bcopy(hp->h_addr, &remote_server, hp->h_length);
  if (connect(out_socket, (struct sockaddr *)&remote_server,
              sizeof(remote_server)) >= 0) {
    close(out_socket);
  }
  fputs("HOSTOK", stdout);
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    11e3 <main+0x43>
lea    0xe2c(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    0x2dfe(%rip),%rax
mov    (%rax),%edi
mov    0x2de5(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,-0x14(%rbp)
mov    0x2de2(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1070 <gethostbyname@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1246 <main+0xa6>
mov    0x2da2(%rip),%rax
mov    (%rax),%esi
lea    0xdef(%rip),%rdi
call   1050 <fputs@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   1090 <atoi@plt>
mov    %eax,%edi
call   1040 <htons@plt>
mov    %eax,-0x20(%rbp)
mov    -0x28(%rbp),%rax
mov    0x4(%rax),%edi
mov    -0x28(%rbp),%rax
mov    (%rax),%edx
lea    -0x20(%rbp),%rsi
call   1080 <bcopy@plt>
mov    -0x14(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x8,%edx
call   10b0 <connect@plt>
cmp    $0x0,%rax
jl     1296 <main+0xf6>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
mov    0x2d33(%rip),%rax
mov    (%rax),%esi
lea    0xd88(%rip),%rdi
call   1050 <fputs@plt>
xor    %edi,%edi
call   10a0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    0x8(%rsi),%rax
cmpb   $0x0,(%rax)
jne    11c7 <main+0x27>
lea    0xe48(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    %rsi,%rbx
mov    0x2e17(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x2dfd(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,%r14d
mov    0x0(%rbp),%eax
mov    %eax,0xc(%rsp)
mov    0x8(%rbx),%rdi
call   1070 <gethostbyname@plt>
test   %rax,%rax
jne    121b <main+0x7b>
mov    0x2dcd(%rip),%rax
mov    (%rax),%esi
lea    0xe1a(%rip),%rdi
call   1050 <fputs@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    %rax,%rbp
mov    0x10(%rbx),%rdi
call   1090 <atoi@plt>
mov    %eax,%edi
call   1040 <htons@plt>
mov    %eax,0x8(%rsp)
mov    0x0(%rbp),%edx
mov    0x4(%rbp),%edi
lea    0x8(%rsp),%rbx
mov    %rbx,%rsi
call   1080 <bcopy@plt>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    $0x8,%edx
call   10b0 <connect@plt>
test   %rax,%rax
js     1262 <main+0xc2>
mov    %r14d,%edi
call   1060 <close@plt>
mov    0x2d67(%rip),%rax
mov    (%rax),%esi
lea    0xdbc(%rip),%rdi
call   1050 <fputs@plt>
xor    %edi,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32 }
%struct.hostent = type { i32, i32 }
%struct.sockaddr = type { i32 }
@.str = [38 x i8] c"Usage: hostcheck <hostname> <port>\0A\0A\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [8 x i8] c"BADHOST\00"
@stdout = global i32 0
@.str.2 = [7 x i8] c"HOSTOK\00"
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 %struct.hostent*
%9 = alloca [1024 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i8**, i8*** %5
%11 = getelementptr inbounds i8*, i8** %10, i64 1
%12 = load i8*, i8** %11
%13 = getelementptr inbounds i8, i8* %12, i64 0
%14 = load i8, i8* %13
%15 = sext i8 %14 to i32
%16 = icmp eq i32 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %2
%18 = call i32 @puts(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0))
%19 = call i32 @exit(i32 1)
unreachable
20: ; preds = %2
%21 = load i32, i32* @AF_INET
%22 = load i32, i32* @SOCK_STREAM
%23 = call i32 @socket(i32 %21, i32 %22, i32 0)
store i32 %23, i32* %6
%24 = load i32, i32* @AF_INET
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 1
store i32 %24, i32* %25
%26 = load i8**, i8*** %5
%27 = getelementptr inbounds i8*, i8** %26, i64 1
%28 = load i8*, i8** %27
%29 = call %struct.hostent* @gethostbyname(i8* %28)
store %struct.hostent* %29, %struct.hostent** %8
%30 = load %struct.hostent*, %struct.hostent** %8
%31 = icmp eq %struct.hostent* %30, null
br i1 %31, label %32, label %36
32: ; preds = %20
%33 = load i32, i32* @stdout
%34 = call i32 @fputs(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i32 %33)
%35 = call i32 @exit(i32 1)
unreachable
36: ; preds = %20
%37 = load i8**, i8*** %5
%38 = getelementptr inbounds i8*, i8** %37, i64 2
%39 = load i8*, i8** %38
%40 = call i32 @atoi(i8* %39)
%41 = call i32 @htons(i32 %40)
%42 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 0
store i32 %41, i32* %42
%43 = load %struct.hostent*, %struct.hostent** %8
%44 = getelementptr inbounds %struct.hostent, %struct.hostent* %43, i32 0, i32 1
%45 = load i32, i32* %44
%46 = load %struct.hostent*, %struct.hostent** %8
%47 = getelementptr inbounds %struct.hostent, %struct.hostent* %46, i32 0, i32 0
%48 = load i32, i32* %47
%49 = call i32 @bcopy(i32 %45, %struct.sockaddr_in* %7, i32 %48)
%50 = load i32, i32* %6
%51 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%52 = call i64 @connect(i32 %50, %struct.sockaddr* %51, i32 8)
%53 = icmp sge i64 %52, 0
br i1 %53, label %54, label %57
54: ; preds = %36
%55 = load i32, i32* %6
%56 = call i32 @close(i32 %55)
br label %57
57: ; preds = %54, %36
%58 = load i32, i32* @stdout
%59 = call i32 @fputs(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i32 %58)
%60 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32 }
%struct.hostent = type { i32, i32 }
%struct.sockaddr = type { i32 }
@.str = [38 x i8] c"Usage: hostcheck <hostname> <port>\0A\0A\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [8 x i8] c"BADHOST\00"
@stdout = global i32 0
@.str.2 = [7 x i8] c"HOSTOK\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.sockaddr_in
%4 = bitcast %struct.sockaddr_in* %3 to i8*
%5 = getelementptr inbounds i8*, i8** %1, i64 1
%6 = load i8*, i8** %5
%7 = load i8, i8* %6
%8 = icmp eq i8 %7, 0
br i1 %8, label %9, label %12
9: ; preds = %2
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0))
%11 = tail call i32 @exit(i32 1)
unreachable
12: ; preds = %2
%13 = load i32, i32* @AF_INET
%14 = load i32, i32* @SOCK_STREAM
%15 = tail call i32 @socket(i32 %13, i32 %14, i32 0)
%16 = load i32, i32* @AF_INET
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i32 %16, i32* %17
%18 = load i8*, i8** %5
%19 = tail call %struct.hostent* @gethostbyname(i8* %18)
%20 = icmp eq %struct.hostent* %19, null
br i1 %20, label %21, label %25
21: ; preds = %12
%22 = load i32, i32* @stdout
%23 = tail call i32 @fputs(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i32 %22)
%24 = tail call i32 @exit(i32 1)
unreachable
25: ; preds = %12
%26 = getelementptr inbounds i8*, i8** %1, i64 2
%27 = load i8*, i8** %26
%28 = tail call i32 @atoi(i8* %27)
%29 = tail call i32 @htons(i32 %28)
%30 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %29, i32* %30
%31 = getelementptr inbounds %struct.hostent, %struct.hostent* %19, i64 0, i32 1
%32 = load i32, i32* %31
%33 = getelementptr inbounds %struct.hostent, %struct.hostent* %19, i64 0, i32 0
%34 = load i32, i32* %33
%35 = call i32 @bcopy(i32 %32, %struct.sockaddr_in* %3, i32 %34)
%36 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%37 = call i64 @connect(i32 %15, %struct.sockaddr* %36, i32 8)
%38 = icmp sgt i64 %37, -1
br i1 %38, label %39, label %41
39: ; preds = %25
%40 = call i32 @close(i32 %15)
br label %41
41: ; preds = %39, %25
%42 = load i32, i32* @stdout
%43 = call i32 @fputs(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i32 %42)
%44 = call i32 @exit(i32 0)
unreachable
}