#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_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
struct servent {
  int dummy;
};
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*/ PF_INET;
int SOCK_DGRAM;
int SOCK_STREAM;
scalar_t__ atoi(char const *);
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ errno;
struct hostent *gethostbyname(char const *);
struct protoent *getprotobyname(char const *);
int /*<<< orphan*/ htons(unsigned short);
int /*<<< orphan*/ memcpy(int /*<<< orphan*/ *, int /*<<< orphan*/,
                          int /*<<< orphan*/);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, char *);
int socket(int /*<<< orphan*/, int, int /*<<< orphan*/);
scalar_t__ strcmp(char const *, char *);
char *strerror(int /*<<< orphan*/);
int connectsock(const char *host, const char *port, const char *transport) {
  struct hostent *phe;
  struct servent *pse;
  struct protoent *ppe;
  struct sockaddr_in sin;
  int s, type;
  memset(&sin, 0, sizeof(sin));
  sin.sin_family = AF_INET;
  sin.sin_port = htons((unsigned short)atoi(port));
  if (phe = gethostbyname(host))
    memcpy(&sin.sin_addr, phe->h_addr, phe->h_length);
  if ((ppe = getprotobyname(transport)) == 0) {
    printf("    : %s\n", strerror(errno));
    return -1;
  }
  if (strcmp(transport, "udp") == 0)
    type = SOCK_DGRAM;
  else
    type = SOCK_STREAM;
  s = socket(PF_INET, type, ppe->p_proto);
  if (s < 0) {
    printf("  : %s\n", strerror(errno));
    return -1;
  }
  if (connect(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
    printf("    : %s\n", strerror(errno));
    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
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    0x2e0d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x40(%rbp)
mov    -0x18(%rbp),%rdi
call   10a0 <atoi@plt>
movzwl %ax,%edi
call   1030 <htons@plt>
mov    %eax,-0x44(%rbp)
mov    -0x10(%rbp),%rdi
call   1070 <gethostbyname@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
je     1221 <connectsock+0x71>
lea    -0x48(%rbp),%rdi
mov    -0x28(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x28(%rbp),%rax
mov    (%rax),%edx
call   1080 <memcpy@plt>
mov    -0x20(%rbp),%rdi
call   1090 <getprotobyname@plt>
mov    %rax,-0x38(%rbp)
cmp    $0x0,%rax
jne    1261 <connectsock+0xb1>
mov    0x2d89(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    %rax,%rsi
lea    0xdb0(%rip),%rdi
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    132f <connectsock+0x17f>
mov    -0x20(%rbp),%rdi
lea    0xd9e(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
jne    128c <connectsock+0xdc>
mov    0x2d5e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x50(%rbp)
jmp    1298 <connectsock+0xe8>
mov    0x2d3d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x50(%rbp)
mov    0x2d21(%rip),%rax
mov    (%rax),%edi
mov    -0x50(%rbp),%esi
mov    -0x38(%rbp),%rax
mov    (%rax),%edx
call   10d0 <socket@plt>
mov    %eax,-0x4c(%rbp)
cmpl   $0x0,-0x4c(%rbp)
jge    12e5 <connectsock+0x135>
mov    0x2d05(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    %rax,%rsi
lea    0xd2e(%rip),%rdi
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    132f <connectsock+0x17f>
mov    -0x4c(%rbp),%edi
lea    -0x48(%rbp),%rsi
mov    $0xc,%edx
call   10b0 <connect@plt>
cmp    $0x0,%rax
jge    1329 <connectsock+0x179>
mov    0x2cc1(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    %rax,%rsi
lea    0xce8(%rip),%rdi
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    132f <connectsock+0x17f>
mov    -0x4c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<connectsock>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%r15
mov    %rsi,%rbx
mov    %rdi,%r14
mov    %rsp,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    0x2e10(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
mov    %rbx,%rdi
call   10a0 <atoi@plt>
movzwl %ax,%edi
call   1030 <htons@plt>
mov    %eax,0x4(%rsp)
mov    %r14,%rdi
call   1070 <gethostbyname@plt>
test   %rax,%rax
je     120c <connectsock+0x5c>
mov    (%rax),%edx
mov    0x4(%rax),%esi
mov    %rsp,%rdi
call   1080 <memcpy@plt>
mov    %r15,%rdi
call   1090 <getprotobyname@plt>
lea    0xde5(%rip),%r14
test   %rax,%rax
je     127c <connectsock+0xcc>
mov    %rax,%rbx
lea    0xde0(%rip),%rsi
mov    %r15,%rdi
call   1060 <strcmp@plt>
test   %rax,%rax
jne    1240 <connectsock+0x90>
mov    0x2da2(%rip),%rax
jmp    1247 <connectsock+0x97>
mov    0x2d89(%rip),%rax
mov    (%rax),%esi
mov    0x2d70(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%edx
call   10d0 <socket@plt>
test   %eax,%eax
js     1275 <connectsock+0xc5>
mov    %eax,%ebx
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0xc,%edx
call   10b0 <connect@plt>
test   %rax,%rax
jns    129a <connectsock+0xea>
jmp    127c <connectsock+0xcc>
lea    0xd86(%rip),%r14
mov    0x2d45(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    %r14,%rdi
mov    %rax,%rsi
call   1040 <printf@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.servent = type { i32 }
%struct.hostent = type { i32, i32 }
%struct.protoent = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@.str = [10 x i8] c" : %s\0A\00"
@errno = global i32 0
@.str.1 = [4 x i8] c"udp\00"
@SOCK_DGRAM = global i32 0
@SOCK_STREAM = global i32 0
@PF_INET = global i32 0
@.str.2 = [8 x i8] c" : %s\0A\00"
define i32 @connectsock(i8* %0, i8* nocapture %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 i64 @atoi(i8* %1)
%10 = trunc i64 %9 to i16
%11 = call i32 @htons(i16 zeroext %10)
%12 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1
store i32 %11, i32* %12
%13 = call %struct.hostent* @gethostbyname(i8* %0)
%14 = icmp eq %struct.hostent* %13, null
br i1 %14, label %22, label %15
15: ; preds = %3
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0
%17 = getelementptr inbounds %struct.hostent, %struct.hostent* %13, i64 0, i32 1
%18 = load i32, i32* %17
%19 = getelementptr inbounds %struct.hostent, %struct.hostent* %13, i64 0, i32 0
%20 = load i32, i32* %19
%21 = call i32 @memcpy(i32* %16, i32 %18, i32 %20)
br label %22
22: ; preds = %15, %3
%23 = call %struct.protoent* @getprotobyname(i8* %2)
%24 = icmp eq %struct.protoent* %23, null
br i1 %24, label %40, label %25
25: ; preds = %22
%26 = call i64 @strcmp(i8* %2, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%27 = icmp eq i64 %26, 0
%28 = load i32, i32* @SOCK_DGRAM
%29 = load i32, i32* @SOCK_STREAM
%30 = select i1 %27, i32 %28, i32 %29
%31 = load i32, i32* @PF_INET
%32 = getelementptr inbounds %struct.protoent, %struct.protoent* %23, i64 0, i32 0
%33 = load i32, i32* %32
%34 = call i32 @socket(i32 %31, i32 %30, i32 %33)
%35 = icmp slt i32 %34, 0
br i1 %35, label %40, label %36
36: ; preds = %25
%37 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%38 = call i64 @connect(i32 %34, %struct.sockaddr* %37, i32 12)
%39 = icmp slt i64 %38, 0
br i1 %39, label %40, label %45
40: ; preds = %36, %25, %22
%41 = phi i8* [ getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), %22 ], [ getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), %25 ], [ getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), %36 ]
%42 = load i32, i32* @errno
%43 = call i8* @strerror(i32 %42)
%44 = call i32 @printf(i8* %41, i8* %43)
br label %45
45: ; preds = %40, %36
%46 = phi i32 [ %34, %36 ], [ -1, %40 ]
ret i32 %46
}