// File: /workspace/github_c_makefile_star2_O3/liuhaotian[P]xmppsh/libstrophe/src/event.c
// Function: xmpp_run_once
void xmpp_run_once(xmpp_ctx_t *ctx, const unsigned long timeout)
{
    xmpp_connlist_t *connitem;
    xmpp_conn_t *conn;
    fd_set rfds, wfds;
    sock_t max = 0;
    int ret;
    struct timeval tv;
    xmpp_send_queue_t *sq, *tsq;
    int towrite;
    char buf[4096];
    uint64_t next;
    long usec;
    int tls_read_bytes = 0;

    if (ctx->loop_status == XMPP_LOOP_QUIT) return;
    ctx->loop_status = XMPP_LOOP_RUNNING;

    /* send queued data */
    connitem = ctx->connlist;
    while (connitem) {
	conn = connitem->conn;
	if (conn->state != XMPP_STATE_CONNECTED) {
	    connitem = connitem->next;
	    continue;
	}

	/* if we're running tls, there may be some remaining data waiting to
	 * be sent, so push that out */
	if (conn->tls) {
	    ret = tls_clear_pending_write(conn->tls);

	    if (ret < 0 && !tls_is_recoverable(tls_error(conn->tls))) {
		/* an error occured */
		xmpp_debug(ctx, "xmpp", "Send error occured, disconnecting.");
		conn->error = ECONNABORTED;
		conn_disconnect(conn);
	    }
	}

	/* write all data from the send queue to the socket */
	sq = conn->send_queue_head;
	while (sq) {
	    towrite = sq->len - sq->written;

	    if (conn->tls) {
		ret = tls_write(conn->tls, &sq->data[sq->written], towrite);

		if (ret < 0 && !tls_is_recoverable(tls_error(conn->tls))) {
		    /* an error occured */
		    conn->error = tls_error(conn->tls);
		    break;
		} else if (ret < towrite) {
		    /* not all data could be sent now */
		    if (ret >= 0) sq->written += ret;
		    break;
		}

	    } else {
		ret = sock_write(conn->sock, &sq->data[sq->written], towrite);

		if (ret < 0 && !sock_is_recoverable(sock_error())) {
		    /* an error occured */
		    conn->error = sock_error();
		    break;
		} else if (ret < towrite) {
		    /* not all data could be sent now */
		    if (ret >= 0) sq->written += ret;
		    break;
		}
	    }

	    /* all data for this queue item written, delete and move on */
	    xmpp_free(ctx, sq->data);
	    tsq = sq;
	    sq = sq->next;
	    xmpp_free(ctx, tsq);

	    /* pop the top item */
	    conn->send_queue_head = sq;
	    /* if we've sent everything update the tail */
	    if (!sq) conn->send_queue_tail = NULL;
	}

	/* tear down connection on error */
	if (conn->error) {
	    /* FIXME: need to tear down send queues and random other things
	     * maybe this should be abstracted */
	    xmpp_debug(ctx, "xmpp", "Send error occured, disconnecting.");
	    conn->error = ECONNABORTED;
	    conn_disconnect(conn);
	}
	
	connitem = connitem->next;
    }

    /* reset parsers if needed */
    for (connitem = ctx->connlist; connitem; connitem = connitem->next) {
	if (connitem->conn->reset_parser)
	    conn_parser_reset(connitem->conn);
    }


    /* fire any ready timed handlers, then
       make sure we don't wait past the time when timed handlers need 
       to be called */
    next = handler_fire_timed(ctx);

    usec = ((next < timeout) ? next : timeout) * 1000;
    tv.tv_sec = usec / 1000000;
    tv.tv_usec = usec % 1000000;

    FD_ZERO(&rfds); 
    FD_ZERO(&wfds);

    /* find events to watch */
    connitem = ctx->connlist;
    while (connitem) {
	conn = connitem->conn;
	
	switch (conn->state) {
	case XMPP_STATE_CONNECTING:
	    /* connect has been called and we're waiting for it to complete */
	    /* connection will give us write or error events */
	    
	    /* make sure the timeout hasn't expired */
	    if (time_elapsed(conn->timeout_stamp, time_stamp()) <= 
		conn->connect_timeout)
		FD_SET(conn->sock, &wfds);
	    else {
		conn->error = ETIMEDOUT;
		xmpp_info(ctx, "xmpp", "Connection attempt timed out.");
		conn_disconnect(conn);
	    }
	    break;
	case XMPP_STATE_CONNECTED:
	    FD_SET(conn->sock, &rfds);
	    break;
	case XMPP_STATE_DISCONNECTED:
	    /* do nothing */
	default:
	    break;
	}
	
	/* Check if there is something in the SSL buffer. */
	if (conn->tls) {
	    tls_read_bytes += tls_pending(conn->tls);
	}
	
	if (conn->sock > max) max = conn->sock;

	connitem = connitem->next;
    }

    /* check for events */
    ret = select(max + 1, &rfds,  &wfds, NULL, &tv);

    /* select errored */
    if (ret < 0) {
	if (!sock_is_recoverable(sock_error()))
	    xmpp_error(ctx, "xmpp", "event watcher internal error %d", 
		       sock_error());
	return;
    }
    
    /* no events happened */
    if (ret == 0 && tls_read_bytes == 0) return;

    /* process events */
    connitem = ctx->connlist;
    while (connitem) {
	conn = connitem->conn;

	switch (conn->state) {
	case XMPP_STATE_CONNECTING:
	    if (FD_ISSET(conn->sock, &wfds)) {
		/* connection complete */

		/* check for error */
		if (sock_connect_error(conn->sock) != 0) {
		    /* connection failed */
		    xmpp_debug(ctx, "xmpp", "connection failed");
		    conn_disconnect(conn);
		    break;
		}

		conn->state = XMPP_STATE_CONNECTED;
		xmpp_debug(ctx, "xmpp", "connection successful");

		
		/* send stream init */
		conn_open_stream(conn);
	    }

	    break;
	case XMPP_STATE_CONNECTED:
	    if (FD_ISSET(conn->sock, &rfds) || (conn->tls && tls_pending(conn->tls))) {
		if (conn->tls) {
		    ret = tls_read(conn->tls, buf, 4096);
		} else {
		    ret = sock_read(conn->sock, buf, 4096);
		}

		if (ret > 0) {
		    ret = parser_feed(conn->parser, buf, ret);
		    if (!ret) {
			/* parse error, we need to shut down */
			/* FIXME */
			xmpp_debug(ctx, "xmpp", "parse error, disconnecting");
			conn_disconnect(conn);
		    }
		} else {
		    if (conn->tls) {
			if (!tls_is_recoverable(tls_error(conn->tls)))
			{
			    xmpp_debug(ctx, "xmpp", "Unrecoverable TLS error, %d.", tls_error(conn->tls));
			    conn->error = tls_error(conn->tls);
			    conn_disconnect(conn);
			}
		    } else {
			/* return of 0 means socket closed by server */
			xmpp_debug(ctx, "xmpp", "Socket closed by remote host.");
			conn->error = ECONNRESET;
			conn_disconnect(conn);
		    }
		}
	    }

	    break;
	case XMPP_STATE_DISCONNECTED:
	    /* do nothing */
	default:
	    break;
	}

	connitem = connitem->next;
    }

    /* fire any ready handlers */
    handler_fire_timed(ctx);
}

// Assembly:
cmpl $0x2, 0x10(%rdi)
je 0x5be9
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1128, %rsp           # imm = 0x1128
movq %rsi, %r15
movq %rdi, %rbx
movl $0x1, 0x10(%rdi)
movq 0x18(%rdi), %r12
testq %r12, %r12
je 0x57fa
movq %r15, 0x8(%rsp)
movq (%r12), %r13
cmpl $0x2, 0x14(%r13)
jne 0x57c4
movq 0x38(%r13), %rdi
testq %rdi, %rdi
je 0x56bf
callq 0xc0cb
testl %eax, %eax
jns 0x56bf
movq 0x38(%r13), %rdi
callq 0xbe74
movl %eax, %edi
callq 0xc04c
testl %eax, %eax
jne 0x56bf
movq %rbx, %rdi
leaq 0x79a8(%rip), %rsi      # 0xd049
leaq 0x85d8(%rip), %rdx      # 0xdc80
xorl %eax, %eax
callq 0x5516
movl $0x67, 0x20(%r13)
movq %r13, %rdi
callq 0x4e5c
movq 0xa8(%r13), %rbp
testq %rbp, %rbp
je 0x5795
movq 0x8(%rbp), %r14
movq 0x10(%rbp), %rsi
subq %rsi, %r14
movq 0x38(%r13), %rdi
testq %rdi, %rdi
je 0x570c
addq (%rbp), %rsi
movslq %r14d, %rdx
callq 0xc09f
movl %eax, %r15d
testl %eax, %eax
jns 0x5733
movq 0x38(%r13), %rdi
callq 0xbe74
movl %eax, %edi
callq 0xc04c
testl %eax, %eax
jne 0x5733
jmp 0x5781
movl 0x30(%r13), %edi
addq (%rbp), %rsi
movslq %r14d, %rdx
callq 0xaacc
movl %eax, %r15d
testl %eax, %eax
jns 0x5733
callq 0xa9b2
movl %eax, %edi
callq 0xaad3
testl %eax, %eax
je 0x578c
cmpl %r14d, %r15d
jl 0x5773
movq (%rbp), %rsi
movq %rbx, %rdi
callq 0x5263
movq 0x18(%rbp), %r14
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x5263
movq %r14, 0xa8(%r13)
movq %r14, %rbp
testq %r14, %r14
jne 0x56cf
movq $0x0, 0xb0(%r13)
jmp 0x5795
testl %r15d, %r15d
js 0x5795
movl %r15d, %eax
addq %rax, 0x10(%rbp)
jmp 0x5795
movq 0x38(%r13), %rdi
callq 0xbe74
jmp 0x5791
callq 0xa9b2
movl %eax, 0x20(%r13)
cmpl $0x0, 0x20(%r13)
je 0x57c4
movq %rbx, %rdi
leaq 0x78a3(%rip), %rsi      # 0xd049
leaq 0x84d3(%rip), %rdx      # 0xdc80
xorl %eax, %eax
callq 0x5516
movl $0x67, 0x20(%r13)
movq %r13, %rdi
callq 0x4e5c
movq 0x8(%r12), %r12
testq %r12, %r12
jne 0x5662
movq 0x18(%rbx), %r14
testq %r14, %r14
movq 0x8(%rsp), %r15
je 0x57fa
movq (%r14), %rdi
cmpl $0x0, 0xb8(%rdi)
je 0x57f1
callq 0x4ec7
movq 0x8(%r14), %r14
testq %r14, %r14
jne 0x57e0
movq %rbx, %rdi
callq 0x5eae
cmpq %r15, %rax
cmovbq %rax, %r15
imulq $0x3e8, %r15, %rcx      # imm = 0x3E8
movabsq $0x431bde82d7b634db, %rdx # imm = 0x431BDE82D7B634DB
movq %rcx, %rax
imulq %rdx
movq %rdx, %rax
shrq $0x3f, %rax
sarq $0x12, %rdx
addq %rax, %rdx
movq %rdx, 0x10(%rsp)
imulq $0xf4240, %rdx, %rax    # imm = 0xF4240
subq %rax, %rcx
movq %rcx, 0x18(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0xa0(%rsp)
movaps %xmm0, 0xb0(%rsp)
movaps %xmm0, 0xc0(%rsp)
movaps %xmm0, 0xd0(%rsp)
movaps %xmm0, 0xe0(%rsp)
movaps %xmm0, 0xf0(%rsp)
movaps %xmm0, 0x100(%rsp)
movaps %xmm0, 0x110(%rsp)
movaps %xmm0, 0x20(%rsp)
movaps %xmm0, 0x30(%rsp)
movaps %xmm0, 0x40(%rsp)
movaps %xmm0, 0x50(%rsp)
movaps %xmm0, 0x60(%rsp)
movaps %xmm0, 0x70(%rsp)
movaps %xmm0, 0x80(%rsp)
movaps %xmm0, 0x90(%rsp)
movq 0x18(%rbx), %r14
testq %r14, %r14
je 0x59ad
xorl %r15d, %r15d
xorl %ebp, %ebp
movq (%r14), %r12
movl 0x14(%r12), %eax
cmpl $0x2, %eax
je 0x5928
cmpl $0x1, %eax
jne 0x5979
movq 0x18(%r12), %r13
callq 0xc13f
movq %r13, %rdi
movq %rax, %rsi
callq 0xc17d
movl 0xc8(%r12), %ecx
cmpq %rcx, %rax
jbe 0x5953
movl $0x6e, 0x20(%r12)
movq %rbx, %rdi
leaq 0x7739(%rip), %rsi      # 0xd049
leaq 0x838c(%rip), %rdx      # 0xdca3
xorl %eax, %eax
callq 0x5488
movq %r12, %rdi
callq 0x4e5c
jmp 0x5979
movl 0x30(%r12), %ecx
leal 0x3f(%rcx), %eax
testl %ecx, %ecx
cmovnsl %ecx, %eax
movl %eax, %edx
andl $-0x40, %edx
subl %edx, %ecx
movl $0x1, %edx
shlq %cl, %rdx
sarl $0x6, %eax
cltq
orq %rdx, 0xa0(%rsp,%rax,8)
jmp 0x5979
movl 0x30(%r12), %ecx
leal 0x3f(%rcx), %eax
testl %ecx, %ecx
cmovnsl %ecx, %eax
movl %eax, %edx
andl $-0x40, %edx
subl %edx, %ecx
movl $0x1, %edx
shlq %cl, %rdx
sarl $0x6, %eax
cltq
orq %rdx, 0x20(%rsp,%rax,8)
movq 0x38(%r12), %rdi
testq %rdi, %rdi
je 0x598b
callq 0xc06a
addl %eax, %r15d
movl 0x30(%r12), %eax
cmpl %ebp, %eax
cmovgl %eax, %ebp
movq 0x8(%r14), %r14
testq %r14, %r14
jne 0x58c5
incl %ebp
testl %r15d, %r15d
sete %r14b
jmp 0x59b5
movb $0x1, %r14b
movl $0x1, %ebp
leaq 0xa0(%rsp), %rsi
leaq 0x20(%rsp), %rdx
leaq 0x10(%rsp), %r8
movl %ebp, %edi
xorl %ecx, %ecx
callq 0x2040
testl %eax, %eax
js 0x5b9f
sete %al
testb %r14b, %al
jne 0x5bd8
movq 0x18(%rbx), %rbp
testq %rbp, %rbp
je 0x5bd0
leaq 0x120(%rsp), %r14
leaq 0x7649(%rip), %r15      # 0xd049
leaq 0x82da(%rip), %r13      # 0xdce1
movq (%rbp), %r12
movl 0x14(%r12), %eax
cmpl $0x2, %eax
je 0x5a5a
cmpl $0x1, %eax
jne 0x5b6e
movl 0x30(%r12), %edi
leal 0x3f(%rdi), %eax
testl %edi, %edi
cmovnsl %edi, %eax
sarl $0x6, %eax
cltq
movq 0x20(%rsp,%rax,8), %rax
btq %rdi, %rax
jae 0x5b6e
callq 0xaae5
testl %eax, %eax
je 0x5b49
movq %rbx, %rdi
movq %r15, %rsi
movq %r13, %rdx
jmp 0x5aed
movl 0x30(%r12), %eax
leal 0x3f(%rax), %ecx
testl %eax, %eax
cmovnsl %eax, %ecx
sarl $0x6, %ecx
movslq %ecx, %rcx
movq 0xa0(%rsp,%rcx,8), %rcx
btq %rax, %rcx
jae 0x5a94
movq 0x38(%r12), %rdi
testq %rdi, %rdi
je 0x5ab0
movl $0x1000, %edx           # imm = 0x1000
movq %r14, %rsi
callq 0xc073
jmp 0x5ac2
movq 0x38(%r12), %rdi
testq %rdi, %rdi
je 0x5b6e
callq 0xc06a
testl %eax, %eax
jne 0x5a7b
jmp 0x5b6e
movl 0x30(%r12), %edi
movl $0x1000, %edx           # imm = 0x1000
movq %r14, %rsi
callq 0xaac5
testl %eax, %eax
jle 0x5afe
movq 0xc0(%r12), %rdi
movq %r14, %rsi
movl %eax, %edx
callq 0x77e3
testl %eax, %eax
jne 0x5b6e
movq %rbx, %rdi
movq %r15, %rsi
leaq 0x821c(%rip), %rdx      # 0xdd09
xorl %eax, %eax
callq 0x5516
movq %r12, %rdi
callq 0x4e5c
jmp 0x5b6e
movq 0x38(%r12), %rdi
testq %rdi, %rdi
je 0x5b7d
callq 0xbe74
movl %eax, %edi
callq 0xc04c
testl %eax, %eax
jne 0x5b6e
movq 0x38(%r12), %rdi
callq 0xbe74
movq %rbx, %rdi
movq %r15, %rsi
leaq 0x81f5(%rip), %rdx      # 0xdd24
movl %eax, %ecx
xorl %eax, %eax
callq 0x5516
movq 0x38(%r12), %rdi
callq 0xbe74
movl %eax, 0x20(%r12)
jmp 0x5af4
movl $0x2, 0x14(%r12)
movq %rbx, %rdi
movq %r15, %rsi
leaq 0x8194(%rip), %rdx      # 0xdcf3
xorl %eax, %eax
callq 0x5516
movq %r12, %rdi
callq 0x51ab
movq 0x8(%rbp), %rbp
testq %rbp, %rbp
jne 0x5a07
jmp 0x5bd0
movq %rbx, %rdi
movq %r15, %rsi
leaq 0x81b7(%rip), %rdx      # 0xdd41
xorl %eax, %eax
callq 0x5516
movl $0x68, 0x20(%r12)
jmp 0x5af4
callq 0xa9b2
movl %eax, %edi
callq 0xaad3
testl %eax, %eax
jne 0x5bd8
callq 0xa9b2
leaq 0x748e(%rip), %rsi      # 0xd049
leaq 0x80ff(%rip), %rdx      # 0xdcc1
movq %rbx, %rdi
movl %eax, %ecx
xorl %eax, %eax
callq 0x536c
jmp 0x5bd8
movq %rbx, %rdi
callq 0x5eae
addq $0x1128, %rsp           # imm = 0x1128
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq