// File: /workspace/github_c_makefile_star2_O1/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 0x6a51
movq %rax, %r14
testq %rax, %rax
je 0x35f3
movq 0x8(%rbx), %rdi
movq %r14, %rsi
callq 0x5293
cmpl $0x0, 0x40(%rbx)
je 0x3617
movq 0x8(%rbx), %rdi
movl 0x30(%rbx), %esi
callq 0xc004
testq %rax, %rax
jne 0x3897
movl $0x0, 0x40(%rbx)
jmp 0x35d2
movl 0x48(%rbx), %eax
testq %r14, %r14
je 0x390e
testb $0x2, %al
jne 0x3945
testb $0x1, %al
jne 0x39fa
cmpl $0x1, 0x10(%rbx)
jne 0x39eb
movq 0x8(%rbx), %rdi
callq 0xb5a0
testq %rax, %rax
je 0x3adf
movq %rax, %r14
leaq 0x99cd(%rip), %rsi      # 0xd026
movq %rax, %rdi
callq 0xbac6
leaq 0xa7f2(%rip), %rsi      # 0xde5a
movq %r14, %rdi
callq 0xbeea
leaq 0x9e7f(%rip), %rsi      # 0xd4f6
movq %r14, %rdi
callq 0xbedb
movq 0x8(%rbx), %rdi
callq 0xb5a0
testq %rax, %rax
je 0x3a47
movq %rax, %r15
leaq 0x9998(%rip), %rsi      # 0xd033
movq %rax, %rdi
callq 0xbac6
leaq 0x9e58(%rip), %rsi      # 0xd502
movq %r15, %rdi
callq 0xbc1d
movq %r14, %rdi
movq %r15, %rsi
callq 0xbc2c
movq %r15, %rdi
callq 0xb73c
movq 0x8(%rbx), %rdi
callq 0xb5a0
testq %rax, %rax
je 0x3a47
movq %rax, %r12
leaq 0x9e30(%rip), %rsi      # 0xd511
movq %rax, %rdi
callq 0xbac6
movq %r15, %rdi
movq %r12, %rsi
callq 0xbc2c
movq %r12, %rdi
callq 0xb73c
movq 0x8(%rbx), %rdi
callq 0xb5a0
testq %rax, %rax
je 0x3a47
movq %rax, %r13
movq 0x8(%rbx), %rdi
movq 0x78(%rbx), %rsi
callq 0x6a51
movq %rax, %rbp
movq %r13, %rdi
movq %rax, %rsi
callq 0xbc58
movq 0x8(%rbx), %rdi
movq %rbp, %rsi
callq 0x5293
movq %r12, %rdi
movq %r13, %rsi
callq 0xbc2c
movq %r13, %rdi
callq 0xb73c
movq 0x8(%rbx), %rdi
callq 0xb5a0
testq %rax, %rax
je 0x3a47
movq %rax, %r12
leaq 0x9db3(%rip), %rsi      # 0xd51a
movq %rax, %rdi
callq 0xbac6
movq %r15, %rdi
movq %r12, %rsi
callq 0xbc2c
movq %r12, %rdi
callq 0xb73c
movq 0x8(%rbx), %rdi
callq 0xb5a0
testq %rax, %rax
je 0x3a47
movq %rax, %r13
movq 0x80(%rbx), %rsi
movq %rax, %rdi
callq 0xbc58
movq %r12, %rdi
movq %r13, %rsi
callq 0xbc2c
movq %r13, %rdi
callq 0xb73c
movq 0x8(%rbx), %rdi
callq 0xb5a0
testq %rax, %rax
je 0x3a47
movq %rax, %r12
leaq 0x9d6a(%rip), %rsi      # 0xd53f
movq %rax, %rdi
callq 0xbac6
movq %r15, %rdi
movq %r12, %rsi
callq 0xbc2c
movq %r12, %rdi
callq 0xb73c
movq 0x8(%rbx), %rdi
callq 0xb5a0
testq %rax, %rax
je 0x3a47
movq %rax, %r15
movq 0x8(%rbx), %rdi
movq 0x78(%rbx), %rsi
callq 0x6b22
testq %rax, %rax
je 0x3af5
movq %rax, %r13
movq %r15, %rdi
movq %rax, %rsi
callq 0xbc58
movq 0x8(%rbx), %rdi
movq %r13, %rsi
callq 0x5293
movq %r12, %rdi
movq %r15, %rsi
callq 0xbc2c
movq %r15, %rdi
callq 0xb73c
leaq 0x613(%rip), %rsi       # 0x3e62
leaq 0x9ca0(%rip), %rdx      # 0xd4f6
movq %rbx, %rdi
xorl %ecx, %ecx
callq 0x633f
leaq 0x6e7(%rip), %rsi       # 0x3f4e
movl $0x3a98, %edx           # imm = 0x3A98
movq %rbx, %rdi
xorl %ecx, %ecx
callq 0x6244
movq %rbx, %rdi
movq %r14, %rsi
callq 0x5174
movq %r14, %rdi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xb73c
movq %rax, %rdi
callq 0xc0b3
movq 0x8(%rbx), %rdi
callq 0xb5a0
testq %rax, %rax
je 0x3adf
movq %rax, %r14
leaq 0x9b91(%rip), %rsi      # 0xd44c
movq %rax, %rdi
callq 0xbac6
leaq 0x9b8b(%rip), %r15      # 0xd455
movq %r14, %rdi
movq %r15, %rsi
callq 0xbc1d
leaq 0x240(%rip), %rsi       # 0x3b1c
movq %rbx, %rdi
movq %r15, %rdx
xorl %ecx, %ecx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x64cd
movq %rbx, %rdi
movq %r14, %rsi
callq 0x5174
movq %r14, %rdi
callq 0xb73c
movl $0x0, 0x40(%rbx)
jmp 0x39eb
testb $0x4, %al
jne 0x3997
movq 0x8(%rbx), %rdi
leaq 0x9beb(%rip), %rsi      # 0xd50c
leaq 0x9b9e(%rip), %rdx      # 0xd4c6
xorl %eax, %eax
callq 0x539c
movq %rbx, %rdi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x4f0c
leaq 0x9b65(%rip), %rsi      # 0xd4b1
movq %rbx, %rdi
callq 0x3c07
testq %rax, %rax
je 0x3adf
movq %rax, %r14
leaq 0x3ab(%rip), %rsi       # 0x3d12
leaq 0x9b12(%rip), %rdx      # 0xd480
movq %rbx, %rdi
xorl %ecx, %ecx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x64cd
movq %rbx, %rdi
movq %r14, %rsi
callq 0x5174
movq %r14, %rdi
callq 0xb73c
andb $-0x3, 0x48(%rbx)
jmp 0x39eb
leaq 0x9b1e(%rip), %rsi      # 0xd4bc
movq %rbx, %rdi
callq 0x3c07
testq %rax, %rax
je 0x3adf
movq %rax, %r14
leaq 0x2a1(%rip), %rsi       # 0x3c5a
leaq 0x9ac0(%rip), %rdx      # 0xd480
leaq 0x9af5(%rip), %r9       # 0xd4bc
movq %rbx, %rdi
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x64cd
movq %rbx, %rdi
movq %r14, %rsi
callq 0x5174
movq %r14, %rdi
callq 0xb73c
andb $-0x5, 0x48(%rbx)
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x9aaa(%rip), %rsi      # 0xd4ab
movq %rbx, %rdi
callq 0x3c07
testq %rax, %rax
je 0x3adf
movq %rax, %r14
movq 0x8(%rbx), %rdi
callq 0xb5a0
testq %rax, %rax
je 0x3adf
movq %rax, %r15
cmpl $0x1, 0x10(%rbx)
jne 0x3a54
movq 0x78(%rbx), %rsi
testq %rsi, %rsi
je 0x3a54
movq 0x8(%rbx), %rdi
callq 0x6a51
movq %rax, %rsi
jmp 0x3a56
movq %r14, %rdi
callq 0xb73c
jmp 0x3adf
xorl %esi, %esi
testq %rsi, %rsi
je 0x3adf
movq 0x8(%rbx), %rdi
movq 0x80(%rbx), %rdx
callq 0x7934
testq %rax, %rax
je 0x3adf
movq %rax, %r12
movq %r15, %rdi
movq %rax, %rsi
callq 0xbc58
movq 0x8(%rbx), %rdi
movq %r12, %rsi
callq 0x5293
movq %r14, %rdi
movq %r15, %rsi
callq 0xbc2c
movq %r15, %rdi
callq 0xb73c
leaq 0x1b2(%rip), %rsi       # 0x3c5a
leaq 0x99d1(%rip), %rdx      # 0xd480
leaq 0x99f5(%rip), %r9       # 0xd4ab
movq %rbx, %rdi
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x64cd
movq %rbx, %rdi
movq %r14, %rsi
callq 0x5174
movq %r14, %rdi
callq 0xb73c
andb $-0x2, 0x48(%rbx)
jmp 0x39eb
movq %rbx, %rdi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xc305
movq %r15, %rdi
callq 0xb73c
movq %r14, %rdi
callq 0xb73c
movq 0x8(%rbx), %rdi
leaq 0x99fc(%rip), %rsi      # 0xd50c
leaq 0x9a0c(%rip), %rdx      # 0xd523
jmp 0x3928