// File: /workspace/github_c_makefile_star2_O2/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 0x67e8
testq %rax, %rax
je 0x3573
movq 0x8(%rbx), %rdi
movq %rax, %rsi
callq 0x51a1
cmpl $0x0, 0x40(%rbx)
jne 0x357d
jmp 0x3608
cmpl $0x0, 0x40(%rbx)
je 0x387f
movq 0x8(%rbx), %rdi
movl 0x30(%rbx), %esi
callq 0xbaa8
testq %rax, %rax
jne 0x3594
andl $0x0, 0x40(%rbx)
jmp 0x354a
movq %rax, %rdi
callq 0xbb46
movq 0x8(%rbx), %rdi
callq 0xb0b4
testq %rax, %rax
je 0x3a51
movq %rax, %r14
leaq 0x8e94(%rip), %rsi      # 0xc44c
movq %rax, %rdi
callq 0xb57d
leaq 0x8e8e(%rip), %r15      # 0xc455
movq %r14, %rdi
movq %r15, %rsi
callq 0xb6d6
leaq 0x4b5(%rip), %rsi       # 0x3a8e
movq %rbx, %rdi
movq %r15, %rdx
xorl %ecx, %ecx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x62ff
movq %rbx, %rdi
movq %r14, %rsi
callq 0x5084
movq %r14, %rdi
callq 0xb220
andl $0x0, 0x40(%rbx)
jmp 0x395e
movl 0x48(%rbx), %eax
testb $0x2, %al
jne 0x38b8
testb $0x1, %al
jne 0x396d
cmpl $0x1, 0x10(%rbx)
jne 0x395e
movq 0x8(%rbx), %rdi
callq 0xb0b4
testq %rax, %rax
je 0x3a51
movq %rax, %r14
leaq 0x89e5(%rip), %rsi      # 0xc026
movq %rax, %rdi
callq 0xb57d
leaq 0x980a(%rip), %rsi      # 0xce5a
movq %r14, %rdi
callq 0xb995
leaq 0x8e97(%rip), %rsi      # 0xc4f6
movq %r14, %rdi
callq 0xb986
movq 0x8(%rbx), %rdi
callq 0xb0b4
testq %rax, %rax
je 0x3a49
movq %rax, %r15
leaq 0x89b0(%rip), %rsi      # 0xc033
movq %rax, %rdi
callq 0xb57d
leaq 0x8e70(%rip), %rsi      # 0xc502
movq %r15, %rdi
callq 0xb6d6
movq %r14, %rdi
movq %r15, %rsi
callq 0xb6e5
movq %r15, %rdi
callq 0xb220
movq 0x8(%rbx), %rdi
callq 0xb0b4
testq %rax, %rax
je 0x3a49
movq %rax, %r12
leaq 0x8e48(%rip), %rsi      # 0xc511
movq %rax, %rdi
callq 0xb57d
movq %r15, %rdi
movq %r12, %rsi
callq 0xb6e5
movq %r12, %rdi
callq 0xb220
movq 0x8(%rbx), %rdi
callq 0xb0b4
testq %rax, %rax
je 0x3a49
movq %rax, %r13
movq 0x8(%rbx), %rdi
movq 0x78(%rbx), %rsi
callq 0x67e8
movq %rax, %rbp
movq %r13, %rdi
movq %rax, %rsi
callq 0xb711
movq 0x8(%rbx), %rdi
movq %rbp, %rsi
callq 0x51a1
movq %r12, %rdi
movq %r13, %rsi
callq 0xb6e5
movq %r13, %rdi
callq 0xb220
movq 0x8(%rbx), %rdi
callq 0xb0b4
testq %rax, %rax
je 0x3a49
movq %rax, %r12
leaq 0x8dcb(%rip), %rsi      # 0xc51a
movq %rax, %rdi
callq 0xb57d
movq %r15, %rdi
movq %r12, %rsi
callq 0xb6e5
movq %r12, %rdi
callq 0xb220
movq 0x8(%rbx), %rdi
callq 0xb0b4
testq %rax, %rax
je 0x3a49
movq %rax, %r13
movq 0x80(%rbx), %rsi
movq %rax, %rdi
callq 0xb711
movq %r12, %rdi
movq %r13, %rsi
callq 0xb6e5
movq %r13, %rdi
callq 0xb220
movq 0x8(%rbx), %rdi
callq 0xb0b4
testq %rax, %rax
je 0x3a49
movq %rax, %r12
leaq 0x8d82(%rip), %rsi      # 0xc53f
movq %rax, %rdi
callq 0xb57d
movq %r15, %rdi
movq %r12, %rsi
callq 0xb6e5
movq %r12, %rdi
callq 0xb220
movq 0x8(%rbx), %rdi
callq 0xb0b4
testq %rax, %rax
je 0x3a49
movq %rax, %r15
movq 0x8(%rbx), %rdi
movq 0x78(%rbx), %rsi
callq 0x68b3
testq %rax, %rax
je 0x3a67
movq %rax, %r13
movq %r15, %rdi
movq %rax, %rsi
callq 0xb711
movq 0x8(%rbx), %rdi
movq %r13, %rsi
callq 0x51a1
movq %r12, %rdi
movq %r15, %rsi
callq 0xb6e5
movq %r15, %rdi
callq 0xb220
leaq 0x59a(%rip), %rsi       # 0x3dd1
leaq 0x8cb8(%rip), %rdx      # 0xc4f6
movq %rbx, %rdi
xorl %ecx, %ecx
callq 0x6180
leaq 0x66c(%rip), %rsi       # 0x3ebb
movl $0x3a98, %edx           # imm = 0x3A98
movq %rbx, %rdi
xorl %ecx, %ecx
callq 0x608f
movq %rbx, %rdi
movq %r14, %rsi
callq 0x5084
movq %r14, %rdi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xb220
testb $0x4, 0x48(%rbx)
jne 0x390a
movq 0x8(%rbx), %rdi
leaq 0x8c78(%rip), %rsi      # 0xc50c
leaq 0x8c2b(%rip), %rdx      # 0xc4c6
xorl %eax, %eax
callq 0x52aa
movq %rbx, %rdi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x4e1e
leaq 0x8bf2(%rip), %rsi      # 0xc4b1
movq %rbx, %rdi
callq 0x3b76
testq %rax, %rax
je 0x3a51
movq %rax, %r14
leaq 0x3a7(%rip), %rsi       # 0x3c81
leaq 0x8b9f(%rip), %rdx      # 0xc480
movq %rbx, %rdi
xorl %ecx, %ecx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x62ff
movq %rbx, %rdi
movq %r14, %rsi
callq 0x5084
movq %r14, %rdi
callq 0xb220
andb $-0x3, 0x48(%rbx)
jmp 0x395e
leaq 0x8bab(%rip), %rsi      # 0xc4bc
movq %rbx, %rdi
callq 0x3b76
testq %rax, %rax
je 0x3a51
movq %rax, %r14
leaq 0x29d(%rip), %rsi       # 0x3bc9
leaq 0x8b4d(%rip), %rdx      # 0xc480
leaq 0x8b82(%rip), %r9       # 0xc4bc
movq %rbx, %rdi
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x62ff
movq %rbx, %rdi
movq %r14, %rsi
callq 0x5084
movq %r14, %rdi
callq 0xb220
andb $-0x5, 0x48(%rbx)
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x8b37(%rip), %rsi      # 0xc4ab
movq %rbx, %rdi
callq 0x3b76
testq %rax, %rax
je 0x3a51
movq %rax, %r14
movq 0x8(%rbx), %rdi
callq 0xb0b4
testq %rax, %rax
je 0x3a51
cmpl $0x1, 0x10(%rbx)
jne 0x3a51
movq 0x78(%rbx), %rsi
testq %rsi, %rsi
je 0x3a51
movq %rax, %r15
movq 0x8(%rbx), %rdi
callq 0x67e8
testq %rax, %rax
je 0x3a51
movq 0x8(%rbx), %rdi
movq 0x80(%rbx), %rdx
movq %rax, %rsi
callq 0x766c
testq %rax, %rax
je 0x3a51
movq %rax, %r12
movq %r15, %rdi
movq %rax, %rsi
callq 0xb711
movq 0x8(%rbx), %rdi
movq %r12, %rsi
callq 0x51a1
movq %r14, %rdi
movq %r15, %rsi
callq 0xb6e5
movq %r15, %rdi
callq 0xb220
leaq 0x1b7(%rip), %rsi       # 0x3bc9
leaq 0x8a67(%rip), %rdx      # 0xc480
leaq 0x8a8b(%rip), %r9       # 0xc4ab
movq %rbx, %rdi
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x62ff
movq %rbx, %rdi
movq %r14, %rsi
callq 0x5084
movq %r14, %rdi
callq 0xb220
andb $-0x2, 0x48(%rbx)
jmp 0x395e
movq %r14, %rdi
callq 0xb220
movq %rbx, %rdi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xbd7a
movq %r15, %rdi
callq 0xb220
movq %r14, %rdi
callq 0xb220
movq 0x8(%rbx), %rdi
leaq 0x8a8a(%rip), %rsi      # 0xc50c
leaq 0x8a9a(%rip), %rdx      # 0xc523
jmp 0x389b