// File: /workspace/github_c_makefile_star2_O3/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, 0x851c(%rip)      # 0xfc50
jne 0x7752
movl $0x200002, %edi         # imm = 0x200002
xorl %esi, %esi
callq 0x2230
xorl %edi, %edi
xorl %esi, %esi
callq 0x2230
movb $0x1, 0x84fe(%rip)      # 0xfc50
xorl %r15d, %r15d
movl $0x2, %edi
movl $0x1, %esi
xorl %edx, %edx
callq 0x20f0
cmpl $-0x1, %eax
je 0x78ef
movl %eax, %ebp
movq $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 0x77c2
movq %rbx, %rdi
callq 0x2360
testq %rax, %rax
je 0x78e0
leaq 0x4(%rsp), %rdi
movq 0x18(%rax), %rcx
movq (%rcx), %rsi
movslq 0x14(%rax), %rdx
callq 0x2200
movq %rsp, %rsi
movl %ebp, %edi
movl $0x10, %edx
callq 0x2210
cmpl $-0x1, %eax
je 0x78ec
callq 0x2190
movq %rax, %rdi
callq 0x2480
testq %rax, %rax
je 0x78af
movq %rax, %r12
movq %rax, %rdi
callq 0x22a0
testq %rax, %rax
je 0x78af
movq %rax, %r14
movq %rax, %rdi
movl %ebp, %esi
callq 0x21d0
testl %eax, %eax
je 0x78c0
movq %r14, %rdi
movl $0x37, %esi
xorl %edx, %edx
movq %rbx, %rcx
callq 0x2130
testq %rax, %rax
jne 0x783f
movq 0x77a1(%rip), %rax      # 0xefd8
movq (%rax), %rdi
callq 0x21b0
movq %r14, %rdi
callq 0x2470
cmpl $0x1, %eax
jne 0x78c0
movl $0x38, %edi
callq 0x2420
testq %rax, %rax
je 0x78ff
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 0x78ef
leaq 0x2df1(%rip), %rdi      # 0xa68f
xorl %eax, %eax
callq 0x2110
movq %r15, %rdi
callq 0x7921
jmp 0x78ec
movq 0x7722(%rip), %rax      # 0xefd8
movq (%rax), %rdi
callq 0x21b0
jmp 0x78ec
movq 0x7711(%rip), %rax      # 0xefd8
movq (%rax), %rdi
callq 0x21b0
movl %ebp, %edi
callq 0x25c0
movq %r14, %rdi
callq 0x21c0
jmp 0x78ec
leaq 0x3d23(%rip), %rdi      # 0xb60a
callq 0x23d0
xorl %r15d, %r15d
movq %r15, %rax
addq $0x10, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
leaq 0x2d80(%rip), %rdi      # 0xa686
xorl %r15d, %r15d
xorl %eax, %eax
callq 0x2110
movl %ebp, %edi
callq 0x25c0
movq %r14, %rdi
callq 0x21c0
jmp 0x78ef