#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 */
typedef int /*<<< orphan*/ u_short;
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 hostent {
  int /*<<< orphan*/ h_length;
  int /*<<< orphan*/ h_addr;
};
typedef int /*<<< orphan*/ sin;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_NONE;
int /*<<< orphan*/ SOCK_STREAM;
scalar_t__ atoi(char const *);
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ errno;
struct hostent *gethostbyname(char const *);
struct servent *getservbyname(char const *, char const *);
int /*<<< orphan*/ htons(int /*<<< orphan*/);
int /*<<< orphan*/ inet_addr(char const *);
int /*<<< orphan*/ memcpy(TYPE_1__ *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, char const *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
int active_socket(const char *host, const char *service,
                  const char *transport) {
  struct hostent *phe;
  struct servent *pse;
  struct sockaddr_in sin;
  int s;
  memset(&sin, 0, sizeof(struct sockaddr_in));
  sin.sin_family = AF_INET;
  if ((pse = getservbyname(service, transport)))
    sin.sin_port = pse->s_port;
  else if ((sin.sin_port = htons((u_short)atoi(service))) == 0)
    printf("servicio \"%s\" desconocido\n", service);
  if ((phe = gethostbyname(host)))
    memcpy(&sin.sin_addr, phe->h_addr, phe->h_length);
  else if ((sin.sin_addr.s_addr = inet_addr(host)) == INADDR_NONE)
    printf("nombre de host \"%s\" desconocido\n", host);
  s = socket(AF_INET, SOCK_STREAM, 0);
  if (s > 0) {
    if (connect(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
      s = -1;
      strerror(errno);
    }
  } else
    s = -1;
  return s;
}


x86 assembly (clang, -O0):
<active_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
lea    -0x38(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    0x2e0d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1050 <getservbyname@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
je     1209 <active_socket+0x59>
mov    -0x28(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
jmp    123a <active_socket+0x8a>
mov    -0x10(%rbp),%rdi
call   10a0 <atoi@plt>
mov    %eax,%edi
call   1030 <htons@plt>
mov    %eax,-0x34(%rbp)
cmp    $0x0,%eax
jne    1235 <active_socket+0x85>
mov    -0x10(%rbp),%rsi
lea    0xdd0(%rip),%rdi
call   1040 <printf@plt>
jmp    123a <active_socket+0x8a>
mov    -0x8(%rbp),%rdi
call   1080 <gethostbyname@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     126c <active_socket+0xbc>
lea    -0x38(%rbp),%rdi
mov    -0x20(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x20(%rbp),%rax
mov    (%rax),%edx
call   1090 <memcpy@plt>
jmp    129c <active_socket+0xec>
mov    -0x8(%rbp),%rdi
call   1070 <inet_addr@plt>
mov    %eax,-0x38(%rbp)
mov    0x2d61(%rip),%rcx
cmp    (%rcx),%eax
jne    1297 <active_socket+0xe7>
mov    -0x8(%rbp),%rsi
lea    0xd89(%rip),%rdi
call   1040 <printf@plt>
jmp    129c <active_socket+0xec>
mov    0x2d45(%rip),%rax
mov    (%rax),%edi
mov    0x2d24(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
jle    12f7 <active_socket+0x147>
mov    -0x3c(%rbp),%edi
lea    -0x38(%rbp),%rsi
mov    $0xc,%edx
call   10b0 <connect@plt>
cmp    $0x0,%rax
jge    12f2 <active_socket+0x142>
movl   $0xffffffff,-0x3c(%rbp)
mov    0x2cdd(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
jmp    12fe <active_socket+0x14e>
movl   $0xffffffff,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<active_socket>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%rbx
mov    %rsi,%r15
mov    %rdi,%r14
lea    0x8(%rsp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    0x2e0c(%rip),%r12
mov    (%r12),%eax
mov    %eax,0x10(%rsp)
mov    %r15,%rdi
mov    %rbx,%rsi
call   1050 <getservbyname@plt>
test   %rax,%rax
je     11fc <active_socket+0x4c>
mov    (%rax),%eax
mov    %eax,0xc(%rsp)
jmp    1222 <active_socket+0x72>
mov    %r15,%rdi
call   10a0 <atoi@plt>
mov    %eax,%edi
call   1030 <htons@plt>
mov    %eax,0xc(%rsp)
test   %eax,%eax
jne    1222 <active_socket+0x72>
lea    0xde6(%rip),%rdi
mov    %r15,%rsi
call   1040 <printf@plt>
mov    %r14,%rdi
call   1080 <gethostbyname@plt>
test   %rax,%rax
je     1240 <active_socket+0x90>
mov    (%rax),%edx
mov    0x4(%rax),%esi
lea    0x8(%rsp),%rdi
call   1090 <memcpy@plt>
jmp    1266 <active_socket+0xb6>
mov    %r14,%rdi
call   1070 <inet_addr@plt>
mov    %eax,0x8(%rsp)
mov    0x2d8d(%rip),%rcx
cmp    (%rcx),%eax
jne    1266 <active_socket+0xb6>
lea    0xdbd(%rip),%rdi
mov    %r14,%rsi
call   1040 <printf@plt>
mov    (%r12),%edi
mov    0x2d5f(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
mov    %eax,%ebx
mov    $0xffffffff,%eax
test   %ebx,%ebx
jle    12b3 <active_socket+0x103>
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   10b0 <connect@plt>
mov    %rax,%rcx
mov    %ebx,%eax
test   %rcx,%rcx
jns    12b3 <active_socket+0x103>
mov    0x2d21(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    $0xffffffff,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.hostent = type { i32, i32 }
%struct.servent = type { i32 }
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@.str = [27 x i8] c"servicio \22%s\22 desconocido\0A\00"
@INADDR_NONE = global i32 0
@.str.1 = [33 x i8] c"nombre de host \22%s\22 desconocido\0A\00"
@SOCK_STREAM = global i32 0
@errno = global i32 0
define i32 @active_socket(i8* %0, i8* %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca %struct.hostent*
%8 = alloca %struct.servent*
%9 = alloca %struct.sockaddr_in
%10 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
store i8* %2, i8** %6
%11 = call i32 @memset(%struct.sockaddr_in* %9, i32 0, i32 12)
%12 = load i32, i32* @AF_INET
%13 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 2
store i32 %12, i32* %13
%14 = load i8*, i8** %5
%15 = load i8*, i8** %6
%16 = call %struct.servent* @getservbyname(i8* %14, i8* %15)
store %struct.servent* %16, %struct.servent** %8
%17 = icmp ne %struct.servent* %16, null
br i1 %17, label %18, label %23
18: ; preds = %3
%19 = load %struct.servent*, %struct.servent** %8
%20 = getelementptr inbounds %struct.servent, %struct.servent* %19, i32 0, i32 0
%21 = load i32, i32* %20
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 1
store i32 %21, i32* %22
br label %34
23: ; preds = %3
%24 = load i8*, i8** %5
%25 = call i64 @atoi(i8* %24)
%26 = trunc i64 %25 to i32
%27 = call i32 @htons(i32 %26)
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 1
store i32 %27, i32* %28
%29 = icmp eq i32 %27, 0
br i1 %29, label %30, label %33
30: ; preds = %23
%31 = load i8*, i8** %5
%32 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i8* %31)
br label %33
33: ; preds = %30, %23
br label %34
34: ; preds = %33, %18
%35 = load i8*, i8** %4
%36 = call %struct.hostent* @gethostbyname(i8* %35)
store %struct.hostent* %36, %struct.hostent** %7
%37 = icmp ne %struct.hostent* %36, null
br i1 %37, label %38, label %47
38: ; preds = %34
%39 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 0
%40 = load %struct.hostent*, %struct.hostent** %7
%41 = getelementptr inbounds %struct.hostent, %struct.hostent* %40, i32 0, i32 1
%42 = load i32, i32* %41
%43 = load %struct.hostent*, %struct.hostent** %7
%44 = getelementptr inbounds %struct.hostent, %struct.hostent* %43, i32 0, i32 0
%45 = load i32, i32* %44
%46 = call i32 @memcpy(%struct.TYPE_2__* %39, i32 %42, i32 %45)
br label %58
47: ; preds = %34
%48 = load i8*, i8** %4
%49 = call i32 @inet_addr(i8* %48)
%50 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 0
%51 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %50, i32 0, i32 0
store i32 %49, i32* %51
%52 = load i32, i32* @INADDR_NONE
%53 = icmp eq i32 %49, %52
br i1 %53, label %54, label %57
54: ; preds = %47
%55 = load i8*, i8** %4
%56 = call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i8* %55)
br label %57
57: ; preds = %54, %47
br label %58
58: ; preds = %57, %38
%59 = load i32, i32* @AF_INET
%60 = load i32, i32* @SOCK_STREAM
%61 = call i32 @socket(i32 %59, i32 %60, i32 0)
store i32 %61, i32* %10
%62 = load i32, i32* %10
%63 = icmp sgt i32 %62, 0
br i1 %63, label %64, label %73
64: ; preds = %58
%65 = load i32, i32* %10
%66 = bitcast %struct.sockaddr_in* %9 to %struct.sockaddr*
%67 = call i64 @connect(i32 %65, %struct.sockaddr* %66, i32 12)
%68 = icmp slt i64 %67, 0
br i1 %68, label %69, label %72
69: ; preds = %64
store i32 -1, i32* %10
%70 = load i32, i32* @errno
%71 = call i32 @strerror(i32 %70)
br label %72
72: ; preds = %69, %64
br label %74
73: ; preds = %58
store i32 -1, i32* %10
br label %74
74: ; preds = %73, %72
%75 = load i32, i32* %10
ret i32 %75
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.servent = type { i32 }
%struct.hostent = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@.str = [27 x i8] c"servicio \22%s\22 desconocido\0A\00"
@INADDR_NONE = global i32 0
@.str.1 = [33 x i8] c"nombre de host \22%s\22 desconocido\0A\00"
@SOCK_STREAM = global i32 0
@errno = global i32 0
define i32 @active_socket(i8* %0, i8* %1, i8* %2) {
%4 = alloca %struct.sockaddr_in
%5 = bitcast %struct.sockaddr_in* %4 to i8*
%6 = call i32 @memset(%struct.sockaddr_in* %4, i32 0, i32 12)
%7 = load i32, i32* @AF_INET
%8 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 2
store i32 %7, i32* %8
%9 = call %struct.servent* @getservbyname(i8* %1, i8* %2)
%10 = icmp eq %struct.servent* %9, null
br i1 %10, label %15, label %11
11: ; preds = %3
%12 = getelementptr inbounds %struct.servent, %struct.servent* %9, i64 0, i32 0
%13 = load i32, i32* %12
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1
store i32 %13, i32* %14
br label %23
15: ; preds = %3
%16 = call i64 @atoi(i8* %1)
%17 = trunc i64 %16 to i32
%18 = call i32 @htons(i32 %17)
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1
store i32 %18, i32* %19
%20 = icmp eq i32 %18, 0
br i1 %20, label %21, label %23
21: ; preds = %15
%22 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i8* %1)
br label %23
23: ; preds = %15, %21, %11
%24 = call %struct.hostent* @gethostbyname(i8* %0)
%25 = icmp eq %struct.hostent* %24, null
br i1 %25, label %33, label %26
26: ; preds = %23
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0
%28 = getelementptr inbounds %struct.hostent, %struct.hostent* %24, i64 0, i32 1
%29 = load i32, i32* %28
%30 = getelementptr inbounds %struct.hostent, %struct.hostent* %24, i64 0, i32 0
%31 = load i32, i32* %30
%32 = call i32 @memcpy(%struct.TYPE_2__* %27, i32 %29, i32 %31)
br label %40
33: ; preds = %23
%34 = call i32 @inet_addr(i8* %0)
%35 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0, i32 0
store i32 %34, i32* %35
%36 = load i32, i32* @INADDR_NONE
%37 = icmp eq i32 %34, %36
br i1 %37, label %38, label %40
38: ; preds = %33
%39 = call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %40
40: ; preds = %33, %38, %26
%41 = load i32, i32* @AF_INET
%42 = load i32, i32* @SOCK_STREAM
%43 = call i32 @socket(i32 %41, i32 %42, i32 0)
%44 = icmp sgt i32 %43, 0
br i1 %44, label %45, label %52
45: ; preds = %40
%46 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%47 = call i64 @connect(i32 %43, %struct.sockaddr* %46, i32 12)
%48 = icmp slt i64 %47, 0
br i1 %48, label %49, label %52
49: ; preds = %45
%50 = load i32, i32* @errno
%51 = call i32 @strerror(i32 %50)
br label %52
52: ; preds = %40, %45, %49
%53 = phi i32 [ -1, %49 ], [ %43, %45 ], [ -1, %40 ]
ret i32 %53
}