// File: /workspace/github_c_makefile_star2_O3/liuhaotian[P]xmppsh/libstrophe/src/auth.c
// Function: auth
static void _auth(xmpp_conn_t * const conn)
{
    xmpp_stanza_t *auth, *authdata, *query, *child, *iq;
    char *str, *authid;
    int anonjid;

    /* if there is no node in conn->jid, we assume anonymous connect */
    str = xmpp_jid_node(conn->ctx, conn->jid);
    if (str == NULL) {
	anonjid = 1;
    } else {
	xmpp_free(conn->ctx, str);
	anonjid = 0;
    }

    if (conn->tls_support)
    {
	tls_t *tls = tls_new(conn->ctx, conn->sock);

	/* If we couldn't init tls, it isn't there, so go on */
	if (!tls)
	{
	    conn->tls_support = 0;
	    _auth(conn);
	    return;
	}
	else
	{
	    tls_free(tls);
	}

	auth = _make_starttls(conn);

	if (!auth) {
	    disconnect_mem_error(conn);
	    return;
	}

	handler_add(conn, _handle_proceedtls_default, 
		    XMPP_NS_TLS, NULL, NULL, NULL);

	xmpp_send(conn, auth);
	xmpp_stanza_release(auth);

	/* TLS was tried, unset flag */
	conn->tls_support = 0;
    } else if (anonjid && conn->sasl_support & SASL_MASK_ANONYMOUS) {
	/* some crap here */
	auth = _make_sasl_auth(conn, "ANONYMOUS");
	if (!auth) {
	    disconnect_mem_error(conn);
	    return;
	}

	handler_add(conn, _handle_sasl_result, XMPP_NS_SASL,
	            NULL, NULL, "ANONYMOUS");

	xmpp_send(conn, auth);
	xmpp_stanza_release(auth);

	/* SASL ANONYMOUS was tried, unset flag */
	conn->sasl_support &= ~SASL_MASK_ANONYMOUS;
    } else if (anonjid) {
	xmpp_error(conn->ctx, "auth", 
		   "No node in JID, and SASL ANONYMOUS unsupported.");
	xmpp_disconnect(conn);
    } else if (conn->sasl_support & SASL_MASK_DIGESTMD5) {
	auth = _make_sasl_auth(conn, "DIGEST-MD5");
	if (!auth) {
	    disconnect_mem_error(conn);
	    return;

	}

	handler_add(conn, _handle_digestmd5_challenge, 
		    XMPP_NS_SASL, NULL, NULL, NULL);

	xmpp_send(conn, auth);
	xmpp_stanza_release(auth);

	/* SASL DIGEST-MD5 was tried, unset flag */
	conn->sasl_support &= ~SASL_MASK_DIGESTMD5;
    } else if (conn->sasl_support & SASL_MASK_PLAIN) {
	auth = _make_sasl_auth(conn, "PLAIN");
	if (!auth) {
	    disconnect_mem_error(conn);
	    return;
	}
	authdata = xmpp_stanza_new(conn->ctx);
	if (!authdata) {
	    disconnect_mem_error(conn);
	    return;
	}	
	authid = _get_authid(conn);
	if (!authid) {
	    disconnect_mem_error(conn);
	    return;
	}
	str = sasl_plain(conn->ctx, authid, conn->pass);
	if (!str) {
	    disconnect_mem_error(conn);
	    return;
	}
	xmpp_stanza_set_text(authdata, str);
	xmpp_free(conn->ctx, str);

	xmpp_stanza_add_child(auth, authdata);
	xmpp_stanza_release(authdata);

	handler_add(conn, _handle_sasl_result,
		    XMPP_NS_SASL, NULL, NULL, "PLAIN");

	xmpp_send(conn, auth);
	xmpp_stanza_release(auth);

	/* SASL PLAIN was tried */
	conn->sasl_support &= ~SASL_MASK_PLAIN;
    } else if (conn->type == XMPP_CLIENT) {
	/* legacy client authentication */
	
	iq = xmpp_stanza_new(conn->ctx);
	if (!iq) {
	    disconnect_mem_error(conn);
	    return;
	}
	xmpp_stanza_set_name(iq, "iq");
	xmpp_stanza_set_type(iq, "set");
	xmpp_stanza_set_id(iq, "_xmpp_auth1");

	query = xmpp_stanza_new(conn->ctx);
	if (!query) {
	    xmpp_stanza_release(iq);
	    disconnect_mem_error(conn);
	    return;
	}
	xmpp_stanza_set_name(query, "query");
	xmpp_stanza_set_ns(query, XMPP_NS_AUTH);
	xmpp_stanza_add_child(iq, query);
	xmpp_stanza_release(query);

	child = xmpp_stanza_new(conn->ctx);
	if (!child) {
	    xmpp_stanza_release(iq);
	    disconnect_mem_error(conn);
	    return;
	}
	xmpp_stanza_set_name(child, "username");
	xmpp_stanza_add_child(query, child);
	xmpp_stanza_release(child);

	authdata = xmpp_stanza_new(conn->ctx);
	if (!authdata) {
	    xmpp_stanza_release(iq);
	    disconnect_mem_error(conn);
	    return;
	}
	str = xmpp_jid_node(conn->ctx, conn->jid);
	xmpp_stanza_set_text(authdata, str);
	xmpp_free(conn->ctx, str);
	xmpp_stanza_add_child(child, authdata);
	xmpp_stanza_release(authdata);

	child = xmpp_stanza_new(conn->ctx);
	if (!child) {
	    xmpp_stanza_release(iq);
	    disconnect_mem_error(conn);
	    return;
	}
	xmpp_stanza_set_name(child, "password");
	xmpp_stanza_add_child(query, child);
	xmpp_stanza_release(child);

	authdata = xmpp_stanza_new(conn->ctx);
	if (!authdata) {
	    xmpp_stanza_release(iq);
	    disconnect_mem_error(conn);
	    return;
	}
	xmpp_stanza_set_text(authdata, conn->pass);
	xmpp_stanza_add_child(child, authdata);
	xmpp_stanza_release(authdata);

	child = xmpp_stanza_new(conn->ctx);
	if (!child) {
	    xmpp_stanza_release(iq);
	    disconnect_mem_error(conn);
	    return;
	}
	xmpp_stanza_set_name(child, "resource");
	xmpp_stanza_add_child(query, child);
	xmpp_stanza_release(child);

	authdata = xmpp_stanza_new(conn->ctx);
	if (!authdata) {
	    xmpp_stanza_release(iq);
	    disconnect_mem_error(conn);
	    return;
	}
	str = xmpp_jid_resource(conn->ctx, conn->jid);
	if (str) {
	    xmpp_stanza_set_text(authdata, str);
	    xmpp_free(conn->ctx, str);
	} else {
	    xmpp_stanza_release(authdata);
	    xmpp_stanza_release(iq);
	    xmpp_error(conn->ctx, "auth", 
		       "Cannot authenticate without resource");
	    xmpp_disconnect(conn);
	    return;
	}
	xmpp_stanza_add_child(child, authdata);
	xmpp_stanza_release(authdata);

	handler_add_id(conn, _handle_legacy, "_xmpp_auth1", NULL);
	handler_add_timed(conn, _handle_missing_legacy, 
			  LEGACY_TIMEOUT, NULL);

	xmpp_send(conn, iq);
	xmpp_stanza_release(iq);
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rdi, %rbx
movq 0x8(%rbx), %rdi
movq 0x78(%rbx), %rsi
callq 0x6a0e
testq %rax, %rax
je 0x35de
movq 0x8(%rbx), %rdi
movq %rax, %rsi
callq 0x5263
cmpl $0x0, 0x40(%rbx)
jne 0x35e8
jmp 0x3679
cmpl $0x0, 0x40(%rbx)
je 0x38f0
movq 0x8(%rbx), %rdi
movl 0x30(%rbx), %esi
callq 0xbe78
testq %rax, %rax
jne 0x3602
movl $0x0, 0x40(%rbx)
jmp 0x35b5
movq %rax, %rdi
callq 0xbf20
movq 0x8(%rbx), %rdi
callq 0xb40c
testq %rax, %rax
je 0x3abd
movq %rax, %r14
leaq 0x9e26(%rip), %rsi      # 0xd44c
movq %rax, %rdi
callq 0xb932
leaq 0x9e20(%rip), %r15      # 0xd455
movq %r14, %rdi
movq %r15, %rsi
callq 0xba89
leaq 0x4b3(%rip), %rsi       # 0x3afa
movq %rbx, %rdi
movq %r15, %rdx
xorl %ecx, %ecx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x648b
movq %rbx, %rdi
movq %r14, %rsi
callq 0x5145
movq %r14, %rdi
callq 0xb5a8
movl $0x0, 0x40(%rbx)
jmp 0x39c9
movl 0x48(%rbx), %eax
testb $0x2, %al
jne 0x3925
testb $0x1, %al
jne 0x39d8
cmpl $0x1, 0x10(%rbx)
jne 0x39c9
movq 0x8(%rbx), %rdi
callq 0xb40c
testq %rax, %rax
je 0x3abd
movq %rax, %r14
leaq 0x9974(%rip), %rsi      # 0xd026
movq %rax, %rdi
callq 0xb932
leaq 0xa799(%rip), %rsi      # 0xde5a
movq %r14, %rdi
callq 0xbd5d
leaq 0x9e26(%rip), %rsi      # 0xd4f6
movq %r14, %rdi
callq 0xbd4e
movq 0x8(%rbx), %rdi
callq 0xb40c
testq %rax, %rax
je 0x3ab5
movq %rax, %r15
leaq 0x993f(%rip), %rsi      # 0xd033
movq %rax, %rdi
callq 0xb932
leaq 0x9dff(%rip), %rsi      # 0xd502
movq %r15, %rdi
callq 0xba89
movq %r14, %rdi
movq %r15, %rsi
callq 0xba98
movq %r15, %rdi
callq 0xb5a8
movq 0x8(%rbx), %rdi
callq 0xb40c
testq %rax, %rax
je 0x3ab5
movq %rax, %r12
leaq 0x9dd7(%rip), %rsi      # 0xd511
movq %rax, %rdi
callq 0xb932
movq %r15, %rdi
movq %r12, %rsi
callq 0xba98
movq %r12, %rdi
callq 0xb5a8
movq 0x8(%rbx), %rdi
callq 0xb40c
testq %rax, %rax
je 0x3ab5
movq %rax, %r13
movq 0x8(%rbx), %rdi
movq 0x78(%rbx), %rsi
callq 0x6a0e
movq %rax, %rbp
movq %r13, %rdi
movq %rax, %rsi
callq 0xbac4
movq 0x8(%rbx), %rdi
movq %rbp, %rsi
callq 0x5263
movq %r12, %rdi
movq %r13, %rsi
callq 0xba98
movq %r13, %rdi
callq 0xb5a8
movq 0x8(%rbx), %rdi
callq 0xb40c
testq %rax, %rax
je 0x3ab5
movq %rax, %r12
leaq 0x9d5a(%rip), %rsi      # 0xd51a
movq %rax, %rdi
callq 0xb932
movq %r15, %rdi
movq %r12, %rsi
callq 0xba98
movq %r12, %rdi
callq 0xb5a8
movq 0x8(%rbx), %rdi
callq 0xb40c
testq %rax, %rax
je 0x3ab5
movq %rax, %r13
movq 0x80(%rbx), %rsi
movq %rax, %rdi
callq 0xbac4
movq %r12, %rdi
movq %r13, %rsi
callq 0xba98
movq %r13, %rdi
callq 0xb5a8
movq 0x8(%rbx), %rdi
callq 0xb40c
testq %rax, %rax
je 0x3ab5
movq %rax, %r12
leaq 0x9d11(%rip), %rsi      # 0xd53f
movq %rax, %rdi
callq 0xb932
movq %r15, %rdi
movq %r12, %rsi
callq 0xba98
movq %r12, %rdi
callq 0xb5a8
movq 0x8(%rbx), %rdi
callq 0xb40c
testq %rax, %rax
je 0x3ab5
movq %rax, %r15
movq 0x8(%rbx), %rdi
movq 0x78(%rbx), %rsi
callq 0x6adf
testq %rax, %rax
je 0x3ad3
movq %rax, %r13
movq %r15, %rdi
movq %rax, %rsi
callq 0xbac4
movq 0x8(%rbx), %rdi
movq %r13, %rsi
callq 0x5263
movq %r12, %rdi
movq %r15, %rsi
callq 0xba98
movq %r15, %rdi
callq 0xb5a8
leaq 0x594(%rip), %rsi       # 0x3e3c
leaq 0x9c47(%rip), %rdx      # 0xd4f6
movq %rbx, %rdi
xorl %ecx, %ecx
callq 0x62fd
leaq 0x666(%rip), %rsi       # 0x3f26
movl $0x3a98, %edx           # imm = 0x3A98
movq %rbx, %rdi
xorl %ecx, %ecx
callq 0x6202
movq %rbx, %rdi
movq %r14, %rsi
callq 0x5145
movq %r14, %rdi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xb5a8
testb $0x4, 0x48(%rbx)
movq 0x8(%rbx), %rdi
jne 0x3978
leaq 0x9c0b(%rip), %rsi      # 0xd50c
leaq 0x9bbe(%rip), %rdx      # 0xd4c6
xorl %eax, %eax
callq 0x536c
movq %rbx, %rdi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x4edd
movq 0x8(%rbx), %rdi
leaq 0x9b81(%rip), %rsi      # 0xd4b1
callq 0x3be5
testq %rax, %rax
je 0x3abd
movq %rax, %r14
leaq 0x3a4(%rip), %rsi       # 0x3cec
leaq 0x9b31(%rip), %rdx      # 0xd480
movq %rbx, %rdi
xorl %ecx, %ecx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x648b
movq %rbx, %rdi
movq %r14, %rsi
callq 0x5145
movq %r14, %rdi
callq 0xb5a8
andb $-0x3, 0x48(%rbx)
jmp 0x39c9
leaq 0x9b3d(%rip), %rsi      # 0xd4bc
callq 0x3be5
testq %rax, %rax
je 0x3abd
movq %rax, %r14
leaq 0x29d(%rip), %rsi       # 0x3c34
leaq 0x9ae2(%rip), %rdx      # 0xd480
leaq 0x9b17(%rip), %r9       # 0xd4bc
movq %rbx, %rdi
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x648b
movq %rbx, %rdi
movq %r14, %rsi
callq 0x5145
movq %r14, %rdi
callq 0xb5a8
andb $-0x5, 0x48(%rbx)
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x8(%rbx), %rdi
leaq 0x9ac8(%rip), %rsi      # 0xd4ab
callq 0x3be5
testq %rax, %rax
je 0x3abd
movq %rax, %r14
movq 0x8(%rbx), %rdi
callq 0xb40c
testq %rax, %rax
je 0x3abd
cmpl $0x1, 0x10(%rbx)
jne 0x3abd
movq 0x78(%rbx), %rsi
testq %rsi, %rsi
je 0x3abd
movq %rax, %r15
movq 0x8(%rbx), %rdi
callq 0x6a0e
testq %rax, %rax
je 0x3abd
movq 0x8(%rbx), %rdi
movq 0x80(%rbx), %rdx
movq %rax, %rsi
callq 0x7838
testq %rax, %rax
je 0x3abd
movq %rax, %r12
movq %r15, %rdi
movq %rax, %rsi
callq 0xbac4
movq 0x8(%rbx), %rdi
movq %r12, %rsi
callq 0x5263
movq %r14, %rdi
movq %r15, %rsi
callq 0xba98
movq %r15, %rdi
callq 0xb5a8
leaq 0x1b6(%rip), %rsi       # 0x3c34
leaq 0x99fb(%rip), %rdx      # 0xd480
leaq 0x9a1f(%rip), %r9       # 0xd4ab
movq %rbx, %rdi
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x648b
movq %rbx, %rdi
movq %r14, %rsi
callq 0x5145
movq %r14, %rdi
callq 0xb5a8
andb $-0x2, 0x48(%rbx)
jmp 0x39c9
movq %r14, %rdi
callq 0xb5a8
movq %rbx, %rdi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xc184
movq %r15, %rdi
callq 0xb5a8
movq %r14, %rdi
callq 0xb5a8
movq 0x8(%rbx), %rdi
leaq 0x9a1e(%rip), %rsi      # 0xd50c
leaq 0x9a2e(%rip), %rdx      # 0xd523
jmp 0x3908