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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int connect(int, struct sockaddr *, int);
int /*<<< orphan*/ exit(int);
struct hostent *gethostbyname(char *);
TYPE_1__ *getprotobyname(char *);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ memcpy(int /*<<< orphan*/ *, int /*<<< orphan*/,
                          int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int openhost(char *host, int port) {
  int sock;
  struct sockaddr_in addr;
  struct hostent *he;
  he = gethostbyname(host);
  if (he == NULL) {
    perror("gethostbyname()");
    exit(-1);
  }
  sock = socket(AF_INET, SOCK_STREAM, getprotobyname("tcp")->p_proto);
  if (sock == -1) {
    perror("socket()");
    exit(-1);
  }
  memcpy(&addr.sin_addr, he->h_addr, he->h_length);
  addr.sin_family = AF_INET;
  addr.sin_port = htons(port);
  if (connect(sock, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
    perror("connect()");
    exit(-1);
  }
  return sock;
}


x86 assembly (clang, -O0):
<openhost>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <gethostbyname@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11bd <openhost+0x3d>
lea    0xe52(%rip),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>
mov    0x2e24(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    0x2e08(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
lea    0xe34(%rip),%rdi
call   1060 <getprotobyname@plt>
mov    -0x30(%rbp),%edi
mov    -0x2c(%rbp),%esi
mov    (%rax),%edx
call   10a0 <socket@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0xffffffff,-0x10(%rbp)
jne    1211 <openhost+0x91>
lea    0xe12(%rip),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>
lea    -0x20(%rbp),%rdi
add    $0x8,%rdi
mov    -0x28(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x28(%rbp),%rax
mov    (%rax),%edx
call   1050 <memcpy@plt>
mov    0x2db6(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0xc(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   1090 <connect@plt>
cmp    $0xffffffff,%eax
jne    1272 <openhost+0xf2>
lea    0xdba(%rip),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>
mov    -0x10(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<openhost>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %esi,%r14d
call   1040 <gethostbyname@plt>
test   %rax,%rax
je     1214 <openhost+0x94>
mov    %rax,%rbx
mov    0x2e45(%rip),%r12
mov    (%r12),%r15d
mov    0x2e2a(%rip),%rax
mov    (%rax),%ebp
lea    0xe59(%rip),%rdi
call   1060 <getprotobyname@plt>
mov    (%rax),%edx
mov    %r15d,%edi
mov    %ebp,%esi
call   10a0 <socket@plt>
cmp    $0xffffffff,%eax
je     121d <openhost+0x9d>
mov    %eax,%ebp
lea    0x8(%rsp),%rdi
mov    (%rbx),%edx
mov    0x4(%rbx),%esi
call   1050 <memcpy@plt>
mov    (%r12),%eax
mov    %eax,0x4(%rsp)
mov    %r14d,%edi
call   1030 <htons@plt>
mov    %eax,(%rsp)
mov    %rsp,%rsi
mov    %ebp,%edi
mov    $0xc,%edx
call   1090 <connect@plt>
cmp    $0xffffffff,%eax
je     1226 <openhost+0xa6>
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xde5(%rip),%rdi
jmp    122d <openhost+0xad>
lea    0xdf0(%rip),%rdi
jmp    122d <openhost+0xad>
lea    0xdf0(%rip),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.hostent = type { i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [16 x i8] c"gethostbyname()\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [4 x i8] c"tcp\00"
@.str.2 = [9 x i8] c"socket()\00"
@.str.3 = [10 x i8] c"connect()\00"
define i32 @openhost(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.sockaddr_in
%7 = alloca %struct.hostent*
store i8* %0, i8** %3
store i32 %1, i32* %4
%8 = load i8*, i8** %3
%9 = call %struct.hostent* @gethostbyname(i8* %8)
store %struct.hostent* %9, %struct.hostent** %7
%10 = load %struct.hostent*, %struct.hostent** %7
%11 = icmp eq %struct.hostent* %10, null
br i1 %11, label %12, label %15
12: ; preds = %2
%13 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%14 = call i32 @exit(i32 -1)
unreachable
15: ; preds = %2
%16 = load i32, i32* @AF_INET
%17 = load i32, i32* @SOCK_STREAM
%18 = call %struct.TYPE_2__* @getprotobyname(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%19 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %18, i32 0, i32 0
%20 = load i32, i32* %19
%21 = call i32 @socket(i32 %16, i32 %17, i32 %20)
store i32 %21, i32* %5
%22 = load i32, i32* %5
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %27
24: ; preds = %15
%25 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
%26 = call i32 @exit(i32 -1)
unreachable
27: ; preds = %15
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 2
%29 = load %struct.hostent*, %struct.hostent** %7
%30 = getelementptr inbounds %struct.hostent, %struct.hostent* %29, i32 0, i32 1
%31 = load i32, i32* %30
%32 = load %struct.hostent*, %struct.hostent** %7
%33 = getelementptr inbounds %struct.hostent, %struct.hostent* %32, i32 0, i32 0
%34 = load i32, i32* %33
%35 = call i32 @memcpy(i32* %28, i32 %31, i32 %34)
%36 = load i32, i32* @AF_INET
%37 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 1
store i32 %36, i32* %37
%38 = load i32, i32* %4
%39 = call i32 @htons(i32 %38)
%40 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
store i32 %39, i32* %40
%41 = load i32, i32* %5
%42 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%43 = call i32 @connect(i32 %41, %struct.sockaddr* %42, i32 12)
%44 = icmp eq i32 %43, -1
br i1 %44, label %45, label %48
45: ; preds = %27
%46 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0))
%47 = call i32 @exit(i32 -1)
unreachable
48: ; preds = %27
%49 = load i32, i32* %5
ret i32 %49
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.hostent = type { i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [16 x i8] c"gethostbyname()\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [4 x i8] c"tcp\00"
@.str.2 = [9 x i8] c"socket()\00"
@.str.3 = [10 x i8] c"connect()\00"
define i32 @openhost(i8* %0, i32 %1) {
%3 = alloca %struct.sockaddr_in
%4 = bitcast %struct.sockaddr_in* %3 to i8*
%5 = tail call %struct.hostent* @gethostbyname(i8* %0)
%6 = icmp eq %struct.hostent* %5, null
br i1 %6, label %7, label %10
7: ; preds = %2
%8 = tail call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%9 = tail call i32 @exit(i32 -1)
unreachable
10: ; preds = %2
%11 = load i32, i32* @AF_INET
%12 = load i32, i32* @SOCK_STREAM
%13 = tail call %struct.TYPE_2__* @getprotobyname(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%14 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %13, i64 0, i32 0
%15 = load i32, i32* %14
%16 = tail call i32 @socket(i32 %11, i32 %12, i32 %15)
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %21
18: ; preds = %10
%19 = tail call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
%20 = tail call i32 @exit(i32 -1)
unreachable
21: ; preds = %10
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
%23 = getelementptr inbounds %struct.hostent, %struct.hostent* %5, i64 0, i32 1
%24 = load i32, i32* %23
%25 = getelementptr inbounds %struct.hostent, %struct.hostent* %5, i64 0, i32 0
%26 = load i32, i32* %25
%27 = call i32 @memcpy(i32* %22, i32 %24, i32 %26)
%28 = load i32, i32* @AF_INET
%29 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i32 %28, i32* %29
%30 = call i32 @htons(i32 %1)
%31 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %30, i32* %31
%32 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%33 = call i32 @connect(i32 %16, %struct.sockaddr* %32, i32 12)
%34 = icmp eq i32 %33, -1
br i1 %34, label %35, label %38
35: ; preds = %21
%36 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0))
%37 = call i32 @exit(i32 -1)
unreachable
38: ; preds = %21
ret i32 %16
}