#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 {
  int /*<<< orphan*/ sin_port;
  TYPE_1__ sin_addr;
  scalar_t__ sin_zero;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
struct hostent {
  scalar_t__ h_length;
  scalar_t__ *h_addr_list;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int const);
int connect(int const, struct sockaddr *, int);
int /*<<< orphan*/ errno;
struct hostent *gethostbyname(char const *const);
int /*<<< orphan*/ htons(unsigned short);
int /*<<< orphan*/ memcpy(void *, void *, size_t);
int /*<<< orphan*/ memset(void *, int /*<<< orphan*/, int);
int /*<<< orphan*/ pm_asprintf(char const **const, char *, char const *const,
                               ...);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
void ConnectToSocket(const char *const machineName, int const portNum,
                     struct hostent **const hostEnt, int *const socketFdP,
                     const char **const errorP) {
  int rc;
  *errorP = NULL;
  if ((*hostEnt) == NULL) {
    (*hostEnt) = gethostbyname(machineName);
    if ((*hostEnt) == NULL)
      pm_asprintf(errorP, "Couldn't get host by name (%s)", machineName);
  }
  if (!*errorP) {
    rc = socket(AF_INET, SOCK_STREAM, 0);
    if (rc < 0)
      pm_asprintf(errorP, "socket() failed with errno %d (%s)", errno,
                  strerror(errno));
    else {
      int const socketFd = rc;
      int rc;
      unsigned short tempShort;
      struct sockaddr_in nameEntry;
      nameEntry.sin_family = AF_INET;
      memset((void *)nameEntry.sin_zero, 0, 8);
      memcpy((void *)&(nameEntry.sin_addr.s_addr),
             (void *)(*hostEnt)->h_addr_list[0], (size_t)(*hostEnt)->h_length);
      tempShort = portNum;
      nameEntry.sin_port = htons(tempShort);
      rc = connect(socketFd, (struct sockaddr *)&nameEntry,
                   sizeof(struct sockaddr));
      if (rc != 0)
        pm_asprintf(errorP,
                    "connect() to host '%s', port %d failed with "
                    "errno %d (%s)",
                    machineName, portNum, errno, strerror(errno));
      else {
        *errorP = NULL;
        *socketFdP = socketFd;
      }
      if (*errorP)
        close(socketFd);
    }
  }
}


x86 assembly (clang, -O0):
<ConnectToSocket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
mov    -0x28(%rbp),%rax
movq   $0x0,(%rax)
mov    -0x18(%rbp),%rax
cmpq   $0x0,(%rax)
jne    1203 <ConnectToSocket+0x73>
mov    -0x8(%rbp),%rdi
call   1070 <gethostbyname@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x18(%rbp),%rax
cmpq   $0x0,(%rax)
jne    11fe <ConnectToSocket+0x6e>
mov    -0x28(%rbp),%rdi
mov    -0x8(%rbp),%rdx
lea    0xe09(%rip),%rsi
mov    $0x0,%al
call   1030 <pm_asprintf@plt>
jmp    1203 <ConnectToSocket+0x73>
mov    -0x28(%rbp),%rax
cmpq   $0x0,(%rax)
jne    1379 <ConnectToSocket+0x1e9>
mov    0x2dd0(%rip),%rax
mov    (%rax),%edi
mov    0x2db7(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jge    1278 <ConnectToSocket+0xe8>
mov    -0x28(%rbp),%rax
mov    %rax,-0x60(%rbp)
mov    0x2d8a(%rip),%rax
movslq (%rax),%rax
mov    %rax,-0x58(%rbp)
mov    0x2d7c(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x60(%rbp),%rdi
mov    -0x58(%rbp),%rdx
mov    %eax,%ecx
lea    0xdb3(%rip),%rsi
mov    $0x0,%al
call   1030 <pm_asprintf@plt>
jmp    1374 <ConnectToSocket+0x1e4>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    0x2d63(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x40(%rbp)
mov    -0x48(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
lea    -0x50(%rbp),%rdi
add    $0x4,%rdi
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    0x8(%rax),%rax
mov    (%rax),%rsi
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    (%rax),%rdx
call   1080 <memcpy@plt>
mov    -0xc(%rbp),%eax
mov    %ax,-0x36(%rbp)
movzwl -0x36(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x50(%rbp)
mov    -0x30(%rbp),%edi
lea    -0x50(%rbp),%rsi
mov    $0x4,%edx
call   1090 <connect@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
je     1345 <ConnectToSocket+0x1b5>
mov    -0x28(%rbp),%rax
mov    %rax,-0x78(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x70(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x68(%rbp)
mov    0x2cc3(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x64(%rbp)
mov    0x2cb7(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x78(%rbp),%rdi
mov    -0x70(%rbp),%rdx
mov    -0x68(%rbp),%ecx
mov    -0x64(%rbp),%r8d
mov    %eax,%r9d
lea    0xd09(%rip),%rsi
mov    $0x0,%al
call   1030 <pm_asprintf@plt>
jmp    1359 <ConnectToSocket+0x1c9>
mov    -0x28(%rbp),%rax
movq   $0x0,(%rax)
mov    -0x30(%rbp),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x28(%rbp),%rax
cmpq   $0x0,(%rax)
je     136f <ConnectToSocket+0x1df>
mov    -0x30(%rbp),%edi
call   1060 <close@plt>
jmp    1374 <ConnectToSocket+0x1e4>
jmp    1379 <ConnectToSocket+0x1e9>
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ConnectToSocket>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %r8,%r13
mov    %rcx,%rbp
mov    %rdx,%rbx
mov    %esi,%r14d
mov    %rdi,%r12
movq   $0x0,(%r8)
cmpq   $0x0,(%rdx)
je     1274 <ConnectToSocket+0xe4>
mov    %rbp,0x8(%rsp)
mov    0x2e1e(%rip),%r15
mov    (%r15),%edi
mov    0x2e04(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
test   %eax,%eax
js     12a4 <ConnectToSocket+0x114>
mov    %eax,%ebp
mov    (%r15),%eax
mov    %eax,0x20(%rsp)
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
lea    0x14(%rsp),%rdi
mov    (%rbx),%rax
mov    (%rax),%rdx
mov    0x8(%rax),%rax
mov    (%rax),%rsi
call   1080 <memcpy@plt>
movzwl %r14w,%edi
call   1040 <htons@plt>
mov    %eax,0x10(%rsp)
lea    0x10(%rsp),%rsi
mov    %ebp,%edi
mov    $0x4,%edx
call   1090 <connect@plt>
test   %eax,%eax
je     12db <ConnectToSocket+0x14b>
mov    0x2d92(%rip),%rax
mov    (%rax),%ebx
mov    %ebx,%edi
call   10a0 <strerror@plt>
lea    0xdf4(%rip),%rsi
mov    %r13,%rdi
mov    %r12,%rdx
mov    %r14d,%ecx
mov    %ebx,%r8d
mov    %eax,%r9d
xor    %eax,%eax
call   1030 <pm_asprintf@plt>
cmpq   $0x0,0x0(%r13)
je     12ea <ConnectToSocket+0x15a>
mov    %ebp,%edi
call   1060 <close@plt>
jmp    12ea <ConnectToSocket+0x15a>
mov    %r12,%rdi
call   1070 <gethostbyname@plt>
mov    %rax,(%rbx)
test   %rax,%rax
jne    1298 <ConnectToSocket+0x108>
lea    0xd75(%rip),%rsi
mov    %r13,%rdi
mov    %r12,%rdx
xor    %eax,%eax
call   1030 <pm_asprintf@plt>
cmpq   $0x0,0x0(%r13)
jne    12ea <ConnectToSocket+0x15a>
jmp    11be <ConnectToSocket+0x2e>
mov    0x2d25(%rip),%rax
mov    (%rax),%eax
movslq %eax,%rbx
mov    %ebx,%edi
call   10a0 <strerror@plt>
lea    0xd61(%rip),%rsi
mov    %r13,%rdi
mov    %rbx,%rdx
mov    %eax,%ecx
xor    %eax,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <pm_asprintf@plt>
movq   $0x0,0x0(%r13)
mov    0x8(%rsp),%rax
mov    %ebp,(%rax)
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.hostent = type { i64, i64* }
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i64, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [31 x i8] c"Couldn't get host by name (%s)\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [35 x i8] c"socket() failed with errno %d (%s)\00"
@errno = global i32 0
@.str.2 = [58 x i8] c"connect() to host '%s', port %d failed with errno %d (%s)\00"
define void @ConnectToSocket(i8* %0, i32 %1, %struct.hostent** %2, i32* %3, i8** %4) {
%6 = alloca i8*
%7 = alloca i32
%8 = alloca %struct.hostent**
%9 = alloca i32*
%10 = alloca i8**
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i16
%15 = alloca %struct.sockaddr_in
store i8* %0, i8** %6
store i32 %1, i32* %7
store %struct.hostent** %2, %struct.hostent*** %8
store i32* %3, i32** %9
store i8** %4, i8*** %10
%16 = load i8**, i8*** %10
store i8* null, i8** %16
%17 = load %struct.hostent**, %struct.hostent*** %8
%18 = load %struct.hostent*, %struct.hostent** %17
%19 = icmp eq %struct.hostent* %18, null
br i1 %19, label %20, label %32
20: ; preds = %5
%21 = load i8*, i8** %6
%22 = call %struct.hostent* @gethostbyname(i8* %21)
%23 = load %struct.hostent**, %struct.hostent*** %8
store %struct.hostent* %22, %struct.hostent** %23
%24 = load %struct.hostent**, %struct.hostent*** %8
%25 = load %struct.hostent*, %struct.hostent** %24
%26 = icmp eq %struct.hostent* %25, null
br i1 %26, label %27, label %31
27: ; preds = %20
%28 = load i8**, i8*** %10
%29 = load i8*, i8** %6
%30 = call i32 (i8**, i8*, i8*, ...) @pm_asprintf(i8** %28, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i8* %29)
br label %31
31: ; preds = %27, %20
br label %32
32: ; preds = %31, %5
%33 = load i8**, i8*** %10
%34 = load i8*, i8** %33
%35 = icmp ne i8* %34, null
br i1 %35, label %104, label %36
36: ; preds = %32
%37 = load i32, i32* @AF_INET
%38 = load i32, i32* @SOCK_STREAM
%39 = call i32 @socket(i32 %37, i32 %38, i32 0)
store i32 %39, i32* %11
%40 = load i32, i32* %11
%41 = icmp slt i32 %40, 0
br i1 %41, label %42, label %50
42: ; preds = %36
%43 = load i8**, i8*** %10
%44 = load i32, i32* @errno
%45 = sext i32 %44 to i64
%46 = inttoptr i64 %45 to i8*
%47 = load i32, i32* @errno
%48 = call i32 @strerror(i32 %47)
%49 = call i32 (i8**, i8*, i8*, ...) @pm_asprintf(i8** %43, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), i8* %46, i32 %48)
br label %103
50: ; preds = %36
%51 = load i32, i32* %11
store i32 %51, i32* %12
%52 = load i32, i32* @AF_INET
%53 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %15, i32 0, i32 3
store i32 %52, i32* %53
%54 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %15, i32 0, i32 2
%55 = load i64, i64* %54
%56 = inttoptr i64 %55 to i8*
%57 = call i32 @memset(i8* %56, i32 0, i32 8)
%58 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %15, i32 0, i32 1
%59 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %58, i32 0, i32 0
%60 = bitcast i32* %59 to i8*
%61 = load %struct.hostent**, %struct.hostent*** %8
%62 = load %struct.hostent*, %struct.hostent** %61
%63 = getelementptr inbounds %struct.hostent, %struct.hostent* %62, i32 0, i32 1
%64 = load i64*, i64** %63
%65 = getelementptr inbounds i64, i64* %64, i64 0
%66 = load i64, i64* %65
%67 = inttoptr i64 %66 to i8*
%68 = load %struct.hostent**, %struct.hostent*** %8
%69 = load %struct.hostent*, %struct.hostent** %68
%70 = getelementptr inbounds %struct.hostent, %struct.hostent* %69, i32 0, i32 0
%71 = load i64, i64* %70
%72 = call i32 @memcpy(i8* %60, i8* %67, i64 %71)
%73 = load i32, i32* %7
%74 = trunc i32 %73 to i16
store i16 %74, i16* %14
%75 = load i16, i16* %14
%76 = call i32 @htons(i16 zeroext %75)
%77 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %15, i32 0, i32 0
store i32 %76, i32* %77
%78 = load i32, i32* %12
%79 = bitcast %struct.sockaddr_in* %15 to %struct.sockaddr*
%80 = call i32 @connect(i32 %78, %struct.sockaddr* %79, i32 4)
store i32 %80, i32* %13
%81 = load i32, i32* %13
%82 = icmp ne i32 %81, 0
br i1 %82, label %83, label %91
83: ; preds = %50
%84 = load i8**, i8*** %10
%85 = load i8*, i8** %6
%86 = load i32, i32* %7
%87 = load i32, i32* @errno
%88 = load i32, i32* @errno
%89 = call i32 @strerror(i32 %88)
%90 = call i32 (i8**, i8*, i8*, ...) @pm_asprintf(i8** %84, i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str.2, i64 0, i64 0), i8* %85, i32 %86, i32 %87, i32 %89)
br label %95
91: ; preds = %50
%92 = load i8**, i8*** %10
store i8* null, i8** %92
%93 = load i32, i32* %12
%94 = load i32*, i32** %9
store i32 %93, i32* %94
br label %95
95: ; preds = %91, %83
%96 = load i8**, i8*** %10
%97 = load i8*, i8** %96
%98 = icmp ne i8* %97, null
br i1 %98, label %99, label %102
99: ; preds = %95
%100 = load i32, i32* %12
%101 = call i32 @close(i32 %100)
br label %102
102: ; preds = %99, %95
br label %103
103: ; preds = %102, %42
br label %104
104: ; preds = %103, %32
ret void
}

LLVM IR (-O2):
%struct.hostent = type { i64, i64* }
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i64, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [31 x i8] c"Couldn't get host by name (%s)\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [35 x i8] c"socket() failed with errno %d (%s)\00"
@errno = global i32 0
@.str.2 = [58 x i8] c"connect() to host '%s', port %d failed with errno %d (%s)\00"
define void @ConnectToSocket(i8* %0, i32 %1, %struct.hostent** nocapture %2, i32* nocapture writeonly %3, i8** %4) {
%6 = alloca %struct.sockaddr_in
store i8* null, i8** %4
%7 = load %struct.hostent*, %struct.hostent** %2
%8 = icmp eq %struct.hostent* %7, null
br i1 %8, label %9, label %17
9: ; preds = %5
%10 = tail call %struct.hostent* @gethostbyname(i8* %0)
store %struct.hostent* %10, %struct.hostent** %2
%11 = icmp eq %struct.hostent* %10, null
br i1 %11, label %12, label %14
12: ; preds = %9
%13 = tail call i32 (i8**, i8*, i8*, ...) @pm_asprintf(i8** %4, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %14
14: ; preds = %9, %12
%15 = load i8*, i8** %4
%16 = icmp eq i8* %15, null
br i1 %16, label %17, label %59
17: ; preds = %5, %14
%18 = load i32, i32* @AF_INET
%19 = load i32, i32* @SOCK_STREAM
%20 = tail call i32 @socket(i32 %18, i32 %19, i32 0)
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %28
22: ; preds = %17
%23 = load i32, i32* @errno
%24 = sext i32 %23 to i64
%25 = inttoptr i64 %24 to i8*
%26 = tail call i32 @strerror(i32 %23)
%27 = tail call i32 (i8**, i8*, i8*, ...) @pm_asprintf(i8** %4, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), i8* %25, i32 %26)
br label %59
28: ; preds = %17
%29 = bitcast %struct.sockaddr_in* %6 to i8*
%30 = load i32, i32* @AF_INET
%31 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i64 0, i32 3
store i32 %30, i32* %31
%32 = tail call i32 @memset(i8* undef, i32 0, i32 8)
%33 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i64 0, i32 1, i32 0
%34 = bitcast i32* %33 to i8*
%35 = load %struct.hostent*, %struct.hostent** %2
%36 = getelementptr inbounds %struct.hostent, %struct.hostent* %35, i64 0, i32 1
%37 = load i64*, i64** %36
%38 = load i64, i64* %37
%39 = inttoptr i64 %38 to i8*
%40 = getelementptr inbounds %struct.hostent, %struct.hostent* %35, i64 0, i32 0
%41 = load i64, i64* %40
%42 = call i32 @memcpy(i8* %34, i8* %39, i64 %41)
%43 = trunc i32 %1 to i16
%44 = call i32 @htons(i16 zeroext %43)
%45 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i64 0, i32 0
store i32 %44, i32* %45
%46 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%47 = call i32 @connect(i32 %20, %struct.sockaddr* %46, i32 4)
%48 = icmp eq i32 %47, 0
br i1 %48, label %49, label %50
49: ; preds = %28
store i8* null, i8** %4
store i32 %20, i32* %3
br label %58
50: ; preds = %28
%51 = load i32, i32* @errno
%52 = call i32 @strerror(i32 %51)
%53 = call i32 (i8**, i8*, i8*, ...) @pm_asprintf(i8** %4, i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str.2, i64 0, i64 0), i8* %0, i32 %1, i32 %51, i32 %52)
%54 = load i8*, i8** %4
%55 = icmp eq i8* %54, null
br i1 %55, label %58, label %56
56: ; preds = %50
%57 = call i32 @close(i32 %20)
br label %58
58: ; preds = %49, %56, %50
br label %59
59: ; preds = %22, %58, %14
ret void
}