// File: /workspace/github_c_makefile_star2_O2/xiaowenhuyuehu[P]remote_access/src/connectivity/ws_nopoll.c
// Function: ws_nopoll_connect
noPollConn *ws_nopoll_connect(const int debug_on, const char *host, const char *port, const char *get_url, const int tls_on, const char *ca_crt)
{
    noPollCtx  *ctx = NULL;
    noPollConn *conn = NULL;
	noPollConnOpts *opts = NULL;
    nopoll_bool debug = (nopoll_bool)debug_on;
    int i = 0;

    if (NULL == host || NULL == port) {
        log_debug("host or port is NULL ");
        return NULL;
    }

    /* create context */
    ctx = nopoll_ctx_new ();
    nopoll_log_enable (ctx, debug);
    nopoll_log_color_enable (ctx, debug);

    /* check connections registered */
    if (nopoll_ctx_conns(ctx) != 0) {
        log_debug("ERROR: expected to find 0 registered connections but found ");
        return NULL;
    }

    /* call to create a connection */
    if (1 == tls_on) {
    	opts = nopoll_conn_opts_new ();
    	if (NULL == ca_crt) {
            /* disable verification */
            log_debug("set peer verify false ");
            nopoll_conn_opts_ssl_peer_verify (opts, nopoll_false);
    	} else {
            nopoll_conn_opts_set_ssl_certs (opts, NULL, NULL, NULL, ca_crt);
    	}

        conn = nopoll_conn_tls_new (ctx, opts, host, port, NULL, get_url, NULL, NULL);
        if (!nopoll_conn_is_ok (conn)) {
            log_debug("ERROR: Expected to find proper client connection status, but found error.. ");
            nopoll_ctx_unref(ctx);
            return NULL;
        }

        if (!nopoll_conn_is_tls_on (conn)) {
            log_debug("ERROR: expected to find TLS enabled on the connection but found it isn't.. ");
            nopoll_ctx_unref(ctx);
            return NULL;
        }    
    } else {
        conn = nopoll_conn_new (ctx, host, port, NULL, get_url, NULL, NULL);
        if (!nopoll_conn_is_ok (conn)) {
            log_debug("ERROR: Expected to find proper client connection status, but found error.. ");
            nopoll_ctx_unref(ctx);
            return NULL;
        }
    }

    /* wait for ready */
    while (nopoll_true) {
        if (nopoll_conn_is_ready(conn)) {
            break;
        }
        i++;
        if (i > 100) {
            log_debug("ERROR: connection not ready ");
            nopoll_conn_close(conn);
            nopoll_ctx_unref(ctx);
            return NULL;
        }
        nopoll_sleep (100000);
    }

    return conn;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rdx, %r15
testq %rsi, %rsi
setne %al
testq %rdx, %rdx
setne %dl
testb %dl, %al
jne 0x2b56
leaq 0x54c3(%rip), %rsi      # 0x8004
leaq 0x54f8(%rip), %rdx      # 0x8040
leaq 0x555a(%rip), %r8       # 0x80a9
xorl %r14d, %r14d
pushq $0x5d
jmp 0x2bab
movq %r9, %r13
movl %r8d, %ebp
movq %rsi, %r12
movl %edi, %r14d
movq %rcx, 0x10(%rsp)
callq 0x24e0
movq %rax, %rbx
movq %rax, %rdi
movl %r14d, %esi
callq 0x22e0
movq %rbx, %rdi
movl %r14d, %esi
callq 0x2390
movq %rbx, %rdi
callq 0x2590
testl %eax, %eax
je 0x2bc7
leaq 0x546c(%rip), %rsi      # 0x8004
leaq 0x54a1(%rip), %rdx      # 0x8040
leaq 0x5519(%rip), %r8       # 0x80bf
xorl %r14d, %r14d
pushq $0x68
popq %rcx
xorl %edi, %edi
xorl %eax, %eax
callq 0x403f
movq %r14, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpl $0x1, %ebp
jne 0x2bec
callq 0x24f0
movq %rax, %rbp
testq %r13, %r13
je 0x2c40
movq %rbp, %rdi
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
movq %r13, %r8
callq 0x21f0
jmp 0x2c6b
andq $0x0, (%rsp)
movq %rbx, %rdi
movq %r12, %rsi
movq %r15, %rdx
xorl %ecx, %ecx
movq 0x10(%rsp), %r8
xorl %r9d, %r9d
callq 0x2050
movq %rax, %r14
movq %rax, %rdi
callq 0x2320
testl %eax, %eax
jne 0x2ca6
leaq 0x53e1(%rip), %rsi      # 0x8004
leaq 0x5416(%rip), %rdx      # 0x8040
leaq 0x54e1(%rip), %r8       # 0x8112
xorl %r14d, %r14d
xorl %edi, %edi
movl $0x86, %ecx
jmp 0x2d3e
leaq 0x53bd(%rip), %rsi      # 0x8004
leaq 0x53f2(%rip), %rdx      # 0x8040
leaq 0x54a6(%rip), %r8       # 0x80fb
pushq $0x71
popq %rcx
xorl %edi, %edi
xorl %eax, %eax
callq 0x403f
movq %rbp, %rdi
xorl %esi, %esi
callq 0x2260
xorps %xmm0, %xmm0
movups %xmm0, (%rsp)
movq %rbx, %rdi
movq %rbp, %rsi
movq %r12, %rdx
movq %r15, %rcx
xorl %r8d, %r8d
movq 0x10(%rsp), %r9
callq 0x2480
movq %rax, %r14
movq %rax, %rdi
callq 0x2320
testl %eax, %eax
je 0x2d05
movq %r14, %rdi
callq 0x21a0
testl %eax, %eax
je 0x2d21
pushq $0x64
popq %rbp
movq %r14, %rdi
callq 0x23f0
testl %eax, %eax
jne 0x2bb5
subl $0x1, %ebp
jb 0x2cca
movl $0x186a0, %edi          # imm = 0x186A0
callq 0x25b0
jmp 0x2ca9
leaq 0x5333(%rip), %rsi      # 0x8004
leaq 0x5368(%rip), %rdx      # 0x8040
leaq 0x54cb(%rip), %r8       # 0x81aa
xorl %edi, %edi
movl $0x93, %ecx
xorl %eax, %eax
callq 0x403f
movq %r14, %rdi
callq 0x2060
movq %rbx, %rdi
callq 0x25e0
xorl %r14d, %r14d
jmp 0x2bb5
leaq 0x52f8(%rip), %rsi      # 0x8004
leaq 0x532d(%rip), %rdx      # 0x8040
leaq 0x53f8(%rip), %r8       # 0x8112
xorl %r14d, %r14d
pushq $0x79
jmp 0x2d3b
leaq 0x52dc(%rip), %rsi      # 0x8004
leaq 0x5311(%rip), %rdx      # 0x8040
leaq 0x5428(%rip), %r8       # 0x815e
xorl %r14d, %r14d
pushq $0x7f
popq %rcx
xorl %edi, %edi
xorl %eax, %eax
callq 0x403f
movq %rbx, %rdi
callq 0x25e0
jmp 0x2bb5