#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;
};
typedef int /*<<< orphan*/ sin;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_NONE;
int /*<<< orphan*/ PF_INET;
int SOCK_DGRAM;
int SOCK_STREAM;
scalar_t__ atoi(char *);
int /*<<< orphan*/ bcopy(int /*<<< orphan*/, char *, int /*<<< orphan*/);
int /*<<< orphan*/ bzero(char *, int);
scalar_t__ connect(int, struct sockaddr *, int);
struct hostent *gethostbyname(char *);
struct protoent *getprotobyname(char *);
struct servent *getservbyname(char *, char *);
int /*<<< orphan*/ htons(unsigned short);
int /*<<< orphan*/ inet_addr(char *);
int /*<<< orphan*/ printf(char *, ...);
int socket(int /*<<< orphan*/, int, int /*<<< orphan*/);
scalar_t__ strcmp(char *, char *);
int connectsock(char *host, char *service, char *protocol) {
  struct hostent *phe;
  struct servent *pse;
  struct protoent *ppe;
  struct sockaddr_in sin;
  int s, type;
  bzero((char *)&sin, sizeof(struct sockaddr_in));
  sin.sin_family = AF_INET;
  if ((pse = getservbyname(service, protocol)))
    sin.sin_port = pse->s_port;
  else if ((sin.sin_port = htons((unsigned short)atoi(service))) == 0) {
    printf("Can't get services\n");
    return -1;
  }
  if ((phe = gethostbyname(host)))
    bcopy(phe->h_addr, (char *)&sin.sin_addr, phe->h_length);
  else if ((sin.sin_addr.s_addr = inet_addr(host)) == INADDR_NONE) {
    printf("Can't get host: \"%s\".\n", host);
    return -1;
  }
  if ((ppe = getprotobyname(protocol)) == 0)
    return -1;
  if (strcmp(protocol, "udp") == 0)
    type = SOCK_DGRAM;
  else
    type = SOCK_STREAM;
  s = socket(PF_INET, type, ppe->p_proto);
  if (s < 0) {
    printf("Can't get socket.\n");
    return -1;
  }
  if (connect(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
    printf("Can't connect to socket.\n");
    return -1;
  }
  return s;
}


x86 assembly (clang, -O0):
<connectsock>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
lea    -0x48(%rbp),%rdi
mov    $0xc,%esi
call   10b0 <bzero@plt>
mov    0x2dff(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x40(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1050 <getservbyname@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
je     1217 <connectsock+0x57>
mov    -0x30(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
jmp    1253 <connectsock+0x93>
mov    -0x18(%rbp),%rdi
call   10c0 <atoi@plt>
movzwl %ax,%edi
call   1030 <htons@plt>
mov    %eax,-0x44(%rbp)
cmp    $0x0,%eax
jne    124e <connectsock+0x8e>
lea    0xdc5(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1396 <connectsock+0x1d6>
jmp    1253 <connectsock+0x93>
mov    -0x10(%rbp),%rdi
call   1080 <gethostbyname@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
je     1285 <connectsock+0xc5>
mov    -0x28(%rbp),%rax
mov    0x4(%rax),%edi
lea    -0x48(%rbp),%rsi
mov    -0x28(%rbp),%rax
mov    (%rax),%edx
call   10a0 <bcopy@plt>
jmp    12c3 <connectsock+0x103>
mov    -0x10(%rbp),%rdi
call   1060 <inet_addr@plt>
mov    %eax,-0x48(%rbp)
mov    0x2d48(%rip),%rcx
cmp    (%rcx),%eax
jne    12be <connectsock+0xfe>
mov    -0x10(%rbp),%rsi
lea    0xd69(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1396 <connectsock+0x1d6>
jmp    12c3 <connectsock+0x103>
mov    -0x20(%rbp),%rdi
call   1090 <getprotobyname@plt>
mov    %rax,-0x38(%rbp)
cmp    $0x0,%rax
jne    12e6 <connectsock+0x126>
movl   $0xffffffff,-0x4(%rbp)
jmp    1396 <connectsock+0x1d6>
mov    -0x20(%rbp),%rdi
lea    0xd3a(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%rax
jne    1311 <connectsock+0x151>
mov    0x2cd1(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x50(%rbp)
jmp    131d <connectsock+0x15d>
mov    0x2cb0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x50(%rbp)
mov    0x2c9c(%rip),%rax
mov    (%rax),%edi
mov    -0x50(%rbp),%esi
mov    -0x38(%rbp),%rax
mov    (%rax),%edx
call   10e0 <socket@plt>
mov    %eax,-0x4c(%rbp)
cmpl   $0x0,-0x4c(%rbp)
jge    135b <connectsock+0x19b>
lea    0xce7(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1396 <connectsock+0x1d6>
mov    -0x4c(%rbp),%edi
lea    -0x48(%rbp),%rsi
mov    $0xc,%edx
call   10d0 <connect@plt>
cmp    $0x0,%rax
jge    1390 <connectsock+0x1d0>
lea    0xcc5(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1396 <connectsock+0x1d6>
mov    -0x4c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<connectsock>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%r15
mov    %rsi,%rbx
mov    %rdi,%r14
lea    0x8(%rsp),%rdi
mov    $0xc,%esi
call   10c0 <bzero@plt>
mov    0x2def(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
mov    %rbx,%rdi
mov    %r15,%rsi
call   1060 <getservbyname@plt>
test   %rax,%rax
je     1217 <connectsock+0x47>
mov    (%rax),%eax
mov    %eax,0xc(%rsp)
jmp    1233 <connectsock+0x63>
mov    %rbx,%rdi
call   10d0 <atoi@plt>
movzwl %ax,%edi
call   1040 <htons@plt>
mov    %eax,0xc(%rsp)
test   %eax,%eax
je     12f7 <connectsock+0x127>
mov    %r14,%rdi
call   1090 <gethostbyname@plt>
test   %rax,%rax
je     1251 <connectsock+0x81>
mov    (%rax),%edx
mov    0x4(%rax),%edi
lea    0x8(%rsp),%rsi
call   10b0 <bcopy@plt>
jmp    127e <connectsock+0xae>
mov    %r14,%rdi
call   1070 <inet_addr@plt>
mov    %eax,0x8(%rsp)
mov    0x2d7c(%rip),%rcx
cmp    (%rcx),%eax
jne    127e <connectsock+0xae>
lea    0xd91(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    1303 <connectsock+0x133>
mov    %r15,%rdi
call   10a0 <getprotobyname@plt>
mov    $0xffffffff,%ebp
test   %rax,%rax
je     1308 <connectsock+0x138>
mov    %rax,%rbx
lea    0xd7d(%rip),%rsi
mov    %r15,%rdi
call   1080 <strcmp@plt>
test   %rax,%rax
jne    12b0 <connectsock+0xe0>
mov    0x2d2a(%rip),%rax
jmp    12b7 <connectsock+0xe7>
mov    0x2d11(%rip),%rax
mov    (%rax),%esi
mov    0x2d00(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%edx
call   10f0 <socket@plt>
test   %eax,%eax
js     12ee <connectsock+0x11e>
mov    %eax,%ebp
lea    0x8(%rsp),%rsi
mov    %eax,%edi
mov    $0xc,%edx
call   10e0 <connect@plt>
test   %rax,%rax
jns    1308 <connectsock+0x138>
lea    0xd2f(%rip),%rdi
jmp    12fe <connectsock+0x12e>
lea    0xd3f(%rip),%rdi
jmp    12fe <connectsock+0x12e>
lea    0xd48(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.hostent = type { i32, i32 }
%struct.servent = type { i32 }
%struct.protoent = 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 = [20 x i8] c"Can't get services\0A\00"
@INADDR_NONE = global i32 0
@.str.1 = [23 x i8] c"Can't get host: \22%s\22.\0A\00"
@.str.2 = [4 x i8] c"udp\00"
@SOCK_DGRAM = global i32 0
@SOCK_STREAM = global i32 0
@PF_INET = global i32 0
@.str.3 = [19 x i8] c"Can't get socket.\0A\00"
@.str.4 = [26 x i8] c"Can't connect to socket.\0A\00"
define i32 @connectsock(i8* %0, i8* %1, i8* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca %struct.hostent*
%9 = alloca %struct.servent*
%10 = alloca %struct.protoent*
%11 = alloca %struct.sockaddr_in
%12 = alloca i32
%13 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i8* %2, i8** %7
%14 = bitcast %struct.sockaddr_in* %11 to i8*
%15 = call i32 @bzero(i8* %14, i32 12)
%16 = load i32, i32* @AF_INET
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 2
store i32 %16, i32* %17
%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** %9
%21 = icmp ne %struct.servent* %20, null
br i1 %21, label %22, label %27
22: ; preds = %3
%23 = load %struct.servent*, %struct.servent** %9
%24 = getelementptr inbounds %struct.servent, %struct.servent* %23, i32 0, i32 0
%25 = load i32, i32* %24
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 1
store i32 %25, i32* %26
br label %37
27: ; preds = %3
%28 = load i8*, i8** %6
%29 = call i64 @atoi(i8* %28)
%30 = trunc i64 %29 to i16
%31 = call i32 @htons(i16 zeroext %30)
%32 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 1
store i32 %31, i32* %32
%33 = icmp eq i32 %31, 0
br i1 %33, label %34, label %36
34: ; preds = %27
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %4
br label %95
36: ; preds = %27
br label %37
37: ; preds = %36, %22
%38 = load i8*, i8** %5
%39 = call %struct.hostent* @gethostbyname(i8* %38)
store %struct.hostent* %39, %struct.hostent** %8
%40 = icmp ne %struct.hostent* %39, null
br i1 %40, label %41, label %51
41: ; preds = %37
%42 = load %struct.hostent*, %struct.hostent** %8
%43 = getelementptr inbounds %struct.hostent, %struct.hostent* %42, i32 0, i32 1
%44 = load i32, i32* %43
%45 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 0
%46 = bitcast %struct.TYPE_2__* %45 to i8*
%47 = load %struct.hostent*, %struct.hostent** %8
%48 = getelementptr inbounds %struct.hostent, %struct.hostent* %47, i32 0, i32 0
%49 = load i32, i32* %48
%50 = call i32 @bcopy(i32 %44, i8* %46, i32 %49)
br label %62
51: ; preds = %37
%52 = load i8*, i8** %5
%53 = call i32 @inet_addr(i8* %52)
%54 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 0
%55 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %54, i32 0, i32 0
store i32 %53, i32* %55
%56 = load i32, i32* @INADDR_NONE
%57 = icmp eq i32 %53, %56
br i1 %57, label %58, label %61
58: ; preds = %51
%59 = load i8*, i8** %5
%60 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i8* %59)
store i32 -1, i32* %4
br label %95
61: ; preds = %51
br label %62
62: ; preds = %61, %41
%63 = load i8*, i8** %7
%64 = call %struct.protoent* @getprotobyname(i8* %63)
store %struct.protoent* %64, %struct.protoent** %10
%65 = icmp eq %struct.protoent* %64, null
br i1 %65, label %66, label %67
66: ; preds = %62
store i32 -1, i32* %4
br label %95
67: ; preds = %62
%68 = load i8*, i8** %7
%69 = call i64 @strcmp(i8* %68, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0))
%70 = icmp eq i64 %69, 0
br i1 %70, label %71, label %73
71: ; preds = %67
%72 = load i32, i32* @SOCK_DGRAM
store i32 %72, i32* %13
br label %75
73: ; preds = %67
%74 = load i32, i32* @SOCK_STREAM
store i32 %74, i32* %13
br label %75
75: ; preds = %73, %71
%76 = load i32, i32* @PF_INET
%77 = load i32, i32* %13
%78 = load %struct.protoent*, %struct.protoent** %10
%79 = getelementptr inbounds %struct.protoent, %struct.protoent* %78, i32 0, i32 0
%80 = load i32, i32* %79
%81 = call i32 @socket(i32 %76, i32 %77, i32 %80)
store i32 %81, i32* %12
%82 = load i32, i32* %12
%83 = icmp slt i32 %82, 0
br i1 %83, label %84, label %86
84: ; preds = %75
%85 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %4
br label %95
86: ; preds = %75
%87 = load i32, i32* %12
%88 = bitcast %struct.sockaddr_in* %11 to %struct.sockaddr*
%89 = call i64 @connect(i32 %87, %struct.sockaddr* %88, i32 12)
%90 = icmp slt i64 %89, 0
br i1 %90, label %91, label %93
91: ; preds = %86
%92 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0))
store i32 -1, i32* %4
br label %95
93: ; preds = %86
%94 = load i32, i32* %12
store i32 %94, i32* %4
br label %95
95: ; preds = %93, %91, %84, %66, %58, %34
%96 = load i32, i32* %4
ret i32 %96
}

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.protoent = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@INADDR_NONE = global i32 0
@.str.1 = [23 x i8] c"Can't get host: \22%s\22.\0A\00"
@.str.2 = [4 x i8] c"udp\00"
@SOCK_DGRAM = global i32 0
@SOCK_STREAM = global i32 0
@PF_INET = global i32 0
@str = [25 x i8] c"Can't connect to socket.\00"
@str.5 = [18 x i8] c"Can't get socket.\00"
@str.6 = [19 x i8] c"Can't get services\00"
define i32 @connectsock(i8* %0, i8* %1, i8* %2) {
%4 = alloca %struct.sockaddr_in
%5 = bitcast %struct.sockaddr_in* %4 to i8*
%6 = call i32 @bzero(i8* %5, 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 = tail 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 = tail call i64 @atoi(i8* %1)
%17 = trunc i64 %16 to i16
%18 = tail call i32 @htons(i16 zeroext %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 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str.6, i64 0, i64 0))
br label %61
23: ; preds = %15, %11
%24 = tail call %struct.hostent* @gethostbyname(i8* %0)
%25 = icmp eq %struct.hostent* %24, null
br i1 %25, label %32, label %26
26: ; preds = %23
%27 = getelementptr inbounds %struct.hostent, %struct.hostent* %24, i64 0, i32 1
%28 = load i32, i32* %27
%29 = getelementptr inbounds %struct.hostent, %struct.hostent* %24, i64 0, i32 0
%30 = load i32, i32* %29
%31 = call i32 @bcopy(i32 %28, i8* %5, i32 %30)
br label %39
32: ; preds = %23
%33 = tail call i32 @inet_addr(i8* %0)
%34 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0, i32 0
store i32 %33, i32* %34
%35 = load i32, i32* @INADDR_NONE
%36 = icmp eq i32 %33, %35
br i1 %36, label %37, label %39
37: ; preds = %32
%38 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %61
39: ; preds = %32, %26
%40 = call %struct.protoent* @getprotobyname(i8* %2)
%41 = icmp eq %struct.protoent* %40, null
br i1 %41, label %61, label %42
42: ; preds = %39
%43 = call i64 @strcmp(i8* %2, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0))
%44 = icmp eq i64 %43, 0
%45 = load i32, i32* @SOCK_DGRAM
%46 = load i32, i32* @SOCK_STREAM
%47 = select i1 %44, i32 %45, i32 %46
%48 = load i32, i32* @PF_INET
%49 = getelementptr inbounds %struct.protoent, %struct.protoent* %40, i64 0, i32 0
%50 = load i32, i32* %49
%51 = call i32 @socket(i32 %48, i32 %47, i32 %50)
%52 = icmp slt i32 %51, 0
br i1 %52, label %53, label %55
53: ; preds = %42
%54 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str.5, i64 0, i64 0))
br label %61
55: ; preds = %42
%56 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%57 = call i64 @connect(i32 %51, %struct.sockaddr* %56, i32 12)
%58 = icmp slt i64 %57, 0
br i1 %58, label %59, label %61
59: ; preds = %55
%60 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str, i64 0, i64 0))
br label %61
61: ; preds = %55, %39, %59, %53, %37, %21
%62 = phi i32 [ -1, %53 ], [ -1, %59 ], [ -1, %37 ], [ -1, %21 ], [ -1, %39 ], [ %51, %55 ]
ret i32 %62
}