#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__ {
  scalar_t__ s_addr;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
  TYPE_1__ sin_addr;
};
struct sockaddr {
  int dummy;
};
struct in_addr {
  scalar_t__ s_addr;
};
struct hostent {
  scalar_t__ h_length;
  scalar_t__ *h_addr_list;
};
typedef int /*<<< orphan*/ addr;
typedef int /*<<< orphan*/ SSL;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
scalar_t__ SSL_connect(int /*<<< orphan*/ *);
int /*<<< orphan*/ SSL_set_fd(int /*<<< orphan*/ *, int);
int /*<<< orphan*/ close(int);
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ errmsg(char *);
struct hostent *gethostbyname(char *);
int /*<<< orphan*/ htons(unsigned short);
scalar_t__ inet_addr(char *);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int sslConnect(SSL *ssl, char *host, int port) {
  struct sockaddr_in addr;
  struct hostent *p;
  int sd;
  memset(&addr, 0, sizeof(addr));
  if ((long)(addr.sin_addr.s_addr = inet_addr(host)) == -1) {
    if (!(p = gethostbyname(host)) || p->h_length == 0)
      return -1;
    addr.sin_addr.s_addr = ((struct in_addr *)p->h_addr_list[0])->s_addr;
  }
  if ((sd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
    errmsg("No socket");
    return -1;
  }
  addr.sin_family = AF_INET;
  addr.sin_port = htons((unsigned short)port);
  if (connect(sd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
    close(sd);
    return -1;
  }
  SSL_set_fd(ssl, sd);
  if (SSL_connect(ssl) >= 0)
    return sd;
  close(sd);
  return -1;
}


x86 assembly (clang, -O0):
<sslConnect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1050 <memset@plt>
mov    -0x18(%rbp),%rdi
call   1070 <inet_addr@plt>
mov    %rax,-0x28(%rbp)
cmp    $0xffffffffffffffff,%rax
jne    121d <sslConnect+0x7d>
mov    -0x18(%rbp),%rdi
call   1080 <gethostbyname@plt>
mov    %rax,-0x38(%rbp)
cmp    $0x0,%rax
je     11ff <sslConnect+0x5f>
mov    -0x38(%rbp),%rax
cmpq   $0x0,(%rax)
jne    120b <sslConnect+0x6b>
movl   $0xffffffff,-0x4(%rbp)
jmp    12dc <sslConnect+0x13c>
mov    -0x38(%rbp),%rax
mov    0x8(%rax),%rax
mov    (%rax),%rax
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
mov    0x2dc4(%rip),%rax
mov    (%rax),%edi
mov    0x2dab(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,-0x3c(%rbp)
cmp    $0x0,%eax
jge    125a <sslConnect+0xba>
lea    0xdb7(%rip),%rdi
call   1090 <errmsg@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12dc <sslConnect+0x13c>
mov    0x2d87(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x1c(%rbp),%eax
movzwl %ax,%edi
call   1040 <htons@plt>
mov    %eax,-0x30(%rbp)
mov    -0x3c(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0x10,%edx
call   10b0 <connect@plt>
cmp    $0x0,%rax
jge    12a3 <sslConnect+0x103>
mov    -0x3c(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12dc <sslConnect+0x13c>
mov    -0x10(%rbp),%rdi
mov    -0x3c(%rbp),%esi
call   1030 <SSL_set_fd@plt>
mov    -0x10(%rbp),%rdi
call   10a0 <SSL_connect@plt>
cmp    $0x0,%rax
jl     12cd <sslConnect+0x12d>
mov    -0x3c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12dc <sslConnect+0x13c>
mov    -0x3c(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sslConnect>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %edx,%r15d
mov    %rsi,%rbp
mov    %rdi,%r14
lea    0x8(%rsp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1050 <memset@plt>
mov    %rbp,%rdi
call   1070 <inet_addr@plt>
mov    %rax,0x10(%rsp)
cmp    $0xffffffffffffffff,%rax
jne    1206 <sslConnect+0x66>
mov    %rbp,%rdi
call   1080 <gethostbyname@plt>
mov    $0xffffffff,%ebp
test   %rax,%rax
je     127f <sslConnect+0xdf>
cmpq   $0x0,(%rax)
je     127f <sslConnect+0xdf>
mov    0x8(%rax),%rax
mov    (%rax),%rax
mov    (%rax),%rax
mov    %rax,0x10(%rsp)
mov    0x2ddb(%rip),%rbx
mov    (%rbx),%edi
mov    0x2dc2(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
test   %eax,%eax
js     126e <sslConnect+0xce>
mov    %eax,%ebp
mov    (%rbx),%eax
mov    %eax,0xc(%rsp)
movzwl %r15w,%edi
call   1040 <htons@plt>
mov    %eax,0x8(%rsp)
lea    0x8(%rsp),%rsi
mov    %ebp,%edi
mov    $0x10,%edx
call   10b0 <connect@plt>
test   %rax,%rax
js     1265 <sslConnect+0xc5>
mov    %r14,%rdi
mov    %ebp,%esi
call   1030 <SSL_set_fd@plt>
mov    %r14,%rdi
call   10a0 <SSL_connect@plt>
test   %rax,%rax
jns    127f <sslConnect+0xdf>
mov    %ebp,%edi
call   1060 <close@plt>
jmp    127a <sslConnect+0xda>
lea    0xd8b(%rip),%rdi
call   1090 <errmsg@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i64 }
%struct.hostent = type { i64, i64* }
%struct.in_addr = type { i64 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [10 x i8] c"No socket\00"
define i32 @sslConnect(i32* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca %struct.sockaddr_in
%9 = alloca %struct.hostent*
%10 = alloca i32
store i32* %0, i32** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%11 = call i32 @memset(%struct.sockaddr_in* %8, i32 0, i32 16)
%12 = load i8*, i8** %6
%13 = call i64 @inet_addr(i8* %12)
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 2
%15 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %14, i32 0, i32 0
store i64 %13, i64* %15
%16 = icmp eq i64 %13, -1
br i1 %16, label %17, label %38
17: ; preds = %3
%18 = load i8*, i8** %6
%19 = call %struct.hostent* @gethostbyname(i8* %18)
store %struct.hostent* %19, %struct.hostent** %9
%20 = icmp ne %struct.hostent* %19, null
br i1 %20, label %21, label %26
21: ; preds = %17
%22 = load %struct.hostent*, %struct.hostent** %9
%23 = getelementptr inbounds %struct.hostent, %struct.hostent* %22, i32 0, i32 0
%24 = load i64, i64* %23
%25 = icmp eq i64 %24, 0
br i1 %25, label %26, label %27
26: ; preds = %21, %17
store i32 -1, i32* %4
br label %71
27: ; preds = %21
%28 = load %struct.hostent*, %struct.hostent** %9
%29 = getelementptr inbounds %struct.hostent, %struct.hostent* %28, i32 0, i32 1
%30 = load i64*, i64** %29
%31 = getelementptr inbounds i64, i64* %30, i64 0
%32 = load i64, i64* %31
%33 = inttoptr i64 %32 to %struct.in_addr*
%34 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %33, i32 0, i32 0
%35 = load i64, i64* %34
%36 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 2
%37 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %36, i32 0, i32 0
store i64 %35, i64* %37
br label %38
38: ; preds = %27, %3
%39 = load i32, i32* @AF_INET
%40 = load i32, i32* @SOCK_STREAM
%41 = call i32 @socket(i32 %39, i32 %40, i32 0)
store i32 %41, i32* %10
%42 = icmp slt i32 %41, 0
br i1 %42, label %43, label %45
43: ; preds = %38
%44 = call i32 @errmsg(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %4
br label %71
45: ; preds = %38
%46 = load i32, i32* @AF_INET
%47 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 1
store i32 %46, i32* %47
%48 = load i32, i32* %7
%49 = trunc i32 %48 to i16
%50 = call i32 @htons(i16 zeroext %49)
%51 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 0
store i32 %50, i32* %51
%52 = load i32, i32* %10
%53 = bitcast %struct.sockaddr_in* %8 to %struct.sockaddr*
%54 = call i64 @connect(i32 %52, %struct.sockaddr* %53, i32 16)
%55 = icmp slt i64 %54, 0
br i1 %55, label %56, label %59
56: ; preds = %45
%57 = load i32, i32* %10
%58 = call i32 @close(i32 %57)
store i32 -1, i32* %4
br label %71
59: ; preds = %45
%60 = load i32*, i32** %5
%61 = load i32, i32* %10
%62 = call i32 @SSL_set_fd(i32* %60, i32 %61)
%63 = load i32*, i32** %5
%64 = call i64 @SSL_connect(i32* %63)
%65 = icmp sge i64 %64, 0
br i1 %65, label %66, label %68
66: ; preds = %59
%67 = load i32, i32* %10
store i32 %67, i32* %4
br label %71
68: ; preds = %59
%69 = load i32, i32* %10
%70 = call i32 @close(i32 %69)
store i32 -1, i32* %4
br label %71
71: ; preds = %68, %66, %56, %43, %26
%72 = load i32, i32* %4
ret i32 %72
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i64 }
%struct.hostent = type { i64, i64* }
%struct.in_addr = type { i64 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [10 x i8] c"No socket\00"
define i32 @sslConnect(i32* %0, i8* %1, i32 %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 16)
%7 = call i64 @inet_addr(i8* %1)
%8 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 2, i32 0
store i64 %7, i64* %8
%9 = icmp eq i64 %7, -1
br i1 %9, label %10, label %24
10: ; preds = %3
%11 = call %struct.hostent* @gethostbyname(i8* %1)
%12 = icmp eq %struct.hostent* %11, null
br i1 %12, label %48, label %13
13: ; preds = %10
%14 = getelementptr inbounds %struct.hostent, %struct.hostent* %11, i64 0, i32 0
%15 = load i64, i64* %14
%16 = icmp eq i64 %15, 0
br i1 %16, label %48, label %17
17: ; preds = %13
%18 = getelementptr inbounds %struct.hostent, %struct.hostent* %11, i64 0, i32 1
%19 = load i64*, i64** %18
%20 = load i64, i64* %19
%21 = inttoptr i64 %20 to %struct.in_addr*
%22 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %21, i64 0, i32 0
%23 = load i64, i64* %22
store i64 %23, i64* %8
br label %24
24: ; preds = %17, %3
%25 = load i32, i32* @AF_INET
%26 = load i32, i32* @SOCK_STREAM
%27 = call i32 @socket(i32 %25, i32 %26, i32 0)
%28 = icmp slt i32 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %24
%30 = call i32 @errmsg(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
br label %48
31: ; preds = %24
%32 = load i32, i32* @AF_INET
%33 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1
store i32 %32, i32* %33
%34 = trunc i32 %2 to i16
%35 = call i32 @htons(i16 zeroext %34)
%36 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0
store i32 %35, i32* %36
%37 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%38 = call i64 @connect(i32 %27, %struct.sockaddr* %37, i32 16)
%39 = icmp slt i64 %38, 0
br i1 %39, label %40, label %42
40: ; preds = %31
%41 = call i32 @close(i32 %27)
br label %48
42: ; preds = %31
%43 = call i32 @SSL_set_fd(i32* %0, i32 %27)
%44 = call i64 @SSL_connect(i32* %0)
%45 = icmp sgt i64 %44, -1
br i1 %45, label %48, label %46
46: ; preds = %42
%47 = call i32 @close(i32 %27)
br label %48
48: ; preds = %42, %10, %13, %46, %40, %29
%49 = phi i32 [ -1, %29 ], [ -1, %40 ], [ -1, %46 ], [ -1, %13 ], [ -1, %10 ], [ %27, %42 ]
ret i32 %49
}