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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
unsigned long INADDR_NONE;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
scalar_t__ connect(int, struct sockaddr *, int);
struct hostent *gethostbyname(char *);
int /*<<< orphan*/ htons(int);
unsigned long inet_addr(char *);
int /*<<< orphan*/ memcpy(int /*<<< orphan*/ *, unsigned long *, int);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ shutdown(int, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int open_socket(char *host, int clientPort) {
  int sock;
  unsigned long inaddr;
  struct sockaddr_in ad;
  struct hostent *hp;
  memset(&ad, 0, sizeof(ad));
  ad.sin_family = AF_INET;
  inaddr = inet_addr(host);
  if (inaddr != INADDR_NONE)
    memcpy(&ad.sin_addr, &inaddr, sizeof(inaddr));
  else {
    if ((hp = gethostbyname(host)) == NULL)
      return -1;
    memcpy(&ad.sin_addr, hp->h_addr, hp->h_length);
  }
  ad.sin_port = htons(clientPort);
  if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
    return sock;
  if (connect(sock, (struct sockaddr *)&ad, sizeof(ad)) < 0) {
    shutdown(sock, 2);
    close(sock);
    return -1;
  }
  return sock;
}


x86 assembly (clang, -O0):
<open_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    0x2e32(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
call   1070 <inet_addr@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    0x2e0d(%rip),%rcx
cmp    (%rcx),%rax
je     11f7 <open_socket+0x67>
lea    -0x30(%rbp),%rdi
add    $0x4,%rdi
lea    -0x20(%rbp),%rsi
mov    $0x8,%edx
call   1090 <memcpy@plt>
jmp    1235 <open_socket+0xa5>
mov    -0x10(%rbp),%rdi
call   1080 <gethostbyname@plt>
mov    %rax,-0x38(%rbp)
cmp    $0x0,%rax
jne    121a <open_socket+0x8a>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b2 <open_socket+0x122>
lea    -0x30(%rbp),%rdi
add    $0x4,%rdi
mov    -0x38(%rbp),%rax
mov    (%rax),%rsi
mov    -0x38(%rbp),%rax
mov    0x8(%rax),%edx
call   1090 <memcpy@plt>
mov    -0x14(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x30(%rbp)
mov    0x2da1(%rip),%rax
mov    (%rax),%edi
mov    0x2d80(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jge    1270 <open_socket+0xe0>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12b2 <open_socket+0x122>
mov    -0x18(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0xc,%edx
call   10a0 <connect@plt>
cmp    $0x0,%rax
jge    12ac <open_socket+0x11c>
mov    -0x18(%rbp),%edi
mov    $0x2,%esi
call   1030 <shutdown@plt>
mov    -0x18(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b2 <open_socket+0x122>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_socket>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %esi,%ebp
mov    %rdi,%rbx
lea    0x8(%rsp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    0x2e33(%rip),%r14
mov    (%r14),%eax
mov    %eax,0x10(%rsp)
mov    %rbx,%rdi
call   1070 <inet_addr@plt>
mov    %rax,0x18(%rsp)
mov    0x2e10(%rip),%rcx
cmp    (%rcx),%rax
jne    11ef <open_socket+0x5f>
mov    %rbx,%rdi
call   1080 <gethostbyname@plt>
test   %rax,%rax
je     1250 <open_socket+0xc0>
lea    0xc(%rsp),%rdi
mov    (%rax),%rsi
mov    0x8(%rax),%edx
jmp    11fe <open_socket+0x6e>
lea    0xc(%rsp),%rdi
lea    0x18(%rsp),%rsi
mov    $0x8,%edx
call   1090 <memcpy@plt>
mov    %ebp,%edi
call   1040 <htons@plt>
mov    %eax,0x8(%rsp)
mov    (%r14),%edi
mov    0x2db8(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
mov    %eax,%ebx
test   %eax,%eax
js     1255 <open_socket+0xc5>
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   10a0 <connect@plt>
test   %rax,%rax
jns    1255 <open_socket+0xc5>
mov    %ebx,%edi
mov    $0x2,%esi
call   1030 <shutdown@plt>
mov    %ebx,%edi
call   1060 <close@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.hostent = type { i64*, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@INADDR_NONE = global i64 0
@SOCK_STREAM = global i32 0
define i32 @open_socket(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i64
%8 = alloca %struct.sockaddr_in
%9 = alloca %struct.hostent*
store i8* %0, i8** %4
store i32 %1, i32* %5
%10 = call i32 @memset(%struct.sockaddr_in* %8, i32 0, i32 12)
%11 = load i32, i32* @AF_INET
%12 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 2
store i32 %11, i32* %12
%13 = load i8*, i8** %4
%14 = call i64 @inet_addr(i8* %13)
store i64 %14, i64* %7
%15 = load i64, i64* %7
%16 = load i64, i64* @INADDR_NONE
%17 = icmp ne i64 %15, %16
br i1 %17, label %18, label %21
18: ; preds = %2
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 1
%20 = call i32 @memcpy(i32* %19, i64* %7, i32 8)
br label %35
21: ; preds = %2
%22 = load i8*, i8** %4
%23 = call %struct.hostent* @gethostbyname(i8* %22)
store %struct.hostent* %23, %struct.hostent** %9
%24 = icmp eq %struct.hostent* %23, null
br i1 %24, label %25, label %26
25: ; preds = %21
store i32 -1, i32* %3
br label %57
26: ; preds = %21
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 1
%28 = load %struct.hostent*, %struct.hostent** %9
%29 = getelementptr inbounds %struct.hostent, %struct.hostent* %28, i32 0, i32 0
%30 = load i64*, i64** %29
%31 = load %struct.hostent*, %struct.hostent** %9
%32 = getelementptr inbounds %struct.hostent, %struct.hostent* %31, i32 0, i32 1
%33 = load i32, i32* %32
%34 = call i32 @memcpy(i32* %27, i64* %30, i32 %33)
br label %35
35: ; preds = %26, %18
%36 = load i32, i32* %5
%37 = call i32 @htons(i32 %36)
%38 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 0
store i32 %37, i32* %38
%39 = load i32, i32* @AF_INET
%40 = load i32, i32* @SOCK_STREAM
%41 = call i32 @socket(i32 %39, i32 %40, i32 0)
store i32 %41, i32* %6
%42 = icmp slt i32 %41, 0
br i1 %42, label %43, label %45
43: ; preds = %35
%44 = load i32, i32* %6
store i32 %44, i32* %3
br label %57
45: ; preds = %35
%46 = load i32, i32* %6
%47 = bitcast %struct.sockaddr_in* %8 to %struct.sockaddr*
%48 = call i64 @connect(i32 %46, %struct.sockaddr* %47, i32 12)
%49 = icmp slt i64 %48, 0
br i1 %49, label %50, label %55
50: ; preds = %45
%51 = load i32, i32* %6
%52 = call i32 @shutdown(i32 %51, i32 2)
%53 = load i32, i32* %6
%54 = call i32 @close(i32 %53)
store i32 -1, i32* %3
br label %57
55: ; preds = %45
%56 = load i32, i32* %6
store i32 %56, i32* %3
br label %57
57: ; preds = %55, %50, %43, %25
%58 = load i32, i32* %3
ret i32 %58
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.hostent = type { i64*, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@INADDR_NONE = global i64 0
@SOCK_STREAM = global i32 0
define i32 @open_socket(i8* %0, i32 %1) {
%3 = alloca i64
%4 = alloca %struct.sockaddr_in
%5 = bitcast i64* %3 to i8*
%6 = bitcast %struct.sockaddr_in* %4 to i8*
%7 = call i32 @memset(%struct.sockaddr_in* %4, i32 0, i32 12)
%8 = load i32, i32* @AF_INET
%9 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 2
store i32 %8, i32* %9
%10 = call i64 @inet_addr(i8* %0)
store i64 %10, i64* %3
%11 = load i64, i64* @INADDR_NONE
%12 = icmp eq i64 %10, %11
br i1 %12, label %16, label %13
13: ; preds = %2
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1
%15 = call i32 @memcpy(i32* %14, i64* %3, i32 8)
br label %26
16: ; preds = %2
%17 = call %struct.hostent* @gethostbyname(i8* %0)
%18 = icmp eq %struct.hostent* %17, null
br i1 %18, label %40, label %19
19: ; preds = %16
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1
%21 = getelementptr inbounds %struct.hostent, %struct.hostent* %17, i64 0, i32 0
%22 = load i64*, i64** %21
%23 = getelementptr inbounds %struct.hostent, %struct.hostent* %17, i64 0, i32 1
%24 = load i32, i32* %23
%25 = call i32 @memcpy(i32* %20, i64* %22, i32 %24)
br label %26
26: ; preds = %19, %13
%27 = call i32 @htons(i32 %1)
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0
store i32 %27, i32* %28
%29 = load i32, i32* @AF_INET
%30 = load i32, i32* @SOCK_STREAM
%31 = call i32 @socket(i32 %29, i32 %30, i32 0)
%32 = icmp slt i32 %31, 0
br i1 %32, label %40, label %33
33: ; preds = %26
%34 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%35 = call i64 @connect(i32 %31, %struct.sockaddr* %34, i32 12)
%36 = icmp slt i64 %35, 0
br i1 %36, label %37, label %40
37: ; preds = %33
%38 = call i32 @shutdown(i32 %31, i32 2)
%39 = call i32 @close(i32 %31)
br label %40
40: ; preds = %33, %26, %16, %37
%41 = phi i32 [ -1, %37 ], [ -1, %16 ], [ %31, %26 ], [ %31, %33 ]
ret i32 %41
}