#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 protoent {
  int /*<<< orphan*/ p_proto;
};
struct hostent {
  int /*<<< orphan*/ h_length;
  int /*<<< orphan*/ *h_addr_list;
};
typedef int /*<<< orphan*/ sin;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ PF_INET;
int SOCK_DGRAM;
int SOCK_STREAM;
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ errexit(char *, char const *, ...);
int /*<<< orphan*/ errno;
struct hostent *gethostbyname(char const *);
struct protoent *getprotobyname(char const *);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ memcpy(int /*<<< orphan*/ *, int /*<<< orphan*/,
                          int /*<<< orphan*/);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int, int /*<<< orphan*/);
scalar_t__ strcmp(char const *, char *);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
int connect_sock(const char *host, int port, const char *transport) {
  struct hostent *phe;
  struct protoent *ppe;
  struct sockaddr_in sin;
  int s, type;
  memset(&sin, 0, sizeof(sin));
  sin.sin_family = AF_INET;
  sin.sin_port = htons(port);
  if ((phe = gethostbyname(host)))
    memcpy(&sin.sin_addr, phe->h_addr_list[0], phe->h_length);
  else
    errexit("can't get \"%s\" host entry \n", host);
  if ((ppe = getprotobyname(transport)) == 0)
    errexit("can't get \"%s\" protocol entry\n", transport);
  if (strcmp(transport, "udp") == 0)
    type = SOCK_DGRAM;
  else
    type = SOCK_STREAM;
  s = socket(PF_INET, type, ppe->p_proto);
  if (s < 0)
    errexit("can't create socket: %s\n", strerror(errno));
  if (connect(s, (struct sockaddr *)&sin, sizeof(sin)) < 0)
    errexit("can't connect to %s.%d:%s\n", host, port, strerror(errno));
  return s;
}


x86 assembly (clang, -O0):
<connect_sock>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
lea    -0x38(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1040 <memset@plt>
mov    0x2e1e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    -0xc(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x34(%rbp)
mov    -0x8(%rbp),%rdi
call   1070 <gethostbyname@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     120f <connect_sock+0x6f>
lea    -0x38(%rbp),%rdi
mov    -0x20(%rbp),%rax
mov    0x8(%rax),%rax
mov    (%rax),%esi
mov    -0x20(%rbp),%rax
mov    (%rax),%edx
call   1080 <memcpy@plt>
jmp    1221 <connect_sock+0x81>
mov    -0x8(%rbp),%rsi
lea    0xde6(%rip),%rdi
mov    $0x0,%al
call   1050 <errexit@plt>
mov    -0x18(%rbp),%rdi
call   1090 <getprotobyname@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    124a <connect_sock+0xaa>
mov    -0x18(%rbp),%rsi
lea    0xdd9(%rip),%rdi
mov    $0x0,%al
call   1050 <errexit@plt>
mov    -0x18(%rbp),%rdi
lea    0xde6(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
jne    1275 <connect_sock+0xd5>
mov    0x2d75(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x40(%rbp)
jmp    1281 <connect_sock+0xe1>
mov    0x2d54(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x40(%rbp)
mov    0x2d38(%rip),%rax
mov    (%rax),%edi
mov    -0x40(%rbp),%esi
mov    -0x28(%rbp),%rax
mov    (%rax),%edx
call   10c0 <socket@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
jge    12c4 <connect_sock+0x124>
mov    0x2d1c(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
movslq %eax,%rsi
lea    0xd82(%rip),%rdi
mov    $0x0,%al
call   1050 <errexit@plt>
mov    -0x3c(%rbp),%edi
lea    -0x38(%rbp),%rsi
mov    $0xc,%edx
call   10a0 <connect@plt>
cmp    $0x0,%rax
jge    1312 <connect_sock+0x172>
mov    -0x8(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x44(%rbp)
mov    0x2cd4(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    -0x50(%rbp),%rsi
mov    -0x44(%rbp),%edx
mov    %eax,%ecx
lea    0xd4d(%rip),%rdi
mov    $0x0,%al
call   1050 <errexit@plt>
mov    -0x3c(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<connect_sock>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%rbp
mov    %esi,%r14d
mov    %rdi,%r15
lea    0x8(%rsp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1040 <memset@plt>
mov    0x2e1d(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
mov    %r14d,%edi
call   1030 <htons@plt>
mov    %eax,0xc(%rsp)
mov    %r15,%rdi
call   1070 <gethostbyname@plt>
test   %rax,%rax
je     11fe <connect_sock+0x5e>
mov    0x8(%rax),%rcx
mov    (%rcx),%esi
mov    (%rax),%edx
lea    0x8(%rsp),%rdi
call   1080 <memcpy@plt>
jmp    120f <connect_sock+0x6f>
lea    0xdfb(%rip),%rdi
mov    %r15,%rsi
xor    %eax,%eax
call   1050 <errexit@plt>
mov    %rbp,%rdi
call   1090 <getprotobyname@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    1230 <connect_sock+0x90>
lea    0xdf6(%rip),%rdi
mov    %rbp,%rsi
xor    %eax,%eax
call   1050 <errexit@plt>
lea    0xe04(%rip),%rsi
mov    %rbp,%rdi
call   1060 <strcmp@plt>
test   %rax,%rax
jne    124d <connect_sock+0xad>
mov    0x2d95(%rip),%rax
jmp    1254 <connect_sock+0xb4>
mov    0x2d7c(%rip),%rax
mov    (%rax),%esi
mov    0x2d63(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%edx
call   10c0 <socket@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    128b <connect_sock+0xeb>
mov    0x2d55(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
movslq %eax,%rsi
lea    0xdbb(%rip),%rdi
xor    %eax,%eax
call   1050 <errexit@plt>
lea    0x8(%rsp),%rsi
mov    %ebp,%edi
mov    $0xc,%edx
call   10a0 <connect@plt>
test   %rax,%rax
jns    12c5 <connect_sock+0x125>
mov    0x2d20(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xda2(%rip),%rdi
mov    %r15,%rsi
mov    %r14d,%edx
mov    %eax,%ecx
xor    %eax,%eax
call   1050 <errexit@plt>
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.hostent = type { i32, i32* }
%struct.protoent = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@.str = [28 x i8] c"can't get \22%s\22 host entry \0A\00"
@.str.1 = [31 x i8] c"can't get \22%s\22 protocol entry\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 = [25 x i8] c"can't create socket: %s\0A\00"
@errno = global i32 0
@.str.4 = [27 x i8] c"can't connect to %s.%d:%s\0A\00"
define i32 @connect_sock(i8* %0, i32 %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 i32 @htons(i32 %1)
%10 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1
store i32 %9, i32* %10
%11 = call %struct.hostent* @gethostbyname(i8* %0)
%12 = icmp eq %struct.hostent* %11, null
br i1 %12, label %21, label %13
13: ; preds = %3
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0
%15 = getelementptr inbounds %struct.hostent, %struct.hostent* %11, i64 0, i32 1
%16 = load i32*, i32** %15
%17 = load i32, i32* %16
%18 = getelementptr inbounds %struct.hostent, %struct.hostent* %11, i64 0, i32 0
%19 = load i32, i32* %18
%20 = call i32 @memcpy(i32* %14, i32 %17, i32 %19)
br label %23
21: ; preds = %3
%22 = call i32 (i8*, i8*, ...) @errexit(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %23
23: ; preds = %21, %13
%24 = call %struct.protoent* @getprotobyname(i8* %2)
%25 = icmp eq %struct.protoent* %24, null
br i1 %25, label %26, label %28
26: ; preds = %23
%27 = call i32 (i8*, i8*, ...) @errexit(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i8* %2)
br label %28
28: ; preds = %26, %23
%29 = call i64 @strcmp(i8* %2, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0))
%30 = icmp eq i64 %29, 0
%31 = load i32, i32* @SOCK_DGRAM
%32 = load i32, i32* @SOCK_STREAM
%33 = select i1 %30, i32 %31, i32 %32
%34 = load i32, i32* @PF_INET
%35 = getelementptr inbounds %struct.protoent, %struct.protoent* %24, i64 0, i32 0
%36 = load i32, i32* %35
%37 = call i32 @socket(i32 %34, i32 %33, i32 %36)
%38 = icmp slt i32 %37, 0
br i1 %38, label %39, label %45
39: ; preds = %28
%40 = load i32, i32* @errno
%41 = call i32 @strerror(i32 %40)
%42 = sext i32 %41 to i64
%43 = inttoptr i64 %42 to i8*
%44 = call i32 (i8*, i8*, ...) @errexit(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0), i8* %43)
br label %45
45: ; preds = %39, %28
%46 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%47 = call i64 @connect(i32 %37, %struct.sockaddr* %46, i32 12)
%48 = icmp slt i64 %47, 0
br i1 %48, label %49, label %53
49: ; preds = %45
%50 = load i32, i32* @errno
%51 = call i32 @strerror(i32 %50)
%52 = call i32 (i8*, i8*, ...) @errexit(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.4, i64 0, i64 0), i8* %0, i32 %1, i32 %51)
br label %53
53: ; preds = %49, %45
ret i32 %37
}