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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ bcopy(char *, char *, int /*<<< orphan*/);
int /*<<< orphan*/ bzero(char *, int);
int connect(int, struct sockaddr *, int);
struct hostent *gethostbyname(char *);
int /*<<< orphan*/ htons(int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int cf_socket_connect(char *host, int port, int *sd) {
  struct sockaddr_in peer;
  struct hostent *hp;
  bzero((char *)&peer, sizeof(peer));
  hp = gethostbyname(host);
  if (hp == NULL) {
    return -1;
  }
  peer.sin_family = AF_INET;
  peer.sin_port = htons(port);
  bcopy((char *)hp->h_addr, (char *)&peer.sin_addr, hp->h_length);
  *sd = socket(AF_INET, SOCK_STREAM, 0);
  if (*sd == -1) {
    return -1;
  }
  return connect(*sd, (struct sockaddr *)&peer, sizeof(peer));
}


x86 assembly (clang, -O0):
<cf_socket_connect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
lea    -0x30(%rbp),%rdi
mov    $0xc,%esi
call   1060 <bzero@plt>
mov    -0x10(%rbp),%rdi
call   1040 <gethostbyname@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    11a5 <cf_socket_connect+0x45>
movl   $0xffffffff,-0x4(%rbp)
jmp    1224 <cf_socket_connect+0xc4>
mov    0x2e3c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x14(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x38(%rbp),%rax
mov    0x8(%rax),%rdi
lea    -0x30(%rbp),%rsi
mov    -0x38(%rbp),%rax
mov    (%rax),%edx
call   1050 <bcopy@plt>
mov    0x2e0e(%rip),%rax
mov    (%rax),%edi
mov    0x2df5(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x20(%rbp),%rax
cmpl   $0xffffffff,(%rax)
jne    120d <cf_socket_connect+0xad>
movl   $0xffffffff,-0x4(%rbp)
jmp    1224 <cf_socket_connect+0xc4>
mov    -0x20(%rbp),%rax
mov    (%rax),%edi
lea    -0x30(%rbp),%rsi
mov    $0xc,%edx
call   1070 <connect@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cf_socket_connect>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%r14
mov    %esi,%ebx
mov    %rdi,%rbp
mov    %rsp,%rdi
mov    $0xc,%esi
call   1060 <bzero@plt>
mov    %rbp,%rdi
call   1040 <gethostbyname@plt>
mov    $0xffffffff,%r15d
test   %rax,%rax
je     11ee <cf_socket_connect+0x8e>
mov    %rax,%rbp
mov    0x2e4a(%rip),%r12
mov    (%r12),%eax
mov    %eax,0x8(%rsp)
mov    %ebx,%edi
call   1030 <htons@plt>
mov    %eax,0x4(%rsp)
mov    0x8(%rbp),%rsi
mov    0x0(%rbp),%edx
mov    %rsp,%rdi
call   1050 <memmove@plt>
mov    (%r12),%edi
mov    0x2e0d(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,(%r14)
cmp    $0xffffffff,%eax
je     11ee <cf_socket_connect+0x8e>
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0xc,%edx
call   1070 <connect@plt>
mov    %eax,%r15d
mov    %r15d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.hostent = type { i32, i64 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
define i32 @cf_socket_connect(i8* %0, i32 %1, i32* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32*
%8 = alloca %struct.sockaddr_in
%9 = alloca %struct.hostent*
store i8* %0, i8** %5
store i32 %1, i32* %6
store i32* %2, i32** %7
%10 = bitcast %struct.sockaddr_in* %8 to i8*
%11 = call i32 @bzero(i8* %10, i32 12)
%12 = load i8*, i8** %5
%13 = call %struct.hostent* @gethostbyname(i8* %12)
store %struct.hostent* %13, %struct.hostent** %9
%14 = load %struct.hostent*, %struct.hostent** %9
%15 = icmp eq %struct.hostent* %14, null
br i1 %15, label %16, label %17
16: ; preds = %3
store i32 -1, i32* %4
br label %46
17: ; preds = %3
%18 = load i32, i32* @AF_INET
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 2
store i32 %18, i32* %19
%20 = load i32, i32* %6
%21 = call i32 @htons(i32 %20)
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 1
store i32 %21, i32* %22
%23 = load %struct.hostent*, %struct.hostent** %9
%24 = getelementptr inbounds %struct.hostent, %struct.hostent* %23, i32 0, i32 1
%25 = load i64, i64* %24
%26 = inttoptr i64 %25 to i8*
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 0
%28 = bitcast i32* %27 to i8*
%29 = load %struct.hostent*, %struct.hostent** %9
%30 = getelementptr inbounds %struct.hostent, %struct.hostent* %29, i32 0, i32 0
%31 = load i32, i32* %30
%32 = call i32 @bcopy(i8* %26, i8* %28, i32 %31)
%33 = load i32, i32* @AF_INET
%34 = load i32, i32* @SOCK_STREAM
%35 = call i32 @socket(i32 %33, i32 %34, i32 0)
%36 = load i32*, i32** %7
store i32 %35, i32* %36
%37 = load i32*, i32** %7
%38 = load i32, i32* %37
%39 = icmp eq i32 %38, -1
br i1 %39, label %40, label %41
40: ; preds = %17
store i32 -1, i32* %4
br label %46
41: ; preds = %17
%42 = load i32*, i32** %7
%43 = load i32, i32* %42
%44 = bitcast %struct.sockaddr_in* %8 to %struct.sockaddr*
%45 = call i32 @connect(i32 %43, %struct.sockaddr* %44, i32 12)
store i32 %45, i32* %4
br label %46
46: ; preds = %41, %40, %16
%47 = load i32, i32* %4
ret i32 %47
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.hostent = type { i32, i64 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
define i32 @cf_socket_connect(i8* %0, i32 %1, i32* nocapture writeonly %2) {
%4 = alloca %struct.sockaddr_in
%5 = bitcast %struct.sockaddr_in* %4 to i8*
%6 = call i32 @bzero(i8* %5, i32 12)
%7 = tail call %struct.hostent* @gethostbyname(i8* %0)
%8 = icmp eq %struct.hostent* %7, null
br i1 %8, label %26, label %9
9: ; preds = %3
%10 = load i32, i32* @AF_INET
%11 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 2
store i32 %10, i32* %11
%12 = tail call i32 @htons(i32 %1)
%13 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1
store i32 %12, i32* %13
%14 = getelementptr inbounds %struct.hostent, %struct.hostent* %7, i64 0, i32 1
%15 = load i64, i64* %14
%16 = inttoptr i64 %15 to i8*
%17 = getelementptr inbounds %struct.hostent, %struct.hostent* %7, i64 0, i32 0
%18 = load i32, i32* %17
%19 = load i32, i32* @AF_INET
%20 = load i32, i32* @SOCK_STREAM
%21 = tail call i32 @socket(i32 %19, i32 %20, i32 0)
store i32 %21, i32* %2
%22 = icmp eq i32 %21, -1
br i1 %22, label %26, label %23
23: ; preds = %9
%24 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%25 = call i32 @connect(i32 %21, %struct.sockaddr* %24, i32 12)
br label %26
26: ; preds = %9, %3, %23
%27 = phi i32 [ %25, %23 ], [ -1, %3 ], [ -1, %9 ]
ret i32 %27
}