// File: /workspace/github_c_makefile_star2_O1/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
movl $0x0, (%rax)
cmpb $0x0, 0x8404(%rip)      # 0xfc50
jne 0x786a
movl $0x200002, %edi         # imm = 0x200002
xorl %esi, %esi
callq 0x2230
xorl %edi, %edi
xorl %esi, %esi
callq 0x2230
movb $0x1, 0x83e6(%rip)      # 0xfc50
xorl %r15d, %r15d
movl $0x2, %edi
movl $0x1, %esi
xorl %edx, %edx
callq 0x20f0
cmpl $-0x1, %eax
je 0x7a0d
movl %eax, %ebp
xorps %xmm0, %xmm0
movaps %xmm0, (%rsp)
movw $0x2, (%rsp)
rolw $0x8, %r14w
movw %r14w, 0x2(%rsp)
movq %rbx, %rdi
callq 0x2290
movl %eax, 0x4(%rsp)
cmpl $-0x1, %eax
jne 0x78ee
movq %rbx, %rdi
callq 0x2360
movq %rax, %r14
testq %rax, %rax
je 0x78d9
leaq 0x4(%rsp), %rdi
movq 0x18(%r14), %rax
movq (%rax), %rsi
movslq 0x14(%r14), %rdx
callq 0x2200
jmp 0x78e5
leaq 0x3d4a(%rip), %rdi      # 0xb62a
callq 0x23d0
testq %r14, %r14
je 0x7a0a
movq %rsp, %rsi
movl %ebp, %edi
movl $0x10, %edx
callq 0x2210
cmpl $-0x1, %eax
je 0x7a0a
callq 0x2190
movq %rax, %rdi
callq 0x2480
testq %rax, %rax
je 0x79db
movq %rax, %r12
movq %rax, %rdi
callq 0x22a0
testq %rax, %rax
je 0x79db
movq %rax, %r14
movq %rax, %rdi
movl %ebp, %esi
callq 0x21d0
testl %eax, %eax
je 0x79ec
movq %r14, %rdi
movl $0x37, %esi
xorl %edx, %edx
movq %rbx, %rcx
callq 0x2130
testq %rax, %rax
jne 0x796b
movq 0x7675(%rip), %rax      # 0xefd8
movq (%rax), %rdi
callq 0x21b0
movq %r14, %rdi
callq 0x2470
cmpl $0x1, %eax
jne 0x79ec
movl $0x38, %edi
callq 0x2420
testq %rax, %rax
je 0x7a1d
movq %rax, %r15
movq %r12, 0x30(%rax)
movl %ebp, 0xc(%rax)
movq %r14, 0x28(%rax)
xorl %eax, %eax
movq %rax, 0x20(%r15)
movl $0x1, 0x10(%r15)
movq %rax, (%r15)
movl $0x0, 0x8(%r15)
movq %rbx, %rdi
callq 0x2570
movq %rax, 0x18(%r15)
testq %rax, %rax
jne 0x7a0d
leaq 0x2ce1(%rip), %rdi      # 0xa6ab
xorl %eax, %eax
callq 0x2110
movq %r15, %rdi
callq 0x7a3f
jmp 0x7a0a
movq 0x75f6(%rip), %rax      # 0xefd8
movq (%rax), %rdi
callq 0x21b0
jmp 0x7a0a
movq 0x75e5(%rip), %rax      # 0xefd8
movq (%rax), %rdi
callq 0x21b0
movl %ebp, %edi
callq 0x25c0
movq %r14, %rdi
callq 0x21c0
xorl %r15d, %r15d
movq %r15, %rax
addq $0x10, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
leaq 0x2c7e(%rip), %rdi      # 0xa6a2
xorl %r15d, %r15d
xorl %eax, %eax
callq 0x2110
movl %ebp, %edi
callq 0x25c0
movq %r14, %rdi
callq 0x21c0
jmp 0x7a0d