// File: /workspace/github_c_makefile_star2_O2/tomka[P]cliaspora/ssl.c
// Function: ssl_connect
ssl_conn_t *
ssl_connect(const char *host, u_short port)
{
	int	s;
	SSL	*handle;
	SSL_CTX *ctx;
	ssl_conn_t	   *cp;
	static int	   init = 1;
	struct sockaddr_in target;

	errno = 0;
	if (init == 1) {
		SSL_load_error_strings();
		(void)SSL_library_init();
		init = 0;
	}
        if ((s = socket(AF_INET, SOCK_STREAM, 0)) == -1)
		return (NULL);
	(void)memset(&target, 0, sizeof(target));
	target.sin_family = AF_INET;
	target.sin_port = htons(port);
	if ((target.sin_addr.s_addr = inet_addr(host)) == INADDR_NONE) {
		struct hostent *t_info;

		if ((t_info = gethostbyname(host)) == NULL) {
			herror("gethostbyname()");
			return (NULL);
		}
		(void)memcpy((char *)&target.sin_addr.s_addr, t_info->h_addr,
		    t_info->h_length);
	}
	if (connect(s, (struct sockaddr *)&target,
	    sizeof(struct sockaddr)) == -1)
		return (NULL);
	if ((ctx = SSL_CTX_new(SSLv23_client_method())) == NULL) {
		ERR_print_errors_fp(stderr);
		return (NULL);
	}
	if ((handle = SSL_new(ctx)) == NULL) {
		ERR_print_errors_fp(stderr);
		return (NULL);
	}
	if (SSL_set_fd(handle, s) == 0) {
		ERR_print_errors_fp(stderr);
		(void)close(s); SSL_free(handle);
		return (NULL);
	}

	if (!SSL_set_tlsext_host_name(handle, host))
		ERR_print_errors_fp(stderr);

	if (SSL_connect(handle) != 1) {
		ERR_print_errors_fp(stderr);
		(void)close(s); SSL_free(handle);
		return (NULL);
	}
	if ((cp = malloc(sizeof(ssl_conn_t))) == NULL) {
		warn("malloc()"); (void)close(s); SSL_free(handle);
		return (NULL);
	}
	cp->ctx	   = ctx;
	cp->sock   = s;
	cp->handle = handle;
	cp->lnbuf  = NULL;
	cp->state  = SSL_STATE_CONNECTED;
	cp->slen   = cp->bufsz = cp->rd = 0;
	cp->host   = strdup(host);
	if (cp->host == NULL) {
		warn("strdup()"); ssl_disconnect(cp);
		return (NULL);
	}
	return (cp);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x10, %rsp
movl %esi, %r14d
movq %rdi, %rbx
callq 0x2060
andl $0x0, (%rax)
cmpb $0x0, 0x8b63(%rip)      # 0xfc60
jne 0x711b
movl $0x200002, %edi         # imm = 0x200002
xorl %esi, %esi
callq 0x2230
xorl %edi, %edi
xorl %esi, %esi
callq 0x2230
movb $0x1, 0x8b45(%rip)      # 0xfc60
pushq $0x2
popq %rdi
pushq $0x1
popq %rsi
xorl %r15d, %r15d
xorl %edx, %edx
callq 0x20f0
cmpl $-0x1, %eax
je 0x72a6
movl %eax, %ebp
andq $0x0, 0x8(%rsp)
movw $0x2, (%rsp)
rolw $0x8, %r14w
movw %r14w, 0x2(%rsp)
movq %rbx, %rdi
callq 0x2290
movl %eax, 0x4(%rsp)
cmpl $-0x1, %eax
jne 0x7184
movq %rbx, %rdi
callq 0x2370
testq %rax, %rax
je 0x7297
leaq 0x4(%rsp), %rdi
movq 0x18(%rax), %rcx
movq (%rcx), %rsi
movslq 0x14(%rax), %rdx
callq 0x2200
movq %rsp, %rsi
pushq $0x10
popq %rdx
movl %ebp, %edi
callq 0x2210
cmpl $-0x1, %eax
je 0x72a3
callq 0x2190
movq %rax, %rdi
callq 0x2490
testq %rax, %rax
je 0x7266
movq %rax, %r12
movq %rax, %rdi
callq 0x22a0
testq %rax, %rax
je 0x7266
movq %rax, %r14
movq %rax, %rdi
movl %ebp, %esi
callq 0x21d0
testl %eax, %eax
je 0x7277
pushq $0x37
popq %rsi
movq %r14, %rdi
xorl %edx, %edx
movq %rbx, %rcx
callq 0x2130
testq %rax, %rax
jne 0x71fd
movq 0x7de3(%rip), %rax      # 0xefd8
movq (%rax), %rdi
callq 0x21b0
movq %r14, %rdi
callq 0x2480
cmpl $0x1, %eax
jne 0x7277
pushq $0x38
popq %rdi
callq 0x2430
testq %rax, %rax
je 0x72b6
movq %rax, %r15
movq %r12, 0x30(%rax)
movl %ebp, 0xc(%rax)
movq %r14, 0x28(%rax)
andq $0x0, 0x20(%rax)
movl $0x1, 0x10(%rax)
andq $0x0, (%rax)
andl $0x0, 0x8(%rax)
movq %rbx, %rdi
callq 0x2580
movq %rax, 0x18(%r15)
testq %rax, %rax
jne 0x72a6
leaq 0x343a(%rip), %rdi      # 0xa68f
xorl %eax, %eax
callq 0x2110
movq %r15, %rdi
callq 0x72d8
jmp 0x72a3
movq 0x7d6b(%rip), %rax      # 0xefd8
movq (%rax), %rdi
callq 0x21b0
jmp 0x72a3
movq 0x7d5a(%rip), %rax      # 0xefd8
movq (%rax), %rdi
callq 0x21b0
movl %ebp, %edi
callq 0x25d0
movq %r14, %rdi
callq 0x21c0
jmp 0x72a3
leaq 0x436c(%rip), %rdi      # 0xb60a
callq 0x23e0
xorl %r15d, %r15d
movq %r15, %rax
addq $0x10, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
leaq 0x33c9(%rip), %rdi      # 0xa686
xorl %r15d, %r15d
xorl %eax, %eax
callq 0x2110
movl %ebp, %edi
callq 0x25d0
movq %r14, %rdi
callq 0x21c0
jmp 0x72a6