#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 {
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
struct servent {
  int /*<<< orphan*/ s_port;
};
struct protoent {
  int /*<<< orphan*/ p_proto;
};
struct hostent {
  int /*<<< orphan*/ h_length;
  int /*<<< orphan*/ *h_addr_list;
};
typedef int /*<<< orphan*/ sockdata;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ bcopy(int /*<<< orphan*/, char *, int /*<<< orphan*/);
int /*<<< orphan*/ bzero(char *, int);
int connect(int, struct sockaddr *, int);
struct hostent *gethostbyname(char *);
struct protoent *getprotobyname(char *);
struct servent *getservbyname(char *, char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int openSocket(char *hostname, char *service, char *protocol) {
  int sock, status;
  struct sockaddr_in sockdata;
  struct hostent *host;
  struct protoent *proto;
  struct servent *serv;
  host = gethostbyname(hostname);
  proto = getprotobyname(protocol);
  serv = getservbyname(service, protocol);
  bzero((char *)&sockdata, sizeof(sockdata));
  sockdata.sin_family = AF_INET;
  sockdata.sin_addr.s_addr = INADDR_ANY;
  sockdata.sin_port = serv->s_port;
  bcopy(host->h_addr_list[0], (char *)&sockdata.sin_addr, host->h_length);
  sock = socket(PF_INET, SOCK_STREAM, proto->p_proto);
  if (sock < 3) {
    return -1;
  }
  status = connect(sock, (struct sockaddr *)&sockdata, sizeof(sockdata));
  if (status < 0) {
    return -1;
  }
  return sock;
}


x86 assembly (clang, -O0):
<openSocket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <gethostbyname@plt>
mov    %rax,-0x40(%rbp)
mov    -0x20(%rbp),%rdi
call   1050 <getprotobyname@plt>
mov    %rax,-0x48(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1030 <getservbyname@plt>
mov    %rax,-0x50(%rbp)
lea    -0x38(%rbp),%rdi
mov    $0xc,%esi
call   1070 <bzero@plt>
mov    0x2e24(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    0x2e10(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
mov    -0x50(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    -0x40(%rbp),%rax
mov    0x8(%rax),%rax
mov    (%rax),%edi
lea    -0x38(%rbp),%rsi
mov    -0x40(%rbp),%rax
mov    (%rax),%edx
call   1060 <bcopy@plt>
mov    0x2dca(%rip),%rax
mov    (%rax),%edi
mov    0x2dc9(%rip),%rax
mov    (%rax),%esi
mov    -0x48(%rbp),%rax
mov    (%rax),%edx
call   1090 <socket@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x3,-0x24(%rbp)
jge    122d <openSocket+0xbd>
movl   $0xffffffff,-0x4(%rbp)
jmp    125d <openSocket+0xed>
mov    -0x24(%rbp),%edi
lea    -0x38(%rbp),%rsi
mov    $0xc,%edx
call   1080 <connect@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jge    1257 <openSocket+0xe7>
movl   $0xffffffff,-0x4(%rbp)
jmp    125d <openSocket+0xed>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<openSocket>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%rbx
mov    %rsi,%r15
call   1040 <gethostbyname@plt>
mov    %rax,%r12
mov    %rbx,%rdi
call   1050 <getprotobyname@plt>
mov    %rax,%r14
mov    %r15,%rdi
mov    %rbx,%rsi
call   1030 <getservbyname@plt>
mov    %rax,%rbx
lea    0x8(%rsp),%r15
mov    %r15,%rdi
mov    $0xc,%esi
call   1070 <bzero@plt>
mov    0x2e2d(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
mov    0x2e18(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
mov    (%rbx),%eax
mov    %eax,0xc(%rsp)
mov    0x8(%r12),%rax
mov    (%rax),%edi
mov    (%r12),%edx
mov    %r15,%rsi
call   1060 <bcopy@plt>
mov    0x2dda(%rip),%rax
mov    (%rax),%edi
mov    0x2dd9(%rip),%rax
mov    (%rax),%esi
mov    (%r14),%edx
call   1090 <socket@plt>
mov    %eax,%ebx
mov    $0xffffffff,%eax
cmp    $0x3,%ebx
jl     1228 <openSocket+0xb8>
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   1080 <connect@plt>
test   %eax,%eax
mov    $0xffffffff,%eax
cmovns %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.hostent = type { i32, i32* }
%struct.protoent = type { i32 }
%struct.servent = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@INADDR_ANY = global i32 0
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
define i32 @openSocket(i8* %0, i8* %1, i8* %2) {
%4 = alloca %struct.sockaddr_in
%5 = bitcast %struct.sockaddr_in* %4 to i8*
%6 = tail call %struct.hostent* @gethostbyname(i8* %0)
%7 = tail call %struct.protoent* @getprotobyname(i8* %2)
%8 = tail call %struct.servent* @getservbyname(i8* %1, i8* %2)
%9 = call i32 @bzero(i8* %5, i32 12)
%10 = load i32, i32* @AF_INET
%11 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 2
store i32 %10, i32* %11
%12 = load i32, i32* @INADDR_ANY
%13 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0, i32 0
store i32 %12, i32* %13
%14 = getelementptr inbounds %struct.servent, %struct.servent* %8, i64 0, i32 0
%15 = load i32, i32* %14
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1
store i32 %15, i32* %16
%17 = getelementptr inbounds %struct.hostent, %struct.hostent* %6, i64 0, i32 1
%18 = load i32*, i32** %17
%19 = load i32, i32* %18
%20 = getelementptr inbounds %struct.hostent, %struct.hostent* %6, i64 0, i32 0
%21 = load i32, i32* %20
%22 = call i32 @bcopy(i32 %19, i8* %5, i32 %21)
%23 = load i32, i32* @PF_INET
%24 = load i32, i32* @SOCK_STREAM
%25 = getelementptr inbounds %struct.protoent, %struct.protoent* %7, i64 0, i32 0
%26 = load i32, i32* %25
%27 = call i32 @socket(i32 %23, i32 %24, i32 %26)
%28 = icmp slt i32 %27, 3
br i1 %28, label %34, label %29
29: ; preds = %3
%30 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%31 = call i32 @connect(i32 %27, %struct.sockaddr* %30, i32 12)
%32 = icmp sgt i32 %31, -1
%33 = select i1 %32, i32 %27, i32 -1
br label %34
34: ; preds = %29, %3
%35 = phi i32 [ -1, %3 ], [ %33, %29 ]
ret i32 %35
}