// File: /workspace/github_c_makefile_star2_O2/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 0x5a84
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1128, %rsp           # imm = 0x1128
movq %rdi, %rbx
movq %rsi, 0x8(%rsp)
movl $0x1, 0x10(%rdi)
leaq 0x18(%rdi), %r15
movq %r15, (%rsp)
movq (%r15), %r15
testq %r15, %r15
je 0x56ff
movq (%r15), %r13
cmpl $0x2, 0x14(%r13)
jne 0x56f6
movq 0x38(%r13), %rdi
testq %rdi, %rdi
je 0x55ee
callq 0xbcce
testl %eax, %eax
jns 0x55ee
movq 0x38(%r13), %rdi
callq 0xbaa4
movl %eax, %edi
callq 0xbc51
testl %eax, %eax
jne 0x55ee
movq %rbx, %rdi
leaq 0x6a79(%rip), %rsi      # 0xc049
leaq 0x76a9(%rip), %rdx      # 0xcc80
xorl %eax, %eax
callq 0x544e
movl $0x67, 0x20(%r13)
movq %r13, %rdi
callq 0x4da8
movq 0xa8(%r13), %rbp
testq %rbp, %rbp
je 0x56c7
movq 0x8(%rbp), %r12
movq 0x10(%rbp), %rsi
subq %rsi, %r12
movq 0x38(%r13), %rdi
testq %rdi, %rdi
je 0x563b
addq (%rbp), %rsi
movslq %r12d, %rdx
callq 0xbca2
movl %eax, %r14d
testl %eax, %eax
jns 0x5662
movq 0x38(%r13), %rdi
callq 0xbaa4
movl %eax, %edi
callq 0xbc51
testl %eax, %eax
jne 0x5662
jmp 0x56b3
movl 0x30(%r13), %edi
addq (%rbp), %rsi
movslq %r12d, %rdx
callq 0xa843
movl %eax, %r14d
testl %eax, %eax
jns 0x5662
callq 0xa746
movl %eax, %edi
callq 0xa84a
testl %eax, %eax
je 0x56be
cmpl %r12d, %r14d
jl 0x56a5
movq (%rbp), %rsi
movq %rbx, %rdi
callq 0x51a1
movq 0x18(%rbp), %r14
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x51a1
movq %r14, 0xa8(%r13)
movq %r14, %rbp
testq %r14, %r14
jne 0x55f5
andq $0x0, 0xb0(%r13)
movq %r14, %rbp
jmp 0x55f5
testl %r14d, %r14d
js 0x56c7
movl %r14d, %eax
addq %rax, 0x10(%rbp)
jmp 0x56c7
movq 0x38(%r13), %rdi
callq 0xbaa4
jmp 0x56c3
callq 0xa746
movl %eax, 0x20(%r13)
cmpl $0x0, 0x20(%r13)
je 0x56f6
movq %rbx, %rdi
leaq 0x6971(%rip), %rsi      # 0xc049
leaq 0x75a1(%rip), %rdx      # 0xcc80
xorl %eax, %eax
callq 0x544e
movl $0x67, 0x20(%r13)
movq %r13, %rdi
callq 0x4da8
addq $0x8, %r15
jmp 0x5586
movq (%rsp), %r15
movq %r15, %r14
movq (%r14), %r14
testq %r14, %r14
je 0x5725
movq (%r14), %rdi
cmpl $0x0, 0xb8(%rdi)
je 0x571f
callq 0x4e0b
addq $0x8, %r14
jmp 0x5706
movq %rbx, %rdi
callq 0x5d54
movq 0x8(%rsp), %rcx
cmpq %rcx, %rax
cmovbq %rax, %rcx
imulq $0x3e8, %rcx, %rax      # imm = 0x3E8
movl $0xf4240, %ecx          # imm = 0xF4240
cqto
idivq %rcx
movq %rax, 0x10(%rsp)
movq %rdx, 0x18(%rsp)
xorl %eax, %eax
cmpq $0x10, %rax
je 0x576a
andq $0x0, 0xa0(%rsp,%rax,8)
incq %rax
jmp 0x5756
xorl %eax, %eax
cmpq $0x10, %rax
je 0x577d
andq $0x0, 0x20(%rsp,%rax,8)
incq %rax
jmp 0x576c
xorl %ebp, %ebp
movq %r15, %r14
xorl %r15d, %r15d
movq (%r14), %r14
testq %r14, %r14
je 0x585e
movq (%r14), %r12
movl 0x14(%r12), %eax
cmpl $0x2, %eax
je 0x57f4
cmpl $0x1, %eax
jne 0x5839
movq 0x18(%r12), %r13
callq 0xbd43
movq %r13, %rdi
movq %rax, %rsi
callq 0xbd73
movl 0xc8(%r12), %ecx
cmpq %rcx, %rax
jbe 0x5819
movl $0x6e, 0x20(%r12)
movq %rbx, %rdi
leaq 0x686d(%rip), %rsi      # 0xc049
leaq 0x74c0(%rip), %rdx      # 0xcca3
xorl %eax, %eax
callq 0x53c2
movq %r12, %rdi
callq 0x4da8
jmp 0x5839
movl 0x30(%r12), %ecx
movl %ecx, %eax
cltd
pushq $0x40
popq %rsi
idivl %esi
movslq %eax, %rdx
shll $0x6, %eax
subl %eax, %ecx
pushq $0x1
popq %rax
shlq %cl, %rax
orq %rax, 0xa0(%rsp,%rdx,8)
jmp 0x5839
movl 0x30(%r12), %ecx
movl %ecx, %eax
cltd
pushq $0x40
popq %rsi
idivl %esi
movslq %eax, %rdx
shll $0x6, %eax
subl %eax, %ecx
pushq $0x1
popq %rax
shlq %cl, %rax
orq %rax, 0x20(%rsp,%rdx,8)
movq 0x38(%r12), %rdi
testq %rdi, %rdi
je 0x584b
callq 0xbc6d
addl %eax, %r15d
movl 0x30(%r12), %eax
cmpl %ebp, %eax
cmovgl %eax, %ebp
addq $0x8, %r14
jmp 0x5785
incl %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 0x5a3a
orl %r15d, %eax
je 0x5a73
leaq 0x120(%rsp), %r13
leaq 0x67ae(%rip), %rbp      # 0xc049
leaq 0x7467(%rip), %r15      # 0xcd09
movq (%rsp), %r14
movq (%r14), %r14
testq %r14, %r14
je 0x5a6b
movq (%r14), %r12
movl 0x14(%r12), %eax
cmpl $0x2, %eax
je 0x5905
cmpl $0x1, %eax
jne 0x5a0f
movl 0x30(%r12), %edi
movl %edi, %eax
cltd
pushq $0x40
popq %rcx
idivl %ecx
cltq
movq 0x20(%rsp,%rax,8), %rax
btq %rdx, %rax
jae 0x5a0f
callq 0xa85c
testl %eax, %eax
je 0x59ea
movq %rbx, %rdi
movq %rbp, %rsi
leaq 0x73e1(%rip), %rdx      # 0xcce1
jmp 0x598e
movl 0x30(%r12), %eax
cltd
pushq $0x40
popq %rcx
idivl %ecx
cltq
movq 0xa0(%rsp,%rax,8), %rax
btq %rdx, %rax
jae 0x5939
movq 0x38(%r12), %rdi
testq %rdi, %rdi
je 0x5955
movl $0x1000, %edx           # imm = 0x1000
movq %r13, %rsi
callq 0xbc76
jmp 0x5967
movq 0x38(%r12), %rdi
testq %rdi, %rdi
je 0x5a0f
callq 0xbc6d
testl %eax, %eax
jne 0x5920
jmp 0x5a0f
movl 0x30(%r12), %edi
movl $0x1000, %edx           # imm = 0x1000
movq %r13, %rsi
callq 0xa83c
testl %eax, %eax
jle 0x599f
movq 0xc0(%r12), %rdi
movq %r13, %rsi
movl %eax, %edx
callq 0x761e
testl %eax, %eax
jne 0x5a0f
movq %rbx, %rdi
movq %rbp, %rsi
movq %r15, %rdx
xorl %eax, %eax
callq 0x544e
movq %r12, %rdi
callq 0x4da8
jmp 0x5a0f
movq 0x38(%r12), %rdi
testq %rdi, %rdi
je 0x5a18
callq 0xbaa4
movl %eax, %edi
callq 0xbc51
testl %eax, %eax
jne 0x5a0f
movq 0x38(%r12), %rdi
callq 0xbaa4
movq %rbx, %rdi
movq %rbp, %rsi
leaq 0x7354(%rip), %rdx      # 0xcd24
movl %eax, %ecx
xorl %eax, %eax
callq 0x544e
movq 0x38(%r12), %rdi
callq 0xbaa4
movl %eax, 0x20(%r12)
jmp 0x5995
movl $0x2, 0x14(%r12)
movq %rbx, %rdi
movq %rbp, %rsi
leaq 0x72f3(%rip), %rdx      # 0xccf3
xorl %eax, %eax
callq 0x544e
movq %r12, %rdi
callq 0x50ea
addq $0x8, %r14
jmp 0x58a6
movq %rbx, %rdi
movq %rbp, %rsi
leaq 0x731c(%rip), %rdx      # 0xcd41
xorl %eax, %eax
callq 0x544e
movl $0x68, 0x20(%r12)
jmp 0x5995
callq 0xa746
movl %eax, %edi
callq 0xa84a
testl %eax, %eax
jne 0x5a73
callq 0xa746
leaq 0x65f3(%rip), %rsi      # 0xc049
leaq 0x7264(%rip), %rdx      # 0xccc1
movq %rbx, %rdi
movl %eax, %ecx
xorl %eax, %eax
callq 0x52aa
jmp 0x5a73
movq %rbx, %rdi
callq 0x5d54
addq $0x1128, %rsp           # imm = 0x1128
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq